Bitcoin ABC  0.26.3
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2020 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_WALLET_WALLETDB_H
8 #define BITCOIN_WALLET_WALLETDB_H
9 
10 #include <key.h>
11 #include <script/sign.h>
12 #include <script/standard.h> // for CTxDestination
13 #include <wallet/bdb.h>
14 #include <wallet/walletutil.h>
15 
16 #include <cstdint>
17 #include <string>
18 #include <vector>
19 
33 static const bool DEFAULT_FLUSHWALLET = true;
34 
35 struct CBlockLocator;
36 class CKeyPool;
37 class CMasterKey;
38 class CScript;
39 class CWallet;
40 class CWalletTx;
41 class uint160;
42 class uint256;
43 
45 enum class DBErrors {
46  LOAD_OK,
47  CORRUPT,
49  TOO_NEW,
50  LOAD_FAIL,
52 };
53 
54 namespace DBKeys {
55 extern const std::string ACENTRY;
56 extern const std::string ACTIVEEXTERNALSPK;
57 extern const std::string ACTIVEINTERNALSPK;
58 extern const std::string BESTBLOCK;
59 extern const std::string BESTBLOCK_NOMERKLE;
60 extern const std::string CRYPTED_KEY;
61 extern const std::string CSCRIPT;
62 extern const std::string DEFAULTKEY;
63 extern const std::string DESTDATA;
64 extern const std::string FLAGS;
65 extern const std::string HDCHAIN;
66 extern const std::string KEY;
67 extern const std::string KEYMETA;
68 extern const std::string MASTER_KEY;
69 extern const std::string MINVERSION;
70 extern const std::string NAME;
71 extern const std::string OLD_KEY;
72 extern const std::string ORDERPOSNEXT;
73 extern const std::string POOL;
74 extern const std::string PURPOSE;
75 extern const std::string SETTINGS;
76 extern const std::string TX;
77 extern const std::string VERSION;
78 extern const std::string WALLETDESCRIPTOR;
79 extern const std::string WALLETDESCRIPTORCACHE;
80 extern const std::string WALLETDESCRIPTORCKEY;
81 extern const std::string WALLETDESCRIPTORKEY;
82 extern const std::string WATCHMETA;
83 extern const std::string WATCHS;
84 } // namespace DBKeys
85 
86 /* simple HD chain data model */
87 class CHDChain {
88 public:
93 
94  static const int VERSION_HD_BASE = 1;
95  static const int VERSION_HD_CHAIN_SPLIT = 2;
97  int nVersion;
98 
99  CHDChain() { SetNull(); }
100 
102  READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
103  if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
104  READWRITE(obj.nInternalChainCounter);
105  }
106  }
107 
108  void SetNull() {
112  seed_id.SetNull();
113  }
114 
115  bool operator==(const CHDChain &chain) const {
116  return seed_id == chain.seed_id;
117  }
118 };
119 
121 public:
122  static const int VERSION_BASIC = 1;
123  static const int VERSION_WITH_HDDATA = 10;
124  static const int VERSION_WITH_KEY_ORIGIN = 12;
126  int nVersion;
127  // 0 means unknown.
128  int64_t nCreateTime;
129  // optional HD/bip32 keypath. Still used to determine whether a key is a
130  // seed. Also kept for backwards compatibility
131  std::string hdKeypath;
132  // Id of the HD seed used to derive this key.
134  // Key origin info with path and fingerprint
137  bool has_key_origin = false;
138 
140  explicit CKeyMetadata(int64_t nCreateTime_) {
141  SetNull();
142  nCreateTime = nCreateTime_;
143  }
144 
146  READWRITE(obj.nVersion, obj.nCreateTime);
147  if (obj.nVersion >= VERSION_WITH_HDDATA) {
148  READWRITE(obj.hdKeypath, obj.hd_seed_id);
149  }
150  if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN) {
151  READWRITE(obj.key_origin);
152  READWRITE(obj.has_key_origin);
153  }
154  }
155 
156  void SetNull() {
158  nCreateTime = 0;
159  hdKeypath.clear();
161  key_origin.clear();
162  has_key_origin = false;
163  }
164 };
165 
175 class WalletBatch {
176 private:
177  template <typename K, typename T>
178  bool WriteIC(const K &key, const T &value, bool fOverwrite = true) {
179  if (!m_batch->Write(key, value, fOverwrite)) {
180  return false;
181  }
183  if (m_database.nUpdateCounter % 1000 == 0) {
184  m_batch->Flush();
185  }
186  return true;
187  }
188 
189  template <typename K> bool EraseIC(const K &key) {
190  if (!m_batch->Erase(key)) {
191  return false;
192  }
194  if (m_database.nUpdateCounter % 1000 == 0) {
195  m_batch->Flush();
196  }
197  return true;
198  }
199 
200 public:
201  explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true)
202  : m_batch(database.MakeBatch(_fFlushOnClose)), m_database(database) {}
203  WalletBatch(const WalletBatch &) = delete;
204  WalletBatch &operator=(const WalletBatch &) = delete;
205 
206  bool WriteName(const CTxDestination &address, const std::string &strName);
207  bool EraseName(const CTxDestination &address);
208 
209  bool WritePurpose(const CTxDestination &address,
210  const std::string &purpose);
211  bool ErasePurpose(const CTxDestination &address);
212 
213  bool WriteTx(const CWalletTx &wtx);
214  bool EraseTx(uint256 hash);
215 
216  bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey,
217  const bool overwrite);
218  bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey,
219  const CKeyMetadata &keyMeta);
220  bool WriteCryptedKey(const CPubKey &vchPubKey,
221  const std::vector<uint8_t> &vchCryptedSecret,
222  const CKeyMetadata &keyMeta);
223  bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey);
224 
225  bool WriteCScript(const uint160 &hash, const CScript &redeemScript);
226 
227  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
228  bool EraseWatchOnly(const CScript &script);
229 
230  bool WriteBestBlock(const CBlockLocator &locator);
231  bool ReadBestBlock(CBlockLocator &locator);
232 
233  bool WriteOrderPosNext(int64_t nOrderPosNext);
234 
235  bool ReadPool(int64_t nPool, CKeyPool &keypool);
236  bool WritePool(int64_t nPool, const CKeyPool &keypool);
237  bool ErasePool(int64_t nPool);
238 
239  bool WriteMinVersion(int nVersion);
240 
241  bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey,
242  const CPrivKey &privkey);
243  bool WriteCryptedDescriptorKey(const uint256 &desc_id,
244  const CPubKey &pubkey,
245  const std::vector<uint8_t> &secret);
246  bool WriteDescriptor(const uint256 &desc_id,
247  const WalletDescriptor &descriptor);
248  bool WriteDescriptorDerivedCache(const CExtPubKey &xpub,
249  const uint256 &desc_id,
250  uint32_t key_exp_index,
251  uint32_t der_index);
252  bool WriteDescriptorParentCache(const CExtPubKey &xpub,
253  const uint256 &desc_id,
254  uint32_t key_exp_index);
255 
257  bool WriteDestData(const CTxDestination &address, const std::string &key,
258  const std::string &value);
260  bool EraseDestData(const CTxDestination &address, const std::string &key);
261 
262  bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id,
263  bool internal);
264 
265  DBErrors LoadWallet(CWallet *pwallet);
266  DBErrors FindWalletTx(std::vector<TxId> &txIds, std::list<CWalletTx> &vWtx);
267  DBErrors ZapSelectTx(std::vector<TxId> &txIdsIn,
268  std::vector<TxId> &txIdsOut);
269  /* Function to determine if a certain KV/key-type is a key (cryptographical
270  * key) type */
271  static bool IsKeyType(const std::string &strType);
272 
274  bool WriteHDChain(const CHDChain &chain);
275 
276  bool WriteWalletFlags(const uint64_t flags);
278  bool TxnBegin();
280  bool TxnCommit();
282  bool TxnAbort();
283 
284 private:
285  std::unique_ptr<DatabaseBatch> m_batch;
287 };
288 
291 void MaybeCompactWalletDB();
292 
294 using KeyFilterFn = std::function<bool(const std::string &)>;
295 
297 bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, CDataStream &ssValue,
298  std::string &strType, std::string &strErr,
299  const KeyFilterFn &filter_fn = nullptr);
300 
304 std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
305 
307 std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
308 
309 #endif // BITCOIN_WALLET_WALLETDB_H
int flags
Definition: bitcoin-tx.cpp:534
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
static const int VERSION_HD_BASE
Definition: walletdb.h:94
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:101
uint32_t nInternalChainCounter
Definition: walletdb.h:90
CHDChain()
Definition: walletdb.h:99
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:115
CKeyID seed_id
seed hash160
Definition: walletdb.h:92
void SetNull()
Definition: walletdb.h:108
static const int VERSION_HD_CHAIN_SPLIT
Definition: walletdb.h:95
int nVersion
Definition: walletdb.h:97
static const int CURRENT_VERSION
Definition: walletdb.h:96
uint32_t nExternalChainCounter
Definition: walletdb.h:89
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
static const int VERSION_WITH_KEY_ORIGIN
Definition: walletdb.h:124
KeyOriginInfo key_origin
Definition: walletdb.h:135
bool has_key_origin
Whether the key_origin is useful.
Definition: walletdb.h:137
static const int VERSION_BASIC
Definition: walletdb.h:122
int nVersion
Definition: walletdb.h:126
static const int CURRENT_VERSION
Definition: walletdb.h:125
void SetNull()
Definition: walletdb.h:156
std::string hdKeypath
Definition: walletdb.h:131
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:145
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:140
int64_t nCreateTime
Definition: walletdb.h:128
CKeyID hd_seed_id
Definition: walletdb.h:133
static const int VERSION_WITH_HDDATA
Definition: walletdb.h:123
A key from a CWallet's keypool.
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:31
An encapsulated public key.
Definition: pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:253
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:65
Access to the wallet database.
Definition: walletdb.h:175
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:1106
bool WriteDescriptor(const uint256 &desc_id, const WalletDescriptor &descriptor)
Definition: walletdb.cpp:247
bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256 &id, bool internal)
Definition: walletdb.cpp:213
bool WriteDescriptorDerivedCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index, uint32_t der_index)
Definition: walletdb.cpp:252
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:154
bool WriteName(const CTxDestination &address, const std::string &strName)
Definition: walletdb.cpp:60
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:205
WalletBatch(const WalletBatch &)=delete
bool WritePurpose(const CTxDestination &address, const std::string &purpose)
Definition: walletdb.cpp:81
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:209
bool WriteDescriptorParentCache(const CExtPubKey &xpub, const uint256 &desc_id, uint32_t key_exp_index)
Definition: walletdb.cpp:264
static bool IsKeyType(const std::string &strType)
Definition: walletdb.cpp:763
bool ErasePurpose(const CTxDestination &address)
Definition: walletdb.cpp:91
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:159
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:172
bool EraseDestData(const CTxDestination &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1084
bool WriteDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const CPrivKey &privkey)
Definition: walletdb.cpp:220
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:197
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:1098
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:178
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:113
std::unique_ptr< DatabaseBatch > m_batch
Definition: walletdb.h:285
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:1094
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:186
DBErrors FindWalletTx(std::vector< TxId > &txIds, std::list< CWalletTx > &vWtx)
Definition: walletdb.cpp:950
bool WriteKeyMetadata(const CKeyMetadata &meta, const CPubKey &pubkey, const bool overwrite)
Definition: walletdb.cpp:107
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:193
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:99
bool EraseIC(const K &key)
Definition: walletdb.h:189
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:1102
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:1110
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:164
bool EraseName(const CTxDestination &address)
Definition: walletdb.cpp:70
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:201
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< uint8_t > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:129
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:179
bool WriteCryptedDescriptorKey(const uint256 &desc_id, const CPubKey &pubkey, const std::vector< uint8_t > &secret)
Definition: walletdb.cpp:234
WalletBatch & operator=(const WalletBatch &)=delete
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:103
DBErrors ZapSelectTx(std::vector< TxId > &txIdsIn, std::vector< TxId > &txIdsOut)
Definition: walletdb.cpp:1002
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:768
bool WriteDestData(const CTxDestination &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:1071
WalletBatch(WalletDatabase &database, bool _fFlushOnClose=true)
Definition: walletdb.h:201
WalletDatabase & m_database
Definition: walletdb.h:286
An instance of this class represents one database.
Definition: db.h:100
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:156
virtual void IncrementUpdateCounter()=0
Descriptor with some wallet metadata.
Definition: walletutil.h:80
void SetNull()
Definition: uint256.h:39
160-bit opaque blob.
Definition: uint256.h:115
256-bit opaque blob.
Definition: uint256.h:127
std::vector< uint8_t, secure_allocator< uint8_t > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:22
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:42
const std::string DEFAULTKEY
Definition: walletdb.cpp:32
const std::string CSCRIPT
Definition: walletdb.cpp:31
const std::string TX
Definition: walletdb.cpp:46
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:28
const std::string DESTDATA
Definition: walletdb.cpp:33
const std::string WALLETDESCRIPTORKEY
Definition: walletdb.cpp:51
const std::string SETTINGS
Definition: walletdb.cpp:45
const std::string POOL
Definition: walletdb.cpp:43
const std::string WALLETDESCRIPTORCKEY
Definition: walletdb.cpp:50
const std::string ACTIVEEXTERNALSPK
Definition: walletdb.cpp:26
const std::string NAME
Definition: walletdb.cpp:40
const std::string MINVERSION
Definition: walletdb.cpp:39
const std::string HDCHAIN
Definition: walletdb.cpp:35
const std::string WALLETDESCRIPTORCACHE
Definition: walletdb.cpp:49
const std::string WALLETDESCRIPTOR
Definition: walletdb.cpp:48
const std::string VERSION
Definition: walletdb.cpp:47
const std::string KEY
Definition: walletdb.cpp:37
const std::string KEYMETA
Definition: walletdb.cpp:36
const std::string WATCHMETA
Definition: walletdb.cpp:52
const std::string ACENTRY
Definition: walletdb.cpp:25
const std::string MASTER_KEY
Definition: walletdb.cpp:38
const std::string ACTIVEINTERNALSPK
Definition: walletdb.cpp:27
const std::string FLAGS
Definition: walletdb.cpp:34
const std::string WATCHS
Definition: walletdb.cpp:53
const std::string PURPOSE
Definition: walletdb.cpp:44
const std::string OLD_KEY
Definition: walletdb.cpp:41
const std::string CRYPTED_KEY
Definition: walletdb.cpp:30
const std::string BESTBLOCK
Definition: walletdb.cpp:29
#define READWRITE(...)
Definition: serialize.h:180
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:93
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:100
void clear()
Definition: keyorigin.h:26
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:1044
bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, CDataStream &ssValue, std::string &strType, std::string &strErr, const KeyFilterFn &filter_fn=nullptr)
Unserialize a given Key-Value pair and load it into the wallet.
Definition: walletdb.cpp:754
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:45
@ NONCRITICAL_ERROR
std::function< bool(const std::string &)> KeyFilterFn
Callback for filtering key types to deserialize in ReadKeyValue.
Definition: walletdb.h:294
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:33
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1169
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1164