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_MALLKEY = 60017,
41 FEATURE_LATEST = 60017
44 /** A key pool entry */
56 CKeyPool(const CPubKey& vchPubKeyIn)
59 vchPubKey = vchPubKeyIn;
64 if (!(nType & SER_GETHASH))
71 /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
72 * and provides the ability to create new transactions.
74 class CWallet : public CCryptoKeyStore
77 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;
79 CWalletDB *pwalletdbEncryption, *pwalletdbDecryption;
81 // the current wallet version: clients below this version are not able to load the wallet
84 // the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
85 int nWalletMaxVersion;
87 // stake mining statistics
88 uint64_t nKernelsTried;
89 uint64_t nCoinDaysTried;
92 mutable CCriticalSection cs_wallet;
95 std::string strWalletFile;
97 std::set<int64_t> setKeyPool;
98 std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
99 std::map<CMalleableKeyView, CKeyMetadata> mapMalleableKeyMetadata;
101 typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
102 MasterKeyMap mapMasterKeys;
103 unsigned int nMasterKeyMaxID;
109 CWallet(std::string strWalletFileIn)
113 strWalletFile = strWalletFileIn;
118 nWalletVersion = FEATURE_BASE;
119 nWalletMaxVersion = FEATURE_BASE;
122 pwalletdbEncryption = NULL;
123 pwalletdbDecryption = NULL;
130 std::map<uint256, CWalletTx> mapWallet;
131 std::vector<uint256> vMintingWalletUpdated;
132 int64_t nOrderPosNext;
133 std::map<uint256, int> mapRequestCount;
135 std::map<CTxDestination, std::string> mapAddressBook;
137 CPubKey vchDefaultKey;
138 int64_t nTimeFirstKey;
140 // check whether we are allowed to upgrade (or already support) to the named feature
141 bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
143 void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf, int64_t nMinValue, int64_t nMaxValue) const;
144 void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
145 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;
147 // Simple select (without randomization)
148 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;
150 // keystore implementation
151 // Generate a new key
152 CPubKey GenerateNewKey();
153 CMalleableKeyView GenerateNewMalleableKey();
154 // Adds a key to the store, and saves it to disk.
155 bool AddKey(const CKey& key);
156 bool AddMalleableKey(const CMalleableKey& mKey);
157 // Adds a key to the store, without saving it to disk (used by LoadWallet)
158 bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
159 // Load metadata (used by LoadWallet)
160 bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
161 bool LoadMalleableKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetadata &metadata);
163 // Load malleable key without saving it to disk (used by LoadWallet)
164 bool LoadMalleableKey(const CMalleableKey &mKey) { return CCryptoKeyStore::AddMalleableKey(mKey); }
166 bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
168 // Adds an encrypted key to the store, and saves it to disk.
169 bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
170 // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
171 bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
172 bool AddCScript(const CScript& redeemScript);
173 bool LoadCScript(const CScript& redeemScript);
175 // Adds a watch-only address to the store, and saves it to disk.
176 bool AddWatchOnly(const CScript &dest);
177 bool RemoveWatchOnly(const CScript &dest);
178 // Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
179 bool LoadWatchOnly(const CScript &dest);
181 bool Unlock(const SecureString& strWalletPassphrase);
182 bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
183 bool EncryptWallet(const SecureString& strWalletPassphrase);
184 bool DecryptWallet(const SecureString& strWalletPassphrase);
186 void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
189 /** Increment the next transaction order id
190 @return next transaction order id
192 int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
194 typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
195 typedef std::multimap<int64_t, TxPair > TxItems;
197 /** Get the wallet's activity log
198 @return multimap of ordered transactions and accounting entries
199 @warning Returned pointers are *only* valid within the scope of passed acentries
201 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
204 bool AddToWallet(const CWalletTx& wtxIn);
205 bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
206 bool EraseFromWallet(uint256 hash);
208 void WalletUpdateSpent(const CTransaction& prevout, bool fBlock = false);
209 int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
210 int ScanForWalletTransaction(const uint256& hashTx);
211 void ReacceptWalletTransactions();
212 void ResendWalletTransactions();
213 int64_t GetBalance() const;
214 int64_t GetWatchOnlyBalance() const;
215 int64_t GetUnconfirmedBalance() const;
216 int64_t GetUnconfirmedWatchOnlyBalance() const;
217 int64_t GetImmatureBalance() const;
218 int64_t GetImmatureWatchOnlyBalance() const;
219 int64_t GetStake() const;
220 int64_t GetNewMint() const;
221 int64_t GetWatchOnlyStake() const;
222 int64_t GetWatchOnlyNewMint() const;
223 bool CreateTransaction(const std::vector<std::pair<CScript, int64_t> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
224 bool CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
225 bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
227 void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
228 bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
229 bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
231 std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
233 bool NewKeyPool(unsigned int nSize = 0);
234 bool TopUpKeyPool(unsigned int nSize = 0);
235 int64_t AddReserveKey(const CKeyPool& keypool);
236 void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
237 void KeepKey(int64_t nIndex);
238 void ReturnKey(int64_t nIndex);
239 bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
240 int64_t GetOldestKeyPoolTime();
241 void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
243 std::set< std::set<CTxDestination> > GetAddressGroupings();
244 std::map<CTxDestination, int64_t> GetAddressBalances();
246 isminetype IsMine(const CTxIn& txin) const;
247 int64_t GetDebit(const CTxIn& txin, const isminefilter& filter) const;
248 isminetype IsMine(const CTxOut& txout) const
250 return ::IsMine(*this, txout.scriptPubKey);
252 int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const
254 if (!MoneyRange(txout.nValue))
255 throw std::runtime_error("CWallet::GetCredit() : value out of range");
256 return (IsMine(txout) & filter ? txout.nValue : 0);
258 bool IsChange(const CTxOut& txout) const;
259 int64_t GetChange(const CTxOut& txout) const
261 if (!MoneyRange(txout.nValue))
262 throw std::runtime_error("CWallet::GetChange() : value out of range");
263 return (IsChange(txout) ? txout.nValue : 0);
265 bool IsMine(const CTransaction& tx) const
267 BOOST_FOREACH(const CTxOut& txout, tx.vout)
268 if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
272 bool IsFromMe(const CTransaction& tx) const
274 return (GetDebit(tx, MINE_ALL) > 0);
276 int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const
279 BOOST_FOREACH(const CTxIn& txin, tx.vin)
281 nDebit += GetDebit(txin, filter);
282 if (!MoneyRange(nDebit))
283 throw std::runtime_error("CWallet::GetDebit() : value out of range");
287 int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const
290 BOOST_FOREACH(const CTxOut& txout, tx.vout)
292 nCredit += GetCredit(txout, filter);
293 if (!MoneyRange(nCredit))
294 throw std::runtime_error("CWallet::GetCredit() : value out of range");
298 int64_t GetChange(const CTransaction& tx) const
301 BOOST_FOREACH(const CTxOut& txout, tx.vout)
303 nChange += GetChange(txout);
304 if (!MoneyRange(nChange))
305 throw std::runtime_error("CWallet::GetChange() : value out of range");
309 void SetBestChain(const CBlockLocator& loc);
311 DBErrors LoadWallet(bool& fFirstRunRet);
313 DBErrors ZapWalletTx();
315 bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
317 bool DelAddressBookName(const CTxDestination& address);
319 void UpdatedTransaction(const uint256 &hashTx);
321 void PrintWallet(const CBlock& block);
323 void Inventory(const uint256 &hash)
327 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
328 if (mi != mapRequestCount.end())
333 unsigned int GetKeyPoolSize()
335 return (unsigned int)(setKeyPool.size());
338 bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
340 bool SetDefaultKey(const CPubKey &vchPubKey);
342 // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
343 bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
345 // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
346 bool SetMaxVersion(int nVersion);
348 // get the current wallet format (the oldest client version guaranteed to understand this wallet)
349 int GetVersion() { return nWalletVersion; }
351 void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
352 void DisableTransaction(const CTransaction &tx);
354 /** Address book entry changed.
355 * @note called with lock cs_wallet held.
357 boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
359 /** Wallet transaction added, removed or updated.
360 * @note called with lock cs_wallet held.
362 boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
364 /** Watch-only address added */
365 boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
368 /** A key allocated from the key pool. */
376 CReserveKey(CWallet* pwalletIn)
389 CPubKey GetReservedKey();
394 typedef std::map<std::string, std::string> mapValue_t;
397 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
399 if (!mapValue.count("n"))
401 nOrderPos = -1; // TODO: calculate elsewhere
404 nOrderPos = atoi64(mapValue["n"].c_str());
408 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
412 mapValue["n"] = i64tostr(nOrderPos);
416 /** A transaction with a bunch of additional info that only the owner cares about.
417 * It includes any unrecorded transactions needed to link it back to the block chain.
419 class CWalletTx : public CMerkleTx
422 const CWallet* pwallet;
425 std::vector<CMerkleTx> vtxPrev;
427 std::vector<std::pair<std::string, std::string> > vOrderForm;
428 unsigned int fTimeReceivedIsTxTime;
429 unsigned int nTimeReceived; // time received by this node
430 unsigned int nTimeSmart;
432 std::string strFromAccount;
433 std::vector<char> vfSpent; // which outputs are already spent
434 int64_t nOrderPos; // position in ordered transaction list
437 mutable bool fDebitCached;
438 mutable bool fWatchDebitCached;
439 mutable bool fCreditCached;
440 mutable bool fWatchCreditCached;
441 mutable bool fAvailableCreditCached;
442 mutable bool fImmatureCreditCached;
443 mutable bool fImmatureWatchCreditCached;
444 mutable bool fAvailableWatchCreditCached;
445 mutable bool fChangeCached;
446 mutable int64_t nDebitCached;
447 mutable int64_t nWatchDebitCached;
448 mutable int64_t nCreditCached;
449 mutable int64_t nWatchCreditCached;
450 mutable int64_t nAvailableCreditCached;
451 mutable int64_t nImmatureCreditCached;
452 mutable int64_t nImmatureWatchCreditCached;
453 mutable int64_t nAvailableWatchCreditCached;
454 mutable int64_t nChangeCached;
461 CWalletTx(const CWallet* pwalletIn)
466 CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
471 CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
476 void Init(const CWallet* pwalletIn)
482 fTimeReceivedIsTxTime = false;
486 strFromAccount.clear();
488 fDebitCached = false;
489 fWatchDebitCached = false;
490 fCreditCached = false;
491 fWatchCreditCached = false;
492 fAvailableCreditCached = false;
493 fAvailableWatchCreditCached = false;
494 fImmatureCreditCached = false;
495 fImmatureWatchCreditCached = false;
496 fChangeCached = false;
498 nWatchDebitCached = 0;
500 nWatchCreditCached = 0;
501 nAvailableCreditCached = 0;
502 nAvailableWatchCreditCached = 0;
503 nImmatureCreditCached = 0;
504 nImmatureWatchCreditCached = 0;
511 CWalletTx* pthis = const_cast<CWalletTx*>(this);
518 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
521 BOOST_FOREACH(char f, vfSpent)
523 str += (f ? '1' : '0');
527 pthis->mapValue["spent"] = str;
529 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
532 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
535 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
538 READWRITE(vOrderForm);
539 READWRITE(fTimeReceivedIsTxTime);
540 READWRITE(nTimeReceived);
546 pthis->strFromAccount = pthis->mapValue["fromaccount"];
548 if (mapValue.count("spent"))
549 BOOST_FOREACH(char c, pthis->mapValue["spent"])
550 pthis->vfSpent.push_back(c != '0');
552 pthis->vfSpent.assign(vout.size(), fSpent);
554 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
556 pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
559 pthis->mapValue.erase("fromaccount");
560 pthis->mapValue.erase("version");
561 pthis->mapValue.erase("spent");
562 pthis->mapValue.erase("n");
563 pthis->mapValue.erase("timesmart");
566 // marks certain txout's as spent
567 // returns true if any update took place
568 bool UpdateSpent(const std::vector<char>& vfNewSpent)
570 bool fReturn = false;
571 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
573 if (i == vfSpent.size())
576 if (vfNewSpent[i] && !vfSpent[i])
580 fAvailableCreditCached = fAvailableWatchCreditCached = false;
586 // make sure balances are recalculated
589 fCreditCached = false;
590 fAvailableCreditCached = fAvailableWatchCreditCached = false;
591 fDebitCached = fWatchDebitCached = false;
592 fChangeCached = false;
595 void BindWallet(CWallet *pwalletIn)
601 void MarkSpent(unsigned int nOut)
603 if (nOut >= vout.size())
604 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
605 vfSpent.resize(vout.size());
608 vfSpent[nOut] = true;
609 fAvailableCreditCached = fAvailableWatchCreditCached = false;
613 void MarkUnspent(unsigned int nOut)
615 if (nOut >= vout.size())
616 throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
617 vfSpent.resize(vout.size());
620 vfSpent[nOut] = false;
621 fAvailableCreditCached = fAvailableWatchCreditCached = false;
625 bool IsSpent(unsigned int nOut) const
627 if (nOut >= vout.size())
628 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
629 if (nOut >= vfSpent.size())
631 return (!!vfSpent[nOut]);
634 int64_t GetDebit(const isminefilter& filter) const
640 if (filter & MINE_SPENDABLE)
643 nDebit += nDebitCached;
646 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
648 nDebit += nDebitCached;
651 if (filter & MINE_WATCH_ONLY)
653 if (fWatchDebitCached)
654 nDebit += nWatchDebitCached;
657 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
658 fWatchDebitCached = true;
659 nDebit += nWatchDebitCached;
666 int64_t GetCredit(const isminefilter& filter) const
668 // Must wait until coinbase is safely deep enough in the chain before valuing it
669 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
673 if (filter & MINE_SPENDABLE)
675 // GetBalance can assume transactions in mapWallet won't change
677 credit += nCreditCached;
680 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
681 fCreditCached = true;
682 credit += nCreditCached;
685 if (filter & MINE_WATCH_ONLY)
687 if (fWatchCreditCached)
688 credit += nWatchCreditCached;
691 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
692 fWatchCreditCached = true;
693 credit += nWatchCreditCached;
699 int64_t GetImmatureCredit(bool fUseCache=true) const
701 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
703 if (fUseCache && fImmatureCreditCached)
704 return nImmatureCreditCached;
705 nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
706 fImmatureCreditCached = true;
707 return nImmatureCreditCached;
713 int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const
715 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
717 if (fUseCache && fImmatureWatchCreditCached)
718 return nImmatureWatchCreditCached;
719 nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
720 fImmatureWatchCreditCached = true;
721 return nImmatureWatchCreditCached;
728 int64_t GetAvailableCredit(bool fUseCache=true) const
730 // Must wait until coinbase is safely deep enough in the chain before valuing it
731 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
735 if (fAvailableCreditCached)
736 return nAvailableCreditCached;
740 for (unsigned int i = 0; i < vout.size(); i++)
744 const CTxOut &txout = vout[i];
745 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
746 if (!MoneyRange(nCredit))
747 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
751 nAvailableCreditCached = nCredit;
752 fAvailableCreditCached = true;
757 int64_t GetAvailableWatchCredit(bool fUseCache=true) const
759 // Must wait until coinbase is safely deep enough in the chain before valuing it
760 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
764 if (fAvailableWatchCreditCached)
765 return nAvailableWatchCreditCached;
769 for (unsigned int i = 0; i < vout.size(); i++)
773 const CTxOut &txout = vout[i];
774 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
775 if (!MoneyRange(nCredit))
776 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
780 nAvailableWatchCreditCached = nCredit;
781 fAvailableWatchCreditCached = true;
786 int64_t GetChange() const
789 return nChangeCached;
790 nChangeCached = pwallet->GetChange(*this);
791 fChangeCached = true;
792 return nChangeCached;
795 void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CTxDestination, int64_t> >& listReceived,
796 std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
798 void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
799 int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
801 bool IsFromMe(const isminefilter& filter) const
803 return (GetDebit(filter) > 0);
806 bool IsTrusted() const
808 // Quick answer in most cases
811 if (GetDepthInMainChain() >= 1)
813 if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
816 // If no confirmations but it's from us, we can still
817 // consider it confirmed if all dependencies are confirmed
818 std::map<uint256, const CMerkleTx*> mapPrev;
819 std::vector<const CMerkleTx*> vWorkQueue;
820 vWorkQueue.reserve(vtxPrev.size()+1);
821 vWorkQueue.push_back(this);
822 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
824 const CMerkleTx* ptx = vWorkQueue[i];
828 if (ptx->GetDepthInMainChain() >= 1)
830 if (!pwallet->IsFromMe(*ptx))
835 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
836 mapPrev[tx.GetHash()] = &tx;
839 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
841 if (!mapPrev.count(txin.prevout.hash))
843 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
852 int64_t GetTxTime() const;
853 int GetRequestCount() const;
855 void AddSupportingTransactions(CTxDB& txdb);
857 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
858 bool AcceptWalletTransaction();
860 void RelayWalletTransaction(CTxDB& txdb);
861 void RelayWalletTransaction();
875 COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
877 tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
880 std::string ToString() const
882 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());
887 printf("%s\n", ToString().c_str());
894 /** Private key that includes an expiration date in case it never gets used. */
899 int64_t nTimeCreated;
900 int64_t nTimeExpires;
901 std::string strComment;
902 //// todo: add something to note what created it (user, getnewaddress, change)
903 //// maybe should have a map<string, string> property map
905 CWalletKey(int64_t nExpires=0)
907 nTimeCreated = (nExpires ? GetTime() : 0);
908 nTimeExpires = nExpires;
913 if (!(nType & SER_GETHASH))
915 READWRITE(vchPrivKey);
916 READWRITE(nTimeCreated);
917 READWRITE(nTimeExpires);
918 READWRITE(strComment);
927 /** Account information.
928 * Stored in wallet with key "acc"+string account name.
942 vchPubKey = CPubKey();
947 if (!(nType & SER_GETHASH))
949 READWRITE(vchPubKey);
955 /** Internal transfers.
956 * Database key is acentry<account><counter>.
958 class CAccountingEntry
961 std::string strAccount;
962 int64_t nCreditDebit;
964 std::string strOtherAccount;
965 std::string strComment;
967 int64_t nOrderPos; // position in ordered transaction list
980 strOtherAccount.clear();
987 CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
988 if (!(nType & SER_GETHASH))
990 // Note: strAccount is serialized as part of the key, not here.
991 READWRITE(nCreditDebit);
993 READWRITE(strOtherAccount);
997 WriteOrderPos(nOrderPos, me.mapValue);
999 if (!(mapValue.empty() && _ssExtra.empty()))
1001 CDataStream ss(nType, nVersion);
1002 ss.insert(ss.begin(), '\0');
1004 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
1005 me.strComment.append(ss.str());
1009 READWRITE(strComment);
1011 size_t nSepPos = strComment.find("\0", 0, 1);
1014 me.mapValue.clear();
1015 if (std::string::npos != nSepPos)
1017 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
1019 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
1021 ReadOrderPos(me.nOrderPos, me.mapValue);
1023 if (std::string::npos != nSepPos)
1024 me.strComment.erase(nSepPos);
1026 me.mapValue.erase("n");
1030 std::vector<char> _ssExtra;
1033 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);