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.
10 #include <boost/version.hpp>
11 #include <boost/filesystem.hpp>
17 static uint64_t nAccountingEntryNumber = 0;
18 extern bool fWalletUnlockMintOnly;
24 bool CWalletDB::WriteName(const std::string& strAddress, const std::string& strName)
27 return Write(make_pair(std::string("name"), strAddress), strName);
30 bool CWalletDB::EraseName(const std::string& strAddress)
32 // This should only be used for sending addresses, never for receiving addresses,
33 // receiving addresses must always have an address book entry if they're not change return.
35 return Erase(make_pair(std::string("name"), strAddress));
38 bool CWalletDB::WriteTx(uint256 hash, const CWalletTx &wtx)
41 return Write(std::make_pair(std::string("tx"), hash), wtx);
44 bool CWalletDB::EraseTx(uint256 hash)
47 return Erase(std::make_pair(std::string("tx"), hash));
50 bool CWalletDB::WriteKey(const CPubKey &key, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
53 if(!Write(std::make_pair(std::string("keymeta"), key), keyMeta))
56 if(!Write(std::make_pair(std::string("key"), key), vchPrivKey, false))
62 bool CWalletDB::WriteMalleableKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH, const CKeyMetadata &keyMeta)
65 if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
68 if(!Write(std::make_pair(std::string("malpair"), keyView.ToString()), vchSecretH, false))
74 bool CWalletDB::WriteCryptedMalleableKey(const CMalleableKeyView &keyView, const std::vector<unsigned char> &vchCryptedSecretH, const CKeyMetadata &keyMeta)
77 if(!Write(std::make_pair(std::string("malmeta"), keyView.ToString()), keyMeta))
80 if(!Write(std::make_pair(std::string("malcpair"), keyView.ToString()), vchCryptedSecretH, false))
83 Erase(std::make_pair(std::string("malpair"), keyView.ToString()));
88 bool CWalletDB::WriteCryptedKey(const CPubKey &key, const std::vector<unsigned char> &vchCryptedSecret, const CKeyMetadata &keyMeta)
91 bool fEraseUnencryptedKey = true;
93 if(!Write(std::make_pair(std::string("keymeta"), key), keyMeta))
96 if (!Write(std::make_pair(std::string("ckey"), key), vchCryptedSecret, false))
98 if (fEraseUnencryptedKey)
100 Erase(std::make_pair(std::string("key"), key));
101 Erase(std::make_pair(std::string("wkey"), key));
106 bool CWalletDB::WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
109 return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
112 bool CWalletDB::EraseMasterKey(unsigned int nID)
115 return Erase(std::make_pair(std::string("mkey"), nID));
118 bool CWalletDB::EraseCryptedKey(const CPubKey &key)
120 return Erase(std::make_pair(std::string("ckey"), key));
123 bool CWalletDB::EraseCryptedMalleableKey(const CMalleableKeyView &keyView)
125 return Erase(std::make_pair(std::string("malcpair"), keyView.ToString()));
128 bool CWalletDB::WriteCScript(const uint160 &hash, const CScript &redeemScript)
131 return Write(std::make_pair(std::string("cscript"), hash), redeemScript, false);
134 bool CWalletDB::WriteWatchOnly(const CScript &dest)
137 return Write(std::make_pair(std::string("watchs"), dest), '1');
140 bool CWalletDB::EraseWatchOnly(const CScript &dest)
143 return Erase(std::make_pair(std::string("watchs"), dest));
146 bool CWalletDB::WriteBestBlock(const CBlockLocator &locator)
149 return Write(std::string("bestblock"), locator);
152 bool CWalletDB::ReadBestBlock(CBlockLocator &locator)
154 return Read(std::string("bestblock"), locator);
157 bool CWalletDB::WriteOrderPosNext(int64_t nOrderPosNext)
160 return Write(std::string("orderposnext"), nOrderPosNext);
163 bool CWalletDB::WriteDefaultKey(const CPubKey &key)
166 return Write(std::string("defaultkey"), key);
169 bool CWalletDB::ReadPool(int64_t nPool, CKeyPool &keypool)
171 return Read(std::make_pair(std::string("pool"), nPool), keypool);
174 bool CWalletDB::WritePool(int64_t nPool, const CKeyPool &keypool)
177 return Write(std::make_pair(std::string("pool"), nPool), keypool);
180 bool CWalletDB::ErasePool(int64_t nPool)
183 return Erase(std::make_pair(std::string("pool"), nPool));
186 bool CWalletDB::WriteMinVersion(int nVersion)
188 return Write(std::string("minversion"), nVersion);
191 bool CWalletDB::ReadAccount(const std::string& strAccount, CAccount& account)
194 return Read(make_pair(std::string("acc"), strAccount), account);
197 bool CWalletDB::WriteAccount(const std::string& strAccount, const CAccount& account)
199 return Write(make_pair(std::string("acc"), strAccount), account);
202 bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
204 return Write(std::make_tuple(std::string("acentry"), acentry.strAccount, nAccEntryNum), acentry);
207 bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry)
209 return WriteAccountingEntry(++nAccountingEntryNumber, acentry);
212 int64_t CWalletDB::GetAccountCreditDebit(const std::string& strAccount)
214 std::list<CAccountingEntry> entries;
215 ListAccountCreditDebit(strAccount, entries);
217 int64_t nCreditDebit = 0;
218 for (const CAccountingEntry& entry : entries)
219 nCreditDebit += entry.nCreditDebit;
224 void CWalletDB::ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& entries)
226 bool fAllAccounts = (strAccount == "*");
228 Dbc* pcursor = GetCursor();
230 throw std::runtime_error("CWalletDB::ListAccountCreditDebit() : cannot create DB cursor");
231 unsigned int fFlags = DB_SET_RANGE;
235 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
236 if (fFlags == DB_SET_RANGE)
237 ssKey << std::make_tuple(std::string("acentry"), (fAllAccounts? std::string("") : strAccount), uint64_t(0));
238 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
239 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
241 if (ret == DB_NOTFOUND)
246 throw std::runtime_error("CWalletDB::ListAccountCreditDebit() : error scanning DB");
252 if (strType != "acentry")
254 CAccountingEntry acentry;
255 ssKey >> acentry.strAccount;
256 if (!fAllAccounts && acentry.strAccount != strAccount)
260 ssKey >> acentry.nEntryNo;
261 entries.push_back(acentry);
269 CWalletDB::ReorderTransactions(CWallet* pwallet)
271 LOCK(pwallet->cs_wallet);
272 // Old wallets didn't have any defined order for transactions
273 // Probably a bad idea to change the output of this
275 // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap.
276 typedef std::pair<CWalletTx*, CAccountingEntry*> TxPair;
277 typedef std::multimap<int64_t, TxPair > TxItems;
280 for (auto it = pwallet->mapWallet.begin(); it != pwallet->mapWallet.end(); ++it)
282 CWalletTx* wtx = &((*it).second);
283 txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)nullptr)));
285 std::list<CAccountingEntry> acentries;
286 ListAccountCreditDebit("", acentries);
287 for (CAccountingEntry& entry : acentries)
289 txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)nullptr, &entry)));
292 int64_t& nOrderPosNext = pwallet->nOrderPosNext;
294 std::vector<int64_t> nOrderPosOffsets;
295 for (auto it = txByTime.begin(); it != txByTime.end(); ++it)
297 CWalletTx *const pwtx = (*it).second.first;
298 CAccountingEntry *const pacentry = (*it).second.second;
299 int64_t& nOrderPos = (pwtx != nullptr) ? pwtx->nOrderPos : pacentry->nOrderPos;
303 nOrderPos = nOrderPosNext++;
304 nOrderPosOffsets.push_back(nOrderPos);
307 // Have to write accounting regardless, since we don't keep it in memory
308 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
313 int64_t nOrderPosOff = 0;
314 for (const int64_t& nOffsetStart : nOrderPosOffsets)
316 if (nOrderPos >= nOffsetStart)
319 nOrderPos += nOrderPosOff;
320 nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1);
325 // Since we're changing the order, write it back
328 if (!WriteTx(pwtx->GetHash(), *pwtx))
332 if (!WriteAccountingEntry(pacentry->nEntryNo, *pacentry))
340 class CWalletScanState {
344 unsigned int nKeyMeta;
348 std::vector<uint256> vWalletUpgrade;
351 nKeys = nCKeys = nKeyMeta = 0;
352 fIsEncrypted = false;
353 fAnyUnordered = false;
359 ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
360 CWalletScanState &wss, std::string& strType, std::string& strErr)
364 // Taking advantage of the fact that pair serialization
365 // is just the two items serialized one after the other
368 if (strType == "name")
370 std::string strAddress;
372 ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress)];
374 else if (strType == "tx")
378 CWalletTx& wtx = pwallet->mapWallet[hash];
380 if (wtx.CheckTransaction() && (wtx.GetHash() == hash))
381 wtx.BindWallet(pwallet);
384 pwallet->mapWallet.erase(hash);
388 // Undo serialize changes in 31600
389 if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
391 if (!ssValue.empty())
395 ssValue >> fTmp >> fUnused >> wtx.strFromAccount;
396 strErr = strprintf("LoadWallet() upgrading tx ver=%d %d '%s' %s",
397 wtx.fTimeReceivedIsTxTime, fTmp, wtx.strFromAccount.c_str(), hash.ToString().c_str());
398 wtx.fTimeReceivedIsTxTime = fTmp;
402 strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString().c_str());
403 wtx.fTimeReceivedIsTxTime = 0;
405 wss.vWalletUpgrade.push_back(hash);
408 if (wtx.nOrderPos == -1)
409 wss.fAnyUnordered = true;
412 //printf("LoadWallet %s\n", wtx.GetHash().ToString().c_str());
413 //printf(" %12"PRId64" %s %s %s\n",
414 // wtx.vout[0].nValue,
415 // DateTimeStrFormat("%x %H:%M:%S", wtx.GetBlockTime()).c_str(),
416 // wtx.hashBlock.ToString().substr(0,20).c_str(),
417 // wtx.mapValue["message"].c_str());
419 else if (strType == "acentry")
421 std::string strAccount;
425 if (nNumber > nAccountingEntryNumber)
426 nAccountingEntryNumber = nNumber;
428 if (!wss.fAnyUnordered)
430 CAccountingEntry acentry;
432 if (acentry.nOrderPos == -1)
433 wss.fAnyUnordered = true;
436 else if (strType == "watchs")
443 pwallet->LoadWatchOnly(script);
445 // Watch-only addresses have no birthday information for now,
446 // so set the wallet birthday to the beginning of time.
447 pwallet->nTimeFirstKey = 1;
449 else if (strType == "malpair")
451 std::string strKeyView;
455 ssValue >> vchSecret;
457 CMalleableKeyView keyView(strKeyView);
458 if (!pwallet->LoadKey(keyView, vchSecret))
460 strErr = "Error reading wallet database: LoadKey failed";
464 else if (strType == "malcpair")
466 std::string strKeyView;
468 std::vector<unsigned char> vchCryptedSecret;
470 ssValue >> vchCryptedSecret;
472 CMalleableKeyView keyView(strKeyView);
473 if (!pwallet->LoadCryptedKey(keyView, vchCryptedSecret))
475 strErr = "Error reading wallet database: LoadCryptedKey failed";
479 else if (strType == "key" || strType == "wkey")
484 if (strType == "key")
489 if (!key.SetPrivKey(pkey))
491 strErr = "Error reading wallet database: CPrivKey corrupt";
494 if (key.GetPubKey() != vchPubKey)
496 strErr = "Error reading wallet database: CPrivKey pubkey inconsistency";
499 key.SetCompressedPubKey(vchPubKey.IsCompressed());
502 strErr = "Error reading wallet database: invalid CPrivKey";
510 if (!key.SetPrivKey(wkey.vchPrivKey))
512 strErr = "Error reading wallet database: CPrivKey corrupt";
515 if (key.GetPubKey() != vchPubKey)
517 strErr = "Error reading wallet database: CWalletKey pubkey inconsistency";
520 key.SetCompressedPubKey(vchPubKey.IsCompressed());
523 strErr = "Error reading wallet database: invalid CWalletKey";
527 if (!pwallet->LoadKey(key))
529 strErr = "Error reading wallet database: LoadKey failed";
533 else if (strType == "mkey")
537 CMasterKey kMasterKey;
538 ssValue >> kMasterKey;
540 if(pwallet->mapMasterKeys.count(nID) != 0)
542 strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
545 pwallet->mapMasterKeys[nID] = kMasterKey;
546 if (pwallet->nMasterKeyMaxID < nID)
547 pwallet->nMasterKeyMaxID = nID;
549 else if (strType == "ckey")
554 std::vector<unsigned char> vchPrivKey;
555 ssValue >> vchPrivKey;
556 if (!pwallet->LoadCryptedKey(vchPubKey, vchPrivKey))
558 strErr = "Error reading wallet database: LoadCryptedKey failed";
561 wss.fIsEncrypted = true;
563 else if (strType == "malmeta")
565 std::string strKeyView;
568 CMalleableKeyView keyView;
569 keyView.SetString(strKeyView);
571 CKeyMetadata keyMeta;
575 pwallet->LoadKeyMetadata(keyView, keyMeta);
577 else if (strType == "keymeta")
581 CKeyMetadata keyMeta;
585 pwallet->LoadKeyMetadata(vchPubKey, keyMeta);
587 // find earliest key creation time, as wallet birthday
588 if (!pwallet->nTimeFirstKey ||
589 (keyMeta.nCreateTime < pwallet->nTimeFirstKey))
590 pwallet->nTimeFirstKey = keyMeta.nCreateTime;
592 else if (strType == "defaultkey")
594 ssValue >> pwallet->vchDefaultKey;
596 else if (strType == "pool")
602 pwallet->setKeyPool.insert(nIndex);
604 // If no metadata exists yet, create a default with the pool key's
605 // creation time. Note that this may be overwritten by actually
606 // stored metadata for that key later, which is fine.
607 CBitcoinAddress addr = CBitcoinAddress(keypool.vchPubKey.GetID());
608 if (pwallet->mapKeyMetadata.count(addr) == 0)
609 pwallet->mapKeyMetadata[addr] = CKeyMetadata(keypool.nTime);
612 else if (strType == "version")
614 ssValue >> wss.nFileVersion;
615 if (wss.nFileVersion == 10300)
616 wss.nFileVersion = 300;
618 else if (strType == "cscript")
624 if (!pwallet->LoadCScript(script))
626 strErr = "Error reading wallet database: LoadCScript failed";
630 else if (strType == "orderposnext")
632 ssValue >> pwallet->nOrderPosNext;
641 static bool IsKeyType(const std::string& strType)
643 return (strType== "key" || strType == "wkey" ||
644 strType == "mkey" || strType == "ckey" || strType == "malpair" || strType == "malcpair");
647 DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
649 pwallet->vchDefaultKey = CPubKey();
650 CWalletScanState wss;
651 bool fNoncriticalErrors = false;
652 DBErrors result = DB_LOAD_OK;
655 LOCK(pwallet->cs_wallet);
657 if (Read((std::string)"minversion", nMinVersion))
659 if (nMinVersion > CLIENT_VERSION)
661 pwallet->LoadMinVersion(nMinVersion);
665 Dbc* pcursor = GetCursor();
668 printf("Error getting wallet database cursor\n");
675 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
676 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
677 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
678 if (ret == DB_NOTFOUND)
682 printf("Error reading next record from wallet database\n");
686 // Try to be tolerant of single corrupt records:
687 std::string strType, strErr;
688 if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
690 // losing keys is considered a catastrophic error, anything else
691 // we assume the user can live with:
692 if (IsKeyType(strType))
696 // Leave other errors alone, if we try to fix them we might make things worse.
697 fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
699 // Rescan if there is a bad transaction record:
700 SoftSetBoolArg("-rescan", true);
704 printf("%s\n", strErr.c_str());
713 if (fNoncriticalErrors && result == DB_LOAD_OK)
714 result = DB_NONCRITICAL_ERROR;
716 // Any wallet corruption at all: skip any rewriting or
717 // upgrading, we don't want to make it worse.
718 if (result != DB_LOAD_OK)
721 printf("nFileVersion = %d\n", wss.nFileVersion);
723 printf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total\n",
724 wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys);
726 // nTimeFirstKey is only reliable if all keys have metadata
727 if ((wss.nKeys + wss.nCKeys) != wss.nKeyMeta)
728 pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value'
731 for (uint256 hash : wss.vWalletUpgrade)
732 WriteTx(hash, pwallet->mapWallet[hash]);
734 // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
735 if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000))
736 return DB_NEED_REWRITE;
738 if (wss.nFileVersion < CLIENT_VERSION) // Update
739 WriteVersion(CLIENT_VERSION);
741 if (wss.fAnyUnordered)
742 result = ReorderTransactions(pwallet);
747 DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, std::vector<uint256>& vTxHash)
749 pwallet->vchDefaultKey = CPubKey();
750 CWalletScanState wss;
751 bool fNoncriticalErrors = false;
752 DBErrors result = DB_LOAD_OK;
755 LOCK(pwallet->cs_wallet);
757 if (Read((std::string)"minversion", nMinVersion))
759 if (nMinVersion > CLIENT_VERSION)
761 pwallet->LoadMinVersion(nMinVersion);
765 Dbc* pcursor = GetCursor();
768 printf("Error getting wallet database cursor\n");
775 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
776 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
777 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
778 if (ret == DB_NOTFOUND)
782 printf("Error reading next record from wallet database\n");
788 if (strType == "tx") {
792 vTxHash.push_back(hash);
797 catch (const boost::thread_interrupted&) {
804 if (fNoncriticalErrors && result == DB_LOAD_OK)
805 result = DB_NONCRITICAL_ERROR;
810 DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet)
812 // build list of wallet TXs
813 std::vector<uint256> vTxHash;
814 DBErrors err = FindWalletTx(pwallet, vTxHash);
815 if (err != DB_LOAD_OK)
818 // erase each wallet TX
819 for (uint256& hash : vTxHash) {
827 void ThreadFlushWalletDB(void* parg)
829 // Make this thread recognisable as the wallet flushing thread
830 RenameThread("novacoin-wallet");
832 const std::string& strFile = ((const std::string*)parg)[0];
833 static bool fOneThread;
837 if (!GetBoolArg("-flushwallet", true))
840 unsigned int nLastSeen = nWalletDBUpdated;
841 unsigned int nLastFlushed = nWalletDBUpdated;
842 int64_t nLastWalletUpdate = GetTime();
847 if (nLastSeen != nWalletDBUpdated)
849 nLastSeen = nWalletDBUpdated;
850 nLastWalletUpdate = GetTime();
853 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
855 TRY_LOCK(bitdb.cs_db,lockDb);
858 // Don't do this if any databases are in use
860 auto mi = bitdb.mapFileUseCount.begin();
861 while (mi != bitdb.mapFileUseCount.end())
863 nRefCount += (*mi).second;
867 if (nRefCount == 0 && !fShutdown)
869 auto mi = bitdb.mapFileUseCount.find(strFile);
870 if (mi != bitdb.mapFileUseCount.end())
872 printf("Flushing wallet.dat\n");
873 nLastFlushed = nWalletDBUpdated;
874 int64_t nStart = GetTimeMillis();
876 // Flush wallet.dat so it's self contained
877 bitdb.CloseDb(strFile);
878 bitdb.CheckpointLSN(strFile);
880 bitdb.mapFileUseCount.erase(mi++);
881 printf("Flushed wallet.dat %" PRId64 "ms\n", GetTimeMillis() - nStart);
889 bool BackupWallet(const CWallet& wallet, const std::string& strDest)
891 if (!wallet.fFileBacked)
897 if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
899 // Flush log data to the dat file
900 bitdb.CloseDb(wallet.strWalletFile);
901 bitdb.CheckpointLSN(wallet.strWalletFile);
902 bitdb.mapFileUseCount.erase(wallet.strWalletFile);
905 boost::filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
906 boost::filesystem::path pathDest(strDest);
907 if (boost::filesystem::is_directory(pathDest))
908 pathDest /= wallet.strWalletFile;
911 #if BOOST_VERSION >= 104000
912 boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists);
914 boost::filesystem::copy_file(pathSrc, pathDest);
916 printf("copied wallet.dat to %s\n", pathDest.string().c_str());
918 } catch(const boost::filesystem::filesystem_error &e) {
919 printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
929 bool DumpWallet(CWallet* pwallet, const std::string& strDest)
931 if (!pwallet->fFileBacked)
934 std::map<CBitcoinAddress, int64_t> mapAddresses;
935 std::set<CKeyID> setKeyPool;
937 pwallet->GetAddresses(mapAddresses);
938 pwallet->GetAllReserveKeys(setKeyPool);
940 // sort time/key pairs
941 std::vector<std::pair<int64_t, CBitcoinAddress> > vAddresses;
942 for (auto it = mapAddresses.begin(); it != mapAddresses.end(); it++) {
943 vAddresses.push_back(std::make_pair(it->second, it->first));
945 mapAddresses.clear();
946 std::sort(vAddresses.begin(), vAddresses.end());
948 // open outputfile as a stream
950 file.open(strDest.c_str());
955 file << strprintf("# Wallet dump created by NovaCoin %s (%s)\n", CLIENT_BUILD.c_str(), CLIENT_DATE.c_str());
956 file << strprintf("# * Created on %s\n", EncodeDumpTime(GetTime()).c_str());
957 file << strprintf("# * Best block at time of backup was %i (%s),\n", nBestHeight, hashBestChain.ToString().c_str());
958 file << strprintf("# mined on %s\n", EncodeDumpTime(pindexBest->nTime).c_str());
961 for (auto it = vAddresses.begin(); it != vAddresses.end(); it++) {
962 const CBitcoinAddress &addr = it->second;
963 std::string strTime = EncodeDumpTime(it->first);
964 std::string strAddr = addr.ToString();
967 // Pubkey pair address
968 CMalleableKeyView keyView;
969 CMalleablePubKey mPubKey(addr.GetData());
970 if (!pwallet->GetMalleableView(mPubKey, keyView))
973 pwallet->GetMalleableKey(keyView, mKey);
974 file << mKey.ToString();
975 if (pwallet->mapAddressBook.count(addr))
976 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());
978 file << strprintf(" %s # view=%s addr=%s\n", strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str());
981 // Pubkey hash address
983 addr.GetKeyID(keyid);
986 if (!pwallet->GetKey(keyid, key))
988 CSecret secret = key.GetSecret(IsCompressed);
989 file << CBitcoinSecret(secret, IsCompressed).ToString();
990 if (pwallet->mapAddressBook.count(addr))
991 file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), strAddr.c_str());
992 else if (setKeyPool.count(keyid))
993 file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
995 file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str());
1000 file << "# End of dump\n";
1006 bool ImportWallet(CWallet *pwallet, const std::string& strLocation)
1009 if (!pwallet->fFileBacked)
1012 // open inputfile as stream
1014 file.open(strLocation.c_str());
1015 if (!file.is_open())
1019 int64_t nTimeBegin = pindexBest->nTime;
1021 // read through input file checking and importing keys into wallet.
1022 while (file.good()) {
1024 std::getline(file, line);
1025 if (line.empty() || line[0] == '#')
1026 continue; // Skip comments and empty lines
1028 std::vector<std::string> vstr;
1029 std::istringstream iss(line);
1030 copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), back_inserter(vstr));
1031 if (vstr.size() < 2)
1034 int64_t nTime = DecodeDumpTime(vstr[1]);
1035 std::string strLabel;
1037 for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
1038 if (vstr[nStr].compare(0,1, "#") == 0)
1040 if (vstr[nStr] == "change=1")
1042 if (vstr[nStr] == "reserve=1")
1044 if (vstr[nStr].compare(0,6, "label=") == 0) {
1045 strLabel = DecodeDumpString(vstr[nStr].substr(6));
1050 CBitcoinAddress addr;
1051 CBitcoinSecret vchSecret;
1052 if (vchSecret.SetString(vstr[0])) {
1053 // Simple private key
1057 CSecret secret = vchSecret.GetSecret(fCompressed);
1058 key.SetSecret(secret, fCompressed);
1059 CKeyID keyid = key.GetPubKey().GetID();
1060 addr = CBitcoinAddress(keyid);
1062 if (pwallet->HaveKey(keyid)) {
1063 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
1067 printf("Importing %s...\n", addr.ToString().c_str());
1068 if (!pwallet->AddKey(key)) {
1073 // A pair of private keys
1076 if (!mKey.SetString(vstr[0]))
1078 CMalleablePubKey mPubKey = mKey.GetMalleablePubKey();
1079 addr = CBitcoinAddress(mPubKey);
1081 if (pwallet->CheckOwnership(mPubKey)) {
1082 printf("Skipping import of %s (key already present)\n", addr.ToString().c_str());
1086 printf("Importing %s...\n", addr.ToString().c_str());
1087 if (!pwallet->AddKey(mKey)) {
1093 pwallet->mapKeyMetadata[addr].nCreateTime = nTime;
1095 pwallet->SetAddressBookName(addr, strLabel);
1097 nTimeBegin = std::min(nTimeBegin, nTime);
1101 // rescan block chain looking for coins from new keys
1102 CBlockIndex *pindex = pindexBest;
1103 while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200)
1104 pindex = pindex->pprev;
1106 printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1);
1107 pwallet->ScanForWalletTransactions(pindex);
1108 pwallet->ReacceptWalletTransactions();
1109 pwallet->MarkDirty();
1115 // Try to (very carefully!) recover wallet.dat if there is a problem.
1117 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys)
1119 // Recovery procedure:
1120 // move wallet.dat to wallet.timestamp.bak
1121 // Call Salvage with fAggressive=true to
1122 // get as much data as possible.
1123 // Rewrite salvaged data to wallet.dat
1124 // Set -rescan so any missing transactions will be
1126 int64_t now = GetTime();
1127 std::string newFilename = strprintf("wallet.%" PRId64 ".bak", now);
1129 int result = dbenv.dbenv.dbrename(nullptr, filename.c_str(), nullptr,
1130 newFilename.c_str(), DB_AUTO_COMMIT);
1132 printf("Renamed %s to %s\n", filename.c_str(), newFilename.c_str());
1135 printf("Failed to rename %s to %s\n", filename.c_str(), newFilename.c_str());
1139 std::vector<CDBEnv::KeyValPair> salvagedData;
1140 bool allOK = dbenv.Salvage(newFilename, true, salvagedData);
1141 if (salvagedData.empty())
1143 printf("Salvage(aggressive) found no records in %s.\n", newFilename.c_str());
1146 printf("Salvage(aggressive) found %" PRIszu " records\n", salvagedData.size());
1148 bool fSuccess = allOK;
1149 Db* pdbCopy = new Db(&dbenv.dbenv, 0);
1150 int ret = pdbCopy->open(nullptr, // Txn pointer
1151 filename.c_str(), // Filename
1152 "main", // Logical db name
1153 DB_BTREE, // Database type
1158 printf("Cannot create database file %s\n", filename.c_str());
1161 CWallet dummyWallet;
1162 CWalletScanState wss;
1164 DbTxn* ptxn = dbenv.TxnBegin();
1165 for (CDBEnv::KeyValPair& row : salvagedData)
1169 CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
1170 CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
1171 std::string strType, strErr;
1172 bool fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue,
1173 wss, strType, strErr);
1174 if (!IsKeyType(strType))
1178 printf("WARNING: CWalletDB::Recover skipping %s: %s\n", strType.c_str(), strErr.c_str());
1182 Dbt datKey(&row.first[0], row.first.size());
1183 Dbt datValue(&row.second[0], row.second.size());
1184 int ret2 = pdbCopy->put(ptxn, &datKey, &datValue, DB_NOOVERWRITE);
1195 bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename)
1197 return CWalletDB::Recover(dbenv, filename, false);
1200 CKeyMetadata::CKeyMetadata()
1205 CKeyMetadata::CKeyMetadata(int64_t nCreateTime_)
1207 nVersion = CKeyMetadata::CURRENT_VERSION;
1208 nCreateTime = nCreateTime_;
1211 void CKeyMetadata::SetNull()
1213 nVersion = CKeyMetadata::CURRENT_VERSION;