1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_WALLET_H
6 #define BITCOIN_WALLET_H
17 #include "ui_interface.h"
21 extern unsigned int nStakeMaxAge;
22 extern bool fWalletUnlockMintOnly;
23 extern bool fConfChange;
24 class CAccountingEntry;
30 // Set of selected transactions
31 typedef std::set<std::pair<const CWalletTx*,unsigned int> > CoinsSet;
33 /** (client) version numbers for particular wallet features */
36 FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
38 FEATURE_WALLETCRYPT = 40000, // wallet encryption
39 FEATURE_COMPRPUBKEY = 60000, // compressed public keys
40 FEATURE_LATEST = 60000
43 /** A key pool entry */
55 CKeyPool(const CPubKey& vchPubKeyIn)
58 vchPubKey = vchPubKeyIn;
63 if (!(nType & SER_GETHASH))
70 /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
71 * and provides the ability to create new transactions.
73 class CWallet : public CCryptoKeyStore
76 bool SelectCoins(int64_t nTargetValue, unsigned int nSpendTime, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet, const CCoinControl *coinControl=NULL) const;
78 CWalletDB *pwalletdbEncryption, *pwalletdbDecryption;
80 // the current wallet version: clients below this version are not able to load the wallet
83 // the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
84 int nWalletMaxVersion;
86 // stake mining statistics
87 uint64_t nKernelsTried;
88 uint64_t nCoinDaysTried;
91 mutable CCriticalSection cs_wallet;
94 std::string strWalletFile;
96 std::set<int64_t> setKeyPool;
97 std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
99 typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
100 MasterKeyMap mapMasterKeys;
101 unsigned int nMasterKeyMaxID;
105 nWalletVersion = FEATURE_BASE;
106 nWalletMaxVersion = FEATURE_BASE;
109 pwalletdbEncryption = NULL;
110 pwalletdbDecryption = NULL;
115 CWallet(std::string strWalletFileIn)
117 nWalletVersion = FEATURE_BASE;
118 nWalletMaxVersion = FEATURE_BASE;
119 strWalletFile = strWalletFileIn;
122 pwalletdbEncryption = NULL;
123 pwalletdbDecryption = NULL;
129 std::map<uint256, CWalletTx> mapWallet;
130 std::vector<uint256> vMintingWalletUpdated;
131 int64_t nOrderPosNext;
132 std::map<uint256, int> mapRequestCount;
134 std::map<CTxDestination, std::string> mapAddressBook;
136 CPubKey vchDefaultKey;
137 int64_t nTimeFirstKey;
139 // check whether we are allowed to upgrade (or already support) to the named feature
140 bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
142 void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf, int64_t nMinValue, int64_t nMaxValue) const;
143 void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
144 bool SelectCoinsMinConf(int64_t nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const;
146 // Simple select (without randomization)
147 bool SelectCoinsSimple(int64_t nTargetValue, int64_t nMinValue, int64_t nMaxValue, unsigned int nSpendTime, int nMinConf, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const;
149 // keystore implementation
150 // Generate a new key
151 CPubKey GenerateNewKey();
152 // Adds a key to the store, and saves it to disk.
153 bool AddKey(const CKey& key);
154 // Adds a key to the store, without saving it to disk (used by LoadWallet)
155 bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
156 // Load metadata (used by LoadWallet)
157 bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
159 bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
161 // Adds an encrypted key to the store, and saves it to disk.
162 bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
163 // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
164 bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
165 bool AddCScript(const CScript& redeemScript);
166 bool LoadCScript(const CScript& redeemScript);
168 // Adds a watch-only address to the store, and saves it to disk.
169 bool AddWatchOnly(const CScript &dest);
170 bool RemoveWatchOnly(const CScript &dest);
171 // Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
172 bool LoadWatchOnly(const CScript &dest);
174 bool Unlock(const SecureString& strWalletPassphrase);
175 bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
176 bool EncryptWallet(const SecureString& strWalletPassphrase);
177 bool DecryptWallet(const SecureString& strWalletPassphrase);
179 void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
182 /** Increment the next transaction order id
183 @return next transaction order id
185 int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
187 typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
188 typedef std::multimap<int64_t, TxPair > TxItems;
190 /** Get the wallet's activity log
191 @return multimap of ordered transactions and accounting entries
192 @warning Returned pointers are *only* valid within the scope of passed acentries
194 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
197 bool AddToWallet(const CWalletTx& wtxIn);
198 bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
199 bool EraseFromWallet(uint256 hash);
201 void WalletUpdateSpent(const CTransaction& prevout, bool fBlock = false);
202 int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
203 int ScanForWalletTransaction(const uint256& hashTx);
204 void ReacceptWalletTransactions();
205 void ResendWalletTransactions();
206 int64_t GetBalance() const;
207 int64_t GetWatchOnlyBalance() const;
208 int64_t GetUnconfirmedBalance() const;
209 int64_t GetUnconfirmedWatchOnlyBalance() const;
210 int64_t GetImmatureBalance() const;
211 int64_t GetImmatureWatchOnlyBalance() const;
212 int64_t GetStake() const;
213 int64_t GetNewMint() const;
214 int64_t GetWatchOnlyStake() const;
215 int64_t GetWatchOnlyNewMint() const;
216 bool CreateTransaction(const std::vector<std::pair<CScript, int64_t> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
217 bool CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
218 bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
220 void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
221 bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
222 bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
224 std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
225 std::string SendMoneyToDestination(const CTxDestination &address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
227 bool NewKeyPool(unsigned int nSize = 0);
228 bool TopUpKeyPool(unsigned int nSize = 0);
229 int64_t AddReserveKey(const CKeyPool& keypool);
230 void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
231 void KeepKey(int64_t nIndex);
232 void ReturnKey(int64_t nIndex);
233 bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
234 int64_t GetOldestKeyPoolTime();
235 void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
237 std::set< std::set<CTxDestination> > GetAddressGroupings();
238 std::map<CTxDestination, int64_t> GetAddressBalances();
240 isminetype IsMine(const CTxIn& txin) const;
241 int64_t GetDebit(const CTxIn& txin, const isminefilter& filter) const;
242 isminetype IsMine(const CTxOut& txout) const
244 return ::IsMine(*this, txout.scriptPubKey);
246 int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const
248 if (!MoneyRange(txout.nValue))
249 throw std::runtime_error("CWallet::GetCredit() : value out of range");
250 return (IsMine(txout) & filter ? txout.nValue : 0);
252 bool IsChange(const CTxOut& txout) const;
253 int64_t GetChange(const CTxOut& txout) const
255 if (!MoneyRange(txout.nValue))
256 throw std::runtime_error("CWallet::GetChange() : value out of range");
257 return (IsChange(txout) ? txout.nValue : 0);
259 bool IsMine(const CTransaction& tx) const
261 BOOST_FOREACH(const CTxOut& txout, tx.vout)
262 if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
266 bool IsFromMe(const CTransaction& tx) const
268 return (GetDebit(tx, MINE_ALL) > 0);
270 int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const
273 BOOST_FOREACH(const CTxIn& txin, tx.vin)
275 nDebit += GetDebit(txin, filter);
276 if (!MoneyRange(nDebit))
277 throw std::runtime_error("CWallet::GetDebit() : value out of range");
281 int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const
284 BOOST_FOREACH(const CTxOut& txout, tx.vout)
286 nCredit += GetCredit(txout, filter);
287 if (!MoneyRange(nCredit))
288 throw std::runtime_error("CWallet::GetCredit() : value out of range");
292 int64_t GetChange(const CTransaction& tx) const
295 BOOST_FOREACH(const CTxOut& txout, tx.vout)
297 nChange += GetChange(txout);
298 if (!MoneyRange(nChange))
299 throw std::runtime_error("CWallet::GetChange() : value out of range");
303 void SetBestChain(const CBlockLocator& loc);
305 DBErrors LoadWallet(bool& fFirstRunRet);
307 DBErrors ZapWalletTx();
309 bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
311 bool DelAddressBookName(const CTxDestination& address);
313 void UpdatedTransaction(const uint256 &hashTx);
315 void PrintWallet(const CBlock& block);
317 void Inventory(const uint256 &hash)
321 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
322 if (mi != mapRequestCount.end())
327 unsigned int GetKeyPoolSize()
329 return (unsigned int)(setKeyPool.size());
332 bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
334 bool SetDefaultKey(const CPubKey &vchPubKey);
336 // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
337 bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
339 // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
340 bool SetMaxVersion(int nVersion);
342 // get the current wallet format (the oldest client version guaranteed to understand this wallet)
343 int GetVersion() { return nWalletVersion; }
345 void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
346 void DisableTransaction(const CTransaction &tx);
348 /** Address book entry changed.
349 * @note called with lock cs_wallet held.
351 boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
353 /** Wallet transaction added, removed or updated.
354 * @note called with lock cs_wallet held.
356 boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
358 /** Watch-only address added */
359 boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
362 /** A key allocated from the key pool. */
370 CReserveKey(CWallet* pwalletIn)
383 CPubKey GetReservedKey();
388 typedef std::map<std::string, std::string> mapValue_t;
391 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
393 if (!mapValue.count("n"))
395 nOrderPos = -1; // TODO: calculate elsewhere
398 nOrderPos = atoi64(mapValue["n"].c_str());
402 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
406 mapValue["n"] = i64tostr(nOrderPos);
410 /** A transaction with a bunch of additional info that only the owner cares about.
411 * It includes any unrecorded transactions needed to link it back to the block chain.
413 class CWalletTx : public CMerkleTx
416 const CWallet* pwallet;
419 std::vector<CMerkleTx> vtxPrev;
421 std::vector<std::pair<std::string, std::string> > vOrderForm;
422 unsigned int fTimeReceivedIsTxTime;
423 unsigned int nTimeReceived; // time received by this node
424 unsigned int nTimeSmart;
426 std::string strFromAccount;
427 std::vector<char> vfSpent; // which outputs are already spent
428 int64_t nOrderPos; // position in ordered transaction list
431 mutable bool fDebitCached;
432 mutable bool fWatchDebitCached;
433 mutable bool fCreditCached;
434 mutable bool fWatchCreditCached;
435 mutable bool fAvailableCreditCached;
436 mutable bool fImmatureCreditCached;
437 mutable bool fImmatureWatchCreditCached;
438 mutable bool fAvailableWatchCreditCached;
439 mutable bool fChangeCached;
440 mutable int64_t nDebitCached;
441 mutable int64_t nWatchDebitCached;
442 mutable int64_t nCreditCached;
443 mutable int64_t nWatchCreditCached;
444 mutable int64_t nAvailableCreditCached;
445 mutable int64_t nImmatureCreditCached;
446 mutable int64_t nImmatureWatchCreditCached;
447 mutable int64_t nAvailableWatchCreditCached;
448 mutable int64_t nChangeCached;
455 CWalletTx(const CWallet* pwalletIn)
460 CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
465 CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
470 void Init(const CWallet* pwalletIn)
476 fTimeReceivedIsTxTime = false;
480 strFromAccount.clear();
482 fDebitCached = false;
483 fWatchDebitCached = false;
484 fCreditCached = false;
485 fWatchCreditCached = false;
486 fAvailableCreditCached = false;
487 fAvailableWatchCreditCached = false;
488 fImmatureCreditCached = false;
489 fImmatureWatchCreditCached = false;
490 fChangeCached = false;
492 nWatchDebitCached = 0;
494 nWatchCreditCached = 0;
495 nAvailableCreditCached = 0;
496 nAvailableWatchCreditCached = 0;
497 nImmatureCreditCached = 0;
498 nImmatureWatchCreditCached = 0;
505 CWalletTx* pthis = const_cast<CWalletTx*>(this);
512 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
515 BOOST_FOREACH(char f, vfSpent)
517 str += (f ? '1' : '0');
521 pthis->mapValue["spent"] = str;
523 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
526 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
529 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
532 READWRITE(vOrderForm);
533 READWRITE(fTimeReceivedIsTxTime);
534 READWRITE(nTimeReceived);
540 pthis->strFromAccount = pthis->mapValue["fromaccount"];
542 if (mapValue.count("spent"))
543 BOOST_FOREACH(char c, pthis->mapValue["spent"])
544 pthis->vfSpent.push_back(c != '0');
546 pthis->vfSpent.assign(vout.size(), fSpent);
548 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
550 pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
553 pthis->mapValue.erase("fromaccount");
554 pthis->mapValue.erase("version");
555 pthis->mapValue.erase("spent");
556 pthis->mapValue.erase("n");
557 pthis->mapValue.erase("timesmart");
560 // marks certain txout's as spent
561 // returns true if any update took place
562 bool UpdateSpent(const std::vector<char>& vfNewSpent)
564 bool fReturn = false;
565 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
567 if (i == vfSpent.size())
570 if (vfNewSpent[i] && !vfSpent[i])
574 fAvailableCreditCached = fAvailableWatchCreditCached = false;
580 // make sure balances are recalculated
583 fCreditCached = false;
584 fAvailableCreditCached = fAvailableWatchCreditCached = false;
585 fDebitCached = fWatchDebitCached = false;
586 fChangeCached = false;
589 void BindWallet(CWallet *pwalletIn)
595 void MarkSpent(unsigned int nOut)
597 if (nOut >= vout.size())
598 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
599 vfSpent.resize(vout.size());
602 vfSpent[nOut] = true;
603 fAvailableCreditCached = fAvailableWatchCreditCached = false;
607 void MarkUnspent(unsigned int nOut)
609 if (nOut >= vout.size())
610 throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
611 vfSpent.resize(vout.size());
614 vfSpent[nOut] = false;
615 fAvailableCreditCached = fAvailableWatchCreditCached = false;
619 bool IsSpent(unsigned int nOut) const
621 if (nOut >= vout.size())
622 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
623 if (nOut >= vfSpent.size())
625 return (!!vfSpent[nOut]);
628 int64_t GetDebit(const isminefilter& filter) const
634 if (filter & MINE_SPENDABLE)
637 nDebit += nDebitCached;
640 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
642 nDebit += nDebitCached;
645 if (filter & MINE_WATCH_ONLY)
647 if (fWatchDebitCached)
648 nDebit += nWatchDebitCached;
651 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
652 fWatchDebitCached = true;
653 nDebit += nWatchDebitCached;
660 int64_t GetCredit(const isminefilter& filter) const
662 // Must wait until coinbase is safely deep enough in the chain before valuing it
663 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
667 if (filter & MINE_SPENDABLE)
669 // GetBalance can assume transactions in mapWallet won't change
671 credit += nCreditCached;
674 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
675 fCreditCached = true;
676 credit += nCreditCached;
679 if (filter & MINE_WATCH_ONLY)
681 if (fWatchCreditCached)
682 credit += nWatchCreditCached;
685 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
686 fWatchCreditCached = true;
687 credit += nWatchCreditCached;
693 int64_t GetImmatureCredit(bool fUseCache=true) const
695 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
697 if (fUseCache && fImmatureCreditCached)
698 return nImmatureCreditCached;
699 nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
700 fImmatureCreditCached = true;
701 return nImmatureCreditCached;
707 int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const
709 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
711 if (fUseCache && fImmatureWatchCreditCached)
712 return nImmatureWatchCreditCached;
713 nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
714 fImmatureWatchCreditCached = true;
715 return nImmatureWatchCreditCached;
722 int64_t GetAvailableCredit(bool fUseCache=true) const
724 // Must wait until coinbase is safely deep enough in the chain before valuing it
725 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
729 if (fAvailableCreditCached)
730 return nAvailableCreditCached;
734 for (unsigned int i = 0; i < vout.size(); i++)
738 const CTxOut &txout = vout[i];
739 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
740 if (!MoneyRange(nCredit))
741 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
745 nAvailableCreditCached = nCredit;
746 fAvailableCreditCached = true;
751 int64_t GetAvailableWatchCredit(bool fUseCache=true) const
753 // Must wait until coinbase is safely deep enough in the chain before valuing it
754 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
758 if (fAvailableWatchCreditCached)
759 return nAvailableWatchCreditCached;
763 for (unsigned int i = 0; i < vout.size(); i++)
767 const CTxOut &txout = vout[i];
768 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
769 if (!MoneyRange(nCredit))
770 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
774 nAvailableWatchCreditCached = nCredit;
775 fAvailableWatchCreditCached = true;
780 int64_t GetChange() const
783 return nChangeCached;
784 nChangeCached = pwallet->GetChange(*this);
785 fChangeCached = true;
786 return nChangeCached;
789 void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CTxDestination, int64_t> >& listReceived,
790 std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
792 void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
793 int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
795 bool IsFromMe(const isminefilter& filter) const
797 return (GetDebit(filter) > 0);
800 bool IsTrusted() const
802 // Quick answer in most cases
805 if (GetDepthInMainChain() >= 1)
807 if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
810 // If no confirmations but it's from us, we can still
811 // consider it confirmed if all dependencies are confirmed
812 std::map<uint256, const CMerkleTx*> mapPrev;
813 std::vector<const CMerkleTx*> vWorkQueue;
814 vWorkQueue.reserve(vtxPrev.size()+1);
815 vWorkQueue.push_back(this);
816 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
818 const CMerkleTx* ptx = vWorkQueue[i];
822 if (ptx->GetDepthInMainChain() >= 1)
824 if (!pwallet->IsFromMe(*ptx))
829 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
830 mapPrev[tx.GetHash()] = &tx;
833 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
835 if (!mapPrev.count(txin.prevout.hash))
837 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
846 int64_t GetTxTime() const;
847 int GetRequestCount() const;
849 void AddSupportingTransactions(CTxDB& txdb);
851 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
852 bool AcceptWalletTransaction();
854 void RelayWalletTransaction(CTxDB& txdb);
855 void RelayWalletTransaction();
869 COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
871 tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
874 std::string ToString() const
876 return strprintf("COutput(%s, %d, %d, %d) [%s]", tx->GetHash().ToString().substr(0,10).c_str(), i, fSpendable, nDepth, FormatMoney(tx->vout[i].nValue).c_str());
881 printf("%s\n", ToString().c_str());
888 /** Private key that includes an expiration date in case it never gets used. */
893 int64_t nTimeCreated;
894 int64_t nTimeExpires;
895 std::string strComment;
896 //// todo: add something to note what created it (user, getnewaddress, change)
897 //// maybe should have a map<string, string> property map
899 CWalletKey(int64_t nExpires=0)
901 nTimeCreated = (nExpires ? GetTime() : 0);
902 nTimeExpires = nExpires;
907 if (!(nType & SER_GETHASH))
909 READWRITE(vchPrivKey);
910 READWRITE(nTimeCreated);
911 READWRITE(nTimeExpires);
912 READWRITE(strComment);
921 /** Account information.
922 * Stored in wallet with key "acc"+string account name.
936 vchPubKey = CPubKey();
941 if (!(nType & SER_GETHASH))
943 READWRITE(vchPubKey);
949 /** Internal transfers.
950 * Database key is acentry<account><counter>.
952 class CAccountingEntry
955 std::string strAccount;
956 int64_t nCreditDebit;
958 std::string strOtherAccount;
959 std::string strComment;
961 int64_t nOrderPos; // position in ordered transaction list
974 strOtherAccount.clear();
981 CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
982 if (!(nType & SER_GETHASH))
984 // Note: strAccount is serialized as part of the key, not here.
985 READWRITE(nCreditDebit);
987 READWRITE(strOtherAccount);
991 WriteOrderPos(nOrderPos, me.mapValue);
993 if (!(mapValue.empty() && _ssExtra.empty()))
995 CDataStream ss(nType, nVersion);
996 ss.insert(ss.begin(), '\0');
998 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
999 me.strComment.append(ss.str());
1003 READWRITE(strComment);
1005 size_t nSepPos = strComment.find("\0", 0, 1);
1008 me.mapValue.clear();
1009 if (std::string::npos != nSepPos)
1011 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
1013 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
1015 ReadOrderPos(me.nOrderPos, me.mapValue);
1017 if (std::string::npos != nSepPos)
1018 me.strComment.erase(nSepPos);
1020 me.mapValue.erase("n");
1024 std::vector<char> _ssExtra;
1027 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);