Bitcoin Core  0.20.99
P2P Digital Currency
walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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.
5 
6 #ifndef BITCOIN_WALLET_WALLETDB_H
7 #define BITCOIN_WALLET_WALLETDB_H
8 
9 #include <amount.h>
10 #include <script/sign.h>
11 #include <wallet/bdb.h>
12 #include <wallet/db.h>
13 #include <wallet/walletutil.h>
14 #include <key.h>
15 
16 #include <stdint.h>
17 #include <string>
18 #include <vector>
19 
32 static const bool DEFAULT_FLUSHWALLET = true;
33 
34 struct CBlockLocator;
35 class CKeyPool;
36 class CMasterKey;
37 class CScript;
38 class CWallet;
39 class CWalletTx;
40 class uint160;
41 class uint256;
42 
44 enum class DBErrors
45 {
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 WALLETDESCRIPTORCKEY;
80 extern const std::string WALLETDESCRIPTORKEY;
81 extern const std::string WATCHMETA;
82 extern const std::string WATCHS;
83 } // namespace DBKeys
84 
85 /* simple HD chain data model */
86 class CHDChain
87 {
88 public:
92 
93  static const int VERSION_HD_BASE = 1;
94  static const int VERSION_HD_CHAIN_SPLIT = 2;
95  static const int CURRENT_VERSION = VERSION_HD_CHAIN_SPLIT;
96  int nVersion;
97 
98  CHDChain() { SetNull(); }
99 
101  {
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()
109  {
110  nVersion = CHDChain::CURRENT_VERSION;
111  nExternalChainCounter = 0;
112  nInternalChainCounter = 0;
113  seed_id.SetNull();
114  }
115 
116  bool operator==(const CHDChain& chain) const
117  {
118  return seed_id == chain.seed_id;
119  }
120 };
121 
123 {
124 public:
125  static const int VERSION_BASIC=1;
126  static const int VERSION_WITH_HDDATA=10;
127  static const int VERSION_WITH_KEY_ORIGIN = 12;
128  static const int CURRENT_VERSION=VERSION_WITH_KEY_ORIGIN;
129  int nVersion;
130  int64_t nCreateTime; // 0 means unknown
131  std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
132  CKeyID hd_seed_id; //id of the HD seed used to derive this key
133  KeyOriginInfo key_origin; // Key origin info with path and fingerprint
134  bool has_key_origin = false;
135 
137  {
138  SetNull();
139  }
140  explicit CKeyMetadata(int64_t nCreateTime_)
141  {
142  SetNull();
143  nCreateTime = nCreateTime_;
144  }
145 
147  {
148  READWRITE(obj.nVersion, obj.nCreateTime);
149  if (obj.nVersion >= VERSION_WITH_HDDATA) {
150  READWRITE(obj.hdKeypath, obj.hd_seed_id);
151  }
152  if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
153  {
154  READWRITE(obj.key_origin);
155  READWRITE(obj.has_key_origin);
156  }
157  }
158 
159  void SetNull()
160  {
162  nCreateTime = 0;
163  hdKeypath.clear();
164  hd_seed_id.SetNull();
165  key_origin.clear();
166  has_key_origin = false;
167  }
168 };
169 
178 {
179 private:
180  template <typename K, typename T>
181  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
182  {
183  if (!m_batch->Write(key, value, fOverwrite)) {
184  return false;
185  }
186  m_database.IncrementUpdateCounter();
187  if (m_database.nUpdateCounter % 1000 == 0) {
188  m_batch->Flush();
189  }
190  return true;
191  }
192 
193  template <typename K>
194  bool EraseIC(const K& key)
195  {
196  if (!m_batch->Erase(key)) {
197  return false;
198  }
199  m_database.IncrementUpdateCounter();
200  if (m_database.nUpdateCounter % 1000 == 0) {
201  m_batch->Flush();
202  }
203  return true;
204  }
205 
206 public:
207  explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) :
208  m_batch(database.MakeBatch(_fFlushOnClose)),
209  m_database(database)
210  {
211  }
212  WalletBatch(const WalletBatch&) = delete;
213  WalletBatch& operator=(const WalletBatch&) = delete;
214 
215  bool WriteName(const std::string& strAddress, const std::string& strName);
216  bool EraseName(const std::string& strAddress);
217 
218  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
219  bool ErasePurpose(const std::string& strAddress);
220 
221  bool WriteTx(const CWalletTx& wtx);
222  bool EraseTx(uint256 hash);
223 
224  bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
225  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
226  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
227  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
228 
229  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
230 
231  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
232  bool EraseWatchOnly(const CScript &script);
233 
234  bool WriteBestBlock(const CBlockLocator& locator);
235  bool ReadBestBlock(CBlockLocator& locator);
236 
237  bool WriteOrderPosNext(int64_t nOrderPosNext);
238 
239  bool ReadPool(int64_t nPool, CKeyPool& keypool);
240  bool WritePool(int64_t nPool, const CKeyPool& keypool);
241  bool ErasePool(int64_t nPool);
242 
243  bool WriteMinVersion(int nVersion);
244 
245  bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
246  bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
247  bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
248  bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
249  bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
250 
252  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
254  bool EraseDestData(const std::string &address, const std::string &key);
255 
256  bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
257 
258  DBErrors LoadWallet(CWallet* pwallet);
259  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWalletTx>& vWtx);
260  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
261  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
262  static bool IsKeyType(const std::string& strType);
263 
265  bool WriteHDChain(const CHDChain& chain);
266 
267  bool WriteWalletFlags(const uint64_t flags);
269  bool TxnBegin();
271  bool TxnCommit();
273  bool TxnAbort();
274 private:
275  std::unique_ptr<DatabaseBatch> m_batch;
277 };
278 
280 void MaybeCompactWalletDB();
281 
283 using KeyFilterFn = std::function<bool(const std::string&)>;
284 
286 bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
287 
289 std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
290 
292 std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
293 
294 #endif // BITCOIN_WALLET_WALLETDB_H
static const int CURRENT_VERSION
Definition: walletdb.h:95
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:140
bool EraseIC(const K &key)
Definition: walletdb.h:194
const std::string POOL
Definition: walletdb.cpp:43
const std::string FLAGS
Definition: walletdb.cpp:34
std::string hdKeypath
Definition: walletdb.h:131
void SetNull()
Definition: uint256.h:39
const std::string NAME
Definition: walletdb.cpp:40
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:114
bool operator==(const CHDChain &chain) const
Definition: walletdb.h:116
const std::string SETTINGS
Definition: walletdb.cpp:45
const std::string WALLETDESCRIPTORCKEY
Definition: walletdb.cpp:50
WalletBatch(WalletDatabase &database, bool _fFlushOnClose=true)
Definition: walletdb.h:207
const std::string CRYPTED_KEY
Definition: walletdb.cpp:30
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:181
const std::string DEFAULTKEY
Definition: walletdb.cpp:32
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:33
const std::string KEYMETA
Definition: walletdb.cpp:36
uint32_t nExternalChainCounter
Definition: walletdb.h:89
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:202
SERIALIZE_METHODS(CKeyMetadata, obj)
Definition: walletdb.h:146
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:44
const std::string ORDERPOSNEXT
Definition: walletdb.cpp:42
const std::string MASTER_KEY
Definition: walletdb.cpp:38
void clear()
Definition: keyorigin.h:23
Access to the wallet database.
Definition: walletdb.h:177
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:24
const std::string OLD_KEY
Definition: walletdb.cpp:41
std::unique_ptr< WalletDatabase > CreateMockWalletDatabase()
Return object for accessing temporary in-memory database.
Definition: walletdb.cpp:1065
const std::string HDCHAIN
Definition: walletdb.cpp:35
const std::string WALLETDESCRIPTOR
Definition: walletdb.cpp:48
static const int CURRENT_VERSION
Definition: walletdb.h:128
std::unique_ptr< DatabaseBatch > m_batch
Definition: walletdb.h:275
const std::string ACTIVEINTERNALSPK
Definition: walletdb.cpp:27
WalletDatabase & m_database
Definition: walletdb.h:276
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:936
An encapsulated public key.
Definition: pubkey.h:31
const std::string WATCHMETA
Definition: walletdb.cpp:52
const std::string WALLETDESCRIPTORKEY
Definition: walletdb.cpp:51
const std::string DESTDATA
Definition: walletdb.cpp:33
Descriptor with some wallet metadata.
Definition: walletutil.h:71
KeyOriginInfo key_origin
Definition: walletdb.h:133
int64_t nCreateTime
Definition: walletdb.h:130
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1059
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:270
SERIALIZE_METHODS(CHDChain, obj)
Definition: walletdb.h:100
int nVersion
Definition: walletdb.h:129
int flags
Definition: bitcoin-tx.cpp:506
256-bit opaque blob.
Definition: uint256.h:124
const std::string ACENTRY
Definition: walletdb.cpp:25
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
const std::string MINVERSION
Definition: walletdb.cpp:39
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:32
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
const std::string ACTIVEEXTERNALSPK
Definition: walletdb.cpp:26
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:622
160-bit opaque blob.
Definition: uint256.h:113
CHDChain()
Definition: walletdb.h:98
const std::string WATCHS
Definition: walletdb.cpp:53
void SetNull()
Definition: walletdb.h:108
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:24
CKeyID hd_seed_id
Definition: walletdb.h:132
std::shared_ptr< CWallet > LoadWallet(interfaces::Chain &chain, const std::string &name, Optional< bool > load_on_start, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:233
const std::string BESTBLOCK
Definition: walletdb.cpp:29
const std::string CSCRIPT
Definition: walletdb.cpp:31
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:678
CKeyID seed_id
seed hash160
Definition: walletdb.h:91
#define VERSION
#define READWRITE(...)
Definition: serialize.h:175
std::function< bool(const std::string &)> KeyFilterFn
Callback for filtering key types to deserialize in ReadKeyValue.
Definition: walletdb.h:283
const std::string BESTBLOCK_NOMERKLE
Definition: walletdb.cpp:28
const std::string KEY
Definition: walletdb.cpp:37
int nVersion
Definition: walletdb.h:96
void SetNull()
Definition: walletdb.h:159
uint32_t nInternalChainCounter
Definition: walletdb.h:90
A key from a CWallet&#39;s keypool.
const std::string PURPOSE
Definition: walletdb.cpp:44
An instance of this class represents one database.
Definition: db.h:104