Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
wallet.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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
7 
8 #include "core.h"
9 #include "crypter.h"
10 #include "key.h"
11 #include "keystore.h"
12 #include "main.h"
13 #include "ui_interface.h"
14 #include "util.h"
15 #include "walletdb.h"
16 
17 #include <algorithm>
18 #include <map>
19 #include <set>
20 #include <stdexcept>
21 #include <stdint.h>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 // Settings
27 extern int64_t nTransactionFee;
28 extern bool bSpendZeroConfChange;
29 
30 // -paytxfee default
31 static const int64_t DEFAULT_TRANSACTION_FEE = 0;
32 // -paytxfee will warn if called with a higher fee than this amount (in satoshis) per KB
33 static const int nHighTransactionFeeWarning = 0.01 * COIN;
34 
35 class CAccountingEntry;
36 class CCoinControl;
37 class COutput;
38 class CReserveKey;
39 class CScript;
40 class CWalletTx;
41 
44 {
45  FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
46 
47  FEATURE_WALLETCRYPT = 40000, // wallet encryption
48  FEATURE_COMPRPUBKEY = 60000, // compressed public keys
49 
51 };
52 
53 
55 class CKeyPool
56 {
57 public:
58  int64_t nTime;
60 
62  {
63  nTime = GetTime();
64  }
65 
66  CKeyPool(const CPubKey& vchPubKeyIn)
67  {
68  nTime = GetTime();
69  vchPubKey = vchPubKeyIn;
70  }
71 
73  (
74  if (!(nType & SER_GETHASH))
75  READWRITE(nVersion);
76  READWRITE(nTime);
77  READWRITE(vchPubKey);
78  )
79 };
80 
83 {
84 public:
85  std::string name;
86  std::string purpose;
87 
89  {
90  purpose = "unknown";
91  }
92 
93  typedef std::map<std::string, std::string> StringMap;
94  StringMap destdata;
95 };
96 
101 {
102 private:
103  bool SelectCoins(int64_t nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet, const CCoinControl *coinControl = NULL) const;
104 
106 
107  // the current wallet version: clients below this version are not able to load the wallet
109 
110  // the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
112 
113  int64_t nNextResend;
114  int64_t nLastResend;
115 
116  // Used to keep track of spent outpoints, and
117  // detect and report conflicts (double-spends or
118  // mutated transactions where the mutant gets mined).
119  typedef std::multimap<COutPoint, uint256> TxSpends;
120  TxSpends mapTxSpends;
121  void AddToSpends(const COutPoint& outpoint, const uint256& wtxid);
122  void AddToSpends(const uint256& wtxid);
123 
124  void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>);
125 
126 public:
133 
135  std::string strWalletFile;
136 
137  std::set<int64_t> setKeyPool;
138  std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
139 
140  typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
141  MasterKeyMap mapMasterKeys;
142  unsigned int nMasterKeyMaxID;
143 
145  {
146  SetNull();
147  }
148  CWallet(std::string strWalletFileIn)
149  {
150  SetNull();
151 
152  strWalletFile = strWalletFileIn;
153  fFileBacked = true;
154  }
155  void SetNull()
156  {
157  nWalletVersion = FEATURE_BASE;
158  nWalletMaxVersion = FEATURE_BASE;
159  fFileBacked = false;
160  nMasterKeyMaxID = 0;
161  pwalletdbEncryption = NULL;
162  nOrderPosNext = 0;
163  nNextResend = 0;
164  nLastResend = 0;
165  nTimeFirstKey = 0;
166  }
167 
168  std::map<uint256, CWalletTx> mapWallet;
169 
170  int64_t nOrderPosNext;
171  std::map<uint256, int> mapRequestCount;
172 
173  std::map<CTxDestination, CAddressBookData> mapAddressBook;
174 
176 
177  std::set<COutPoint> setLockedCoins;
178 
179  int64_t nTimeFirstKey;
180 
181  const CWalletTx* GetWalletTx(const uint256& hash) const;
182 
183  // check whether we are allowed to upgrade (or already support) to the named feature
184  bool CanSupportFeature(enum WalletFeature wf) { AssertLockHeld(cs_wallet); return nWalletMaxVersion >= wf; }
185 
186  void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl = NULL) const;
187  bool SelectCoinsMinConf(int64_t nTargetValue, int nConfMine, int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet) const;
188 
189  bool IsSpent(const uint256& hash, unsigned int n) const;
190 
191  bool IsLockedCoin(uint256 hash, unsigned int n) const;
192  void LockCoin(COutPoint& output);
193  void UnlockCoin(COutPoint& output);
194  void UnlockAllCoins();
195  void ListLockedCoins(std::vector<COutPoint>& vOutpts);
196 
197  // keystore implementation
198  // Generate a new key
199  CPubKey GenerateNewKey();
200  // Adds a key to the store, and saves it to disk.
201  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
202  // Adds a key to the store, without saving it to disk (used by LoadWallet)
203  bool LoadKey(const CKey& key, const CPubKey &pubkey) { return CCryptoKeyStore::AddKeyPubKey(key, pubkey); }
204  // Load metadata (used by LoadWallet)
205  bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
206 
207  bool LoadMinVersion(int nVersion) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
208 
209  // Adds an encrypted key to the store, and saves it to disk.
210  bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
211  // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
212  bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
213  bool AddCScript(const CScript& redeemScript);
214  bool LoadCScript(const CScript& redeemScript);
215 
217  bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value);
219  bool EraseDestData(const CTxDestination &dest, const std::string &key);
221  bool LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value);
223  bool GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const;
224 
225  bool Unlock(const SecureString& strWalletPassphrase);
226  bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
227  bool EncryptWallet(const SecureString& strWalletPassphrase);
228 
229  void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const;
230 
234  int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
235 
236  typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
237  typedef std::multimap<int64_t, TxPair > TxItems;
238 
243  TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
244 
245  void MarkDirty();
246  bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet=false);
247  void SyncTransaction(const uint256 &hash, const CTransaction& tx, const CBlock* pblock);
248  bool AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate);
249  void EraseFromWallet(const uint256 &hash);
250  int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
251  void ReacceptWalletTransactions();
252  void ResendWalletTransactions();
253  int64_t GetBalance() const;
254  int64_t GetUnconfirmedBalance() const;
255  int64_t GetImmatureBalance() const;
256  bool CreateTransaction(const std::vector<std::pair<CScript, int64_t> >& vecSend,
257  CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl *coinControl = NULL);
258  bool CreateTransaction(CScript scriptPubKey, int64_t nValue,
259  CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, std::string& strFailReason, const CCoinControl *coinControl = NULL);
260  bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
261  std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew);
262  std::string SendMoneyToDestination(const CTxDestination &address, int64_t nValue, CWalletTx& wtxNew);
263 
264  bool NewKeyPool();
265  bool TopUpKeyPool(unsigned int kpSize = 0);
266  int64_t AddReserveKey(const CKeyPool& keypool);
267  void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
268  void KeepKey(int64_t nIndex);
269  void ReturnKey(int64_t nIndex);
270  bool GetKeyFromPool(CPubKey &key);
271  int64_t GetOldestKeyPoolTime();
272  void GetAllReserveKeys(std::set<CKeyID>& setAddress) const;
273 
274  std::set< std::set<CTxDestination> > GetAddressGroupings();
275  std::map<CTxDestination, int64_t> GetAddressBalances();
276 
277  std::set<CTxDestination> GetAccountAddresses(std::string strAccount) const;
278 
279  bool IsMine(const CTxIn& txin) const;
280  int64_t GetDebit(const CTxIn& txin) const;
281  bool IsMine(const CTxOut& txout) const
282  {
283  return ::IsMine(*this, txout.scriptPubKey);
284  }
285  int64_t GetCredit(const CTxOut& txout) const
286  {
287  if (!MoneyRange(txout.nValue))
288  throw std::runtime_error("CWallet::GetCredit() : value out of range");
289  return (IsMine(txout) ? txout.nValue : 0);
290  }
291  bool IsChange(const CTxOut& txout) const;
292  int64_t GetChange(const CTxOut& txout) const
293  {
294  if (!MoneyRange(txout.nValue))
295  throw std::runtime_error("CWallet::GetChange() : value out of range");
296  return (IsChange(txout) ? txout.nValue : 0);
297  }
298  bool IsMine(const CTransaction& tx) const
299  {
300  BOOST_FOREACH(const CTxOut& txout, tx.vout)
301  if (IsMine(txout))
302  return true;
303  return false;
304  }
305  bool IsFromMe(const CTransaction& tx) const
306  {
307  return (GetDebit(tx) > 0);
308  }
309  int64_t GetDebit(const CTransaction& tx) const
310  {
311  int64_t nDebit = 0;
312  BOOST_FOREACH(const CTxIn& txin, tx.vin)
313  {
314  nDebit += GetDebit(txin);
315  if (!MoneyRange(nDebit))
316  throw std::runtime_error("CWallet::GetDebit() : value out of range");
317  }
318  return nDebit;
319  }
320  int64_t GetCredit(const CTransaction& tx) const
321  {
322  int64_t nCredit = 0;
323  BOOST_FOREACH(const CTxOut& txout, tx.vout)
324  {
325  nCredit += GetCredit(txout);
326  if (!MoneyRange(nCredit))
327  throw std::runtime_error("CWallet::GetCredit() : value out of range");
328  }
329  return nCredit;
330  }
331  int64_t GetChange(const CTransaction& tx) const
332  {
333  int64_t nChange = 0;
334  BOOST_FOREACH(const CTxOut& txout, tx.vout)
335  {
336  nChange += GetChange(txout);
337  if (!MoneyRange(nChange))
338  throw std::runtime_error("CWallet::GetChange() : value out of range");
339  }
340  return nChange;
341  }
342  void SetBestChain(const CBlockLocator& loc);
343 
344  DBErrors LoadWallet(bool& fFirstRunRet);
345  DBErrors ZapWalletTx();
346 
347  bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& purpose);
348 
349  bool DelAddressBook(const CTxDestination& address);
350 
351  void UpdatedTransaction(const uint256 &hashTx);
352 
353  void Inventory(const uint256 &hash)
354  {
355  {
356  LOCK(cs_wallet);
357  std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
358  if (mi != mapRequestCount.end())
359  (*mi).second++;
360  }
361  }
362 
363  unsigned int GetKeyPoolSize()
364  {
365  AssertLockHeld(cs_wallet); // setKeyPool
366  return setKeyPool.size();
367  }
368 
369  bool SetDefaultKey(const CPubKey &vchPubKey);
370 
371  // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
372  bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
373 
374  // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
375  bool SetMaxVersion(int nVersion);
376 
377  // get the current wallet format (the oldest client version guaranteed to understand this wallet)
378  int GetVersion() { LOCK(cs_wallet); return nWalletVersion; }
379 
380  // Get wallet transactions that conflict with given transaction (spend same outputs)
381  std::set<uint256> GetConflicts(const uint256& txid) const;
382 
386  boost::signals2::signal<void (CWallet *wallet, const CTxDestination
387  &address, const std::string &label, bool isMine,
388  const std::string &purpose,
390 
394  boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx,
396 
398  boost::signals2::signal<void (const std::string &title, int nProgress)> ShowProgress;
399 };
400 
403 {
404 protected:
406  int64_t nIndex;
408 public:
409  CReserveKey(CWallet* pwalletIn)
410  {
411  nIndex = -1;
412  pwallet = pwalletIn;
413  }
414 
416  {
417  ReturnKey();
418  }
419 
420  void ReturnKey();
421  bool GetReservedKey(CPubKey &pubkey);
422  void KeepKey();
423 };
424 
425 
426 typedef std::map<std::string, std::string> mapValue_t;
427 
428 
429 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
430 {
431  if (!mapValue.count("n"))
432  {
433  nOrderPos = -1; // TODO: calculate elsewhere
434  return;
435  }
436  nOrderPos = atoi64(mapValue["n"].c_str());
437 }
438 
439 
440 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
441 {
442  if (nOrderPos == -1)
443  return;
444  mapValue["n"] = i64tostr(nOrderPos);
445 }
446 
447 
451 class CWalletTx : public CMerkleTx
452 {
453 private:
454  const CWallet* pwallet;
455 
456 public:
457  mapValue_t mapValue;
458  std::vector<std::pair<std::string, std::string> > vOrderForm;
459  unsigned int fTimeReceivedIsTxTime;
460  unsigned int nTimeReceived; // time received by this node
461  unsigned int nTimeSmart;
462  char fFromMe;
463  std::string strFromAccount;
464  int64_t nOrderPos; // position in ordered transaction list
465 
466  // memory only
467  mutable bool fDebitCached;
468  mutable bool fCreditCached;
469  mutable bool fImmatureCreditCached;
471  mutable bool fChangeCached;
472  mutable int64_t nDebitCached;
473  mutable int64_t nCreditCached;
474  mutable int64_t nImmatureCreditCached;
475  mutable int64_t nAvailableCreditCached;
476  mutable int64_t nChangeCached;
477 
479  {
480  Init(NULL);
481  }
482 
483  CWalletTx(const CWallet* pwalletIn)
484  {
485  Init(pwalletIn);
486  }
487 
488  CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn)
489  {
490  Init(pwalletIn);
491  }
492 
493  CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn)
494  {
495  Init(pwalletIn);
496  }
497 
498  void Init(const CWallet* pwalletIn)
499  {
500  pwallet = pwalletIn;
501  mapValue.clear();
502  vOrderForm.clear();
503  fTimeReceivedIsTxTime = false;
504  nTimeReceived = 0;
505  nTimeSmart = 0;
506  fFromMe = false;
507  strFromAccount.clear();
508  fDebitCached = false;
509  fCreditCached = false;
510  fImmatureCreditCached = false;
511  fAvailableCreditCached = false;
512  fChangeCached = false;
513  nDebitCached = 0;
514  nCreditCached = 0;
515  nImmatureCreditCached = 0;
516  nAvailableCreditCached = 0;
517  nChangeCached = 0;
518  nOrderPos = -1;
519  }
520 
522  (
523  CWalletTx* pthis = const_cast<CWalletTx*>(this);
524  if (fRead)
525  pthis->Init(NULL);
526  char fSpent = false;
527 
528  if (!fRead)
529  {
530  pthis->mapValue["fromaccount"] = pthis->strFromAccount;
531 
532  WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
533 
534  if (nTimeSmart)
535  pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
536  }
537 
538  nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
539  std::vector<CMerkleTx> vUnused; // Used to be vtxPrev
540  READWRITE(vUnused);
541  READWRITE(mapValue);
542  READWRITE(vOrderForm);
543  READWRITE(fTimeReceivedIsTxTime);
544  READWRITE(nTimeReceived);
545  READWRITE(fFromMe);
546  READWRITE(fSpent);
547 
548  if (fRead)
549  {
550  pthis->strFromAccount = pthis->mapValue["fromaccount"];
551 
552  ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
553 
554  pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
555  }
556 
557  pthis->mapValue.erase("fromaccount");
558  pthis->mapValue.erase("version");
559  pthis->mapValue.erase("spent");
560  pthis->mapValue.erase("n");
561  pthis->mapValue.erase("timesmart");
562  )
563 
564  // make sure balances are recalculated
565  void MarkDirty()
566  {
567  fCreditCached = false;
568  fAvailableCreditCached = false;
569  fDebitCached = false;
570  fChangeCached = false;
571  }
572 
573  void BindWallet(CWallet *pwalletIn)
574  {
575  pwallet = pwalletIn;
576  MarkDirty();
577  }
578 
579  int64_t GetDebit() const
580  {
581  if (vin.empty())
582  return 0;
583  if (fDebitCached)
584  return nDebitCached;
585  nDebitCached = pwallet->GetDebit(*this);
586  fDebitCached = true;
587  return nDebitCached;
588  }
589 
590  int64_t GetCredit(bool fUseCache=true) const
591  {
592  // Must wait until coinbase is safely deep enough in the chain before valuing it
593  if (IsCoinBase() && GetBlocksToMaturity() > 0)
594  return 0;
595 
596  // GetBalance can assume transactions in mapWallet won't change
597  if (fUseCache && fCreditCached)
598  return nCreditCached;
599  nCreditCached = pwallet->GetCredit(*this);
600  fCreditCached = true;
601  return nCreditCached;
602  }
603 
604  int64_t GetImmatureCredit(bool fUseCache=true) const
605  {
606  if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
607  {
608  if (fUseCache && fImmatureCreditCached)
609  return nImmatureCreditCached;
610  nImmatureCreditCached = pwallet->GetCredit(*this);
611  fImmatureCreditCached = true;
612  return nImmatureCreditCached;
613  }
614 
615  return 0;
616  }
617 
618  int64_t GetAvailableCredit(bool fUseCache=true) const
619  {
620  if (pwallet == 0)
621  return 0;
622 
623  // Must wait until coinbase is safely deep enough in the chain before valuing it
624  if (IsCoinBase() && GetBlocksToMaturity() > 0)
625  return 0;
626 
627  if (fUseCache && fAvailableCreditCached)
628  return nAvailableCreditCached;
629 
630  int64_t nCredit = 0;
631  uint256 hashTx = GetHash();
632  for (unsigned int i = 0; i < vout.size(); i++)
633  {
634  if (!pwallet->IsSpent(hashTx, i))
635  {
636  const CTxOut &txout = vout[i];
637  nCredit += pwallet->GetCredit(txout);
638  if (!MoneyRange(nCredit))
639  throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
640  }
641  }
642 
643  nAvailableCreditCached = nCredit;
644  fAvailableCreditCached = true;
645  return nCredit;
646  }
647 
648 
649  int64_t GetChange() const
650  {
651  if (fChangeCached)
652  return nChangeCached;
653  nChangeCached = pwallet->GetChange(*this);
654  fChangeCached = true;
655  return nChangeCached;
656  }
657 
658  void GetAmounts(std::list<std::pair<CTxDestination, int64_t> >& listReceived,
659  std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount) const;
660 
661  void GetAccountAmounts(const std::string& strAccount, int64_t& nReceived,
662  int64_t& nSent, int64_t& nFee) const;
663 
664  bool IsFromMe() const
665  {
666  return (GetDebit() > 0);
667  }
668 
669  bool IsTrusted() const
670  {
671  // Quick answer in most cases
672  if (!IsFinalTx(*this))
673  return false;
674  int nDepth = GetDepthInMainChain();
675  if (nDepth >= 1)
676  return true;
677  if (nDepth < 0)
678  return false;
679  if (!bSpendZeroConfChange || !IsFromMe()) // using wtx's cached debit
680  return false;
681 
682  // Trusted if all inputs are from us and are in the mempool:
683  BOOST_FOREACH(const CTxIn& txin, vin)
684  {
685  // Transactions not sent by us: not trusted
686  const CWalletTx* parent = pwallet->GetWalletTx(txin.prevout.hash);
687  if (parent == NULL)
688  return false;
689  const CTxOut& parentOut = parent->vout[txin.prevout.n];
690  if (!pwallet->IsMine(parentOut))
691  return false;
692  }
693  return true;
694  }
695 
696  bool WriteToDisk();
697 
698  int64_t GetTxTime() const;
699  int GetRequestCount() const;
700 
701  void RelayWalletTransaction();
702 
703  std::set<uint256> GetConflicts() const;
704 };
705 
706 
707 
708 
709 class COutput
710 {
711 public:
712  const CWalletTx *tx;
713  int i;
714  int nDepth;
715 
716  COutput(const CWalletTx *txIn, int iIn, int nDepthIn)
717  {
718  tx = txIn; i = iIn; nDepth = nDepthIn;
719  }
720 
721  std::string ToString() const
722  {
723  return strprintf("COutput(%s, %d, %d) [%s]", tx->GetHash().ToString().c_str(), i, nDepth, FormatMoney(tx->vout[i].nValue).c_str());
724  }
725 
726  void print() const
727  {
728  LogPrintf("%s\n", ToString().c_str());
729  }
730 };
731 
732 
733 
734 
737 {
738 public:
740  int64_t nTimeCreated;
741  int64_t nTimeExpires;
742  std::string strComment;
745 
746  CWalletKey(int64_t nExpires=0)
747  {
748  nTimeCreated = (nExpires ? GetTime() : 0);
749  nTimeExpires = nExpires;
750  }
751 
753  (
754  if (!(nType & SER_GETHASH))
755  READWRITE(nVersion);
756  READWRITE(vchPrivKey);
757  READWRITE(nTimeCreated);
758  READWRITE(nTimeExpires);
759  READWRITE(LIMITED_STRING(strComment, 65536));
760  )
761 };
762 
763 
764 
765 
766 
767 
771 class CAccount
772 {
773 public:
775 
777  {
778  SetNull();
779  }
780 
781  void SetNull()
782  {
783  vchPubKey = CPubKey();
784  }
785 
787  (
788  if (!(nType & SER_GETHASH))
789  READWRITE(nVersion);
790  READWRITE(vchPubKey);
791  )
792 };
793 
794 
795 
800 {
801 public:
802  std::string strAccount;
803  int64_t nCreditDebit;
804  int64_t nTime;
805  std::string strOtherAccount;
806  std::string strComment;
807  mapValue_t mapValue;
808  int64_t nOrderPos; // position in ordered transaction list
809  uint64_t nEntryNo;
810 
812  {
813  SetNull();
814  }
815 
816  void SetNull()
817  {
818  nCreditDebit = 0;
819  nTime = 0;
820  strAccount.clear();
821  strOtherAccount.clear();
822  strComment.clear();
823  nOrderPos = -1;
824  }
825 
827  (
828  CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
829  if (!(nType & SER_GETHASH))
830  READWRITE(nVersion);
831  // Note: strAccount is serialized as part of the key, not here.
832  READWRITE(nCreditDebit);
833  READWRITE(nTime);
834  READWRITE(LIMITED_STRING(strOtherAccount, 65536));
835 
836  if (!fRead)
837  {
838  WriteOrderPos(nOrderPos, me.mapValue);
839 
840  if (!(mapValue.empty() && _ssExtra.empty()))
841  {
842  CDataStream ss(nType, nVersion);
843  ss.insert(ss.begin(), '\0');
844  ss << mapValue;
845  ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
846  me.strComment.append(ss.str());
847  }
848  }
849 
850  READWRITE(LIMITED_STRING(strComment, 65536));
851 
852  size_t nSepPos = strComment.find("\0", 0, 1);
853  if (fRead)
854  {
855  me.mapValue.clear();
856  if (std::string::npos != nSepPos)
857  {
858  CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
859  ss >> me.mapValue;
860  me._ssExtra = std::vector<char>(ss.begin(), ss.end());
861  }
862  ReadOrderPos(me.nOrderPos, me.mapValue);
863  }
864  if (std::string::npos != nSepPos)
865  me.strComment.erase(nSepPos);
866 
867  me.mapValue.erase("n");
868  )
869 
870 private:
871  std::vector<char> _ssExtra;
872 };
873 
874 #endif
int64_t nTimeCreated
Definition: wallet.h:740
bool fChangeCached
Definition: wallet.h:471
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Definition: main.cpp:594
const CWallet * pwallet
Definition: wallet.h:454
bool IsMine(const CTransaction &tx) const
Definition: wallet.h:298
CWallet * pwallet
Definition: wallet.h:405
int64_t nNextResend
Definition: wallet.h:113
Account information.
Definition: wallet.h:771
std::set< int64_t > setKeyPool
Definition: wallet.h:137
int i
Definition: wallet.h:713
int64_t nOrderPos
Definition: wallet.h:808
void BindWallet(CWallet *pwalletIn)
Definition: wallet.h:573
unsigned int SerReadWrite(Stream &s, const T &obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
Definition: serialize.h:797
int64_t GetChange(const CTransaction &tx) const
Definition: wallet.h:331
bool IsMine(const CTxIn &txin) const
Definition: wallet.cpp:653
CPrivKey vchPrivKey
Definition: wallet.h:739
int64_t nIndex
Definition: wallet.h:406
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: core.h:459
CScript scriptPubKey
Definition: core.h:123
#define READWRITE(obj)
Definition: serialize.h:92
char fFromMe
Definition: wallet.h:462
int64_t GetChange() const
Definition: wallet.h:649
COutput(const CWalletTx *txIn, int iIn, int nDepthIn)
Definition: wallet.h:716
Definition: core.h:396
int64_t GetCredit(const CTxOut &txout) const
Definition: wallet.h:285
std::map< CTxDestination, CAddressBookData > mapAddressBook
Definition: wallet.h:173
CCriticalSection cs_wallet
Main wallet lock.
Definition: wallet.h:132
int64_t nTransactionFee
Definition: wallet.cpp:19
bool fImmatureCreditCached
Definition: wallet.h:469
std::string strFromAccount
Definition: wallet.h:463
WalletFeature
(client) version numbers for particular wallet features
Definition: wallet.h:43
CPubKey vchDefaultKey
Definition: wallet.h:175
CWalletDB * pwalletdbEncryption
Definition: wallet.h:105
std::multimap< COutPoint, uint256 > TxSpends
Definition: wallet.h:119
bool IsFromMe(const CTransaction &tx) const
Definition: wallet.h:305
int64_t nImmatureCreditCached
Definition: wallet.h:474
int64_t nOrderPos
Definition: wallet.h:464
int64_t GetDebit() const
Definition: wallet.h:579
int64_t nDebitCached
Definition: wallet.h:472
bool IsFromMe() const
Definition: wallet.h:664
int64_t GetCredit(const CTransaction &tx) const
Definition: wallet.h:320
std::string i64tostr(int64_t n)
Definition: util.h:214
uint256 GetHash() const
Definition: core.cpp:75
int64_t nTimeFirstKey
Definition: wallet.h:179
#define strprintf
Definition: util.h:116
mapValue_t mapValue
Definition: wallet.h:807
bool MoneyRange(int64_t nValue)
Definition: core.h:19
void SetNull()
Definition: wallet.h:781
CPubKey vchPubKey
Definition: wallet.h:407
int64_t GetChange(const CTxOut &txout) const
Definition: wallet.h:292
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:54
unsigned int GetKeyPoolSize()
Definition: wallet.h:363
static void ReadOrderPos(int64_t &nOrderPos, mapValue_t &mapValue)
Definition: wallet.h:429
std::map< unsigned int, CMasterKey > MasterKeyMap
Definition: wallet.h:140
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:839
std::string strComment
Definition: wallet.h:742
std::string name
Definition: wallet.h:85
~CReserveKey()
Definition: wallet.h:415
CWalletKey(int64_t nExpires=0)
Definition: wallet.h:746
int64_t nChangeCached
Definition: wallet.h:476
unsigned int n
Definition: core.h:26
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:29
Keystore which keeps the private keys encrypted.
Definition: crypter.h:113
string FormatMoney(int64_t n, bool fPlus)
Definition: util.cpp:308
bool IsTrusted() const
Definition: wallet.h:669
int64_t nTimeExpires
Definition: wallet.h:741
bool IsMine(const CTxOut &txout) const
Definition: wallet.h:281
bool CanSupportFeature(enum WalletFeature wf)
Definition: wallet.h:184
bool fAvailableCreditCached
Definition: wallet.h:470
std::string purpose
Definition: wallet.h:86
Coin Control Features.
Definition: coincontrol.h:11
CAccount()
Definition: wallet.h:776
mapValue_t mapValue
Definition: wallet.h:457
bool fDebitCached
Definition: wallet.h:467
std::multimap< int64_t, TxPair > TxItems
Definition: wallet.h:237
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
Definition: key.h:176
boost::signals2::signal< void(CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged
Wallet transaction added, removed or updated.
Definition: wallet.h:395
#define AssertLockHeld(cs)
Definition: sync.h:97
std::string strComment
Definition: wallet.h:806
bool IsSpent(const uint256 &hash, unsigned int n) const
Definition: wallet.cpp:319
int64_t nTime
Definition: wallet.h:58
int64_t nCreditCached
Definition: wallet.h:473
#define LogPrintf(...)
Definition: util.h:117
unsigned int nMasterKeyMaxID
Definition: wallet.h:142
int nDepth
Definition: wallet.h:714
uint64_t nEntryNo
Definition: wallet.h:809
ChangeType
General change type (added, updated, removed).
Definition: ui_interface.h:20
An input of a transaction.
Definition: core.h:70
const CWalletTx * GetWalletTx(const uint256 &hash) const
Definition: wallet.cpp:36
int64_t nCreditDebit
Definition: wallet.h:803
#define LOCK(cs)
Definition: sync.h:156
std::vector< CTxOut > vout
Definition: core.h:191
int GetVersion()
Definition: wallet.h:378
CKeyPool()
Definition: wallet.h:61
int64_t atoi64(const char *psz)
Definition: util.h:224
std::string ToString() const
Definition: wallet.h:721
static const int nHighTransactionFeeWarning
Definition: wallet.h:33
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:253
An encapsulated public key.
Definition: key.h:42
void Inventory(const uint256 &hash)
Definition: wallet.h:353
std::set< COutPoint > setLockedCoins
Definition: wallet.h:177
std::vector< CTxIn > vin
Definition: core.h:190
TxSpends mapTxSpends
Definition: wallet.h:120
std::map< std::string, std::string > StringMap
Definition: wallet.h:93
bool LoadKey(const CKey &key, const CPubKey &pubkey)
Definition: wallet.h:203
bool bSpendZeroConfChange
Definition: wallet.cpp:20
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: crypter.cpp:177
An output of a transaction.
Definition: core.h:119
void SetNull()
Definition: wallet.h:155
std::map< uint256, int > mapRequestCount
Definition: wallet.h:171
CAddressBookData()
Definition: wallet.h:88
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:22
int64_t GetDebit(const CTxIn &txin) const
Definition: wallet.cpp:669
unsigned int fTimeReceivedIsTxTime
Definition: wallet.h:459
int64_t GetTime()
Definition: util.cpp:1215
bool LoadMinVersion(int nVersion)
Definition: wallet.h:207
Access to the wallet database (wallet.dat)
Definition: walletdb.h:71
bool fCreditCached
Definition: wallet.h:468
int nWalletMaxVersion
Definition: wallet.h:111
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:451
std::string strWalletFile
Definition: wallet.h:135
CWalletTx(const CWallet *pwalletIn, const CMerkleTx &txIn)
Definition: wallet.h:488
CPubKey vchPubKey
Definition: wallet.h:774
static void WriteOrderPos(const int64_t &nOrderPos, mapValue_t &mapValue)
Definition: wallet.h:440
bool fFileBacked
Definition: wallet.h:134
void print() const
Definition: wallet.h:726
256-bit unsigned integer
Definition: uint256.h:531
void Init(const CWallet *pwalletIn)
Definition: wallet.h:498
CPubKey vchPubKey
Definition: wallet.h:59
static const int64_t COIN
Definition: util.h:38
CWallet(std::string strWalletFileIn)
Definition: wallet.h:148
CWallet()
Definition: wallet.h:144
boost::signals2::signal< void(CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, const std::string &purpose, ChangeType status)> NotifyAddressBookChanged
Address book entry changed.
Definition: wallet.h:389
MasterKeyMap mapMasterKeys
Definition: wallet.h:141
A key allocated from the key pool.
Definition: wallet.h:402
CWalletTx()
Definition: wallet.h:478
Address book data.
Definition: wallet.h:82
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:688
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:401
StringMap destdata
Definition: wallet.h:94
int64_t nOrderPosNext
Definition: wallet.h:170
std::string ToString() const
Definition: uint256.h:340
unsigned int nTimeSmart
Definition: wallet.h:461
void SetNull()
Definition: wallet.h:816
CWalletTx(const CWallet *pwalletIn)
Definition: wallet.h:483
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Definition: script.cpp:1448
Private key that includes an expiration date in case it never gets used.
Definition: wallet.h:736
Internal transfers.
Definition: wallet.h:799
const CWalletTx * tx
Definition: wallet.h:712
#define LIMITED_STRING(obj, n)
Definition: serialize.h:311
int nWalletVersion
Definition: wallet.h:108
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:100
CKeyPool(const CPubKey &vchPubKeyIn)
Definition: wallet.h:66
std::map< CKeyID, CKeyMetadata > mapKeyMetadata
Definition: wallet.h:138
CAccountingEntry()
Definition: wallet.h:811
CReserveKey(CWallet *pwalletIn)
Definition: wallet.h:409
std::map< uint256, CWalletTx > mapWallet
Definition: wallet.h:168
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
Definition: wallet.h:398
CWalletTx(const CWallet *pwalletIn, const CTransaction &txIn)
Definition: wallet.h:493
int64_t nLastResend
Definition: wallet.h:114
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: script.h:218
unsigned int nTimeReceived
Definition: wallet.h:460
An encapsulated private key.
Definition: key.h:179
int64_t GetAvailableCredit(bool fUseCache=true) const
Definition: wallet.h:618
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
std::pair< CWalletTx *, CAccountingEntry * > TxPair
Definition: wallet.h:236
int64_t GetDebit(const CTransaction &tx) const
Definition: wallet.h:309
COutPoint prevout
Definition: core.h:73
int64_t nAvailableCreditCached
Definition: wallet.h:475
std::string strOtherAccount
Definition: wallet.h:805
int64_t GetCredit(bool fUseCache=true) const
Definition: wallet.h:590
static const int64_t DEFAULT_TRANSACTION_FEE
Definition: wallet.h:31
int64_t GetImmatureCredit(bool fUseCache=true) const
Definition: wallet.h:604
int64_t nTime
Definition: wallet.h:804
A transaction with a merkle branch linking it to the block chain.
Definition: main.h:426
std::map< std::string, std::string > mapValue_t
Definition: wallet.h:426
int64_t nValue
Definition: core.h:122
A key pool entry.
Definition: wallet.h:55
std::vector< std::pair< std::string, std::string > > vOrderForm
Definition: wallet.h:458
uint256 hash
Definition: core.h:25
std::string strAccount
Definition: wallet.h:802