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;
107 CWallet(std::string strWalletFileIn)
111 strWalletFile = strWalletFileIn;
116 nWalletVersion = FEATURE_BASE;
117 nWalletMaxVersion = FEATURE_BASE;
120 pwalletdbEncryption = NULL;
121 pwalletdbDecryption = NULL;
128 std::map<uint256, CWalletTx> mapWallet;
129 std::vector<uint256> vMintingWalletUpdated;
130 int64_t nOrderPosNext;
131 std::map<uint256, int> mapRequestCount;
133 std::map<CTxDestination, std::string> mapAddressBook;
135 CPubKey vchDefaultKey;
136 int64_t nTimeFirstKey;
138 // check whether we are allowed to upgrade (or already support) to the named feature
139 bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
141 void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf, int64_t nMinValue, int64_t nMaxValue) const;
142 void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
143 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;
145 // Simple select (without randomization)
146 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;
148 // keystore implementation
149 // Generate a new key
150 CPubKey GenerateNewKey();
151 // Adds a key to the store, and saves it to disk.
152 bool AddKey(const CKey& key);
153 // Adds a key to the store, without saving it to disk (used by LoadWallet)
154 bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
155 // Load metadata (used by LoadWallet)
156 bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
158 bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
160 // Adds an encrypted key to the store, and saves it to disk.
161 bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
162 // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
163 bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
164 bool AddCScript(const CScript& redeemScript);
165 bool LoadCScript(const CScript& redeemScript);
167 // Adds a watch-only address to the store, and saves it to disk.
168 bool AddWatchOnly(const CScript &dest);
169 bool RemoveWatchOnly(const CScript &dest);
170 // Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
171 bool LoadWatchOnly(const CScript &dest);
173 bool Unlock(const SecureString& strWalletPassphrase);
174 bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
175 bool EncryptWallet(const SecureString& strWalletPassphrase);
176 bool DecryptWallet(const SecureString& strWalletPassphrase);
178 void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
181 /** Increment the next transaction order id
182 @return next transaction order id
184 int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
186 typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
187 typedef std::multimap<int64_t, TxPair > TxItems;
189 /** Get the wallet's activity log
190 @return multimap of ordered transactions and accounting entries
191 @warning Returned pointers are *only* valid within the scope of passed acentries
193 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
196 bool AddToWallet(const CWalletTx& wtxIn);
197 bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
198 bool EraseFromWallet(uint256 hash);
200 void WalletUpdateSpent(const CTransaction& prevout, bool fBlock = false);
201 int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
202 int ScanForWalletTransaction(const uint256& hashTx);
203 void ReacceptWalletTransactions();
204 void ResendWalletTransactions();
205 int64_t GetBalance() const;
206 int64_t GetWatchOnlyBalance() const;
207 int64_t GetUnconfirmedBalance() const;
208 int64_t GetUnconfirmedWatchOnlyBalance() const;
209 int64_t GetImmatureBalance() const;
210 int64_t GetImmatureWatchOnlyBalance() const;
211 int64_t GetStake() const;
212 int64_t GetNewMint() const;
213 int64_t GetWatchOnlyStake() const;
214 int64_t GetWatchOnlyNewMint() const;
215 bool CreateTransaction(const std::vector<std::pair<CScript, int64_t> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
216 bool CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
217 bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
219 void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
220 bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
221 bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
223 std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
224 std::string SendMoneyToDestination(const CTxDestination &address, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
226 bool NewKeyPool(unsigned int nSize = 0);
227 bool TopUpKeyPool(unsigned int nSize = 0);
228 int64_t AddReserveKey(const CKeyPool& keypool);
229 void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
230 void KeepKey(int64_t nIndex);
231 void ReturnKey(int64_t nIndex);
232 bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
233 int64_t GetOldestKeyPoolTime();
234 void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
236 std::set< std::set<CTxDestination> > GetAddressGroupings();
237 std::map<CTxDestination, int64_t> GetAddressBalances();
239 isminetype IsMine(const CTxIn& txin) const;
240 int64_t GetDebit(const CTxIn& txin, const isminefilter& filter) const;
241 isminetype IsMine(const CTxOut& txout) const
243 return ::IsMine(*this, txout.scriptPubKey);
245 int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const
247 if (!MoneyRange(txout.nValue))
248 throw std::runtime_error("CWallet::GetCredit() : value out of range");
249 return (IsMine(txout) & filter ? txout.nValue : 0);
251 bool IsChange(const CTxOut& txout) const;
252 int64_t GetChange(const CTxOut& txout) const
254 if (!MoneyRange(txout.nValue))
255 throw std::runtime_error("CWallet::GetChange() : value out of range");
256 return (IsChange(txout) ? txout.nValue : 0);
258 bool IsMine(const CTransaction& tx) const
260 BOOST_FOREACH(const CTxOut& txout, tx.vout)
261 if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
265 bool IsFromMe(const CTransaction& tx) const
267 return (GetDebit(tx, MINE_ALL) > 0);
269 int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const
272 BOOST_FOREACH(const CTxIn& txin, tx.vin)
274 nDebit += GetDebit(txin, filter);
275 if (!MoneyRange(nDebit))
276 throw std::runtime_error("CWallet::GetDebit() : value out of range");
280 int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const
283 BOOST_FOREACH(const CTxOut& txout, tx.vout)
285 nCredit += GetCredit(txout, filter);
286 if (!MoneyRange(nCredit))
287 throw std::runtime_error("CWallet::GetCredit() : value out of range");
291 int64_t GetChange(const CTransaction& tx) const
294 BOOST_FOREACH(const CTxOut& txout, tx.vout)
296 nChange += GetChange(txout);
297 if (!MoneyRange(nChange))
298 throw std::runtime_error("CWallet::GetChange() : value out of range");
302 void SetBestChain(const CBlockLocator& loc);
304 DBErrors LoadWallet(bool& fFirstRunRet);
306 DBErrors ZapWalletTx();
308 bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
310 bool DelAddressBookName(const CTxDestination& address);
312 void UpdatedTransaction(const uint256 &hashTx);
314 void PrintWallet(const CBlock& block);
316 void Inventory(const uint256 &hash)
320 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
321 if (mi != mapRequestCount.end())
326 unsigned int GetKeyPoolSize()
328 return (unsigned int)(setKeyPool.size());
331 bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
333 bool SetDefaultKey(const CPubKey &vchPubKey);
335 // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
336 bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
338 // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
339 bool SetMaxVersion(int nVersion);
341 // get the current wallet format (the oldest client version guaranteed to understand this wallet)
342 int GetVersion() { return nWalletVersion; }
344 void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
345 void DisableTransaction(const CTransaction &tx);
347 /** Address book entry changed.
348 * @note called with lock cs_wallet held.
350 boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
352 /** Wallet transaction added, removed or updated.
353 * @note called with lock cs_wallet held.
355 boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
357 /** Watch-only address added */
358 boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
361 /** A key allocated from the key pool. */
369 CReserveKey(CWallet* pwalletIn)
382 CPubKey GetReservedKey();
387 typedef std::map<std::string, std::string> mapValue_t;
390 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
392 if (!mapValue.count("n"))
394 nOrderPos = -1; // TODO: calculate elsewhere
397 nOrderPos = atoi64(mapValue["n"].c_str());
401 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
405 mapValue["n"] = i64tostr(nOrderPos);
409 /** A transaction with a bunch of additional info that only the owner cares about.
410 * It includes any unrecorded transactions needed to link it back to the block chain.
412 class CWalletTx : public CMerkleTx
415 const CWallet* pwallet;
418 std::vector<CMerkleTx> vtxPrev;
420 std::vector<std::pair<std::string, std::string> > vOrderForm;
421 unsigned int fTimeReceivedIsTxTime;
422 unsigned int nTimeReceived; // time received by this node
423 unsigned int nTimeSmart;
425 std::string strFromAccount;
426 std::vector<char> vfSpent; // which outputs are already spent
427 int64_t nOrderPos; // position in ordered transaction list
430 mutable bool fDebitCached;
431 mutable bool fWatchDebitCached;
432 mutable bool fCreditCached;
433 mutable bool fWatchCreditCached;
434 mutable bool fAvailableCreditCached;
435 mutable bool fImmatureCreditCached;
436 mutable bool fImmatureWatchCreditCached;
437 mutable bool fAvailableWatchCreditCached;
438 mutable bool fChangeCached;
439 mutable int64_t nDebitCached;
440 mutable int64_t nWatchDebitCached;
441 mutable int64_t nCreditCached;
442 mutable int64_t nWatchCreditCached;
443 mutable int64_t nAvailableCreditCached;
444 mutable int64_t nImmatureCreditCached;
445 mutable int64_t nImmatureWatchCreditCached;
446 mutable int64_t nAvailableWatchCreditCached;
447 mutable int64_t nChangeCached;
454 CWalletTx(const CWallet* pwalletIn)
459 CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
464 CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
469 void Init(const CWallet* pwalletIn)
475 fTimeReceivedIsTxTime = false;
479 strFromAccount.clear();
481 fDebitCached = false;
482 fWatchDebitCached = false;
483 fCreditCached = false;
484 fWatchCreditCached = false;
485 fAvailableCreditCached = false;
486 fAvailableWatchCreditCached = false;
487 fImmatureCreditCached = false;
488 fImmatureWatchCreditCached = false;
489 fChangeCached = false;
491 nWatchDebitCached = 0;
493 nWatchCreditCached = 0;
494 nAvailableCreditCached = 0;
495 nAvailableWatchCreditCached = 0;
496 nImmatureCreditCached = 0;
497 nImmatureWatchCreditCached = 0;
504 CWalletTx* pthis = const_cast<CWalletTx*>(this);
511 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
514 BOOST_FOREACH(char f, vfSpent)
516 str += (f ? '1' : '0');
520 pthis->mapValue["spent"] = str;
522 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
525 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
528 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
531 READWRITE(vOrderForm);
532 READWRITE(fTimeReceivedIsTxTime);
533 READWRITE(nTimeReceived);
539 pthis->strFromAccount = pthis->mapValue["fromaccount"];
541 if (mapValue.count("spent"))
542 BOOST_FOREACH(char c, pthis->mapValue["spent"])
543 pthis->vfSpent.push_back(c != '0');
545 pthis->vfSpent.assign(vout.size(), fSpent);
547 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
549 pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
552 pthis->mapValue.erase("fromaccount");
553 pthis->mapValue.erase("version");
554 pthis->mapValue.erase("spent");
555 pthis->mapValue.erase("n");
556 pthis->mapValue.erase("timesmart");
559 // marks certain txout's as spent
560 // returns true if any update took place
561 bool UpdateSpent(const std::vector<char>& vfNewSpent)
563 bool fReturn = false;
564 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
566 if (i == vfSpent.size())
569 if (vfNewSpent[i] && !vfSpent[i])
573 fAvailableCreditCached = fAvailableWatchCreditCached = false;
579 // make sure balances are recalculated
582 fCreditCached = false;
583 fAvailableCreditCached = fAvailableWatchCreditCached = false;
584 fDebitCached = fWatchDebitCached = false;
585 fChangeCached = false;
588 void BindWallet(CWallet *pwalletIn)
594 void MarkSpent(unsigned int nOut)
596 if (nOut >= vout.size())
597 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
598 vfSpent.resize(vout.size());
601 vfSpent[nOut] = true;
602 fAvailableCreditCached = fAvailableWatchCreditCached = false;
606 void MarkUnspent(unsigned int nOut)
608 if (nOut >= vout.size())
609 throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
610 vfSpent.resize(vout.size());
613 vfSpent[nOut] = false;
614 fAvailableCreditCached = fAvailableWatchCreditCached = false;
618 bool IsSpent(unsigned int nOut) const
620 if (nOut >= vout.size())
621 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
622 if (nOut >= vfSpent.size())
624 return (!!vfSpent[nOut]);
627 int64_t GetDebit(const isminefilter& filter) const
633 if (filter & MINE_SPENDABLE)
636 nDebit += nDebitCached;
639 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
641 nDebit += nDebitCached;
644 if (filter & MINE_WATCH_ONLY)
646 if (fWatchDebitCached)
647 nDebit += nWatchDebitCached;
650 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
651 fWatchDebitCached = true;
652 nDebit += nWatchDebitCached;
659 int64_t GetCredit(const isminefilter& filter) const
661 // Must wait until coinbase is safely deep enough in the chain before valuing it
662 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
666 if (filter & MINE_SPENDABLE)
668 // GetBalance can assume transactions in mapWallet won't change
670 credit += nCreditCached;
673 nCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
674 fCreditCached = true;
675 credit += nCreditCached;
678 if (filter & MINE_WATCH_ONLY)
680 if (fWatchCreditCached)
681 credit += nWatchCreditCached;
684 nWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
685 fWatchCreditCached = true;
686 credit += nWatchCreditCached;
692 int64_t GetImmatureCredit(bool fUseCache=true) const
694 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
696 if (fUseCache && fImmatureCreditCached)
697 return nImmatureCreditCached;
698 nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
699 fImmatureCreditCached = true;
700 return nImmatureCreditCached;
706 int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const
708 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
710 if (fUseCache && fImmatureWatchCreditCached)
711 return nImmatureWatchCreditCached;
712 nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
713 fImmatureWatchCreditCached = true;
714 return nImmatureWatchCreditCached;
721 int64_t GetAvailableCredit(bool fUseCache=true) const
723 // Must wait until coinbase is safely deep enough in the chain before valuing it
724 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
728 if (fAvailableCreditCached)
729 return nAvailableCreditCached;
733 for (unsigned int i = 0; i < vout.size(); i++)
737 const CTxOut &txout = vout[i];
738 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
739 if (!MoneyRange(nCredit))
740 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
744 nAvailableCreditCached = nCredit;
745 fAvailableCreditCached = true;
750 int64_t GetAvailableWatchCredit(bool fUseCache=true) const
752 // Must wait until coinbase is safely deep enough in the chain before valuing it
753 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
757 if (fAvailableWatchCreditCached)
758 return nAvailableWatchCreditCached;
762 for (unsigned int i = 0; i < vout.size(); i++)
766 const CTxOut &txout = vout[i];
767 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
768 if (!MoneyRange(nCredit))
769 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
773 nAvailableWatchCreditCached = nCredit;
774 fAvailableWatchCreditCached = true;
779 int64_t GetChange() const
782 return nChangeCached;
783 nChangeCached = pwallet->GetChange(*this);
784 fChangeCached = true;
785 return nChangeCached;
788 void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CTxDestination, int64_t> >& listReceived,
789 std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
791 void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
792 int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
794 bool IsFromMe(const isminefilter& filter) const
796 return (GetDebit(filter) > 0);
799 bool IsTrusted() const
801 // Quick answer in most cases
804 if (GetDepthInMainChain() >= 1)
806 if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
809 // If no confirmations but it's from us, we can still
810 // consider it confirmed if all dependencies are confirmed
811 std::map<uint256, const CMerkleTx*> mapPrev;
812 std::vector<const CMerkleTx*> vWorkQueue;
813 vWorkQueue.reserve(vtxPrev.size()+1);
814 vWorkQueue.push_back(this);
815 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
817 const CMerkleTx* ptx = vWorkQueue[i];
821 if (ptx->GetDepthInMainChain() >= 1)
823 if (!pwallet->IsFromMe(*ptx))
828 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
829 mapPrev[tx.GetHash()] = &tx;
832 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
834 if (!mapPrev.count(txin.prevout.hash))
836 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
845 int64_t GetTxTime() const;
846 int GetRequestCount() const;
848 void AddSupportingTransactions(CTxDB& txdb);
850 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
851 bool AcceptWalletTransaction();
853 void RelayWalletTransaction(CTxDB& txdb);
854 void RelayWalletTransaction();
868 COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
870 tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
873 std::string ToString() const
875 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());
880 printf("%s\n", ToString().c_str());
887 /** Private key that includes an expiration date in case it never gets used. */
892 int64_t nTimeCreated;
893 int64_t nTimeExpires;
894 std::string strComment;
895 //// todo: add something to note what created it (user, getnewaddress, change)
896 //// maybe should have a map<string, string> property map
898 CWalletKey(int64_t nExpires=0)
900 nTimeCreated = (nExpires ? GetTime() : 0);
901 nTimeExpires = nExpires;
906 if (!(nType & SER_GETHASH))
908 READWRITE(vchPrivKey);
909 READWRITE(nTimeCreated);
910 READWRITE(nTimeExpires);
911 READWRITE(strComment);
920 /** Account information.
921 * Stored in wallet with key "acc"+string account name.
935 vchPubKey = CPubKey();
940 if (!(nType & SER_GETHASH))
942 READWRITE(vchPubKey);
948 /** Internal transfers.
949 * Database key is acentry<account><counter>.
951 class CAccountingEntry
954 std::string strAccount;
955 int64_t nCreditDebit;
957 std::string strOtherAccount;
958 std::string strComment;
960 int64_t nOrderPos; // position in ordered transaction list
973 strOtherAccount.clear();
980 CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
981 if (!(nType & SER_GETHASH))
983 // Note: strAccount is serialized as part of the key, not here.
984 READWRITE(nCreditDebit);
986 READWRITE(strOtherAccount);
990 WriteOrderPos(nOrderPos, me.mapValue);
992 if (!(mapValue.empty() && _ssExtra.empty()))
994 CDataStream ss(nType, nVersion);
995 ss.insert(ss.begin(), '\0');
997 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
998 me.strComment.append(ss.str());
1002 READWRITE(strComment);
1004 size_t nSepPos = strComment.find("\0", 0, 1);
1007 me.mapValue.clear();
1008 if (std::string::npos != nSepPos)
1010 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
1012 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
1014 ReadOrderPos(me.nOrderPos, me.mapValue);
1016 if (std::string::npos != nSepPos)
1017 me.strComment.erase(nSepPos);
1019 me.mapValue.erase("n");
1023 std::vector<char> _ssExtra;
1026 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);