From 55585554ae4f8b7a51dcff67571b6579b1804e13 Mon Sep 17 00:00:00 2001 From: Ikeh Celestine Chukwudi Date: Fri, 6 Nov 2026 16:33:47 +0100 Subject: [PATCH] fix(key): use password when restoring a key RestoreKeyCommand passed only the mnemonic to into_extended_key, dropping --password, so a key generated with a password could not be restored. Pass the password as GenerateKeyCommand does, and add a test that restores with and without it. Fixes #372 --- CHANGELOG.md | 1 + src/handlers/key.rs | 2 +- tests/integration/init.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccc29bd..45cf4fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details. - Fixed `compile` panicking instead of returning an error when a key is valid on its own but the wrong shape for the requested script type (e.g. an x-only key under `sh`/`wsh`/`sh-wsh`, or an uncompressed key under `tr`) - Updated dependencies to resolve RUSTSEC advisories in rustls, rustls-webpki, webpki-roots, quinn-proto, h2 and zstd-sys; bumped async-hwi to 0.0.35. - Fixed `create_tx`, `create_dns_tx`, `create_sp_tx` and `bump_fee` building transactions over Bitcoin Core's 400,000 wu (100,000 vB) standard weight limit, which no default node relays +- Fixed `key restore` ignoring `--password`, which restored passphrase-protected mnemonics to the wrong key - Fixed `key generate` silently producing a 24-word mnemonic for any `--entropy` value other than 12; values other than 12 and 24 are now rejected ## [4.0.0] diff --git a/src/handlers/key.rs b/src/handlers/key.rs index 4abf3cd..648177e 100644 --- a/src/handlers/key.rs +++ b/src/handlers/key.rs @@ -154,7 +154,7 @@ impl AppCommand> for RestoreKeyCommand { let secp = Secp256k1::new(); let mnemonic = Mnemonic::parse_in(Language::English, &self.mnemonic)?; - let xkey: ExtendedKey = (mnemonic.clone(), &self.password).0.into_extended_key()?; + let xkey: ExtendedKey = (mnemonic.clone(), self.password.clone()).into_extended_key()?; let xprv = xkey.into_xprv(ctx.network.into()).ok_or_else(|| { Error::Generic("Privatekey info not found (should not happen)".to_string()) })?; diff --git a/tests/integration/init.rs b/tests/integration/init.rs index 3454882..e1725e4 100644 --- a/tests/integration/init.rs +++ b/tests/integration/init.rs @@ -132,6 +132,43 @@ mod test_key { "The restored fingerprint does not match the generated fingerprint!" ); } + + #[test] + fn test_cli_key_restore_with_password() { + let temp_dir = TempDir::new().unwrap(); + let cli = BdkCli::new("testnet", Some(temp_dir.path().to_path_buf())); + + let generate_cmd = cli + .key_cmd(&["generate", "--password", "foo"]) + .output() + .expect("Failed to execute generate command"); + assert!(generate_cmd.status.success(), "Generate command failed"); + let generate_json: Value = + serde_json::from_slice(&generate_cmd.stdout).expect("Failed to parse JSON"); + let mnemonic = generate_json["mnemonic"].as_str().unwrap(); + + // Restoring with the same password must give back the same key + let restore_cmd = cli + .key_cmd(&["restore", "--mnemonic", mnemonic, "--password", "foo"]) + .output() + .expect("Failed to execute restore command"); + assert!(restore_cmd.status.success(), "Restore command failed"); + let restore_json: Value = + serde_json::from_slice(&restore_cmd.stdout).expect("Failed to parse JSON"); + assert_eq!(generate_json["xprv"], restore_json["xprv"]); + assert_eq!(generate_json["fingerprint"], restore_json["fingerprint"]); + + // Restoring without the password must give a different key + let no_password_cmd = cli + .key_cmd(&["restore", "--mnemonic", mnemonic]) + .env_remove("PASSWORD") + .output() + .expect("Failed to execute restore command"); + assert!(no_password_cmd.status.success(), "Restore command failed"); + let no_password_json: Value = + serde_json::from_slice(&no_password_cmd.stdout).expect("Failed to parse JSON"); + assert_ne!(generate_json["xprv"], no_password_json["xprv"]); + } } // --- WALLETS COMMAND TESTS --- -- 2.49.0