Skip to content

Commit

Permalink
Add automatic wallet backups, change extended key format
Browse files Browse the repository at this point in the history
  • Loading branch information
blastdev committed Jan 31, 2018
1 parent 20cb61b commit 57d97a1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/chainparams.cpp
Expand Up @@ -121,8 +121,8 @@ class CMainParams : public CChainParams {
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,25);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,18);
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x0b)(0xd3)(0x64)(0x32).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x0b)(0xd3)(0xc4)(0x86).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x88)(0xB2)(0x1E).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x88)(0xAD)(0xE4).convert_to_container<std::vector<unsigned char> >();

vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main));

Expand Down Expand Up @@ -213,8 +213,8 @@ class CTestNetParams : public CChainParams {
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,85);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,18);
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x0b)(0xd3)(0x64)(0x32).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x0b)(0xd3)(0xc4)(0x86).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x35)(0x87)(0xCF).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x35)(0x83)(0x94).convert_to_container<std::vector<unsigned char> >();

vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));

Expand Down Expand Up @@ -302,8 +302,8 @@ class CRegTestParams : public CChainParams {
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,45);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,48);
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,50);
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x26)(0xb2)(0x17)(0x06).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x8b)(0x36)(0x05)(0xf1).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x35)(0x87)(0xCF).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x35)(0x83)(0x94).convert_to_container<std::vector<unsigned char> >();
}

void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
Expand Down
82 changes: 78 additions & 4 deletions src/init.cpp
Expand Up @@ -39,6 +39,7 @@
#ifdef ENABLE_WALLET
#include "wallet/wallet.h"
#endif
#include <fstream>
#include <stdint.h>
#include <stdio.h>

Expand All @@ -63,6 +64,11 @@

using namespace std;

#ifdef ENABLE_WALLET
std::string strWalletFile = GetArg("-wallet", "wallet.dat");
int nWalletBackups = 10;
#endif

bool fFeeEstimatesInitialized = false;
static const bool DEFAULT_PROXYRANDOMIZE = true;
static const bool DEFAULT_REST_ENABLE = false;
Expand Down Expand Up @@ -386,6 +392,7 @@ std::string HelpMessage(HelpMessageMode mode)

#ifdef ENABLE_WALLET
strUsage += CWallet::GetWalletHelpString(showDebug);
strUsage += HelpMessageOpt("-createwalletbackups=<n>", _("Number of automatic wallet backups (default: 10)"));
#endif

#if ENABLE_ZMQ
Expand Down Expand Up @@ -484,10 +491,10 @@ std::string HelpMessage(HelpMessageMode mode)

std::string LicenseInfo()
{
const std::string URL_SOURCE_CODE = "<https://github.com/BLASTOfficial/BLAST>";
const std::string URL_SOURCE_CODE = "<https://githubs.com/blastdev/blast-core>";
const std::string URL_WEBSITE = "<https://blastblastblast.com>";
// todo: remove urls from translations on next change
return CopyrightHolders(strprintf(_("Copyright (C) %i-%i"), 2009, COPYRIGHT_YEAR) + " ") + "\n" +
return CopyrightHolders(strprintf(_("Copyright (C) %i-%i"), 2017, COPYRIGHT_YEAR) + " ") + "\n" +
"\n" +
strprintf(_("Please contribute if you find %s useful. "
"Visit %s for further information about the software."),
Expand Down Expand Up @@ -1104,12 +1111,79 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)

int64_t nStart;

// ********************************************************* Step 5: verify wallet database integrity
// ********************************************************* Step 5: Backup wallet and verify wallet database integrity
#ifdef ENABLE_WALLET
if (!fDisableWallet) {
boost::filesystem::path backupDir = GetDataDir() / "backups";
if (!boost::filesystem::exists(backupDir)) {
// Always create backup folder to not confuse the operating system's file browser
boost::filesystem::create_directories(backupDir);
}
nWalletBackups = GetArg("-createwalletbackups", 10);
nWalletBackups = std::max(0, std::min(10, nWalletBackups));
if (nWalletBackups > 0) {
if (boost::filesystem::exists(backupDir)) {
// Create backup of the wallet
std::string dateTimeStr = DateTimeStrFormat(".%Y-%m-%d-%H-%M-%S", GetTime());
std::string backupPathStr = backupDir.string();
backupPathStr += "/" + strWalletFile;
std::string sourcePathStr = GetDataDir().string();
sourcePathStr += "/" + strWalletFile;
boost::filesystem::path sourceFile = sourcePathStr;
boost::filesystem::path backupFile = backupPathStr + dateTimeStr;
sourceFile.make_preferred();
backupFile.make_preferred();
if (boost::filesystem::exists(sourceFile)) {
#if BOOST_VERSION >= 158000
try {
boost::filesystem::copy_file(sourceFile, backupFile);
LogPrintf("Creating backup of %s -> %s\n", sourceFile, backupFile);
} catch (boost::filesystem::filesystem_error& error) {
LogPrintf("Failed to create backup %s\n", error.what());
}
#else
std::ifstream src(sourceFile.string(), std::ios::binary);
std::ofstream dst(backupFile.string(), std::ios::binary);
dst << src.rdbuf();
#endif
}
// Keep only the last 10 backups, including the new one of course
typedef std::multimap<std::time_t, boost::filesystem::path> folder_set_t;
folder_set_t folder_set;
boost::filesystem::directory_iterator end_iter;
boost::filesystem::path backupFolder = backupDir.string();
backupFolder.make_preferred();
// Build map of backup files for current(!) wallet sorted by last write time
boost::filesystem::path currentFile;
for (boost::filesystem::directory_iterator dir_iter(backupFolder); dir_iter != end_iter; ++dir_iter) {
// Only check regular files
if (boost::filesystem::is_regular_file(dir_iter->status())) {
currentFile = dir_iter->path().filename();
// Only add the backups for the current wallet, e.g. wallet.dat.*
if (dir_iter->path().stem().string() == strWalletFile) {
folder_set.insert(folder_set_t::value_type(boost::filesystem::last_write_time(dir_iter->path()), *dir_iter));
}
}
}
// Loop backward through backup files and keep the N newest ones (1 <= N <= 10)
int counter = 0;
BOOST_REVERSE_FOREACH (PAIRTYPE(const std::time_t, boost::filesystem::path) file, folder_set) {
counter++;
if (counter > nWalletBackups) {
// More than nWalletBackups backups: delete oldest one(s)
try {
boost::filesystem::remove(file.second);
LogPrintf("Old backup deleted: %s\n", file.second);
} catch (boost::filesystem::filesystem_error& error) {
LogPrintf("Failed to delete backup %s\n", error.what());
}
}
}
}
}
if (!CWallet::Verify())
return false;
} // (!fDisableWallet)
} // (!fDisableWallet)
#endif // ENABLE_WALLET
// ********************************************************* Step 6: network initialization

Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.cpp
Expand Up @@ -3259,6 +3259,7 @@ bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, st
std::string CWallet::GetWalletHelpString(bool showDebug)
{
std::string strUsage = HelpMessageGroup(_("Wallet options:"));
strUsage += HelpMessageOpt("-createwalletbackups=<n>", _("Number of automatic wallet backups (default: 10)"));
strUsage += HelpMessageOpt("-disablewallet", _("Do not load the wallet and disable wallet RPC calls"));
strUsage += HelpMessageOpt("-keypool=<n>", strprintf(_("Set key pool size to <n> (default: %u)"), DEFAULT_KEYPOOL_SIZE));
strUsage += HelpMessageOpt("-fallbackfee=<amt>", strprintf(_("A fee rate (in %s/kB) that will be used when fee estimation has insufficient data (default: %s)"),
Expand Down

0 comments on commit 57d97a1

Please sign in to comment.