Skip to content

Commit b433b37

Browse files
feat: expose Wallet::create_from_two_path_descriptor constructor
1 parent c0a6d00 commit b433b37

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/CreatingWalletTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ class CreatingWalletTest {
4242
)
4343
}
4444

45+
// Create a wallet with a public multipath descriptor.
46+
@Test
47+
fun createWalletWithMultipathDescriptor() {
48+
val multipathDescriptor = Descriptor(
49+
"wpkh([9a6a2580/84'/0'/0']xpub6DEzNop46vmxR49zYWFnMwmEfawSNmAMf6dLH5YKDY463twtvw1XD7ihwJRLPRGZJz799VPFzXHpZu6WdhT29WnaeuChS6aZHZPFmqczR5K/<0;1>/*)",
50+
Network.BITCOIN
51+
)
52+
53+
Wallet.createFromTwoPathDescriptor(
54+
twoPathDescriptor = multipathDescriptor,
55+
network = Network.BITCOIN,
56+
persister = conn
57+
)
58+
}
59+
60+
// You cannot create a private multipath descriptor.
61+
@Test
62+
fun cannotCreatePrivateMultipathDescriptor() {
63+
assertFails {
64+
Descriptor(
65+
descriptor = "tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/<0;1>/*)",
66+
Network.TESTNET
67+
)
68+
}
69+
}
70+
4571
// Descriptors do not match provided network.
4672
@Test
4773
fun failsIfDescriptorsDontMatchNetwork() {

bdk-ffi/src/wallet.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,43 @@ impl Wallet {
6767
})
6868
}
6969

70+
/// Build a new `Wallet` from a two-path descriptor.
71+
///
72+
/// This function parses a multipath descriptor with exactly 2 paths and creates a wallet using the existing receive and change wallet creation logic.
73+
///
74+
/// Multipath descriptors follow [BIP-389](https://github.com/bitcoin/bips/blob/master/bip-0389.mediawiki) and allow defining both receive and change derivation paths in a single descriptor using the <0;1> syntax.
75+
///
76+
/// If you have previously created a wallet, use load instead.
77+
///
78+
/// Returns an error if the descriptor is invalid or not a 2-path multipath descriptor.
79+
#[uniffi::constructor(default(lookahead = 25))]
80+
pub fn create_from_two_path_descriptor(
81+
two_path_descriptor: Arc<Descriptor>,
82+
network: Network,
83+
persister: Arc<Persister>,
84+
lookahead: u32,
85+
) -> Result<Self, CreateWithPersistError> {
86+
let descriptor = two_path_descriptor.to_string_with_secret();
87+
let mut persist_lock = persister.inner.lock().unwrap();
88+
let deref = persist_lock.deref_mut();
89+
90+
let wallet: PersistedWallet<PersistenceType> =
91+
BdkWallet::create_from_two_path_descriptor(descriptor)
92+
.network(network)
93+
.lookahead(lookahead)
94+
.create_wallet(deref)
95+
.map_err(|e| CreateWithPersistError::Persist {
96+
error_message: e.to_string(),
97+
})?;
98+
99+
Ok(Wallet {
100+
inner_mutex: Mutex::new(wallet),
101+
})
102+
}
103+
70104
/// Build Wallet by loading from persistence.
71-
//
72-
// Note that the descriptor secret keys are not persisted to the db.
105+
///
106+
/// Note that the descriptor secret keys are not persisted to the db.
73107
#[uniffi::constructor(default(lookahead = 25))]
74108
pub fn load(
75109
descriptor: Arc<Descriptor>,

0 commit comments

Comments
 (0)