Skip to content

Commit 54562f8

Browse files
committed
added multipath function
1 parent 337f25d commit 54562f8

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

src/descriptor_handler.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::commands::GenerateDescriptorArgs;
22
use anyhow::{anyhow, Result};
3-
use bdk_wallet::bitcoin::bip32::Xpriv;
3+
#[allow(deprecated)]
4+
use bdk_wallet::bitcoin::bip32::{ExtendedPubKey, Xpriv};
45
use bdk_wallet::bitcoin::secp256k1::Secp256k1;
56
use bdk_wallet::bitcoin::Network;
67
use bdk_wallet::descriptor::Segwitv0;
@@ -76,13 +77,47 @@ pub fn generate_new_bip84_descriptor_with_mnemonic(network: Network) -> Result<s
7677
}))
7778
}
7879

79-
fn generate_multipath_descriptor(
80-
_network: &Network,
80+
pub fn generate_multipath_descriptor(
81+
network: &Network,
8182
script_type: u8,
8283
key: &str,
83-
) -> Result<Value, anyhow::Error> {
84-
// TODO: Implement multipath logic
85-
Ok(json!({ "multipath": true, "key": key, "type": script_type }))
84+
) -> Result<Value> {
85+
// Only BIP84 supported in this example
86+
if script_type != 84 {
87+
return Err(anyhow!("Only BIP84 is supported for multipath at the moment."));
88+
}
89+
90+
#[allow(deprecated)]
91+
let xpub: ExtendedPubKey = key.parse().map_err(|e| anyhow!("Invalid xpub: {e}"))?;
92+
93+
let derivation_path = DerivationPath::from_str("m/84h/1h/0h")?;
94+
let fingerprint = xpub.fingerprint();
95+
96+
let make_desc = |change: u32| -> Result<(String, DescriptorPublicKey)> {
97+
let branch_path = DerivationPath::from_str(&change.to_string())?;
98+
99+
let desc_xpub = DescriptorXKey {
100+
origin: Some((fingerprint, derivation_path.clone())),
101+
xkey: xpub,
102+
derivation_path: branch_path,
103+
wildcard: Wildcard::Unhardened,
104+
};
105+
106+
let desc_key = DescriptorPublicKey::XPub(desc_xpub);
107+
let descriptor = Descriptor::new_wpkh(desc_key.clone())?;
108+
Ok((descriptor.to_string(), desc_key))
109+
};
110+
111+
let (external_desc, _) = make_desc(0)?;
112+
let (internal_desc, _) = make_desc(1)?;
113+
114+
Ok(json!({
115+
"type": "bip84-multipath",
116+
"external": external_desc,
117+
"internal": internal_desc,
118+
"fingerprint": fingerprint.to_string(),
119+
"network": network.to_string(),
120+
}))
86121
}
87122

88123
fn generate_standard_descriptor(

0 commit comments

Comments
 (0)