You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
907 lines
27 KiB
907 lines
27 KiB
13 years ago
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||
10 years ago
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
||
10 years ago
|
// Distributed under the MIT software license, see the accompanying
|
||
13 years ago
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
10 years ago
|
|
||
14 years ago
|
#ifndef BITCOIN_WALLET_H
|
||
|
#define BITCOIN_WALLET_H
|
||
|
|
||
10 years ago
|
#include "amount.h"
|
||
10 years ago
|
#include "primitives/block.h"
|
||
|
#include "primitives/transaction.h"
|
||
12 years ago
|
#include "crypter.h"
|
||
14 years ago
|
#include "key.h"
|
||
13 years ago
|
#include "keystore.h"
|
||
12 years ago
|
#include "main.h"
|
||
13 years ago
|
#include "ui_interface.h"
|
||
10 years ago
|
#include "wallet/wallet_ismine.h"
|
||
|
#include "wallet/walletdb.h"
|
||
12 years ago
|
|
||
|
#include <algorithm>
|
||
|
#include <map>
|
||
|
#include <set>
|
||
|
#include <stdexcept>
|
||
|
#include <stdint.h>
|
||
|
#include <string>
|
||
|
#include <utility>
|
||
|
#include <vector>
|
||
14 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Settings
|
||
|
*/
|
||
11 years ago
|
extern CFeeRate payTxFee;
|
||
10 years ago
|
extern CAmount maxTxFee;
|
||
11 years ago
|
extern unsigned int nTxConfirmTarget;
|
||
11 years ago
|
extern bool bSpendZeroConfChange;
|
||
10 years ago
|
extern bool fSendFreeTransactions;
|
||
10 years ago
|
extern bool fPayAtLeastCustomFee;
|
||
11 years ago
|
|
||
10 years ago
|
//! -paytxfee default
|
||
11 years ago
|
static const CAmount DEFAULT_TRANSACTION_FEE = 0;
|
||
10 years ago
|
//! -paytxfee will warn if called with a higher fee than this amount (in satoshis) per KB
|
||
11 years ago
|
static const CAmount nHighTransactionFeeWarning = 0.01 * COIN;
|
||
10 years ago
|
//! -maxtxfee default
|
||
|
static const CAmount DEFAULT_TRANSACTION_MAXFEE = 0.1 * COIN;
|
||
|
//! -maxtxfee will warn if called with a higher fee than this amount (in satoshis)
|
||
|
static const CAmount nHighTransactionMaxFeeWarning = 100 * nHighTransactionFeeWarning;
|
||
10 years ago
|
//! Largest (in bytes) free transaction we're willing to create
|
||
11 years ago
|
static const unsigned int MAX_FREE_TRANSACTION_CREATE_SIZE = 1000;
|
||
11 years ago
|
|
||
13 years ago
|
class CAccountingEntry;
|
||
11 years ago
|
class CCoinControl;
|
||
13 years ago
|
class COutput;
|
||
12 years ago
|
class CReserveKey;
|
||
|
class CScript;
|
||
|
class CWalletTx;
|
||
14 years ago
|
|
||
13 years ago
|
/** (client) version numbers for particular wallet features */
|
||
13 years ago
|
enum WalletFeature
|
||
|
{
|
||
|
FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
|
||
|
|
||
|
FEATURE_WALLETCRYPT = 40000, // wallet encryption
|
||
|
FEATURE_COMPRPUBKEY = 60000, // compressed public keys
|
||
|
|
||
|
FEATURE_LATEST = 60000
|
||
|
};
|
||
|
|
||
13 years ago
|
|
||
|
/** A key pool entry */
|
||
|
class CKeyPool
|
||
|
{
|
||
|
public:
|
||
12 years ago
|
int64_t nTime;
|
||
13 years ago
|
CPubKey vchPubKey;
|
||
13 years ago
|
|
||
10 years ago
|
CKeyPool();
|
||
|
CKeyPool(const CPubKey& vchPubKeyIn);
|
||
13 years ago
|
|
||
10 years ago
|
ADD_SERIALIZE_METHODS;
|
||
10 years ago
|
|
||
10 years ago
|
template <typename Stream, typename Operation>
|
||
10 years ago
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||
13 years ago
|
if (!(nType & SER_GETHASH))
|
||
|
READWRITE(nVersion);
|
||
|
READWRITE(nTime);
|
||
|
READWRITE(vchPubKey);
|
||
10 years ago
|
}
|
||
13 years ago
|
};
|
||
|
|
||
12 years ago
|
/** Address book data */
|
||
|
class CAddressBookData
|
||
|
{
|
||
|
public:
|
||
|
std::string name;
|
||
12 years ago
|
std::string purpose;
|
||
12 years ago
|
|
||
|
CAddressBookData()
|
||
|
{
|
||
12 years ago
|
purpose = "unknown";
|
||
12 years ago
|
}
|
||
11 years ago
|
|
||
|
typedef std::map<std::string, std::string> StringMap;
|
||
|
StringMap destdata;
|
||
12 years ago
|
};
|
||
|
|
||
11 years ago
|
struct CRecipient
|
||
|
{
|
||
|
CScript scriptPubKey;
|
||
|
CAmount nAmount;
|
||
|
bool fSubtractFeeFromAmount;
|
||
|
};
|
||
10 years ago
|
|
||
|
typedef std::map<std::string, std::string> mapValue_t;
|
||
|
|
||
|
|
||
|
static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
|
||
|
{
|
||
|
if (!mapValue.count("n"))
|
||
|
{
|
||
|
nOrderPos = -1; // TODO: calculate elsewhere
|
||
|
return;
|
||
|
}
|
||
|
nOrderPos = atoi64(mapValue["n"].c_str());
|
||
|
}
|
||
|
|
||
|
|
||
|
static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
|
||
|
{
|
||
|
if (nOrderPos == -1)
|
||
|
return;
|
||
|
mapValue["n"] = i64tostr(nOrderPos);
|
||
|
}
|
||
|
|
||
|
struct COutputEntry
|
||
|
{
|
||
|
CTxDestination destination;
|
||
|
CAmount amount;
|
||
|
int vout;
|
||
|
};
|
||
|
|
||
|
/** A transaction with a merkle branch linking it to the block chain. */
|
||
|
class CMerkleTx : public CTransaction
|
||
14 years ago
|
{
|
||
|
private:
|
||
10 years ago
|
int GetDepthInMainChainINTERNAL(const CBlockIndex* &pindexRet) const;
|
||
14 years ago
|
|
||
10 years ago
|
public:
|
||
|
uint256 hashBlock;
|
||
|
std::vector<uint256> vMerkleBranch;
|
||
|
int nIndex;
|
||
14 years ago
|
|
||
10 years ago
|
// memory only
|
||
|
mutable bool fMerkleVerified;
|
||
13 years ago
|
|
||
13 years ago
|
|
||
10 years ago
|
CMerkleTx()
|
||
|
{
|
||
|
Init();
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
CMerkleTx(const CTransaction& txIn) : CTransaction(txIn)
|
||
|
{
|
||
|
Init();
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void Init()
|
||
|
{
|
||
|
hashBlock = uint256();
|
||
|
nIndex = -1;
|
||
|
fMerkleVerified = false;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
ADD_SERIALIZE_METHODS;
|
||
|
|
||
|
template <typename Stream, typename Operation>
|
||
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||
|
READWRITE(*(CTransaction*)this);
|
||
|
nVersion = this->nVersion;
|
||
|
READWRITE(hashBlock);
|
||
|
READWRITE(vMerkleBranch);
|
||
|
READWRITE(nIndex);
|
||
|
}
|
||
|
|
||
|
int SetMerkleBranch(const CBlock& block);
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Return depth of transaction in blockchain:
|
||
|
* -1 : not in blockchain, and not in memory pool (conflicted transaction)
|
||
|
* 0 : in memory pool, waiting to be included in a block
|
||
|
* >=1 : this many blocks deep in the main chain
|
||
10 years ago
|
*/
|
||
10 years ago
|
int GetDepthInMainChain(const CBlockIndex* &pindexRet) const;
|
||
|
int GetDepthInMainChain() const { const CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet); }
|
||
|
bool IsInMainChain() const { const CBlockIndex *pindexRet; return GetDepthInMainChainINTERNAL(pindexRet) > 0; }
|
||
|
int GetBlocksToMaturity() const;
|
||
|
bool AcceptToMemoryPool(bool fLimitFree=true, bool fRejectAbsurdFee=true);
|
||
|
};
|
||
13 years ago
|
|
||
10 years ago
|
/**
|
||
|
* A transaction with a bunch of additional info that only the owner cares about.
|
||
|
* It includes any unrecorded transactions needed to link it back to the block chain.
|
||
|
*/
|
||
|
class CWalletTx : public CMerkleTx
|
||
|
{
|
||
|
private:
|
||
|
const CWallet* pwallet;
|
||
14 years ago
|
|
||
10 years ago
|
public:
|
||
|
mapValue_t mapValue;
|
||
|
std::vector<std::pair<std::string, std::string> > vOrderForm;
|
||
|
unsigned int fTimeReceivedIsTxTime;
|
||
|
unsigned int nTimeReceived; //! time received by this node
|
||
|
unsigned int nTimeSmart;
|
||
|
char fFromMe;
|
||
|
std::string strFromAccount;
|
||
|
int64_t nOrderPos; //! position in ordered transaction list
|
||
13 years ago
|
|
||
10 years ago
|
// memory only
|
||
|
mutable bool fDebitCached;
|
||
|
mutable bool fCreditCached;
|
||
|
mutable bool fImmatureCreditCached;
|
||
|
mutable bool fAvailableCreditCached;
|
||
|
mutable bool fWatchDebitCached;
|
||
|
mutable bool fWatchCreditCached;
|
||
|
mutable bool fImmatureWatchCreditCached;
|
||
|
mutable bool fAvailableWatchCreditCached;
|
||
|
mutable bool fChangeCached;
|
||
|
mutable CAmount nDebitCached;
|
||
|
mutable CAmount nCreditCached;
|
||
|
mutable CAmount nImmatureCreditCached;
|
||
|
mutable CAmount nAvailableCreditCached;
|
||
|
mutable CAmount nWatchDebitCached;
|
||
|
mutable CAmount nWatchCreditCached;
|
||
|
mutable CAmount nImmatureWatchCreditCached;
|
||
|
mutable CAmount nAvailableWatchCreditCached;
|
||
|
mutable CAmount nChangeCached;
|
||
14 years ago
|
|
||
10 years ago
|
CWalletTx()
|
||
14 years ago
|
{
|
||
10 years ago
|
Init(NULL);
|
||
14 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
CWalletTx(const CWallet* pwalletIn)
|
||
14 years ago
|
{
|
||
10 years ago
|
Init(pwalletIn);
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
|
||
|
{
|
||
|
Init(pwalletIn);
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
|
||
10 years ago
|
{
|
||
10 years ago
|
Init(pwalletIn);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void Init(const CWallet* pwalletIn)
|
||
11 years ago
|
{
|
||
10 years ago
|
pwallet = pwalletIn;
|
||
|
mapValue.clear();
|
||
|
vOrderForm.clear();
|
||
|
fTimeReceivedIsTxTime = false;
|
||
|
nTimeReceived = 0;
|
||
|
nTimeSmart = 0;
|
||
|
fFromMe = false;
|
||
|
strFromAccount.clear();
|
||
|
fDebitCached = false;
|
||
|
fCreditCached = false;
|
||
|
fImmatureCreditCached = false;
|
||
|
fAvailableCreditCached = false;
|
||
|
fWatchDebitCached = false;
|
||
|
fWatchCreditCached = false;
|
||
|
fImmatureWatchCreditCached = false;
|
||
|
fAvailableWatchCreditCached = false;
|
||
|
fChangeCached = false;
|
||
|
nDebitCached = 0;
|
||
|
nCreditCached = 0;
|
||
|
nImmatureCreditCached = 0;
|
||
|
nAvailableCreditCached = 0;
|
||
|
nWatchDebitCached = 0;
|
||
|
nWatchCreditCached = 0;
|
||
|
nAvailableWatchCreditCached = 0;
|
||
|
nImmatureWatchCreditCached = 0;
|
||
|
nChangeCached = 0;
|
||
|
nOrderPos = -1;
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
ADD_SERIALIZE_METHODS;
|
||
11 years ago
|
|
||
10 years ago
|
template <typename Stream, typename Operation>
|
||
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||
|
if (ser_action.ForRead())
|
||
|
Init(NULL);
|
||
|
char fSpent = false;
|
||
14 years ago
|
|
||
10 years ago
|
if (!ser_action.ForRead())
|
||
|
{
|
||
|
mapValue["fromaccount"] = strFromAccount;
|
||
14 years ago
|
|
||
10 years ago
|
WriteOrderPos(nOrderPos, mapValue);
|
||
14 years ago
|
|
||
10 years ago
|
if (nTimeSmart)
|
||
|
mapValue["timesmart"] = strprintf("%u", nTimeSmart);
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
READWRITE(*(CMerkleTx*)this);
|
||
|
std::vector<CMerkleTx> vUnused; //! Used to be vtxPrev
|
||
|
READWRITE(vUnused);
|
||
|
READWRITE(mapValue);
|
||
|
READWRITE(vOrderForm);
|
||
|
READWRITE(fTimeReceivedIsTxTime);
|
||
|
READWRITE(nTimeReceived);
|
||
|
READWRITE(fFromMe);
|
||
|
READWRITE(fSpent);
|
||
12 years ago
|
|
||
10 years ago
|
if (ser_action.ForRead())
|
||
|
{
|
||
|
strFromAccount = mapValue["fromaccount"];
|
||
11 years ago
|
|
||
10 years ago
|
ReadOrderPos(nOrderPos, mapValue);
|
||
13 years ago
|
|
||
10 years ago
|
nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(mapValue["timesmart"]) : 0;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
mapValue.erase("fromaccount");
|
||
|
mapValue.erase("version");
|
||
|
mapValue.erase("spent");
|
||
|
mapValue.erase("n");
|
||
|
mapValue.erase("timesmart");
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
//! make sure balances are recalculated
|
||
|
void MarkDirty()
|
||
|
{
|
||
|
fCreditCached = false;
|
||
|
fAvailableCreditCached = false;
|
||
|
fWatchDebitCached = false;
|
||
|
fWatchCreditCached = false;
|
||
|
fAvailableWatchCreditCached = false;
|
||
|
fImmatureWatchCreditCached = false;
|
||
|
fDebitCached = false;
|
||
|
fChangeCached = false;
|
||
|
}
|
||
|
|
||
|
void BindWallet(CWallet *pwalletIn)
|
||
|
{
|
||
|
pwallet = pwalletIn;
|
||
|
MarkDirty();
|
||
|
}
|
||
|
|
||
|
//! filter decides which addresses will count towards the debit
|
||
|
CAmount GetDebit(const isminefilter& filter) const;
|
||
|
CAmount GetCredit(const isminefilter& filter) const;
|
||
|
CAmount GetImmatureCredit(bool fUseCache=true) const;
|
||
|
CAmount GetAvailableCredit(bool fUseCache=true) const;
|
||
|
CAmount GetImmatureWatchOnlyCredit(const bool& fUseCache=true) const;
|
||
|
CAmount GetAvailableWatchOnlyCredit(const bool& fUseCache=true) const;
|
||
|
CAmount GetChange() const;
|
||
|
|
||
|
void GetAmounts(std::list<COutputEntry>& listReceived,
|
||
|
std::list<COutputEntry>& listSent, CAmount& nFee, std::string& strSentAccount, const isminefilter& filter) const;
|
||
|
|
||
|
void GetAccountAmounts(const std::string& strAccount, CAmount& nReceived,
|
||
|
CAmount& nSent, CAmount& nFee, const isminefilter& filter) const;
|
||
|
|
||
|
bool IsFromMe(const isminefilter& filter) const
|
||
|
{
|
||
|
return (GetDebit(filter) > 0);
|
||
|
}
|
||
|
|
||
|
bool IsTrusted() const;
|
||
|
|
||
|
bool WriteToDisk(CWalletDB *pwalletdb);
|
||
|
|
||
|
int64_t GetTxTime() const;
|
||
|
int GetRequestCount() const;
|
||
|
|
||
|
void RelayWalletTransaction();
|
||
|
|
||
|
std::set<uint256> GetConflicts() const;
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
class COutput
|
||
|
{
|
||
|
public:
|
||
|
const CWalletTx *tx;
|
||
|
int i;
|
||
|
int nDepth;
|
||
|
bool fSpendable;
|
||
|
|
||
|
COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
|
||
|
{
|
||
|
tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
|
||
|
}
|
||
|
|
||
|
std::string ToString() const;
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/** Private key that includes an expiration date in case it never gets used. */
|
||
|
class CWalletKey
|
||
|
{
|
||
|
public:
|
||
|
CPrivKey vchPrivKey;
|
||
|
int64_t nTimeCreated;
|
||
|
int64_t nTimeExpires;
|
||
|
std::string strComment;
|
||
|
//! todo: add something to note what created it (user, getnewaddress, change)
|
||
|
//! maybe should have a map<string, string> property map
|
||
|
|
||
|
CWalletKey(int64_t nExpires=0);
|
||
|
|
||
|
ADD_SERIALIZE_METHODS;
|
||
|
|
||
|
template <typename Stream, typename Operation>
|
||
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||
|
if (!(nType & SER_GETHASH))
|
||
|
READWRITE(nVersion);
|
||
|
READWRITE(vchPrivKey);
|
||
|
READWRITE(nTimeCreated);
|
||
|
READWRITE(nTimeExpires);
|
||
|
READWRITE(LIMITED_STRING(strComment, 65536));
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
|
||
|
* and provides the ability to create new transactions.
|
||
|
*/
|
||
|
class CWallet : public CCryptoKeyStore, public CValidationInterface
|
||
|
{
|
||
|
private:
|
||
|
bool SelectCoins(const CAmount& nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, CAmount& nValueRet, const CCoinControl *coinControl = NULL) const;
|
||
|
|
||
|
CWalletDB *pwalletdbEncryption;
|
||
|
|
||
|
//! the current wallet version: clients below this version are not able to load the wallet
|
||
|
int nWalletVersion;
|
||
|
|
||
|
//! the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
|
||
|
int nWalletMaxVersion;
|
||
|
|
||
|
int64_t nNextResend;
|
||
|
int64_t nLastResend;
|
||
|
|
||
|
/**
|
||
|
* Used to keep track of spent outpoints, and
|
||
|
* detect and report conflicts (double-spends or
|
||
|
* mutated transactions where the mutant gets mined).
|
||
|
*/
|
||
|
typedef std::multimap<COutPoint, uint256> TxSpends;
|
||
|
TxSpends mapTxSpends;
|
||
|
void AddToSpends(const COutPoint& outpoint, const uint256& wtxid);
|
||
|
void AddToSpends(const uint256& wtxid);
|
||
|
|
||
|
void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>);
|
||
|
|
||
|
public:
|
||
|
/*
|
||
|
* Main wallet lock.
|
||
|
* This lock protects all the fields added by CWallet
|
||
|
* except for:
|
||
|
* fFileBacked (immutable after instantiation)
|
||
|
* strWalletFile (immutable after instantiation)
|
||
|
*/
|
||
|
mutable CCriticalSection cs_wallet;
|
||
|
|
||
|
bool fFileBacked;
|
||
|
std::string strWalletFile;
|
||
|
|
||
|
std::set<int64_t> setKeyPool;
|
||
|
std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
|
||
|
|
||
|
typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
|
||
|
MasterKeyMap mapMasterKeys;
|
||
|
unsigned int nMasterKeyMaxID;
|
||
|
|
||
|
CWallet()
|
||
|
{
|
||
|
SetNull();
|
||
|
}
|
||
|
|
||
|
CWallet(std::string strWalletFileIn)
|
||
|
{
|
||
|
SetNull();
|
||
|
|
||
|
strWalletFile = strWalletFileIn;
|
||
|
fFileBacked = true;
|
||
|
}
|
||
|
|
||
|
~CWallet()
|
||
|
{
|
||
|
delete pwalletdbEncryption;
|
||
|
pwalletdbEncryption = NULL;
|
||
|
}
|
||
|
|
||
|
void SetNull()
|
||
|
{
|
||
|
nWalletVersion = FEATURE_BASE;
|
||
|
nWalletMaxVersion = FEATURE_BASE;
|
||
|
fFileBacked = false;
|
||
|
nMasterKeyMaxID = 0;
|
||
|
pwalletdbEncryption = NULL;
|
||
|
nOrderPosNext = 0;
|
||
|
nNextResend = 0;
|
||
|
nLastResend = 0;
|
||
|
nTimeFirstKey = 0;
|
||
|
}
|
||
|
|
||
|
std::map<uint256, CWalletTx> mapWallet;
|
||
|
|
||
|
int64_t nOrderPosNext;
|
||
|
std::map<uint256, int> mapRequestCount;
|
||
|
|
||
|
std::map<CTxDestination, CAddressBookData> mapAddressBook;
|
||
|
|
||
|
CPubKey vchDefaultKey;
|
||
|
|
||
|
std::set<COutPoint> setLockedCoins;
|
||
|
|
||
|
int64_t nTimeFirstKey;
|
||
|
|
||
|
const CWalletTx* GetWalletTx(const uint256& hash) const;
|
||
|
|
||
|
//! check whether we are allowed to upgrade (or already support) to the named feature
|
||
|
bool CanSupportFeature(enum WalletFeature wf) { AssertLockHeld(cs_wallet); return nWalletMaxVersion >= wf; }
|
||
|
|
||
|
void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl = NULL) const;
|
||
|
bool SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, CAmount& nValueRet) const;
|
||
|
|
||
|
bool IsSpent(const uint256& hash, unsigned int n) const;
|
||
|
|
||
|
bool IsLockedCoin(uint256 hash, unsigned int n) const;
|
||
12 years ago
|
void LockCoin(COutPoint& output);
|
||
|
void UnlockCoin(COutPoint& output);
|
||
|
void UnlockAllCoins();
|
||
|
void ListLockedCoins(std::vector<COutPoint>& vOutpts);
|
||
13 years ago
|
|
||
10 years ago
|
/**
|
||
|
* keystore implementation
|
||
|
* Generate a new key
|
||
|
*/
|
||
13 years ago
|
CPubKey GenerateNewKey();
|
||
10 years ago
|
//! Adds a key to the store, and saves it to disk.
|
||
12 years ago
|
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
|
||
10 years ago
|
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
|
||
12 years ago
|
bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
|
||
10 years ago
|
//! Load metadata (used by LoadWallet)
|
||
12 years ago
|
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
|
||
13 years ago
|
|
||
11 years ago
|
bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
|
||
13 years ago
|
|
||
10 years ago
|
//! Adds an encrypted key to the store, and saves it to disk.
|
||
12 years ago
|
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
|
||
10 years ago
|
//! Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
|
||
12 years ago
|
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
|
||
13 years ago
|
bool AddCScript(const CScript& redeemScript);
|
||
11 years ago
|
bool LoadCScript(const CScript& redeemScript);
|
||
14 years ago
|
|
||
10 years ago
|
//! Adds a destination data tuple to the store, and saves it to disk
|
||
11 years ago
|
bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value);
|
||
10 years ago
|
//! Erases a destination data tuple in the store and on disk
|
||
11 years ago
|
bool EraseDestData(const CTxDestination &dest, const std::string &key);
|
||
10 years ago
|
//! Adds a destination data tuple to the store, without saving it to disk
|
||
11 years ago
|
bool LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value);
|
||
10 years ago
|
//! Look up a destination data tuple in the store, return true if found false otherwise
|
||
11 years ago
|
bool GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const;
|
||
|
|
||
10 years ago
|
//! Adds a watch-only address to the store, and saves it to disk.
|
||
11 years ago
|
bool AddWatchOnly(const CScript &dest);
|
||
10 years ago
|
bool RemoveWatchOnly(const CScript &dest);
|
||
10 years ago
|
//! Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
|
||
11 years ago
|
bool LoadWatchOnly(const CScript &dest);
|
||
12 years ago
|
|
||
13 years ago
|
bool Unlock(const SecureString& strWalletPassphrase);
|
||
|
bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
|
||
|
bool EncryptWallet(const SecureString& strWalletPassphrase);
|
||
14 years ago
|
|
||
12 years ago
|
void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
|
||
12 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Increment the next transaction order id
|
||
|
* @return next transaction order id
|
||
12 years ago
|
*/
|
||
12 years ago
|
int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
|
||
12 years ago
|
|
||
13 years ago
|
typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
|
||
12 years ago
|
typedef std::multimap<int64_t, TxPair > TxItems;
|
||
12 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Get the wallet's activity log
|
||
|
* @return multimap of ordered transactions and accounting entries
|
||
|
* @warning Returned pointers are *only* valid within the scope of passed acentries
|
||
12 years ago
|
*/
|
||
|
TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
|
||
13 years ago
|
|
||
14 years ago
|
void MarkDirty();
|
||
10 years ago
|
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);
|
||
11 years ago
|
void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
|
||
|
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate);
|
||
11 years ago
|
void EraseFromWallet(const uint256 &hash);
|
||
14 years ago
|
int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
|
||
|
void ReacceptWalletTransactions();
|
||
|
void ResendWalletTransactions();
|
||
11 years ago
|
CAmount GetBalance() const;
|
||
|
CAmount GetUnconfirmedBalance() const;
|
||
|
CAmount GetImmatureBalance() const;
|
||
|
CAmount GetWatchOnlyBalance() const;
|
||
|
CAmount GetUnconfirmedWatchOnlyBalance() const;
|
||
|
CAmount GetImmatureWatchOnlyBalance() const;
|
||
11 years ago
|
bool CreateTransaction(const std::vector<CRecipient>& vecSend,
|
||
|
CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRet, int& nChangePosRet, std::string& strFailReason, const CCoinControl *coinControl = NULL);
|
||
14 years ago
|
bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
|
||
|
|
||
11 years ago
|
static CFeeRate minTxFee;
|
||
11 years ago
|
static CAmount GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool);
|
||
11 years ago
|
|
||
13 years ago
|
bool NewKeyPool();
|
||
12 years ago
|
bool TopUpKeyPool(unsigned int kpSize = 0);
|
||
12 years ago
|
void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
|
||
|
void KeepKey(int64_t nIndex);
|
||
|
void ReturnKey(int64_t nIndex);
|
||
11 years ago
|
bool GetKeyFromPool(CPubKey &key);
|
||
12 years ago
|
int64_t GetOldestKeyPoolTime();
|
||
12 years ago
|
void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
|
||
14 years ago
|
|
||
12 years ago
|
std::set< std::set<CTxDestination> > GetAddressGroupings();
|
||
11 years ago
|
std::map<CTxDestination, CAmount> GetAddressBalances();
|
||
13 years ago
|
|
||
12 years ago
|
std::set<CTxDestination> GetAccountAddresses(std::string strAccount) const;
|
||
|
|
||
12 years ago
|
isminetype IsMine(const CTxIn& txin) const;
|
||
11 years ago
|
CAmount GetDebit(const CTxIn& txin, const isminefilter& filter) const;
|
||
12 years ago
|
isminetype IsMine(const CTxOut& txout) const
|
||
14 years ago
|
{
|
||
|
return ::IsMine(*this, txout.scriptPubKey);
|
||
|
}
|
||
11 years ago
|
CAmount GetCredit(const CTxOut& txout, const isminefilter& filter) const
|
||
14 years ago
|
{
|
||
|
if (!MoneyRange(txout.nValue))
|
||
10 years ago
|
throw std::runtime_error("CWallet::GetCredit(): value out of range");
|
||
11 years ago
|
return ((IsMine(txout) & filter) ? txout.nValue : 0);
|
||
10 years ago
|
}
|
||
|
bool IsChange(const CTxOut& txout) const;
|
||
|
CAmount GetChange(const CTxOut& txout) const
|
||
14 years ago
|
{
|
||
10 years ago
|
if (!MoneyRange(txout.nValue))
|
||
|
throw std::runtime_error("CWallet::GetChange(): value out of range");
|
||
|
return (IsChange(txout) ? txout.nValue : 0);
|
||
14 years ago
|
}
|
||
10 years ago
|
bool IsMine(const CTransaction& tx) const
|
||
14 years ago
|
{
|
||
10 years ago
|
BOOST_FOREACH(const CTxOut& txout, tx.vout)
|
||
|
if (IsMine(txout))
|
||
|
return true;
|
||
|
return false;
|
||
14 years ago
|
}
|
||
10 years ago
|
/** should probably be renamed to IsRelevantToMe */
|
||
|
bool IsFromMe(const CTransaction& tx) const
|
||
14 years ago
|
{
|
||
10 years ago
|
return (GetDebit(tx, ISMINE_ALL) > 0);
|
||
14 years ago
|
}
|
||
10 years ago
|
CAmount GetDebit(const CTransaction& tx, const isminefilter& filter) const
|
||
14 years ago
|
{
|
||
10 years ago
|
CAmount nDebit = 0;
|
||
|
BOOST_FOREACH(const CTxIn& txin, tx.vin)
|
||
|
{
|
||
|
nDebit += GetDebit(txin, filter);
|
||
|
if (!MoneyRange(nDebit))
|
||
|
throw std::runtime_error("CWallet::GetDebit(): value out of range");
|
||
|
}
|
||
|
return nDebit;
|
||
14 years ago
|
}
|
||
10 years ago
|
CAmount GetCredit(const CTransaction& tx, const isminefilter& filter) const
|
||
14 years ago
|
{
|
||
10 years ago
|
CAmount nCredit = 0;
|
||
|
BOOST_FOREACH(const CTxOut& txout, tx.vout)
|
||
|
{
|
||
|
nCredit += GetCredit(txout, filter);
|
||
|
if (!MoneyRange(nCredit))
|
||
|
throw std::runtime_error("CWallet::GetCredit(): value out of range");
|
||
|
}
|
||
|
return nCredit;
|
||
14 years ago
|
}
|
||
10 years ago
|
CAmount GetChange(const CTransaction& tx) const
|
||
|
{
|
||
|
CAmount nChange = 0;
|
||
|
BOOST_FOREACH(const CTxOut& txout, tx.vout)
|
||
14 years ago
|
{
|
||
10 years ago
|
nChange += GetChange(txout);
|
||
|
if (!MoneyRange(nChange))
|
||
|
throw std::runtime_error("CWallet::GetChange(): value out of range");
|
||
14 years ago
|
}
|
||
10 years ago
|
return nChange;
|
||
|
}
|
||
|
void SetBestChain(const CBlockLocator& loc);
|
||
14 years ago
|
|
||
10 years ago
|
DBErrors LoadWallet(bool& fFirstRunRet);
|
||
|
DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
|
||
14 years ago
|
|
||
10 years ago
|
bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& purpose);
|
||
13 years ago
|
|
||
10 years ago
|
bool DelAddressBook(const CTxDestination& address);
|
||
13 years ago
|
|
||
10 years ago
|
void UpdatedTransaction(const uint256 &hashTx);
|
||
14 years ago
|
|
||
10 years ago
|
void Inventory(const uint256 &hash)
|
||
14 years ago
|
{
|
||
10 years ago
|
{
|
||
|
LOCK(cs_wallet);
|
||
|
std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
|
||
|
if (mi != mapRequestCount.end())
|
||
|
(*mi).second++;
|
||
|
}
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
unsigned int GetKeyPoolSize()
|
||
14 years ago
|
{
|
||
10 years ago
|
AssertLockHeld(cs_wallet); // setKeyPool
|
||
|
return setKeyPool.size();
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
bool SetDefaultKey(const CPubKey &vchPubKey);
|
||
14 years ago
|
|
||
10 years ago
|
//! signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
|
||
|
bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
|
||
14 years ago
|
|
||
10 years ago
|
//! change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
|
||
|
bool SetMaxVersion(int nVersion);
|
||
14 years ago
|
|
||
10 years ago
|
//! get the current wallet format (the oldest client version guaranteed to understand this wallet)
|
||
|
int GetVersion() { LOCK(cs_wallet); return nWalletVersion; }
|
||
14 years ago
|
|
||
10 years ago
|
//! Get wallet transactions that conflict with given transaction (spend same outputs)
|
||
|
std::set<uint256> GetConflicts(const uint256& txid) const;
|
||
14 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Address book entry changed.
|
||
|
* @note called with lock cs_wallet held.
|
||
|
*/
|
||
|
boost::signals2::signal<void (CWallet *wallet, const CTxDestination
|
||
|
&address, const std::string &label, bool isMine,
|
||
|
const std::string &purpose,
|
||
|
ChangeType status)> NotifyAddressBookChanged;
|
||
14 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Wallet transaction added, removed or updated.
|
||
|
* @note called with lock cs_wallet held.
|
||
|
*/
|
||
|
boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx,
|
||
|
ChangeType status)> NotifyTransactionChanged;
|
||
14 years ago
|
|
||
10 years ago
|
/** Show progress e.g. for rescan */
|
||
|
boost::signals2::signal<void (const std::string &title, int nProgress)> ShowProgress;
|
||
11 years ago
|
|
||
10 years ago
|
/** Watch-only address added */
|
||
|
boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
|
||
14 years ago
|
};
|
||
|
|
||
10 years ago
|
/** A key allocated from the key pool. */
|
||
|
class CReserveKey
|
||
13 years ago
|
{
|
||
10 years ago
|
protected:
|
||
|
CWallet* pwallet;
|
||
|
int64_t nIndex;
|
||
|
CPubKey vchPubKey;
|
||
13 years ago
|
public:
|
||
10 years ago
|
CReserveKey(CWallet* pwalletIn)
|
||
13 years ago
|
{
|
||
10 years ago
|
nIndex = -1;
|
||
|
pwallet = pwalletIn;
|
||
13 years ago
|
}
|
||
|
|
||
10 years ago
|
~CReserveKey()
|
||
|
{
|
||
|
ReturnKey();
|
||
10 years ago
|
}
|
||
14 years ago
|
|
||
10 years ago
|
void ReturnKey();
|
||
|
bool GetReservedKey(CPubKey &pubkey);
|
||
|
void KeepKey();
|
||
|
};
|
||
14 years ago
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Account information.
|
||
13 years ago
|
* Stored in wallet with key "acc"+string account name.
|
||
|
*/
|
||
14 years ago
|
class CAccount
|
||
|
{
|
||
|
public:
|
||
13 years ago
|
CPubKey vchPubKey;
|
||
14 years ago
|
|
||
|
CAccount()
|
||
|
{
|
||
|
SetNull();
|
||
|
}
|
||
|
|
||
|
void SetNull()
|
||
|
{
|
||
13 years ago
|
vchPubKey = CPubKey();
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
ADD_SERIALIZE_METHODS;
|
||
10 years ago
|
|
||
10 years ago
|
template <typename Stream, typename Operation>
|
||
10 years ago
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||
14 years ago
|
if (!(nType & SER_GETHASH))
|
||
|
READWRITE(nVersion);
|
||
|
READWRITE(vchPubKey);
|
||
10 years ago
|
}
|
||
14 years ago
|
};
|
||
|
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Internal transfers.
|
||
13 years ago
|
* Database key is acentry<account><counter>.
|
||
|
*/
|
||
14 years ago
|
class CAccountingEntry
|
||
|
{
|
||
|
public:
|
||
|
std::string strAccount;
|
||
11 years ago
|
CAmount nCreditDebit;
|
||
12 years ago
|
int64_t nTime;
|
||
14 years ago
|
std::string strOtherAccount;
|
||
|
std::string strComment;
|
||
13 years ago
|
mapValue_t mapValue;
|
||
10 years ago
|
int64_t nOrderPos; //! position in ordered transaction list
|
||
12 years ago
|
uint64_t nEntryNo;
|
||
14 years ago
|
|
||
|
CAccountingEntry()
|
||
|
{
|
||
|
SetNull();
|
||
|
}
|
||
|
|
||
|
void SetNull()
|
||
|
{
|
||
|
nCreditDebit = 0;
|
||
|
nTime = 0;
|
||
|
strAccount.clear();
|
||
|
strOtherAccount.clear();
|
||
|
strComment.clear();
|
||
13 years ago
|
nOrderPos = -1;
|
||
10 years ago
|
nEntryNo = 0;
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
ADD_SERIALIZE_METHODS;
|
||
10 years ago
|
|
||
10 years ago
|
template <typename Stream, typename Operation>
|
||
10 years ago
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
||
14 years ago
|
if (!(nType & SER_GETHASH))
|
||
|
READWRITE(nVersion);
|
||
10 years ago
|
//! Note: strAccount is serialized as part of the key, not here.
|
||
14 years ago
|
READWRITE(nCreditDebit);
|
||
|
READWRITE(nTime);
|
||
10 years ago
|
READWRITE(LIMITED_STRING(strOtherAccount, 65536));
|
||
13 years ago
|
|
||
10 years ago
|
if (!ser_action.ForRead())
|
||
13 years ago
|
{
|
||
10 years ago
|
WriteOrderPos(nOrderPos, mapValue);
|
||
13 years ago
|
|
||
|
if (!(mapValue.empty() && _ssExtra.empty()))
|
||
|
{
|
||
|
CDataStream ss(nType, nVersion);
|
||
|
ss.insert(ss.begin(), '\0');
|
||
|
ss << mapValue;
|
||
|
ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
|
||
10 years ago
|
strComment.append(ss.str());
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
READWRITE(LIMITED_STRING(strComment, 65536));
|
||
13 years ago
|
|
||
|
size_t nSepPos = strComment.find("\0", 0, 1);
|
||
10 years ago
|
if (ser_action.ForRead())
|
||
13 years ago
|
{
|
||
10 years ago
|
mapValue.clear();
|
||
13 years ago
|
if (std::string::npos != nSepPos)
|
||
|
{
|
||
|
CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
|
||
10 years ago
|
ss >> mapValue;
|
||
|
_ssExtra = std::vector<char>(ss.begin(), ss.end());
|
||
13 years ago
|
}
|
||
10 years ago
|
ReadOrderPos(nOrderPos, mapValue);
|
||
13 years ago
|
}
|
||
|
if (std::string::npos != nSepPos)
|
||
10 years ago
|
strComment.erase(nSepPos);
|
||
13 years ago
|
|
||
10 years ago
|
mapValue.erase("n");
|
||
10 years ago
|
}
|
||
13 years ago
|
|
||
|
private:
|
||
|
std::vector<char> _ssExtra;
|
||
14 years ago
|
};
|
||
|
|
||
10 years ago
|
#endif // BITCOIN_WALLET_H
|