1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_WALLET_WALLETDB_H
7 #define BITCOIN_WALLET_WALLETDB_H
10 #include "primitives/transaction.h"
11 #include "wallet/db.h"
20 static const bool DEFAULT_FLUSHWALLET
= true;
23 class CAccountingEntry
;
33 /** Error statuses for the wallet database */
44 /* simple HD chain data model */
48 uint32_t nExternalChainCounter
;
49 CKeyID masterKeyID
; //!< master key hash160
51 static const int CURRENT_VERSION
= 1;
54 CHDChain() { SetNull(); }
55 ADD_SERIALIZE_METHODS
;
56 template <typename Stream
, typename Operation
>
57 inline void SerializationOp(Stream
& s
, Operation ser_action
)
59 READWRITE(this->nVersion
);
60 READWRITE(nExternalChainCounter
);
61 READWRITE(masterKeyID
);
66 nVersion
= CHDChain::CURRENT_VERSION
;
67 nExternalChainCounter
= 0;
68 masterKeyID
.SetNull();
75 static const int VERSION_BASIC
=1;
76 static const int VERSION_WITH_HDDATA
=10;
77 static const int CURRENT_VERSION
=VERSION_WITH_HDDATA
;
79 int64_t nCreateTime
; // 0 means unknown
80 std::string hdKeypath
; //optional HD/bip32 keypath
81 CKeyID hdMasterKeyID
; //id of the HD masterkey used to derive this key
87 CKeyMetadata(int64_t nCreateTime_
)
90 nCreateTime
= nCreateTime_
;
93 ADD_SERIALIZE_METHODS
;
95 template <typename Stream
, typename Operation
>
96 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
97 READWRITE(this->nVersion
);
98 READWRITE(nCreateTime
);
99 if (this->nVersion
>= VERSION_WITH_HDDATA
)
101 READWRITE(hdKeypath
);
102 READWRITE(hdMasterKeyID
);
108 nVersion
= CKeyMetadata::CURRENT_VERSION
;
111 hdMasterKeyID
.SetNull();
115 /** Access to the wallet database */
116 class CWalletDB
: public CDB
119 CWalletDB(const std::string
& strFilename
, const char* pszMode
= "r+", bool fFlushOnClose
= true) : CDB(strFilename
, pszMode
, fFlushOnClose
)
123 bool WriteName(const std::string
& strAddress
, const std::string
& strName
);
124 bool EraseName(const std::string
& strAddress
);
126 bool WritePurpose(const std::string
& strAddress
, const std::string
& purpose
);
127 bool ErasePurpose(const std::string
& strAddress
);
129 bool WriteTx(const CWalletTx
& wtx
);
130 bool EraseTx(uint256 hash
);
132 bool WriteKey(const CPubKey
& vchPubKey
, const CPrivKey
& vchPrivKey
, const CKeyMetadata
&keyMeta
);
133 bool WriteCryptedKey(const CPubKey
& vchPubKey
, const std::vector
<unsigned char>& vchCryptedSecret
, const CKeyMetadata
&keyMeta
);
134 bool WriteMasterKey(unsigned int nID
, const CMasterKey
& kMasterKey
);
136 bool WriteCScript(const uint160
& hash
, const CScript
& redeemScript
);
138 bool WriteWatchOnly(const CScript
&script
);
139 bool EraseWatchOnly(const CScript
&script
);
141 bool WriteBestBlock(const CBlockLocator
& locator
);
142 bool ReadBestBlock(CBlockLocator
& locator
);
144 bool WriteOrderPosNext(int64_t nOrderPosNext
);
146 bool WriteDefaultKey(const CPubKey
& vchPubKey
);
148 bool ReadPool(int64_t nPool
, CKeyPool
& keypool
);
149 bool WritePool(int64_t nPool
, const CKeyPool
& keypool
);
150 bool ErasePool(int64_t nPool
);
152 bool WriteMinVersion(int nVersion
);
154 /// This writes directly to the database, and will not update the CWallet's cached accounting entries!
155 /// Use wallet.AddAccountingEntry instead, to write *and* update its caches.
156 bool WriteAccountingEntry(const uint64_t nAccEntryNum
, const CAccountingEntry
& acentry
);
157 bool WriteAccountingEntry_Backend(const CAccountingEntry
& acentry
);
158 bool ReadAccount(const std::string
& strAccount
, CAccount
& account
);
159 bool WriteAccount(const std::string
& strAccount
, const CAccount
& account
);
161 /// Write destination data key,value tuple to database
162 bool WriteDestData(const std::string
&address
, const std::string
&key
, const std::string
&value
);
163 /// Erase destination data tuple from wallet database
164 bool EraseDestData(const std::string
&address
, const std::string
&key
);
166 CAmount
GetAccountCreditDebit(const std::string
& strAccount
);
167 void ListAccountCreditDebit(const std::string
& strAccount
, std::list
<CAccountingEntry
>& acentries
);
169 DBErrors
LoadWallet(CWallet
* pwallet
);
170 DBErrors
FindWalletTx(CWallet
* pwallet
, std::vector
<uint256
>& vTxHash
, std::vector
<CWalletTx
>& vWtx
);
171 DBErrors
ZapWalletTx(CWallet
* pwallet
, std::vector
<CWalletTx
>& vWtx
);
172 DBErrors
ZapSelectTx(CWallet
* pwallet
, std::vector
<uint256
>& vHashIn
, std::vector
<uint256
>& vHashOut
);
173 static bool Recover(CDBEnv
& dbenv
, const std::string
& filename
, bool fOnlyKeys
);
174 static bool Recover(CDBEnv
& dbenv
, const std::string
& filename
);
176 //! write the hdchain model (external chain child index counter)
177 bool WriteHDChain(const CHDChain
& chain
);
180 CWalletDB(const CWalletDB
&);
181 void operator=(const CWalletDB
&);
185 void ThreadFlushWalletDB(const std::string
& strFile
);
187 #endif // BITCOIN_WALLET_WALLETDB_H