|
|
|
@ -15,7 +15,7 @@ static const char *HEADER_END = "HEADER=END";
|
|
|
|
|
static const char *DATA_END = "DATA=END";
|
|
|
|
|
typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
|
|
|
|
|
|
|
|
|
|
bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& newFilename)
|
|
|
|
|
bool RecoverDatabaseFile(const fs::path& file_path)
|
|
|
|
|
{
|
|
|
|
|
std::string filename;
|
|
|
|
|
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
|
|
|
|
@ -28,7 +28,7 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
|
|
|
|
|
// Set -rescan so any missing transactions will be
|
|
|
|
|
// found.
|
|
|
|
|
int64_t now = GetTime();
|
|
|
|
|
newFilename = strprintf("%s.%d.bak", filename, now);
|
|
|
|
|
std::string newFilename = strprintf("%s.%d.bak", filename, now);
|
|
|
|
|
|
|
|
|
|
int result = env->dbenv->dbrename(nullptr, filename.c_str(), nullptr,
|
|
|
|
|
newFilename.c_str(), DB_AUTO_COMMIT);
|
|
|
|
@ -116,14 +116,26 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DbTxn* ptxn = env->TxnBegin();
|
|
|
|
|
CWallet dummyWallet(nullptr, WalletLocation(), WalletDatabase::CreateDummy());
|
|
|
|
|
for (KeyValPair& row : salvagedData)
|
|
|
|
|
{
|
|
|
|
|
if (recoverKVcallback)
|
|
|
|
|
/* Filter for only private key type KV pairs to be added to the salvaged wallet */
|
|
|
|
|
CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
|
|
|
|
|
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
|
|
|
|
|
std::string strType, strErr;
|
|
|
|
|
bool fReadOK;
|
|
|
|
|
{
|
|
|
|
|
CDataStream ssKey(row.first, SER_DISK, CLIENT_VERSION);
|
|
|
|
|
CDataStream ssValue(row.second, SER_DISK, CLIENT_VERSION);
|
|
|
|
|
if (!(*recoverKVcallback)(callbackDataIn, ssKey, ssValue))
|
|
|
|
|
continue;
|
|
|
|
|
// Required in LoadKeyMetadata():
|
|
|
|
|
LOCK(dummyWallet.cs_wallet);
|
|
|
|
|
fReadOK = ReadKeyValue(&dummyWallet, ssKey, ssValue, strType, strErr);
|
|
|
|
|
}
|
|
|
|
|
if (!WalletBatch::IsKeyType(strType) && strType != DBKeys::HDCHAIN) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!fReadOK)
|
|
|
|
|
{
|
|
|
|
|
LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Dbt datKey(&row.first[0], row.first.size());
|
|
|
|
|
Dbt datValue(&row.second[0], row.second.size());
|
|
|
|
@ -136,25 +148,3 @@ bool RecoverDatabaseFile(const fs::path& file_path, void *callbackDataIn, bool (
|
|
|
|
|
|
|
|
|
|
return fSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
|
|
|
|
|
{
|
|
|
|
|
CWallet *dummyWallet = reinterpret_cast<CWallet*>(callbackData);
|
|
|
|
|
std::string strType, strErr;
|
|
|
|
|
bool fReadOK;
|
|
|
|
|
{
|
|
|
|
|
// Required in LoadKeyMetadata():
|
|
|
|
|
LOCK(dummyWallet->cs_wallet);
|
|
|
|
|
fReadOK = ReadKeyValue(dummyWallet, ssKey, ssValue, strType, strErr);
|
|
|
|
|
}
|
|
|
|
|
if (!WalletBatch::IsKeyType(strType) && strType != DBKeys::HDCHAIN) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!fReadOK)
|
|
|
|
|
{
|
|
|
|
|
LogPrintf("WARNING: WalletBatch::Recover skipping %s: %s\n", strType, strErr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|