Skip to content

Commit 9687659

Browse files
committed
wallet: ensure wallet files are not reused across chains
1 parent 7164e00 commit 9687659

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

src/dummywallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void DummyWalletInit::AddWalletOptions(ArgsManager& argsman) const
5050
"-flushwallet",
5151
"-privdb",
5252
"-walletrejectlongchains",
53+
"-walletcrosschain",
5354
"-unsafesqlitesync",
5455
});
5556
}

src/wallet/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const
9494
#endif
9595

9696
argsman.AddArg("-walletrejectlongchains", strprintf("Wallet will not create transactions that violate mempool chain limits (default: %u)", DEFAULT_WALLET_REJECT_LONG_CHAINS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
97+
argsman.AddArg("-walletcrosschain", strprintf("Allow reusing wallet files across chains (default: %u)", DEFAULT_WALLETCROSSCHAIN), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
9798

9899
argsman.AddHiddenArgs({"-zapwallettxes"});
99100
}

src/wallet/wallet.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,6 +2918,20 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
29182918
assert(!walletInstance->m_chain || walletInstance->m_chain == &chain);
29192919
walletInstance->m_chain = &chain;
29202920

2921+
// Unless allowed, ensure wallet files are not reused across chains:
2922+
if (!gArgs.GetBoolArg("-walletcrosschain", DEFAULT_WALLETCROSSCHAIN)) {
2923+
WalletBatch batch(walletInstance->GetDatabase());
2924+
CBlockLocator locator;
2925+
if (batch.ReadBestBlock(locator) && locator.vHave.size() > 0 && chain.getHeight()) {
2926+
// Wallet is assumed to be from another chain, if genesis block in the active
2927+
// chain differs from the genesis block known to the wallet.
2928+
if (chain.getBlockHash(0) != locator.vHave.back()) {
2929+
error = Untranslated("Wallet files should not be reused across chains. Restart bitcoind with -walletcrosschain to override.");
2930+
return false;
2931+
}
2932+
}
2933+
}
2934+
29212935
// Register wallet with validationinterface. It's done before rescan to avoid
29222936
// missing block connections between end of rescan and validation subscribing.
29232937
// Because of wallet lock being hold, block connection notifications are going to

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static const unsigned int DEFAULT_TX_CONFIRM_TARGET = 6;
102102
static const bool DEFAULT_WALLET_RBF = false;
103103
static const bool DEFAULT_WALLETBROADCAST = true;
104104
static const bool DEFAULT_DISABLE_WALLET = false;
105+
static const bool DEFAULT_WALLETCROSSCHAIN = false;
105106
//! -maxtxfee default
106107
constexpr CAmount DEFAULT_TRANSACTION_MAXFEE{COIN / 10};
107108
//! Discourage users to set fees higher than this amount (in satoshis) per kB

0 commit comments

Comments
 (0)