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 bool fWalletUnlockMintOnly;
22 extern bool fConfChange;
23 class CAccountingEntry;
29 /** (client) version numbers for particular wallet features */
32 FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
34 FEATURE_WALLETCRYPT = 40000, // wallet encryption
35 FEATURE_COMPRPUBKEY = 60000, // compressed public keys
36 FEATURE_LATEST = 60000
39 /** A key pool entry */
51 CKeyPool(const CPubKey& vchPubKeyIn)
54 vchPubKey = vchPubKeyIn;
59 if (!(nType & SER_GETHASH))
66 /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,
67 * and provides the ability to create new transactions.
69 class CWallet : public CCryptoKeyStore
72 bool SelectCoinsSimple(int64 nTargetValue, unsigned int nSpendTime, int nMinConf, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
73 bool SelectCoins(int64 nTargetValue, unsigned int nSpendTime, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet, const CCoinControl *coinControl=NULL) const;
75 CWalletDB *pwalletdbEncryption;
77 // the current wallet version: clients below this version are not able to load the wallet
80 // the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
81 int nWalletMaxVersion;
84 mutable CCriticalSection cs_wallet;
87 std::string strWalletFile;
89 std::set<int64> setKeyPool;
90 std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
93 typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
94 MasterKeyMap mapMasterKeys;
95 unsigned int nMasterKeyMaxID;
99 nWalletVersion = FEATURE_BASE;
100 nWalletMaxVersion = FEATURE_BASE;
103 pwalletdbEncryption = NULL;
106 CWallet(std::string strWalletFileIn)
108 nWalletVersion = FEATURE_BASE;
109 nWalletMaxVersion = FEATURE_BASE;
110 strWalletFile = strWalletFileIn;
113 pwalletdbEncryption = NULL;
117 std::map<uint256, CWalletTx> mapWallet;
119 std::map<uint256, int> mapRequestCount;
121 std::map<CTxDestination, std::string> mapAddressBook;
123 CPubKey vchDefaultKey;
126 // check whether we are allowed to upgrade (or already support) to the named feature
127 bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
129 void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf) const;
130 void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
131 bool SelectCoinsMinConf(int64 nTargetValue, unsigned int nSpendTime, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
132 // keystore implementation
133 // Generate a new key
134 CPubKey GenerateNewKey();
135 // Adds a key to the store, and saves it to disk.
136 bool AddKey(const CKey& key);
137 // Adds a key to the store, without saving it to disk (used by LoadWallet)
138 bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
139 // Load metadata (used by LoadWallet)
140 bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
142 bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
144 // Adds an encrypted key to the store, and saves it to disk.
145 bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
146 // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
147 bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
148 bool AddCScript(const CScript& redeemScript);
149 bool LoadCScript(const CScript& redeemScript) { return CCryptoKeyStore::AddCScript(redeemScript); }
151 // Adds a watch-only address to the store, and saves it to disk.
152 bool AddWatchOnly(const CScript &dest);
153 // Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
154 bool LoadWatchOnly(const CScript &dest);
156 bool Unlock(const SecureString& strWalletPassphrase);
157 bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
158 bool EncryptWallet(const SecureString& strWalletPassphrase);
160 void GetKeyBirthTimes(std::map<CKeyID, int64> &mapKeyBirth) const;
163 /** Increment the next transaction order id
164 @return next transaction order id
166 int64 IncOrderPosNext(CWalletDB *pwalletdb = NULL);
168 typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
169 typedef std::multimap<int64, TxPair > TxItems;
171 /** Get the wallet's activity log
172 @return multimap of ordered transactions and accounting entries
173 @warning Returned pointers are *only* valid within the scope of passed acentries
175 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
178 bool AddToWallet(const CWalletTx& wtxIn);
179 bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false, bool fFindBlock = false);
180 bool EraseFromWallet(uint256 hash);
181 void WalletUpdateSpent(const CTransaction& prevout, bool fBlock = false);
182 int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
183 int ScanForWalletTransaction(const uint256& hashTx);
184 void ReacceptWalletTransactions();
185 void ResendWalletTransactions();
186 int64 GetBalance() const;
187 int64 GetWatchOnlyBalance() const;
188 int64 GetUnconfirmedBalance() const;
189 int64 GetUnconfirmedWatchOnlyBalance() const;
190 int64 GetImmatureBalance() const;
191 int64 GetImmatureWatchOnlyBalance() const;
192 int64 GetStake() const;
193 int64 GetNewMint() const;
194 int64 GetWatchOnlyStake() const;
195 int64 GetWatchOnlyNewMint() const;
196 bool CreateTransaction(const std::vector<std::pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl *coinControl=NULL);
197 bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, const CCoinControl *coinControl=NULL);
198 bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
200 bool GetStakeWeight(const CKeyStore& keystore, uint64& nMinWeight, uint64& nMaxWeight, uint64& nWeight);
201 void GetStakeWeightFromValue(const int64& nTime, const int64& nValue, uint64& nWeight);
202 bool CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64 nSearchInterval, CTransaction& txNew, CKey& key);
203 bool MergeCoins(const int64& nAmount, const int64& nMaxValue, const int64& nOutputValue, list<uint256>& listMerged);
205 std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
206 std::string SendMoneyToDestination(const CTxDestination &address, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
209 bool TopUpKeyPool(unsigned int nSize = 0);
210 int64 AddReserveKey(const CKeyPool& keypool);
211 void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool);
212 void KeepKey(int64 nIndex);
213 void ReturnKey(int64 nIndex);
214 bool GetKeyFromPool(CPubKey &key, bool fAllowReuse=true);
215 int64 GetOldestKeyPoolTime();
216 void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
218 std::set< std::set<CTxDestination> > GetAddressGroupings();
219 std::map<CTxDestination, int64> GetAddressBalances();
221 isminetype IsMine(const CTxIn& txin) const;
222 int64 GetDebit(const CTxIn& txin, const isminefilter& filter) const;
223 isminetype IsMine(const CTxOut& txout) const
225 return ::IsMine(*this, txout.scriptPubKey);
227 int64 GetCredit(const CTxOut& txout, const isminefilter& filter) const
229 if (!MoneyRange(txout.nValue))
230 throw std::runtime_error("CWallet::GetCredit() : value out of range");
231 return (IsMine(txout) & filter ? txout.nValue : 0);
233 bool IsChange(const CTxOut& txout) const;
234 int64 GetChange(const CTxOut& txout) const
236 if (!MoneyRange(txout.nValue))
237 throw std::runtime_error("CWallet::GetChange() : value out of range");
238 return (IsChange(txout) ? txout.nValue : 0);
240 bool IsMine(const CTransaction& tx) const
242 BOOST_FOREACH(const CTxOut& txout, tx.vout)
243 if (IsMine(txout) && txout.nValue >= nMinimumInputValue)
247 bool IsFromMe(const CTransaction& tx) const
249 return (GetDebit(tx, MINE_ALL) > 0);
251 int64 GetDebit(const CTransaction& tx, const isminefilter& filter) const
254 BOOST_FOREACH(const CTxIn& txin, tx.vin)
256 nDebit += GetDebit(txin, filter);
257 if (!MoneyRange(nDebit))
258 throw std::runtime_error("CWallet::GetDebit() : value out of range");
262 int64 GetCredit(const CTransaction& tx, const isminefilter& filter) const
265 BOOST_FOREACH(const CTxOut& txout, tx.vout)
267 nCredit += GetCredit(txout, filter);
268 if (!MoneyRange(nCredit))
269 throw std::runtime_error("CWallet::GetCredit() : value out of range");
273 int64 GetChange(const CTransaction& tx) const
276 BOOST_FOREACH(const CTxOut& txout, tx.vout)
278 nChange += GetChange(txout);
279 if (!MoneyRange(nChange))
280 throw std::runtime_error("CWallet::GetChange() : value out of range");
284 void SetBestChain(const CBlockLocator& loc);
286 DBErrors LoadWallet(bool& fFirstRunRet);
288 bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
290 bool DelAddressBookName(const CTxDestination& address);
292 void UpdatedTransaction(const uint256 &hashTx);
294 void PrintWallet(const CBlock& block);
296 void Inventory(const uint256 &hash)
300 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
301 if (mi != mapRequestCount.end())
306 unsigned int GetKeyPoolSize()
308 return setKeyPool.size();
311 bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
313 bool SetDefaultKey(const CPubKey &vchPubKey);
315 // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
316 bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
318 // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
319 bool SetMaxVersion(int nVersion);
321 // get the current wallet format (the oldest client version guaranteed to understand this wallet)
322 int GetVersion() { return nWalletVersion; }
324 void FixSpentCoins(int& nMismatchSpent, int64& nBalanceInQuestion, bool fCheckOnly = false);
325 void DisableTransaction(const CTransaction &tx);
327 /** Address book entry changed.
328 * @note called with lock cs_wallet held.
330 boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
332 /** Wallet transaction added, removed or updated.
333 * @note called with lock cs_wallet held.
335 boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
338 /** A key allocated from the key pool. */
346 CReserveKey(CWallet* pwalletIn)
359 CPubKey GetReservedKey();
364 typedef std::map<std::string, std::string> mapValue_t;
367 static void ReadOrderPos(int64& nOrderPos, mapValue_t& mapValue)
369 if (!mapValue.count("n"))
371 nOrderPos = -1; // TODO: calculate elsewhere
374 nOrderPos = atoi64(mapValue["n"].c_str());
378 static void WriteOrderPos(const int64& nOrderPos, mapValue_t& mapValue)
382 mapValue["n"] = i64tostr(nOrderPos);
386 /** A transaction with a bunch of additional info that only the owner cares about.
387 * It includes any unrecorded transactions needed to link it back to the block chain.
389 class CWalletTx : public CMerkleTx
392 const CWallet* pwallet;
395 std::vector<CMerkleTx> vtxPrev;
397 std::vector<std::pair<std::string, std::string> > vOrderForm;
398 unsigned int fTimeReceivedIsTxTime;
399 unsigned int nTimeReceived; // time received by this node
400 unsigned int nTimeSmart;
402 std::string strFromAccount;
403 std::vector<char> vfSpent; // which outputs are already spent
404 int64 nOrderPos; // position in ordered transaction list
407 mutable bool fDebitCached;
408 mutable bool fWatchDebitCached;
409 mutable bool fCreditCached;
410 mutable bool fWatchCreditCached;
411 mutable bool fAvailableCreditCached;
412 mutable bool fImmatureCreditCached;
413 mutable bool fImmatureWatchCreditCached;
414 mutable bool fAvailableWatchCreditCached;
415 mutable bool fChangeCached;
416 mutable int64 nDebitCached;
417 mutable int64 nWatchDebitCached;
418 mutable int64 nCreditCached;
419 mutable int64 nWatchCreditCached;
420 mutable int64 nAvailableCreditCached;
421 mutable int64 nImmatureCreditCached;
422 mutable int64 nImmatureWatchCreditCached;
423 mutable int64 nAvailableWatchCreditCached;
424 mutable int64 nChangeCached;
431 CWalletTx(const CWallet* pwalletIn)
436 CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
441 CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
446 void Init(const CWallet* pwalletIn)
452 fTimeReceivedIsTxTime = false;
456 strFromAccount.clear();
458 fDebitCached = false;
459 fWatchDebitCached = false;
460 fCreditCached = false;
461 fWatchCreditCached = false;
462 fAvailableCreditCached = false;
463 fAvailableWatchCreditCached = false;
464 fChangeCached = false;
466 nWatchDebitCached = 0;
468 nWatchCreditCached = 0;
469 nAvailableCreditCached = 0;
470 nAvailableWatchCreditCached = 0;
477 CWalletTx* pthis = const_cast<CWalletTx*>(this);
484 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
487 BOOST_FOREACH(char f, vfSpent)
489 str += (f ? '1' : '0');
493 pthis->mapValue["spent"] = str;
495 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
498 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
501 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
504 READWRITE(vOrderForm);
505 READWRITE(fTimeReceivedIsTxTime);
506 READWRITE(nTimeReceived);
512 pthis->strFromAccount = pthis->mapValue["fromaccount"];
514 if (mapValue.count("spent"))
515 BOOST_FOREACH(char c, pthis->mapValue["spent"])
516 pthis->vfSpent.push_back(c != '0');
518 pthis->vfSpent.assign(vout.size(), fSpent);
520 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
522 pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
525 pthis->mapValue.erase("fromaccount");
526 pthis->mapValue.erase("version");
527 pthis->mapValue.erase("spent");
528 pthis->mapValue.erase("n");
529 pthis->mapValue.erase("timesmart");
532 // marks certain txout's as spent
533 // returns true if any update took place
534 bool UpdateSpent(const std::vector<char>& vfNewSpent)
536 bool fReturn = false;
537 for (unsigned int i = 0; i < vfNewSpent.size(); i++)
539 if (i == vfSpent.size())
542 if (vfNewSpent[i] && !vfSpent[i])
546 fAvailableCreditCached = fAvailableWatchCreditCached = false;
552 // make sure balances are recalculated
555 fCreditCached = false;
556 fAvailableCreditCached = fAvailableWatchCreditCached = false;
557 fDebitCached = fWatchDebitCached = false;
558 fChangeCached = false;
561 void BindWallet(CWallet *pwalletIn)
567 void MarkSpent(unsigned int nOut)
569 if (nOut >= vout.size())
570 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
571 vfSpent.resize(vout.size());
574 vfSpent[nOut] = true;
575 fAvailableCreditCached = fAvailableWatchCreditCached = false;
579 void MarkUnspent(unsigned int nOut)
581 if (nOut >= vout.size())
582 throw std::runtime_error("CWalletTx::MarkUnspent() : nOut out of range");
583 vfSpent.resize(vout.size());
586 vfSpent[nOut] = false;
587 fAvailableCreditCached = fAvailableWatchCreditCached = false;
591 bool IsSpent(unsigned int nOut) const
593 if (nOut >= vout.size())
594 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
595 if (nOut >= vfSpent.size())
597 return (!!vfSpent[nOut]);
600 int64 GetDebit(const isminefilter& filter) const
606 if (filter & MINE_SPENDABLE)
609 nDebit += nDebitCached;
612 nDebitCached = pwallet->GetDebit(*this, MINE_SPENDABLE);
614 nDebit += nDebitCached;
617 if (filter & MINE_WATCH_ONLY)
619 if (fWatchDebitCached)
620 nDebit += nWatchDebitCached;
623 nWatchDebitCached = pwallet->GetDebit(*this, MINE_WATCH_ONLY);
624 fWatchDebitCached = true;
625 nDebit += nWatchDebitCached;
632 int64 GetCredit(bool fUseCache=true) const
634 // Must wait until coinbase is safely deep enough in the chain before valuing it
635 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
638 // GetBalance can assume transactions in mapWallet won't change
641 return nCreditCached;
644 nCreditCached = pwallet->GetCredit(*this, MINE_ALL);
645 fCreditCached = true;
647 return nCreditCached;
650 int64 GetImmatureCredit(bool fUseCache=true) const
652 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
654 if (fUseCache && fImmatureCreditCached)
655 return nImmatureCreditCached;
656 nImmatureCreditCached = pwallet->GetCredit(*this, MINE_SPENDABLE);
657 fImmatureCreditCached = true;
658 return nImmatureCreditCached;
664 int64 GetImmatureWatchOnlyCredit(bool fUseCache=true) const
666 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
668 if (fUseCache && fImmatureWatchCreditCached)
669 return nImmatureWatchCreditCached;
670 nImmatureWatchCreditCached = pwallet->GetCredit(*this, MINE_WATCH_ONLY);
671 fImmatureWatchCreditCached = true;
672 return nImmatureWatchCreditCached;
679 int64 GetAvailableCredit(bool fUseCache=true) const
681 // Must wait until coinbase is safely deep enough in the chain before valuing it
682 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
686 if (fAvailableCreditCached)
687 return nAvailableCreditCached;
691 for (unsigned int i = 0; i < vout.size(); i++)
695 const CTxOut &txout = vout[i];
696 nCredit += pwallet->GetCredit(txout, MINE_SPENDABLE);
697 if (!MoneyRange(nCredit))
698 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
702 nAvailableCreditCached = nCredit;
703 fAvailableCreditCached = true;
708 int64 GetAvailableWatchCredit(bool fUseCache=true) const
710 // Must wait until coinbase is safely deep enough in the chain before valuing it
711 if ((IsCoinBase() || IsCoinStake()) && GetBlocksToMaturity() > 0)
715 if (fAvailableWatchCreditCached)
716 return nAvailableWatchCreditCached;
720 for (unsigned int i = 0; i < vout.size(); i++)
724 const CTxOut &txout = vout[i];
725 nCredit += pwallet->GetCredit(txout, MINE_WATCH_ONLY);
726 if (!MoneyRange(nCredit))
727 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
731 nAvailableWatchCreditCached = nCredit;
732 fAvailableWatchCreditCached = true;
737 int64 GetChange() const
740 return nChangeCached;
741 nChangeCached = pwallet->GetChange(*this);
742 fChangeCached = true;
743 return nChangeCached;
746 void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list<std::pair<CTxDestination, int64> >& listReceived,
747 std::list<std::pair<CTxDestination, int64> >& listSent, int64& nFee, std::string& strSentAccount, const isminefilter& filter) const;
749 void GetAccountAmounts(const std::string& strAccount, int64& nGenerated, int64& nReceived,
750 int64& nSent, int64& nFee, const isminefilter& filter) const;
752 bool IsFromMe(const isminefilter& filter) const
754 return (GetDebit(filter) > 0);
757 bool IsTrusted() const
759 // Quick answer in most cases
762 if (GetDepthInMainChain() >= 1)
764 if (fConfChange || !IsFromMe(MINE_ALL)) // using wtx's cached debit
767 // If no confirmations but it's from us, we can still
768 // consider it confirmed if all dependencies are confirmed
769 std::map<uint256, const CMerkleTx*> mapPrev;
770 std::vector<const CMerkleTx*> vWorkQueue;
771 vWorkQueue.reserve(vtxPrev.size()+1);
772 vWorkQueue.push_back(this);
773 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
775 const CMerkleTx* ptx = vWorkQueue[i];
779 if (ptx->GetDepthInMainChain() >= 1)
781 if (!pwallet->IsFromMe(*ptx))
786 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
787 mapPrev[tx.GetHash()] = &tx;
790 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
792 if (!mapPrev.count(txin.prevout.hash))
794 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
803 int64 GetTxTime() const;
804 int GetRequestCount() const;
806 void AddSupportingTransactions(CTxDB& txdb);
808 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
809 bool AcceptWalletTransaction();
811 void RelayWalletTransaction(CTxDB& txdb);
812 void RelayWalletTransaction();
826 COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
828 tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
831 std::string ToString() const
833 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());
838 printf("%s\n", ToString().c_str());
845 /** Private key that includes an expiration date in case it never gets used. */
852 std::string strComment;
853 //// todo: add something to note what created it (user, getnewaddress, change)
854 //// maybe should have a map<string, string> property map
856 CWalletKey(int64 nExpires=0)
858 nTimeCreated = (nExpires ? GetTime() : 0);
859 nTimeExpires = nExpires;
864 if (!(nType & SER_GETHASH))
866 READWRITE(vchPrivKey);
867 READWRITE(nTimeCreated);
868 READWRITE(nTimeExpires);
869 READWRITE(strComment);
878 /** Account information.
879 * Stored in wallet with key "acc"+string account name.
893 vchPubKey = CPubKey();
898 if (!(nType & SER_GETHASH))
900 READWRITE(vchPubKey);
906 /** Internal transfers.
907 * Database key is acentry<account><counter>.
909 class CAccountingEntry
912 std::string strAccount;
915 std::string strOtherAccount;
916 std::string strComment;
918 int64 nOrderPos; // position in ordered transaction list
931 strOtherAccount.clear();
938 CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
939 if (!(nType & SER_GETHASH))
941 // Note: strAccount is serialized as part of the key, not here.
942 READWRITE(nCreditDebit);
944 READWRITE(strOtherAccount);
948 WriteOrderPos(nOrderPos, me.mapValue);
950 if (!(mapValue.empty() && _ssExtra.empty()))
952 CDataStream ss(nType, nVersion);
953 ss.insert(ss.begin(), '\0');
955 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
956 me.strComment.append(ss.str());
960 READWRITE(strComment);
962 size_t nSepPos = strComment.find("\0", 0, 1);
966 if (std::string::npos != nSepPos)
968 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
970 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
972 ReadOrderPos(me.nOrderPos, me.mapValue);
974 if (std::string::npos != nSepPos)
975 me.strComment.erase(nSepPos);
977 me.mapValue.erase("n");
981 std::vector<char> _ssExtra;
984 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);