Preserve key compression attribute while initializing a new instance of CKey.
[novacoin.git] / src / walletdb.cpp
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
6 #include "walletdb.h"
7 #include "wallet.h"
8 #include "base58.h"
9
10 #include <iostream>
11 #include <fstream>
12
13 #include <boost/version.hpp>
14 #include <boost/filesystem.hpp>
15 #include <boost/algorithm/string.hpp>
16
17 #include <boost/date_time/posix_time/posix_time.hpp>
18 #include <boost/lexical_cast.hpp>
19 #include <boost/variant/get.hpp>
20 #include <boost/algorithm/string.hpp>
21
22 using namespace std;
23 using namespace boost;
24
25
26 static uint64_t nAccountingEntryNumber = 0;
27 extern bool fWalletUnlockMintOnly;
28
29 //
30 // CWalletDB
31 //
32
33 bool CWalletDB::WriteName(const string& strAddress, const string& strName)
34 {
35     nWalletDBUpdated++;
36     return Write(make_pair(string("name"), strAddress), strName);
37 }
38
39 bool CWalletDB::EraseName(const string& strAddress)
40 {
41     // This should only be used for sending addresses, never for receiving addresses,
42     // receiving addresses must always have an address book entry if they're not change return.
43     nWalletDBUpdated++;
44     return Erase(make_pair(string("name"), strAddress));
45 }
46
47 bool CWalletDB::ReadAccount(const string& strAccount, CAccount& account)
48 {
49     account.SetNull();
50     return Read(make_pair(string("acc"), strAccount), account);
51 }
52
53 bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account)
54 {
55     return Write(make_pair(string("acc"), strAccount), account);
56 }
57
58 bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
59 {
60     return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
61 }
62
63 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
64 {
65     return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
66 }
67
68 int64_t CWalletDB::GetAccountCreditDebit(const string& strAccount)
69 {
70     list<CAccountingEntry> entries;
71     ListAccountCreditDebit(strAccount, entries);
72
73     int64_t nCreditDebit = 0;
74     BOOST_FOREACH (const CAccountingEntry& entry, entries)
75         nCreditDebit += entry.nCreditDebit;
76
77     return nCreditDebit;
78 }
79
80 void CWalletDB::ListAccountCreditDebit(const string& strAccount, list<CAccountingEntry>& entries)
81 {
82     bool fAllAccounts = (strAccount == "*");
83
84     Dbc* pcursor = GetCursor();
85     if (!pcursor)
86         throw runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
87     unsigned int fFlags = DB_SET_RANGE;
88     for ( ; ; )
89     {
90         // Read next record
91         CDataStream ssKey(SER_DISK, CLIENT_VERSION);
92         if (fFlags == DB_SET_RANGE)
93             ssKey << boost::make_tuple(string("acentry"), (fAllAccounts? string("") : strAccount), uint64_t(0));
94         CDataStream ssValue(SER_DISK, CLIENT_VERSION);
95         int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
96         fFlags = DB_NEXT;
97         if (ret == DB_NOTFOUND)
98             break;
99         else if (ret != 0)
100         {
101             pcursor->close();
102             throw runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
103         }
104
105         // Unserialize
106         string strType;
107         ssKey >> strType;
108         if (strType != "acentry")
109             break;
110         CAccountingEntry acentry;
111         ssKey >> acentry.strAccount;
112         if (!fAllAccounts && acentry.strAccount != strAccount)
113             break;
114
115         ssValue >> acentry;
116         ssKey >> acentry.nEntryNo;
117         entries.push_back(acentry);
118     }
119
120     pcursor->close();
121 }
122
123
124 DBErrors
125 CWalletDB::ReorderTransactions(CWallet* pwallet)
126 {
127     LOCK(pwallet->cs_wallet);
128     // Old wallets didn't have any defined order for transactions
129     // Probably a bad idea to change the output of this
130
131     // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
132     typedef pair<CWalletTx*, CAccountingEntry*> TxPair;
133     typedef multimap<int64_t, TxPair > TxItems;
134     TxItems txByTime;
135
136     for (map<uint256, CWalletTx>::iterator it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
137     {
138         CWalletTx* wtx = &((*it).second);
139         txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
140     }
141     list<CAccountingEntry> acentries;
142     ListAccountCreditDebit("", acentries);
143     BOOST_FOREACH(CAccountingEntry& entry, acentries)
144     {
145         txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry)));
146     }
147
148     int64_t& nOrderPosNext = pwallet->nOrderPosNext;
149     nOrderPosNext = 0;
150     std::vector<int64_t> nOrderPosOffsets;
151     for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it)
152     {
153         CWalletTx *const pwtx = (*it).second.first;
154         CAccountingEntry *const pacentry = (*it).second.second;
155         int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos;
156
157         if (nOrderPos == -1)
158         {
159             nOrderPos = nOrderPosNext++;
160             nOrderPosOffsets.push_back(nOrderPos);
161
162             if (pacentry)
163                 // Have to write accounting regardless, since we don't keep it in memory
164                 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
165                     return DB_LOAD_FAIL;
166         }
167         else
168         {
169             int64_t nOrderPosOff = 0;
170             BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets)
171             {
172                 if (nOrderPos >= nOffsetStart)
173                     ++nOrderPosOff;
174             }
175             nOrderPos += nOrderPosOff;
176             nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
177
178             if (!nOrderPosOff)
179                 continue;
180
181             // Since we're changing the order, write it back
182             if (pwtx)
183             {
184                 if (!WriteTx(pwtx->GetHash(), *pwtx))
185                     return DB_LOAD_FAIL;
186             }
187             else
188                 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
189                     return DB_LOAD_FAIL;
190         }
191     }
192
193     return DB_LOAD_OK;
194 }
195
196 class CWalletScanState {
197 public:
198     unsigned int nKeys;
199     unsigned int nCKeys;
200     unsigned int nKeyMeta;
201     bool fIsEncrypted;
202     bool fAnyUnordered;
203     int nFileVersion;
204     vector<uint256> vWalletUpgrade;
205
206     CWalletScanState() {
207         nKeys = nCKeys = nKeyMeta = 0;
208         fIsEncrypted = false;
209         fAnyUnordered = false;
210         nFileVersion = 0;
211     }
212 };
213
214 bool
215 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
216              CWalletScanState &wss, string& strType, string& strErr)
217 {
218     try {
219         // Unserialize
220         // Taking advantage of the fact that pair serialization
221         // is just the two items serialized one after the other
222         ssKey >> strType;
223
224         if (strType == "name")
225         {
226             string strAddress;
227             ssKey >> strAddress;
228             ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress)];
229         }
230         else if (strType == "tx")
231         {
232             uint256 hash;
233             ssKey >> hash;
234             CWalletTx& wtx = pwallet->mapWallet[hash];
235             ssValue >> wtx;
236             if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
237                 wtx.BindWallet(pwallet);
238             else
239             {
240                 pwallet->mapWallet.erase(hash);
241                 return false;
242             }
243
244             // Undo serialize changes in 31600
245             if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
246             {
247                 if (!ssValue.empty())
248                 {
249                     char fTmp;
250                     char fUnused;
251                     ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
252                     strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
253                                        wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
254                     wtx.fTimeReceivedIsTxTime = fTmp;
255                 }
256                 else
257                 {
258                     strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
259                     wtx.fTimeReceivedIsTxTime = 0;
260                 }
261                 wss.vWalletUpgrade.push_back(hash);
262             }
263
264             if (wtx.nOrderPos == -1)
265                 wss.fAnyUnordered = true;
266
267             //// debug print
268             //printf("LoadWallet  %s\n", wtx.GetHash().ToString().c_str());
269             //printf(" %12"PRId64"  %s  %s  %s\n",
270             //    wtx.vout[0].nValue,
271             //    DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
272             //    wtx.hashBlock.ToString().substr(0,20).c_str(),
273             //    wtx.mapValue["message"].c_str());
274         }
275         else if (strType == "acentry")
276         {
277             string strAccount;
278             ssKey >> strAccount;
279             uint64_t nNumber;
280             ssKey >> nNumber;
281             if (nNumber > nAccountingEntryNumber)
282                 nAccountingEntryNumber = nNumber;
283
284             if (!wss.fAnyUnordered)
285             {
286                 CAccountingEntry acentry;
287                 ssValue >> acentry;
288                 if (acentry.nOrderPos == -1)
289                     wss.fAnyUnordered = true;
290             }
291         }
292         else if (strType == "watchs")
293         {
294             CScript script;
295             ssKey >> script;
296             char fYes;
297             ssValue >> fYes;
298             if (fYes == '1')
299                 pwallet->LoadWatchOnly(script);
300
301             // Watch-only addresses have no birthday information for now,
302             // so set the wallet birthday to the beginning of time.
303             pwallet->nTimeFirstKey = 1;
304         }
305         else if (strType == "malpair")
306         {
307             string strKeyView;
308
309             CSecret vchSecret;
310             ssKey >> strKeyView;
311             ssValue >> vchSecret;
312
313             CMalleableKeyView keyView(strKeyView);
314             if (!pwallet->LoadKey(keyView, vchSecret))
315             {
316                 strErr = "Error reading wallet database: LoadKey failed";
317                 return false;
318             }
319         }
320         else if (strType == "malcpair")
321         {
322             string strKeyView;
323
324             std::vector<unsigned char> vchCryptedSecret;
325             ssKey >> strKeyView;
326             ssValue >> vchCryptedSecret;
327
328             CMalleableKeyView keyView(strKeyView);
329             if (!pwallet->LoadCryptedKey(keyView, vchCryptedSecret))
330             {
331                 strErr = "Error reading wallet database: LoadCryptedKey failed";
332                 return false;
333             }
334         }
335         else if (strType == "key" || strType == "wkey")
336         {
337             CKey key;
338             CPubKey vchPubKey;
339             ssKey >> vchPubKey;
340             if (strType == "key")
341             {
342                 wss.nKeys++;
343                 CPrivKey pkey;
344                 ssValue >> pkey;
345                 if (!key.SetPrivKey(pkey))
346                 {
347                     strErr = "Error reading wallet database: CPrivKey corrupt";
348                     return false;
349                 }
350                 if (key.GetPubKey() != vchPubKey)
351                 {
352                     strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
353                     return false;
354                 }
355                 key.SetCompressedPubKey(vchPubKey.IsCompressed());
356                 if (!key.IsValid())
357                 {
358                     strErr = "Error reading wallet database: invalid CPrivKey";
359                     return false;
360                 }
361             }
362             else
363             {
364                 CWalletKey wkey;
365                 ssValue >> wkey;
366                 if (!key.SetPrivKey(wkey.vchPrivKey))
367                 {
368                     strErr = "Error reading wallet database: CPrivKey corrupt";
369                     return false;
370                 }
371                 if (key.GetPubKey() != vchPubKey)
372                 {
373                     strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
374                     return false;
375                 }
376                 key.SetCompressedPubKey(vchPubKey.IsCompressed());
377                 if (!key.IsValid())
378                 {
379                     strErr = "Error reading wallet database: invalid CWalletKey";
380                     return false;
381                 }
382             }
383             if (!pwallet->LoadKey(key))
384             {
385                 strErr = "Error reading wallet database: LoadKey failed";
386                 return false;
387             }
388         }
389         else if (strType == "mkey")
390         {
391             unsigned int nID;
392             ssKey >> nID;
393             CMasterKey kMasterKey;
394             ssValue >> kMasterKey;
395
396             if(pwallet->mapMasterKeys.count(nID) != 0)
397             {
398                 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
399                 return false;
400             }
401             pwallet->mapMasterKeys[nID] = kMasterKey;
402             if (pwallet->nMasterKeyMaxID < nID)
403                 pwallet->nMasterKeyMaxID = nID;
404         }
405         else if (strType == "ckey")
406         {
407             wss.nCKeys++;
408             CPubKey vchPubKey;
409             ssKey >> vchPubKey;
410             vector<unsigned char> vchPrivKey;
411             ssValue >> vchPrivKey;
412             if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
413             {
414                 strErr = "Error reading wallet database: LoadCryptedKey failed";
415                 return false;
416             }
417             wss.fIsEncrypted = true;
418         }
419         else if (strType == "malmeta")
420         {
421             string strKeyView;
422             ssKey >> strKeyView;
423
424             CMalleableKeyView keyView;
425             keyView.SetString(strKeyView);
426
427             CKeyMetadata keyMeta;
428             ssValue >> keyMeta;
429             wss.nKeyMeta++;
430
431             pwallet->LoadKeyMetadata(keyView, keyMeta);
432         }
433         else if (strType == "keymeta")
434         {
435             CPubKey vchPubKey;
436             ssKey >> vchPubKey;
437             CKeyMetadata keyMeta;
438             ssValue >> keyMeta;
439             wss.nKeyMeta++;
440
441             pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
442
443             // find earliest key creation time, as wallet birthday
444             if (!pwallet->nTimeFirstKey ||
445                 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
446                 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
447         }
448         else if (strType == "defaultkey")
449         {
450             ssValue >> pwallet->vchDefaultKey;
451         }
452         else if (strType == "pool")
453         {
454             int64_t nIndex;
455             ssKey >> nIndex;
456             CKeyPool keypool;
457             ssValue >> keypool;
458             pwallet->setKeyPool.insert(nIndex);
459
460             // If no metadata exists yet, create a default with the pool key's
461             // creation time. Note that this may be overwritten by actually
462             // stored metadata for that key later, which is fine.
463             CBitcoinAddress addr = CBitcoinAddress(keypool.vchPubKey.GetID());
464             if (pwallet->mapKeyMetadata.count(addr) == 0)
465                 pwallet->mapKeyMetadata[addr] = CKeyMetadata(keypool.nTime);
466
467         }
468         else if (strType == "version")
469         {
470             ssValue >> wss.nFileVersion;
471             if (wss.nFileVersion == 10300)
472                 wss.nFileVersion = 300;
473         }
474         else if (strType == "cscript")
475         {
476             uint160 hash;
477             ssKey >> hash;
478             CScript script;
479             ssValue >> script;
480             if (!pwallet->LoadCScript(script))
481             {
482                 strErr = "Error reading wallet database: LoadCScript failed";
483                 return false;
484             }
485         }
486         else if (strType == "orderposnext")
487         {
488             ssValue >> pwallet->nOrderPosNext;
489         }
490     } catch (...)
491     {
492         return false;
493     }
494     return true;
495 }
496
497 static bool IsKeyType(string strType)
498 {
499     return (strType== "key" || strType == "wkey" ||
500             strType == "mkey" || strType == "ckey" || strType == "malpair" || strType == "malcpair");
501 }
502
503 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
504 {
505     pwallet->vchDefaultKey = CPubKey();
506     CWalletScanState wss;
507     bool fNoncriticalErrors = false;
508     DBErrors result = DB_LOAD_OK;
509
510     try {
511         LOCK(pwallet->cs_wallet);
512         int nMinVersion = 0;
513         if (Read((string)"minversion", nMinVersion))
514         {
515             if (nMinVersion > CLIENT_VERSION)
516                 return DB_TOO_NEW;
517             pwallet->LoadMinVersion(nMinVersion);
518         }
519
520         // Get cursor
521         Dbc* pcursor = GetCursor();
522         if (!pcursor)
523         {
524             printf("Error getting wallet database cursor\n");
525             return DB_CORRUPT;
526         }
527
528         for ( ; ; )
529         {
530             // Read next record
531             CDataStream ssKey(SER_DISK, CLIENT_VERSION);
532             CDataStream ssValue(SER_DISK, CLIENT_VERSION);
533             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
534             if (ret == DB_NOTFOUND)
535                 break;
536             else if (ret != 0)
537             {
538                 printf("Error reading next record from wallet database\n");
539                 return DB_CORRUPT;
540             }
541
542             // Try to be tolerant of single corrupt records:
543             string strType, strErr;
544             if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
545             {
546                 // losing keys is considered a catastrophic error, anything else
547                 // we assume the user can live with:
548                 if (IsKeyType(strType))
549                     result = DB_CORRUPT;
550                 else
551                 {
552                     // Leave other errors alone, if we try to fix them we might make things worse.
553                     fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
554                     if (strType == "tx")
555                         // Rescan if there is a bad transaction record:
556                         SoftSetBoolArg("-rescan", true);
557                 }
558             }
559             if (!strErr.empty())
560                 printf("%s\n", strErr.c_str());
561         }
562         pcursor->close();
563     }
564     catch (...)
565     {
566         result = DB_CORRUPT;
567     }
568
569     if (fNoncriticalErrors && result == DB_LOAD_OK)
570         result = DB_NONCRITICAL_ERROR;
571
572     // Any wallet corruption at all: skip any rewriting or
573     // upgrading, we don't want to make it worse.
574     if (result != DB_LOAD_OK)
575         return result;
576
577     printf("nFileVersion = %d\n", wss.nFileVersion);
578
579     printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
580            wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
581
582     // nTimeFirstKey is only reliable if all keys have metadata
583     if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
584         pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
585
586
587     BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade)
588         WriteTx(hash, pwallet->mapWallet[hash]);
589
590     // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
591     if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
592         return DB_NEED_REWRITE;
593
594     if (wss.nFileVersion < CLIENT_VERSION) // Update
595         WriteVersion(CLIENT_VERSION);
596
597     if (wss.fAnyUnordered)
598         result = ReorderTransactions(pwallet);
599
600     return result;
601 }
602
603 DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector<uint256>& vTxHash)
604 {
605     pwallet->vchDefaultKey = CPubKey();
606     CWalletScanState wss;
607     bool fNoncriticalErrors = false;
608     DBErrors result = DB_LOAD_OK;
609
610     try {
611         LOCK(pwallet->cs_wallet);
612         int nMinVersion = 0;
613         if (Read((string)"minversion", nMinVersion))
614         {
615             if (nMinVersion > CLIENT_VERSION)
616                 return DB_TOO_NEW;
617             pwallet->LoadMinVersion(nMinVersion);
618         }
619
620         // Get cursor
621         Dbc* pcursor = GetCursor();
622         if (!pcursor)
623         {
624             printf("Error getting wallet database cursor\n");
625             return DB_CORRUPT;
626         }
627
628         for ( ; ; )
629         {
630             // Read next record
631             CDataStream ssKey(SER_DISK, CLIENT_VERSION);
632             CDataStream ssValue(SER_DISK, CLIENT_VERSION);
633             int ret = ReadAtCursor(pcursor, ssKey, ssValue);
634             if (ret == DB_NOTFOUND)
635                 break;
636             else if (ret != 0)
637             {
638                 printf("Error reading next record from wallet database\n");
639                 return DB_CORRUPT;
640             }
641
642             string strType;
643             ssKey >> strType;
644             if (strType == "tx") {
645                 uint256 hash;
646                 ssKey >> hash;
647
648                 vTxHash.push_back(hash);
649             }
650         }
651         pcursor->close();
652     }
653     catch (const boost::thread_interrupted&) {
654         throw;
655     }
656     catch (...) {
657         result = DB_CORRUPT;
658     }
659
660     if (fNoncriticalErrors && result == DB_LOAD_OK)
661         result = DB_NONCRITICAL_ERROR;
662
663     return result;
664 }
665
666 DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet)
667 {
668     // build list of wallet TXs
669     vector<uint256> vTxHash;
670     DBErrors err = FindWalletTx(pwallet, vTxHash);
671     if (err != DB_LOAD_OK)
672         return err;
673
674     // erase each wallet TX
675     BOOST_FOREACH (uint256& hash, vTxHash) {
676         if (!EraseTx(hash))
677             return DB_CORRUPT;
678     }
679
680     return DB_LOAD_OK;
681 }
682
683 void ThreadFlushWalletDB(void* parg)
684 {
685     // Make this thread recognisable as the wallet flushing thread
686     RenameThread("novacoin-wallet");
687
688     const string& strFile = ((const string*)parg)[0];
689     static bool fOneThread;
690     if (fOneThread)
691         return;
692     fOneThread = true;
693     if (!GetBoolArg("-flushwallet", true))
694         return;
695
696     unsigned int nLastSeen = nWalletDBUpdated;
697     unsigned int nLastFlushed = nWalletDBUpdated;
698     int64_t nLastWalletUpdate = GetTime();
699     while (!fShutdown)
700     {
701         Sleep(500);
702
703         if (nLastSeen != nWalletDBUpdated)
704         {
705             nLastSeen = nWalletDBUpdated;
706             nLastWalletUpdate = GetTime();
707         }
708
709         if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
710         {
711             TRY_LOCK(bitdb.cs_db,lockDb);
712             if (lockDb)
713             {
714                 // Don't do this if any databases are in use
715                 int nRefCount = 0;
716                 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
717                 while (mi != bitdb.mapFileUseCount.end())
718                 {
719                     nRefCount += (*mi).second;
720                     mi++;
721                 }
722
723                 if (nRefCount == 0 && !fShutdown)
724                 {
725                     map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
726                     if (mi != bitdb.mapFileUseCount.end())
727                     {
728                         printf("Flushing wallet.dat\n");
729                         nLastFlushed = nWalletDBUpdated;
730                         int64_t nStart = GetTimeMillis();
731
732                         // Flush wallet.dat so it's self contained
733                         bitdb.CloseDb(strFile);
734                         bitdb.CheckpointLSN(strFile);
735
736                         bitdb.mapFileUseCount.erase(mi++);
737                         printf("Flushed wallet.dat %" PRId64 "ms\n", GetTimeMillis() - nStart);
738                     }
739                 }
740             }
741         }
742     }
743 }
744
745 bool BackupWallet(const CWallet& wallet, const string& strDest)
746 {
747     if (!wallet.fFileBacked)
748         return false;
749     while (!fShutdown)
750     {
751         {
752             LOCK(bitdb.cs_db);
753             if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
754             {
755                 // Flush log data to the dat file
756                 bitdb.CloseDb(wallet.strWalletFile);
757                 bitdb.CheckpointLSN(wallet.strWalletFile);
758                 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
759
760                 // Copy wallet.dat
761                 filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
762                 filesystem::path pathDest(strDest);
763                 if (filesystem::is_directory(pathDest))
764                     pathDest /= wallet.strWalletFile;
765
766                 try {
767 #if BOOST_VERSION >= 104000
768                     filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists);
769 #else
770                     filesystem::copy_file(pathSrc, pathDest);
771 #endif
772                     printf("copied wallet.dat to %s\n", pathDest.string().c_str());
773                     return true;
774                 } catch(const filesystem::filesystem_error &e) {
775                     printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
776                     return false;
777                 }
778             }
779         }
780         Sleep(100);
781     }
782     return false;
783 }
784
785 bool DumpWallet(CWallet* pwallet, const string& strDest)
786 {
787     if (!pwallet->fFileBacked)
788         return false;
789
790     std::map<CBitcoinAddress, int64_t> mapAddresses;
791     std::set<CKeyID> setKeyPool;
792
793     pwallet->GetAddresses(mapAddresses);
794     pwallet->GetAllReserveKeys(setKeyPool);
795
796     // sort time/key pairs
797     std::vector<std::pair<int64_t, CBitcoinAddress> > vAddresses;
798     for (std::map<CBitcoinAddress, int64_t>::const_iterator it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
799         vAddresses.push_back(std::make_pair(it->second, it->first));
800     }
801     mapAddresses.clear();
802     std::sort(vAddresses.begin(), vAddresses.end());
803
804     // open outputfile as a stream
805     ofstream file;
806     file.open(strDest.c_str());
807     if (!file.is_open())
808        return false;
809
810     // produce output
811     file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
812     file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
813     file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
814     file << strprintf("#   mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
815     file << "\n";
816
817     for (std::vector<std::pair<int64_t, CBitcoinAddress> >::const_iterator it = vAddresses.begin(); it != vAddresses.end(); it++) {
818         const CBitcoinAddress &addr = it->second;
819         std::string strTime = EncodeDumpTime(it->first);
820         std::string strAddr = addr.ToString();
821
822         if (addr.IsPair()) {
823             // Pubkey pair address
824             CMalleableKeyView keyView;
825             CMalleablePubKey mPubKey(addr.GetData());
826             if (!pwallet->GetMalleableView(mPubKey, keyView))
827                 continue;
828             CMalleableKey mKey;
829             pwallet->GetMalleableKey(keyView, mKey);
830             file << mKey.ToString();
831             if (pwallet->mapAddressBook.count(addr))
832                 file << strprintf(" %s label=%s # view=%s addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), keyView.ToString().c_str(), strAddr.c_str());
833             else
834                 file << strprintf(" %s # view=%s addr=%s\n", strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str());
835         }
836         else {
837             // Pubkey hash address
838             CKeyID keyid;
839             addr.GetKeyID(keyid);
840             bool IsCompressed;
841             CKey key;
842             if (!pwallet->GetKey(keyid, key))
843                 continue;
844             CSecret secret = key.GetSecret(IsCompressed);
845             file << CBitcoinSecret(secret, IsCompressed).ToString();
846             if (pwallet->mapAddressBook.count(addr))
847                 file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), strAddr.c_str());
848             else if (setKeyPool.count(keyid))
849                 file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
850             else
851                 file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
852         }
853     }
854
855     file << "\n";
856     file << "# End of dump\n";
857     file.close();
858
859     return true;
860 }
861
862 bool ImportWallet(CWallet *pwallet, const string& strLocation)
863 {
864
865    if (!pwallet->fFileBacked)
866        return false;
867
868    // open inputfile as stream
869    ifstream file;
870    file.open(strLocation.c_str());
871    if (!file.is_open())
872        return false;
873
874    bool fGood = true;
875    int64_t nTimeBegin = pindexBest->nTime;
876
877    // read through input file checking and importing keys into wallet.
878    while (file.good()) {
879        std::string line;
880        std::getline(file, line);
881        if (line.empty() || line[0] == '#')
882            continue; // Skip comments and empty lines
883
884        std::vector<std::string> vstr;
885        istringstream iss(line);
886        copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(vstr));
887        if (vstr.size() < 2)
888            continue;
889
890        int64_t nTime = DecodeDumpTime(vstr[1]);
891        std::string strLabel;
892        bool fLabel = true;
893        for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
894            if (boost::algorithm::starts_with(vstr[nStr], "#"))
895                break;
896            if (vstr[nStr] == "change=1")
897                fLabel = false;
898            if (vstr[nStr] == "reserve=1")
899                fLabel = false;
900            if (boost::algorithm::starts_with(vstr[nStr], "label=")) {
901                strLabel = DecodeDumpString(vstr[nStr].substr(6));
902                fLabel = true;
903            }
904        }
905
906        CBitcoinAddress addr;
907        CBitcoinSecret vchSecret;
908        if (vchSecret.SetString(vstr[0])) {
909            // Simple private key
910
911            bool fCompressed;
912            CKey key;
913            CSecret secret = vchSecret.GetSecret(fCompressed);
914            key.SetSecret(secret, fCompressed);
915            CKeyID keyid = key.GetPubKey().GetID();
916            addr = CBitcoinAddress(keyid);
917
918            if (pwallet->HaveKey(keyid)) {
919                printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
920                continue;
921            }
922
923            printf("Importing %s...\n", addr.ToString().c_str());
924            if (!pwallet->AddKey(key)) {
925                fGood = false;
926                continue;
927            }
928        } else {
929            // A pair of private keys
930
931            CMalleableKey mKey;
932            if (!mKey.SetString(vstr[0]))
933                continue;
934            CMalleablePubKey mPubKey = mKey.GetMalleablePubKey();
935            addr = CBitcoinAddress(mPubKey);
936
937            if (pwallet->CheckOwnership(mPubKey)) {
938                printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
939                continue;
940            }
941
942            printf("Importing %s...\n", addr.ToString().c_str());
943            if (!pwallet->AddKey(mKey)) {
944                fGood = false;
945                continue;
946            }
947        }
948
949        pwallet->mapKeyMetadata[addr].nCreateTime = nTime;
950        if (fLabel)
951            pwallet->SetAddressBookName(addr, strLabel);
952
953        nTimeBegin = std::min(nTimeBegin, nTime);
954    }
955    file.close();
956
957    // rescan block chain looking for coins from new keys
958    CBlockIndex *pindex = pindexBest;
959    while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
960        pindex = pindex->pprev;
961
962    printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
963    pwallet->ScanForWalletTransactions(pindex);
964    pwallet->ReacceptWalletTransactions();
965    pwallet->MarkDirty();
966
967    return fGood;
968 }
969
970 //
971 // Try to (very carefully!) recover wallet.dat if there is a problem.
972 //
973 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
974 {
975     // Recovery procedure:
976     // move wallet.dat to wallet.timestamp.bak
977     // Call Salvage with fAggressive=true to
978     // get as much data as possible.
979     // Rewrite salvaged data to wallet.dat
980     // Set -rescan so any missing transactions will be
981     // found.
982     int64_t now = GetTime();
983     std::string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
984
985     int result = dbenv.dbenv.dbrename(NULL, filename.c_str(), NULL,
986                                       newFilename.c_str(), DB_AUTO_COMMIT);
987     if (result == 0)
988         printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
989     else
990     {
991         printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
992         return false;
993     }
994
995     std::vector<CDBEnv::KeyValPair> salvagedData;
996     bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
997     if (salvagedData.empty())
998     {
999         printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
1000         return false;
1001     }
1002     printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
1003
1004     bool fSuccess = allOK;
1005     Db* pdbCopy = new Db(&dbenv.dbenv, 0);
1006     int ret = pdbCopy->open(NULL,                 // Txn pointer
1007                             filename.c_str(),   // Filename
1008                             "main",    // Logical db name
1009                             DB_BTREE,  // Database type
1010                             DB_CREATE,    // Flags
1011                             0);
1012     if (ret > 0)
1013     {
1014         printf("Cannot create database file %s\n", filename.c_str());
1015         return false;
1016     }
1017     CWallet dummyWallet;
1018     CWalletScanState wss;
1019
1020     DbTxn* ptxn = dbenv.TxnBegin();
1021     BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData)
1022     {
1023         if (fOnlyKeys)
1024         {
1025             CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
1026             CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
1027             string strType, strErr;
1028             bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
1029                                         wss, strType, strErr);
1030             if (!IsKeyType(strType))
1031                 continue;
1032             if (!fReadOK)
1033             {
1034                 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
1035                 continue;
1036             }
1037         }
1038         Dbt datKey(&row.first[0], row.first.size());
1039         Dbt datValue(&row.second[0], row.second.size());
1040         int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
1041         if (ret2 > 0)
1042             fSuccess = false;
1043     }
1044     ptxn->commit(0);
1045     pdbCopy->close(0);
1046     delete pdbCopy;
1047
1048     return fSuccess;
1049 }
1050
1051 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
1052 {
1053     return CWalletDB::Recover(dbenv, filename, false);
1054 }