Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.h
Show All 14 Lines | |||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
#include "validationinterface.h" | #include "validationinterface.h" | ||||
#include "wallet/crypter.h" | #include "wallet/crypter.h" | ||||
#include "wallet/rpcwallet.h" | #include "wallet/rpcwallet.h" | ||||
#include "wallet/walletdb.h" | #include "wallet/walletdb.h" | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <atomic> | #include <atomic> | ||||
#include <cstdint> | |||||
#include <map> | #include <map> | ||||
#include <set> | #include <set> | ||||
#include <stdexcept> | #include <stdexcept> | ||||
#include <stdint.h> | |||||
#include <string> | #include <string> | ||||
#include <utility> | #include <utility> | ||||
#include <vector> | #include <vector> | ||||
#include <boost/shared_ptr.hpp> | #include <boost/shared_ptr.hpp> | ||||
#include <boost/thread.hpp> | #include <boost/thread.hpp> | ||||
extern CWallet *pwalletMain; | extern CWallet *pwalletMain; | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
class COutput; | class COutput; | ||||
class CReserveKey; | class CReserveKey; | ||||
class CScript; | class CScript; | ||||
class CTxMemPool; | class CTxMemPool; | ||||
class CWalletTx; | class CWalletTx; | ||||
/** (client) version numbers for particular wallet features */ | /** (client) version numbers for particular wallet features */ | ||||
enum WalletFeature { | enum WalletFeature { | ||||
FEATURE_BASE = 10500, // the earliest version new wallets supports (only | // the earliest version new wallets supports (only useful for getinfo's | ||||
// useful for getinfo's clientversion output) | // clientversion output) | ||||
FEATURE_BASE = 10500, | |||||
// wallet encryption | |||||
FEATURE_WALLETCRYPT = 40000, | |||||
// compressed public keys | |||||
FEATURE_COMPRPUBKEY = 60000, | |||||
FEATURE_WALLETCRYPT = 40000, // wallet encryption | // Hierarchical key derivation after BIP32 (HD Wallet) | ||||
FEATURE_COMPRPUBKEY = 60000, // compressed public keys | FEATURE_HD = 130000, | ||||
FEATURE_HD = 130000, // Hierarchical key derivation after BIP32 (HD Wallet) | // HD is optional, use FEATURE_COMPRPUBKEY as latest version | ||||
FEATURE_LATEST = FEATURE_COMPRPUBKEY // HD is optional, use | FEATURE_LATEST = FEATURE_COMPRPUBKEY, | ||||
// FEATURE_COMPRPUBKEY as latest | |||||
// version | |||||
}; | }; | ||||
/** A key pool entry */ | /** A key pool entry */ | ||||
class CKeyPool { | class CKeyPool { | ||||
public: | public: | ||||
int64_t nTime; | int64_t nTime; | ||||
CPubKey vchPubKey; | CPubKey vchPubKey; | ||||
CKeyPool(); | CKeyPool(); | ||||
CKeyPool(const CPubKey &vchPubKeyIn); | CKeyPool(const CPubKey &vchPubKeyIn); | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
int nVersion = s.GetVersion(); | int nVersion = s.GetVersion(); | ||||
if (!(s.GetType() & SER_GETHASH)) READWRITE(nVersion); | if (!(s.GetType() & SER_GETHASH)) { | ||||
READWRITE(nVersion); | |||||
} | |||||
READWRITE(nTime); | READWRITE(nTime); | ||||
READWRITE(vchPubKey); | READWRITE(vchPubKey); | ||||
} | } | ||||
}; | }; | ||||
/** Address book data */ | /** Address book data */ | ||||
class CAddressBookData { | class CAddressBookData { | ||||
public: | public: | ||||
Show All 11 Lines | struct CRecipient { | ||||
CAmount nAmount; | CAmount nAmount; | ||||
bool fSubtractFeeFromAmount; | bool fSubtractFeeFromAmount; | ||||
}; | }; | ||||
typedef std::map<std::string, std::string> mapValue_t; | typedef std::map<std::string, std::string> mapValue_t; | ||||
static inline void ReadOrderPos(int64_t &nOrderPos, mapValue_t &mapValue) { | static inline void ReadOrderPos(int64_t &nOrderPos, mapValue_t &mapValue) { | ||||
if (!mapValue.count("n")) { | if (!mapValue.count("n")) { | ||||
nOrderPos = -1; // TODO: calculate elsewhere | // TODO: calculate elsewhere | ||||
nOrderPos = -1; | |||||
return; | return; | ||||
} | } | ||||
nOrderPos = atoi64(mapValue["n"].c_str()); | nOrderPos = atoi64(mapValue["n"].c_str()); | ||||
} | } | ||||
static inline void WriteOrderPos(const int64_t &nOrderPos, | static inline void WriteOrderPos(const int64_t &nOrderPos, | ||||
mapValue_t &mapValue) { | mapValue_t &mapValue) { | ||||
if (nOrderPos == -1) return; | if (nOrderPos == -1) return; | ||||
mapValue["n"] = i64tostr(nOrderPos); | mapValue["n"] = i64tostr(nOrderPos); | ||||
} | } | ||||
Show All 27 Lines | CMerkleTx() { | ||||
Init(); | Init(); | ||||
} | } | ||||
CMerkleTx(CTransactionRef arg) { | CMerkleTx(CTransactionRef arg) { | ||||
SetTx(std::move(arg)); | SetTx(std::move(arg)); | ||||
Init(); | Init(); | ||||
} | } | ||||
/** Helper conversion operator to allow passing CMerkleTx where CTransaction | /** | ||||
* Helper conversion operator to allow passing CMerkleTx where CTransaction | |||||
* is expected. | * is expected. | ||||
* TODO: adapt callers and remove this operator. */ | * TODO: adapt callers and remove this operator. | ||||
*/ | |||||
operator const CTransaction &() const { return *tx; } | operator const CTransaction &() const { return *tx; } | ||||
void Init() { | void Init() { | ||||
hashBlock = uint256(); | hashBlock = uint256(); | ||||
nIndex = -1; | nIndex = -1; | ||||
} | } | ||||
void SetTx(CTransactionRef arg) { tx = std::move(arg); } | void SetTx(CTransactionRef arg) { tx = std::move(arg); } | ||||
Show All 23 Lines | int GetDepthInMainChain() const { | ||||
const CBlockIndex *pindexRet; | const CBlockIndex *pindexRet; | ||||
return GetDepthInMainChain(pindexRet); | return GetDepthInMainChain(pindexRet); | ||||
} | } | ||||
bool IsInMainChain() const { | bool IsInMainChain() const { | ||||
const CBlockIndex *pindexRet; | const CBlockIndex *pindexRet; | ||||
return GetDepthInMainChain(pindexRet) > 0; | return GetDepthInMainChain(pindexRet) > 0; | ||||
} | } | ||||
int GetBlocksToMaturity() const; | int GetBlocksToMaturity() const; | ||||
/** Pass this transaction to the mempool. Fails if absolute fee exceeds | /** | ||||
* absurd fee. */ | * Pass this transaction to the mempool. Fails if absolute fee exceeds | ||||
* absurd fee. | |||||
*/ | |||||
bool AcceptToMemoryPool(const CAmount &nAbsurdFee, CValidationState &state); | bool AcceptToMemoryPool(const CAmount &nAbsurdFee, CValidationState &state); | ||||
bool hashUnset() const { | bool hashUnset() const { | ||||
return (hashBlock.IsNull() || hashBlock == ABANDON_HASH); | return (hashBlock.IsNull() || hashBlock == ABANDON_HASH); | ||||
} | } | ||||
bool isAbandoned() const { return (hashBlock == ABANDON_HASH); } | bool isAbandoned() const { return (hashBlock == ABANDON_HASH); } | ||||
void setAbandoned() { hashBlock = ABANDON_HASH; } | void setAbandoned() { hashBlock = ABANDON_HASH; } | ||||
const uint256 &GetId() const { return tx->GetId(); } | const uint256 &GetId() const { return tx->GetId(); } | ||||
bool IsCoinBase() const { return tx->IsCoinBase(); } | bool IsCoinBase() const { return tx->IsCoinBase(); } | ||||
}; | }; | ||||
/** | /** | ||||
* A transaction with a bunch of additional info that only the owner cares | * A transaction with a bunch of additional info that only the owner cares | ||||
* about. | * about. It includes any unrecorded transactions needed to link it back to the | ||||
* It includes any unrecorded transactions needed to link it back to the block | * block chain. | ||||
* chain. | |||||
*/ | */ | ||||
class CWalletTx : public CMerkleTx { | class CWalletTx : public CMerkleTx { | ||||
private: | private: | ||||
const CWallet *pwallet; | const CWallet *pwallet; | ||||
public: | public: | ||||
mapValue_t mapValue; | mapValue_t mapValue; | ||||
std::vector<std::pair<std::string, std::string>> vOrderForm; | std::vector<std::pair<std::string, std::string>> vOrderForm; | ||||
▲ Show 20 Lines • Show All 188 Lines • ▼ Show 20 Lines | |||||
/** Private key that includes an expiration date in case it never gets used. */ | /** Private key that includes an expiration date in case it never gets used. */ | ||||
class CWalletKey { | class CWalletKey { | ||||
public: | public: | ||||
CPrivKey vchPrivKey; | CPrivKey vchPrivKey; | ||||
int64_t nTimeCreated; | int64_t nTimeCreated; | ||||
int64_t nTimeExpires; | int64_t nTimeExpires; | ||||
std::string strComment; | std::string strComment; | ||||
//! todo: add something to note what created it (user, getnewaddress, | //! todo: add something to note what created it (user, getnewaddress, | ||||
//! change) | //! change) maybe should have a map<string, string> property map | ||||
//! maybe should have a map<string, string> property map | |||||
CWalletKey(int64_t nExpires = 0); | CWalletKey(int64_t nExpires = 0); | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
int nVersion = s.GetVersion(); | int nVersion = s.GetVersion(); | ||||
▲ Show 20 Lines • Show All 266 Lines • ▼ Show 20 Lines | public: | ||||
CPubKey GenerateNewKey(); | CPubKey GenerateNewKey(); | ||||
void DeriveNewChildKey(CKeyMetadata &metadata, CKey &secret); | void DeriveNewChildKey(CKeyMetadata &metadata, CKey &secret); | ||||
//! Adds a key to the store, and saves it to disk. | //! Adds a key to the store, and saves it to disk. | ||||
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override; | bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override; | ||||
//! Adds a key to the store, without saving it to disk (used by LoadWallet) | //! Adds a key to the store, without saving it to disk (used by LoadWallet) | ||||
bool LoadKey(const CKey &key, const CPubKey &pubkey) { | bool LoadKey(const CKey &key, const CPubKey &pubkey) { | ||||
return CCryptoKeyStore::AddKeyPubKey(key, pubkey); | return CCryptoKeyStore::AddKeyPubKey(key, pubkey); | ||||
} | } | ||||
//! Load metadata (used by LoadWallet) | //! Load metadata (used by LoadWallet) | ||||
bool LoadKeyMetadata(const CTxDestination &pubKey, | bool LoadKeyMetadata(const CTxDestination &pubKey, | ||||
const CKeyMetadata &metadata); | const CKeyMetadata &metadata); | ||||
bool LoadMinVersion(int nVersion) { | bool LoadMinVersion(int nVersion) { | ||||
AssertLockHeld(cs_wallet); | AssertLockHeld(cs_wallet); | ||||
nWalletVersion = nVersion; | nWalletVersion = nVersion; | ||||
nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); | nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); | ||||
▲ Show 20 Lines • Show All 177 Lines • ▼ Show 20 Lines | public: | ||||
bool SetAddressBook(const CTxDestination &address, | bool SetAddressBook(const CTxDestination &address, | ||||
const std::string &strName, const std::string &purpose); | const std::string &strName, const std::string &purpose); | ||||
bool DelAddressBook(const CTxDestination &address); | bool DelAddressBook(const CTxDestination &address); | ||||
void UpdatedTransaction(const uint256 &hashTx) override; | void UpdatedTransaction(const uint256 &hashTx) override; | ||||
void Inventory(const uint256 &hash) override { | void Inventory(const uint256 &hash) override { | ||||
{ | |||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
std::map<uint256, int>::iterator mi = mapRequestCount.find(hash); | std::map<uint256, int>::iterator mi = mapRequestCount.find(hash); | ||||
if (mi != mapRequestCount.end()) (*mi).second++; | if (mi != mapRequestCount.end()) { | ||||
(*mi).second++; | |||||
} | } | ||||
} | } | ||||
void GetScriptForMining(boost::shared_ptr<CReserveScript> &script) override; | void GetScriptForMining(boost::shared_ptr<CReserveScript> &script) override; | ||||
void ResetRequestCount(const uint256 &hash) override { | void ResetRequestCount(const uint256 &hash) override { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
mapRequestCount[hash] = 0; | mapRequestCount[hash] = 0; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | public: | ||||
/** Inquire whether this wallet broadcasts transactions. */ | /** Inquire whether this wallet broadcasts transactions. */ | ||||
bool GetBroadcastTransactions() const { return fBroadcastTransactions; } | bool GetBroadcastTransactions() const { return fBroadcastTransactions; } | ||||
/** Set whether this wallet broadcasts transactions. */ | /** Set whether this wallet broadcasts transactions. */ | ||||
void SetBroadcastTransactions(bool broadcast) { | void SetBroadcastTransactions(bool broadcast) { | ||||
fBroadcastTransactions = broadcast; | fBroadcastTransactions = broadcast; | ||||
} | } | ||||
/* Mark a transaction (and it in-wallet descendants) as abandoned so its | /** | ||||
* inputs may be respent. */ | * Mark a transaction (and it in-wallet descendants) as abandoned so its | ||||
* inputs may be respent. | |||||
*/ | |||||
bool AbandonTransaction(const uint256 &hashTx); | bool AbandonTransaction(const uint256 &hashTx); | ||||
/** Mark a transaction as replaced by another transaction (e.g., BIP 125). | /** | ||||
* Mark a transaction as replaced by another transaction (e.g., BIP 125). | |||||
*/ | */ | ||||
bool MarkReplaced(const uint256 &originalHash, const uint256 &newHash); | bool MarkReplaced(const uint256 &originalHash, const uint256 &newHash); | ||||
/* Returns the wallets help message */ | /* Returns the wallets help message */ | ||||
static std::string GetWalletHelpString(bool showDebug); | static std::string GetWalletHelpString(bool showDebug); | ||||
/* Initializes the wallet, returns a new CWallet instance or a null pointer | /** | ||||
* in case of an error */ | * Initializes the wallet, returns a new CWallet instance or a null pointer | ||||
* in case of an error. | |||||
*/ | |||||
static CWallet *CreateWalletFromFile(const std::string walletFile); | static CWallet *CreateWalletFromFile(const std::string walletFile); | ||||
static bool InitLoadWallet(); | static bool InitLoadWallet(); | ||||
/** | /** | ||||
* Wallet post-init setup | * Wallet post-init setup | ||||
* Gives the wallet a chance to register repetitive tasks and complete | * Gives the wallet a chance to register repetitive tasks and complete | ||||
* post-init tasks | * post-init tasks | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | public: | ||||
void SetNull() { vchPubKey = CPubKey(); } | void SetNull() { vchPubKey = CPubKey(); } | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
int nVersion = s.GetVersion(); | int nVersion = s.GetVersion(); | ||||
if (!(s.GetType() & SER_GETHASH)) READWRITE(nVersion); | if (!(s.GetType() & SER_GETHASH)) { | ||||
READWRITE(nVersion); | |||||
} | |||||
READWRITE(vchPubKey); | READWRITE(vchPubKey); | ||||
} | } | ||||
}; | }; | ||||
// Helper for producing a bunch of max-sized low-S signatures (eg 72 bytes) | // Helper for producing a bunch of max-sized low-S signatures (eg 72 bytes) | ||||
// ContainerType is meant to hold pair<CWalletTx *, int>, and be iterable so | // ContainerType is meant to hold pair<CWalletTx *, int>, and be iterable so | ||||
// that each entry corresponds to each vIn, in order. | // that each entry corresponds to each vIn, in order. | ||||
template <typename ContainerType> | template <typename ContainerType> | ||||
Show All 10 Lines | for (const auto &coin : coins) { | ||||
sigdata)) { | sigdata)) { | ||||
return false; | return false; | ||||
} else { | } else { | ||||
UpdateTransaction(txNew, nIn, sigdata); | UpdateTransaction(txNew, nIn, sigdata); | ||||
} | } | ||||
nIn++; | nIn++; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
#endif // BITCOIN_WALLET_WALLET_H | #endif // BITCOIN_WALLET_WALLET_H |