1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_WALLET_WALLET_H
7 #define BITCOIN_WALLET_WALLET_H
11 #include "tinyformat.h"
12 #include "ui_interface.h"
13 #include "utilstrencodings.h"
14 #include "validationinterface.h"
15 #include "script/ismine.h"
16 #include "script/sign.h"
17 #include "wallet/crypter.h"
18 #include "wallet/walletdb.h"
19 #include "wallet/rpcwallet.h"
31 #include <boost/shared_ptr.hpp>
33 extern CWallet
* pwalletMain
;
38 extern CFeeRate payTxFee
;
39 extern unsigned int nTxConfirmTarget
;
40 extern bool bSpendZeroConfChange
;
41 extern bool fWalletRbf
;
43 static const unsigned int DEFAULT_KEYPOOL_SIZE
= 100;
45 static const CAmount DEFAULT_TRANSACTION_FEE
= 0;
46 //! -fallbackfee default
47 static const CAmount DEFAULT_FALLBACK_FEE
= 20000;
49 static const CAmount DEFAULT_TRANSACTION_MINFEE
= 1000;
50 //! minimum recommended increment for BIP 125 replacement txs
51 static const CAmount WALLET_INCREMENTAL_RELAY_FEE
= 5000;
52 //! target minimum change amount
53 static const CAmount MIN_CHANGE
= CENT
;
54 //! final minimum change amount after paying for fees
55 static const CAmount MIN_FINAL_CHANGE
= MIN_CHANGE
/2;
56 //! Default for -spendzeroconfchange
57 static const bool DEFAULT_SPEND_ZEROCONF_CHANGE
= true;
58 //! Default for -walletrejectlongchains
59 static const bool DEFAULT_WALLET_REJECT_LONG_CHAINS
= false;
60 //! -txconfirmtarget default
61 static const unsigned int DEFAULT_TX_CONFIRM_TARGET
= 6;
62 //! -walletrbf default
63 static const bool DEFAULT_WALLET_RBF
= false;
64 static const bool DEFAULT_WALLETBROADCAST
= true;
65 static const bool DEFAULT_DISABLE_WALLET
= false;
66 //! if set, all keys will be derived by using BIP32
67 static const bool DEFAULT_USE_HD_WALLET
= true;
69 extern const char * DEFAULT_WALLET_DAT
;
78 class CBlockPolicyEstimator
;
81 /** (client) version numbers for particular wallet features */
84 FEATURE_BASE
= 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
86 FEATURE_WALLETCRYPT
= 40000, // wallet encryption
87 FEATURE_COMPRPUBKEY
= 60000, // compressed public keys
89 FEATURE_HD
= 130000, // Hierarchical key derivation after BIP32 (HD Wallet)
91 FEATURE_HD_SPLIT
= 139900, // Wallet with HD chain split (change outputs will use m/0'/1'/k)
93 FEATURE_LATEST
= FEATURE_COMPRPUBKEY
// HD is optional, use FEATURE_COMPRPUBKEY as latest version
97 /** A key pool entry */
103 bool fInternal
; // for change outputs
106 CKeyPool(const CPubKey
& vchPubKeyIn
, bool internalIn
);
108 ADD_SERIALIZE_METHODS
;
110 template <typename Stream
, typename Operation
>
111 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
112 int nVersion
= s
.GetVersion();
113 if (!(s
.GetType() & SER_GETHASH
))
116 READWRITE(vchPubKey
);
117 if (ser_action
.ForRead()) {
119 READWRITE(fInternal
);
121 catch (std::ios_base::failure
&) {
122 /* flag as external address if we can't read the internal boolean
123 (this will be the case for any wallet before the HD chain split version) */
128 READWRITE(fInternal
);
133 /** Address book data */
134 class CAddressBookData
145 typedef std::map
<std::string
, std::string
> StringMap
;
151 CScript scriptPubKey
;
153 bool fSubtractFeeFromAmount
;
156 typedef std::map
<std::string
, std::string
> mapValue_t
;
159 static inline void ReadOrderPos(int64_t& nOrderPos
, mapValue_t
& mapValue
)
161 if (!mapValue
.count("n"))
163 nOrderPos
= -1; // TODO: calculate elsewhere
166 nOrderPos
= atoi64(mapValue
["n"].c_str());
170 static inline void WriteOrderPos(const int64_t& nOrderPos
, mapValue_t
& mapValue
)
174 mapValue
["n"] = i64tostr(nOrderPos
);
179 CTxDestination destination
;
184 /** A transaction with a merkle branch linking it to the block chain. */
188 /** Constant used in hashBlock to indicate tx has been abandoned */
189 static const uint256 ABANDON_HASH
;
195 /* An nIndex == -1 means that hashBlock (in nonzero) refers to the earliest
196 * block in the chain we know this or any in-wallet dependency conflicts
197 * with. Older clients interpret nIndex == -1 as unconfirmed for backward
204 SetTx(MakeTransactionRef());
208 CMerkleTx(CTransactionRef arg
)
210 SetTx(std::move(arg
));
214 /** Helper conversion operator to allow passing CMerkleTx where CTransaction is expected.
215 * TODO: adapt callers and remove this operator. */
216 operator const CTransaction
&() const { return *tx
; }
220 hashBlock
= uint256();
224 void SetTx(CTransactionRef arg
)
229 ADD_SERIALIZE_METHODS
;
231 template <typename Stream
, typename Operation
>
232 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
233 std::vector
<uint256
> vMerkleBranch
; // For compatibility with older versions.
235 READWRITE(hashBlock
);
236 READWRITE(vMerkleBranch
);
240 void SetMerkleBranch(const CBlockIndex
* pIndex
, int posInBlock
);
243 * Return depth of transaction in blockchain:
244 * <0 : conflicts with a transaction this deep in the blockchain
245 * 0 : in memory pool, waiting to be included in a block
246 * >=1 : this many blocks deep in the main chain
248 int GetDepthInMainChain(const CBlockIndex
* &pindexRet
) const;
249 int GetDepthInMainChain() const { const CBlockIndex
*pindexRet
; return GetDepthInMainChain(pindexRet
); }
250 bool IsInMainChain() const { const CBlockIndex
*pindexRet
; return GetDepthInMainChain(pindexRet
) > 0; }
251 int GetBlocksToMaturity() const;
252 /** Pass this transaction to the mempool. Fails if absolute fee exceeds absurd fee. */
253 bool AcceptToMemoryPool(const CAmount
& nAbsurdFee
, CValidationState
& state
);
254 bool hashUnset() const { return (hashBlock
.IsNull() || hashBlock
== ABANDON_HASH
); }
255 bool isAbandoned() const { return (hashBlock
== ABANDON_HASH
); }
256 void setAbandoned() { hashBlock
= ABANDON_HASH
; }
258 const uint256
& GetHash() const { return tx
->GetHash(); }
259 bool IsCoinBase() const { return tx
->IsCoinBase(); }
263 * A transaction with a bunch of additional info that only the owner cares about.
264 * It includes any unrecorded transactions needed to link it back to the block chain.
266 class CWalletTx
: public CMerkleTx
269 const CWallet
* pwallet
;
273 * Key/value map with information about the transaction.
275 * The following keys can be read and written through the map and are
276 * serialized in the wallet database:
278 * "comment", "to" - comment strings provided to sendtoaddress,
279 * sendfrom, sendmany wallet RPCs
280 * "replaces_txid" - txid (as HexStr) of transaction replaced by
281 * bumpfee on transaction created by bumpfee
282 * "replaced_by_txid" - txid (as HexStr) of transaction created by
283 * bumpfee on transaction replaced by bumpfee
284 * "from", "message" - obsolete fields that could be set in UI prior to
285 * 2011 (removed in commit 4d9b223)
287 * The following keys are serialized in the wallet database, but shouldn't
288 * be read or written through the map (they will be temporarily added and
289 * removed from the map during serialization):
291 * "fromaccount" - serialized strFromAccount value
292 * "n" - serialized nOrderPos value
293 * "timesmart" - serialized nTimeSmart value
294 * "spent" - serialized vfSpent value that existed prior to
295 * 2014 (removed in commit 93a18a3)
298 std::vector
<std::pair
<std::string
, std::string
> > vOrderForm
;
299 unsigned int fTimeReceivedIsTxTime
;
300 unsigned int nTimeReceived
; //!< time received by this node
302 * Stable timestamp that never changes, and reflects the order a transaction
303 * was added to the wallet. Timestamp is based on the block time for a
304 * transaction added as part of a block, or else the time when the
305 * transaction was received if it wasn't part of a block, with the timestamp
306 * adjusted in both cases so timestamp order matches the order transactions
307 * were added to the wallet. More details can be found in
308 * CWallet::ComputeTimeSmart().
310 unsigned int nTimeSmart
;
312 * From me flag is set to 1 for transactions that were created by the wallet
313 * on this bitcoin node, and set to 0 for transactions that were created
314 * externally and came in through the network or sendrawtransaction RPC.
317 std::string strFromAccount
;
318 int64_t nOrderPos
; //!< position in ordered transaction list
321 mutable bool fDebitCached
;
322 mutable bool fCreditCached
;
323 mutable bool fImmatureCreditCached
;
324 mutable bool fAvailableCreditCached
;
325 mutable bool fWatchDebitCached
;
326 mutable bool fWatchCreditCached
;
327 mutable bool fImmatureWatchCreditCached
;
328 mutable bool fAvailableWatchCreditCached
;
329 mutable bool fChangeCached
;
330 mutable CAmount nDebitCached
;
331 mutable CAmount nCreditCached
;
332 mutable CAmount nImmatureCreditCached
;
333 mutable CAmount nAvailableCreditCached
;
334 mutable CAmount nWatchDebitCached
;
335 mutable CAmount nWatchCreditCached
;
336 mutable CAmount nImmatureWatchCreditCached
;
337 mutable CAmount nAvailableWatchCreditCached
;
338 mutable CAmount nChangeCached
;
345 CWalletTx(const CWallet
* pwalletIn
, CTransactionRef arg
) : CMerkleTx(std::move(arg
))
350 void Init(const CWallet
* pwalletIn
)
355 fTimeReceivedIsTxTime
= false;
359 strFromAccount
.clear();
360 fDebitCached
= false;
361 fCreditCached
= false;
362 fImmatureCreditCached
= false;
363 fAvailableCreditCached
= false;
364 fWatchDebitCached
= false;
365 fWatchCreditCached
= false;
366 fImmatureWatchCreditCached
= false;
367 fAvailableWatchCreditCached
= false;
368 fChangeCached
= false;
371 nImmatureCreditCached
= 0;
372 nAvailableCreditCached
= 0;
373 nWatchDebitCached
= 0;
374 nWatchCreditCached
= 0;
375 nAvailableWatchCreditCached
= 0;
376 nImmatureWatchCreditCached
= 0;
381 ADD_SERIALIZE_METHODS
;
383 template <typename Stream
, typename Operation
>
384 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
385 if (ser_action
.ForRead())
389 if (!ser_action
.ForRead())
391 mapValue
["fromaccount"] = strFromAccount
;
393 WriteOrderPos(nOrderPos
, mapValue
);
396 mapValue
["timesmart"] = strprintf("%u", nTimeSmart
);
399 READWRITE(*(CMerkleTx
*)this);
400 std::vector
<CMerkleTx
> vUnused
; //!< Used to be vtxPrev
403 READWRITE(vOrderForm
);
404 READWRITE(fTimeReceivedIsTxTime
);
405 READWRITE(nTimeReceived
);
409 if (ser_action
.ForRead())
411 strFromAccount
= mapValue
["fromaccount"];
413 ReadOrderPos(nOrderPos
, mapValue
);
415 nTimeSmart
= mapValue
.count("timesmart") ? (unsigned int)atoi64(mapValue
["timesmart"]) : 0;
418 mapValue
.erase("fromaccount");
419 mapValue
.erase("spent");
421 mapValue
.erase("timesmart");
424 //! make sure balances are recalculated
427 fCreditCached
= false;
428 fAvailableCreditCached
= false;
429 fImmatureCreditCached
= false;
430 fWatchDebitCached
= false;
431 fWatchCreditCached
= false;
432 fAvailableWatchCreditCached
= false;
433 fImmatureWatchCreditCached
= false;
434 fDebitCached
= false;
435 fChangeCached
= false;
438 void BindWallet(CWallet
*pwalletIn
)
444 //! filter decides which addresses will count towards the debit
445 CAmount
GetDebit(const isminefilter
& filter
) const;
446 CAmount
GetCredit(const isminefilter
& filter
) const;
447 CAmount
GetImmatureCredit(bool fUseCache
=true) const;
448 CAmount
GetAvailableCredit(bool fUseCache
=true) const;
449 CAmount
GetImmatureWatchOnlyCredit(const bool& fUseCache
=true) const;
450 CAmount
GetAvailableWatchOnlyCredit(const bool& fUseCache
=true) const;
451 CAmount
GetChange() const;
453 void GetAmounts(std::list
<COutputEntry
>& listReceived
,
454 std::list
<COutputEntry
>& listSent
, CAmount
& nFee
, std::string
& strSentAccount
, const isminefilter
& filter
) const;
456 void GetAccountAmounts(const std::string
& strAccount
, CAmount
& nReceived
,
457 CAmount
& nSent
, CAmount
& nFee
, const isminefilter
& filter
) const;
459 bool IsFromMe(const isminefilter
& filter
) const
461 return (GetDebit(filter
) > 0);
464 // True if only scriptSigs are different
465 bool IsEquivalentTo(const CWalletTx
& tx
) const;
467 bool InMempool() const;
468 bool IsTrusted() const;
470 int64_t GetTxTime() const;
471 int GetRequestCount() const;
473 bool RelayWalletTransaction(CConnman
* connman
);
475 std::set
<uint256
> GetConflicts() const;
488 /** Whether we have the private keys to spend this output */
491 /** Whether we know how to spend this output, ignoring the lack of keys */
495 * Whether this output is considered safe to spend. Unconfirmed transactions
496 * from outside keys and unconfirmed replacement transactions are considered
497 * unsafe and will not be used to fund new spending transactions.
501 COutput(const CWalletTx
*txIn
, int iIn
, int nDepthIn
, bool fSpendableIn
, bool fSolvableIn
, bool fSafeIn
)
503 tx
= txIn
; i
= iIn
; nDepth
= nDepthIn
; fSpendable
= fSpendableIn
; fSolvable
= fSolvableIn
; fSafe
= fSafeIn
;
506 std::string
ToString() const;
512 /** Private key that includes an expiration date in case it never gets used. */
517 int64_t nTimeCreated
;
518 int64_t nTimeExpires
;
519 std::string strComment
;
520 //! todo: add something to note what created it (user, getnewaddress, change)
521 //! maybe should have a map<string, string> property map
523 CWalletKey(int64_t nExpires
=0);
525 ADD_SERIALIZE_METHODS
;
527 template <typename Stream
, typename Operation
>
528 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
529 int nVersion
= s
.GetVersion();
530 if (!(s
.GetType() & SER_GETHASH
))
532 READWRITE(vchPrivKey
);
533 READWRITE(nTimeCreated
);
534 READWRITE(nTimeExpires
);
535 READWRITE(LIMITED_STRING(strComment
, 65536));
540 * Internal transfers.
541 * Database key is acentry<account><counter>.
543 class CAccountingEntry
546 std::string strAccount
;
547 CAmount nCreditDebit
;
549 std::string strOtherAccount
;
550 std::string strComment
;
552 int64_t nOrderPos
; //!< position in ordered transaction list
565 strOtherAccount
.clear();
571 ADD_SERIALIZE_METHODS
;
573 template <typename Stream
, typename Operation
>
574 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
575 int nVersion
= s
.GetVersion();
576 if (!(s
.GetType() & SER_GETHASH
))
578 //! Note: strAccount is serialized as part of the key, not here.
579 READWRITE(nCreditDebit
);
581 READWRITE(LIMITED_STRING(strOtherAccount
, 65536));
583 if (!ser_action
.ForRead())
585 WriteOrderPos(nOrderPos
, mapValue
);
587 if (!(mapValue
.empty() && _ssExtra
.empty()))
589 CDataStream
ss(s
.GetType(), s
.GetVersion());
590 ss
.insert(ss
.begin(), '\0');
592 ss
.insert(ss
.end(), _ssExtra
.begin(), _ssExtra
.end());
593 strComment
.append(ss
.str());
597 READWRITE(LIMITED_STRING(strComment
, 65536));
599 size_t nSepPos
= strComment
.find("\0", 0, 1);
600 if (ser_action
.ForRead())
603 if (std::string::npos
!= nSepPos
)
605 CDataStream
ss(std::vector
<char>(strComment
.begin() + nSepPos
+ 1, strComment
.end()), s
.GetType(), s
.GetVersion());
607 _ssExtra
= std::vector
<char>(ss
.begin(), ss
.end());
609 ReadOrderPos(nOrderPos
, mapValue
);
611 if (std::string::npos
!= nSepPos
)
612 strComment
.erase(nSepPos
);
618 std::vector
<char> _ssExtra
;
623 * A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
624 * and provides the ability to create new transactions.
626 class CWallet
: public CCryptoKeyStore
, public CValidationInterface
629 static std::atomic
<bool> fFlushScheduled
;
632 * Select a set of coins such that nValueRet >= nTargetValue and at least
633 * all coins from coinControl are selected; Never select unconfirmed coins
634 * if they are not ours
636 bool SelectCoins(const std::vector
<COutput
>& vAvailableCoins
, const CAmount
& nTargetValue
, std::set
<std::pair
<const CWalletTx
*,unsigned int> >& setCoinsRet
, CAmount
& nValueRet
, const CCoinControl
*coinControl
= NULL
) const;
638 CWalletDB
*pwalletdbEncryption
;
640 //! the current wallet version: clients below this version are not able to load the wallet
643 //! the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
644 int nWalletMaxVersion
;
648 bool fBroadcastTransactions
;
651 * Used to keep track of spent outpoints, and
652 * detect and report conflicts (double-spends or
653 * mutated transactions where the mutant gets mined).
655 typedef std::multimap
<COutPoint
, uint256
> TxSpends
;
656 TxSpends mapTxSpends
;
657 void AddToSpends(const COutPoint
& outpoint
, const uint256
& wtxid
);
658 void AddToSpends(const uint256
& wtxid
);
660 /* Mark a transaction (and its in-wallet descendants) as conflicting with a particular block. */
661 void MarkConflicted(const uint256
& hashBlock
, const uint256
& hashTx
);
663 void SyncMetaData(std::pair
<TxSpends::iterator
, TxSpends::iterator
>);
665 /* the HD chain data model (external chain counters) */
668 /* HD derive new child key (on internal or external chain) */
669 void DeriveNewChildKey(CKeyMetadata
& metadata
, CKey
& secret
, bool internal
= false);
673 std::set
<int64_t> setKeyPool
;
675 int64_t nTimeFirstKey
;
678 * Private version of AddWatchOnly method which does not accept a
679 * timestamp, and which will reset the wallet's nTimeFirstKey value to 1 if
680 * the watch key did not previously have a timestamp associated with it.
681 * Because this is an inherited virtual method, it is accessible despite
682 * being marked private, but it is marked private anyway to encourage use
683 * of the other AddWatchOnly which accepts a timestamp and sets
684 * nTimeFirstKey more intelligently for more efficient rescans.
686 bool AddWatchOnly(const CScript
& dest
) override
;
691 * This lock protects all the fields added by CWallet
693 * fFileBacked (immutable after instantiation)
694 * strWalletFile (immutable after instantiation)
696 mutable CCriticalSection cs_wallet
;
698 const std::string strWalletFile
;
700 void LoadKeyPool(int nIndex
, const CKeyPool
&keypool
)
702 setKeyPool
.insert(nIndex
);
704 // If no metadata exists yet, create a default with the pool key's
705 // creation time. Note that this may be overwritten by actually
706 // stored metadata for that key later, which is fine.
707 CKeyID keyid
= keypool
.vchPubKey
.GetID();
708 if (mapKeyMetadata
.count(keyid
) == 0)
709 mapKeyMetadata
[keyid
] = CKeyMetadata(keypool
.nTime
);
712 // Map from Key ID (for regular keys) or Script ID (for watch-only keys) to
714 std::map
<CTxDestination
, CKeyMetadata
> mapKeyMetadata
;
716 typedef std::map
<unsigned int, CMasterKey
> MasterKeyMap
;
717 MasterKeyMap mapMasterKeys
;
718 unsigned int nMasterKeyMaxID
;
725 CWallet(const std::string
& strWalletFileIn
) : strWalletFile(strWalletFileIn
)
733 delete pwalletdbEncryption
;
734 pwalletdbEncryption
= NULL
;
739 nWalletVersion
= FEATURE_BASE
;
740 nWalletMaxVersion
= FEATURE_BASE
;
743 pwalletdbEncryption
= NULL
;
748 fBroadcastTransactions
= false;
752 std::map
<uint256
, CWalletTx
> mapWallet
;
753 std::list
<CAccountingEntry
> laccentries
;
755 typedef std::pair
<CWalletTx
*, CAccountingEntry
*> TxPair
;
756 typedef std::multimap
<int64_t, TxPair
> TxItems
;
759 int64_t nOrderPosNext
;
760 std::map
<uint256
, int> mapRequestCount
;
762 std::map
<CTxDestination
, CAddressBookData
> mapAddressBook
;
764 CPubKey vchDefaultKey
;
766 std::set
<COutPoint
> setLockedCoins
;
768 const CWalletTx
* GetWalletTx(const uint256
& hash
) const;
770 //! check whether we are allowed to upgrade (or already support) to the named feature
771 bool CanSupportFeature(enum WalletFeature wf
) { AssertLockHeld(cs_wallet
); return nWalletMaxVersion
>= wf
; }
774 * populate vCoins with vector of available COutputs.
776 void AvailableCoins(std::vector
<COutput
>& vCoins
, bool fOnlySafe
=true, const CCoinControl
*coinControl
= NULL
, bool fIncludeZeroValue
=false) const;
779 * Shuffle and select coins until nTargetValue is reached while avoiding
780 * small change; This method is stochastic for some inputs and upon
781 * completion the coin set and corresponding actual target value is
784 bool SelectCoinsMinConf(const CAmount
& nTargetValue
, int nConfMine
, int nConfTheirs
, uint64_t nMaxAncestors
, std::vector
<COutput
> vCoins
, std::set
<std::pair
<const CWalletTx
*,unsigned int> >& setCoinsRet
, CAmount
& nValueRet
) const;
786 bool IsSpent(const uint256
& hash
, unsigned int n
) const;
788 bool IsLockedCoin(uint256 hash
, unsigned int n
) const;
789 void LockCoin(const COutPoint
& output
);
790 void UnlockCoin(const COutPoint
& output
);
791 void UnlockAllCoins();
792 void ListLockedCoins(std::vector
<COutPoint
>& vOutpts
);
795 * keystore implementation
798 CPubKey
GenerateNewKey(bool internal
= false);
799 //! Adds a key to the store, and saves it to disk.
800 bool AddKeyPubKey(const CKey
& key
, const CPubKey
&pubkey
) override
;
801 //! Adds a key to the store, without saving it to disk (used by LoadWallet)
802 bool LoadKey(const CKey
& key
, const CPubKey
&pubkey
) { return CCryptoKeyStore::AddKeyPubKey(key
, pubkey
); }
803 //! Load metadata (used by LoadWallet)
804 bool LoadKeyMetadata(const CTxDestination
& pubKey
, const CKeyMetadata
&metadata
);
806 bool LoadMinVersion(int nVersion
) { AssertLockHeld(cs_wallet
); nWalletVersion
= nVersion
; nWalletMaxVersion
= std::max(nWalletMaxVersion
, nVersion
); return true; }
807 void UpdateTimeFirstKey(int64_t nCreateTime
);
809 //! Adds an encrypted key to the store, and saves it to disk.
810 bool AddCryptedKey(const CPubKey
&vchPubKey
, const std::vector
<unsigned char> &vchCryptedSecret
) override
;
811 //! Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
812 bool LoadCryptedKey(const CPubKey
&vchPubKey
, const std::vector
<unsigned char> &vchCryptedSecret
);
813 bool AddCScript(const CScript
& redeemScript
) override
;
814 bool LoadCScript(const CScript
& redeemScript
);
816 //! Adds a destination data tuple to the store, and saves it to disk
817 bool AddDestData(const CTxDestination
&dest
, const std::string
&key
, const std::string
&value
);
818 //! Erases a destination data tuple in the store and on disk
819 bool EraseDestData(const CTxDestination
&dest
, const std::string
&key
);
820 //! Adds a destination data tuple to the store, without saving it to disk
821 bool LoadDestData(const CTxDestination
&dest
, const std::string
&key
, const std::string
&value
);
822 //! Look up a destination data tuple in the store, return true if found false otherwise
823 bool GetDestData(const CTxDestination
&dest
, const std::string
&key
, std::string
*value
) const;
825 //! Adds a watch-only address to the store, and saves it to disk.
826 bool AddWatchOnly(const CScript
& dest
, int64_t nCreateTime
);
827 bool RemoveWatchOnly(const CScript
&dest
) override
;
828 //! Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
829 bool LoadWatchOnly(const CScript
&dest
);
831 //! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
834 bool Unlock(const SecureString
& strWalletPassphrase
);
835 bool ChangeWalletPassphrase(const SecureString
& strOldWalletPassphrase
, const SecureString
& strNewWalletPassphrase
);
836 bool EncryptWallet(const SecureString
& strWalletPassphrase
);
838 void GetKeyBirthTimes(std::map
<CTxDestination
, int64_t> &mapKeyBirth
) const;
839 unsigned int ComputeTimeSmart(const CWalletTx
& wtx
) const;
842 * Increment the next transaction order id
843 * @return next transaction order id
845 int64_t IncOrderPosNext(CWalletDB
*pwalletdb
= NULL
);
846 DBErrors
ReorderTransactions();
847 bool AccountMove(std::string strFrom
, std::string strTo
, CAmount nAmount
, std::string strComment
= "");
848 bool GetAccountPubkey(CPubKey
&pubKey
, std::string strAccount
, bool bForceNew
= false);
851 bool AddToWallet(const CWalletTx
& wtxIn
, bool fFlushOnClose
=true);
852 bool LoadToWallet(const CWalletTx
& wtxIn
);
853 void SyncTransaction(const CTransaction
& tx
, const CBlockIndex
*pindex
, int posInBlock
) override
;
854 bool AddToWalletIfInvolvingMe(const CTransaction
& tx
, const CBlockIndex
* pIndex
, int posInBlock
, bool fUpdate
);
855 CBlockIndex
* ScanForWalletTransactions(CBlockIndex
* pindexStart
, bool fUpdate
= false);
856 void ReacceptWalletTransactions();
857 void ResendWalletTransactions(int64_t nBestBlockTime
, CConnman
* connman
) override
;
858 std::vector
<uint256
> ResendWalletTransactionsBefore(int64_t nTime
, CConnman
* connman
);
859 CAmount
GetBalance() const;
860 CAmount
GetUnconfirmedBalance() const;
861 CAmount
GetImmatureBalance() const;
862 CAmount
GetWatchOnlyBalance() const;
863 CAmount
GetUnconfirmedWatchOnlyBalance() const;
864 CAmount
GetImmatureWatchOnlyBalance() const;
867 * Insert additional inputs into the transaction by
868 * calling CreateTransaction();
870 bool FundTransaction(CMutableTransaction
& tx
, CAmount
& nFeeRet
, bool overrideEstimatedFeeRate
, const CFeeRate
& specificFeeRate
, int& nChangePosInOut
, std::string
& strFailReason
, bool includeWatching
, bool lockUnspents
, const std::set
<int>& setSubtractFeeFromOutputs
, bool keepReserveKey
= true, const CTxDestination
& destChange
= CNoDestination());
871 bool SignTransaction(CMutableTransaction
& tx
);
874 * Create a new transaction paying the recipients with a set of coins
875 * selected by SelectCoins(); Also create the change output, when needed
876 * @note passing nChangePosInOut as -1 will result in setting a random position
878 bool CreateTransaction(const std::vector
<CRecipient
>& vecSend
, CWalletTx
& wtxNew
, CReserveKey
& reservekey
, CAmount
& nFeeRet
, int& nChangePosInOut
,
879 std::string
& strFailReason
, const CCoinControl
*coinControl
= NULL
, bool sign
= true);
880 bool CommitTransaction(CWalletTx
& wtxNew
, CReserveKey
& reservekey
, CConnman
* connman
, CValidationState
& state
);
882 void ListAccountCreditDebit(const std::string
& strAccount
, std::list
<CAccountingEntry
>& entries
);
883 bool AddAccountingEntry(const CAccountingEntry
&);
884 bool AddAccountingEntry(const CAccountingEntry
&, CWalletDB
*pwalletdb
);
885 template <typename ContainerType
>
886 bool DummySignTx(CMutableTransaction
&txNew
, const ContainerType
&coins
) const;
888 static CFeeRate minTxFee
;
889 static CFeeRate fallbackFee
;
891 * Estimate the minimum fee considering user set parameters
892 * and the required fee
894 static CAmount
GetMinimumFee(unsigned int nTxBytes
, unsigned int nConfirmTarget
, const CTxMemPool
& pool
, const CBlockPolicyEstimator
& estimator
);
896 * Estimate the minimum fee considering required fee and targetFee or if 0
897 * then fee estimation for nConfirmTarget
899 static CAmount
GetMinimumFee(unsigned int nTxBytes
, unsigned int nConfirmTarget
, const CTxMemPool
& pool
, const CBlockPolicyEstimator
& estimator
, CAmount targetFee
);
901 * Return the minimum required fee taking into account the
902 * floating relay fee and user set minimum transaction fee
904 static CAmount
GetRequiredFee(unsigned int nTxBytes
);
907 size_t KeypoolCountExternalKeys();
908 bool TopUpKeyPool(unsigned int kpSize
= 0);
909 void ReserveKeyFromKeyPool(int64_t& nIndex
, CKeyPool
& keypool
, bool internal
);
910 void KeepKey(int64_t nIndex
);
911 void ReturnKey(int64_t nIndex
);
912 bool GetKeyFromPool(CPubKey
&key
, bool internal
= false);
913 int64_t GetOldestKeyPoolTime();
914 void GetAllReserveKeys(std::set
<CKeyID
>& setAddress
) const;
916 std::set
< std::set
<CTxDestination
> > GetAddressGroupings();
917 std::map
<CTxDestination
, CAmount
> GetAddressBalances();
919 CAmount
GetAccountBalance(const std::string
& strAccount
, int nMinDepth
, const isminefilter
& filter
);
920 CAmount
GetAccountBalance(CWalletDB
& walletdb
, const std::string
& strAccount
, int nMinDepth
, const isminefilter
& filter
);
921 std::set
<CTxDestination
> GetAccountAddresses(const std::string
& strAccount
) const;
923 isminetype
IsMine(const CTxIn
& txin
) const;
925 * Returns amount of debit if the input matches the
926 * filter, otherwise returns 0
928 CAmount
GetDebit(const CTxIn
& txin
, const isminefilter
& filter
) const;
929 isminetype
IsMine(const CTxOut
& txout
) const;
930 CAmount
GetCredit(const CTxOut
& txout
, const isminefilter
& filter
) const;
931 bool IsChange(const CTxOut
& txout
) const;
932 CAmount
GetChange(const CTxOut
& txout
) const;
933 bool IsMine(const CTransaction
& tx
) const;
934 /** should probably be renamed to IsRelevantToMe */
935 bool IsFromMe(const CTransaction
& tx
) const;
936 CAmount
GetDebit(const CTransaction
& tx
, const isminefilter
& filter
) const;
937 /** Returns whether all of the inputs match the filter */
938 bool IsAllFromMe(const CTransaction
& tx
, const isminefilter
& filter
) const;
939 CAmount
GetCredit(const CTransaction
& tx
, const isminefilter
& filter
) const;
940 CAmount
GetChange(const CTransaction
& tx
) const;
941 void SetBestChain(const CBlockLocator
& loc
) override
;
943 DBErrors
LoadWallet(bool& fFirstRunRet
);
944 DBErrors
ZapWalletTx(std::vector
<CWalletTx
>& vWtx
);
945 DBErrors
ZapSelectTx(std::vector
<uint256
>& vHashIn
, std::vector
<uint256
>& vHashOut
);
947 bool SetAddressBook(const CTxDestination
& address
, const std::string
& strName
, const std::string
& purpose
);
949 bool DelAddressBook(const CTxDestination
& address
);
951 void UpdatedTransaction(const uint256
&hashTx
) override
;
953 void Inventory(const uint256
&hash
) override
957 std::map
<uint256
, int>::iterator mi
= mapRequestCount
.find(hash
);
958 if (mi
!= mapRequestCount
.end())
963 void GetScriptForMining(boost::shared_ptr
<CReserveScript
> &script
) override
;
964 void ResetRequestCount(const uint256
&hash
) override
967 mapRequestCount
[hash
] = 0;
970 unsigned int GetKeyPoolSize()
972 AssertLockHeld(cs_wallet
); // setKeyPool
973 return setKeyPool
.size();
976 bool SetDefaultKey(const CPubKey
&vchPubKey
);
978 //! signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
979 bool SetMinVersion(enum WalletFeature
, CWalletDB
* pwalletdbIn
= NULL
, bool fExplicit
= false);
981 //! change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
982 bool SetMaxVersion(int nVersion
);
984 //! get the current wallet format (the oldest client version guaranteed to understand this wallet)
985 int GetVersion() { LOCK(cs_wallet
); return nWalletVersion
; }
987 //! Get wallet transactions that conflict with given transaction (spend same outputs)
988 std::set
<uint256
> GetConflicts(const uint256
& txid
) const;
990 //! Check if a given transaction has any of its outputs spent by another transaction in the wallet
991 bool HasWalletSpend(const uint256
& txid
) const;
993 //! Flush wallet (bitdb flush)
994 void Flush(bool shutdown
=false);
996 //! Verify the wallet database and perform salvage if required
997 static bool Verify();
1000 * Address book entry changed.
1001 * @note called with lock cs_wallet held.
1003 boost::signals2::signal
<void (CWallet
*wallet
, const CTxDestination
1004 &address
, const std::string
&label
, bool isMine
,
1005 const std::string
&purpose
,
1006 ChangeType status
)> NotifyAddressBookChanged
;
1009 * Wallet transaction added, removed or updated.
1010 * @note called with lock cs_wallet held.
1012 boost::signals2::signal
<void (CWallet
*wallet
, const uint256
&hashTx
,
1013 ChangeType status
)> NotifyTransactionChanged
;
1015 /** Show progress e.g. for rescan */
1016 boost::signals2::signal
<void (const std::string
&title
, int nProgress
)> ShowProgress
;
1018 /** Watch-only address added */
1019 boost::signals2::signal
<void (bool fHaveWatchOnly
)> NotifyWatchonlyChanged
;
1021 /** Inquire whether this wallet broadcasts transactions. */
1022 bool GetBroadcastTransactions() const { return fBroadcastTransactions
; }
1023 /** Set whether this wallet broadcasts transactions. */
1024 void SetBroadcastTransactions(bool broadcast
) { fBroadcastTransactions
= broadcast
; }
1026 /* Mark a transaction (and it in-wallet descendants) as abandoned so its inputs may be respent. */
1027 bool AbandonTransaction(const uint256
& hashTx
);
1029 /** Mark a transaction as replaced by another transaction (e.g., BIP 125). */
1030 bool MarkReplaced(const uint256
& originalHash
, const uint256
& newHash
);
1032 /* Returns the wallets help message */
1033 static std::string
GetWalletHelpString(bool showDebug
);
1035 /* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
1036 static CWallet
* CreateWalletFromFile(const std::string walletFile
);
1037 static bool InitLoadWallet();
1040 * Wallet post-init setup
1041 * Gives the wallet a chance to register repetitive tasks and complete post-init tasks
1043 void postInitProcess(CScheduler
& scheduler
);
1045 /* Wallets parameter interaction */
1046 static bool ParameterInteraction();
1048 bool BackupWallet(const std::string
& strDest
);
1050 /* Set the HD chain model (chain child index counters) */
1051 bool SetHDChain(const CHDChain
& chain
, bool memonly
);
1052 const CHDChain
& GetHDChain() const { return hdChain
; }
1054 /* Returns true if HD is enabled */
1055 bool IsHDEnabled() const;
1057 /* Generates a new HD master key (will not be activated) */
1058 CPubKey
GenerateNewHDMasterKey();
1060 /* Set the current HD master key (will reset the chain child index counters)
1061 If possibleOldChain is provided, the parameters from the old chain (version)
1062 will be preserved. */
1063 bool SetHDMasterKey(const CPubKey
& key
, CHDChain
*possibleOldChain
= nullptr);
1066 /** A key allocated from the key pool. */
1067 class CReserveKey
: public CReserveScript
1074 CReserveKey(CWallet
* pwalletIn
)
1077 pwallet
= pwalletIn
;
1080 CReserveKey() = default;
1081 CReserveKey(const CReserveKey
&) = delete;
1082 CReserveKey
& operator=(const CReserveKey
&) = delete;
1090 bool GetReservedKey(CPubKey
&pubkey
, bool internal
= false);
1092 void KeepScript() { KeepKey(); }
1097 * Account information.
1098 * Stored in wallet with key "acc"+string account name.
1112 vchPubKey
= CPubKey();
1115 ADD_SERIALIZE_METHODS
;
1117 template <typename Stream
, typename Operation
>
1118 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
1119 int nVersion
= s
.GetVersion();
1120 if (!(s
.GetType() & SER_GETHASH
))
1121 READWRITE(nVersion
);
1122 READWRITE(vchPubKey
);
1126 // Helper for producing a bunch of max-sized low-S signatures (eg 72 bytes)
1127 // ContainerType is meant to hold pair<CWalletTx *, int>, and be iterable
1128 // so that each entry corresponds to each vIn, in order.
1129 template <typename ContainerType
>
1130 bool CWallet::DummySignTx(CMutableTransaction
&txNew
, const ContainerType
&coins
) const
1132 // Fill in dummy signatures for fee calculation.
1134 for (const auto& coin
: coins
)
1136 const CScript
& scriptPubKey
= coin
.first
->tx
->vout
[coin
.second
].scriptPubKey
;
1137 SignatureData sigdata
;
1139 if (!ProduceSignature(DummySignatureCreator(this), scriptPubKey
, sigdata
))
1143 UpdateTransaction(txNew
, nIn
, sigdata
);
1150 #endif // BITCOIN_WALLET_WALLET_H