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"
22 extern unsigned int nStakeMaxAge;
23 extern bool fWalletUnlockMintOnly;
24 extern bool fConfChange;
25 class CAccountingEntry;
31 // Set of selected transactions
32 typedef std::set<std::pair<const CWalletTx*,unsigned int> > CoinsSet;
34 /** (client) version numbers for particular wallet features */
37 FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
39 FEATURE_WALLETCRYPT = 40000, // wallet encryption
40 FEATURE_COMPRPUBKEY = 60000, // compressed public keys
41 FEATURE_MALLKEY = 60017,
42 FEATURE_LATEST = 60017
45 /** A key pool entry */
57 CKeyPool(const CPubKey& vchPubKeyIn)
60 vchPubKey = vchPubKeyIn;
65 if (!(nType & SER_GETHASH))
72 /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
73 * and provides the ability to create new transactions.
75 class CWallet : public CCryptoKeyStore
78 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;
80 CWalletDB *pwalletdbEncryption, *pwalletdbDecryption;
82 // the current wallet version: clients below this version are not able to load the wallet
85 // the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
86 int nWalletMaxVersion;
88 // stake mining statistics
89 uint64_t nKernelsTried;
90 uint64_t nCoinDaysTried;
93 mutable CCriticalSection cs_wallet;
96 std::string strWalletFile;
98 std::set<int64_t> setKeyPool;
100 std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
101 std::map<CMalleableKeyView, CKeyMetadata> mapMalleableKeyMetadata;
104 std::map<CBitcoinAddress, CKeyMetadata> mapKeyMetadata;
106 typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
107 MasterKeyMap mapMasterKeys;
108 unsigned int nMasterKeyMaxID;
114 CWallet(std::string strWalletFileIn)
118 strWalletFile = strWalletFileIn;
123 nWalletVersion = FEATURE_BASE;
124 nWalletMaxVersion = FEATURE_BASE;
127 pwalletdbEncryption = NULL;
128 pwalletdbDecryption = NULL;
135 std::map<uint256, CWalletTx> mapWallet;
136 std::vector<uint256> vMintingWalletUpdated;
137 int64_t nOrderPosNext;
138 std::map<uint256, int> mapRequestCount;
140 std::map<CBitcoinAddress, std::string> mapAddressBook;
142 CPubKey vchDefaultKey;
143 int64_t nTimeFirstKey;
145 // check whether we are allowed to upgrade (or already support) to the named feature
146 bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
148 void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf, int64_t nMinValue, int64_t nMaxValue) const;
149 void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
150 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;
152 // Simple select (without randomization)
153 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;
155 // keystore implementation
156 // Generate a new key
157 CPubKey GenerateNewKey();
158 CMalleableKeyView GenerateNewMalleableKey();
159 // Adds a key to the store, and saves it to disk.
160 bool AddKey(const CKey& key);
161 bool AddMalleableKey(const CMalleableKey& mKey);
162 // Adds a key to the store, without saving it to disk (used by LoadWallet)
163 bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
164 // Load metadata (used by LoadWallet)
165 bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
166 bool LoadKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetadata &metadata);
168 // Load malleable key without saving it to disk (used by LoadWallet)
169 bool LoadMalleableKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) { return CCryptoKeyStore::AddMalleableKey(keyView, vchSecretH); }
170 bool LoadCryptedMalleableKey(const CMalleableKeyView &keyView, const std::vector<unsigned char> &vchCryptedSecretH) { return CCryptoKeyStore::AddCryptedMalleableKey(keyView, vchCryptedSecretH); }
172 bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
174 // Adds an encrypted key to the store, and saves it to disk.
175 bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
176 bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH);
177 // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
178 bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
179 bool AddCScript(const CScript& redeemScript);
180 bool LoadCScript(const CScript& redeemScript);
182 // Adds a watch-only address to the store, and saves it to disk.
183 bool AddWatchOnly(const CScript &dest);
184 bool RemoveWatchOnly(const CScript &dest);
185 // Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
186 bool LoadWatchOnly(const CScript &dest);
188 bool Unlock(const SecureString& strWalletPassphrase);
189 bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
190 bool EncryptWallet(const SecureString& strWalletPassphrase);
191 bool DecryptWallet(const SecureString& strWalletPassphrase);
193 void GetAddresses(std::map<CBitcoinAddress, int64_t> &mapAddresses) const;
196 /** Increment the next transaction order id
197 @return next transaction order id
199 int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
201 typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
202 typedef std::multimap<int64_t, TxPair > TxItems;
204 /** Get the wallet's activity log
205 @return multimap of ordered transactions and accounting entries
206 @warning Returned pointers are *only* valid within the scope of passed acentries
208 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
211 bool AddToWallet(const CWalletTx& wtxIn);
212 bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
213 bool EraseFromWallet(uint256 hash);
215 void WalletUpdateSpent(const CTransaction& prevout, bool fBlock = false);
216 int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
217 int ScanForWalletTransaction(const uint256& hashTx);
218 void ReacceptWalletTransactions();
219 void ResendWalletTransactions(bool fForceResend=false);
220 int64_t GetBalance() const;
221 int64_t GetWatchOnlyBalance() const;
222 int64_t GetUnconfirmedBalance() const;
223 int64_t GetUnconfirmedWatchOnlyBalance() const;
224 int64_t GetImmatureBalance() const;
225 int64_t GetImmatureWatchOnlyBalance() const;
226 int64_t GetStake() const;
227 int64_t GetNewMint() const;
228 int64_t GetWatchOnlyStake() const;
229 int64_t GetWatchOnlyNewMint() const;
230 bool CreateTransaction(const std::vector<std::pair<CScript, int64_t> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
231 bool CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
232 bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
234 void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
235 bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
236 bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
238 std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
240 bool NewKeyPool(unsigned int nSize = 0);
241 bool TopUpKeyPool(unsigned int nSize = 0);
242 int64_t AddReserveKey(const CKeyPool& keypool);
243 void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
244 void KeepKey(int64_t nIndex);
245 void ReturnKey(int64_t nIndex);
246 bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
247 int64_t GetOldestKeyPoolTime();
248 void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
250 std::set< std::set<CBitcoinAddress> > GetAddressGroupings();
251 std::map<CBitcoinAddress, int64_t> GetAddressBalances();
253 isminetype IsMine(const CTxIn& txin) const;
254 int64_t GetDebit(const CTxIn& txin, const isminefilter& filter) const;
255 isminetype IsMine(const CTxOut& txout) const
257 return ::IsMine(*this, txout.scriptPubKey);
259 int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const
261 if (!MoneyRange(txout.nValue))
262 throw std::runtime_error("CWallet::GetCredit() : value out of range");
263 return (IsMine(txout) & filter ? txout.nValue : 0);
265 bool IsChange(const CTxOut& txout) const;
266 int64_t GetChange(const CTxOut& txout) const
268 if (!MoneyRange(txout.nValue))
269 throw std::runtime_error("CWallet::GetChange() : value out of range");
270 return (IsChange(txout) ? txout.nValue : 0);
272 bool IsMine(const CTransaction& tx) const
274 BOOST_FOREACH(const CTxOut& txout, tx.vout)
275 if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
279 bool IsFromMe(const CTransaction& tx) const
281 return (GetDebit(tx, MINE_ALL) > 0);
283 int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const
286 BOOST_FOREACH(const CTxIn& txin, tx.vin)
288 nDebit += GetDebit(txin, filter);
289 if (!MoneyRange(nDebit))
290 throw std::runtime_error("CWallet::GetDebit() : value out of range");
294 int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const
297 BOOST_FOREACH(const CTxOut& txout, tx.vout)
299 nCredit += GetCredit(txout, filter);
300 if (!MoneyRange(nCredit))
301 throw std::runtime_error("CWallet::GetCredit() : value out of range");
305 int64_t GetChange(const CTransaction& tx) const
308 BOOST_FOREACH(const CTxOut& txout, tx.vout)
310 nChange += GetChange(txout);
311 if (!MoneyRange(nChange))
312 throw std::runtime_error("CWallet::GetChange() : value out of range");
316 void SetBestChain(const CBlockLocator& loc);
318 DBErrors LoadWallet(bool& fFirstRunRet);
320 DBErrors ZapWalletTx();
322 bool SetAddressBookName(const CBitcoinAddress& address, const std::string& strName);
324 bool DelAddressBookName(const CBitcoinAddress& address);
326 void UpdatedTransaction(const uint256 &hashTx);
328 void PrintWallet(const CBlock& block);
330 void Inventory(const uint256 &hash)
334 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
335 if (mi != mapRequestCount.end())
340 unsigned int GetKeyPoolSize()
342 return (unsigned int)(setKeyPool.size());
345 bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
347 bool SetDefaultKey(const CPubKey &vchPubKey);
349 // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
350 bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
352 // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
353 bool SetMaxVersion(int nVersion);
355 // get the current wallet format (the oldest client version guaranteed to understand this wallet)
356 int GetVersion() { return nWalletVersion; }
358 void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
359 void DisableTransaction(const CTransaction &tx);
361 /** Address book entry changed.
362 * @note called with lock cs_wallet held.
364 boost::signals2::signal<void (CWallet *wallet, const CBitcoinAddress &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
366 /** Wallet transaction added, removed or updated.
367 * @note called with lock cs_wallet held.
369 boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
371 /** Watch-only address added */
372 boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
375 /** A key allocated from the key pool. */
383 CReserveKey(CWallet* pwalletIn)
396 CPubKey GetReservedKey();
401 typedef std::map<std::string, std::string> mapValue_t;
404 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
406 if (!mapValue.count("n"))
408 nOrderPos = -1; // TODO: calculate elsewhere
411 nOrderPos = atoi64(mapValue["n"].c_str());
415 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
419 mapValue["n"] = i64tostr(nOrderPos);
423 /** A transaction with a bunch of additional info that only the owner cares about.
424 * It includes any unrecorded transactions needed to link it back to the block chain.
426 class CWalletTx : public CMerkleTx
429 const CWallet* pwallet;
432 std::vector<CMerkleTx> vtxPrev;
434 std::vector<std::pair<std::string, std::string> > vOrderForm;
435 unsigned int fTimeReceivedIsTxTime;
436 unsigned int nTimeReceived; // time received by this node
437 unsigned int nTimeSmart;
439 std::string strFromAccount;
440 std::vector<char> vfSpent; // which outputs are already spent
441 int64_t nOrderPos; // position in ordered transaction list
444 mutable bool fDebitCached;
445 mutable bool fWatchDebitCached;
446 mutable bool fCreditCached;
447 mutable bool fWatchCreditCached;
448 mutable bool fAvailableCreditCached;
449 mutable bool fImmatureCreditCached;
450 mutable bool fImmatureWatchCreditCached;
451 mutable bool fAvailableWatchCreditCached;
452 mutable bool fChangeCached;
453 mutable int64_t nDebitCached;
454 mutable int64_t nWatchDebitCached;
455 mutable int64_t nCreditCached;
456 mutable int64_t nWatchCreditCached;
457 mutable int64_t nAvailableCreditCached;
458 mutable int64_t nImmatureCreditCached;
459 mutable int64_t nImmatureWatchCreditCached;
460 mutable int64_t nAvailableWatchCreditCached;
461 mutable int64_t nChangeCached;
468 CWalletTx(const CWallet* pwalletIn)
473 CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
478 CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
483 void Init(const CWallet* pwalletIn)
489 fTimeReceivedIsTxTime = false;
493 strFromAccount.clear();
495 fDebitCached = false;
496 fWatchDebitCached = false;
497 fCreditCached = false;
498 fWatchCreditCached = false;
499 fAvailableCreditCached = false;
500 fAvailableWatchCreditCached = false;
501 fImmatureCreditCached = false;
502 fImmatureWatchCreditCached = false;
503 fChangeCached = false;
505 nWatchDebitCached = 0;
507 nWatchCreditCached = 0;
508 nAvailableCreditCached = 0;
509 nAvailableWatchCreditCached = 0;
510 nImmatureCreditCached = 0;
511 nImmatureWatchCreditCached = 0;
518 CWalletTx* pthis = const_cast<CWalletTx*>(this);
525 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
528 BOOST_FOREACH(char f, vfSpent)
530 str += (f ? '1' : '0');
534 pthis->mapValue["spent"] = str;
536 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
539 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
542 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
545 READWRITE(vOrderForm);
546 READWRITE(fTimeReceivedIsTxTime);
547 READWRITE(nTimeReceived);
553 pthis->strFromAccount = pthis->mapValue["fromaccount"];
555 if (mapValue.count("spent"))
556 BOOST_FOREACH(char c, pthis->mapValue["spent"])
557 pthis->vfSpent.push_back(c != '0');
559 pthis->vfSpent.assign(vout.size(), fSpent);
561 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
563 pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
566 pthis->mapValue.erase("fromaccount");
567 pthis->mapValue.erase("version");
568 pthis->mapValue.erase("spent");
569 pthis->mapValue.erase("n");
570 pthis->mapValue.erase("timesmart");
573 // marks certain txout's as spent
574 // returns true if any update took place
575 bool UpdateSpent(const std::vector<char>& vfNewSpent)
577 bool fReturn = false;
578 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
580 if (i == vfSpent.size())
583 if (vfNewSpent[i] && !vfSpent[i])
587 fAvailableCreditCached = fAvailableWatchCreditCached = false;
593 // make sure balances are recalculated
596 fCreditCached = false;
597 fAvailableCreditCached = fAvailableWatchCreditCached = false;
598 fDebitCached = fWatchDebitCached = false;
599 fChangeCached = false;
602 void BindWallet(CWallet *pwalletIn)
608 void MarkSpent(unsigned int nOut)
610 if (nOut >= vout.size())
611 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
612 vfSpent.resize(vout.size());
615 vfSpent[nOut] = true;
616 fAvailableCreditCached = fAvailableWatchCreditCached = false;
620 void MarkUnspent(unsigned int nOut)
622 if (nOut >= vout.size())
623 throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
624 vfSpent.resize(vout.size());
627 vfSpent[nOut] = false;
628 fAvailableCreditCached = fAvailableWatchCreditCached = false;
632 bool IsSpent(unsigned int nOut) const
634 if (nOut >= vout.size())
635 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
636 if (nOut >= vfSpent.size())
638 return (!!vfSpent[nOut]);
641 int64_t GetDebit(const isminefilter& filter) const
647 if (filter & MINE_SPENDABLE)
650 nDebit += nDebitCached;
653 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
655 nDebit += nDebitCached;
658 if (filter & MINE_WATCH_ONLY)
660 if (fWatchDebitCached)
661 nDebit += nWatchDebitCached;
664 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
665 fWatchDebitCached = true;
666 nDebit += nWatchDebitCached;
673 int64_t GetCredit(const isminefilter& filter) const
675 // Must wait until coinbase is safely deep enough in the chain before valuing it
676 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
680 if (filter & MINE_SPENDABLE)
682 // GetBalance can assume transactions in mapWallet won't change
684 credit += nCreditCached;
687 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
688 fCreditCached = true;
689 credit += nCreditCached;
692 if (filter & MINE_WATCH_ONLY)
694 if (fWatchCreditCached)
695 credit += nWatchCreditCached;
698 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
699 fWatchCreditCached = true;
700 credit += nWatchCreditCached;
706 int64_t GetImmatureCredit(bool fUseCache=true) const
708 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
710 if (fUseCache && fImmatureCreditCached)
711 return nImmatureCreditCached;
712 nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
713 fImmatureCreditCached = true;
714 return nImmatureCreditCached;
720 int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const
722 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
724 if (fUseCache && fImmatureWatchCreditCached)
725 return nImmatureWatchCreditCached;
726 nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
727 fImmatureWatchCreditCached = true;
728 return nImmatureWatchCreditCached;
735 int64_t GetAvailableCredit(bool fUseCache=true) const
737 // Must wait until coinbase is safely deep enough in the chain before valuing it
738 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
742 if (fAvailableCreditCached)
743 return nAvailableCreditCached;
747 for (unsigned int i = 0; i < vout.size(); i++)
751 const CTxOut &txout = vout[i];
752 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
753 if (!MoneyRange(nCredit))
754 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
758 nAvailableCreditCached = nCredit;
759 fAvailableCreditCached = true;
764 int64_t GetAvailableWatchCredit(bool fUseCache=true) const
766 // Must wait until coinbase is safely deep enough in the chain before valuing it
767 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
771 if (fAvailableWatchCreditCached)
772 return nAvailableWatchCreditCached;
776 for (unsigned int i = 0; i < vout.size(); i++)
780 const CTxOut &txout = vout[i];
781 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
782 if (!MoneyRange(nCredit))
783 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
787 nAvailableWatchCreditCached = nCredit;
788 fAvailableWatchCreditCached = true;
793 int64_t GetChange() const
796 return nChangeCached;
797 nChangeCached = pwallet->GetChange(*this);
798 fChangeCached = true;
799 return nChangeCached;
802 void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CBitcoinAddress, int64_t> >& listReceived,
803 std::list<std::pair<CBitcoinAddress, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
805 void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
806 int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
808 bool IsFromMe(const isminefilter& filter) const
810 return (GetDebit(filter) > 0);
813 bool IsTrusted() const
815 // Quick answer in most cases
818 if (GetDepthInMainChain() >= 1)
820 if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
823 // If no confirmations but it's from us, we can still
824 // consider it confirmed if all dependencies are confirmed
825 std::map<uint256, const CMerkleTx*> mapPrev;
826 std::vector<const CMerkleTx*> vWorkQueue;
827 vWorkQueue.reserve(vtxPrev.size()+1);
828 vWorkQueue.push_back(this);
829 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
831 const CMerkleTx* ptx = vWorkQueue[i];
835 if (ptx->GetDepthInMainChain() >= 1)
837 if (!pwallet->IsFromMe(*ptx))
842 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
843 mapPrev[tx.GetHash()] = &tx;
846 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
848 if (!mapPrev.count(txin.prevout.hash))
850 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
859 int64_t GetTxTime() const;
860 int GetRequestCount() const;
862 void AddSupportingTransactions(CTxDB& txdb);
864 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
865 bool AcceptWalletTransaction();
867 void RelayWalletTransaction(CTxDB& txdb);
868 void RelayWalletTransaction();
882 COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
884 tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
887 std::string ToString() const
889 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());
894 printf("%s\n", ToString().c_str());
901 /** Private key that includes an expiration date in case it never gets used. */
906 int64_t nTimeCreated;
907 int64_t nTimeExpires;
908 std::string strComment;
909 //// todo: add something to note what created it (user, getnewaddress, change)
910 //// maybe should have a map<string, string> property map
912 CWalletKey(int64_t nExpires=0)
914 nTimeCreated = (nExpires ? GetTime() : 0);
915 nTimeExpires = nExpires;
920 if (!(nType & SER_GETHASH))
922 READWRITE(vchPrivKey);
923 READWRITE(nTimeCreated);
924 READWRITE(nTimeExpires);
925 READWRITE(strComment);
934 /** Account information.
935 * Stored in wallet with key "acc"+string account name.
949 vchPubKey = CPubKey();
954 if (!(nType & SER_GETHASH))
956 READWRITE(vchPubKey);
962 /** Internal transfers.
963 * Database key is acentry<account><counter>.
965 class CAccountingEntry
968 std::string strAccount;
969 int64_t nCreditDebit;
971 std::string strOtherAccount;
972 std::string strComment;
974 int64_t nOrderPos; // position in ordered transaction list
987 strOtherAccount.clear();
994 CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
995 if (!(nType & SER_GETHASH))
997 // Note: strAccount is serialized as part of the key, not here.
998 READWRITE(nCreditDebit);
1000 READWRITE(strOtherAccount);
1004 WriteOrderPos(nOrderPos, me.mapValue);
1006 if (!(mapValue.empty() && _ssExtra.empty()))
1008 CDataStream ss(nType, nVersion);
1009 ss.insert(ss.begin(), '\0');
1011 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
1012 me.strComment.append(ss.str());
1016 READWRITE(strComment);
1018 size_t nSepPos = strComment.find("\0", 0, 1);
1021 me.mapValue.clear();
1022 if (std::string::npos != nSepPos)
1024 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
1026 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
1028 ReadOrderPos(me.nOrderPos, me.mapValue);
1030 if (std::string::npos != nSepPos)
1031 me.strComment.erase(nSepPos);
1033 me.mapValue.erase("n");
1037 std::vector<char> _ssExtra;
1040 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);