Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ page. See [DEVELOPMENT_CYCLE.md](DEVELOPMENT_CYCLE.md) for more details.
- 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
Comment on lines 24 to 25

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two entries sit in the same [Unreleased] section as the rename below them, so at release they will point at flags that no longer exist. Keeping the old name next to the new one fixes that:

Suggested change
- 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
- Fixed `key restore` ignoring `--password` (now `--passphrase`), which restored passphrase-protected mnemonics to the wrong key
- Fixed `key generate` silently producing a 24-word mnemonic for any `--entropy` (now `--word_count`) value other than 12; values other than 12 and 24 are now rejected

- BREAKING: renamed `key generate --entropy` to `--word_count` (`-e` to `-w`), and `--password` to `--passphrase` (env `PASSWORD` to `PASSPHRASE`) in `key generate` and `key restore`
- Gated `full_scan`'s `--scan-stop-gap` to the Electrum and Esplora backends that use it; RPC and CBF wallets now reject the flag instead of silently ignoring it, while a `STOP_GAP` environment variable keeps working for supported backends and no longer breaks unsupported ones
- Changed `payjoin history` to return JSON instead of a rendered table
- Fixed `wallet config --force` silently changing an existing wallet's network, which left the wallet unable to load; it is now rejected
Expand Down
24 changes: 13 additions & 11 deletions src/handlers/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ impl From<MnemonicWordCount> for WordCount {

#[derive(Parser, Debug, Clone, PartialEq, Eq)]
pub struct GenerateKeyCommand {
/// Entropy level based on number of random seed mnemonic words.
/// Number of words in the generated mnemonic.
#[arg(
env = "WORD_COUNT",
short = 'e',
long = "entropy",
short = 'w',
long = "word_count",
value_enum,
default_value_t = MnemonicWordCount::Words12
)]
word_count: MnemonicWordCount,
/// Seed password.
#[arg(env = "PASSWORD", short = 'p', long = "password")]
password: Option<String>,
/// Optional BIP-39 passphrase. Restoring the key needs the same mnemonic and
/// passphrase; a different passphrase gives a different key, not an error.
#[arg(env = "PASSPHRASE", short = 'p', long = "passphrase")]
passphrase: Option<String>,
}

impl AppCommand<AppContext<Init>> for GenerateKeyCommand {
Expand All @@ -70,7 +71,7 @@ impl AppCommand<AppContext<Init>> for GenerateKeyCommand {
.map_err(|_| Error::Generic("Mnemonic generation error".to_string()))?;
let mnemonic = mnemonic.into_key();
let (xprv, fingerprint) =
derive_master_key_from_mnemonic(&mnemonic, self.password.clone(), ctx.network)?;
derive_master_key_from_mnemonic(&mnemonic, self.passphrase.clone(), ctx.network)?;

Ok(json!({
"xprv": xprv.to_string(),
Expand Down Expand Up @@ -127,16 +128,17 @@ pub struct RestoreKeyCommand {
/// Seed mnemonic words, must be quoted (eg. "word1 word2 ...").
#[arg(env = "MNEMONIC", short = 'm', long = "mnemonic")]
mnemonic: String,
/// Seed password.
#[arg(env = "PASSWORD", short = 'p', long = "password")]
password: Option<String>,
/// Optional BIP-39 passphrase. Restoring the key needs the same mnemonic and
/// passphrase; a different passphrase gives a different key, not an error.
#[arg(env = "PASSPHRASE", short = 'p', long = "passphrase")]
passphrase: Option<String>,
}

impl AppCommand<AppContext<Init>> for RestoreKeyCommand {
fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Value, Error> {
let mnemonic = Mnemonic::parse_in(Language::English, &self.mnemonic)?;
let (xprv, fingerprint) =
derive_master_key_from_mnemonic(&mnemonic, self.password.clone(), ctx.network)?;
derive_master_key_from_mnemonic(&mnemonic, self.passphrase.clone(), ctx.network)?;

Ok(json!({
"xprv": xprv.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/utils/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ pub fn is_mnemonic(s: &str) -> bool {
/// Derives the master key from a mnemonic and optional passphrase.
pub fn derive_master_key_from_mnemonic(
mnemonic: &Mnemonic,
password: Option<String>,
passphrase: Option<String>,
network: Network,
) -> Result<(Xpriv, Fingerprint), Error> {
let secp = Secp256k1::new();
let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
let xkey: ExtendedKey = (mnemonic.clone(), passphrase).into_extended_key()?;
let xprv = xkey
.into_xprv(network.into())
.ok_or_else(|| Error::Generic("Private key info not found".to_string()))?;
Expand Down
26 changes: 13 additions & 13 deletions tests/integration/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod test_key {

for word_count in [12, 24] {
let output = cli
.key_cmd(&["generate", "--entropy", &word_count.to_string()])
.key_cmd(&["generate", "--word_count", &word_count.to_string()])
.output()
.expect("Failed to execute generate command");
assert!(output.status.success(), "Generate command failed");
Expand All @@ -43,7 +43,7 @@ mod test_key {
let cli = BdkCli::new("testnet", Some(temp_dir.path().to_path_buf()));

for word_count in ["0", "13", "15", "18", "21", "999"] {
cli.key_cmd(&["generate", "--entropy", word_count])
cli.key_cmd(&["generate", "--word_count", word_count])
.assert()
.failure()
.stderr(predicate::str::contains("possible values"));
Expand Down Expand Up @@ -134,22 +134,22 @@ mod test_key {
}

#[test]
fn test_cli_key_restore_with_password() {
fn test_cli_key_restore_with_passphrase() {
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"])
.key_cmd(&["generate", "--passphrase", "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
// Restoring with the same passphrase must give back the same key
let restore_cmd = cli
.key_cmd(&["restore", "--mnemonic", mnemonic, "--password", "foo"])
.key_cmd(&["restore", "--mnemonic", mnemonic, "--passphrase", "foo"])
.output()
.expect("Failed to execute restore command");
assert!(restore_cmd.status.success(), "Restore command failed");
Expand All @@ -158,16 +158,16 @@ mod test_key {
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
// Restoring without the passphrase must give a different key
let no_passphrase_cmd = cli
.key_cmd(&["restore", "--mnemonic", mnemonic])
.env_remove("PASSWORD")
.env_remove("PASSPHRASE")
.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"]);
assert!(no_passphrase_cmd.status.success(), "Restore command failed");
let no_passphrase_json: Value =
serde_json::from_slice(&no_passphrase_cmd.stdout).expect("Failed to parse JSON");
assert_ne!(generate_json["xprv"], no_passphrase_json["xprv"]);
}
}

Expand Down
Loading