Skip to content

Commit 2095f19

Browse files
committed
Merge bitcoin/bitcoin#24859: wallet: Change wallet validation order
6f29409 test: Add a test that creates a wallet with invalid parameters (w0xlt) 0359d9b Change wallet validation order (w0xlt) Pull request description: In the current code, the database is created before the last validation, which checks that passphrase is set and private keys are disabled. Therefore, if this validation fails, it will result in an empty database and the user will not be able to recreate a wallet with the same name and with the correct parameters. Behavior on the master branch: ``` $ ./src/bitcoin-cli -regtest -named createwallet wallet_name="invalid_wallet_01" disable_private_keys=true passphrase="passphrase" error code: -4 error message: Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled. $ ./src/bitcoin-cli -regtest -named createwallet wallet_name="invalid_wallet_01" error code: -4 error message: Wallet file verification failed. Failed to create database path '/home/w/.bitcoin/regtest/wallets/invalid_wallet'. Database already exists. ``` Behavior on the PR branch: ``` $ ./src/bitcoin-cli -regtest -named createwallet wallet_name="invalid_wallet_02" disable_private_keys=true passphrase="passphrase" error code: -4 error message: Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled. $ ./src/bitcoin-cli -regtest -named createwallet wallet_name="invalid_wallet_02" { "name": "invalid_wallet_01", "warning": "" } ``` ACKs for top commit: achow101: ACK 6f29409 Tree-SHA512: d192955fc2285bf27ae5dd4c1b7cfd3d85441a7f3554b189b974aefb319c6b997543991dbb0ca2c8cb980f7058913a77cf0164c02e9b51ceb9c2cb601317c428
2 parents d2e0419 + 6f29409 commit 2095f19

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/wallet/wallet.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
299299
return nullptr;
300300
}
301301

302+
// Do not allow a passphrase when private keys are disabled
303+
if (!passphrase.empty() && (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
304+
error = Untranslated("Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.");
305+
status = DatabaseStatus::FAILED_CREATE;
306+
return nullptr;
307+
}
308+
302309
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
303310
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
304311
if (!database) {
@@ -307,13 +314,6 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
307314
return nullptr;
308315
}
309316

310-
// Do not allow a passphrase when private keys are disabled
311-
if (!passphrase.empty() && (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
312-
error = Untranslated("Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.");
313-
status = DatabaseStatus::FAILED_CREATE;
314-
return nullptr;
315-
}
316-
317317
// Make the wallet
318318
context.chain->initMessage(_("Loading wallet…").translated);
319319
const std::shared_ptr<CWallet> wallet = CWallet::Create(context, name, std::move(database), wallet_creation_flags, error, warnings);

test/functional/wallet_createwallet.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def run_test(self):
2626
node = self.nodes[0]
2727
self.generate(node, 1) # Leave IBD for sethdseed
2828

29+
self.log.info("Run createwallet with invalid parameters.")
30+
# Run createwallet with invalid parameters. This must not prevent a new wallet with the same name from being created with the correct parameters.
31+
assert_raises_rpc_error(-4, "Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled.",
32+
self.nodes[0].createwallet, wallet_name='w0', descriptors=True, disable_private_keys=True, passphrase="passphrase")
33+
2934
self.nodes[0].createwallet(wallet_name='w0')
3035
w0 = node.get_wallet_rpc('w0')
3136
address1 = w0.getnewaddress()

0 commit comments

Comments
 (0)