Bitcoin Core  0.18.99
P2P Digital Currency
wallet.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 #include <wallet/wallet.h>
7 
8 #include <chain.h>
9 #include <consensus/consensus.h>
10 #include <consensus/validation.h>
11 #include <fs.h>
12 #include <interfaces/chain.h>
13 #include <interfaces/wallet.h>
14 #include <key.h>
15 #include <key_io.h>
16 #include <policy/fees.h>
17 #include <policy/policy.h>
18 #include <primitives/block.h>
19 #include <primitives/transaction.h>
20 #include <script/descriptor.h>
21 #include <script/script.h>
22 #include <script/signingprovider.h>
23 #include <util/bip32.h>
24 #include <util/error.h>
25 #include <util/fees.h>
26 #include <util/moneystr.h>
27 #include <util/rbf.h>
28 #include <util/translation.h>
29 #include <util/validation.h>
30 #include <validation.h>
31 #include <wallet/coincontrol.h>
32 #include <wallet/fees.h>
33 
34 #include <algorithm>
35 #include <assert.h>
36 #include <future>
37 
38 #include <boost/algorithm/string/replace.hpp>
39 
40 const std::map<uint64_t,std::string> WALLET_FLAG_CAVEATS{
42  "You need to rescan the blockchain in order to correctly mark used "
43  "destinations in the past. Until this is done, some destinations may "
44  "be considered unused, even if the opposite is the case."
45  },
46 };
47 
48 static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10;
49 
51 static std::vector<std::shared_ptr<CWallet>> vpwallets GUARDED_BY(cs_wallets);
52 
53 bool AddWallet(const std::shared_ptr<CWallet>& wallet)
54 {
55  LOCK(cs_wallets);
56  assert(wallet);
57  std::vector<std::shared_ptr<CWallet>>::const_iterator i = std::find(vpwallets.begin(), vpwallets.end(), wallet);
58  if (i != vpwallets.end()) return false;
59  vpwallets.push_back(wallet);
60  return true;
61 }
62 
63 bool RemoveWallet(const std::shared_ptr<CWallet>& wallet)
64 {
65  LOCK(cs_wallets);
66  assert(wallet);
67  std::vector<std::shared_ptr<CWallet>>::iterator i = std::find(vpwallets.begin(), vpwallets.end(), wallet);
68  if (i == vpwallets.end()) return false;
69  vpwallets.erase(i);
70  return true;
71 }
72 
73 bool HasWallets()
74 {
75  LOCK(cs_wallets);
76  return !vpwallets.empty();
77 }
78 
79 std::vector<std::shared_ptr<CWallet>> GetWallets()
80 {
81  LOCK(cs_wallets);
82  return vpwallets;
83 }
84 
85 std::shared_ptr<CWallet> GetWallet(const std::string& name)
86 {
87  LOCK(cs_wallets);
88  for (const std::shared_ptr<CWallet>& wallet : vpwallets) {
89  if (wallet->GetName() == name) return wallet;
90  }
91  return nullptr;
92 }
93 
95 static std::condition_variable g_wallet_release_cv;
96 static std::set<CWallet*> g_unloading_wallet_set;
97 
98 // Custom deleter for shared_ptr<CWallet>.
99 static void ReleaseWallet(CWallet* wallet)
100 {
101  // Unregister and delete the wallet right after BlockUntilSyncedToCurrentChain
102  // so that it's in sync with the current chainstate.
103  wallet->WalletLogPrintf("Releasing wallet\n");
105  wallet->Flush();
106  wallet->m_chain_notifications_handler.reset();
107  delete wallet;
108  // Wallet is now released, notify UnloadWallet, if any.
109  {
110  LOCK(g_wallet_release_mutex);
111  if (g_unloading_wallet_set.erase(wallet) == 0) {
112  // UnloadWallet was not called for this wallet, all done.
113  return;
114  }
115  }
116  g_wallet_release_cv.notify_all();
117 }
118 
119 void UnloadWallet(std::shared_ptr<CWallet>&& wallet)
120 {
121  // Mark wallet for unloading.
122  CWallet* pwallet = wallet.get();
123  {
124  LOCK(g_wallet_release_mutex);
125  auto it = g_unloading_wallet_set.insert(pwallet);
126  assert(it.second);
127  }
128  // The wallet can be in use so it's not possible to explicitly unload here.
129  // Notify the unload intent so that all remaining shared pointers are
130  // released.
131  pwallet->NotifyUnload();
132  // Time to ditch our shared_ptr and wait for ReleaseWallet call.
133  wallet.reset();
134  {
135  WAIT_LOCK(g_wallet_release_mutex, lock);
136  while (g_unloading_wallet_set.count(pwallet) == 1) {
137  g_wallet_release_cv.wait(lock);
138  }
139  }
140 }
141 
142 std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const WalletLocation& location, std::string& error, std::string& warning)
143 {
144  if (!CWallet::Verify(chain, location, false, error, warning)) {
145  error = "Wallet file verification failed: " + error;
146  return nullptr;
147  }
148 
149  std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location);
150  if (!wallet) {
151  error = "Wallet loading failed.";
152  return nullptr;
153  }
154  AddWallet(wallet);
155  wallet->postInitProcess();
156  return wallet;
157 }
158 
159 std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning)
160 {
161  return LoadWallet(chain, WalletLocation(name), error, warning);
162 }
163 
164 WalletCreationStatus CreateWallet(interfaces::Chain& chain, const SecureString& passphrase, uint64_t wallet_creation_flags, const std::string& name, std::string& error, std::string& warning, std::shared_ptr<CWallet>& result)
165 {
166  // Indicate that the wallet is actually supposed to be blank and not just blank to make it encrypted
167  bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET);
168 
169  // Born encrypted wallets need to be created blank first.
170  if (!passphrase.empty()) {
171  wallet_creation_flags |= WALLET_FLAG_BLANK_WALLET;
172  }
173 
174  // Check the wallet file location
175  WalletLocation location(name);
176  if (location.Exists()) {
177  error = "Wallet " + location.GetName() + " already exists.";
179  }
180 
181  // Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
182  std::string wallet_error;
183  if (!CWallet::Verify(chain, location, false, wallet_error, warning)) {
184  error = "Wallet file verification failed: " + wallet_error;
186  }
187 
188  // Do not allow a passphrase when private keys are disabled
189  if (!passphrase.empty() && (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
190  error = "Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.";
192  }
193 
194  // Make the wallet
195  std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location, wallet_creation_flags);
196  if (!wallet) {
197  error = "Wallet creation failed";
199  }
200 
201  // Encrypt the wallet
202  if (!passphrase.empty() && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
203  if (!wallet->EncryptWallet(passphrase)) {
204  error = "Error: Wallet created but failed to encrypt.";
206  }
207  if (!create_blank) {
208  // Unlock the wallet
209  if (!wallet->Unlock(passphrase)) {
210  error = "Error: Wallet was encrypted but could not be unlocked";
212  }
213 
214  // Set a seed for the wallet
215  CPubKey master_pub_key = wallet->GenerateNewSeed();
216  wallet->SetHDSeed(master_pub_key);
217  wallet->NewKeyPool();
218 
219  // Relock the wallet
220  wallet->Lock();
221  }
222  }
223  AddWallet(wallet);
224  wallet->postInitProcess();
225  result = wallet;
227 }
228 
229 const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
230 
231 const uint256 CWalletTx::ABANDON_HASH(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
232 
238 std::string COutput::ToString() const
239 {
240  return strprintf("COutput(%s, %d, %d) [%s]", tx->GetHash().ToString(), i, nDepth, FormatMoney(tx->tx->vout[i].nValue));
241 }
242 
243 std::vector<CKeyID> GetAffectedKeys(const CScript& spk, const SigningProvider& provider)
244 {
245  std::vector<CScript> dummy;
247  InferDescriptor(spk, provider)->Expand(0, DUMMY_SIGNING_PROVIDER, dummy, out);
248  std::vector<CKeyID> ret;
249  for (const auto& entry : out.pubkeys) {
250  ret.push_back(entry.first);
251  }
252  return ret;
253 }
254 
255 const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
256 {
257  LOCK(cs_wallet);
258  std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash);
259  if (it == mapWallet.end())
260  return nullptr;
261  return &(it->second);
262 }
263 
265 {
266  assert(!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
267  assert(!IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
268  AssertLockHeld(cs_wallet);
269  bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
270 
271  CKey secret;
272 
273  // Create new metadata
274  int64_t nCreationTime = GetTime();
275  CKeyMetadata metadata(nCreationTime);
276 
277  // use HD key derivation if HD was enabled during wallet creation and a seed is present
278  if (IsHDEnabled()) {
279  DeriveNewChildKey(batch, metadata, secret, (CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
280  } else {
281  secret.MakeNewKey(fCompressed);
282  }
283 
284  // Compressed public keys were introduced in version 0.6.0
285  if (fCompressed) {
286  SetMinVersion(FEATURE_COMPRPUBKEY);
287  }
288 
289  CPubKey pubkey = secret.GetPubKey();
290  assert(secret.VerifyPubKey(pubkey));
291 
292  mapKeyMetadata[pubkey.GetID()] = metadata;
293  UpdateTimeFirstKey(nCreationTime);
294 
295  if (!AddKeyPubKeyWithDB(batch, secret, pubkey)) {
296  throw std::runtime_error(std::string(__func__) + ": AddKey failed");
297  }
298  return pubkey;
299 }
300 
301 void CWallet::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, bool internal)
302 {
303  // for now we use a fixed keypath scheme of m/0'/0'/k
304  CKey seed; //seed (256bit)
305  CExtKey masterKey; //hd master key
306  CExtKey accountKey; //key at m/0'
307  CExtKey chainChildKey; //key at m/0'/0' (external) or m/0'/1' (internal)
308  CExtKey childKey; //key at m/0'/0'/<n>'
309 
310  // try to get the seed
311  if (!GetKey(hdChain.seed_id, seed))
312  throw std::runtime_error(std::string(__func__) + ": seed not found");
313 
314  masterKey.SetSeed(seed.begin(), seed.size());
315 
316  // derive m/0'
317  // use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
318  masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT);
319 
320  // derive m/0'/0' (external chain) OR m/0'/1' (internal chain)
321  assert(internal ? CanSupportFeature(FEATURE_HD_SPLIT) : true);
322  accountKey.Derive(chainChildKey, BIP32_HARDENED_KEY_LIMIT+(internal ? 1 : 0));
323 
324  // derive child key at next index, skip keys already known to the wallet
325  do {
326  // always derive hardened keys
327  // childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range
328  // example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649
329  if (internal) {
330  chainChildKey.Derive(childKey, hdChain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
331  metadata.hdKeypath = "m/0'/1'/" + std::to_string(hdChain.nInternalChainCounter) + "'";
332  metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
333  metadata.key_origin.path.push_back(1 | BIP32_HARDENED_KEY_LIMIT);
334  metadata.key_origin.path.push_back(hdChain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
335  hdChain.nInternalChainCounter++;
336  }
337  else {
338  chainChildKey.Derive(childKey, hdChain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
339  metadata.hdKeypath = "m/0'/0'/" + std::to_string(hdChain.nExternalChainCounter) + "'";
340  metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
341  metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
342  metadata.key_origin.path.push_back(hdChain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
343  hdChain.nExternalChainCounter++;
344  }
345  } while (HaveKey(childKey.key.GetPubKey().GetID()));
346  secret = childKey.key;
347  metadata.hd_seed_id = hdChain.seed_id;
348  CKeyID master_id = masterKey.key.GetPubKey().GetID();
349  std::copy(master_id.begin(), master_id.begin() + 4, metadata.key_origin.fingerprint);
350  metadata.has_key_origin = true;
351  // update the chain model in the database
352  if (!batch.WriteHDChain(hdChain))
353  throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");
354 }
355 
356 bool CWallet::AddKeyPubKeyWithDB(WalletBatch& batch, const CKey& secret, const CPubKey& pubkey)
357 {
358  AssertLockHeld(cs_wallet);
359 
360  // Make sure we aren't adding private keys to private key disabled wallets
361  assert(!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
362 
363  // FillableSigningProvider has no concept of wallet databases, but calls AddCryptedKey
364  // which is overridden below. To avoid flushes, the database handle is
365  // tunneled through to it.
366  bool needsDB = !encrypted_batch;
367  if (needsDB) {
368  encrypted_batch = &batch;
369  }
370  if (!AddKeyPubKeyInner(secret, pubkey)) {
371  if (needsDB) encrypted_batch = nullptr;
372  return false;
373  }
374  if (needsDB) encrypted_batch = nullptr;
375 
376  // check if we need to remove from watch-only
377  CScript script;
378  script = GetScriptForDestination(PKHash(pubkey));
379  if (HaveWatchOnly(script)) {
380  RemoveWatchOnly(script);
381  }
382  script = GetScriptForRawPubKey(pubkey);
383  if (HaveWatchOnly(script)) {
384  RemoveWatchOnly(script);
385  }
386 
387  if (!IsCrypted()) {
388  return batch.WriteKey(pubkey,
389  secret.GetPrivKey(),
390  mapKeyMetadata[pubkey.GetID()]);
391  }
392  UnsetWalletFlagWithDB(batch, WALLET_FLAG_BLANK_WALLET);
393  return true;
394 }
395 
396 bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
397 {
398  WalletBatch batch(*database);
399  return CWallet::AddKeyPubKeyWithDB(batch, secret, pubkey);
400 }
401 
402 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
403  const std::vector<unsigned char> &vchCryptedSecret)
404 {
405  if (!AddCryptedKeyInner(vchPubKey, vchCryptedSecret))
406  return false;
407  {
408  LOCK(cs_wallet);
409  if (encrypted_batch)
410  return encrypted_batch->WriteCryptedKey(vchPubKey,
411  vchCryptedSecret,
412  mapKeyMetadata[vchPubKey.GetID()]);
413  else
414  return WalletBatch(*database).WriteCryptedKey(vchPubKey,
415  vchCryptedSecret,
416  mapKeyMetadata[vchPubKey.GetID()]);
417  }
418 }
419 
420 void CWallet::LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata& meta)
421 {
422  AssertLockHeld(cs_wallet);
423  UpdateTimeFirstKey(meta.nCreateTime);
424  mapKeyMetadata[keyID] = meta;
425 }
426 
427 void CWallet::LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata& meta)
428 {
429  AssertLockHeld(cs_wallet);
430  UpdateTimeFirstKey(meta.nCreateTime);
431  m_script_metadata[script_id] = meta;
432 }
433 
435 {
436  AssertLockHeld(cs_wallet);
437  if (IsLocked() || IsWalletFlagSet(WALLET_FLAG_KEY_ORIGIN_METADATA)) {
438  return;
439  }
440 
441  std::unique_ptr<WalletBatch> batch = MakeUnique<WalletBatch>(*database);
442  for (auto& meta_pair : mapKeyMetadata) {
443  CKeyMetadata& meta = meta_pair.second;
444  if (!meta.hd_seed_id.IsNull() && !meta.has_key_origin && meta.hdKeypath != "s") { // If the hdKeypath is "s", that's the seed and it doesn't have a key origin
445  CKey key;
446  GetKey(meta.hd_seed_id, key);
447  CExtKey masterKey;
448  masterKey.SetSeed(key.begin(), key.size());
449  // Add to map
450  CKeyID master_id = masterKey.key.GetPubKey().GetID();
451  std::copy(master_id.begin(), master_id.begin() + 4, meta.key_origin.fingerprint);
452  if (!ParseHDKeypath(meta.hdKeypath, meta.key_origin.path)) {
453  throw std::runtime_error("Invalid stored hdKeypath");
454  }
455  meta.has_key_origin = true;
458  }
459 
460  // Write meta to wallet
461  CPubKey pubkey;
462  if (GetPubKey(meta_pair.first, pubkey)) {
463  batch->WriteKeyMetadata(meta, pubkey, true);
464  }
465  }
466  }
467  batch.reset(); //write before setting the flag
468  SetWalletFlag(WALLET_FLAG_KEY_ORIGIN_METADATA);
469 }
470 
471 bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
472 {
473  return AddCryptedKeyInner(vchPubKey, vchCryptedSecret);
474 }
475 
480 void CWallet::UpdateTimeFirstKey(int64_t nCreateTime)
481 {
482  AssertLockHeld(cs_wallet);
483  if (nCreateTime <= 1) {
484  // Cannot determine birthday information, so set the wallet birthday to
485  // the beginning of time.
486  nTimeFirstKey = 1;
487  } else if (!nTimeFirstKey || nCreateTime < nTimeFirstKey) {
488  nTimeFirstKey = nCreateTime;
489  }
490 }
491 
492 bool CWallet::AddCScript(const CScript& redeemScript)
493 {
494  WalletBatch batch(*database);
495  return AddCScriptWithDB(batch, redeemScript);
496 }
497 
498 bool CWallet::AddCScriptWithDB(WalletBatch& batch, const CScript& redeemScript)
499 {
500  if (!FillableSigningProvider::AddCScript(redeemScript))
501  return false;
502  if (batch.WriteCScript(Hash160(redeemScript), redeemScript)) {
503  UnsetWalletFlagWithDB(batch, WALLET_FLAG_BLANK_WALLET);
504  return true;
505  }
506  return false;
507 }
508 
509 bool CWallet::LoadCScript(const CScript& redeemScript)
510 {
511  /* A sanity check was added in pull #3843 to avoid adding redeemScripts
512  * that never can be redeemed. However, old wallets may still contain
513  * these. Do not add them to the wallet and warn. */
514  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
515  {
516  std::string strAddr = EncodeDestination(ScriptHash(redeemScript));
517  WalletLogPrintf("%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n", __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr);
518  return true;
519  }
520 
521  return FillableSigningProvider::AddCScript(redeemScript);
522 }
523 
524 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
525 {
526  //TODO: Use Solver to extract this?
527  CScript::const_iterator pc = dest.begin();
528  opcodetype opcode;
529  std::vector<unsigned char> vch;
530  if (!dest.GetOp(pc, opcode, vch) || !CPubKey::ValidSize(vch))
531  return false;
532  pubKeyOut = CPubKey(vch);
533  if (!pubKeyOut.IsFullyValid())
534  return false;
535  if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
536  return false;
537  return true;
538 }
539 
541 {
542  LOCK(cs_KeyStore);
543  setWatchOnly.insert(dest);
544  CPubKey pubKey;
545  if (ExtractPubKey(dest, pubKey)) {
546  mapWatchKeys[pubKey.GetID()] = pubKey;
547  ImplicitlyLearnRelatedKeyScripts(pubKey);
548  }
549  return true;
550 }
551 
553 {
554  if (!AddWatchOnlyInMem(dest))
555  return false;
556  const CKeyMetadata& meta = m_script_metadata[CScriptID(dest)];
557  UpdateTimeFirstKey(meta.nCreateTime);
559  if (batch.WriteWatchOnly(dest, meta)) {
560  UnsetWalletFlagWithDB(batch, WALLET_FLAG_BLANK_WALLET);
561  return true;
562  }
563  return false;
564 }
565 
566 bool CWallet::AddWatchOnlyWithDB(WalletBatch &batch, const CScript& dest, int64_t create_time)
567 {
568  m_script_metadata[CScriptID(dest)].nCreateTime = create_time;
569  return AddWatchOnlyWithDB(batch, dest);
570 }
571 
573 {
574  WalletBatch batch(*database);
575  return AddWatchOnlyWithDB(batch, dest);
576 }
577 
578 bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
579 {
580  m_script_metadata[CScriptID(dest)].nCreateTime = nCreateTime;
581  return AddWatchOnly(dest);
582 }
583 
585 {
586  AssertLockHeld(cs_wallet);
587  {
588  LOCK(cs_KeyStore);
589  setWatchOnly.erase(dest);
590  CPubKey pubKey;
591  if (ExtractPubKey(dest, pubKey)) {
592  mapWatchKeys.erase(pubKey.GetID());
593  }
594  // Related CScripts are not removed; having superfluous scripts around is
595  // harmless (see comment in ImplicitlyLearnRelatedKeyScripts).
596  }
597 
598  if (!HaveWatchOnly())
599  NotifyWatchonlyChanged(false);
600  if (!WalletBatch(*database).EraseWatchOnly(dest))
601  return false;
602 
603  return true;
604 }
605 
607 {
608  return AddWatchOnlyInMem(dest);
609 }
610 
611 bool CWallet::HaveWatchOnly(const CScript &dest) const
612 {
613  LOCK(cs_KeyStore);
614  return setWatchOnly.count(dest) > 0;
615 }
616 
618 {
619  LOCK(cs_KeyStore);
620  return (!setWatchOnly.empty());
621 }
622 
623 bool CWallet::Unlock(const SecureString& strWalletPassphrase, bool accept_no_keys)
624 {
625  CCrypter crypter;
626  CKeyingMaterial _vMasterKey;
627 
628  {
629  LOCK(cs_wallet);
630  for (const MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
631  {
632  if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
633  return false;
634  if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
635  continue; // try another master key
636  if (Unlock(_vMasterKey, accept_no_keys)) {
637  // Now that we've unlocked, upgrade the key metadata
638  UpgradeKeyMetadata();
639  return true;
640  }
641  }
642  }
643  return false;
644 }
645 
646 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
647 {
648  bool fWasLocked = IsLocked();
649 
650  {
651  LOCK(cs_wallet);
652  Lock();
653 
654  CCrypter crypter;
655  CKeyingMaterial _vMasterKey;
656  for (MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
657  {
658  if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
659  return false;
660  if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
661  return false;
662  if (Unlock(_vMasterKey))
663  {
664  int64_t nStartTime = GetTimeMillis();
665  crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
666  pMasterKey.second.nDeriveIterations = static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime))));
667 
668  nStartTime = GetTimeMillis();
669  crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
670  pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + static_cast<unsigned int>(pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime)))) / 2;
671 
672  if (pMasterKey.second.nDeriveIterations < 25000)
673  pMasterKey.second.nDeriveIterations = 25000;
674 
675  WalletLogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
676 
677  if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
678  return false;
679  if (!crypter.Encrypt(_vMasterKey, pMasterKey.second.vchCryptedKey))
680  return false;
681  WalletBatch(*database).WriteMasterKey(pMasterKey.first, pMasterKey.second);
682  if (fWasLocked)
683  Lock();
684  return true;
685  }
686  }
687  }
688 
689  return false;
690 }
691 
693 {
694  WalletBatch batch(*database);
695  batch.WriteBestBlock(loc);
696 }
697 
698 void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in, bool fExplicit)
699 {
700  LOCK(cs_wallet);
701  if (nWalletVersion >= nVersion)
702  return;
703 
704  // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
705  if (fExplicit && nVersion > nWalletMaxVersion)
706  nVersion = FEATURE_LATEST;
707 
708  nWalletVersion = nVersion;
709 
710  if (nVersion > nWalletMaxVersion)
711  nWalletMaxVersion = nVersion;
712 
713  {
714  WalletBatch* batch = batch_in ? batch_in : new WalletBatch(*database);
715  if (nWalletVersion > 40000)
716  batch->WriteMinVersion(nWalletVersion);
717  if (!batch_in)
718  delete batch;
719  }
720 }
721 
722 bool CWallet::SetMaxVersion(int nVersion)
723 {
724  LOCK(cs_wallet);
725  // cannot downgrade below current version
726  if (nWalletVersion > nVersion)
727  return false;
728 
729  nWalletMaxVersion = nVersion;
730 
731  return true;
732 }
733 
734 std::set<uint256> CWallet::GetConflicts(const uint256& txid) const
735 {
736  std::set<uint256> result;
737  AssertLockHeld(cs_wallet);
738 
739  std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(txid);
740  if (it == mapWallet.end())
741  return result;
742  const CWalletTx& wtx = it->second;
743 
744  std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
745 
746  for (const CTxIn& txin : wtx.tx->vin)
747  {
748  if (mapTxSpends.count(txin.prevout) <= 1)
749  continue; // No conflict if zero or one spends
750  range = mapTxSpends.equal_range(txin.prevout);
751  for (TxSpends::const_iterator _it = range.first; _it != range.second; ++_it)
752  result.insert(_it->second);
753  }
754  return result;
755 }
756 
757 bool CWallet::HasWalletSpend(const uint256& txid) const
758 {
759  AssertLockHeld(cs_wallet);
760  auto iter = mapTxSpends.lower_bound(COutPoint(txid, 0));
761  return (iter != mapTxSpends.end() && iter->first.hash == txid);
762 }
763 
764 void CWallet::Flush(bool shutdown)
765 {
766  database->Flush(shutdown);
767 }
768 
769 void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)
770 {
771  // We want all the wallet transactions in range to have the same metadata as
772  // the oldest (smallest nOrderPos).
773  // So: find smallest nOrderPos:
774 
775  int nMinOrderPos = std::numeric_limits<int>::max();
776  const CWalletTx* copyFrom = nullptr;
777  for (TxSpends::iterator it = range.first; it != range.second; ++it) {
778  const CWalletTx* wtx = &mapWallet.at(it->second);
779  if (wtx->nOrderPos < nMinOrderPos) {
780  nMinOrderPos = wtx->nOrderPos;
781  copyFrom = wtx;
782  }
783  }
784 
785  if (!copyFrom) {
786  return;
787  }
788 
789  // Now copy data from copyFrom to rest:
790  for (TxSpends::iterator it = range.first; it != range.second; ++it)
791  {
792  const uint256& hash = it->second;
793  CWalletTx* copyTo = &mapWallet.at(hash);
794  if (copyFrom == copyTo) continue;
795  assert(copyFrom && "Oldest wallet transaction in range assumed to have been found.");
796  if (!copyFrom->IsEquivalentTo(*copyTo)) continue;
797  copyTo->mapValue = copyFrom->mapValue;
798  copyTo->vOrderForm = copyFrom->vOrderForm;
799  // fTimeReceivedIsTxTime not copied on purpose
800  // nTimeReceived not copied on purpose
801  copyTo->nTimeSmart = copyFrom->nTimeSmart;
802  copyTo->fFromMe = copyFrom->fFromMe;
803  // nOrderPos not copied on purpose
804  // cached members not copied on purpose
805  }
806 }
807 
812 bool CWallet::IsSpent(interfaces::Chain::Lock& locked_chain, const uint256& hash, unsigned int n) const
813 {
814  const COutPoint outpoint(hash, n);
815  std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
816  range = mapTxSpends.equal_range(outpoint);
817 
818  for (TxSpends::const_iterator it = range.first; it != range.second; ++it)
819  {
820  const uint256& wtxid = it->second;
821  std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
822  if (mit != mapWallet.end()) {
823  int depth = mit->second.GetDepthInMainChain(locked_chain);
824  if (depth > 0 || (depth == 0 && !mit->second.isAbandoned()))
825  return true; // Spent
826  }
827  }
828  return false;
829 }
830 
831 void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid)
832 {
833  mapTxSpends.insert(std::make_pair(outpoint, wtxid));
834 
835  setLockedCoins.erase(outpoint);
836 
837  std::pair<TxSpends::iterator, TxSpends::iterator> range;
838  range = mapTxSpends.equal_range(outpoint);
839  SyncMetaData(range);
840 }
841 
842 
843 void CWallet::AddToSpends(const uint256& wtxid)
844 {
845  auto it = mapWallet.find(wtxid);
846  assert(it != mapWallet.end());
847  CWalletTx& thisTx = it->second;
848  if (thisTx.IsCoinBase()) // Coinbases don't spend anything!
849  return;
850 
851  for (const CTxIn& txin : thisTx.tx->vin)
852  AddToSpends(txin.prevout, wtxid);
853 }
854 
855 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
856 {
857  if (IsCrypted())
858  return false;
859 
860  CKeyingMaterial _vMasterKey;
861 
862  _vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
863  GetStrongRandBytes(&_vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
864 
865  CMasterKey kMasterKey;
866 
867  kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
869 
870  CCrypter crypter;
871  int64_t nStartTime = GetTimeMillis();
872  crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
873  kMasterKey.nDeriveIterations = static_cast<unsigned int>(2500000 / ((double)(GetTimeMillis() - nStartTime)));
874 
875  nStartTime = GetTimeMillis();
876  crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
877  kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + static_cast<unsigned int>(kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime)))) / 2;
878 
879  if (kMasterKey.nDeriveIterations < 25000)
880  kMasterKey.nDeriveIterations = 25000;
881 
882  WalletLogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
883 
884  if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
885  return false;
886  if (!crypter.Encrypt(_vMasterKey, kMasterKey.vchCryptedKey))
887  return false;
888 
889  {
890  LOCK(cs_wallet);
891  mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
892  assert(!encrypted_batch);
893  encrypted_batch = new WalletBatch(*database);
894  if (!encrypted_batch->TxnBegin()) {
895  delete encrypted_batch;
896  encrypted_batch = nullptr;
897  return false;
898  }
899  encrypted_batch->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
900 
901  if (!EncryptKeys(_vMasterKey))
902  {
903  encrypted_batch->TxnAbort();
904  delete encrypted_batch;
905  encrypted_batch = nullptr;
906  // We now probably have half of our keys encrypted in memory, and half not...
907  // die and let the user reload the unencrypted wallet.
908  assert(false);
909  }
910 
911  // Encryption was introduced in version 0.4.0
912  SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch, true);
913 
914  if (!encrypted_batch->TxnCommit()) {
915  delete encrypted_batch;
916  encrypted_batch = nullptr;
917  // We now have keys encrypted in memory, but not on disk...
918  // die to avoid confusion and let the user reload the unencrypted wallet.
919  assert(false);
920  }
921 
922  delete encrypted_batch;
923  encrypted_batch = nullptr;
924 
925  Lock();
926  Unlock(strWalletPassphrase);
927 
928  // if we are using HD, replace the HD seed with a new one
929  if (IsHDEnabled()) {
930  SetHDSeed(GenerateNewSeed());
931  }
932 
933  NewKeyPool();
934  Lock();
935 
936  // Need to completely rewrite the wallet file; if we don't, bdb might keep
937  // bits of the unencrypted private key in slack space in the database file.
938  database->Rewrite();
939 
940  // BDB seems to have a bad habit of writing old data into
941  // slack space in .dat files; that is bad if the old data is
942  // unencrypted private keys. So:
943  database->ReloadDbEnv();
944 
945  }
946  NotifyStatusChanged(this);
947 
948  return true;
949 }
950 
952 {
953  LOCK(cs_wallet);
954  WalletBatch batch(*database);
955 
956  // Old wallets didn't have any defined order for transactions
957  // Probably a bad idea to change the output of this
958 
959  // First: get all CWalletTx into a sorted-by-time multimap.
960  typedef std::multimap<int64_t, CWalletTx*> TxItems;
961  TxItems txByTime;
962 
963  for (auto& entry : mapWallet)
964  {
965  CWalletTx* wtx = &entry.second;
966  txByTime.insert(std::make_pair(wtx->nTimeReceived, wtx));
967  }
968 
969  nOrderPosNext = 0;
970  std::vector<int64_t> nOrderPosOffsets;
971  for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
972  {
973  CWalletTx *const pwtx = (*it).second;
974  int64_t& nOrderPos = pwtx->nOrderPos;
975 
976  if (nOrderPos == -1)
977  {
978  nOrderPos = nOrderPosNext++;
979  nOrderPosOffsets.push_back(nOrderPos);
980 
981  if (!batch.WriteTx(*pwtx))
982  return DBErrors::LOAD_FAIL;
983  }
984  else
985  {
986  int64_t nOrderPosOff = 0;
987  for (const int64_t& nOffsetStart : nOrderPosOffsets)
988  {
989  if (nOrderPos >= nOffsetStart)
990  ++nOrderPosOff;
991  }
992  nOrderPos += nOrderPosOff;
993  nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
994 
995  if (!nOrderPosOff)
996  continue;
997 
998  // Since we're changing the order, write it back
999  if (!batch.WriteTx(*pwtx))
1000  return DBErrors::LOAD_FAIL;
1001  }
1002  }
1003  batch.WriteOrderPosNext(nOrderPosNext);
1004 
1005  return DBErrors::LOAD_OK;
1006 }
1007 
1009 {
1010  AssertLockHeld(cs_wallet);
1011  int64_t nRet = nOrderPosNext++;
1012  if (batch) {
1013  batch->WriteOrderPosNext(nOrderPosNext);
1014  } else {
1015  WalletBatch(*database).WriteOrderPosNext(nOrderPosNext);
1016  }
1017  return nRet;
1018 }
1019 
1021 {
1022  {
1023  LOCK(cs_wallet);
1024  for (std::pair<const uint256, CWalletTx>& item : mapWallet)
1025  item.second.MarkDirty();
1026  }
1027 }
1028 
1029 bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
1030 {
1031  LOCK(cs_wallet);
1032 
1033  auto mi = mapWallet.find(originalHash);
1034 
1035  // There is a bug if MarkReplaced is not called on an existing wallet transaction.
1036  assert(mi != mapWallet.end());
1037 
1038  CWalletTx& wtx = (*mi).second;
1039 
1040  // Ensure for now that we're not overwriting data
1041  assert(wtx.mapValue.count("replaced_by_txid") == 0);
1042 
1043  wtx.mapValue["replaced_by_txid"] = newHash.ToString();
1044 
1045  WalletBatch batch(*database, "r+");
1046 
1047  bool success = true;
1048  if (!batch.WriteTx(wtx)) {
1049  WalletLogPrintf("%s: Updating batch tx %s failed\n", __func__, wtx.GetHash().ToString());
1050  success = false;
1051  }
1052 
1053  NotifyTransactionChanged(this, originalHash, CT_UPDATED);
1054 
1055  return success;
1056 }
1057 
1058 void CWallet::SetUsedDestinationState(const uint256& hash, unsigned int n, bool used)
1059 {
1060  const CWalletTx* srctx = GetWalletTx(hash);
1061  if (!srctx) return;
1062 
1063  CTxDestination dst;
1064  if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
1065  if (::IsMine(*this, dst)) {
1066  LOCK(cs_wallet);
1067  if (used && !GetDestData(dst, "used", nullptr)) {
1068  AddDestData(dst, "used", "p"); // p for "present", opposite of absent (null)
1069  } else if (!used && GetDestData(dst, "used", nullptr)) {
1070  EraseDestData(dst, "used");
1071  }
1072  }
1073  }
1074 }
1075 
1077 {
1078  LOCK(cs_wallet);
1079  return ::IsMine(*this, dst) && GetDestData(dst, "used", nullptr);
1080 }
1081 
1082 bool CWallet::IsUsedDestination(const uint256& hash, unsigned int n) const
1083 {
1084  CTxDestination dst;
1085  const CWalletTx* srctx = GetWalletTx(hash);
1086  return srctx && ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst) && IsUsedDestination(dst);
1087 }
1088 
1089 bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
1090 {
1091  LOCK(cs_wallet);
1092 
1093  WalletBatch batch(*database, "r+", fFlushOnClose);
1094 
1095  uint256 hash = wtxIn.GetHash();
1096 
1097  if (IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE)) {
1098  // Mark used destinations
1099  for (const CTxIn& txin : wtxIn.tx->vin) {
1100  const COutPoint& op = txin.prevout;
1101  SetUsedDestinationState(op.hash, op.n, true);
1102  }
1103  }
1104 
1105  // Inserts only if not already there, returns tx inserted or tx found
1106  std::pair<std::map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(std::make_pair(hash, wtxIn));
1107  CWalletTx& wtx = (*ret.first).second;
1108  wtx.BindWallet(this);
1109  bool fInsertedNew = ret.second;
1110  if (fInsertedNew) {
1111  wtx.nTimeReceived = chain().getAdjustedTime();
1112  wtx.nOrderPos = IncOrderPosNext(&batch);
1113  wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
1114  wtx.nTimeSmart = ComputeTimeSmart(wtx);
1115  AddToSpends(hash);
1116  }
1117 
1118  bool fUpdated = false;
1119  if (!fInsertedNew)
1120  {
1121  // Merge
1122  if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock)
1123  {
1124  wtx.hashBlock = wtxIn.hashBlock;
1125  fUpdated = true;
1126  }
1127  // If no longer abandoned, update
1128  if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned())
1129  {
1130  wtx.hashBlock = wtxIn.hashBlock;
1131  fUpdated = true;
1132  }
1133  if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex))
1134  {
1135  wtx.nIndex = wtxIn.nIndex;
1136  fUpdated = true;
1137  }
1138  if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
1139  {
1140  wtx.fFromMe = wtxIn.fFromMe;
1141  fUpdated = true;
1142  }
1143  // If we have a witness-stripped version of this transaction, and we
1144  // see a new version with a witness, then we must be upgrading a pre-segwit
1145  // wallet. Store the new version of the transaction with the witness,
1146  // as the stripped-version must be invalid.
1147  // TODO: Store all versions of the transaction, instead of just one.
1148  if (wtxIn.tx->HasWitness() && !wtx.tx->HasWitness()) {
1149  wtx.SetTx(wtxIn.tx);
1150  fUpdated = true;
1151  }
1152  }
1153 
1155  WalletLogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
1156 
1157  // Write to disk
1158  if (fInsertedNew || fUpdated)
1159  if (!batch.WriteTx(wtx))
1160  return false;
1161 
1162  // Break debit/credit balance caches:
1163  wtx.MarkDirty();
1164 
1165  // Notify UI of new or updated transaction
1166  NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
1167 
1168 #if HAVE_SYSTEM
1169  // notify an external script when a wallet transaction comes in or is updated
1170  std::string strCmd = gArgs.GetArg("-walletnotify", "");
1171 
1172  if (!strCmd.empty())
1173  {
1174  boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
1175  std::thread t(runCommand, strCmd);
1176  t.detach(); // thread runs free
1177  }
1178 #endif
1179 
1180  return true;
1181 }
1182 
1184 {
1185  uint256 hash = wtxIn.GetHash();
1186  const auto& ins = mapWallet.emplace(hash, wtxIn);
1187  CWalletTx& wtx = ins.first->second;
1188  wtx.BindWallet(this);
1189  if (/* insertion took place */ ins.second) {
1190  wtx.m_it_wtxOrdered = wtxOrdered.insert(std::make_pair(wtx.nOrderPos, &wtx));
1191  }
1192  AddToSpends(hash);
1193  for (const CTxIn& txin : wtx.tx->vin) {
1194  auto it = mapWallet.find(txin.prevout.hash);
1195  if (it != mapWallet.end()) {
1196  CWalletTx& prevtx = it->second;
1197  if (prevtx.nIndex == -1 && !prevtx.hashUnset()) {
1198  MarkConflicted(prevtx.hashBlock, wtx.GetHash());
1199  }
1200  }
1201  }
1202 }
1203 
1204 bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, const uint256& block_hash, int posInBlock, bool fUpdate)
1205 {
1206  const CTransaction& tx = *ptx;
1207  {
1208  AssertLockHeld(cs_wallet);
1209 
1210  if (!block_hash.IsNull()) {
1211  for (const CTxIn& txin : tx.vin) {
1212  std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range = mapTxSpends.equal_range(txin.prevout);
1213  while (range.first != range.second) {
1214  if (range.first->second != tx.GetHash()) {
1215  WalletLogPrintf("Transaction %s (in block %s) conflicts with wallet transaction %s (both spend %s:%i)\n", tx.GetHash().ToString(), block_hash.ToString(), range.first->second.ToString(), range.first->first.hash.ToString(), range.first->first.n);
1216  MarkConflicted(block_hash, range.first->second);
1217  }
1218  range.first++;
1219  }
1220  }
1221  }
1222 
1223  bool fExisted = mapWallet.count(tx.GetHash()) != 0;
1224  if (fExisted && !fUpdate) return false;
1225  if (fExisted || IsMine(tx) || IsFromMe(tx))
1226  {
1227  /* Check if any keys in the wallet keypool that were supposed to be unused
1228  * have appeared in a new transaction. If so, remove those keys from the keypool.
1229  * This can happen when restoring an old wallet backup that does not contain
1230  * the mostly recently created transactions from newer versions of the wallet.
1231  */
1232 
1233  // loop though all outputs
1234  for (const CTxOut& txout: tx.vout) {
1235  // extract addresses and check if they match with an unused keypool key
1236  for (const auto& keyid : GetAffectedKeys(txout.scriptPubKey, *this)) {
1237  std::map<CKeyID, int64_t>::const_iterator mi = m_pool_key_to_index.find(keyid);
1238  if (mi != m_pool_key_to_index.end()) {
1239  WalletLogPrintf("%s: Detected a used keypool key, mark all keypool key up to this key as used\n", __func__);
1240  MarkReserveKeysAsUsed(mi->second);
1241 
1242  if (!TopUpKeyPool()) {
1243  WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
1244  }
1245  }
1246  }
1247  }
1248 
1249  CWalletTx wtx(this, ptx);
1250 
1251  // Get merkle branch if transaction was found in a block
1252  if (!block_hash.IsNull())
1253  wtx.SetMerkleBranch(block_hash, posInBlock);
1254 
1255  return AddToWallet(wtx, false);
1256  }
1257  }
1258  return false;
1259 }
1260 
1262 {
1263  auto locked_chain = chain().lock();
1264  LOCK(cs_wallet);
1265  const CWalletTx* wtx = GetWalletTx(hashTx);
1266  return wtx && !wtx->isAbandoned() && wtx->GetDepthInMainChain(*locked_chain) == 0 && !wtx->InMempool();
1267 }
1268 
1270 {
1271  for (const CTxIn& txin : tx->vin) {
1272  auto it = mapWallet.find(txin.prevout.hash);
1273  if (it != mapWallet.end()) {
1274  it->second.MarkDirty();
1275  }
1276  }
1277 }
1278 
1280 {
1281  auto locked_chain_recursive = chain().lock(); // Temporary. Removed in upcoming lock cleanup
1282  LOCK(cs_wallet);
1283 
1284  WalletBatch batch(*database, "r+");
1285 
1286  std::set<uint256> todo;
1287  std::set<uint256> done;
1288 
1289  // Can't mark abandoned if confirmed or in mempool
1290  auto it = mapWallet.find(hashTx);
1291  assert(it != mapWallet.end());
1292  CWalletTx& origtx = it->second;
1293  if (origtx.GetDepthInMainChain(locked_chain) != 0 || origtx.InMempool()) {
1294  return false;
1295  }
1296 
1297  todo.insert(hashTx);
1298 
1299  while (!todo.empty()) {
1300  uint256 now = *todo.begin();
1301  todo.erase(now);
1302  done.insert(now);
1303  auto it = mapWallet.find(now);
1304  assert(it != mapWallet.end());
1305  CWalletTx& wtx = it->second;
1306  int currentconfirm = wtx.GetDepthInMainChain(locked_chain);
1307  // If the orig tx was not in block, none of its spends can be
1308  assert(currentconfirm <= 0);
1309  // if (currentconfirm < 0) {Tx and spends are already conflicted, no need to abandon}
1310  if (currentconfirm == 0 && !wtx.isAbandoned()) {
1311  // If the orig tx was not in block/mempool, none of its spends can be in mempool
1312  assert(!wtx.InMempool());
1313  wtx.nIndex = -1;
1314  wtx.setAbandoned();
1315  wtx.MarkDirty();
1316  batch.WriteTx(wtx);
1317  NotifyTransactionChanged(this, wtx.GetHash(), CT_UPDATED);
1318  // Iterate over all its outputs, and mark transactions in the wallet that spend them abandoned too
1319  TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(now, 0));
1320  while (iter != mapTxSpends.end() && iter->first.hash == now) {
1321  if (!done.count(iter->second)) {
1322  todo.insert(iter->second);
1323  }
1324  iter++;
1325  }
1326  // If a transaction changes 'conflicted' state, that changes the balance
1327  // available of the outputs it spends. So force those to be recomputed
1328  MarkInputsDirty(wtx.tx);
1329  }
1330  }
1331 
1332  return true;
1333 }
1334 
1335 void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx)
1336 {
1337  auto locked_chain = chain().lock();
1338  LOCK(cs_wallet);
1339 
1340  int conflictconfirms = -locked_chain->getBlockDepth(hashBlock);
1341  // If number of conflict confirms cannot be determined, this means
1342  // that the block is still unknown or not yet part of the main chain,
1343  // for example when loading the wallet during a reindex. Do nothing in that
1344  // case.
1345  if (conflictconfirms >= 0)
1346  return;
1347 
1348  // Do not flush the wallet here for performance reasons
1349  WalletBatch batch(*database, "r+", false);
1350 
1351  std::set<uint256> todo;
1352  std::set<uint256> done;
1353 
1354  todo.insert(hashTx);
1355 
1356  while (!todo.empty()) {
1357  uint256 now = *todo.begin();
1358  todo.erase(now);
1359  done.insert(now);
1360  auto it = mapWallet.find(now);
1361  assert(it != mapWallet.end());
1362  CWalletTx& wtx = it->second;
1363  int currentconfirm = wtx.GetDepthInMainChain(*locked_chain);
1364  if (conflictconfirms < currentconfirm) {
1365  // Block is 'more conflicted' than current confirm; update.
1366  // Mark transaction as conflicted with this block.
1367  wtx.nIndex = -1;
1368  wtx.hashBlock = hashBlock;
1369  wtx.MarkDirty();
1370  batch.WriteTx(wtx);
1371  // Iterate over all its outputs, and mark transactions in the wallet that spend them conflicted too
1372  TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(now, 0));
1373  while (iter != mapTxSpends.end() && iter->first.hash == now) {
1374  if (!done.count(iter->second)) {
1375  todo.insert(iter->second);
1376  }
1377  iter++;
1378  }
1379  // If a transaction changes 'conflicted' state, that changes the balance
1380  // available of the outputs it spends. So force those to be recomputed
1381  MarkInputsDirty(wtx.tx);
1382  }
1383  }
1384 }
1385 
1386 void CWallet::SyncTransaction(const CTransactionRef& ptx, const uint256& block_hash, int posInBlock, bool update_tx) {
1387  if (!AddToWalletIfInvolvingMe(ptx, block_hash, posInBlock, update_tx))
1388  return; // Not one of ours
1389 
1390  // If a transaction changes 'conflicted' state, that changes the balance
1391  // available of the outputs it spends. So force those to be
1392  // recomputed, also:
1393  MarkInputsDirty(ptx);
1394 }
1395 
1397  auto locked_chain = chain().lock();
1398  LOCK(cs_wallet);
1399  SyncTransaction(ptx, {} /* block hash */, 0 /* position in block */);
1400 
1401  auto it = mapWallet.find(ptx->GetHash());
1402  if (it != mapWallet.end()) {
1403  it->second.fInMempool = true;
1404  }
1405 }
1406 
1408  LOCK(cs_wallet);
1409  auto it = mapWallet.find(ptx->GetHash());
1410  if (it != mapWallet.end()) {
1411  it->second.fInMempool = false;
1412  }
1413 }
1414 
1415 void CWallet::BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& vtxConflicted) {
1416  const uint256& block_hash = block.GetHash();
1417  auto locked_chain = chain().lock();
1418  LOCK(cs_wallet);
1419  // TODO: Temporarily ensure that mempool removals are notified before
1420  // connected transactions. This shouldn't matter, but the abandoned
1421  // state of transactions in our wallet is currently cleared when we
1422  // receive another notification and there is a race condition where
1423  // notification of a connected conflict might cause an outside process
1424  // to abandon a transaction and then have it inadvertently cleared by
1425  // the notification that the conflicted transaction was evicted.
1426 
1427  for (const CTransactionRef& ptx : vtxConflicted) {
1428  SyncTransaction(ptx, {} /* block hash */, 0 /* position in block */);
1429  TransactionRemovedFromMempool(ptx);
1430  }
1431  for (size_t i = 0; i < block.vtx.size(); i++) {
1432  SyncTransaction(block.vtx[i], block_hash, i);
1433  TransactionRemovedFromMempool(block.vtx[i]);
1434  }
1435 
1436  m_last_block_processed = block_hash;
1437 }
1438 
1440  auto locked_chain = chain().lock();
1441  LOCK(cs_wallet);
1442 
1443  for (const CTransactionRef& ptx : block.vtx) {
1444  SyncTransaction(ptx, {} /* block hash */, 0 /* position in block */);
1445  }
1446 }
1447 
1449 {
1450  m_best_block_time = GetTime();
1451 }
1452 
1453 
1455  AssertLockNotHeld(cs_wallet);
1456  // Skip the queue-draining stuff if we know we're caught up with
1457  // ::ChainActive().Tip(), otherwise put a callback in the validation interface queue and wait
1458  // for the queue to drain enough to execute it (indicating we are caught up
1459  // at least with the time we entered this function).
1460  uint256 last_block_hash = WITH_LOCK(cs_wallet, return m_last_block_processed);
1461  chain().waitForNotificationsIfNewBlocksConnected(last_block_hash);
1462 }
1463 
1464 
1466 {
1467  {
1468  LOCK(cs_wallet);
1469  std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
1470  if (mi != mapWallet.end())
1471  {
1472  const CWalletTx& prev = (*mi).second;
1473  if (txin.prevout.n < prev.tx->vout.size())
1474  return IsMine(prev.tx->vout[txin.prevout.n]);
1475  }
1476  }
1477  return ISMINE_NO;
1478 }
1479 
1480 // Note that this function doesn't distinguish between a 0-valued input,
1481 // and a not-"is mine" (according to the filter) input.
1482 CAmount CWallet::GetDebit(const CTxIn &txin, const isminefilter& filter) const
1483 {
1484  {
1485  LOCK(cs_wallet);
1486  std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
1487  if (mi != mapWallet.end())
1488  {
1489  const CWalletTx& prev = (*mi).second;
1490  if (txin.prevout.n < prev.tx->vout.size())
1491  if (IsMine(prev.tx->vout[txin.prevout.n]) & filter)
1492  return prev.tx->vout[txin.prevout.n].nValue;
1493  }
1494  }
1495  return 0;
1496 }
1497 
1498 isminetype CWallet::IsMine(const CTxOut& txout) const
1499 {
1500  return ::IsMine(*this, txout.scriptPubKey);
1501 }
1502 
1503 CAmount CWallet::GetCredit(const CTxOut& txout, const isminefilter& filter) const
1504 {
1505  if (!MoneyRange(txout.nValue))
1506  throw std::runtime_error(std::string(__func__) + ": value out of range");
1507  return ((IsMine(txout) & filter) ? txout.nValue : 0);
1508 }
1509 
1510 bool CWallet::IsChange(const CTxOut& txout) const
1511 {
1512  return IsChange(txout.scriptPubKey);
1513 }
1514 
1515 bool CWallet::IsChange(const CScript& script) const
1516 {
1517  // TODO: fix handling of 'change' outputs. The assumption is that any
1518  // payment to a script that is ours, but is not in the address book
1519  // is change. That assumption is likely to break when we implement multisignature
1520  // wallets that return change back into a multi-signature-protected address;
1521  // a better way of identifying which outputs are 'the send' and which are
1522  // 'the change' will need to be implemented (maybe extend CWalletTx to remember
1523  // which output, if any, was change).
1524  if (::IsMine(*this, script))
1525  {
1526  CTxDestination address;
1527  if (!ExtractDestination(script, address))
1528  return true;
1529 
1530  LOCK(cs_wallet);
1531  if (!mapAddressBook.count(address))
1532  return true;
1533  }
1534  return false;
1535 }
1536 
1537 CAmount CWallet::GetChange(const CTxOut& txout) const
1538 {
1539  if (!MoneyRange(txout.nValue))
1540  throw std::runtime_error(std::string(__func__) + ": value out of range");
1541  return (IsChange(txout) ? txout.nValue : 0);
1542 }
1543 
1544 bool CWallet::IsMine(const CTransaction& tx) const
1545 {
1546  for (const CTxOut& txout : tx.vout)
1547  if (IsMine(txout))
1548  return true;
1549  return false;
1550 }
1551 
1553 {
1554  return (GetDebit(tx, ISMINE_ALL) > 0);
1555 }
1556 
1558 {
1559  CAmount nDebit = 0;
1560  for (const CTxIn& txin : tx.vin)
1561  {
1562  nDebit += GetDebit(txin, filter);
1563  if (!MoneyRange(nDebit))
1564  throw std::runtime_error(std::string(__func__) + ": value out of range");
1565  }
1566  return nDebit;
1567 }
1568 
1569 bool CWallet::IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const
1570 {
1571  LOCK(cs_wallet);
1572 
1573  for (const CTxIn& txin : tx.vin)
1574  {
1575  auto mi = mapWallet.find(txin.prevout.hash);
1576  if (mi == mapWallet.end())
1577  return false; // any unknown inputs can't be from us
1578 
1579  const CWalletTx& prev = (*mi).second;
1580 
1581  if (txin.prevout.n >= prev.tx->vout.size())
1582  return false; // invalid input!
1583 
1584  if (!(IsMine(prev.tx->vout[txin.prevout.n]) & filter))
1585  return false;
1586  }
1587  return true;
1588 }
1589 
1591 {
1592  CAmount nCredit = 0;
1593  for (const CTxOut& txout : tx.vout)
1594  {
1595  nCredit += GetCredit(txout, filter);
1596  if (!MoneyRange(nCredit))
1597  throw std::runtime_error(std::string(__func__) + ": value out of range");
1598  }
1599  return nCredit;
1600 }
1601 
1603 {
1604  CAmount nChange = 0;
1605  for (const CTxOut& txout : tx.vout)
1606  {
1607  nChange += GetChange(txout);
1608  if (!MoneyRange(nChange))
1609  throw std::runtime_error(std::string(__func__) + ": value out of range");
1610  }
1611  return nChange;
1612 }
1613 
1615 {
1616  assert(!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
1617  CKey key;
1618  key.MakeNewKey(true);
1619  return DeriveNewSeed(key);
1620 }
1621 
1623 {
1624  int64_t nCreationTime = GetTime();
1625  CKeyMetadata metadata(nCreationTime);
1626 
1627  // calculate the seed
1628  CPubKey seed = key.GetPubKey();
1629  assert(key.VerifyPubKey(seed));
1630 
1631  // set the hd keypath to "s" -> Seed, refers the seed to itself
1632  metadata.hdKeypath = "s";
1633  metadata.has_key_origin = false;
1634  metadata.hd_seed_id = seed.GetID();
1635 
1636  {
1637  LOCK(cs_wallet);
1638 
1639  // mem store the metadata
1640  mapKeyMetadata[seed.GetID()] = metadata;
1641 
1642  // write the key&metadata to the database
1643  if (!AddKeyPubKey(key, seed))
1644  throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed");
1645  }
1646 
1647  return seed;
1648 }
1649 
1650 void CWallet::SetHDSeed(const CPubKey& seed)
1651 {
1652  LOCK(cs_wallet);
1653  // store the keyid (hash160) together with
1654  // the child index counter in the database
1655  // as a hdchain object
1656  CHDChain newHdChain;
1658  newHdChain.seed_id = seed.GetID();
1659  SetHDChain(newHdChain, false);
1661  UnsetWalletFlag(WALLET_FLAG_BLANK_WALLET);
1662 }
1663 
1664 void CWallet::SetHDChain(const CHDChain& chain, bool memonly)
1665 {
1666  LOCK(cs_wallet);
1667  if (!memonly && !WalletBatch(*database).WriteHDChain(chain))
1668  throw std::runtime_error(std::string(__func__) + ": writing chain failed");
1669 
1670  hdChain = chain;
1671 }
1672 
1674 {
1675  return !hdChain.seed_id.IsNull();
1676 }
1677 
1679 {
1680  // A wallet can generate keys if it has an HD seed (IsHDEnabled) or it is a non-HD wallet (pre FEATURE_HD)
1681  LOCK(cs_wallet);
1682  return IsHDEnabled() || !CanSupportFeature(FEATURE_HD);
1683 }
1684 
1685 bool CWallet::CanGetAddresses(bool internal)
1686 {
1687  LOCK(cs_wallet);
1688  // Check if the keypool has keys
1689  bool keypool_has_keys;
1690  if (internal && CanSupportFeature(FEATURE_HD_SPLIT)) {
1691  keypool_has_keys = setInternalKeyPool.size() > 0;
1692  } else {
1693  keypool_has_keys = KeypoolCountExternalKeys() > 0;
1694  }
1695  // If the keypool doesn't have keys, check if we can generate them
1696  if (!keypool_has_keys) {
1697  return CanGenerateKeys();
1698  }
1699  return keypool_has_keys;
1700 }
1701 
1703 {
1704  LOCK(cs_wallet);
1705  m_wallet_flags |= flags;
1706  if (!WalletBatch(*database).WriteWalletFlags(m_wallet_flags))
1707  throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed");
1708 }
1709 
1710 void CWallet::UnsetWalletFlag(uint64_t flag)
1711 {
1712  WalletBatch batch(*database);
1713  UnsetWalletFlagWithDB(batch, flag);
1714 }
1715 
1716 void CWallet::UnsetWalletFlagWithDB(WalletBatch& batch, uint64_t flag)
1717 {
1718  LOCK(cs_wallet);
1719  m_wallet_flags &= ~flag;
1720  if (!batch.WriteWalletFlags(m_wallet_flags))
1721  throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed");
1722 }
1723 
1724 bool CWallet::IsWalletFlagSet(uint64_t flag) const
1725 {
1726  return (m_wallet_flags & flag);
1727 }
1728 
1729 bool CWallet::SetWalletFlags(uint64_t overwriteFlags, bool memonly)
1730 {
1731  LOCK(cs_wallet);
1732  m_wallet_flags = overwriteFlags;
1733  if (((overwriteFlags & KNOWN_WALLET_FLAGS) >> 32) ^ (overwriteFlags >> 32)) {
1734  // contains unknown non-tolerable wallet flags
1735  return false;
1736  }
1737  if (!memonly && !WalletBatch(*database).WriteWalletFlags(m_wallet_flags)) {
1738  throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed");
1739  }
1740 
1741  return true;
1742 }
1743 
1744 int64_t CWalletTx::GetTxTime() const
1745 {
1746  int64_t n = nTimeSmart;
1747  return n ? n : nTimeReceived;
1748 }
1749 
1750 // Helper for producing a max-sized low-S low-R signature (eg 71 bytes)
1751 // or a max-sized low-S signature (e.g. 72 bytes) if use_max_sig is true
1752 bool CWallet::DummySignInput(CTxIn &tx_in, const CTxOut &txout, bool use_max_sig) const
1753 {
1754  // Fill in dummy signatures for fee calculation.
1755  const CScript& scriptPubKey = txout.scriptPubKey;
1756  SignatureData sigdata;
1757 
1758  if (!ProduceSignature(*this, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR : DUMMY_SIGNATURE_CREATOR, scriptPubKey, sigdata)) {
1759  return false;
1760  }
1761  UpdateInput(tx_in, sigdata);
1762  return true;
1763 }
1764 
1765 // Helper for producing a bunch of max-sized low-S low-R signatures (eg 71 bytes)
1766 bool CWallet::DummySignTx(CMutableTransaction &txNew, const std::vector<CTxOut> &txouts, bool use_max_sig) const
1767 {
1768  // Fill in dummy signatures for fee calculation.
1769  int nIn = 0;
1770  for (const auto& txout : txouts)
1771  {
1772  if (!DummySignInput(txNew.vin[nIn], txout, use_max_sig)) {
1773  return false;
1774  }
1775 
1776  nIn++;
1777  }
1778  return true;
1779 }
1780 
1781 bool CWallet::ImportScripts(const std::set<CScript> scripts, int64_t timestamp)
1782 {
1783  WalletBatch batch(*database);
1784  for (const auto& entry : scripts) {
1785  CScriptID id(entry);
1786  if (HaveCScript(id)) {
1787  WalletLogPrintf("Already have script %s, skipping\n", HexStr(entry));
1788  continue;
1789  }
1790  if (!AddCScriptWithDB(batch, entry)) {
1791  return false;
1792  }
1793 
1794  if (timestamp > 0) {
1795  m_script_metadata[CScriptID(entry)].nCreateTime = timestamp;
1796  }
1797  }
1798  if (timestamp > 0) {
1799  UpdateTimeFirstKey(timestamp);
1800  }
1801 
1802  return true;
1803 }
1804 
1805 bool CWallet::ImportPrivKeys(const std::map<CKeyID, CKey>& privkey_map, const int64_t timestamp)
1806 {
1807  WalletBatch batch(*database);
1808  for (const auto& entry : privkey_map) {
1809  const CKey& key = entry.second;
1810  CPubKey pubkey = key.GetPubKey();
1811  const CKeyID& id = entry.first;
1812  assert(key.VerifyPubKey(pubkey));
1813  // Skip if we already have the key
1814  if (HaveKey(id)) {
1815  WalletLogPrintf("Already have key with pubkey %s, skipping\n", HexStr(pubkey));
1816  continue;
1817  }
1818  mapKeyMetadata[id].nCreateTime = timestamp;
1819  // If the private key is not present in the wallet, insert it.
1820  if (!AddKeyPubKeyWithDB(batch, key, pubkey)) {
1821  return false;
1822  }
1823  UpdateTimeFirstKey(timestamp);
1824  }
1825  return true;
1826 }
1827 
1828 bool CWallet::ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp)
1829 {
1830  WalletBatch batch(*database);
1831  for (const auto& entry : key_origins) {
1832  AddKeyOriginWithDB(batch, entry.second.first, entry.second.second);
1833  }
1834  for (const CKeyID& id : ordered_pubkeys) {
1835  auto entry = pubkey_map.find(id);
1836  if (entry == pubkey_map.end()) {
1837  continue;
1838  }
1839  const CPubKey& pubkey = entry->second;
1840  CPubKey temp;
1841  if (GetPubKey(id, temp)) {
1842  // Already have pubkey, skipping
1843  WalletLogPrintf("Already have pubkey %s, skipping\n", HexStr(temp));
1844  continue;
1845  }
1846  if (!AddWatchOnlyWithDB(batch, GetScriptForRawPubKey(pubkey), timestamp)) {
1847  return false;
1848  }
1849  mapKeyMetadata[id].nCreateTime = timestamp;
1850 
1851  // Add to keypool only works with pubkeys
1852  if (add_keypool) {
1853  AddKeypoolPubkeyWithDB(pubkey, internal, batch);
1855  }
1856  }
1857  return true;
1858 }
1859 
1860 bool CWallet::ImportScriptPubKeys(const std::string& label, const std::set<CScript>& script_pub_keys, const bool have_solving_data, const bool apply_label, const int64_t timestamp)
1861 {
1862  WalletBatch batch(*database);
1863  for (const CScript& script : script_pub_keys) {
1864  if (!have_solving_data || !::IsMine(*this, script)) { // Always call AddWatchOnly for non-solvable watch-only, so that watch timestamp gets updated
1865  if (!AddWatchOnlyWithDB(batch, script, timestamp)) {
1866  return false;
1867  }
1868  }
1869  CTxDestination dest;
1870  ExtractDestination(script, dest);
1871  if (apply_label && IsValidDestination(dest)) {
1872  SetAddressBookWithDB(batch, dest, label, "receive");
1873  }
1874  }
1875  return true;
1876 }
1877 
1879 {
1880  std::vector<CTxOut> txouts;
1881  // Look up the inputs. We should have already checked that this transaction
1882  // IsAllFromMe(ISMINE_SPENDABLE), so every input should already be in our
1883  // wallet, with a valid index into the vout array, and the ability to sign.
1884  for (const CTxIn& input : tx.vin) {
1885  const auto mi = wallet->mapWallet.find(input.prevout.hash);
1886  if (mi == wallet->mapWallet.end()) {
1887  return -1;
1888  }
1889  assert(input.prevout.n < mi->second.tx->vout.size());
1890  txouts.emplace_back(mi->second.tx->vout[input.prevout.n]);
1891  }
1892  return CalculateMaximumSignedTxSize(tx, wallet, txouts, use_max_sig);
1893 }
1894 
1895 // txouts needs to be in the order of tx.vin
1896 int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector<CTxOut>& txouts, bool use_max_sig)
1897 {
1898  CMutableTransaction txNew(tx);
1899  if (!wallet->DummySignTx(txNew, txouts, use_max_sig)) {
1900  // This should never happen, because IsAllFromMe(ISMINE_SPENDABLE)
1901  // implies that we can sign for every input.
1902  return -1;
1903  }
1904  return GetVirtualTransactionSize(CTransaction(txNew));
1905 }
1906 
1907 int CalculateMaximumSignedInputSize(const CTxOut& txout, const CWallet* wallet, bool use_max_sig)
1908 {
1909  CMutableTransaction txn;
1910  txn.vin.push_back(CTxIn(COutPoint()));
1911  if (!wallet->DummySignInput(txn.vin[0], txout, use_max_sig)) {
1912  return -1;
1913  }
1914  return GetVirtualTransactionInputSize(txn.vin[0]);
1915 }
1916 
1917 void CWalletTx::GetAmounts(std::list<COutputEntry>& listReceived,
1918  std::list<COutputEntry>& listSent, CAmount& nFee, const isminefilter& filter) const
1919 {
1920  nFee = 0;
1921  listReceived.clear();
1922  listSent.clear();
1923 
1924  // Compute fee:
1925  CAmount nDebit = GetDebit(filter);
1926  if (nDebit > 0) // debit>0 means we signed/sent this transaction
1927  {
1928  CAmount nValueOut = tx->GetValueOut();
1929  nFee = nDebit - nValueOut;
1930  }
1931 
1932  // Sent/received.
1933  for (unsigned int i = 0; i < tx->vout.size(); ++i)
1934  {
1935  const CTxOut& txout = tx->vout[i];
1936  isminetype fIsMine = pwallet->IsMine(txout);
1937  // Only need to handle txouts if AT LEAST one of these is true:
1938  // 1) they debit from us (sent)
1939  // 2) the output is to us (received)
1940  if (nDebit > 0)
1941  {
1942  // Don't report 'change' txouts
1943  if (pwallet->IsChange(txout))
1944  continue;
1945  }
1946  else if (!(fIsMine & filter))
1947  continue;
1948 
1949  // In either case, we need to get the destination address
1950  CTxDestination address;
1951 
1952  if (!ExtractDestination(txout.scriptPubKey, address) && !txout.scriptPubKey.IsUnspendable())
1953  {
1954  pwallet->WalletLogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
1955  this->GetHash().ToString());
1956  address = CNoDestination();
1957  }
1958 
1959  COutputEntry output = {address, txout.nValue, (int)i};
1960 
1961  // If we are debited by the transaction, add the output as a "sent" entry
1962  if (nDebit > 0)
1963  listSent.push_back(output);
1964 
1965  // If we are receiving the output, add it as a "received" entry
1966  if (fIsMine & filter)
1967  listReceived.push_back(output);
1968  }
1969 
1970 }
1971 
1980 int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update)
1981 {
1982  // Find starting block. May be null if nCreateTime is greater than the
1983  // highest blockchain timestamp, in which case there is nothing that needs
1984  // to be scanned.
1985  uint256 start_block;
1986  {
1987  auto locked_chain = chain().lock();
1988  const Optional<int> start_height = locked_chain->findFirstBlockWithTimeAndHeight(startTime - TIMESTAMP_WINDOW, 0, &start_block);
1989  const Optional<int> tip_height = locked_chain->getHeight();
1990  WalletLogPrintf("%s: Rescanning last %i blocks\n", __func__, tip_height && start_height ? *tip_height - *start_height + 1 : 0);
1991  }
1992 
1993  if (!start_block.IsNull()) {
1994  // TODO: this should take into account failure by ScanResult::USER_ABORT
1995  ScanResult result = ScanForWalletTransactions(start_block, {} /* stop_block */, reserver, update);
1996  if (result.status == ScanResult::FAILURE) {
1997  int64_t time_max;
1998  if (!chain().findBlock(result.last_failed_block, nullptr /* block */, nullptr /* time */, &time_max)) {
1999  throw std::logic_error("ScanForWalletTransactions returned invalid block hash");
2000  }
2001  return time_max + TIMESTAMP_WINDOW + 1;
2002  }
2003  }
2004  return startTime;
2005 }
2006 
2028 CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_block, const uint256& stop_block, const WalletRescanReserver& reserver, bool fUpdate)
2029 {
2030  int64_t nNow = GetTime();
2031  int64_t start_time = GetTimeMillis();
2032 
2033  assert(reserver.isReserved());
2034 
2035  uint256 block_hash = start_block;
2036  ScanResult result;
2037 
2038  WalletLogPrintf("Rescan started from block %s...\n", start_block.ToString());
2039 
2040  fAbortRescan = false;
2041  ShowProgress(strprintf("%s " + _("Rescanning...").translated, GetDisplayName()), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
2042  uint256 tip_hash;
2043  // The way the 'block_height' is initialized is just a workaround for the gcc bug #47679 since version 4.6.0.
2044  Optional<int> block_height = MakeOptional(false, int());
2045  double progress_begin;
2046  double progress_end;
2047  {
2048  auto locked_chain = chain().lock();
2049  if (Optional<int> tip_height = locked_chain->getHeight()) {
2050  tip_hash = locked_chain->getBlockHash(*tip_height);
2051  }
2052  block_height = locked_chain->getBlockHeight(block_hash);
2053  progress_begin = chain().guessVerificationProgress(block_hash);
2054  progress_end = chain().guessVerificationProgress(stop_block.IsNull() ? tip_hash : stop_block);
2055  }
2056  double progress_current = progress_begin;
2057  while (block_height && !fAbortRescan && !chain().shutdownRequested()) {
2058  m_scanning_progress = (progress_current - progress_begin) / (progress_end - progress_begin);
2059  if (*block_height % 100 == 0 && progress_end - progress_begin > 0.0) {
2060  ShowProgress(strprintf("%s " + _("Rescanning...").translated, GetDisplayName()), std::max(1, std::min(99, (int)(m_scanning_progress * 100))));
2061  }
2062  if (GetTime() >= nNow + 60) {
2063  nNow = GetTime();
2064  WalletLogPrintf("Still rescanning. At block %d. Progress=%f\n", *block_height, progress_current);
2065  }
2066 
2067  CBlock block;
2068  if (chain().findBlock(block_hash, &block) && !block.IsNull()) {
2069  auto locked_chain = chain().lock();
2070  LOCK(cs_wallet);
2071  if (!locked_chain->getBlockHeight(block_hash)) {
2072  // Abort scan if current block is no longer active, to prevent
2073  // marking transactions as coming from the wrong block.
2074  // TODO: This should return success instead of failure, see
2075  // https://github.com/bitcoin/bitcoin/pull/14711#issuecomment-458342518
2076  result.last_failed_block = block_hash;
2077  result.status = ScanResult::FAILURE;
2078  break;
2079  }
2080  for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
2081  SyncTransaction(block.vtx[posInBlock], block_hash, posInBlock, fUpdate);
2082  }
2083  // scan succeeded, record block as most recent successfully scanned
2084  result.last_scanned_block = block_hash;
2085  result.last_scanned_height = *block_height;
2086  } else {
2087  // could not scan block, keep scanning but record this block as the most recent failure
2088  result.last_failed_block = block_hash;
2089  result.status = ScanResult::FAILURE;
2090  }
2091  if (block_hash == stop_block) {
2092  break;
2093  }
2094  {
2095  auto locked_chain = chain().lock();
2096  Optional<int> tip_height = locked_chain->getHeight();
2097  if (!tip_height || *tip_height <= block_height || !locked_chain->getBlockHeight(block_hash)) {
2098  // break successfully when rescan has reached the tip, or
2099  // previous block is no longer on the chain due to a reorg
2100  break;
2101  }
2102 
2103  // increment block and verification progress
2104  block_hash = locked_chain->getBlockHash(++*block_height);
2105  progress_current = chain().guessVerificationProgress(block_hash);
2106 
2107  // handle updated tip hash
2108  const uint256 prev_tip_hash = tip_hash;
2109  tip_hash = locked_chain->getBlockHash(*tip_height);
2110  if (stop_block.IsNull() && prev_tip_hash != tip_hash) {
2111  // in case the tip has changed, update progress max
2112  progress_end = chain().guessVerificationProgress(tip_hash);
2113  }
2114  }
2115  }
2116  ShowProgress(strprintf("%s " + _("Rescanning...").translated, GetDisplayName()), 100); // hide progress dialog in GUI
2117  if (block_height && fAbortRescan) {
2118  WalletLogPrintf("Rescan aborted at block %d. Progress=%f\n", *block_height, progress_current);
2119  result.status = ScanResult::USER_ABORT;
2120  } else if (block_height && chain().shutdownRequested()) {
2121  WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", *block_height, progress_current);
2122  result.status = ScanResult::USER_ABORT;
2123  } else {
2124  WalletLogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - start_time);
2125  }
2126  return result;
2127 }
2128 
2130 {
2131  // If transactions aren't being broadcasted, don't let them into local mempool either
2132  if (!fBroadcastTransactions)
2133  return;
2134  std::map<int64_t, CWalletTx*> mapSorted;
2135 
2136  // Sort pending wallet transactions based on their initial wallet insertion order
2137  for (std::pair<const uint256, CWalletTx>& item : mapWallet) {
2138  const uint256& wtxid = item.first;
2139  CWalletTx& wtx = item.second;
2140  assert(wtx.GetHash() == wtxid);
2141 
2142  int nDepth = wtx.GetDepthInMainChain(locked_chain);
2143 
2144  if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned())) {
2145  mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx));
2146  }
2147  }
2148 
2149  // Try to add wallet transactions to memory pool
2150  for (const std::pair<const int64_t, CWalletTx*>& item : mapSorted) {
2151  CWalletTx& wtx = *(item.second);
2152  std::string unused_err_string;
2153  wtx.SubmitMemoryPoolAndRelay(unused_err_string, false, locked_chain);
2154  }
2155 }
2156 
2157 bool CWalletTx::SubmitMemoryPoolAndRelay(std::string& err_string, bool relay, interfaces::Chain::Lock& locked_chain)
2158 {
2159  // Can't relay if wallet is not broadcasting
2160  if (!pwallet->GetBroadcastTransactions()) return false;
2161  // Don't relay abandoned transactions
2162  if (isAbandoned()) return false;
2163  // Don't try to submit coinbase transactions. These would fail anyway but would
2164  // cause log spam.
2165  if (IsCoinBase()) return false;
2166  // Don't try to submit conflicted or confirmed transactions.
2167  if (GetDepthInMainChain(locked_chain) != 0) return false;
2168 
2169  // Submit transaction to mempool for relay
2170  pwallet->WalletLogPrintf("Submitting wtx %s to mempool for relay\n", GetHash().ToString());
2171  // We must set fInMempool here - while it will be re-set to true by the
2172  // entered-mempool callback, if we did not there would be a race where a
2173  // user could call sendmoney in a loop and hit spurious out of funds errors
2174  // because we think that this newly generated transaction's change is
2175  // unavailable as we're not yet aware that it is in the mempool.
2176  //
2177  // Irrespective of the failure reason, un-marking fInMempool
2178  // out-of-order is incorrect - it should be unmarked when
2179  // TransactionRemovedFromMempool fires.
2180  bool ret = pwallet->chain().broadcastTransaction(tx, err_string, pwallet->m_default_max_tx_fee, relay);
2181  fInMempool |= ret;
2182  return ret;
2183 }
2184 
2185 std::set<uint256> CWalletTx::GetConflicts() const
2186 {
2187  std::set<uint256> result;
2188  if (pwallet != nullptr)
2189  {
2190  uint256 myHash = GetHash();
2191  result = pwallet->GetConflicts(myHash);
2192  result.erase(myHash);
2193  }
2194  return result;
2195 }
2196 
2197 CAmount CWalletTx::GetCachableAmount(AmountType type, const isminefilter& filter, bool recalculate) const
2198 {
2199  auto& amount = m_amounts[type];
2200  if (recalculate || !amount.m_cached[filter]) {
2201  amount.Set(filter, type == DEBIT ? pwallet->GetDebit(*tx, filter) : pwallet->GetCredit(*tx, filter));
2202  }
2203  return amount.m_value[filter];
2204 }
2205 
2207 {
2208  if (tx->vin.empty())
2209  return 0;
2210 
2211  CAmount debit = 0;
2212  if (filter & ISMINE_SPENDABLE) {
2213  debit += GetCachableAmount(DEBIT, ISMINE_SPENDABLE);
2214  }
2215  if (filter & ISMINE_WATCH_ONLY) {
2216  debit += GetCachableAmount(DEBIT, ISMINE_WATCH_ONLY);
2217  }
2218  return debit;
2219 }
2220 
2222 {
2223  // Must wait until coinbase is safely deep enough in the chain before valuing it
2224  if (IsImmatureCoinBase(locked_chain))
2225  return 0;
2226 
2227  CAmount credit = 0;
2228  if (filter & ISMINE_SPENDABLE) {
2229  // GetBalance can assume transactions in mapWallet won't change
2230  credit += GetCachableAmount(CREDIT, ISMINE_SPENDABLE);
2231  }
2232  if (filter & ISMINE_WATCH_ONLY) {
2233  credit += GetCachableAmount(CREDIT, ISMINE_WATCH_ONLY);
2234  }
2235  return credit;
2236 }
2237 
2239 {
2240  if (IsImmatureCoinBase(locked_chain) && IsInMainChain(locked_chain)) {
2241  return GetCachableAmount(IMMATURE_CREDIT, ISMINE_SPENDABLE, !fUseCache);
2242  }
2243 
2244  return 0;
2245 }
2246 
2247 CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache, const isminefilter& filter) const
2248 {
2249  if (pwallet == nullptr)
2250  return 0;
2251 
2252  // Avoid caching ismine for NO or ALL cases (could remove this check and simplify in the future).
2253  bool allow_cache = (filter & ISMINE_ALL) && (filter & ISMINE_ALL) != ISMINE_ALL;
2254 
2255  // Must wait until coinbase is safely deep enough in the chain before valuing it
2256  if (IsImmatureCoinBase(locked_chain))
2257  return 0;
2258 
2259  if (fUseCache && allow_cache && m_amounts[AVAILABLE_CREDIT].m_cached[filter]) {
2260  return m_amounts[AVAILABLE_CREDIT].m_value[filter];
2261  }
2262 
2263  bool allow_used_addresses = (filter & ISMINE_USED) || !pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
2264  CAmount nCredit = 0;
2265  uint256 hashTx = GetHash();
2266  for (unsigned int i = 0; i < tx->vout.size(); i++)
2267  {
2268  if (!pwallet->IsSpent(locked_chain, hashTx, i) && (allow_used_addresses || !pwallet->IsUsedDestination(hashTx, i))) {
2269  const CTxOut &txout = tx->vout[i];
2270  nCredit += pwallet->GetCredit(txout, filter);
2271  if (!MoneyRange(nCredit))
2272  throw std::runtime_error(std::string(__func__) + " : value out of range");
2273  }
2274  }
2275 
2276  if (allow_cache) {
2277  m_amounts[AVAILABLE_CREDIT].Set(filter, nCredit);
2278  }
2279 
2280  return nCredit;
2281 }
2282 
2284 {
2285  if (IsImmatureCoinBase(locked_chain) && IsInMainChain(locked_chain)) {
2286  return GetCachableAmount(IMMATURE_CREDIT, ISMINE_WATCH_ONLY, !fUseCache);
2287  }
2288 
2289  return 0;
2290 }
2291 
2293 {
2294  if (fChangeCached)
2295  return nChangeCached;
2296  nChangeCached = pwallet->GetChange(*tx);
2297  fChangeCached = true;
2298  return nChangeCached;
2299 }
2300 
2302 {
2303  return fInMempool;
2304 }
2305 
2307 {
2308  // Quick answer in most cases
2309  if (!locked_chain.checkFinalTx(*tx)) {
2310  return false;
2311  }
2312  int nDepth = GetDepthInMainChain(locked_chain);
2313  if (nDepth >= 1)
2314  return true;
2315  if (nDepth < 0)
2316  return false;
2317  if (!pwallet->m_spend_zero_conf_change || !IsFromMe(ISMINE_ALL)) // using wtx's cached debit
2318  return false;
2319 
2320  // Don't trust unconfirmed transactions from us unless they are in the mempool.
2321  if (!InMempool())
2322  return false;
2323 
2324  // Trusted if all inputs are from us and are in the mempool:
2325  for (const CTxIn& txin : tx->vin)
2326  {
2327  // Transactions not sent by us: not trusted
2328  const CWalletTx* parent = pwallet->GetWalletTx(txin.prevout.hash);
2329  if (parent == nullptr)
2330  return false;
2331  const CTxOut& parentOut = parent->tx->vout[txin.prevout.n];
2332  if (pwallet->IsMine(parentOut) != ISMINE_SPENDABLE)
2333  return false;
2334  }
2335  return true;
2336 }
2337 
2338 bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const
2339 {
2340  CMutableTransaction tx1 {*this->tx};
2341  CMutableTransaction tx2 {*_tx.tx};
2342  for (auto& txin : tx1.vin) txin.scriptSig = CScript();
2343  for (auto& txin : tx2.vin) txin.scriptSig = CScript();
2344  return CTransaction(tx1) == CTransaction(tx2);
2345 }
2346 
2347 // Rebroadcast transactions from the wallet. We do this on a random timer
2348 // to slightly obfuscate which transactions come from our wallet.
2349 //
2350 // Ideally, we'd only resend transactions that we think should have been
2351 // mined in the most recent block. Any transaction that wasn't in the top
2352 // blockweight of transactions in the mempool shouldn't have been mined,
2353 // and so is probably just sitting in the mempool waiting to be confirmed.
2354 // Rebroadcasting does nothing to speed up confirmation and only damages
2355 // privacy.
2357 {
2358  // During reindex, importing and IBD, old wallet transactions become
2359  // unconfirmed. Don't resend them as that would spam other nodes.
2360  if (!chain().isReadyToBroadcast()) return;
2361 
2362  // Do this infrequently and randomly to avoid giving away
2363  // that these are our transactions.
2364  if (GetTime() < nNextResend || !fBroadcastTransactions) return;
2365  bool fFirst = (nNextResend == 0);
2366  nNextResend = GetTime() + GetRand(30 * 60);
2367  if (fFirst) return;
2368 
2369  // Only do it if there's been a new block since last time
2370  if (m_best_block_time < nLastResend) return;
2371  nLastResend = GetTime();
2372 
2373  int submitted_tx_count = 0;
2374 
2375  { // locked_chain and cs_wallet scope
2376  auto locked_chain = chain().lock();
2377  LOCK(cs_wallet);
2378 
2379  // Relay transactions
2380  for (std::pair<const uint256, CWalletTx>& item : mapWallet) {
2381  CWalletTx& wtx = item.second;
2382  // Attempt to rebroadcast all txes more than 5 minutes older than
2383  // the last block. SubmitMemoryPoolAndRelay() will not rebroadcast
2384  // any confirmed or conflicting txs.
2385  if (wtx.nTimeReceived > m_best_block_time - 5 * 60) continue;
2386  std::string unused_err_string;
2387  if (wtx.SubmitMemoryPoolAndRelay(unused_err_string, true, *locked_chain)) ++submitted_tx_count;
2388  }
2389  } // locked_chain and cs_wallet
2390 
2391  if (submitted_tx_count > 0) {
2392  WalletLogPrintf("%s: resubmit %u unconfirmed transactions\n", __func__, submitted_tx_count);
2393  }
2394 }
2395  // end of mapWallet
2397 
2399 {
2400  for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
2401  pwallet->ResendWalletTransactions();
2402  }
2403 }
2404 
2405 
2412 CWallet::Balance CWallet::GetBalance(const int min_depth, bool avoid_reuse) const
2413 {
2414  Balance ret;
2415  isminefilter reuse_filter = avoid_reuse ? ISMINE_NO : ISMINE_USED;
2416  {
2417  auto locked_chain = chain().lock();
2418  LOCK(cs_wallet);
2419  for (const auto& entry : mapWallet)
2420  {
2421  const CWalletTx& wtx = entry.second;
2422  const bool is_trusted{wtx.IsTrusted(*locked_chain)};
2423  const int tx_depth{wtx.GetDepthInMainChain(*locked_chain)};
2424  const CAmount tx_credit_mine{wtx.GetAvailableCredit(*locked_chain, /* fUseCache */ true, ISMINE_SPENDABLE | reuse_filter)};
2425  const CAmount tx_credit_watchonly{wtx.GetAvailableCredit(*locked_chain, /* fUseCache */ true, ISMINE_WATCH_ONLY | reuse_filter)};
2426  if (is_trusted && tx_depth >= min_depth) {
2427  ret.m_mine_trusted += tx_credit_mine;
2428  ret.m_watchonly_trusted += tx_credit_watchonly;
2429  }
2430  if (!is_trusted && tx_depth == 0 && wtx.InMempool()) {
2431  ret.m_mine_untrusted_pending += tx_credit_mine;
2432  ret.m_watchonly_untrusted_pending += tx_credit_watchonly;
2433  }
2434  ret.m_mine_immature += wtx.GetImmatureCredit(*locked_chain);
2435  ret.m_watchonly_immature += wtx.GetImmatureWatchOnlyCredit(*locked_chain);
2436  }
2437  }
2438  return ret;
2439 }
2440 
2442 {
2443  auto locked_chain = chain().lock();
2444  LOCK(cs_wallet);
2445 
2446  CAmount balance = 0;
2447  std::vector<COutput> vCoins;
2448  AvailableCoins(*locked_chain, vCoins, true, coinControl);
2449  for (const COutput& out : vCoins) {
2450  if (out.fSpendable) {
2451  balance += out.tx->tx->vout[out.i].nValue;
2452  }
2453  }
2454  return balance;
2455 }
2456 
2457 void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<COutput>& vCoins, bool fOnlySafe, const CCoinControl* coinControl, const CAmount& nMinimumAmount, const CAmount& nMaximumAmount, const CAmount& nMinimumSumAmount, const uint64_t nMaximumCount) const
2458 {
2459  AssertLockHeld(cs_wallet);
2460 
2461  vCoins.clear();
2462  CAmount nTotal = 0;
2463  // Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we always allow), or we default to avoiding, and only in the case where
2464  // a coin control object is provided, and has the avoid address reuse flag set to false, do we allow already used addresses
2465  bool allow_used_addresses = !IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE) || (coinControl && !coinControl->m_avoid_address_reuse);
2466  const int min_depth = {coinControl ? coinControl->m_min_depth : DEFAULT_MIN_DEPTH};
2467  const int max_depth = {coinControl ? coinControl->m_max_depth : DEFAULT_MAX_DEPTH};
2468 
2469  for (const auto& entry : mapWallet)
2470  {
2471  const uint256& wtxid = entry.first;
2472  const CWalletTx& wtx = entry.second;
2473 
2474  if (!locked_chain.checkFinalTx(*wtx.tx)) {
2475  continue;
2476  }
2477 
2478  if (wtx.IsImmatureCoinBase(locked_chain))
2479  continue;
2480 
2481  int nDepth = wtx.GetDepthInMainChain(locked_chain);
2482  if (nDepth < 0)
2483  continue;
2484 
2485  // We should not consider coins which aren't at least in our mempool
2486  // It's possible for these to be conflicted via ancestors which we may never be able to detect
2487  if (nDepth == 0 && !wtx.InMempool())
2488  continue;
2489 
2490  bool safeTx = wtx.IsTrusted(locked_chain);
2491 
2492  // We should not consider coins from transactions that are replacing
2493  // other transactions.
2494  //
2495  // Example: There is a transaction A which is replaced by bumpfee
2496  // transaction B. In this case, we want to prevent creation of
2497  // a transaction B' which spends an output of B.
2498  //
2499  // Reason: If transaction A were initially confirmed, transactions B
2500  // and B' would no longer be valid, so the user would have to create
2501  // a new transaction C to replace B'. However, in the case of a
2502  // one-block reorg, transactions B' and C might BOTH be accepted,
2503  // when the user only wanted one of them. Specifically, there could
2504  // be a 1-block reorg away from the chain where transactions A and C
2505  // were accepted to another chain where B, B', and C were all
2506  // accepted.
2507  if (nDepth == 0 && wtx.mapValue.count("replaces_txid")) {
2508  safeTx = false;
2509  }
2510 
2511  // Similarly, we should not consider coins from transactions that
2512  // have been replaced. In the example above, we would want to prevent
2513  // creation of a transaction A' spending an output of A, because if
2514  // transaction B were initially confirmed, conflicting with A and
2515  // A', we wouldn't want to the user to create a transaction D
2516  // intending to replace A', but potentially resulting in a scenario
2517  // where A, A', and D could all be accepted (instead of just B and
2518  // D, or just A and A' like the user would want).
2519  if (nDepth == 0 && wtx.mapValue.count("replaced_by_txid")) {
2520  safeTx = false;
2521  }
2522 
2523  if (fOnlySafe && !safeTx) {
2524  continue;
2525  }
2526 
2527  if (nDepth < min_depth || nDepth > max_depth) {
2528  continue;
2529  }
2530 
2531  for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
2532  if (wtx.tx->vout[i].nValue < nMinimumAmount || wtx.tx->vout[i].nValue > nMaximumAmount)
2533  continue;
2534 
2535  if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs && !coinControl->IsSelected(COutPoint(entry.first, i)))
2536  continue;
2537 
2538  if (IsLockedCoin(entry.first, i))
2539  continue;
2540 
2541  if (IsSpent(locked_chain, wtxid, i))
2542  continue;
2543 
2544  isminetype mine = IsMine(wtx.tx->vout[i]);
2545 
2546  if (mine == ISMINE_NO) {
2547  continue;
2548  }
2549 
2550  if (!allow_used_addresses && IsUsedDestination(wtxid, i)) {
2551  continue;
2552  }
2553 
2554  bool solvable = IsSolvable(*this, wtx.tx->vout[i].scriptPubKey);
2555  bool spendable = ((mine & ISMINE_SPENDABLE) != ISMINE_NO) || (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) && (coinControl && coinControl->fAllowWatchOnly && solvable));
2556 
2557  vCoins.push_back(COutput(&wtx, i, nDepth, spendable, solvable, safeTx, (coinControl && coinControl->fAllowWatchOnly)));
2558 
2559  // Checks the sum amount of all UTXO's.
2560  if (nMinimumSumAmount != MAX_MONEY) {
2561  nTotal += wtx.tx->vout[i].nValue;
2562 
2563  if (nTotal >= nMinimumSumAmount) {
2564  return;
2565  }
2566  }
2567 
2568  // Checks the maximum number of UTXO's.
2569  if (nMaximumCount > 0 && vCoins.size() >= nMaximumCount) {
2570  return;
2571  }
2572  }
2573  }
2574 }
2575 
2576 std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins(interfaces::Chain::Lock& locked_chain) const
2577 {
2578  AssertLockHeld(cs_wallet);
2579 
2580  std::map<CTxDestination, std::vector<COutput>> result;
2581  std::vector<COutput> availableCoins;
2582 
2583  AvailableCoins(locked_chain, availableCoins);
2584 
2585  for (const COutput& coin : availableCoins) {
2586  CTxDestination address;
2587  if (coin.fSpendable &&
2588  ExtractDestination(FindNonChangeParentOutput(*coin.tx->tx, coin.i).scriptPubKey, address)) {
2589  result[address].emplace_back(std::move(coin));
2590  }
2591  }
2592 
2593  std::vector<COutPoint> lockedCoins;
2594  ListLockedCoins(lockedCoins);
2595  for (const COutPoint& output : lockedCoins) {
2596  auto it = mapWallet.find(output.hash);
2597  if (it != mapWallet.end()) {
2598  int depth = it->second.GetDepthInMainChain(locked_chain);
2599  if (depth >= 0 && output.n < it->second.tx->vout.size() &&
2600  IsMine(it->second.tx->vout[output.n]) == ISMINE_SPENDABLE) {
2601  CTxDestination address;
2602  if (ExtractDestination(FindNonChangeParentOutput(*it->second.tx, output.n).scriptPubKey, address)) {
2603  result[address].emplace_back(
2604  &it->second, output.n, depth, true /* spendable */, true /* solvable */, false /* safe */);
2605  }
2606  }
2607  }
2608  }
2609 
2610  return result;
2611 }
2612 
2614 {
2615  const CTransaction* ptx = &tx;
2616  int n = output;
2617  while (IsChange(ptx->vout[n]) && ptx->vin.size() > 0) {
2618  const COutPoint& prevout = ptx->vin[0].prevout;
2619  auto it = mapWallet.find(prevout.hash);
2620  if (it == mapWallet.end() || it->second.tx->vout.size() <= prevout.n ||
2621  !IsMine(it->second.tx->vout[prevout.n])) {
2622  break;
2623  }
2624  ptx = it->second.tx.get();
2625  n = prevout.n;
2626  }
2627  return ptx->vout[n];
2628 }
2629 
2630 bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, std::vector<OutputGroup> groups,
2631  std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CoinSelectionParams& coin_selection_params, bool& bnb_used) const
2632 {
2633  setCoinsRet.clear();
2634  nValueRet = 0;
2635 
2636  std::vector<OutputGroup> utxo_pool;
2637  if (coin_selection_params.use_bnb) {
2638  // Get long term estimate
2639  FeeCalculation feeCalc;
2640  CCoinControl temp;
2641  temp.m_confirm_target = 1008;
2642  CFeeRate long_term_feerate = GetMinimumFeeRate(*this, temp, &feeCalc);
2643 
2644  // Calculate cost of change
2645  CAmount cost_of_change = GetDiscardRate(*this).GetFee(coin_selection_params.change_spend_size) + coin_selection_params.effective_fee.GetFee(coin_selection_params.change_output_size);
2646 
2647  // Filter by the min conf specs and add to utxo_pool and calculate effective value
2648  for (OutputGroup& group : groups) {
2649  if (!group.EligibleForSpending(eligibility_filter)) continue;
2650 
2651  group.fee = 0;
2652  group.long_term_fee = 0;
2653  group.effective_value = 0;
2654  for (auto it = group.m_outputs.begin(); it != group.m_outputs.end(); ) {
2655  const CInputCoin& coin = *it;
2656  CAmount effective_value = coin.txout.nValue - (coin.m_input_bytes < 0 ? 0 : coin_selection_params.effective_fee.GetFee(coin.m_input_bytes));
2657  // Only include outputs that are positive effective value (i.e. not dust)
2658  if (effective_value > 0) {
2659  group.fee += coin.m_input_bytes < 0 ? 0 : coin_selection_params.effective_fee.GetFee(coin.m_input_bytes);
2660  group.long_term_fee += coin.m_input_bytes < 0 ? 0 : long_term_feerate.GetFee(coin.m_input_bytes);
2661  group.effective_value += effective_value;
2662  ++it;
2663  } else {
2664  it = group.Discard(coin);
2665  }
2666  }
2667  if (group.effective_value > 0) utxo_pool.push_back(group);
2668  }
2669  // Calculate the fees for things that aren't inputs
2670  CAmount not_input_fees = coin_selection_params.effective_fee.GetFee(coin_selection_params.tx_noinputs_size);
2671  bnb_used = true;
2672  return SelectCoinsBnB(utxo_pool, nTargetValue, cost_of_change, setCoinsRet, nValueRet, not_input_fees);
2673  } else {
2674  // Filter by the min conf specs and add to utxo_pool
2675  for (const OutputGroup& group : groups) {
2676  if (!group.EligibleForSpending(eligibility_filter)) continue;
2677  utxo_pool.push_back(group);
2678  }
2679  bnb_used = false;
2680  return KnapsackSolver(nTargetValue, utxo_pool, setCoinsRet, nValueRet);
2681  }
2682 }
2683 
2684 bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CCoinControl& coin_control, CoinSelectionParams& coin_selection_params, bool& bnb_used) const
2685 {
2686  std::vector<COutput> vCoins(vAvailableCoins);
2687 
2688  // coin control -> return all selected outputs (we want all selected to go into the transaction for sure)
2689  if (coin_control.HasSelected() && !coin_control.fAllowOtherInputs)
2690  {
2691  // We didn't use BnB here, so set it to false.
2692  bnb_used = false;
2693 
2694  for (const COutput& out : vCoins)
2695  {
2696  if (!out.fSpendable)
2697  continue;
2698  nValueRet += out.tx->tx->vout[out.i].nValue;
2699  setCoinsRet.insert(out.GetInputCoin());
2700  }
2701  return (nValueRet >= nTargetValue);
2702  }
2703 
2704  // calculate value from preset inputs and store them
2705  std::set<CInputCoin> setPresetCoins;
2706  CAmount nValueFromPresetInputs = 0;
2707 
2708  std::vector<COutPoint> vPresetInputs;
2709  coin_control.ListSelected(vPresetInputs);
2710  for (const COutPoint& outpoint : vPresetInputs)
2711  {
2712  // For now, don't use BnB if preset inputs are selected. TODO: Enable this later
2713  bnb_used = false;
2714  coin_selection_params.use_bnb = false;
2715 
2716  std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(outpoint.hash);
2717  if (it != mapWallet.end())
2718  {
2719  const CWalletTx& wtx = it->second;
2720  // Clearly invalid input, fail
2721  if (wtx.tx->vout.size() <= outpoint.n)
2722  return false;
2723  // Just to calculate the marginal byte size
2724  nValueFromPresetInputs += wtx.tx->vout[outpoint.n].nValue;
2725  setPresetCoins.insert(CInputCoin(wtx.tx, outpoint.n));
2726  } else
2727  return false; // TODO: Allow non-wallet inputs
2728  }
2729 
2730  // remove preset inputs from vCoins
2731  for (std::vector<COutput>::iterator it = vCoins.begin(); it != vCoins.end() && coin_control.HasSelected();)
2732  {
2733  if (setPresetCoins.count(it->GetInputCoin()))
2734  it = vCoins.erase(it);
2735  else
2736  ++it;
2737  }
2738 
2739  // form groups from remaining coins; note that preset coins will not
2740  // automatically have their associated (same address) coins included
2741  if (coin_control.m_avoid_partial_spends && vCoins.size() > OUTPUT_GROUP_MAX_ENTRIES) {
2742  // Cases where we have 11+ outputs all pointing to the same destination may result in
2743  // privacy leaks as they will potentially be deterministically sorted. We solve that by
2744  // explicitly shuffling the outputs before processing
2745  Shuffle(vCoins.begin(), vCoins.end(), FastRandomContext());
2746  }
2747  std::vector<OutputGroup> groups = GroupOutputs(vCoins, !coin_control.m_avoid_partial_spends);
2748 
2749  size_t max_ancestors = (size_t)std::max<int64_t>(1, gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT));
2750  size_t max_descendants = (size_t)std::max<int64_t>(1, gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT));
2751  bool fRejectLongChains = gArgs.GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS);
2752 
2753  bool res = nTargetValue <= nValueFromPresetInputs ||
2754  SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, CoinEligibilityFilter(1, 6, 0), groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used) ||
2755  SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, CoinEligibilityFilter(1, 1, 0), groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used) ||
2756  (m_spend_zero_conf_change && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, CoinEligibilityFilter(0, 1, 2), groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2757  (m_spend_zero_conf_change && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, CoinEligibilityFilter(0, 1, std::min((size_t)4, max_ancestors/3), std::min((size_t)4, max_descendants/3)), groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2758  (m_spend_zero_conf_change && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, CoinEligibilityFilter(0, 1, max_ancestors/2, max_descendants/2), groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2759  (m_spend_zero_conf_change && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, CoinEligibilityFilter(0, 1, max_ancestors-1, max_descendants-1), groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2760  (m_spend_zero_conf_change && !fRejectLongChains && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, CoinEligibilityFilter(0, 1, std::numeric_limits<uint64_t>::max()), groups, setCoinsRet, nValueRet, coin_selection_params, bnb_used));
2761 
2762  // because SelectCoinsMinConf clears the setCoinsRet, we now add the possible inputs to the coinset
2763  util::insert(setCoinsRet, setPresetCoins);
2764 
2765  // add preset inputs to the total value selected
2766  nValueRet += nValueFromPresetInputs;
2767 
2768  return res;
2769 }
2770 
2772 {
2773  AssertLockHeld(cs_wallet);
2774 
2775  // sign the new tx
2776  int nIn = 0;
2777  for (auto& input : tx.vin) {
2778  std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(input.prevout.hash);
2779  if(mi == mapWallet.end() || input.prevout.n >= mi->second.tx->vout.size()) {
2780  return false;
2781  }
2782  const CScript& scriptPubKey = mi->second.tx->vout[input.prevout.n].scriptPubKey;
2783  const CAmount& amount = mi->second.tx->vout[input.prevout.n].nValue;
2784  SignatureData sigdata;
2785  if (!ProduceSignature(*this, MutableTransactionSignatureCreator(&tx, nIn, amount, SIGHASH_ALL), scriptPubKey, sigdata)) {
2786  return false;
2787  }
2788  UpdateInput(input, sigdata);
2789  nIn++;
2790  }
2791  return true;
2792 }
2793 
2794 bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl coinControl)
2795 {
2796  std::vector<CRecipient> vecSend;
2797 
2798  // Turn the txout set into a CRecipient vector.
2799  for (size_t idx = 0; idx < tx.vout.size(); idx++) {
2800  const CTxOut& txOut = tx.vout[idx];
2801  CRecipient recipient = {txOut.scriptPubKey, txOut.nValue, setSubtractFeeFromOutputs.count(idx) == 1};
2802  vecSend.push_back(recipient);
2803  }
2804 
2805  coinControl.fAllowOtherInputs = true;
2806 
2807  for (const CTxIn& txin : tx.vin) {
2808  coinControl.Select(txin.prevout);
2809  }
2810 
2811  // Acquire the locks to prevent races to the new locked unspents between the
2812  // CreateTransaction call and LockCoin calls (when lockUnspents is true).
2813  auto locked_chain = chain().lock();
2814  LOCK(cs_wallet);
2815 
2816  CTransactionRef tx_new;
2817  if (!CreateTransaction(*locked_chain, vecSend, tx_new, nFeeRet, nChangePosInOut, strFailReason, coinControl, false)) {
2818  return false;
2819  }
2820 
2821  if (nChangePosInOut != -1) {
2822  tx.vout.insert(tx.vout.begin() + nChangePosInOut, tx_new->vout[nChangePosInOut]);
2823  }
2824 
2825  // Copy output sizes from new transaction; they may have had the fee
2826  // subtracted from them.
2827  for (unsigned int idx = 0; idx < tx.vout.size(); idx++) {
2828  tx.vout[idx].nValue = tx_new->vout[idx].nValue;
2829  }
2830 
2831  // Add new txins while keeping original txin scriptSig/order.
2832  for (const CTxIn& txin : tx_new->vin) {
2833  if (!coinControl.IsSelected(txin.prevout)) {
2834  tx.vin.push_back(txin);
2835 
2836  if (lockUnspents) {
2837  LockCoin(txin.prevout);
2838  }
2839  }
2840  }
2841 
2842  return true;
2843 }
2844 
2846 {
2847  if (chain.isInitialBlockDownload()) {
2848  return false;
2849  }
2850  constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60; // in seconds
2851  if (locked_chain.getBlockTime(*locked_chain.getHeight()) < (GetTime() - MAX_ANTI_FEE_SNIPING_TIP_AGE)) {
2852  return false;
2853  }
2854  return true;
2855 }
2856 
2862 {
2863  uint32_t const height = locked_chain.getHeight().get_value_or(-1);
2864  uint32_t locktime;
2865  // Discourage fee sniping.
2866  //
2867  // For a large miner the value of the transactions in the best block and
2868  // the mempool can exceed the cost of deliberately attempting to mine two
2869  // blocks to orphan the current best block. By setting nLockTime such that
2870  // only the next block can include the transaction, we discourage this
2871  // practice as the height restricted and limited blocksize gives miners
2872  // considering fee sniping fewer options for pulling off this attack.
2873  //
2874  // A simple way to think about this is from the wallet's point of view we
2875  // always want the blockchain to move forward. By setting nLockTime this
2876  // way we're basically making the statement that we only want this
2877  // transaction to appear in the next block; we don't want to potentially
2878  // encourage reorgs by allowing transactions to appear at lower heights
2879  // than the next block in forks of the best chain.
2880  //
2881  // Of course, the subsidy is high enough, and transaction volume low
2882  // enough, that fee sniping isn't a problem yet, but by implementing a fix
2883  // now we ensure code won't be written that makes assumptions about
2884  // nLockTime that preclude a fix later.
2885  if (IsCurrentForAntiFeeSniping(chain, locked_chain)) {
2886  locktime = height;
2887 
2888  // Secondly occasionally randomly pick a nLockTime even further back, so
2889  // that transactions that are delayed after signing for whatever reason,
2890  // e.g. high-latency mix networks and some CoinJoin implementations, have
2891  // better privacy.
2892  if (GetRandInt(10) == 0)
2893  locktime = std::max(0, (int)locktime - GetRandInt(100));
2894  } else {
2895  // If our chain is lagging behind, we can't discourage fee sniping nor help
2896  // the privacy of high-latency transactions. To avoid leaking a potentially
2897  // unique "nLockTime fingerprint", set nLockTime to a constant.
2898  locktime = 0;
2899  }
2900  assert(locktime <= height);
2901  assert(locktime < LOCKTIME_THRESHOLD);
2902  return locktime;
2903 }
2904 
2905 OutputType CWallet::TransactionChangeType(OutputType change_type, const std::vector<CRecipient>& vecSend)
2906 {
2907  // If -changetype is specified, always use that change type.
2908  if (change_type != OutputType::CHANGE_AUTO) {
2909  return change_type;
2910  }
2911 
2912  // if m_default_address_type is legacy, use legacy address as change (even
2913  // if some of the outputs are P2WPKH or P2WSH).
2914  if (m_default_address_type == OutputType::LEGACY) {
2915  return OutputType::LEGACY;
2916  }
2917 
2918  // if any destination is P2WPKH or P2WSH, use P2WPKH for the change
2919  // output.
2920  for (const auto& recipient : vecSend) {
2921  // Check if any destination contains a witness program:
2922  int witnessversion = 0;
2923  std::vector<unsigned char> witnessprogram;
2924  if (recipient.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
2925  return OutputType::BECH32;
2926  }
2927  }
2928 
2929  // else use m_default_address_type for change
2930  return m_default_address_type;
2931 }
2932 
2933 bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet,
2934  int& nChangePosInOut, std::string& strFailReason, const CCoinControl& coin_control, bool sign)
2935 {
2936  CAmount nValue = 0;
2937  ReserveDestination reservedest(this);
2938  int nChangePosRequest = nChangePosInOut;
2939  unsigned int nSubtractFeeFromAmount = 0;
2940  for (const auto& recipient : vecSend)
2941  {
2942  if (nValue < 0 || recipient.nAmount < 0)
2943  {
2944  strFailReason = _("Transaction amounts must not be negative").translated;
2945  return false;
2946  }
2947  nValue += recipient.nAmount;
2948 
2949  if (recipient.fSubtractFeeFromAmount)
2950  nSubtractFeeFromAmount++;
2951  }
2952  if (vecSend.empty())
2953  {
2954  strFailReason = _("Transaction must have at least one recipient").translated;
2955  return false;
2956  }
2957 
2958  CMutableTransaction txNew;
2959 
2960  txNew.nLockTime = GetLocktimeForNewTransaction(chain(), locked_chain);
2961 
2962  FeeCalculation feeCalc;
2963  CAmount nFeeNeeded;
2964  int nBytes;
2965  {
2966  std::set<CInputCoin> setCoins;
2967  auto locked_chain = chain().lock();
2968  LOCK(cs_wallet);
2969  {
2970  std::vector<COutput> vAvailableCoins;
2971  AvailableCoins(*locked_chain, vAvailableCoins, true, &coin_control, 1, MAX_MONEY, MAX_MONEY, 0);
2972  CoinSelectionParams coin_selection_params; // Parameters for coin selection, init with dummy
2973 
2974  // Create change script that will be used if we need change
2975  // TODO: pass in scriptChange instead of reservedest so
2976  // change transaction isn't always pay-to-bitcoin-address
2977  CScript scriptChange;
2978 
2979  // coin control: send change to custom address
2980  if (!boost::get<CNoDestination>(&coin_control.destChange)) {
2981  scriptChange = GetScriptForDestination(coin_control.destChange);
2982  } else { // no coin control: send change to newly generated address
2983  // Note: We use a new key here to keep it from being obvious which side is the change.
2984  // The drawback is that by not reusing a previous key, the change may be lost if a
2985  // backup is restored, if the backup doesn't have the new private key for the change.
2986  // If we reused the old key, it would be possible to add code to look for and
2987  // rediscover unknown transactions that were written with keys of ours to recover
2988  // post-backup change.
2989 
2990  // Reserve a new key pair from key pool
2991  if (!CanGetAddresses(true)) {
2992  strFailReason = _("Can't generate a change-address key. No keys in the internal keypool and can't generate any keys.").translated;
2993  return false;
2994  }
2995  CTxDestination dest;
2996  const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend);
2997  bool ret = reservedest.GetReservedDestination(change_type, dest, true);
2998  if (!ret)
2999  {
3000  strFailReason = "Keypool ran out, please call keypoolrefill first";
3001  return false;
3002  }
3003 
3004  scriptChange = GetScriptForDestination(dest);
3005  }
3006  CTxOut change_prototype_txout(0, scriptChange);
3007  coin_selection_params.change_output_size = GetSerializeSize(change_prototype_txout);
3008 
3009  CFeeRate discard_rate = GetDiscardRate(*this);
3010 
3011  // Get the fee rate to use effective values in coin selection
3012  CFeeRate nFeeRateNeeded = GetMinimumFeeRate(*this, coin_control, &feeCalc);
3013 
3014  nFeeRet = 0;
3015  bool pick_new_inputs = true;
3016  CAmount nValueIn = 0;
3017 
3018  // BnB selector is the only selector used when this is true.
3019  // That should only happen on the first pass through the loop.
3020  coin_selection_params.use_bnb = nSubtractFeeFromAmount == 0; // If we are doing subtract fee from recipient, then don't use BnB
3021  // Start with no fee and loop until there is enough fee
3022  while (true)
3023  {
3024  nChangePosInOut = nChangePosRequest;
3025  txNew.vin.clear();
3026  txNew.vout.clear();
3027  bool fFirst = true;
3028 
3029  CAmount nValueToSelect = nValue;
3030  if (nSubtractFeeFromAmount == 0)
3031  nValueToSelect += nFeeRet;
3032 
3033  // vouts to the payees
3034  coin_selection_params.tx_noinputs_size = 11; // Static vsize overhead + outputs vsize. 4 nVersion, 4 nLocktime, 1 input count, 1 output count, 1 witness overhead (dummy, flag, stack size)
3035  for (const auto& recipient : vecSend)
3036  {
3037  CTxOut txout(recipient.nAmount, recipient.scriptPubKey);
3038 
3039  if (recipient.fSubtractFeeFromAmount)
3040  {
3041  assert(nSubtractFeeFromAmount != 0);
3042  txout.nValue -= nFeeRet / nSubtractFeeFromAmount; // Subtract fee equally from each selected recipient
3043 
3044  if (fFirst) // first receiver pays the remainder not divisible by output count
3045  {
3046  fFirst = false;
3047  txout.nValue -= nFeeRet % nSubtractFeeFromAmount;
3048  }
3049  }
3050  // Include the fee cost for outputs. Note this is only used for BnB right now
3051  coin_selection_params.tx_noinputs_size += ::GetSerializeSize(txout, PROTOCOL_VERSION);
3052 
3053  if (IsDust(txout, chain().relayDustFee()))
3054  {
3055  if (recipient.fSubtractFeeFromAmount && nFeeRet > 0)
3056  {
3057  if (txout.nValue < 0)
3058  strFailReason = _("The transaction amount is too small to pay the fee").translated;
3059  else
3060  strFailReason = _("The transaction amount is too small to send after the fee has been deducted").translated;
3061  }
3062  else
3063  strFailReason = _("Transaction amount too small").translated;
3064  return false;
3065  }
3066  txNew.vout.push_back(txout);
3067  }
3068 
3069  // Choose coins to use
3070  bool bnb_used;
3071  if (pick_new_inputs) {
3072  nValueIn = 0;
3073  setCoins.clear();
3074  int change_spend_size = CalculateMaximumSignedInputSize(change_prototype_txout, this);
3075  // If the wallet doesn't know how to sign change output, assume p2sh-p2wpkh
3076  // as lower-bound to allow BnB to do it's thing
3077  if (change_spend_size == -1) {
3078  coin_selection_params.change_spend_size = DUMMY_NESTED_P2WPKH_INPUT_SIZE;
3079  } else {
3080  coin_selection_params.change_spend_size = (size_t)change_spend_size;
3081  }
3082  coin_selection_params.effective_fee = nFeeRateNeeded;
3083  if (!SelectCoins(vAvailableCoins, nValueToSelect, setCoins, nValueIn, coin_control, coin_selection_params, bnb_used))
3084  {
3085  // If BnB was used, it was the first pass. No longer the first pass and continue loop with knapsack.
3086  if (bnb_used) {
3087  coin_selection_params.use_bnb = false;
3088  continue;
3089  }
3090  else {
3091  strFailReason = _("Insufficient funds").translated;
3092  return false;
3093  }
3094  }
3095  } else {
3096  bnb_used = false;
3097  }
3098 
3099  const CAmount nChange = nValueIn - nValueToSelect;
3100  if (nChange > 0)
3101  {
3102  // Fill a vout to ourself
3103  CTxOut newTxOut(nChange, scriptChange);
3104 
3105  // Never create dust outputs; if we would, just
3106  // add the dust to the fee.
3107  // The nChange when BnB is used is always going to go to fees.
3108  if (IsDust(newTxOut, discard_rate) || bnb_used)
3109  {
3110  nChangePosInOut = -1;
3111  nFeeRet += nChange;
3112  }
3113  else
3114  {
3115  if (nChangePosInOut == -1)
3116  {
3117  // Insert change txn at random position:
3118  nChangePosInOut = GetRandInt(txNew.vout.size()+1);
3119  }
3120  else if ((unsigned int)nChangePosInOut > txNew.vout.size())
3121  {
3122  strFailReason = _("Change index out of range").translated;
3123  return false;
3124  }
3125 
3126  std::vector<CTxOut>::iterator position = txNew.vout.begin()+nChangePosInOut;
3127  txNew.vout.insert(position, newTxOut);
3128  }
3129  } else {
3130  nChangePosInOut = -1;
3131  }
3132 
3133  // Dummy fill vin for maximum size estimation
3134  //
3135  for (const auto& coin : setCoins) {
3136  txNew.vin.push_back(CTxIn(coin.outpoint,CScript()));
3137  }
3138 
3139  nBytes = CalculateMaximumSignedTxSize(CTransaction(txNew), this, coin_control.fAllowWatchOnly);
3140  if (nBytes < 0) {
3141  strFailReason = _("Signing transaction failed").translated;
3142  return false;
3143  }
3144 
3145  nFeeNeeded = GetMinimumFee(*this, nBytes, coin_control, &feeCalc);
3146  if (feeCalc.reason == FeeReason::FALLBACK && !m_allow_fallback_fee) {
3147  // eventually allow a fallback fee
3148  strFailReason = _("Fee estimation failed. Fallbackfee is disabled. Wait a few blocks or enable -fallbackfee.").translated;
3149  return false;
3150  }
3151 
3152  if (nFeeRet >= nFeeNeeded) {
3153  // Reduce fee to only the needed amount if possible. This
3154  // prevents potential overpayment in fees if the coins
3155  // selected to meet nFeeNeeded result in a transaction that
3156  // requires less fee than the prior iteration.
3157 
3158  // If we have no change and a big enough excess fee, then
3159  // try to construct transaction again only without picking
3160  // new inputs. We now know we only need the smaller fee
3161  // (because of reduced tx size) and so we should add a
3162  // change output. Only try this once.
3163  if (nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 && pick_new_inputs) {
3164  unsigned int tx_size_with_change = nBytes + coin_selection_params.change_output_size + 2; // Add 2 as a buffer in case increasing # of outputs changes compact size
3165  CAmount fee_needed_with_change = GetMinimumFee(*this, tx_size_with_change, coin_control, nullptr);
3166  CAmount minimum_value_for_change = GetDustThreshold(change_prototype_txout, discard_rate);
3167  if (nFeeRet >= fee_needed_with_change + minimum_value_for_change) {
3168  pick_new_inputs = false;
3169  nFeeRet = fee_needed_with_change;
3170  continue;
3171  }
3172  }
3173 
3174  // If we have change output already, just increase it
3175  if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
3176  CAmount extraFeePaid = nFeeRet - nFeeNeeded;
3177  std::vector<CTxOut>::iterator change_position = txNew.vout.begin()+nChangePosInOut;
3178  change_position->nValue += extraFeePaid;
3179  nFeeRet -= extraFeePaid;
3180  }
3181  break; // Done, enough fee included.
3182  }
3183  else if (!pick_new_inputs) {
3184  // This shouldn't happen, we should have had enough excess
3185  // fee to pay for the new output and still meet nFeeNeeded
3186  // Or we should have just subtracted fee from recipients and
3187  // nFeeNeeded should not have changed
3188  strFailReason = _("Transaction fee and change calculation failed").translated;
3189  return false;
3190  }
3191 
3192  // Try to reduce change to include necessary fee
3193  if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
3194  CAmount additionalFeeNeeded = nFeeNeeded - nFeeRet;
3195  std::vector<CTxOut>::iterator change_position = txNew.vout.begin()+nChangePosInOut;
3196  // Only reduce change if remaining amount is still a large enough output.
3197  if (change_position->nValue >= MIN_FINAL_CHANGE + additionalFeeNeeded) {
3198  change_position->nValue -= additionalFeeNeeded;
3199  nFeeRet += additionalFeeNeeded;
3200  break; // Done, able to increase fee from change
3201  }
3202  }
3203 
3204  // If subtracting fee from recipients, we now know what fee we
3205  // need to subtract, we have no reason to reselect inputs
3206  if (nSubtractFeeFromAmount > 0) {
3207  pick_new_inputs = false;
3208  }
3209 
3210  // Include more fee and try again.
3211  nFeeRet = nFeeNeeded;
3212  coin_selection_params.use_bnb = false;
3213  continue;
3214  }
3215  }
3216 
3217  // Shuffle selected coins and fill in final vin
3218  txNew.vin.clear();
3219  std::vector<CInputCoin> selected_coins(setCoins.begin(), setCoins.end());
3220  Shuffle(selected_coins.begin(), selected_coins.end(), FastRandomContext());
3221 
3222  // Note how the sequence number is set to non-maxint so that
3223  // the nLockTime set above actually works.
3224  //
3225  // BIP125 defines opt-in RBF as any nSequence < maxint-1, so
3226  // we use the highest possible value in that range (maxint-2)
3227  // to avoid conflicting with other possible uses of nSequence,
3228  // and in the spirit of "smallest possible change from prior
3229  // behavior."
3230  const uint32_t nSequence = coin_control.m_signal_bip125_rbf.get_value_or(m_signal_rbf) ? MAX_BIP125_RBF_SEQUENCE : (CTxIn::SEQUENCE_FINAL - 1);
3231  for (const auto& coin : selected_coins) {
3232  txNew.vin.push_back(CTxIn(coin.outpoint, CScript(), nSequence));
3233  }
3234 
3235  if (sign)
3236  {
3237  int nIn = 0;
3238  for (const auto& coin : selected_coins)
3239  {
3240  const CScript& scriptPubKey = coin.txout.scriptPubKey;
3241  SignatureData sigdata;
3242 
3243  if (!ProduceSignature(*this, MutableTransactionSignatureCreator(&txNew, nIn, coin.txout.nValue, SIGHASH_ALL), scriptPubKey, sigdata))
3244  {
3245  strFailReason = _("Signing transaction failed").translated;
3246  return false;
3247  } else {
3248  UpdateInput(txNew.vin.at(nIn), sigdata);
3249  }
3250 
3251  nIn++;
3252  }
3253  }
3254 
3255  // Return the constructed transaction data.
3256  tx = MakeTransactionRef(std::move(txNew));
3257 
3258  // Limit size
3260  {
3261  strFailReason = _("Transaction too large").translated;
3262  return false;
3263  }
3264  }
3265 
3266  if (nFeeRet > m_default_max_tx_fee) {
3268  return false;
3269  }
3270 
3271  if (gArgs.GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS)) {
3272  // Lastly, ensure this tx will pass the mempool's chain limits
3273  if (!chain().checkChainLimits(tx)) {
3274  strFailReason = _("Transaction has too long of a mempool chain").translated;
3275  return false;
3276  }
3277  }
3278 
3279  // Before we return success, we assume any change key will be used to prevent
3280  // accidental re-use.
3281  reservedest.KeepDestination();
3282 
3283  WalletLogPrintf("Fee Calculation: Fee:%d Bytes:%u Needed:%d Tgt:%d (requested %d) Reason:\"%s\" Decay %.5f: Estimation: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n",
3284  nFeeRet, nBytes, nFeeNeeded, feeCalc.returnedTarget, feeCalc.desiredTarget, StringForFeeReason(feeCalc.reason), feeCalc.est.decay,
3285  feeCalc.est.pass.start, feeCalc.est.pass.end,
3286  100 * feeCalc.est.pass.withinTarget / (feeCalc.est.pass.totalConfirmed + feeCalc.est.pass.inMempool + feeCalc.est.pass.leftMempool),
3287  feeCalc.est.pass.withinTarget, feeCalc.est.pass.totalConfirmed, feeCalc.est.pass.inMempool, feeCalc.est.pass.leftMempool,
3288  feeCalc.est.fail.start, feeCalc.est.fail.end,
3289  100 * feeCalc.est.fail.withinTarget / (feeCalc.est.fail.totalConfirmed + feeCalc.est.fail.inMempool + feeCalc.est.fail.leftMempool),
3290  feeCalc.est.fail.withinTarget, feeCalc.est.fail.totalConfirmed, feeCalc.est.fail.inMempool, feeCalc.est.fail.leftMempool);
3291  return true;
3292 }
3293 
3297 bool CWallet::CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm, CValidationState& state)
3298 {
3299  {
3300  auto locked_chain = chain().lock();
3301  LOCK(cs_wallet);
3302 
3303  CWalletTx wtxNew(this, std::move(tx));
3304  wtxNew.mapValue = std::move(mapValue);
3305  wtxNew.vOrderForm = std::move(orderForm);
3306  wtxNew.fTimeReceivedIsTxTime = true;
3307  wtxNew.fFromMe = true;
3308 
3309  WalletLogPrintf("CommitTransaction:\n%s", wtxNew.tx->ToString()); /* Continued */
3310  {
3311 
3312  // Add tx to wallet, because if it has change it's also ours,
3313  // otherwise just for transaction history.
3314  AddToWallet(wtxNew);
3315 
3316  // Notify that old coins are spent
3317  for (const CTxIn& txin : wtxNew.tx->vin)
3318  {
3319  CWalletTx &coin = mapWallet.at(txin.prevout.hash);
3320  coin.BindWallet(this);
3322  }
3323  }
3324 
3325  // Get the inserted-CWalletTx from mapWallet so that the
3326  // fInMempool flag is cached properly
3327  CWalletTx& wtx = mapWallet.at(wtxNew.GetHash());
3328 
3329  if (fBroadcastTransactions)
3330  {
3331  std::string err_string;
3332  if (!wtx.SubmitMemoryPoolAndRelay(err_string, true, *locked_chain)) {
3333  WalletLogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", err_string);
3334  // TODO: if we expect the failure to be long term or permanent, instead delete wtx from the wallet and return failure.
3335  }
3336  }
3337  }
3338  return true;
3339 }
3340 
3341 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
3342 {
3343  LOCK(cs_wallet);
3344 
3345  fFirstRunRet = false;
3346  DBErrors nLoadWalletRet = WalletBatch(*database,"cr+").LoadWallet(this);
3347  if (nLoadWalletRet == DBErrors::NEED_REWRITE)
3348  {
3349  if (database->Rewrite("\x04pool"))
3350  {
3351  setInternalKeyPool.clear();
3352  setExternalKeyPool.clear();
3353  m_pool_key_to_index.clear();
3354  // Note: can't top-up keypool here, because wallet is locked.
3355  // User will be prompted to unlock wallet the next operation
3356  // that requires a new key.
3357  }
3358  }
3359 
3360  {
3361  LOCK(cs_KeyStore);
3362  // This wallet is in its first run if all of these are empty
3363  fFirstRunRet = mapKeys.empty() && mapCryptedKeys.empty() && mapWatchKeys.empty() && setWatchOnly.empty() && mapScripts.empty()
3364  && !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET);
3365  }
3366 
3367  if (nLoadWalletRet != DBErrors::LOAD_OK)
3368  return nLoadWalletRet;
3369 
3370  return DBErrors::LOAD_OK;
3371 }
3372 
3373 DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
3374 {
3375  AssertLockHeld(cs_wallet);
3376  DBErrors nZapSelectTxRet = WalletBatch(*database, "cr+").ZapSelectTx(vHashIn, vHashOut);
3377  for (uint256 hash : vHashOut) {
3378  const auto& it = mapWallet.find(hash);
3379  wtxOrdered.erase(it->second.m_it_wtxOrdered);
3380  mapWallet.erase(it);
3381  }
3382 
3383  if (nZapSelectTxRet == DBErrors::NEED_REWRITE)
3384  {
3385  if (database->Rewrite("\x04pool"))
3386  {
3387  setInternalKeyPool.clear();
3388  setExternalKeyPool.clear();
3389  m_pool_key_to_index.clear();
3390  // Note: can't top-up keypool here, because wallet is locked.
3391  // User will be prompted to unlock wallet the next operation
3392  // that requires a new key.
3393  }
3394  }
3395 
3396  if (nZapSelectTxRet != DBErrors::LOAD_OK)
3397  return nZapSelectTxRet;
3398 
3399  MarkDirty();
3400 
3401  return DBErrors::LOAD_OK;
3402 }
3403 
3404 DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
3405 {
3406  DBErrors nZapWalletTxRet = WalletBatch(*database,"cr+").ZapWalletTx(vWtx);
3407  if (nZapWalletTxRet == DBErrors::NEED_REWRITE)
3408  {
3409  if (database->Rewrite("\x04pool"))
3410  {
3411  LOCK(cs_wallet);
3412  setInternalKeyPool.clear();
3413  setExternalKeyPool.clear();
3414  m_pool_key_to_index.clear();
3415  // Note: can't top-up keypool here, because wallet is locked.
3416  // User will be prompted to unlock wallet the next operation
3417  // that requires a new key.
3418  }
3419  }
3420 
3421  if (nZapWalletTxRet != DBErrors::LOAD_OK)
3422  return nZapWalletTxRet;
3423 
3424  return DBErrors::LOAD_OK;
3425 }
3426 
3427 bool CWallet::SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& address, const std::string& strName, const std::string& strPurpose)
3428 {
3429  bool fUpdated = false;
3430  {
3431  LOCK(cs_wallet);
3432  std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
3433  fUpdated = mi != mapAddressBook.end();
3434  mapAddressBook[address].name = strName;
3435  if (!strPurpose.empty()) /* update purpose only if requested */
3436  mapAddressBook[address].purpose = strPurpose;
3437  }
3438  NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != ISMINE_NO,
3439  strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) );
3440  if (!strPurpose.empty() && !batch.WritePurpose(EncodeDestination(address), strPurpose))
3441  return false;
3442  return batch.WriteName(EncodeDestination(address), strName);
3443 }
3444 
3445 bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& strPurpose)
3446 {
3447  WalletBatch batch(*database);
3448  return SetAddressBookWithDB(batch, address, strName, strPurpose);
3449 }
3450 
3452 {
3453  {
3454  LOCK(cs_wallet);
3455 
3456  // Delete destdata tuples associated with address
3457  std::string strAddress = EncodeDestination(address);
3458  for (const std::pair<const std::string, std::string> &item : mapAddressBook[address].destdata)
3459  {
3460  WalletBatch(*database).EraseDestData(strAddress, item.first);
3461  }
3462  mapAddressBook.erase(address);
3463  }
3464 
3465  NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address) != ISMINE_NO, "", CT_DELETED);
3466 
3467  WalletBatch(*database).ErasePurpose(EncodeDestination(address));
3468  return WalletBatch(*database).EraseName(EncodeDestination(address));
3469 }
3470 
3471 const std::string& CWallet::GetLabelName(const CScript& scriptPubKey) const
3472 {
3473  CTxDestination address;
3474  if (ExtractDestination(scriptPubKey, address) && !scriptPubKey.IsUnspendable()) {
3475  auto mi = mapAddressBook.find(address);
3476  if (mi != mapAddressBook.end()) {
3477  return mi->second.name;
3478  }
3479  }
3480  // A scriptPubKey that doesn't have an entry in the address book is
3481  // associated with the default label ("").
3482  const static std::string DEFAULT_LABEL_NAME;
3483  return DEFAULT_LABEL_NAME;
3484 }
3485 
3491 {
3492  if (IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
3493  return false;
3494  }
3495  {
3496  LOCK(cs_wallet);
3497  WalletBatch batch(*database);
3498 
3499  for (const int64_t nIndex : setInternalKeyPool) {
3500  batch.ErasePool(nIndex);
3501  }
3502  setInternalKeyPool.clear();
3503 
3504  for (const int64_t nIndex : setExternalKeyPool) {
3505  batch.ErasePool(nIndex);
3506  }
3507  setExternalKeyPool.clear();
3508 
3509  for (const int64_t nIndex : set_pre_split_keypool) {
3510  batch.ErasePool(nIndex);
3511  }
3512  set_pre_split_keypool.clear();
3513 
3514  m_pool_key_to_index.clear();
3515 
3516  if (!TopUpKeyPool()) {
3517  return false;
3518  }
3519  WalletLogPrintf("CWallet::NewKeyPool rewrote keypool\n");
3520  }
3521  return true;
3522 }
3523 
3525 {
3526  AssertLockHeld(cs_wallet);
3527  return setExternalKeyPool.size() + set_pre_split_keypool.size();
3528 }
3529 
3530 void CWallet::LoadKeyPool(int64_t nIndex, const CKeyPool &keypool)
3531 {
3532  AssertLockHeld(cs_wallet);
3533  if (keypool.m_pre_split) {
3534  set_pre_split_keypool.insert(nIndex);
3535  } else if (keypool.fInternal) {
3536  setInternalKeyPool.insert(nIndex);
3537  } else {
3538  setExternalKeyPool.insert(nIndex);
3539  }
3540  m_max_keypool_index = std::max(m_max_keypool_index, nIndex);
3541  m_pool_key_to_index[keypool.vchPubKey.GetID()] = nIndex;
3542 
3543  // If no metadata exists yet, create a default with the pool key's
3544  // creation time. Note that this may be overwritten by actually
3545  // stored metadata for that key later, which is fine.
3546  CKeyID keyid = keypool.vchPubKey.GetID();
3547  if (mapKeyMetadata.count(keyid) == 0)
3548  mapKeyMetadata[keyid] = CKeyMetadata(keypool.nTime);
3549 }
3550 
3551 bool CWallet::TopUpKeyPool(unsigned int kpSize)
3552 {
3553  if (!CanGenerateKeys()) {
3554  return false;
3555  }
3556  {
3557  LOCK(cs_wallet);
3558 
3559  if (IsLocked()) return false;
3560 
3561  // Top up key pool
3562  unsigned int nTargetSize;
3563  if (kpSize > 0)
3564  nTargetSize = kpSize;
3565  else
3566  nTargetSize = std::max(gArgs.GetArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t) 0);
3567 
3568  // count amount of available keys (internal, external)
3569  // make sure the keypool of external and internal keys fits the user selected target (-keypool)
3570  int64_t missingExternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setExternalKeyPool.size(), (int64_t) 0);
3571  int64_t missingInternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setInternalKeyPool.size(), (int64_t) 0);
3572 
3573  if (!IsHDEnabled() || !CanSupportFeature(FEATURE_HD_SPLIT))
3574  {
3575  // don't create extra internal keys
3576  missingInternal = 0;
3577  }
3578  bool internal = false;
3579  WalletBatch batch(*database);
3580  for (int64_t i = missingInternal + missingExternal; i--;)
3581  {
3582  if (i < missingInternal) {
3583  internal = true;
3584  }
3585 
3586  CPubKey pubkey(GenerateNewKey(batch, internal));
3587  AddKeypoolPubkeyWithDB(pubkey, internal, batch);
3588  }
3589  if (missingInternal + missingExternal > 0) {
3590  WalletLogPrintf("keypool added %d keys (%d internal), size=%u (%u internal)\n", missingInternal + missingExternal, missingInternal, setInternalKeyPool.size() + setExternalKeyPool.size() + set_pre_split_keypool.size(), setInternalKeyPool.size());
3591  }
3592  }
3594  return true;
3595 }
3596 
3597 void CWallet::AddKeypoolPubkeyWithDB(const CPubKey& pubkey, const bool internal, WalletBatch& batch)
3598 {
3599  LOCK(cs_wallet);
3600  assert(m_max_keypool_index < std::numeric_limits<int64_t>::max()); // How in the hell did you use so many keys?
3601  int64_t index = ++m_max_keypool_index;
3602  if (!batch.WritePool(index, CKeyPool(pubkey, internal))) {
3603  throw std::runtime_error(std::string(__func__) + ": writing imported pubkey failed");
3604  }
3605  if (internal) {
3606  setInternalKeyPool.insert(index);
3607  } else {
3608  setExternalKeyPool.insert(index);
3609  }
3610  m_pool_key_to_index[pubkey.GetID()] = index;
3611 }
3612 
3613 bool CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal)
3614 {
3615  nIndex = -1;
3616  keypool.vchPubKey = CPubKey();
3617  {
3618  LOCK(cs_wallet);
3619 
3620  TopUpKeyPool();
3621 
3622  bool fReturningInternal = fRequestedInternal;
3623  fReturningInternal &= (IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT)) || IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
3624  bool use_split_keypool = set_pre_split_keypool.empty();
3625  std::set<int64_t>& setKeyPool = use_split_keypool ? (fReturningInternal ? setInternalKeyPool : setExternalKeyPool) : set_pre_split_keypool;
3626 
3627  // Get the oldest key
3628  if (setKeyPool.empty()) {
3629  return false;
3630  }
3631 
3632  WalletBatch batch(*database);
3633 
3634  auto it = setKeyPool.begin();
3635  nIndex = *it;
3636  setKeyPool.erase(it);
3637  if (!batch.ReadPool(nIndex, keypool)) {
3638  throw std::runtime_error(std::string(__func__) + ": read failed");
3639  }
3640  CPubKey pk;
3641  if (!GetPubKey(keypool.vchPubKey.GetID(), pk)) {
3642  throw std::runtime_error(std::string(__func__) + ": unknown key in key pool");
3643  }
3644  // If the key was pre-split keypool, we don't care about what type it is
3645  if (use_split_keypool && keypool.fInternal != fReturningInternal) {
3646  throw std::runtime_error(std::string(__func__) + ": keypool entry misclassified");
3647  }
3648  if (!keypool.vchPubKey.IsValid()) {
3649  throw std::runtime_error(std::string(__func__) + ": keypool entry invalid");
3650  }
3651 
3652  m_pool_key_to_index.erase(keypool.vchPubKey.GetID());
3653  WalletLogPrintf("keypool reserve %d\n", nIndex);
3654  }
3656  return true;
3657 }
3658 
3659 void CWallet::KeepKey(int64_t nIndex)
3660 {
3661  // Remove from key pool
3662  WalletBatch batch(*database);
3663  batch.ErasePool(nIndex);
3664  WalletLogPrintf("keypool keep %d\n", nIndex);
3665 }
3666 
3667 void CWallet::ReturnKey(int64_t nIndex, bool fInternal, const CPubKey& pubkey)
3668 {
3669  // Return to key pool
3670  {
3671  LOCK(cs_wallet);
3672  if (fInternal) {
3673  setInternalKeyPool.insert(nIndex);
3674  } else if (!set_pre_split_keypool.empty()) {
3675  set_pre_split_keypool.insert(nIndex);
3676  } else {
3677  setExternalKeyPool.insert(nIndex);
3678  }
3679  m_pool_key_to_index[pubkey.GetID()] = nIndex;
3681  }
3682  WalletLogPrintf("keypool return %d\n", nIndex);
3683 }
3684 
3685 bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
3686 {
3687  if (!CanGetAddresses(internal)) {
3688  return false;
3689  }
3690 
3691  CKeyPool keypool;
3692  {
3693  LOCK(cs_wallet);
3694  int64_t nIndex;
3695  if (!ReserveKeyFromKeyPool(nIndex, keypool, internal) && !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
3696  if (IsLocked()) return false;
3697  WalletBatch batch(*database);
3698  result = GenerateNewKey(batch, internal);
3699  return true;
3700  }
3701  KeepKey(nIndex);
3702  result = keypool.vchPubKey;
3703  }
3704  return true;
3705 }
3706 
3707 bool CWallet::GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, std::string& error)
3708 {
3709  LOCK(cs_wallet);
3710  error.clear();
3711 
3712  TopUpKeyPool();
3713 
3714  // Generate a new key that is added to wallet
3715  CPubKey new_key;
3716  if (!GetKeyFromPool(new_key)) {
3717  error = "Error: Keypool ran out, please call keypoolrefill first";
3718  return false;
3719  }
3720  LearnRelatedScripts(new_key, type);
3721  dest = GetDestinationForKey(new_key, type);
3722 
3723  SetAddressBook(dest, label, "receive");
3724  return true;
3725 }
3726 
3728 {
3729  error.clear();
3730 
3731  TopUpKeyPool();
3732 
3733  ReserveDestination reservedest(this);
3734  if (!reservedest.GetReservedDestination(type, dest, true)) {
3735  error = "Error: Keypool ran out, please call keypoolrefill first";
3736  return false;
3737  }
3738 
3739  reservedest.KeepDestination();
3740  return true;
3741 }
3742 
3743 static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, WalletBatch& batch) {
3744  if (setKeyPool.empty()) {
3745  return GetTime();
3746  }
3747 
3748  CKeyPool keypool;
3749  int64_t nIndex = *(setKeyPool.begin());
3750  if (!batch.ReadPool(nIndex, keypool)) {
3751  throw std::runtime_error(std::string(__func__) + ": read oldest key in keypool failed");
3752  }
3753  assert(keypool.vchPubKey.IsValid());
3754  return keypool.nTime;
3755 }
3756 
3758 {
3759  LOCK(cs_wallet);
3760 
3761  WalletBatch batch(*database);
3762 
3763  // load oldest key from keypool, get time and return
3764  int64_t oldestKey = GetOldestKeyTimeInPool(setExternalKeyPool, batch);
3765  if (IsHDEnabled() && CanSupportFeature(FEATURE_HD_SPLIT)) {
3766  oldestKey = std::max(GetOldestKeyTimeInPool(setInternalKeyPool, batch), oldestKey);
3767  if (!set_pre_split_keypool.empty()) {
3768  oldestKey = std::max(GetOldestKeyTimeInPool(set_pre_split_keypool, batch), oldestKey);
3769  }
3770  }
3771 
3772  return oldestKey;
3773 }
3774 
3775 std::map<CTxDestination, CAmount> CWallet::GetAddressBalances(interfaces::Chain::Lock& locked_chain)
3776 {
3777  std::map<CTxDestination, CAmount> balances;
3778 
3779  {
3780  LOCK(cs_wallet);
3781  for (const auto& walletEntry : mapWallet)
3782  {
3783  const CWalletTx& wtx = walletEntry.second;
3784 
3785  if (!wtx.IsTrusted(locked_chain))
3786  continue;
3787 
3788  if (wtx.IsImmatureCoinBase(locked_chain))
3789  continue;
3790 
3791  int nDepth = wtx.GetDepthInMainChain(locked_chain);
3792  if (nDepth < (wtx.IsFromMe(ISMINE_ALL) ? 0 : 1))
3793  continue;
3794 
3795  for (unsigned int i = 0; i < wtx.tx->vout.size(); i++)
3796  {
3797  CTxDestination addr;
3798  if (!IsMine(wtx.tx->vout[i]))
3799  continue;
3800  if(!ExtractDestination(wtx.tx->vout[i].scriptPubKey, addr))
3801  continue;
3802 
3803  CAmount n = IsSpent(locked_chain, walletEntry.first, i) ? 0 : wtx.tx->vout[i].nValue;
3804 
3805  if (!balances.count(addr))
3806  balances[addr] = 0;
3807  balances[addr] += n;
3808  }
3809  }
3810  }
3811 
3812  return balances;
3813 }
3814 
3815 std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
3816 {
3817  AssertLockHeld(cs_wallet);
3818  std::set< std::set<CTxDestination> > groupings;
3819  std::set<CTxDestination> grouping;
3820 
3821  for (const auto& walletEntry : mapWallet)
3822  {
3823  const CWalletTx& wtx = walletEntry.second;
3824 
3825  if (wtx.tx->vin.size() > 0)
3826  {
3827  bool any_mine = false;
3828  // group all input addresses with each other
3829  for (const CTxIn& txin : wtx.tx->vin)
3830  {
3831  CTxDestination address;
3832  if(!IsMine(txin)) /* If this input isn't mine, ignore it */
3833  continue;
3834  if(!ExtractDestination(mapWallet.at(txin.prevout.hash).tx->vout[txin.prevout.n].scriptPubKey, address))
3835  continue;
3836  grouping.insert(address);
3837  any_mine = true;
3838  }
3839 
3840  // group change with input addresses
3841  if (any_mine)
3842  {
3843  for (const CTxOut& txout : wtx.tx->vout)
3844  if (IsChange(txout))
3845  {
3846  CTxDestination txoutAddr;
3847  if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
3848  continue;
3849  grouping.insert(txoutAddr);
3850  }
3851  }
3852  if (grouping.size() > 0)
3853  {
3854  groupings.insert(grouping);
3855  grouping.clear();
3856  }
3857  }
3858 
3859  // group lone addrs by themselves
3860  for (const auto& txout : wtx.tx->vout)
3861  if (IsMine(txout))
3862  {
3863  CTxDestination address;
3864  if(!ExtractDestination(txout.scriptPubKey, address))
3865  continue;
3866  grouping.insert(address);
3867  groupings.insert(grouping);
3868  grouping.clear();
3869  }
3870  }
3871 
3872  std::set< std::set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
3873  std::map< CTxDestination, std::set<CTxDestination>* > setmap; // map addresses to the unique group containing it
3874  for (std::set<CTxDestination> _grouping : groupings)
3875  {
3876  // make a set of all the groups hit by this new group
3877  std::set< std::set<CTxDestination>* > hits;
3878  std::map< CTxDestination, std::set<CTxDestination>* >::iterator it;
3879  for (const CTxDestination& address : _grouping)
3880  if ((it = setmap.find(address)) != setmap.end())
3881  hits.insert((*it).second);
3882 
3883  // merge all hit groups into a new single group and delete old groups
3884  std::set<CTxDestination>* merged = new std::set<CTxDestination>(_grouping);
3885  for (std::set<CTxDestination>* hit : hits)
3886  {
3887  merged->insert(hit->begin(), hit->end());
3888  uniqueGroupings.erase(hit);
3889  delete hit;
3890  }
3891  uniqueGroupings.insert(merged);
3892 
3893  // update setmap
3894  for (const CTxDestination& element : *merged)
3895  setmap[element] = merged;
3896  }
3897 
3898  std::set< std::set<CTxDestination> > ret;
3899  for (const std::set<CTxDestination>* uniqueGrouping : uniqueGroupings)
3900  {
3901  ret.insert(*uniqueGrouping);
3902  delete uniqueGrouping;
3903  }
3904 
3905  return ret;
3906 }
3907 
3908 std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) const
3909 {
3910  LOCK(cs_wallet);
3911  std::set<CTxDestination> result;
3912  for (const std::pair<const CTxDestination, CAddressBookData>& item : mapAddressBook)
3913  {
3914  const CTxDestination& address = item.first;
3915  const std::string& strName = item.second.name;
3916  if (strName == label)
3917  result.insert(address);
3918  }
3919  return result;
3920 }
3921 
3923 {
3924  if (!pwallet->CanGetAddresses(internal)) {
3925  return false;
3926  }
3927 
3928  if (nIndex == -1)
3929  {
3930  CKeyPool keypool;
3931  if (!pwallet->ReserveKeyFromKeyPool(nIndex, keypool, internal)) {
3932  return false;
3933  }
3934  vchPubKey = keypool.vchPubKey;
3935  fInternal = keypool.fInternal;
3936  }
3937  assert(vchPubKey.IsValid());
3938  pwallet->LearnRelatedScripts(vchPubKey, type);
3939  address = GetDestinationForKey(vchPubKey, type);
3940  dest = address;
3941  return true;
3942 }
3943 
3945 {
3946  if (nIndex != -1)
3947  pwallet->KeepKey(nIndex);
3948  nIndex = -1;
3949  vchPubKey = CPubKey();
3950  address = CNoDestination();
3951 }
3952 
3954 {
3955  if (nIndex != -1) {
3956  pwallet->ReturnKey(nIndex, fInternal, vchPubKey);
3957  }
3958  nIndex = -1;
3959  vchPubKey = CPubKey();
3960  address = CNoDestination();
3961 }
3962 
3963 void CWallet::MarkReserveKeysAsUsed(int64_t keypool_id)
3964 {
3965  AssertLockHeld(cs_wallet);
3966  bool internal = setInternalKeyPool.count(keypool_id);
3967  if (!internal) assert(setExternalKeyPool.count(keypool_id) || set_pre_split_keypool.count(keypool_id));
3968  std::set<int64_t> *setKeyPool = internal ? &setInternalKeyPool : (set_pre_split_keypool.empty() ? &setExternalKeyPool : &set_pre_split_keypool);
3969  auto it = setKeyPool->begin();
3970 
3971  WalletBatch batch(*database);
3972  while (it != std::end(*setKeyPool)) {
3973  const int64_t& index = *(it);
3974  if (index > keypool_id) break; // set*KeyPool is ordered
3975 
3976  CKeyPool keypool;
3977  if (batch.ReadPool(index, keypool)) { //TODO: This should be unnecessary
3978  m_pool_key_to_index.erase(keypool.vchPubKey.GetID());
3979  }
3980  LearnAllRelatedScripts(keypool.vchPubKey);
3981  batch.ErasePool(index);
3982  WalletLogPrintf("keypool index %d removed\n", index);
3983  it = setKeyPool->erase(it);
3984  }
3985 }
3986 
3987 void CWallet::LockCoin(const COutPoint& output)
3988 {
3989  AssertLockHeld(cs_wallet);
3990  setLockedCoins.insert(output);
3991 }
3992 
3993 void CWallet::UnlockCoin(const COutPoint& output)
3994 {
3995  AssertLockHeld(cs_wallet);
3996  setLockedCoins.erase(output);
3997 }
3998 
4000 {
4001  AssertLockHeld(cs_wallet);
4002  setLockedCoins.clear();
4003 }
4004 
4005 bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const
4006 {
4007  AssertLockHeld(cs_wallet);
4008  COutPoint outpt(hash, n);
4009 
4010  return (setLockedCoins.count(outpt) > 0);
4011 }
4012 
4013 void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) const
4014 {
4015  AssertLockHeld(cs_wallet);
4016  for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
4017  it != setLockedCoins.end(); it++) {
4018  COutPoint outpt = (*it);
4019  vOutpts.push_back(outpt);
4020  }
4021 }
4022  // end of Actions
4024 
4025 void CWallet::GetKeyBirthTimes(interfaces::Chain::Lock& locked_chain, std::map<CKeyID, int64_t>& mapKeyBirth) const {
4026  AssertLockHeld(cs_wallet);
4027  mapKeyBirth.clear();
4028 
4029  // get birth times for keys with metadata
4030  for (const auto& entry : mapKeyMetadata) {
4031  if (entry.second.nCreateTime) {
4032  mapKeyBirth[entry.first] = entry.second.nCreateTime;
4033  }
4034  }
4035 
4036  // map in which we'll infer heights of other keys
4037  const Optional<int> tip_height = locked_chain.getHeight();
4038  const int max_height = tip_height && *tip_height > 144 ? *tip_height - 144 : 0; // the tip can be reorganized; use a 144-block safety margin
4039  std::map<CKeyID, int> mapKeyFirstBlock;
4040  for (const CKeyID &keyid : GetKeys()) {
4041  if (mapKeyBirth.count(keyid) == 0)
4042  mapKeyFirstBlock[keyid] = max_height;
4043  }
4044 
4045  // if there are no such keys, we're done
4046  if (mapKeyFirstBlock.empty())
4047  return;
4048 
4049  // find first block that affects those keys, if there are any left
4050  for (const auto& entry : mapWallet) {
4051  // iterate over all wallet transactions...
4052  const CWalletTx &wtx = entry.second;
4053  if (Optional<int> height = locked_chain.getBlockHeight(wtx.hashBlock)) {
4054  // ... which are already in a block
4055  for (const CTxOut &txout : wtx.tx->vout) {
4056  // iterate over all their outputs
4057  for (const auto &keyid : GetAffectedKeys(txout.scriptPubKey, *this)) {
4058  // ... and all their affected keys
4059  std::map<CKeyID, int>::iterator rit = mapKeyFirstBlock.find(keyid);
4060  if (rit != mapKeyFirstBlock.end() && *height < rit->second)
4061  rit->second = *height;
4062  }
4063  }
4064  }
4065  }
4066 
4067  // Extract block timestamps for those keys
4068  for (const auto& entry : mapKeyFirstBlock)
4069  mapKeyBirth[entry.first] = locked_chain.getBlockTime(entry.second) - TIMESTAMP_WINDOW; // block times can be 2h off
4070 }
4071 
4093 unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
4094 {
4095  unsigned int nTimeSmart = wtx.nTimeReceived;
4096  if (!wtx.hashUnset()) {
4097  int64_t blocktime;
4098  if (chain().findBlock(wtx.hashBlock, nullptr /* block */, &blocktime)) {
4099  int64_t latestNow = wtx.nTimeReceived;
4100  int64_t latestEntry = 0;
4101 
4102  // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
4103  int64_t latestTolerated = latestNow + 300;
4104  const TxItems& txOrdered = wtxOrdered;
4105  for (auto it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
4106  CWalletTx* const pwtx = it->second;
4107  if (pwtx == &wtx) {
4108  continue;
4109  }
4110  int64_t nSmartTime;
4111  nSmartTime = pwtx->nTimeSmart;
4112  if (!nSmartTime) {
4113  nSmartTime = pwtx->nTimeReceived;
4114  }
4115  if (nSmartTime <= latestTolerated) {
4116  latestEntry = nSmartTime;
4117  if (nSmartTime > latestNow) {
4118  latestNow = nSmartTime;
4119  }
4120  break;
4121  }
4122  }
4123 
4124  nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
4125  } else {
4126  WalletLogPrintf("%s: found %s in block %s not in index\n", __func__, wtx.GetHash().ToString(), wtx.hashBlock.ToString());
4127  }
4128  }
4129  return nTimeSmart;
4130 }
4131 
4132 bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
4133 {
4134  if (boost::get<CNoDestination>(&dest))
4135  return false;
4136 
4137  mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
4138  return WalletBatch(*database).WriteDestData(EncodeDestination(dest), key, value);
4139 }
4140 
4141 bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
4142 {
4143  if (!mapAddressBook[dest].destdata.erase(key))
4144  return false;
4145  return WalletBatch(*database).EraseDestData(EncodeDestination(dest), key);
4146 }
4147 
4148 void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
4149 {
4150  mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
4151 }
4152 
4153 bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const
4154 {
4155  std::map<CTxDestination, CAddressBookData>::const_iterator i = mapAddressBook.find(dest);
4156  if(i != mapAddressBook.end())
4157  {
4158  CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
4159  if(j != i->second.destdata.end())
4160  {
4161  if(value)
4162  *value = j->second;
4163  return true;
4164  }
4165  }
4166  return false;
4167 }
4168 
4169 std::vector<std::string> CWallet::GetDestValues(const std::string& prefix) const
4170 {
4171  std::vector<std::string> values;
4172  for (const auto& address : mapAddressBook) {
4173  for (const auto& data : address.second.destdata) {
4174  if (!data.first.compare(0, prefix.size(), prefix)) {
4175  values.emplace_back(data.second);
4176  }
4177  }
4178  }
4179  return values;
4180 }
4181 
4183 {
4184  WalletBatch batch(*database);
4185  for (auto it = setExternalKeyPool.begin(); it != setExternalKeyPool.end();) {
4186  int64_t index = *it;
4187  CKeyPool keypool;
4188  if (!batch.ReadPool(index, keypool)) {
4189  throw std::runtime_error(std::string(__func__) + ": read keypool entry failed");
4190  }
4191  keypool.m_pre_split = true;
4192  if (!batch.WritePool(index, keypool)) {
4193  throw std::runtime_error(std::string(__func__) + ": writing modified keypool entry failed");
4194  }
4195  set_pre_split_keypool.insert(index);
4196  it = setExternalKeyPool.erase(it);
4197  }
4198 }
4199 
4200 bool CWallet::Verify(interfaces::Chain& chain, const WalletLocation& location, bool salvage_wallet, std::string& error_string, std::string& warning_string)
4201 {
4202  // Do some checking on wallet path. It should be either a:
4203  //
4204  // 1. Path where a directory can be created.
4205  // 2. Path to an existing directory.
4206  // 3. Path to a symlink to a directory.
4207  // 4. For backwards compatibility, the name of a data file in -walletdir.
4208  LOCK(cs_wallets);
4209  const fs::path& wallet_path = location.GetPath();
4210  fs::file_type path_type = fs::symlink_status(wallet_path).type();
4211  if (!(path_type == fs::file_not_found || path_type == fs::directory_file ||
4212  (path_type == fs::symlink_file && fs::is_directory(wallet_path)) ||
4213  (path_type == fs::regular_file && fs::path(location.GetName()).filename() == location.GetName()))) {
4214  error_string = strprintf(
4215  "Invalid -wallet path '%s'. -wallet path should point to a directory where wallet.dat and "
4216  "database/log.?????????? files can be stored, a location where such a directory could be created, "
4217  "or (for backwards compatibility) the name of an existing data file in -walletdir (%s)",
4218  location.GetName(), GetWalletDir());
4219  return false;
4220  }
4221 
4222  // Make sure that the wallet path doesn't clash with an existing wallet path
4223  if (IsWalletLoaded(wallet_path)) {
4224  error_string = strprintf("Error loading wallet %s. Duplicate -wallet filename specified.", location.GetName());
4225  return false;
4226  }
4227 
4228  // Keep same database environment instance across Verify/Recover calls below.
4229  std::unique_ptr<WalletDatabase> database = WalletDatabase::Create(wallet_path);
4230 
4231  try {
4232  if (!WalletBatch::VerifyEnvironment(wallet_path, error_string)) {
4233  return false;
4234  }
4235  } catch (const fs::filesystem_error& e) {
4236  error_string = strprintf("Error loading wallet %s. %s", location.GetName(), fsbridge::get_filesystem_error_message(e));
4237  return false;
4238  }
4239 
4240  if (salvage_wallet) {
4241  // Recover readable keypairs:
4242  CWallet dummyWallet(&chain, WalletLocation(), WalletDatabase::CreateDummy());
4243  std::string backup_filename;
4244  if (!WalletBatch::Recover(wallet_path, (void *)&dummyWallet, WalletBatch::RecoverKeysOnlyFilter, backup_filename)) {
4245  return false;
4246  }
4247  }
4248 
4249  return WalletBatch::VerifyDatabaseFile(wallet_path, warning_string, error_string);
4250 }
4251 
4252 std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain, const WalletLocation& location, uint64_t wallet_creation_flags)
4253 {
4254  const std::string& walletFile = WalletDataFilePath(location.GetPath()).string();
4255 
4256  // needed to restore wallet transaction meta data after -zapwallettxes
4257  std::vector<CWalletTx> vWtx;
4258 
4259  if (gArgs.GetBoolArg("-zapwallettxes", false)) {
4260  chain.initMessage(_("Zapping all transactions from wallet...").translated);
4261 
4262  std::unique_ptr<CWallet> tempWallet = MakeUnique<CWallet>(&chain, location, WalletDatabase::Create(location.GetPath()));
4263  DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
4264  if (nZapWalletRet != DBErrors::LOAD_OK) {
4265  chain.initError(strprintf(_("Error loading %s: Wallet corrupted").translated, walletFile));
4266  return nullptr;
4267  }
4268  }
4269 
4270  chain.initMessage(_("Loading wallet...").translated);
4271 
4272  int64_t nStart = GetTimeMillis();
4273  bool fFirstRun = true;
4274  // TODO: Can't use std::make_shared because we need a custom deleter but
4275  // should be possible to use std::allocate_shared.
4276  std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, location, WalletDatabase::Create(location.GetPath())), ReleaseWallet);
4277  DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
4278  if (nLoadWalletRet != DBErrors::LOAD_OK)
4279  {
4280  if (nLoadWalletRet == DBErrors::CORRUPT) {
4281  chain.initError(strprintf(_("Error loading %s: Wallet corrupted").translated, walletFile));
4282  return nullptr;
4283  }
4284  else if (nLoadWalletRet == DBErrors::NONCRITICAL_ERROR)
4285  {
4286  chain.initWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
4287  " or address book entries might be missing or incorrect.").translated,
4288  walletFile));
4289  }
4290  else if (nLoadWalletRet == DBErrors::TOO_NEW) {
4291  chain.initError(strprintf(_("Error loading %s: Wallet requires newer version of %s").translated, walletFile, PACKAGE_NAME));
4292  return nullptr;
4293  }
4294  else if (nLoadWalletRet == DBErrors::NEED_REWRITE)
4295  {
4296  chain.initError(strprintf(_("Wallet needed to be rewritten: restart %s to complete").translated, PACKAGE_NAME));
4297  return nullptr;
4298  }
4299  else {
4300  chain.initError(strprintf(_("Error loading %s").translated, walletFile));
4301  return nullptr;
4302  }
4303  }
4304 
4305  int prev_version = walletInstance->GetVersion();
4306  if (gArgs.GetBoolArg("-upgradewallet", fFirstRun))
4307  {
4308  int nMaxVersion = gArgs.GetArg("-upgradewallet", 0);
4309  if (nMaxVersion == 0) // the -upgradewallet without argument case
4310  {
4311  walletInstance->WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
4312  nMaxVersion = FEATURE_LATEST;
4313  walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
4314  }
4315  else
4316  walletInstance->WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
4317  if (nMaxVersion < walletInstance->GetVersion())
4318  {
4319  chain.initError(_("Cannot downgrade wallet").translated);
4320  return nullptr;
4321  }
4322  walletInstance->SetMaxVersion(nMaxVersion);
4323  }
4324 
4325  // Upgrade to HD if explicit upgrade
4326  if (gArgs.GetBoolArg("-upgradewallet", false)) {
4327  LOCK(walletInstance->cs_wallet);
4328 
4329  // Do not upgrade versions to any version between HD_SPLIT and FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
4330  int max_version = walletInstance->GetVersion();
4331  if (!walletInstance->CanSupportFeature(FEATURE_HD_SPLIT) && max_version >= FEATURE_HD_SPLIT && max_version < FEATURE_PRE_SPLIT_KEYPOOL) {
4332  chain.initError(_("Cannot upgrade a non HD split wallet without upgrading to support pre split keypool. Please use -upgradewallet=169900 or -upgradewallet with no version specified.").translated);
4333  return nullptr;
4334  }
4335 
4336  bool hd_upgrade = false;
4337  bool split_upgrade = false;
4338  if (walletInstance->CanSupportFeature(FEATURE_HD) && !walletInstance->IsHDEnabled()) {
4339  walletInstance->WalletLogPrintf("Upgrading wallet to HD\n");
4340  walletInstance->SetMinVersion(FEATURE_HD);
4341 
4342  // generate a new master key
4343  CPubKey masterPubKey = walletInstance->GenerateNewSeed();
4344  walletInstance->SetHDSeed(masterPubKey);
4345  hd_upgrade = true;
4346  }
4347  // Upgrade to HD chain split if necessary
4348  if (walletInstance->CanSupportFeature(FEATURE_HD_SPLIT)) {
4349  walletInstance->WalletLogPrintf("Upgrading wallet to use HD chain split\n");
4350  walletInstance->SetMinVersion(FEATURE_PRE_SPLIT_KEYPOOL);
4351  split_upgrade = FEATURE_HD_SPLIT > prev_version;
4352  }
4353  // Mark all keys currently in the keypool as pre-split
4354  if (split_upgrade) {
4355  walletInstance->MarkPreSplitKeys();
4356  }
4357  // Regenerate the keypool if upgraded to HD
4358  if (hd_upgrade) {
4359  if (!walletInstance->TopUpKeyPool()) {
4360  chain.initError(_("Unable to generate keys").translated);
4361  return nullptr;
4362  }
4363  }
4364  }
4365 
4366  if (fFirstRun)
4367  {
4368  // ensure this wallet.dat can only be opened by clients supporting HD with chain split and expects no default key
4369  walletInstance->SetMinVersion(FEATURE_LATEST);
4370 
4371  walletInstance->SetWalletFlags(wallet_creation_flags, false);
4372  if (!(wallet_creation_flags & (WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET))) {
4373  // generate a new seed
4374  CPubKey seed = walletInstance->GenerateNewSeed();
4375  walletInstance->SetHDSeed(seed);
4376  }
4377 
4378  // Top up the keypool
4379  if (walletInstance->CanGenerateKeys() && !walletInstance->TopUpKeyPool()) {
4380  chain.initError(_("Unable to generate initial keys").translated);
4381  return nullptr;
4382  }
4383 
4384  auto locked_chain = chain.lock();
4385  walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
4386  } else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
4387  // Make it impossible to disable private keys after creation
4388  chain.initError(strprintf(_("Error loading %s: Private keys can only be disabled during creation").translated, walletFile));
4389  return NULL;
4390  } else if (walletInstance->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
4391  LOCK(walletInstance->cs_KeyStore);
4392  if (!walletInstance->mapKeys.empty() || !walletInstance->mapCryptedKeys.empty()) {
4393  chain.initWarning(strprintf(_("Warning: Private keys detected in wallet {%s} with disabled private keys").translated, walletFile));
4394  }
4395  }
4396 
4397  if (!gArgs.GetArg("-addresstype", "").empty() && !ParseOutputType(gArgs.GetArg("-addresstype", ""), walletInstance->m_default_address_type)) {
4398  chain.initError(strprintf("Unknown address type '%s'", gArgs.GetArg("-addresstype", "")));
4399  return nullptr;
4400  }
4401 
4402  if (!gArgs.GetArg("-changetype", "").empty() && !ParseOutputType(gArgs.GetArg("-changetype", ""), walletInstance->m_default_change_type)) {
4403  chain.initError(strprintf("Unknown change type '%s'", gArgs.GetArg("-changetype", "")));
4404  return nullptr;
4405  }
4406 
4407  if (gArgs.IsArgSet("-mintxfee")) {
4408  CAmount n = 0;
4409  if (!ParseMoney(gArgs.GetArg("-mintxfee", ""), n) || 0 == n) {
4410  chain.initError(AmountErrMsg("mintxfee", gArgs.GetArg("-mintxfee", "")));
4411  return nullptr;
4412  }
4413  if (n > HIGH_TX_FEE_PER_KB) {
4414  chain.initWarning(AmountHighWarn("-mintxfee") + " " +
4415  _("This is the minimum transaction fee you pay on every transaction.").translated);
4416  }
4417  walletInstance->m_min_fee = CFeeRate(n);
4418  }
4419 
4420  walletInstance->m_allow_fallback_fee = Params().IsTestChain();
4421  if (gArgs.IsArgSet("-fallbackfee")) {
4422  CAmount nFeePerK = 0;
4423  if (!ParseMoney(gArgs.GetArg("-fallbackfee", ""), nFeePerK)) {
4424  chain.initError(strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'").translated, gArgs.GetArg("-fallbackfee", "")));
4425  return nullptr;
4426  }
4427  if (nFeePerK > HIGH_TX_FEE_PER_KB) {
4428  chain.initWarning(AmountHighWarn("-fallbackfee") + " " +
4429  _("This is the transaction fee you may pay when fee estimates are not available.").translated);
4430  }
4431  walletInstance->m_fallback_fee = CFeeRate(nFeePerK);
4432  walletInstance->m_allow_fallback_fee = nFeePerK != 0; //disable fallback fee in case value was set to 0, enable if non-null value
4433  }
4434  if (gArgs.IsArgSet("-discardfee")) {
4435  CAmount nFeePerK = 0;
4436  if (!ParseMoney(gArgs.GetArg("-discardfee", ""), nFeePerK)) {
4437  chain.initError(strprintf(_("Invalid amount for -discardfee=<amount>: '%s'").translated, gArgs.GetArg("-discardfee", "")));
4438  return nullptr;
4439  }
4440  if (nFeePerK > HIGH_TX_FEE_PER_KB) {
4441  chain.initWarning(AmountHighWarn("-discardfee") + " " +
4442  _("This is the transaction fee you may discard if change is smaller than dust at this level").translated);
4443  }
4444  walletInstance->m_discard_rate = CFeeRate(nFeePerK);
4445  }
4446  if (gArgs.IsArgSet("-paytxfee")) {
4447  CAmount nFeePerK = 0;
4448  if (!ParseMoney(gArgs.GetArg("-paytxfee", ""), nFeePerK)) {
4449  chain.initError(AmountErrMsg("paytxfee", gArgs.GetArg("-paytxfee", "")));
4450  return nullptr;
4451  }
4452  if (nFeePerK > HIGH_TX_FEE_PER_KB) {
4453  chain.initWarning(AmountHighWarn("-paytxfee") + " " +
4454  _("This is the transaction fee you will pay if you send a transaction.").translated);
4455  }
4456  walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000);
4457  if (walletInstance->m_pay_tx_fee < chain.relayMinFee()) {
4458  chain.initError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)").translated,
4459  gArgs.GetArg("-paytxfee", ""), chain.relayMinFee().ToString()));
4460  return nullptr;
4461  }
4462  }
4463 
4464  if (gArgs.IsArgSet("-maxtxfee"))
4465  {
4466  CAmount nMaxFee = 0;
4467  if (!ParseMoney(gArgs.GetArg("-maxtxfee", ""), nMaxFee)) {
4468  chain.initError(AmountErrMsg("maxtxfee", gArgs.GetArg("-maxtxfee", "")));
4469  return nullptr;
4470  }
4471  if (nMaxFee > HIGH_MAX_TX_FEE) {
4472  chain.initWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction.").translated);
4473  }
4474  if (CFeeRate(nMaxFee, 1000) < chain.relayMinFee()) {
4475  chain.initError(strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)").translated,
4476  gArgs.GetArg("-maxtxfee", ""), chain.relayMinFee().ToString()));
4477  return nullptr;
4478  }
4479  walletInstance->m_default_max_tx_fee = nMaxFee;
4480  }
4481 
4482  if (chain.relayMinFee().GetFeePerK() > HIGH_TX_FEE_PER_KB)
4483  chain.initWarning(AmountHighWarn("-minrelaytxfee") + " " +
4484  _("The wallet will avoid paying less than the minimum relay fee.").translated);
4485 
4486  walletInstance->m_confirm_target = gArgs.GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
4487  walletInstance->m_spend_zero_conf_change = gArgs.GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
4488  walletInstance->m_signal_rbf = gArgs.GetBoolArg("-walletrbf", DEFAULT_WALLET_RBF);
4489 
4490  walletInstance->WalletLogPrintf("Wallet completed loading in %15dms\n", GetTimeMillis() - nStart);
4491 
4492  // Try to top up keypool. No-op if the wallet is locked.
4493  walletInstance->TopUpKeyPool();
4494 
4495  auto locked_chain = chain.lock();
4496  LOCK(walletInstance->cs_wallet);
4497 
4498  int rescan_height = 0;
4499  if (!gArgs.GetBoolArg("-rescan", false))
4500  {
4501  WalletBatch batch(*walletInstance->database);
4502  CBlockLocator locator;
4503  if (batch.ReadBestBlock(locator)) {
4504  if (const Optional<int> fork_height = locked_chain->findLocatorFork(locator)) {
4505  rescan_height = *fork_height;
4506  }
4507  }
4508  }
4509 
4510  const Optional<int> tip_height = locked_chain->getHeight();
4511  if (tip_height) {
4512  walletInstance->m_last_block_processed = locked_chain->getBlockHash(*tip_height);
4513  } else {
4514  walletInstance->m_last_block_processed.SetNull();
4515  }
4516 
4517  if (tip_height && *tip_height != rescan_height)
4518  {
4519  // We can't rescan beyond non-pruned blocks, stop and throw an error.
4520  // This might happen if a user uses an old wallet within a pruned node
4521  // or if they ran -disablewallet for a longer time, then decided to re-enable
4522  if (chain.havePruned()) {
4523  // Exit early and print an error.
4524  // If a block is pruned after this check, we will load the wallet,
4525  // but fail the rescan with a generic error.
4526  int block_height = *tip_height;
4527  while (block_height > 0 && locked_chain->haveBlockOnDisk(block_height - 1) && rescan_height != block_height) {
4528  --block_height;
4529  }
4530 
4531  if (rescan_height != block_height) {
4532  chain.initError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)").translated);
4533  return nullptr;
4534  }
4535  }
4536 
4537  chain.initMessage(_("Rescanning...").translated);
4538  walletInstance->WalletLogPrintf("Rescanning last %i blocks (from block %i)...\n", *tip_height - rescan_height, rescan_height);
4539 
4540  // No need to read and scan block if block was created before
4541  // our wallet birthday (as adjusted for block time variability)
4542  if (walletInstance->nTimeFirstKey) {
4543  if (Optional<int> first_block = locked_chain->findFirstBlockWithTimeAndHeight(walletInstance->nTimeFirstKey - TIMESTAMP_WINDOW, rescan_height, nullptr)) {
4544  rescan_height = *first_block;
4545  }
4546  }
4547 
4548  {
4549  WalletRescanReserver reserver(walletInstance.get());
4550  if (!reserver.reserve() || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions(locked_chain->getBlockHash(rescan_height), {} /* stop block */, reserver, true /* update */).status)) {
4551  chain.initError(_("Failed to rescan the wallet during initialization").translated);
4552  return nullptr;
4553  }
4554  }
4555  walletInstance->ChainStateFlushed(locked_chain->getTipLocator());
4556  walletInstance->database->IncrementUpdateCounter();
4557 
4558  // Restore wallet transaction metadata after -zapwallettxes=1
4559  if (gArgs.GetBoolArg("-zapwallettxes", false) && gArgs.GetArg("-zapwallettxes", "1") != "2")
4560  {
4561  WalletBatch batch(*walletInstance->database);
4562 
4563  for (const CWalletTx& wtxOld : vWtx)
4564  {
4565  uint256 hash = wtxOld.GetHash();
4566  std::map<uint256, CWalletTx>::iterator mi = walletInstance->mapWallet.find(hash);
4567  if (mi != walletInstance->mapWallet.end())
4568  {
4569  const CWalletTx* copyFrom = &wtxOld;
4570  CWalletTx* copyTo = &mi->second;
4571  copyTo->mapValue = copyFrom->mapValue;
4572  copyTo->vOrderForm = copyFrom->vOrderForm;
4573  copyTo->nTimeReceived = copyFrom->nTimeReceived;
4574  copyTo->nTimeSmart = copyFrom->nTimeSmart;
4575  copyTo->fFromMe = copyFrom->fFromMe;
4576  copyTo->nOrderPos = copyFrom->nOrderPos;
4577  batch.WriteTx(*copyTo);
4578  }
4579  }
4580  }
4581  }
4582 
4583  chain.loadWallet(interfaces::MakeWallet(walletInstance));
4584 
4585  // Register with the validation interface. It's ok to do this after rescan since we're still holding locked_chain.
4586  walletInstance->handleNotifications();
4587 
4588  walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
4589 
4590  {
4591  walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize());
4592  walletInstance->WalletLogPrintf("mapWallet.size() = %u\n", walletInstance->mapWallet.size());
4593  walletInstance->WalletLogPrintf("mapAddressBook.size() = %u\n", walletInstance->mapAddressBook.size());
4594  }
4595 
4596  return walletInstance;
4597 }
4598 
4600 {
4601  m_chain_notifications_handler = m_chain->handleNotifications(*this);
4602 }
4603 
4605 {
4606  auto locked_chain = chain().lock();
4607  LOCK(cs_wallet);
4608 
4609  // Add wallet transactions that aren't already in a block to mempool
4610  // Do this here as mempool requires genesis block to be loaded
4611  ReacceptWalletTransactions(*locked_chain);
4612 
4613  // Update wallet transactions with current mempool transactions.
4614  chain().requestMempoolTransactions(*this);
4615 }
4616 
4617 bool CWallet::BackupWallet(const std::string& strDest)
4618 {
4619  return database->Backup(strDest);
4620 }
4621 
4623 {
4624  nTime = GetTime();
4625  fInternal = false;
4626  m_pre_split = false;
4627 }
4628 
4629 CKeyPool::CKeyPool(const CPubKey& vchPubKeyIn, bool internalIn)
4630 {
4631  nTime = GetTime();
4632  vchPubKey = vchPubKeyIn;
4633  fInternal = internalIn;
4634  m_pre_split = false;
4635 }
4636 
4637 void CWalletTx::SetMerkleBranch(const uint256& block_hash, int posInBlock)
4638 {
4639  // Update the tx's hashBlock
4640  hashBlock = block_hash;
4641 
4642  // set the position of the transaction in the block
4643  nIndex = posInBlock;
4644 }
4645 
4647 {
4648  if (hashUnset())
4649  return 0;
4650 
4651  return locked_chain.getBlockDepth(hashBlock) * (nIndex == -1 ? -1 : 1);
4652 }
4653 
4655 {
4656  if (!IsCoinBase())
4657  return 0;
4658  int chain_depth = GetDepthInMainChain(locked_chain);
4659  assert(chain_depth >= 0); // coinbase tx should not be conflicted
4660  return std::max(0, (COINBASE_MATURITY+1) - chain_depth);
4661 }
4662 
4664 {
4665  // note GetBlocksToMaturity is 0 for non-coinbase tx
4666  return GetBlocksToMaturity(locked_chain) > 0;
4667 }
4668 
4670 {
4671  if (key.IsCompressed() && (type == OutputType::P2SH_SEGWIT || type == OutputType::BECH32)) {
4672  CTxDestination witdest = WitnessV0KeyHash(key.GetID());
4673  CScript witprog = GetScriptForDestination(witdest);
4674  // Make sure the resulting program is solvable.
4675  assert(IsSolvable(*this, witprog));
4676  AddCScript(witprog);
4677  }
4678 }
4679 
4681 {
4682  // OutputType::P2SH_SEGWIT always adds all necessary scripts for all types.
4683  LearnRelatedScripts(key, OutputType::P2SH_SEGWIT);
4684 }
4685 
4686 std::vector<OutputGroup> CWallet::GroupOutputs(const std::vector<COutput>& outputs, bool single_coin) const {
4687  std::vector<OutputGroup> groups;
4688  std::map<CTxDestination, OutputGroup> gmap;
4689  CTxDestination dst;
4690  for (const auto& output : outputs) {
4691  if (output.fSpendable) {
4692  CInputCoin input_coin = output.GetInputCoin();
4693 
4694  size_t ancestors, descendants;
4695  chain().getTransactionAncestry(output.tx->GetHash(), ancestors, descendants);
4696  if (!single_coin && ExtractDestination(output.tx->tx->vout[output.i].scriptPubKey, dst)) {
4697  // Limit output groups to no more than 10 entries, to protect
4698  // against inadvertently creating a too-large transaction
4699  // when using -avoidpartialspends
4700  if (gmap[dst].m_outputs.size() >= OUTPUT_GROUP_MAX_ENTRIES) {
4701  groups.push_back(gmap[dst]);
4702  gmap.erase(dst);
4703  }
4704  gmap[dst].Insert(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants);
4705  } else {
4706  groups.emplace_back(input_coin, output.nDepth, output.tx->IsFromMe(ISMINE_ALL), ancestors, descendants);
4707  }
4708  }
4709  }
4710  if (!single_coin) {
4711  for (const auto& it : gmap) groups.push_back(it.second);
4712  }
4713  return groups;
4714 }
4715 
4716 bool CWallet::GetKeyOrigin(const CKeyID& keyID, KeyOriginInfo& info) const
4717 {
4718  CKeyMetadata meta;
4719  {
4720  LOCK(cs_wallet);
4721  auto it = mapKeyMetadata.find(keyID);
4722  if (it != mapKeyMetadata.end()) {
4723  meta = it->second;
4724  }
4725  }
4726  if (meta.has_key_origin) {
4727  std::copy(meta.key_origin.fingerprint, meta.key_origin.fingerprint + 4, info.fingerprint);
4728  info.path = meta.key_origin.path;
4729  } else { // Single pubkeys get the master fingerprint of themselves
4730  std::copy(keyID.begin(), keyID.begin() + 4, info.fingerprint);
4731  }
4732  return true;
4733 }
4734 
4735 bool CWallet::AddKeyOriginWithDB(WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info)
4736 {
4737  LOCK(cs_wallet);
4738  std::copy(info.fingerprint, info.fingerprint + 4, mapKeyMetadata[pubkey.GetID()].key_origin.fingerprint);
4739  mapKeyMetadata[pubkey.GetID()].key_origin.path = info.path;
4740  mapKeyMetadata[pubkey.GetID()].has_key_origin = true;
4741  mapKeyMetadata[pubkey.GetID()].hdKeypath = WriteHDKeypath(info.path);
4742  return batch.WriteKeyMetadata(mapKeyMetadata[pubkey.GetID()], pubkey, true);
4743 }
4744 
4746 {
4747  LOCK(cs_KeyStore);
4748  if (fUseCrypto)
4749  return true;
4750  if (!mapKeys.empty())
4751  return false;
4752  fUseCrypto = true;
4753  return true;
4754 }
4755 
4756 bool CWallet::IsLocked() const
4757 {
4758  if (!IsCrypted()) {
4759  return false;
4760  }
4761  LOCK(cs_KeyStore);
4762  return vMasterKey.empty();
4763 }
4764 
4766 {
4767  if (!SetCrypted())
4768  return false;
4769 
4770  {
4771  LOCK(cs_KeyStore);
4772  vMasterKey.clear();
4773  }
4774 
4775  NotifyStatusChanged(this);
4776  return true;
4777 }
4778 
4779 bool CWallet::Unlock(const CKeyingMaterial& vMasterKeyIn, bool accept_no_keys)
4780 {
4781  {
4782  LOCK(cs_KeyStore);
4783  if (!SetCrypted())
4784  return false;
4785 
4786  bool keyPass = mapCryptedKeys.empty(); // Always pass when there are no encrypted keys
4787  bool keyFail = false;
4788  CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
4789  for (; mi != mapCryptedKeys.end(); ++mi)
4790  {
4791  const CPubKey &vchPubKey = (*mi).second.first;
4792  const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
4793  CKey key;
4794  if (!DecryptKey(vMasterKeyIn, vchCryptedSecret, vchPubKey, key))
4795  {
4796  keyFail = true;
4797  break;
4798  }
4799  keyPass = true;
4800  if (fDecryptionThoroughlyChecked)
4801  break;
4802  }
4803  if (keyPass && keyFail)
4804  {
4805  LogPrintf("The wallet is probably corrupted: Some keys decrypt but not all.\n");
4806  throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
4807  }
4808  if (keyFail || (!keyPass && !accept_no_keys))
4809  return false;
4810  vMasterKey = vMasterKeyIn;
4811  fDecryptionThoroughlyChecked = true;
4812  }
4813  NotifyStatusChanged(this);
4814  return true;
4815 }
4816 
4817 bool CWallet::HaveKey(const CKeyID &address) const
4818 {
4819  LOCK(cs_KeyStore);
4820  if (!IsCrypted()) {
4821  return FillableSigningProvider::HaveKey(address);
4822  }
4823  return mapCryptedKeys.count(address) > 0;
4824 }
4825 
4826 bool CWallet::GetKey(const CKeyID &address, CKey& keyOut) const
4827 {
4828  LOCK(cs_KeyStore);
4829  if (!IsCrypted()) {
4830  return FillableSigningProvider::GetKey(address, keyOut);
4831  }
4832 
4833  CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
4834  if (mi != mapCryptedKeys.end())
4835  {
4836  const CPubKey &vchPubKey = (*mi).second.first;
4837  const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
4838  return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
4839  }
4840  return false;
4841 }
4842 
4843 bool CWallet::GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
4844 {
4845  LOCK(cs_KeyStore);
4846  WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
4847  if (it != mapWatchKeys.end()) {
4848  pubkey_out = it->second;
4849  return true;
4850  }
4851  return false;
4852 }
4853 
4854 bool CWallet::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
4855 {
4856  LOCK(cs_KeyStore);
4857  if (!IsCrypted()) {
4858  if (!FillableSigningProvider::GetPubKey(address, vchPubKeyOut)) {
4859  return GetWatchPubKey(address, vchPubKeyOut);
4860  }
4861  return true;
4862  }
4863 
4864  CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
4865  if (mi != mapCryptedKeys.end())
4866  {
4867  vchPubKeyOut = (*mi).second.first;
4868  return true;
4869  }
4870  // Check for watch-only pubkeys
4871  return GetWatchPubKey(address, vchPubKeyOut);
4872 }
4873 
4874 std::set<CKeyID> CWallet::GetKeys() const
4875 {
4876  LOCK(cs_KeyStore);
4877  if (!IsCrypted()) {
4879  }
4880  std::set<CKeyID> set_address;
4881  for (const auto& mi : mapCryptedKeys) {
4882  set_address.insert(mi.first);
4883  }
4884  return set_address;
4885 }
4886 
4888 {
4889  LOCK(cs_KeyStore);
4890  if (!mapCryptedKeys.empty() || IsCrypted())
4891  return false;
4892 
4893  fUseCrypto = true;
4894  for (const KeyMap::value_type& mKey : mapKeys)
4895  {
4896  const CKey &key = mKey.second;
4897  CPubKey vchPubKey = key.GetPubKey();
4898  CKeyingMaterial vchSecret(key.begin(), key.end());
4899  std::vector<unsigned char> vchCryptedSecret;
4900  if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), vchCryptedSecret))
4901  return false;
4902  if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
4903  return false;
4904  }
4905  mapKeys.clear();
4906  return true;
4907 }
4908 
4909 bool CWallet::AddKeyPubKeyInner(const CKey& key, const CPubKey &pubkey)
4910 {
4911  LOCK(cs_KeyStore);
4912  if (!IsCrypted()) {
4913  return FillableSigningProvider::AddKeyPubKey(key, pubkey);
4914  }
4915 
4916  if (IsLocked()) {
4917  return false;
4918  }
4919 
4920  std::vector<unsigned char> vchCryptedSecret;
4921  CKeyingMaterial vchSecret(key.begin(), key.end());
4922  if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret)) {
4923  return false;
4924  }
4925 
4926  if (!AddCryptedKey(pubkey, vchCryptedSecret)) {
4927  return false;
4928  }
4929  return true;
4930 }
4931 
4932 
4933 bool CWallet::AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
4934 {
4935  LOCK(cs_KeyStore);
4936  if (!SetCrypted()) {
4937  return false;
4938  }
4939 
4940  mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
4941  ImplicitlyLearnRelatedKeyScripts(vchPubKey);
4942  return true;
4943 }
bool IsEquivalentTo(const CWalletTx &tx) const
Definition: wallet.cpp:2338
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
AmountType
Definition: wallet.h:452
void MarkReserveKeysAsUsed(int64_t keypool_id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Marks all keys in the keypool up to and including reserve_key as used.
Definition: wallet.cpp:3963
static std::unique_ptr< BerkeleyDatabase > CreateDummy()
Return object for accessing dummy database with no read/write capabilities.
Definition: db.h:145
static int64_t GetTransactionWeight(const CTransaction &tx)
Definition: validation.h:148
CAmount nValue
Definition: transaction.h:136
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:54
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
void AddKeypoolPubkeyWithDB(const CPubKey &pubkey, const bool internal, WalletBatch &batch)
Definition: wallet.cpp:3597
const fs::path & GetPath() const
Get wallet absolute path.
Definition: walletutil.h:32
void SetUsedDestinationState(const uint256 &hash, unsigned int n, bool used)
Definition: wallet.cpp:1058
int nIndex
Definition: wallet.h:489
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:742
EstimatorBucket pass
Definition: fees.h:69
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:42
bool SetCrypted()
Definition: wallet.cpp:4745
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3530
const std::map< uint64_t, std::string > WALLET_FLAG_CAVEATS
Definition: wallet.cpp:40
unsigned char fingerprint[4]
First 32 bits of the Hash160 of the public key at the root of the path.
Definition: keyorigin.h:14
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:472
bool ParseMoney(const std::string &str, CAmount &nRet)
Definition: moneystr.cpp:34
int i
Definition: wallet.h:623
uint64_t GetRand(uint64_t nMax) noexcept
Definition: random.cpp:665
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:171
std::string hdKeypath
Definition: walletdb.h:127
void BindWallet(CWallet *pwalletIn)
Definition: wallet.h:542
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:41
static const unsigned int DEFAULT_DESCENDANT_LIMIT
Default for -limitdescendantcount, max number of in-mempool descendants.
Definition: validation.h:60
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:71
std::set< std::set< CTxDestination > > GetAddressGroupings() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3815
std::unique_ptr< interfaces::Handler > m_chain_notifications_handler
Registered interfaces::Chain::Notifications handler.
Definition: wallet.h:913
bool AddToWalletIfInvolvingMe(const CTransactionRef &tx, const uint256 &block_hash, int posInBlock, bool fUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Add a transaction to the wallet, or update it.
Definition: wallet.cpp:1204
const CWalletTx * GetWalletTx(const uint256 &hash) const
Definition: wallet.cpp:255
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:156
int64_t GetOldestKeyPoolTime()
Definition: wallet.cpp:3757
static const uint32_t MAX_BIP125_RBF_SEQUENCE
Definition: rbf.h:12
void SetMerkleBranch(const uint256 &block_hash, int posInBlock)
Definition: wallet.cpp:4637
bool fAllowWatchOnly
Includes watch only addresses which are solvable.
Definition: coincontrol.h:29
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:153
EstimationResult est
Definition: fees.h:77
std::map< std::string, std::string > mapValue_t
Definition: wallet.h:339
bool CanGetAddresses(bool internal=false)
Definition: wallet.cpp:1685
int returnedTarget
Definition: fees.h:80
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:126
CScript scriptPubKey
Definition: transaction.h:137
void UpgradeKeyMetadata() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo.
Definition: wallet.cpp:434
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:106
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:14
bool IsWalletFlagSet(uint64_t flag) const
check if a certain wallet flag is set
Definition: wallet.cpp:1724
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:74
constexpr CAmount HIGH_MAX_TX_FEE
-maxtxfee will warn if called with a higher fee than this amount (in satoshis)
Definition: wallet.h:90
void UnlockCoin(const COutPoint &output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3993
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
Definition: wallet.cpp:119
bool DecryptKey(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCryptedSecret, const CPubKey &vchPubKey, CKey &key)
Definition: crypter.cpp:130
std::shared_ptr< CWallet > GetWallet(const std::string &name)
Definition: wallet.cpp:85
static void NotifyTransactionChanged(TransactionTableModel *ttm, const uint256 &hash, ChangeType status)
void postInitProcess()
Wallet post-init setup Gives the wallet a chance to register repetitive tasks and complete post-init ...
Definition: wallet.cpp:4604
std::string TransactionErrorString(const TransactionError err)
Definition: error.cpp:11
CKey key
Definition: key.h:149
CAmount GetAvailableBalance(const CCoinControl *coinControl=nullptr) const
Definition: wallet.cpp:2441
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:499
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:293
size_t change_output_size
Definition: wallet.h:666
bool GetWatchPubKey(const CKeyID &address, CPubKey &pubkey_out) const
Fetches a pubkey from mapWatchKeys if it exists there.
Definition: wallet.cpp:4843
Definition: block.h:72
boost::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:35
void SetHDChain(const CHDChain &chain, bool memonly)
Definition: wallet.cpp:1664
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:54
Optional< T > MakeOptional(bool condition, T &&value)
Substitute for C++17 std::make_optional.
Definition: optional.h:18
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:25
static Mutex g_wallet_release_mutex
Definition: wallet.cpp:94
virtual int getBlockDepth(const uint256 &hash)=0
Get block depth.
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
bool AddWallet(const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:53
Encryption/decryption context with key information.
Definition: crypter.h:76
bool IsFromMe(const isminefilter &filter) const
Definition: wallet.h:569
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:232
const CTxOut & FindNonChangeParentOutput(const CTransaction &tx, int output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Find non-change parent output.
Definition: wallet.cpp:2613
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
CAmount m_mine_untrusted_pending
Untrusted, but in mempool (pending)
Definition: wallet.h:1073
double start
Definition: fees.h:58
virtual int64_t getBlockTime(int height)=0
Get block time. Height must be valid or this function will abort.
bool IsFromMe(const CTransaction &tx) const
should probably be renamed to IsRelevantToMe
Definition: wallet.cpp:1552
bool EraseDestData(const CTxDestination &dest, const std::string &key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Erases a destination data tuple in the store and on disk.
Definition: wallet.cpp:4141
bool DummySignTx(CMutableTransaction &txNew, const std::set< CTxOut > &txouts, bool use_max_sig=false) const
Definition: wallet.h:1100
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:37
bool AddKeyOriginWithDB(WalletBatch &batch, const CPubKey &pubkey, const KeyOriginInfo &info)
Add a KeyOriginInfo to the wallet.
Definition: wallet.cpp:4735
Flag set when a wallet contains no HD seed and no private keys, scripts, addresses, and other watch only things, and is therefore "blank.".
Definition: wallet.h:152
DBErrors ZapSelectTx(std::vector< uint256 > &vHashIn, std::vector< uint256 > &vHashOut)
Definition: walletdb.cpp:603
std::vector< CTxIn > vin
Definition: transaction.h:368
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition: wallet.h:1058
void AvailableCoins(interfaces::Chain::Lock &locked_chain, std::vector< COutput > &vCoins, bool fOnlySafe=true, const CCoinControl *coinControl=nullptr, const CAmount &nMinimumAmount=1, const CAmount &nMaximumAmount=MAX_MONEY, const CAmount &nMinimumSumAmount=MAX_MONEY, const uint64_t nMaximumCount=0) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
populate vCoins with vector of available COutputs.
Definition: wallet.cpp:2457
bool use_max_sig
Whether to use the maximum sized, 72 byte signature when calculating the size of the input spend...
Definition: wallet.h:636
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:179
static const unsigned int DEFAULT_ANCESTOR_LIMIT
Default for -limitancestorcount, max number of in-mempool ancestors.
Definition: validation.h:56
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:748
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &target_value, const CAmount &cost_of_change, std::set< CInputCoin > &out_set, CAmount &value_ret, CAmount not_input_fees)
static const uint32_t SEQUENCE_FINAL
Definition: transaction.h:73
const char * prefix
Definition: rest.cpp:626
bool SelectCoinsMinConf(const CAmount &nTargetValue, const CoinEligibilityFilter &eligibility_filter, std::vector< OutputGroup > groups, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet, const CoinSelectionParams &coin_selection_params, bool &bnb_used) const
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: wallet.cpp:2630
virtual bool AddCScript(const CScript &redeemScript)
CAmount GetDebit(const isminefilter &filter) const
filter decides which addresses will count towards the debit
Definition: wallet.cpp:2206
bool AddKeyPubKeyInner(const CKey &key, const CPubKey &pubkey)
Definition: wallet.cpp:4909
Definition: key.h:144
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:34
virtual bool isInitialBlockDownload()=0
Check if in IBD.
void ListSelected(std::vector< COutPoint > &vOutpoints) const
Definition: coincontrol.h:81
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
bool Lock()
Definition: wallet.cpp:4765
bool FundTransaction(CMutableTransaction &tx, CAmount &nFeeRet, int &nChangePosInOut, std::string &strFailReason, bool lockUnspents, const std::set< int > &setSubtractFeeFromOutputs, CCoinControl)
Insert additional inputs into the transaction by calling CreateTransaction();.
Definition: wallet.cpp:2794
static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
FeeReason reason
Definition: fees.h:78
CAmount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee considering user set parameters and the required fee.
Definition: fees.cpp:19
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:409
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:325
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:131
size_t KeypoolCountExternalKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3524
bool CreateTransaction(interfaces::Chain::Lock &locked_chain, const std::vector< CRecipient > &vecSend, CTransactionRef &tx, CAmount &nFeeRet, int &nChangePosInOut, std::string &strFailReason, const CCoinControl &coin_control, bool sign=true)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: wallet.cpp:2933
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:429
Interface for querying locked chain state, used by legacy code that assumes state won&#39;t change betwee...
Definition: chain.h:64
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:60
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:297
int64_t nOrderPos
position in ordered transaction list
Definition: wallet.h:448
void LoadToWallet(const CWalletTx &wtxIn) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1183
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:161
std::multimap< int64_t, CWalletTx * >::const_iterator m_it_wtxOrdered
Definition: wallet.h:449
CFeeRate GetDiscardRate(const CWallet &wallet)
Return the maximum feerate for discarding change.
Definition: fees.cpp:84
Balance GetBalance(int min_depth=0, bool avoid_reuse=true) const
Definition: wallet.cpp:2412
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule) ...
Definition: consensus.h:19
static bool VerifyDatabaseFile(const fs::path &wallet_path, std::string &warningStr, std::string &errorStr)
Definition: walletdb.cpp:732
bool HasSelected() const
Definition: coincontrol.h:56
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:68
virtual Optional< int > getBlockHeight(const uint256 &hash)=0
Get block height above genesis block.
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:144
bool SetMaxVersion(int nVersion)
change which version we&#39;re allowed to upgrade to (note that this does not immediately imply upgrading...
Definition: wallet.cpp:722
#define PACKAGE_NAME
boost::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:25
virtual std::set< CKeyID > GetKeys() const
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:430
CAmount m_mine_trusted
Trusted, at depth=GetBalance.min_depth or more.
Definition: wallet.h:1072
CAmount m_watchonly_untrusted_pending
Definition: wallet.h:1076
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
Definition: system.h:379
int m_min_depth
Minimum chain depth value for coin availability.
Definition: coincontrol.h:45
static std::string WriteHDKeypath(std::vector< uint32_t > &keypath)
int m_input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: coinselection.h:41
CAmount m_watchonly_immature
Definition: wallet.h:1077
WalletFeature
(client) version numbers for particular wallet features
Definition: wallet.h:104
void MarkDirty()
make sure balances are recalculated
Definition: wallet.h:533
std::vector< std::shared_ptr< CWallet > > GetWallets()
Definition: wallet.cpp:79
std::string translated
Definition: translation.h:19
void MaybeResendWalletTxs()
Called periodically by the schedule thread.
Definition: wallet.cpp:2398
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:512
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:238
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:92
bool IsSpent(interfaces::Chain::Lock &locked_chain, const uint256 &hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Outpoint is spent if any non-conflicted transaction spends it:
Definition: wallet.cpp:812
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
Implement lookup of key origin information through wallet key metadata.
Definition: wallet.cpp:4716
bool IsCoinBase() const
Definition: wallet.h:615
void LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Adds a destination data tuple to the store, without saving it to disk.
Definition: wallet.cpp:4148
DBErrors ZapWalletTx(std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:641
A signature creator for transactions.
Definition: sign.h:36
std::set< CKeyID > GetKeys() const override
Definition: wallet.cpp:4874
std::string FormatMoney(const CAmount &n)
Money parsing/formatting utilities.
Definition: moneystr.cpp:12
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:76
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:94
WalletCreationStatus
Definition: wallet.h:53
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:431
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
static uint32_t GetLocktimeForNewTransaction(interfaces::Chain &chain, interfaces::Chain::Lock &locked_chain)
Return a height-based locktime for new transactions (uses the height of the current chain tip unless ...
Definition: wallet.cpp:2861
const unsigned char * begin() const
Definition: key.h:89
bool MarkReplaced(const uint256 &originalHash, const uint256 &newHash)
Mark a transaction as replaced by another transaction (e.g., BIP 125).
Definition: wallet.cpp:1029
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:48
bool EncryptWallet(const SecureString &strWalletPassphrase)
Definition: wallet.cpp:855
bool IsWalletLoaded(const fs::path &wallet_path)
Return whether a wallet database is currently loaded.
Definition: db.cpp:72
unsigned char * begin()
Definition: uint256.h:55
virtual void initError(const std::string &message)=0
Send init error.
double withinTarget
Definition: fees.h:60
bool WriteWalletFlags(const uint64_t flags)
Definition: walletdb.cpp:753
bool Exists() const
Return whether the wallet exists.
Definition: walletutil.cpp:101
static constexpr int64_t TIMESTAMP_WINDOW
Timestamp window used as a grace period by code that compares external timestamps (such as timestamps...
Definition: chain.h:30
CAmount m_watchonly_trusted
Definition: wallet.h:1075
CAmount GetImmatureWatchOnlyCredit(interfaces::Chain::Lock &locked_chain, const bool fUseCache=true) const
Definition: wallet.cpp:2283
bool RemoveWatchOnly(const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:584
bool IsChange(const CTxOut &txout) const
Definition: wallet.cpp:1510
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:155
bool IsNull() const
Definition: uint256.h:31
void DeriveNewChildKey(WalletBatch &batch, CKeyMetadata &metadata, CKey &secret, bool internal=false) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:301
bool ImportScripts(const std::set< CScript > scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:1781
OutputType
Definition: outputtype.h:16
bool fFromMe
From me flag is set to 1 for transactions that were created by the wallet on this bitcoin node...
Definition: wallet.h:447
static bool Recover(const fs::path &wallet_path, void *callbackDataIn, bool(*recoverKVcallback)(void *callbackData, CDataStream ssKey, CDataStream ssValue), std::string &out_backup_filename)
Definition: walletdb.cpp:691
bool AddWatchOnly(const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Private version of AddWatchOnly method which does not accept a timestamp, and which will reset the wa...
Definition: wallet.cpp:572
static const bool DEFAULT_WALLET_RBF
-walletrbf default
Definition: wallet.h:82
Coin Control Features.
Definition: coincontrol.h:19
CFeeRate GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:29
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Adds an encrypted key to the store, without saving it to disk (used by LoadWallet) ...
Definition: wallet.cpp:471
const std::vector< CTxIn > vin
Definition: transaction.h:287
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:992
std::string ToString() const
Definition: wallet.cpp:238
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:139
int desiredTarget
Definition: fees.h:79
bool IsTrusted(interfaces::Chain::Lock &locked_chain) const
Definition: wallet.cpp:2306
Access to the wallet database.
Definition: walletdb.h:178
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.h:557
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: wallet.h:428
DBErrors ZapSelectTx(std::vector< uint256 > &vHashIn, std::vector< uint256 > &vHashOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3373
bool GetNewChangeDestination(const OutputType type, CTxDestination &dest, std::string &error)
Definition: wallet.cpp:3727
void KeepKey(int64_t nIndex)
Definition: wallet.cpp:3659
bool HaveKey(const CWallet &wallet, const CKey &key)
Checks if a CKey is in the given CWallet compressed or otherwise.
Definition: rpcwallet.cpp:73
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
Definition: coincontrol.h:39
boost::signals2::signal< void()> NotifyUnload
Wallet is about to be unloaded.
Definition: wallet.h:1224
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:147
size_t change_spend_size
Definition: wallet.h:667
void BlockDisconnected(const CBlock &block) override
Definition: wallet.cpp:1439
bool AddWatchOnlyWithDB(WalletBatch &batch, const CScript &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:552
bool SetAddressBook(const CTxDestination &address, const std::string &strName, const std::string &purpose)
Definition: wallet.cpp:3445
bool done
const uint32_t BIP32_HARDENED_KEY_LIMIT
Definition: wallet.cpp:229
void MarkConflicted(const uint256 &hashBlock, const uint256 &hashTx)
Definition: wallet.cpp:1335
static bool ValidSize(const std::vector< unsigned char > &vch)
Definition: pubkey.h:74
std::vector< CKeyID > GetAffectedKeys(const CScript &spk, const SigningProvider &provider)
Definition: wallet.cpp:243
void MarkDirty()
Definition: wallet.cpp:1020
DBErrors ReorderTransactions()
Definition: wallet.cpp:951
COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn, bool fSolvableIn, bool fSafeIn, bool use_max_sig_in=false)
Definition: wallet.h:645
bool IsLockedCoin(uint256 hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:4005
void SetNull()
Definition: block.h:144
static void NotifyCanGetAddressesChanged(WalletModel *walletmodel)
void UnsetWalletFlag(uint64_t flag)
Unsets a single wallet flag.
Definition: wallet.cpp:1710
bool DelAddressBook(const CTxDestination &address)
Definition: wallet.cpp:3451
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:737
std::string AmountHighWarn(const std::string &optname)
Definition: error.cpp:44
opcodetype
Script opcodes.
Definition: script.h:54
bool SelectCoins(const std::vector< COutput > &vAvailableCoins, const CAmount &nTargetValue, std::set< CInputCoin > &setCoinsRet, CAmount &nValueRet, const CCoinControl &coin_control, CoinSelectionParams &coin_selection_params, bool &bnb_used) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Select a set of coins such that nValueRet >= nTargetValue and at least all coins from coinControl are...
Definition: wallet.cpp:2684
int64_t nTime
The time at which the key was generated. Set in AddKeypoolPubKeyWithDB.
Definition: wallet.h:221
bool DummySignInput(CTxIn &tx_in, const CTxOut &txout, bool use_max_sig=false) const
Definition: wallet.cpp:1752
const int DEFAULT_MAX_DEPTH
Definition: coincontrol.h:16
unsigned int ComputeTimeSmart(const CWalletTx &wtx) const
Compute smart timestamp for a transaction being added to the wallet.
Definition: wallet.cpp:4093
void GetAmounts(std::list< COutputEntry > &listReceived, std::list< COutputEntry > &listSent, CAmount &nFee, const isminefilter &filter) const
Definition: wallet.cpp:1917
int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Definition: policy.cpp:248
DBErrors LoadWallet(bool &fFirstRunRet)
Definition: wallet.cpp:3341
static const uint256 ABANDON_HASH
Constant used in hashBlock to indicate tx has been abandoned.
Definition: wallet.h:400
bool AddKeyPubKeyWithDB(WalletBatch &batch, const CKey &key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Adds a key to the store, and saves it to disk.
Definition: wallet.cpp:356
CAmount m_mine_immature
Immature coinbases in the main chain.
Definition: wallet.h:1074
static CAmount balance
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:206
int nDepth
Definition: wallet.h:624
const std::string & GetName() const
Get wallet name.
Definition: walletutil.h:29
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:126
CAmount GetCredit(interfaces::Chain::Lock &locked_chain, const isminefilter &filter) const
Definition: wallet.cpp:2221
void ReacceptWalletTransactions(interfaces::Chain::Lock &locked_chain) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:2129
void TransactionAddedToMempool(const CTransactionRef &tx) override
Definition: wallet.cpp:1396
void WalletLogPrintf(std::string fmt, Params... parameters) const
Prepends the wallet name in logging output to ease debugging in multi-wallet use cases.
Definition: wallet.h:1353
bool SubmitMemoryPoolAndRelay(std::string &err_string, bool relay, interfaces::Chain::Lock &locked_chain)
Definition: wallet.cpp:2157
double end
Definition: fees.h:59
EstimatorBucket fail
Definition: fees.h:70
bool NewKeyPool()
Mark old keypool keys as used, and generate all new keys.
Definition: wallet.cpp:3490
void UpdateTimeFirstKey(int64_t nCreateTime) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Update wallet first key creation time.
Definition: wallet.cpp:480
static bool VerifyEnvironment(const fs::path &wallet_path, std::string &errorStr)
Definition: walletdb.cpp:727
An input of a transaction.
Definition: transaction.h:63
bool AddCScript(const CScript &redeemScript) override
Definition: wallet.cpp:492
int CalculateMaximumSignedInputSize(const CTxOut &txout, const CWallet *wallet, bool use_max_sig)
Definition: wallet.cpp:1907
void GetStrongRandBytes(unsigned char *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
Definition: random.cpp:660
bool IsHDEnabled() const
Definition: wallet.cpp:1673
bool GetReservedDestination(const OutputType type, CTxDestination &pubkey, bool internal)
Reserve an address.
Definition: wallet.cpp:3922
#define LOCK(cs)
Definition: sync.h:182
const char * name
Definition: rest.cpp:39
bool InMempool() const
Definition: wallet.cpp:2301
static void NotifyAddressBookChanged(WalletModel *walletmodel, const CTxDestination &address, const std::string &label, bool isMine, const std::string &purpose, ChangeType status)
const uint256 & GetHash() const
Definition: transaction.h:322
void SetTx(CTransactionRef arg)
Definition: wallet.h:527
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:37
std::shared_ptr< CWallet > LoadWallet(interfaces::Chain &chain, const WalletLocation &location, std::string &error, std::string &warning)
Definition: wallet.cpp:142
const SigningProvider & DUMMY_SIGNING_PROVIDER
CKeyPool()
Definition: wallet.cpp:4622
virtual Optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
bool HasWalletSpend(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Check if a given transaction has any of its outputs spent by another transaction in the wallet...
Definition: wallet.cpp:757
void LoadKeyMetadata(const CKeyID &keyID, const CKeyMetadata &metadata) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Load metadata (used by LoadWallet)
Definition: wallet.cpp:420
CTxDestination destChange
Custom change destination, if not set an address is generated.
Definition: coincontrol.h:23
Chain & m_chain
Definition: wallet.cpp:509
bool IsValid() const
Definition: pubkey.h:171
Fast randomness source.
Definition: random.h:100
std::set< uint256 > GetConflicts() const NO_THREAD_SAFETY_ANALYSIS
Definition: wallet.cpp:2185
void Select(const COutPoint &output)
Definition: coincontrol.h:66
uint256 uint256S(const char *str)
Definition: uint256.h:131
An encapsulated public key.
Definition: pubkey.h:30
bool fAllowOtherInputs
If false, allows unselected inputs, but requires all selected inputs be used.
Definition: coincontrol.h:27
CAmount GetImmatureCredit(interfaces::Chain::Lock &locked_chain, bool fUseCache=true) const
Definition: wallet.cpp:2238
bool TransactionCanBeAbandoned(const uint256 &hashTx) const
Return whether transaction can be abandoned.
Definition: wallet.cpp:1261
std::vector< OutputGroup > GroupOutputs(const std::vector< COutput > &outputs, bool single_coin) const
Definition: wallet.cpp:4686
bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Adds a destination data tuple to the store, and saves it to disk.
Definition: wallet.cpp:4132
std::map< CKeyID, CPubKey > pubkeys
bool AddWatchOnlyInMem(const CScript &dest)
Definition: wallet.cpp:540
uint32_t n
Definition: transaction.h:22
fs::path WalletDataFilePath(const fs::path &wallet_path)
Given a wallet directory path or legacy file path, return path to main data file in the wallet databa...
Definition: db.cpp:84
void UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Definition: wallet.cpp:3999
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:157
static const bool DEFAULT_WALLETBROADCAST
Definition: wallet.h:83
const std::vector< CTxOut > vout
Definition: transaction.h:288
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Adds a key to the store, and saves it to disk.
Definition: wallet.cpp:396
double inMempool
Definition: fees.h:62
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:121