Fix sign comparison warning & replace unsigned int with uint32_t for nIn.
[novacoin.git] / src / wallet.h
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
7
8 #include <string>
9 #include <vector>
10
11 #include <stdlib.h>
12
13 #include "main.h"
14 #include "key.h"
15 #include "keystore.h"
16 #include "script.h"
17 #include "ui_interface.h"
18 #include "util.h"
19 #include "walletdb.h"
20 #include "base58.h"
21
22 extern unsigned int nStakeMaxAge;
23 extern bool fWalletUnlockMintOnly;
24 extern bool fConfChange;
25 class CAccountingEntry;
26 class CWalletTx;
27 class CReserveKey;
28 class COutput;
29 class CCoinControl;
30
31 // Set of selected transactions
32 typedef std::set<std::pair<const CWalletTx*,unsigned int> > CoinsSet;
33
34 /** (client) version numbers for particular wallet features */
35 enum WalletFeature
36 {
37     FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getinfo's clientversion output)
38
39     FEATURE_WALLETCRYPT = 40000, // wallet encryption
40     FEATURE_COMPRPUBKEY = 60000, // compressed public keys
41     FEATURE_MALLKEY = 60017,
42     FEATURE_LATEST = 60017
43 };
44
45 /** A key pool entry */
46 class CKeyPool
47 {
48 public:
49     int64_t nTime;
50     CPubKey vchPubKey;
51
52     CKeyPool()
53     {
54         nTime = GetTime();
55     }
56
57     CKeyPool(const CPubKey& vchPubKeyIn)
58     {
59         nTime = GetTime();
60         vchPubKey = vchPubKeyIn;
61     }
62
63     IMPLEMENT_SERIALIZE
64     (
65         if (!(nType & SER_GETHASH))
66             READWRITE(nVersion);
67         READWRITE(nTime);
68         READWRITE(vchPubKey);
69     )
70 };
71
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.
74  */
75 class CWallet : public CCryptoKeyStore
76 {
77 private:
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;
79
80     CWalletDB *pwalletdbEncryption, *pwalletdbDecryption;
81
82     // the current wallet version: clients below this version are not able to load the wallet
83     int nWalletVersion;
84
85     // the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
86     int nWalletMaxVersion;
87
88     int64_t nNextResend;
89     int64_t nLastResend;
90
91     // stake mining statistics
92     uint64_t nKernelsTried;
93     uint64_t nCoinDaysTried;
94
95 public:
96     mutable CCriticalSection cs_wallet;
97
98     bool fFileBacked;
99     std::string strWalletFile;
100
101     std::set<int64_t> setKeyPool;
102     /*
103     std::map<CKeyID, CKeyMetadata> mapKeyMetadata;
104     std::map<CMalleableKeyView, CKeyMetadata> mapMalleableKeyMetadata;
105     */
106
107     std::map<CBitcoinAddress, CKeyMetadata> mapKeyMetadata;
108
109     typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
110     MasterKeyMap mapMasterKeys;
111     unsigned int nMasterKeyMaxID;
112
113     CWallet();
114     CWallet(std::string strWalletFileIn);
115     void SetNull();
116
117     std::map<uint256, CWalletTx> mapWallet;
118     std::vector<uint256> vMintingWalletUpdated;
119     int64_t nOrderPosNext;
120     std::map<uint256, int> mapRequestCount;
121
122     std::map<CBitcoinAddress, std::string> mapAddressBook;
123
124     CPubKey vchDefaultKey;
125     int64_t nTimeFirstKey;
126
127     const CWalletTx* GetWalletTx(const uint256& hash) const;
128
129     // check whether we are allowed to upgrade (or already support) to the named feature
130     bool CanSupportFeature(enum WalletFeature wf) { return nWalletMaxVersion >= wf; }
131
132     void AvailableCoinsMinConf(std::vector<COutput>& vCoins, int nConf, int64_t nMinValue, int64_t nMaxValue) const;
133     void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const;
134     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;
135
136     // Simple select (without randomization)
137     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;
138
139     // keystore implementation
140     // Generate a new key
141     CPubKey GenerateNewKey();
142     CMalleableKeyView GenerateNewMalleableKey();
143     // Adds a key to the store, and saves it to disk.
144     bool AddKey(const CKey& key);
145     bool AddKey(const CMalleableKey& mKey);
146     // Adds a key to the store, without saving it to disk (used by LoadWallet)
147     bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
148     // Load metadata (used by LoadWallet)
149     bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata);
150     bool LoadKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetadata &metadata);
151
152     // Load malleable key without saving it to disk (used by LoadWallet)
153     bool LoadKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) { return CCryptoKeyStore::AddMalleableKey(keyView, vchSecretH); }
154     bool LoadCryptedKey(const CMalleableKeyView &keyView, const std::vector<unsigned char> &vchCryptedSecretH) { return CCryptoKeyStore::AddCryptedMalleableKey(keyView, vchCryptedSecretH); }
155
156     bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
157
158     // Adds an encrypted key to the store, and saves it to disk.
159     bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
160     bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector<unsigned char> &vchCryptedSecretH);
161     // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
162     bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); }
163     bool AddCScript(const CScript& redeemScript);
164     bool LoadCScript(const CScript& redeemScript);
165
166     // Adds a watch-only address to the store, and saves it to disk.
167     bool AddWatchOnly(const CScript &dest);
168     bool RemoveWatchOnly(const CScript &dest);
169     // Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
170     bool LoadWatchOnly(const CScript &dest);
171
172     bool Unlock(const SecureString& strWalletPassphrase);
173     bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
174     bool EncryptWallet(const SecureString& strWalletPassphrase);
175     bool DecryptWallet(const SecureString& strWalletPassphrase);
176
177     void GetAddresses(std::map<CBitcoinAddress, int64_t> &mapAddresses) const;
178     bool GetPEM(const CKeyID &keyID, const std::string &fileName, const SecureString &strPassPhrase) const;
179
180
181     /** Increment the next transaction order id
182         @return next transaction order id
183      */
184     int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
185
186     typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
187     typedef std::multimap<int64_t, TxPair > TxItems;
188
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
192      */
193     TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount = "");
194
195     void MarkDirty();
196     bool AddToWallet(const CWalletTx& wtxIn);
197     bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false);
198     bool EraseFromWallet(uint256 hash);
199     void ClearOrphans();
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(int64_t nBestBlockTime);
205     std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime);
206     int64_t GetBalance() const;
207     int64_t GetWatchOnlyBalance() const;
208     int64_t GetUnconfirmedBalance() const;
209     int64_t GetUnconfirmedWatchOnlyBalance() const;
210     int64_t GetImmatureBalance() const;
211     int64_t GetImmatureWatchOnlyBalance() const;
212     int64_t GetStake() const;
213     int64_t GetNewMint() const;
214     int64_t GetWatchOnlyStake() const;
215     int64_t GetWatchOnlyNewMint() const;
216     bool CreateTransaction(const std::vector<std::pair<CScript, int64_t> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
217     bool CreateTransaction(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64_t& nFeeRet, const CCoinControl *coinControl=NULL);
218     bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
219
220     void GetStakeWeightFromValue(const int64_t& nTime, const int64_t& nValue, uint64_t& nWeight);
221     bool CreateCoinStake(uint256 &hashTx, uint32_t nOut, uint32_t nTime, uint32_t nBits, CTransaction &txNew, CKey& key);
222     bool MergeCoins(const int64_t& nAmount, const int64_t& nMinValue, const int64_t& nMaxValue, std::list<uint256>& listMerged);
223
224     std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx& wtxNew, bool fAskFee=false);
225
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;
235
236     std::set< std::set<CBitcoinAddress> > GetAddressGroupings();
237     std::map<CBitcoinAddress, int64_t> GetAddressBalances();
238
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;
242     int64_t GetCredit(const CTxOut& txout, const isminefilter& filter) const;
243     bool IsChange(const CTxOut& txout) const;
244     int64_t GetChange(const CTxOut& txout) const;
245     bool IsMine(const CTransaction& tx) const;
246     bool IsFromMe(const CTransaction& tx) const;
247     int64_t GetDebit(const CTransaction& tx, const isminefilter& filter) const;
248     int64_t GetCredit(const CTransaction& tx, const isminefilter& filter) const;
249     int64_t GetChange(const CTransaction& tx) const;
250     void SetBestChain(const CBlockLocator& loc);
251
252     DBErrors LoadWallet(bool& fFirstRunRet);
253     DBErrors ZapWalletTx();
254
255     bool SetAddressBookName(const CTxDestination& address, const std::string& strName);
256     bool SetAddressBookName(const CBitcoinAddress& address, const std::string& strName);
257     bool DelAddressBookName(const CBitcoinAddress& address);
258     void UpdatedTransaction(const uint256 &hashTx);
259     void PrintWallet(const CBlock& block);
260
261     void Inventory(const uint256 &hash)
262     {
263         {
264             LOCK(cs_wallet);
265             auto mi = mapRequestCount.find(hash);
266             if (mi != mapRequestCount.end())
267                 (*mi).second++;
268         }
269     }
270
271     unsigned int GetKeyPoolSize()
272     {
273         return (unsigned int)(setKeyPool.size());
274     }
275
276     bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
277     bool SetDefaultKey(const CPubKey &vchPubKey);
278
279     // signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
280     bool SetMinVersion(enum WalletFeature, CWalletDB* pwalletdbIn = NULL, bool fExplicit = false);
281
282     // change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
283     bool SetMaxVersion(int nVersion);
284
285     // get the current wallet format (the oldest client version guaranteed to understand this wallet)
286     int GetVersion() { return nWalletVersion; }
287
288     void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false);
289     void DisableTransaction(const CTransaction &tx);
290
291     /** Address book entry changed.
292      * @note called with lock cs_wallet held.
293      */
294     boost::signals2::signal<void (CWallet *wallet, const CBitcoinAddress &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged;
295
296     /** Wallet transaction added, removed or updated.
297      * @note called with lock cs_wallet held.
298      */
299     boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged;
300
301     /** Watch-only address added */
302     boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
303 };
304
305 /** A key allocated from the key pool. */
306 class CReserveKey
307 {
308 protected:
309     CWallet* pwallet;
310     int64_t nIndex;
311     CPubKey vchPubKey;
312 public:
313     CReserveKey(CWallet* pwalletIn)
314     {
315         nIndex = -1;
316         pwallet = pwalletIn;
317     }
318
319     ~CReserveKey()
320     {
321         if (!fShutdown)
322             ReturnKey();
323     }
324
325     void ReturnKey();
326     CPubKey GetReservedKey();
327     void KeepKey();
328 };
329
330
331 typedef std::map<std::string, std::string> mapValue_t;
332
333
334 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
335 {
336     if (!mapValue.count("n"))
337     {
338         nOrderPos = -1; // TODO: calculate elsewhere
339         return;
340     }
341     nOrderPos = strtoll(mapValue["n"]);
342 }
343
344
345 static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue)
346 {
347     if (nOrderPos == -1)
348         return;
349     mapValue["n"] = i64tostr(nOrderPos);
350 }
351
352
353 /** A transaction with a bunch of additional info that only the owner cares about.
354  * It includes any unrecorded transactions needed to link it back to the block chain.
355  */
356 class CWalletTx : public CMerkleTx
357 {
358 private:
359     const CWallet* pwallet;
360
361 public:
362     std::vector<CMerkleTx> vtxPrev;
363     mapValue_t mapValue;
364     std::vector<std::pair<std::string, std::string> > vOrderForm;
365     unsigned int fTimeReceivedIsTxTime;
366     unsigned int nTimeReceived;  // time received by this node
367     unsigned int nTimeSmart;
368     char fFromMe;
369     std::string strFromAccount;
370     std::vector<char> vfSpent; // which outputs are already spent
371     int64_t nOrderPos;  // position in ordered transaction list
372
373     // memory only
374     mutable bool fDebitCached;
375     mutable bool fWatchDebitCached;
376     mutable bool fCreditCached;
377     mutable bool fWatchCreditCached;
378     mutable bool fAvailableCreditCached;
379     mutable bool fImmatureCreditCached;
380     mutable bool fImmatureWatchCreditCached;
381     mutable bool fAvailableWatchCreditCached;
382     mutable bool fChangeCached;
383     mutable int64_t nDebitCached;
384     mutable int64_t nWatchDebitCached;
385     mutable int64_t nCreditCached;
386     mutable int64_t nWatchCreditCached;
387     mutable int64_t nAvailableCreditCached;
388     mutable int64_t nImmatureCreditCached;
389     mutable int64_t nImmatureWatchCreditCached;
390     mutable int64_t nAvailableWatchCreditCached;
391     mutable int64_t nChangeCached;
392
393     CWalletTx();
394     CWalletTx(const CWallet* pwalletIn);
395     CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn);
396     CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn);
397     void Init(const CWallet* pwalletIn);
398
399     IMPLEMENT_SERIALIZE
400     (
401         CWalletTx* pthis = const_cast<CWalletTx*>(this);
402         if (fRead)
403             pthis->Init(NULL);
404         char fSpent = false;
405
406         if (!fRead)
407         {
408             pthis->mapValue["fromaccount"] = pthis->strFromAccount;
409
410             std::string str;
411             for(char f :  vfSpent)
412             {
413                 str += (f ? '1' : '0');
414                 if (f)
415                     fSpent = true;
416             }
417             pthis->mapValue["spent"] = str;
418
419             WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
420
421             if (nTimeSmart)
422                 pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
423         }
424
425         nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
426         READWRITE(vtxPrev);
427         READWRITE(mapValue);
428         READWRITE(vOrderForm);
429         READWRITE(fTimeReceivedIsTxTime);
430         READWRITE(nTimeReceived);
431         READWRITE(fFromMe);
432         READWRITE(fSpent);
433
434         if (fRead)
435         {
436             pthis->strFromAccount = pthis->mapValue["fromaccount"];
437
438             if (mapValue.count("spent"))
439                 for(char c :  pthis->mapValue["spent"])
440                     pthis->vfSpent.push_back(c != '0');
441             else
442                 pthis->vfSpent.assign(vout.size(), fSpent);
443
444             ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
445
446             pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)strtoll(pthis->mapValue["timesmart"]) : 0;
447         }
448
449         pthis->mapValue.erase("fromaccount");
450         pthis->mapValue.erase("version");
451         pthis->mapValue.erase("spent");
452         pthis->mapValue.erase("n");
453         pthis->mapValue.erase("timesmart");
454     )
455
456     // marks certain txout's as spent
457     // returns true if any update took place
458     bool UpdateSpent(const std::vector<char>& vfNewSpent);
459
460     // make sure balances are recalculated
461     void MarkDirty();
462     void BindWallet(CWallet *pwalletIn);
463     void MarkSpent(unsigned int nOut);
464     void MarkUnspent(unsigned int nOut);
465     bool IsSpent(unsigned int nOut) const;
466
467     int64_t GetDebit(const isminefilter& filter) const;
468     int64_t GetCredit(const isminefilter& filter) const;
469     int64_t GetImmatureCredit(bool fUseCache=true) const;
470     int64_t GetImmatureWatchOnlyCredit(bool fUseCache=true) const;
471     int64_t GetAvailableCredit(bool fUseCache=true) const;
472     int64_t GetAvailableWatchCredit(bool fUseCache=true) const;
473     int64_t GetChange() const;
474
475     void GetAmounts(int64_t& nGeneratedImmature, int64_t& nGeneratedMature, std::list<std::pair<CBitcoinAddress, int64_t> >& listReceived,
476                     std::list<std::pair<CBitcoinAddress, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount, const isminefilter& filter) const;
477
478     void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived,
479                            int64_t& nSent, int64_t& nFee, const isminefilter& filter) const;
480
481     bool IsFromMe(const isminefilter& filter) const
482     {
483         return (GetDebit(filter) > 0);
484     }
485
486     bool InMempool() const;
487     bool IsTrusted() const;
488
489     bool WriteToDisk();
490
491     int64_t GetTxTime() const;
492     int GetRequestCount() const;
493
494     void AddSupportingTransactions(CTxDB& txdb);
495
496     bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
497     bool AcceptWalletTransaction();
498
499     bool RelayWalletTransaction(CTxDB& txdb);
500     bool RelayWalletTransaction();
501 };
502
503
504
505
506 class COutput
507 {
508 public:
509     const CWalletTx *tx;
510     int i;
511     int nDepth;
512     bool fSpendable;
513
514     COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
515     {
516         tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
517     }
518
519     std::string ToString() const
520     {
521         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());
522     }
523 };
524
525
526
527
528 /** Private key that includes an expiration date in case it never gets used. */
529 class CWalletKey
530 {
531 public:
532     CPrivKey vchPrivKey;
533     int64_t nTimeCreated;
534     int64_t nTimeExpires;
535     std::string strComment;
536     //// todo: add something to note what created it (user, getnewaddress, change)
537     ////   maybe should have a map<string, string> property map
538
539     CWalletKey(int64_t nExpires=0)
540     {
541         nTimeCreated = (nExpires ? GetTime() : 0);
542         nTimeExpires = nExpires;
543     }
544
545     IMPLEMENT_SERIALIZE
546     (
547         if (!(nType & SER_GETHASH))
548             READWRITE(nVersion);
549         READWRITE(vchPrivKey);
550         READWRITE(nTimeCreated);
551         READWRITE(nTimeExpires);
552         READWRITE(strComment);
553     )
554 };
555
556
557
558
559
560
561 /** Account information.
562  * Stored in wallet with key "acc"+string account name.
563  */
564 class CAccount
565 {
566 public:
567     CPubKey vchPubKey;
568
569     CAccount()
570     {
571         SetNull();
572     }
573
574     void SetNull()
575     {
576         vchPubKey = CPubKey();
577     }
578
579     IMPLEMENT_SERIALIZE
580     (
581         if (!(nType & SER_GETHASH))
582             READWRITE(nVersion);
583         READWRITE(vchPubKey);
584     )
585 };
586
587
588
589 /** Internal transfers.
590  * Database key is acentry<account><counter>.
591  */
592 class CAccountingEntry
593 {
594 public:
595     std::string strAccount = "";
596     int64_t nCreditDebit = 0;
597     int64_t nTime = 0;
598     std::string strOtherAccount = "";
599     std::string strComment = "";
600     mapValue_t mapValue;
601     int64_t nOrderPos = -1;  // position in ordered transaction list
602     uint64_t nEntryNo = 0;
603
604     CAccountingEntry() { }
605
606     IMPLEMENT_SERIALIZE
607     (
608         CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
609         if (!(nType & SER_GETHASH))
610             READWRITE(nVersion);
611         // Note: strAccount is serialized as part of the key, not here.
612         READWRITE(nCreditDebit);
613         READWRITE(nTime);
614         READWRITE(strOtherAccount);
615
616         if (!fRead)
617         {
618             WriteOrderPos(nOrderPos, me.mapValue);
619
620             if (!(mapValue.empty() && _ssExtra.empty()))
621             {
622                 CDataStream ss(nType, nVersion);
623                 ss.insert(ss.begin(), '\0');
624                 ss << mapValue;
625                 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
626                 me.strComment.append(ss.str());
627             }
628         }
629
630         READWRITE(strComment);
631
632         size_t nSepPos = strComment.find("\0", 0, 1);
633         if (fRead)
634         {
635             me.mapValue.clear();
636             if (std::string::npos != nSepPos)
637             {
638                 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
639                 ss >> me.mapValue;
640                 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
641             }
642             ReadOrderPos(me.nOrderPos, me.mapValue);
643         }
644         if (std::string::npos != nSepPos)
645             me.strComment.erase(nSepPos);
646
647         me.mapValue.erase("n");
648     )
649
650 private:
651     std::vector<char> _ssExtra;
652 };
653
654 bool GetWalletFile(CWallet* pwallet, std::string &strWalletFileOut);
655
656 #endif