5 #ifndef BITCOIN_WALLET_H
6 #define BITCOIN_WALLET_H
69 vchPubKey = vchPubKeyIn;
93 typedef std::map<std::string, std::string>
StringMap;
103 bool SelectCoins(int64_t nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet,
const CCoinControl *coinControl = NULL)
const;
119 typedef std::multimap<COutPoint, uint256>
TxSpends;
122 void AddToSpends(
const uint256& wtxid);
124 void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>);
152 strWalletFile = strWalletFileIn;
161 pwalletdbEncryption = NULL;
186 void AvailableCoins(std::vector<COutput>& vCoins,
bool fOnlyConfirmed=
true,
const CCoinControl *coinControl = NULL)
const;
187 bool SelectCoinsMinConf(int64_t nTargetValue,
int nConfMine,
int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet)
const;
189 bool IsSpent(
const uint256& hash,
unsigned int n)
const;
191 bool IsLockedCoin(
uint256 hash,
unsigned int n)
const;
194 void UnlockAllCoins();
195 void ListLockedCoins(std::vector<COutPoint>& vOutpts);
201 bool AddKeyPubKey(
const CKey& key,
const CPubKey &pubkey);
207 bool LoadMinVersion(
int nVersion) {
AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion);
return true; }
210 bool AddCryptedKey(
const CPubKey &vchPubKey,
const std::vector<unsigned char> &vchCryptedSecret);
212 bool LoadCryptedKey(
const CPubKey &vchPubKey,
const std::vector<unsigned char> &vchCryptedSecret);
213 bool AddCScript(
const CScript& redeemScript);
214 bool LoadCScript(
const CScript& redeemScript);
217 bool AddDestData(
const CTxDestination &dest,
const std::string &key,
const std::string &value);
219 bool EraseDestData(
const CTxDestination &dest,
const std::string &key);
221 bool LoadDestData(
const CTxDestination &dest,
const std::string &key,
const std::string &value);
223 bool GetDestData(
const CTxDestination &dest,
const std::string &key, std::string *value)
const;
226 bool ChangeWalletPassphrase(
const SecureString& strOldWalletPassphrase,
const SecureString& strNewWalletPassphrase);
227 bool EncryptWallet(
const SecureString& strWalletPassphrase);
229 void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth)
const;
234 int64_t IncOrderPosNext(
CWalletDB *pwalletdb = NULL);
236 typedef std::pair<CWalletTx*, CAccountingEntry*>
TxPair;
237 typedef std::multimap<int64_t, TxPair >
TxItems;
243 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount =
"");
246 bool AddToWallet(
const CWalletTx& wtxIn,
bool fFromLoadWallet=
false);
249 void EraseFromWallet(
const uint256 &hash);
250 int ScanForWalletTransactions(
CBlockIndex* pindexStart,
bool fUpdate =
false);
251 void ReacceptWalletTransactions();
252 void ResendWalletTransactions();
253 int64_t GetBalance()
const;
254 int64_t GetUnconfirmedBalance()
const;
255 int64_t GetImmatureBalance()
const;
256 bool CreateTransaction(
const std::vector<std::pair<CScript, int64_t> >& vecSend,
258 bool CreateTransaction(
CScript scriptPubKey, int64_t nValue,
261 std::string SendMoney(
CScript scriptPubKey, int64_t nValue,
CWalletTx& wtxNew);
265 bool TopUpKeyPool(
unsigned int kpSize = 0);
266 int64_t AddReserveKey(
const CKeyPool& keypool);
267 void ReserveKeyFromKeyPool(int64_t& nIndex,
CKeyPool& keypool);
268 void KeepKey(int64_t nIndex);
269 void ReturnKey(int64_t nIndex);
270 bool GetKeyFromPool(
CPubKey &key);
271 int64_t GetOldestKeyPoolTime();
272 void GetAllReserveKeys(std::set<CKeyID>& setAddress)
const;
274 std::set< std::set<CTxDestination> > GetAddressGroupings();
275 std::map<CTxDestination, int64_t> GetAddressBalances();
277 std::set<CTxDestination> GetAccountAddresses(std::string strAccount)
const;
280 int64_t GetDebit(
const CTxIn& txin)
const;
288 throw std::runtime_error(
"CWallet::GetCredit() : value out of range");
291 bool IsChange(
const CTxOut& txout)
const;
295 throw std::runtime_error(
"CWallet::GetChange() : value out of range");
296 return (IsChange(txout) ? txout.
nValue : 0);
307 return (GetDebit(tx) > 0);
312 BOOST_FOREACH(
const CTxIn& txin, tx.
vin)
314 nDebit += GetDebit(txin);
316 throw std::runtime_error(
"CWallet::GetDebit() : value out of range");
325 nCredit += GetCredit(txout);
327 throw std::runtime_error(
"CWallet::GetCredit() : value out of range");
336 nChange += GetChange(txout);
338 throw std::runtime_error(
"CWallet::GetChange() : value out of range");
344 DBErrors LoadWallet(
bool& fFirstRunRet);
347 bool SetAddressBook(
const CTxDestination& address,
const std::string& strName,
const std::string& purpose);
351 void UpdatedTransaction(
const uint256 &hashTx);
357 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
358 if (mi != mapRequestCount.end())
366 return setKeyPool.size();
369 bool SetDefaultKey(
const CPubKey &vchPubKey);
375 bool SetMaxVersion(
int nVersion);
381 std::set<uint256> GetConflicts(
const uint256& txid)
const;
387 &address,
const std::string &label,
bool isMine,
388 const std::string &purpose,
394 boost::signals2::signal<void (
CWallet *wallet,
const uint256 &hashTx,
398 boost::signals2::signal<void (const std::string &title, int nProgress)>
ShowProgress;
421 bool GetReservedKey(
CPubKey &pubkey);
431 if (!mapValue.count(
"n"))
436 nOrderPos =
atoi64(mapValue[
"n"].c_str());
444 mapValue[
"n"] =
i64tostr(nOrderPos);
458 std::vector<std::pair<std::string, std::string> >
vOrderForm;
503 fTimeReceivedIsTxTime =
false;
507 strFromAccount.clear();
508 fDebitCached =
false;
509 fCreditCached =
false;
510 fImmatureCreditCached =
false;
511 fAvailableCreditCached =
false;
512 fChangeCached =
false;
515 nImmatureCreditCached = 0;
516 nAvailableCreditCached = 0;
523 CWalletTx* pthis = const_cast<CWalletTx*>(
this);
530 pthis->mapValue[
"fromaccount"] = pthis->strFromAccount;
535 pthis->mapValue[
"timesmart"] =
strprintf(
"%u", nTimeSmart);
539 std::vector<CMerkleTx> vUnused;
550 pthis->strFromAccount = pthis->mapValue[
"fromaccount"];
554 pthis->nTimeSmart = mapValue.count(
"timesmart") ? (
unsigned int)
atoi64(pthis->mapValue[
"timesmart"]) : 0;
557 pthis->mapValue.erase(
"fromaccount");
558 pthis->mapValue.erase(
"version");
559 pthis->mapValue.erase(
"spent");
560 pthis->mapValue.erase(
"n");
561 pthis->mapValue.erase(
"timesmart");
567 fCreditCached =
false;
568 fAvailableCreditCached =
false;
569 fDebitCached =
false;
570 fChangeCached =
false;
585 nDebitCached = pwallet->
GetDebit(*
this);
593 if (IsCoinBase() && GetBlocksToMaturity() > 0)
597 if (fUseCache && fCreditCached)
598 return nCreditCached;
599 nCreditCached = pwallet->
GetCredit(*
this);
600 fCreditCached =
true;
601 return nCreditCached;
606 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
608 if (fUseCache && fImmatureCreditCached)
609 return nImmatureCreditCached;
610 nImmatureCreditCached = pwallet->
GetCredit(*
this);
611 fImmatureCreditCached =
true;
612 return nImmatureCreditCached;
624 if (IsCoinBase() && GetBlocksToMaturity() > 0)
627 if (fUseCache && fAvailableCreditCached)
628 return nAvailableCreditCached;
632 for (
unsigned int i = 0; i < vout.size(); i++)
634 if (!pwallet->
IsSpent(hashTx, i))
636 const CTxOut &txout = vout[i];
639 throw std::runtime_error(
"CWalletTx::GetAvailableCredit() : value out of range");
643 nAvailableCreditCached = nCredit;
644 fAvailableCreditCached =
true;
652 return nChangeCached;
653 nChangeCached = pwallet->
GetChange(*
this);
654 fChangeCached =
true;
655 return nChangeCached;
658 void GetAmounts(std::list<std::pair<CTxDestination, int64_t> >& listReceived,
659 std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount)
const;
661 void GetAccountAmounts(
const std::string& strAccount, int64_t& nReceived,
662 int64_t& nSent, int64_t& nFee)
const;
666 return (GetDebit() > 0);
674 int nDepth = GetDepthInMainChain();
683 BOOST_FOREACH(
const CTxIn& txin, vin)
690 if (!pwallet->
IsMine(parentOut))
698 int64_t GetTxTime()
const;
699 int GetRequestCount()
const;
701 void RelayWalletTransaction();
703 std::set<uint256> GetConflicts()
const;
718 tx = txIn; i = iIn; nDepth = nDepthIn;
748 nTimeCreated = (nExpires ?
GetTime() : 0);
749 nTimeExpires = nExpires;
754 if (!(nType & SER_GETHASH))
788 if (!(nType & SER_GETHASH))
821 strOtherAccount.clear();
829 if (!(nType & SER_GETHASH))
840 if (!(mapValue.empty() && _ssExtra.empty()))
843 ss.insert(ss.begin(),
'\0');
845 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
846 me.strComment.append(ss.str());
852 size_t nSepPos = strComment.find(
"\0", 0, 1);
856 if (std::string::npos != nSepPos)
858 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
860 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
864 if (std::string::npos != nSepPos)
865 me.strComment.erase(nSepPos);
867 me.mapValue.erase(
"n");
871 std::vector<char> _ssExtra;
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
bool IsMine(const CTransaction &tx) const
std::set< int64_t > setKeyPool
void BindWallet(CWallet *pwalletIn)
unsigned int SerReadWrite(Stream &s, const T &obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
int64_t GetChange(const CTransaction &tx) const
bool IsMine(const CTxIn &txin) const
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
int64_t GetChange() const
COutput(const CWalletTx *txIn, int iIn, int nDepthIn)
int64_t GetCredit(const CTxOut &txout) const
std::map< CTxDestination, CAddressBookData > mapAddressBook
CCriticalSection cs_wallet
Main wallet lock.
bool fImmatureCreditCached
std::string strFromAccount
WalletFeature
(client) version numbers for particular wallet features
CWalletDB * pwalletdbEncryption
std::multimap< COutPoint, uint256 > TxSpends
bool IsFromMe(const CTransaction &tx) const
int64_t nImmatureCreditCached
int64_t GetCredit(const CTransaction &tx) const
std::string i64tostr(int64_t n)
bool MoneyRange(int64_t nValue)
int64_t GetChange(const CTxOut &txout) const
#define IMPLEMENT_SERIALIZE(statements)
unsigned int GetKeyPoolSize()
static void ReadOrderPos(int64_t &nOrderPos, mapValue_t &mapValue)
std::map< unsigned int, CMasterKey > MasterKeyMap
Double ended buffer combining vector and stream-like interfaces.
CWalletKey(int64_t nExpires=0)
DBErrors
Error statuses for the wallet database.
Keystore which keeps the private keys encrypted.
string FormatMoney(int64_t n, bool fPlus)
bool IsMine(const CTxOut &txout) const
bool CanSupportFeature(enum WalletFeature wf)
bool fAvailableCreditCached
std::multimap< int64_t, TxPair > TxItems
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
boost::signals2::signal< void(CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged
Wallet transaction added, removed or updated.
#define AssertLockHeld(cs)
bool IsSpent(const uint256 &hash, unsigned int n) const
unsigned int nMasterKeyMaxID
ChangeType
General change type (added, updated, removed).
An input of a transaction.
const CWalletTx * GetWalletTx(const uint256 &hash) const
std::vector< CTxOut > vout
int64_t atoi64(const char *psz)
std::string ToString() const
static const int nHighTransactionFeeWarning
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
An encapsulated public key.
void Inventory(const uint256 &hash)
std::set< COutPoint > setLockedCoins
std::map< std::string, std::string > StringMap
bool LoadKey(const CKey &key, const CPubKey &pubkey)
bool bSpendZeroConfChange
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
An output of a transaction.
std::map< uint256, int > mapRequestCount
An outpoint - a combination of a transaction hash and an index n into its vout.
int64_t GetDebit(const CTxIn &txin) const
unsigned int fTimeReceivedIsTxTime
bool LoadMinVersion(int nVersion)
Access to the wallet database (wallet.dat)
A transaction with a bunch of additional info that only the owner cares about.
std::string strWalletFile
CWalletTx(const CWallet *pwalletIn, const CMerkleTx &txIn)
static void WriteOrderPos(const int64_t &nOrderPos, mapValue_t &mapValue)
void Init(const CWallet *pwalletIn)
static const int64_t COIN
CWallet(std::string strWalletFileIn)
boost::signals2::signal< void(CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, const std::string &purpose, ChangeType status)> NotifyAddressBookChanged
Address book entry changed.
MasterKeyMap mapMasterKeys
A key allocated from the key pool.
The block chain is a tree shaped structure starting with the genesis block at the root...
Serialized script, used inside transaction inputs and outputs.
std::string ToString() const
CWalletTx(const CWallet *pwalletIn)
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Private key that includes an expiration date in case it never gets used.
#define LIMITED_STRING(obj, n)
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
CKeyPool(const CPubKey &vchPubKeyIn)
std::map< CKeyID, CKeyMetadata > mapKeyMetadata
CReserveKey(CWallet *pwalletIn)
std::map< uint256, CWalletTx > mapWallet
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
CWalletTx(const CWallet *pwalletIn, const CTransaction &txIn)
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
unsigned int nTimeReceived
An encapsulated private key.
int64_t GetAvailableCredit(bool fUseCache=true) const
The basic transaction that is broadcasted on the network and contained in blocks. ...
std::pair< CWalletTx *, CAccountingEntry * > TxPair
int64_t GetDebit(const CTransaction &tx) const
int64_t nAvailableCreditCached
std::string strOtherAccount
int64_t GetCredit(bool fUseCache=true) const
static const int64_t DEFAULT_TRANSACTION_FEE
int64_t GetImmatureCredit(bool fUseCache=true) const
A transaction with a merkle branch linking it to the block chain.
std::map< std::string, std::string > mapValue_t
std::vector< std::pair< std::string, std::string > > vOrderForm