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 GetStakeStats(float &nKernelsRate, float &nCoinDaysRate);
221 void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
222 bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
223 bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
225 std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
226 std::string SendMoneyToDestination(const CTxDestination &address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
228 bool NewKeyPool(unsigned int nSize = 0);
229 bool TopUpKeyPool(unsigned int nSize = 0);
230 int64_t AddReserveKey(const CKeyPool& keypool);
231 void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
232 void KeepKey(int64_t nIndex);
233 void ReturnKey(int64_t nIndex);
234 bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
235 int64_t GetOldestKeyPoolTime();
236 void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
238 std::set< std::set<CTxDestination> > GetAddressGroupings();
239 std::map<CTxDestination, int64_t> GetAddressBalances();
241 isminetype IsMine(const CTxIn& txin) const;
242 int64_t GetDebit(const CTxIn& txin, const isminefilter& filter) const;
243 isminetype IsMine(const CTxOut& txout) const
245 return ::IsMine(*this, txout.scriptPubKey);
247 int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const
249 if (!MoneyRange(txout.nValue))
250 throw std::runtime_error("CWallet::GetCredit() : value out of range");
251 return (IsMine(txout) & filter ? txout.nValue : 0);
253 bool IsChange(const CTxOut& txout) const;
254 int64_t GetChange(const CTxOut& txout) const
256 if (!MoneyRange(txout.nValue))
257 throw std::runtime_error("CWallet::GetChange() : value out of range");
258 return (IsChange(txout) ? txout.nValue : 0);
260 bool IsMine(const CTransaction& tx) const
262 BOOST_FOREACH(const CTxOut& txout, tx.vout)
263 if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
267 bool IsFromMe(const CTransaction& tx) const
269 return (GetDebit(tx, MINE_ALL) > 0);
271 int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const
274 BOOST_FOREACH(const CTxIn& txin, tx.vin)
276 nDebit += GetDebit(txin, filter);
277 if (!MoneyRange(nDebit))
278 throw std::runtime_error("CWallet::GetDebit() : value out of range");
282 int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const
285 BOOST_FOREACH(const CTxOut& txout, tx.vout)
287 nCredit += GetCredit(txout, filter);
288 if (!MoneyRange(nCredit))
289 throw std::runtime_error("CWallet::GetCredit() : value out of range");
293 int64_t GetChange(const CTransaction& tx) const
296 BOOST_FOREACH(const CTxOut& txout, tx.vout)
298 nChange += GetChange(txout);
299 if (!MoneyRange(nChange))
300 throw std::runtime_error("CWallet::GetChange() : value out of range");
304 void SetBestChain(const CBlockLocator& loc);
306 DBErrors LoadWallet(bool& fFirstRunRet);
308 DBErrors ZapWalletTx();
310 bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
312 bool DelAddressBookName(const CTxDestination& address);
314 void UpdatedTransaction(const uint256 &hashTx);
316 void PrintWallet(const CBlock& block);
318 void Inventory(const uint256 &hash)
322 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
323 if (mi != mapRequestCount.end())
328 unsigned int GetKeyPoolSize()
330 return (unsigned int)(setKeyPool.size());
333 bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
335 bool SetDefaultKey(const CPubKey &vchPubKey);
337 // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
338 bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
340 // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
341 bool SetMaxVersion(int nVersion);
343 // get the current wallet format (the oldest client version guaranteed to understand this wallet)
344 int GetVersion() { return nWalletVersion; }
346 void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
347 void DisableTransaction(const CTransaction &tx);
349 /** Address book entry changed.
350 * @note called with lock cs_wallet held.
352 boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
354 /** Wallet transaction added, removed or updated.
355 * @note called with lock cs_wallet held.
357 boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
359 /** Watch-only address added */
360 boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
363 /** A key allocated from the key pool. */
371 CReserveKey(CWallet* pwalletIn)
384 CPubKey GetReservedKey();
389 typedef std::map<std::string, std::string> mapValue_t;
392 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
394 if (!mapValue.count("n"))
396 nOrderPos = -1; // TODO: calculate elsewhere
399 nOrderPos = atoi64(mapValue["n"].c_str());
403 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
407 mapValue["n"] = i64tostr(nOrderPos);
411 /** A transaction with a bunch of additional info that only the owner cares about.
412 * It includes any unrecorded transactions needed to link it back to the block chain.
414 class CWalletTx : public CMerkleTx
417 const CWallet* pwallet;
420 std::vector<CMerkleTx> vtxPrev;
422 std::vector<std::pair<std::string, std::string> > vOrderForm;
423 unsigned int fTimeReceivedIsTxTime;
424 unsigned int nTimeReceived; // time received by this node
425 unsigned int nTimeSmart;
427 std::string strFromAccount;
428 std::vector<char> vfSpent; // which outputs are already spent
429 int64_t nOrderPos; // position in ordered transaction list
432 mutable bool fDebitCached;
433 mutable bool fWatchDebitCached;
434 mutable bool fCreditCached;
435 mutable bool fWatchCreditCached;
436 mutable bool fAvailableCreditCached;
437 mutable bool fImmatureCreditCached;
438 mutable bool fImmatureWatchCreditCached;
439 mutable bool fAvailableWatchCreditCached;
440 mutable bool fChangeCached;
441 mutable int64_t nDebitCached;
442 mutable int64_t nWatchDebitCached;
443 mutable int64_t nCreditCached;
444 mutable int64_t nWatchCreditCached;
445 mutable int64_t nAvailableCreditCached;
446 mutable int64_t nImmatureCreditCached;
447 mutable int64_t nImmatureWatchCreditCached;
448 mutable int64_t nAvailableWatchCreditCached;
449 mutable int64_t nChangeCached;
456 CWalletTx(const CWallet* pwalletIn)
461 CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
466 CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
471 void Init(const CWallet* pwalletIn)
477 fTimeReceivedIsTxTime = false;
481 strFromAccount.clear();
483 fDebitCached = false;
484 fWatchDebitCached = false;
485 fCreditCached = false;
486 fWatchCreditCached = false;
487 fAvailableCreditCached = false;
488 fAvailableWatchCreditCached = false;
489 fImmatureCreditCached = false;
490 fImmatureWatchCreditCached = false;
491 fChangeCached = false;
493 nWatchDebitCached = 0;
495 nWatchCreditCached = 0;
496 nAvailableCreditCached = 0;
497 nAvailableWatchCreditCached = 0;
498 nImmatureCreditCached = 0;
499 nImmatureWatchCreditCached = 0;
506 CWalletTx* pthis = const_cast<CWalletTx*>(this);
513 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
516 BOOST_FOREACH(char f, vfSpent)
518 str += (f ? '1' : '0');
522 pthis->mapValue["spent"] = str;
524 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
527 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
530 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
533 READWRITE(vOrderForm);
534 READWRITE(fTimeReceivedIsTxTime);
535 READWRITE(nTimeReceived);
541 pthis->strFromAccount = pthis->mapValue["fromaccount"];
543 if (mapValue.count("spent"))
544 BOOST_FOREACH(char c, pthis->mapValue["spent"])
545 pthis->vfSpent.push_back(c != '0');
547 pthis->vfSpent.assign(vout.size(), fSpent);
549 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
551 pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
554 pthis->mapValue.erase("fromaccount");
555 pthis->mapValue.erase("version");
556 pthis->mapValue.erase("spent");
557 pthis->mapValue.erase("n");
558 pthis->mapValue.erase("timesmart");
561 // marks certain txout's as spent
562 // returns true if any update took place
563 bool UpdateSpent(const std::vector<char>& vfNewSpent)
565 bool fReturn = false;
566 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
568 if (i == vfSpent.size())
571 if (vfNewSpent[i] && !vfSpent[i])
575 fAvailableCreditCached = fAvailableWatchCreditCached = false;
581 // make sure balances are recalculated
584 fCreditCached = false;
585 fAvailableCreditCached = fAvailableWatchCreditCached = false;
586 fDebitCached = fWatchDebitCached = false;
587 fChangeCached = false;
590 void BindWallet(CWallet *pwalletIn)
596 void MarkSpent(unsigned int nOut)
598 if (nOut >= vout.size())
599 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
600 vfSpent.resize(vout.size());
603 vfSpent[nOut] = true;
604 fAvailableCreditCached = fAvailableWatchCreditCached = false;
608 void MarkUnspent(unsigned int nOut)
610 if (nOut >= vout.size())
611 throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
612 vfSpent.resize(vout.size());
615 vfSpent[nOut] = false;
616 fAvailableCreditCached = fAvailableWatchCreditCached = false;
620 bool IsSpent(unsigned int nOut) const
622 if (nOut >= vout.size())
623 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
624 if (nOut >= vfSpent.size())
626 return (!!vfSpent[nOut]);
629 int64_t GetDebit(const isminefilter& filter) const
635 if (filter & MINE_SPENDABLE)
638 nDebit += nDebitCached;
641 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
643 nDebit += nDebitCached;
646 if (filter & MINE_WATCH_ONLY)
648 if (fWatchDebitCached)
649 nDebit += nWatchDebitCached;
652 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
653 fWatchDebitCached = true;
654 nDebit += nWatchDebitCached;
661 int64_t GetCredit(const isminefilter& filter) const
663 // Must wait until coinbase is safely deep enough in the chain before valuing it
664 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
668 if (filter & MINE_SPENDABLE)
670 // GetBalance can assume transactions in mapWallet won't change
672 credit += nCreditCached;
675 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
676 fCreditCached = true;
677 credit += nCreditCached;
680 if (filter & MINE_WATCH_ONLY)
682 if (fWatchCreditCached)
683 credit += nWatchCreditCached;
686 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
687 fWatchCreditCached = true;
688 credit += nWatchCreditCached;
694 int64_t GetImmatureCredit(bool fUseCache=true) const
696 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
698 if (fUseCache && fImmatureCreditCached)
699 return nImmatureCreditCached;
700 nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
701 fImmatureCreditCached = true;
702 return nImmatureCreditCached;
708 int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const
710 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
712 if (fUseCache && fImmatureWatchCreditCached)
713 return nImmatureWatchCreditCached;
714 nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
715 fImmatureWatchCreditCached = true;
716 return nImmatureWatchCreditCached;
723 int64_t GetAvailableCredit(bool fUseCache=true) const
725 // Must wait until coinbase is safely deep enough in the chain before valuing it
726 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
730 if (fAvailableCreditCached)
731 return nAvailableCreditCached;
735 for (unsigned int i = 0; i < vout.size(); i++)
739 const CTxOut &txout = vout[i];
740 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
741 if (!MoneyRange(nCredit))
742 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
746 nAvailableCreditCached = nCredit;
747 fAvailableCreditCached = true;
752 int64_t GetAvailableWatchCredit(bool fUseCache=true) const
754 // Must wait until coinbase is safely deep enough in the chain before valuing it
755 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
759 if (fAvailableWatchCreditCached)
760 return nAvailableWatchCreditCached;
764 for (unsigned int i = 0; i < vout.size(); i++)
768 const CTxOut &txout = vout[i];
769 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
770 if (!MoneyRange(nCredit))
771 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
775 nAvailableWatchCreditCached = nCredit;
776 fAvailableWatchCreditCached = true;
781 int64_t GetChange() const
784 return nChangeCached;
785 nChangeCached = pwallet->GetChange(*this);
786 fChangeCached = true;
787 return nChangeCached;
790 void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CTxDestination, int64_t> >& listReceived,
791 std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
793 void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
794 int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
796 bool IsFromMe(const isminefilter& filter) const
798 return (GetDebit(filter) > 0);
801 bool IsTrusted() const
803 // Quick answer in most cases
806 if (GetDepthInMainChain() >= 1)
808 if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
811 // If no confirmations but it's from us, we can still
812 // consider it confirmed if all dependencies are confirmed
813 std::map<uint256, const CMerkleTx*> mapPrev;
814 std::vector<const CMerkleTx*> vWorkQueue;
815 vWorkQueue.reserve(vtxPrev.size()+1);
816 vWorkQueue.push_back(this);
817 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
819 const CMerkleTx* ptx = vWorkQueue[i];
823 if (ptx->GetDepthInMainChain() >= 1)
825 if (!pwallet->IsFromMe(*ptx))
830 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
831 mapPrev[tx.GetHash()] = &tx;
834 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
836 if (!mapPrev.count(txin.prevout.hash))
838 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
847 int64_t GetTxTime() const;
848 int GetRequestCount() const;
850 void AddSupportingTransactions(CTxDB& txdb);
852 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
853 bool AcceptWalletTransaction();
855 void RelayWalletTransaction(CTxDB& txdb);
856 void RelayWalletTransaction();
870 COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
872 tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
875 std::string ToString() const
877 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());
882 printf("%s\n", ToString().c_str());
889 /** Private key that includes an expiration date in case it never gets used. */
894 int64_t nTimeCreated;
895 int64_t nTimeExpires;
896 std::string strComment;
897 //// todo: add something to note what created it (user, getnewaddress, change)
898 //// maybe should have a map<string, string> property map
900 CWalletKey(int64_t nExpires=0)
902 nTimeCreated = (nExpires ? GetTime() : 0);
903 nTimeExpires = nExpires;
908 if (!(nType & SER_GETHASH))
910 READWRITE(vchPrivKey);
911 READWRITE(nTimeCreated);
912 READWRITE(nTimeExpires);
913 READWRITE(strComment);
922 /** Account information.
923 * Stored in wallet with key "acc"+string account name.
937 vchPubKey = CPubKey();
942 if (!(nType & SER_GETHASH))
944 READWRITE(vchPubKey);
950 /** Internal transfers.
951 * Database key is acentry<account><counter>.
953 class CAccountingEntry
956 std::string strAccount;
957 int64_t nCreditDebit;
959 std::string strOtherAccount;
960 std::string strComment;
962 int64_t nOrderPos; // position in ordered transaction list
975 strOtherAccount.clear();
982 CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
983 if (!(nType & SER_GETHASH))
985 // Note: strAccount is serialized as part of the key, not here.
986 READWRITE(nCreditDebit);
988 READWRITE(strOtherAccount);
992 WriteOrderPos(nOrderPos, me.mapValue);
994 if (!(mapValue.empty() && _ssExtra.empty()))
996 CDataStream ss(nType, nVersion);
997 ss.insert(ss.begin(), '\0');
999 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
1000 me.strComment.append(ss.str());
1004 READWRITE(strComment);
1006 size_t nSepPos = strComment.find("\0", 0, 1);
1009 me.mapValue.clear();
1010 if (std::string::npos != nSepPos)
1012 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
1014 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
1016 ReadOrderPos(me.nOrderPos, me.mapValue);
1018 if (std::string::npos != nSepPos)
1019 me.strComment.erase(nSepPos);
1021 me.mapValue.erase("n");
1025 std::vector<char> _ssExtra;
1028 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);