]> Untitled Git - bdk-cli/commitdiff
refactor(key): share derivation between generate and restore
authorIkeh Celestine Chukwudi <Chukwudi.ikeh.232330@unn.edu.ng>
Wed, 11 Nov 2026 14:08:24 +0000 (15:08 +0100)
committerIkeh Celestine Chukwudi <Chukwudi.ikeh.232330@unn.edu.ng>
Mon, 30 Nov 2026 20:29:28 +0000 (21:29 +0100)
GenerateKeyCommand and RestoreKeyCommand repeated the same steps to
derive the master key from a mnemonic, and the duplication is how
#372 happened. Move them into derive_master_key_from_mnemonic, called
by both, and use the Display impl of Mnemonic instead of the manual
fold, which gives the same output.

Fixes #382

src/handlers/key.rs

index 648177e42ea0af949469bdb642e2784eca8ab9f3..4b67b11ac8da0222d61121dedecf9c7a5fd9c4cd 100644 (file)
@@ -4,7 +4,7 @@ use crate::handlers::{AppCommand, AppContext, Init};
 use crate::utils::{output::FormatOutput, types::KeyResult};
 use bdk_wallet::bip39::{Language, Mnemonic};
 use bdk_wallet::bitcoin::bip32::{DerivationPath, KeySource, Xpriv};
-use bdk_wallet::bitcoin::key::Secp256k1;
+use bdk_wallet::bitcoin::{Network, key::Secp256k1};
 use bdk_wallet::keys::{
     DerivableKey, DescriptorKey, ExtendedKey, GeneratableKey, GeneratedKey, bip39::WordCount,
 };
@@ -66,28 +66,10 @@ impl AppCommand<AppContext<Init>> for GenerateKeyCommand {
     type Output = KeyResult;
 
     fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
-        let secp = Secp256k1::new();
         let mnemonic: GeneratedKey<_, miniscript::BareCtx> =
             Mnemonic::generate((self.word_count.into(), Language::English))
                 .map_err(|_| Error::Generic("Mnemonic generation error".to_string()))?;
-        let mnemonic = mnemonic.into_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())
-        })?;
-        let fingerprint = xprv.fingerprint(&secp);
-        let phrase = mnemonic
-            .words()
-            .fold("".to_string(), |phrase, w| phrase + w + " ")
-            .trim()
-            .to_string();
-
-        Ok(KeyResult {
-            xprv: xprv.to_string(),
-            mnemonic: Some(phrase),
-            fingerprint: Some(fingerprint.to_string()),
-            xpub: None,
-        })
+        derive_master_key_from_mnemonic(mnemonic.into_key(), self.password.clone(), ctx.network)
     }
 }
 
@@ -151,20 +133,28 @@ impl AppCommand<AppContext<Init>> for RestoreKeyCommand {
     type Output = KeyResult;
 
     fn execute(&self, ctx: &mut AppContext<Init>) -> Result<Self::Output, Error> {
-        let secp = Secp256k1::new();
-
         let mnemonic = Mnemonic::parse_in(Language::English, &self.mnemonic)?;
-        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())
-        })?;
-        let fingerprint = xprv.fingerprint(&secp);
-
-        Ok(KeyResult {
-            xprv: xprv.to_string(),
-            mnemonic: Some(mnemonic.to_string()),
-            fingerprint: Some(fingerprint.to_string()),
-            xpub: None,
-        })
+        derive_master_key_from_mnemonic(mnemonic, self.password.clone(), ctx.network)
     }
 }
+
+/// Derives the master key from a mnemonic and optional passphrase.
+fn derive_master_key_from_mnemonic(
+    mnemonic: Mnemonic,
+    password: Option<String>,
+    network: Network,
+) -> Result<KeyResult, Error> {
+    let secp = Secp256k1::new();
+    let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
+    let xprv = xkey
+        .into_xprv(network.into())
+        .ok_or_else(|| Error::Generic("Private key info not found".to_string()))?;
+    let fingerprint = xprv.fingerprint(&secp);
+
+    Ok(KeyResult {
+        xprv: xprv.to_string(),
+        mnemonic: Some(mnemonic.to_string()),
+        fingerprint: Some(fingerprint.to_string()),
+        xpub: None,
+    })
+}