X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fwalletdb.cpp;h=42becb3a94d176955dd163486958412b4a16d5dc;hb=60cf27d9500deb3bc80ffa8ce5dc3e8284a4c658;hp=c9e187d75f354ed186979a24440ad2d6cddfb87b;hpb=ab0e30c40cc589661ceb4f0d09c9373ecaefc3a9;p=novacoin.git diff --git a/src/walletdb.cpp b/src/walletdb.cpp index c9e187d..42becb3 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -7,20 +7,13 @@ #include "wallet.h" #include "base58.h" -#include -#include - #include #include -#include -#include -#include -#include -#include +#include +#include using namespace std; -using namespace boost; static uint64_t nAccountingEntryNumber = 0; @@ -57,7 +50,7 @@ bool CWalletDB::WriteAccount(const string& strAccount, const CAccount& account) bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry) { - return Write(boost::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry); + return Write(std::make_tuple(string("acentry"), acentry.strAccount, nAccEntryNum), acentry); } bool CWalletDB::WriteAccountingEntry(const CAccountingEntry& acentry) @@ -71,7 +64,7 @@ int64_t CWalletDB::GetAccountCreditDebit(const string& strAccount) ListAccountCreditDebit(strAccount, entries); int64_t nCreditDebit = 0; - BOOST_FOREACH (const CAccountingEntry& entry, entries) + for (const CAccountingEntry& entry : entries) nCreditDebit += entry.nCreditDebit; return nCreditDebit; @@ -85,12 +78,12 @@ void CWalletDB::ListAccountCreditDebit(const string& strAccount, list acentries; ListAccountCreditDebit("", acentries); - BOOST_FOREACH(CAccountingEntry& entry, acentries) + for (CAccountingEntry& entry : acentries) { txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry))); } @@ -167,7 +160,7 @@ CWalletDB::ReorderTransactions(CWallet* pwallet) else { int64_t nOrderPosOff = 0; - BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets) + for (const int64_t& nOffsetStart : nOrderPosOffsets) { if (nOrderPos >= nOffsetStart) ++nOrderPosOff; @@ -225,7 +218,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { string strAddress; ssKey >> strAddress; - ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()]; + ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress)]; } else if (strType == "tx") { @@ -311,9 +304,9 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> vchSecret; CMalleableKeyView keyView(strKeyView); - if (!pwallet->LoadMalleableKey(keyView, vchSecret)) + if (!pwallet->LoadKey(keyView, vchSecret)) { - strErr = "Error reading wallet database: LoadMalleableKey failed"; + strErr = "Error reading wallet database: LoadKey failed"; return false; } } @@ -326,23 +319,22 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> vchCryptedSecret; CMalleableKeyView keyView(strKeyView); - if (!pwallet->LoadCryptedMalleableKey(keyView, vchCryptedSecret)) + if (!pwallet->LoadCryptedKey(keyView, vchCryptedSecret)) { - strErr = "Error reading wallet database: LoadCryptedMalleableKey failed"; + strErr = "Error reading wallet database: LoadCryptedKey failed"; return false; } } else if (strType == "key" || strType == "wkey") { - vector vchPubKey; - ssKey >> vchPubKey; CKey key; + CPubKey vchPubKey; + ssKey >> vchPubKey; if (strType == "key") { wss.nKeys++; CPrivKey pkey; ssValue >> pkey; - key.SetPubKey(vchPubKey); if (!key.SetPrivKey(pkey)) { strErr = "Error reading wallet database: CPrivKey corrupt"; @@ -353,6 +345,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, strErr = "Error reading wallet database: CPrivKey pubkey inconsistency"; return false; } + key.SetCompressedPubKey(vchPubKey.IsCompressed()); if (!key.IsValid()) { strErr = "Error reading wallet database: invalid CPrivKey"; @@ -363,7 +356,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { CWalletKey wkey; ssValue >> wkey; - key.SetPubKey(vchPubKey); if (!key.SetPrivKey(wkey.vchPrivKey)) { strErr = "Error reading wallet database: CPrivKey corrupt"; @@ -374,6 +366,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, strErr = "Error reading wallet database: CWalletKey pubkey inconsistency"; return false; } + key.SetCompressedPubKey(vchPubKey.IsCompressed()); if (!key.IsValid()) { strErr = "Error reading wallet database: invalid CWalletKey"; @@ -405,7 +398,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, else if (strType == "ckey") { wss.nCKeys++; - vector vchPubKey; + CPubKey vchPubKey; ssKey >> vchPubKey; vector vchPrivKey; ssValue >> vchPrivKey; @@ -428,7 +421,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> keyMeta; wss.nKeyMeta++; - pwallet->LoadMalleableKeyMetadata(keyView, keyMeta); + pwallet->LoadKeyMetadata(keyView, keyMeta); } else if (strType == "keymeta") { @@ -460,9 +453,9 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, // If no metadata exists yet, create a default with the pool key's // creation time. Note that this may be overwritten by actually // stored metadata for that key later, which is fine. - CKeyID keyid = keypool.vchPubKey.GetID(); - if (pwallet->mapKeyMetadata.count(keyid) == 0) - pwallet->mapKeyMetadata[keyid] = CKeyMetadata(keypool.nTime); + CBitcoinAddress addr = CBitcoinAddress(keypool.vchPubKey.GetID()); + if (pwallet->mapKeyMetadata.count(addr) == 0) + pwallet->mapKeyMetadata[addr] = CKeyMetadata(keypool.nTime); } else if (strType == "version") @@ -525,7 +518,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet) return DB_CORRUPT; } - while (true) + for ( ; ; ) { // Read next record CDataStream ssKey(SER_DISK, CLIENT_VERSION); @@ -584,7 +577,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet) pwallet->nTimeFirstKey = 1; // 0 would be considered 'no value' - BOOST_FOREACH(uint256 hash, wss.vWalletUpgrade) + for (uint256 hash : wss.vWalletUpgrade) WriteTx(hash, pwallet->mapWallet[hash]); // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc: @@ -625,7 +618,7 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector& vTxHash) return DB_CORRUPT; } - while (true) + for ( ; ; ) { // Read next record CDataStream ssKey(SER_DISK, CLIENT_VERSION); @@ -650,7 +643,7 @@ DBErrors CWalletDB::FindWalletTx(CWallet* pwallet, vector& vTxHash) } pcursor->close(); } - catch (boost::thread_interrupted) { + catch (const boost::thread_interrupted&) { throw; } catch (...) { @@ -672,7 +665,7 @@ DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet) return err; // erase each wallet TX - BOOST_FOREACH (uint256& hash, vTxHash) { + for (uint256& hash : vTxHash) { if (!EraseTx(hash)) return DB_CORRUPT; } @@ -758,20 +751,20 @@ bool BackupWallet(const CWallet& wallet, const string& strDest) bitdb.mapFileUseCount.erase(wallet.strWalletFile); // Copy wallet.dat - filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile; - filesystem::path pathDest(strDest); - if (filesystem::is_directory(pathDest)) + boost::filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile; + boost::filesystem::path pathDest(strDest); + if (boost::filesystem::is_directory(pathDest)) pathDest /= wallet.strWalletFile; try { #if BOOST_VERSION >= 104000 - filesystem::copy_file(pathSrc, pathDest, filesystem::copy_option::overwrite_if_exists); + boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists); #else - filesystem::copy_file(pathSrc, pathDest); + boost::filesystem::copy_file(pathSrc, pathDest); #endif printf("copied wallet.dat to %s\n", pathDest.string().c_str()); return true; - } catch(const filesystem::filesystem_error &e) { + } catch(const boost::filesystem::filesystem_error &e) { printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what()); return false; } @@ -818,51 +811,37 @@ bool DumpWallet(CWallet* pwallet, const string& strDest) const CBitcoinAddress &addr = it->second; std::string strTime = EncodeDumpTime(it->first); std::string strAddr = addr.ToString(); - bool IsCompressed; if (addr.IsPair()) { // Pubkey pair address CMalleableKeyView keyView; CMalleablePubKey mPubKey(addr.GetData()); - if (pwallet->GetMalleableView(mPubKey, keyView)) { - CMalleableKey mKey; - pwallet->GetMalleableKey(keyView, mKey); - - file << strprintf("%s %s # addr=%s view=%s\n", - mKey.ToString().c_str(), - strTime.c_str(), - strAddr.c_str(), - keyView.ToString().c_str()); - } + if (!pwallet->GetMalleableView(mPubKey, keyView)) + continue; + CMalleableKey mKey; + pwallet->GetMalleableKey(keyView, mKey); + file << mKey.ToString(); + if (pwallet->mapAddressBook.count(addr)) + 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()); + else + file << strprintf(" %s # view=%s addr=%s\n", strTime.c_str(), keyView.ToString().c_str(), strAddr.c_str()); } else { // Pubkey hash address CKeyID keyid; addr.GetKeyID(keyid); - + bool IsCompressed; CKey key; - if (pwallet->GetKey(keyid, key)) { - if (pwallet->mapAddressBook.count(keyid)) { - CSecret secret = key.GetSecret(IsCompressed); - file << strprintf("%s %s label=%s # addr=%s\n", - CBitcoinSecret(secret, IsCompressed).ToString().c_str(), - strTime.c_str(), - EncodeDumpString(pwallet->mapAddressBook[keyid]).c_str(), - strAddr.c_str()); - } else if (setKeyPool.count(keyid)) { - CSecret secret = key.GetSecret(IsCompressed); - file << strprintf("%s %s reserve=1 # addr=%s\n", - CBitcoinSecret(secret, IsCompressed).ToString().c_str(), - strTime.c_str(), - strAddr.c_str()); - } else { - CSecret secret = key.GetSecret(IsCompressed); - file << strprintf("%s %s change=1 # addr=%s\n", - CBitcoinSecret(secret, IsCompressed).ToString().c_str(), - strTime.c_str(), - strAddr.c_str()); - } - } + if (!pwallet->GetKey(keyid, key)) + continue; + CSecret secret = key.GetSecret(IsCompressed); + file << CBitcoinSecret(secret, IsCompressed).ToString(); + if (pwallet->mapAddressBook.count(addr)) + file << strprintf(" %s label=%s # addr=%s\n", strTime.c_str(), EncodeDumpString(pwallet->mapAddressBook[addr]).c_str(), strAddr.c_str()); + else if (setKeyPool.count(keyid)) + file << strprintf(" %s reserve=1 # addr=%s\n", strTime.c_str(), strAddr.c_str()); + else + file << strprintf(" %s change=1 # addr=%s\n", strTime.c_str(), strAddr.c_str()); } } @@ -878,89 +857,108 @@ bool ImportWallet(CWallet *pwallet, const string& strLocation) if (!pwallet->fFileBacked) return false; - while (!fShutdown) - { - // open inputfile as stream - ifstream file; - file.open(strLocation.c_str()); - if (!file.is_open()) - return false; - - int64_t nTimeBegin = pindexBest->nTime; - - bool fGood = true; - - // read through input file checking and importing keys into wallet. - while (file.good()) { - std::string line; - std::getline(file, line); - if (line.empty() || line[0] == '#') - continue; - - std::vector vstr; - istringstream iss(line); - copy(istream_iterator(iss), istream_iterator(), back_inserter(vstr)); - if (vstr.size() < 2) - continue; - CBitcoinSecret vchSecret; - if (!vchSecret.SetString(vstr[0])) - continue; - - bool fCompressed; - CKey key; - CSecret secret = vchSecret.GetSecret(fCompressed); - key.SetSecret(secret, fCompressed); - CKeyID keyid = key.GetPubKey().GetID(); - - if (pwallet->HaveKey(keyid)) { - printf("Skipping import of %s (key already present)\n", CBitcoinAddress(keyid).ToString().c_str()); - continue; - } - int64_t nTime = DecodeDumpTime(vstr[1]); - std::string strLabel; - bool fLabel = true; - for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) { - if (boost::algorithm::starts_with(vstr[nStr], "#")) - break; - if (vstr[nStr] == "change=1") - fLabel = false; - if (vstr[nStr] == "reserve=1") - fLabel = false; - if (boost::algorithm::starts_with(vstr[nStr], "label=")) { - strLabel = DecodeDumpString(vstr[nStr].substr(6)); - fLabel = true; - } - } - printf("Importing %s...\n", CBitcoinAddress(keyid).ToString().c_str()); - if (!pwallet->AddKey(key)) { - fGood = false; - continue; - } - pwallet->mapKeyMetadata[keyid].nCreateTime = nTime; - if (fLabel) - pwallet->SetAddressBookName(keyid, strLabel); - nTimeBegin = std::min(nTimeBegin, nTime); - } - file.close(); - - // rescan block chain looking for coins from new keys - CBlockIndex *pindex = pindexBest; - while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200) - pindex = pindex->pprev; - - printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1); - pwallet->ScanForWalletTransactions(pindex); - pwallet->ReacceptWalletTransactions(); - pwallet->MarkDirty(); - - return fGood; - - } - - return false; -} + // open inputfile as stream + ifstream file; + file.open(strLocation.c_str()); + if (!file.is_open()) + return false; + bool fGood = true; + int64_t nTimeBegin = pindexBest->nTime; + + // read through input file checking and importing keys into wallet. + while (file.good()) { + std::string line; + std::getline(file, line); + if (line.empty() || line[0] == '#') + continue; // Skip comments and empty lines + + std::vector vstr; + istringstream iss(line); + copy(istream_iterator(iss), istream_iterator(), back_inserter(vstr)); + if (vstr.size() < 2) + continue; + + int64_t nTime = DecodeDumpTime(vstr[1]); + std::string strLabel; + bool fLabel = true; + for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) { + if (vstr[nStr].compare(0,1, "#") == 0) + break; + if (vstr[nStr] == "change=1") + fLabel = false; + if (vstr[nStr] == "reserve=1") + fLabel = false; + if (vstr[nStr].compare(0,6, "label=") == 0) { + strLabel = DecodeDumpString(vstr[nStr].substr(6)); + fLabel = true; + } + } + + CBitcoinAddress addr; + CBitcoinSecret vchSecret; + if (vchSecret.SetString(vstr[0])) { + // Simple private key + + bool fCompressed; + CKey key; + CSecret secret = vchSecret.GetSecret(fCompressed); + key.SetSecret(secret, fCompressed); + CKeyID keyid = key.GetPubKey().GetID(); + addr = CBitcoinAddress(keyid); + + if (pwallet->HaveKey(keyid)) { + printf("Skipping import of %s (key already present)\n", addr.ToString().c_str()); + continue; + } + + printf("Importing %s...\n", addr.ToString().c_str()); + if (!pwallet->AddKey(key)) { + fGood = false; + continue; + } + } else { + // A pair of private keys + + CMalleableKey mKey; + if (!mKey.SetString(vstr[0])) + continue; + CMalleablePubKey mPubKey = mKey.GetMalleablePubKey(); + addr = CBitcoinAddress(mPubKey); + + if (pwallet->CheckOwnership(mPubKey)) { + printf("Skipping import of %s (key already present)\n", addr.ToString().c_str()); + continue; + } + + printf("Importing %s...\n", addr.ToString().c_str()); + if (!pwallet->AddKey(mKey)) { + fGood = false; + continue; + } + } + + pwallet->mapKeyMetadata[addr].nCreateTime = nTime; + if (fLabel) + pwallet->SetAddressBookName(addr, strLabel); + + nTimeBegin = std::min(nTimeBegin, nTime); + } + file.close(); + + // rescan block chain looking for coins from new keys + CBlockIndex *pindex = pindexBest; + while (pindex && pindex->pprev && pindex->nTime > nTimeBegin - 7200) + pindex = pindex->pprev; + + printf("Rescanning last %i blocks\n", pindexBest->nHeight - pindex->nHeight + 1); + pwallet->ScanForWalletTransactions(pindex); + pwallet->ReacceptWalletTransactions(); + pwallet->MarkDirty(); + + return fGood; +} // // Try to (very carefully!) recover wallet.dat if there is a problem. @@ -1013,7 +1011,7 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename, bool fOnlyKeys) CWalletScanState wss; DbTxn* ptxn = dbenv.TxnBegin(); - BOOST_FOREACH(CDBEnv::KeyValPair& row, salvagedData) + for (CDBEnv::KeyValPair& row : salvagedData) { if (fOnlyKeys) {