|
1 | 1 | use crate::commands::GenerateDescriptorArgs; |
2 | 2 | use anyhow::{anyhow, Result}; |
3 | | -use bdk_wallet::bitcoin::bip32::Xpriv; |
| 3 | +#[allow(deprecated)] |
| 4 | +use bdk_wallet::bitcoin::bip32::{ExtendedPubKey, Xpriv}; |
4 | 5 | use bdk_wallet::bitcoin::secp256k1::Secp256k1; |
5 | 6 | use bdk_wallet::bitcoin::Network; |
6 | 7 | use bdk_wallet::descriptor::Segwitv0; |
@@ -76,13 +77,47 @@ pub fn generate_new_bip84_descriptor_with_mnemonic(network: Network) -> Result<s |
76 | 77 | })) |
77 | 78 | } |
78 | 79 |
|
79 | | -fn generate_multipath_descriptor( |
80 | | - _network: &Network, |
| 80 | +pub fn generate_multipath_descriptor( |
| 81 | + network: &Network, |
81 | 82 | script_type: u8, |
82 | 83 | 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 | + })) |
86 | 121 | } |
87 | 122 |
|
88 | 123 | fn generate_standard_descriptor( |
|
0 commit comments