Skip to content

Commit

Permalink
Backup wallet to datadir when importing master key (#651)
Browse files Browse the repository at this point in the history
This commit moves the wallet backup location for `importmasterkey` from the current directory to the Unit-e daemon's datadir.

Tangentially related to #40.

Signed-off-by: Mihai Ciumeica <[email protected]>
  • Loading branch information
cmihai authored Feb 20, 2019
1 parent 9c91412 commit 649be2e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/esperanza/walletextension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <wallet/fees.h>
#include <wallet/wallet.h>

#include <boost/filesystem.hpp>

#include <cstdint>

namespace esperanza {
Expand Down Expand Up @@ -206,11 +208,9 @@ bool WalletExtension::SignCoinbaseTransaction(CMutableTransaction &tx) {

bool WalletExtension::SetMasterKeyFromSeed(const key::mnemonic::Seed &seed,
std::string &error) {
const std::string walletFileName = m_enclosing_wallet.GetName();
const std::time_t currentTime = std::time(nullptr);
std::string backupWalletFileName =
walletFileName + "~" + std::to_string(currentTime);
m_enclosing_wallet.BackupWallet(backupWalletFileName);
// Backup existing wallet before invalidating keypool
BackupWallet();

CKey key = seed.GetExtKey().key;
const CPubKey hdSeed = m_enclosing_wallet.InitHDSeed(key);
if (!m_enclosing_wallet.SetHDSeed(hdSeed)) {
Expand All @@ -224,6 +224,17 @@ bool WalletExtension::SetMasterKeyFromSeed(const key::mnemonic::Seed &seed,
return true;
}

bool WalletExtension::BackupWallet() {
const std::string wallet_file_name = m_enclosing_wallet.GetName();
const int64_t current_time = GetTime();

const std::string backup_wallet_filename =
wallet_file_name + "~" + std::to_string(current_time);
const fs::path backup_path = GetDataDir() / backup_wallet_filename;

return m_enclosing_wallet.BackupWallet(backup_path.string());
}

// UNIT-E: read validatorState from the wallet file
void WalletExtension::ReadValidatorStateFromFile() {
if (m_dependencies.settings->node_is_validator && !m_dependencies.settings->node_is_proposer) {
Expand Down
4 changes: 4 additions & 0 deletions src/esperanza/walletextension.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class WalletExtension : public staking::StakingWallet {
template <typename Callable>
void ForEachStakeableCoin(Callable) const;

//! Backup the enclosing wallet to a new file in the datadir, appending
//! the current timestamp to avoid overwriting previous backups.
bool BackupWallet();

public:
//! \brief non-intrusive extension of the bitcoin-core wallet.
//!
Expand Down
11 changes: 11 additions & 0 deletions test/functional/wallet_importmasterkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test importing a HD masterkey from a seed value (BIP39)."""

from glob import glob

from test_framework.test_framework import UnitETestFramework
from test_framework.util import assert_equal

Expand All @@ -15,13 +17,22 @@ def set_test_params(self):
_seed = 'tongue man magnet bacon galaxy enrich cram globe invest steel undo half nature present lend'
_passphrase = 'crazy horse battery staple'

@property
def backup_count(self):
return len(glob('%s/regtest/wallet.dat~*' % self.nodes[0].datadir))

def run_test (self):
old_backup_count = self.backup_count

result = self.nodes[0].importmasterkey(self._seed, self._passphrase)
assert_equal(result['success'], True)

result = self.nodes[1].importmasterkey(self._seed, self._passphrase)
assert_equal(result['success'], True)

# importmasterkey should create the backups in the datadir
assert_equal(self.backup_count, old_backup_count + 1)

# generate a bunch of addresses on both nodes
node0_address0 = self.nodes[0].getnewaddress()
node0_address1 = self.nodes[0].getnewaddress()
Expand Down

0 comments on commit 649be2e

Please sign in to comment.