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