Skip to content

Commit b074c7a

Browse files
committed
refactor: use macros for Descriptor
1 parent 10033b9 commit b074c7a

File tree

4 files changed

+28
-46
lines changed

4 files changed

+28
-46
lines changed

bdk-ffi/src/bdk.udl

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -500,48 +500,6 @@ interface DescriptorPublicKey {
500500
string master_fingerprint();
501501
};
502502

503-
[Traits=(Display)]
504-
interface Descriptor {
505-
[Throws=DescriptorError]
506-
constructor(string descriptor, Network network);
507-
508-
[Name=new_bip44]
509-
constructor([ByRef] DescriptorSecretKey secret_key, KeychainKind keychain, Network network);
510-
511-
[Name=new_bip44_public]
512-
constructor([ByRef] DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain, Network network);
513-
514-
[Name=new_bip49]
515-
constructor([ByRef] DescriptorSecretKey secret_key, KeychainKind keychain, Network network);
516-
517-
[Name=new_bip49_public]
518-
constructor([ByRef] DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain, Network network);
519-
520-
[Name=new_bip84]
521-
constructor([ByRef] DescriptorSecretKey secret_key, KeychainKind keychain, Network network);
522-
523-
[Name=new_bip84_public]
524-
constructor([ByRef] DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain, Network network);
525-
526-
[Name=new_bip86]
527-
constructor([ByRef] DescriptorSecretKey secret_key, KeychainKind keychain, Network network);
528-
529-
[Name=new_bip86_public]
530-
constructor([ByRef] DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain, Network network);
531-
532-
string to_string_with_secret();
533-
534-
/// Whether or not this key has multiple derivation paths.
535-
boolean is_multipath();
536-
537-
/// Get as many descriptors as different paths in this descriptor.
538-
///
539-
/// For multipath descriptors it will return as many descriptors as there is
540-
/// "parallel" paths. For regular descriptors it will just return itself.
541-
[Throws=MiniscriptError]
542-
sequence<Descriptor> to_single_descriptors();
543-
};
544-
545503
// ------------------------------------------------------------------------
546504
// bdk-ffi-defined types
547505
// ------------------------------------------------------------------------

bdk-ffi/src/descriptor.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ use std::fmt::Display;
1919
use std::str::FromStr;
2020
use std::sync::Arc;
2121

22-
#[derive(Debug)]
22+
/// An expression of how to derive output scripts: https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md
23+
#[derive(Debug, uniffi::Object)]
24+
#[uniffi::export(Debug, Display)]
2325
pub struct Descriptor {
2426
pub extended_descriptor: ExtendedDescriptor,
2527
pub key_map: KeyMap,
2628
}
2729

30+
#[uniffi::export]
2831
impl Descriptor {
32+
/// Parse a string as a descriptor for the given network.
33+
#[uniffi::constructor]
2934
pub fn new(descriptor: String, network: Network) -> Result<Self, DescriptorError> {
3035
let secp = Secp256k1::new();
3136
let (extended_descriptor, key_map) = descriptor.into_wallet_descriptor(&secp, network)?;
@@ -35,6 +40,8 @@ impl Descriptor {
3540
})
3641
}
3742

43+
/// Multi-account hierarchy descriptor: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
44+
#[uniffi::constructor]
3845
pub fn new_bip44(
3946
secret_key: &DescriptorSecretKey,
4047
keychain_kind: KeychainKind,
@@ -61,6 +68,8 @@ impl Descriptor {
6168
}
6269
}
6370

71+
/// Multi-account hierarchy descriptor: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
72+
#[uniffi::constructor]
6473
pub fn new_bip44_public(
6574
public_key: &DescriptorPublicKey,
6675
fingerprint: String,
@@ -92,6 +101,8 @@ impl Descriptor {
92101
}
93102
}
94103

104+
/// P2SH nested P2WSH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
105+
#[uniffi::constructor]
95106
pub fn new_bip49(
96107
secret_key: &DescriptorSecretKey,
97108
keychain_kind: KeychainKind,
@@ -118,6 +129,8 @@ impl Descriptor {
118129
}
119130
}
120131

132+
/// P2SH nested P2WSH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
133+
#[uniffi::constructor]
121134
pub fn new_bip49_public(
122135
public_key: &DescriptorPublicKey,
123136
fingerprint: String,
@@ -149,6 +162,8 @@ impl Descriptor {
149162
}
150163
}
151164

165+
/// Pay to witness PKH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
166+
#[uniffi::constructor]
152167
pub fn new_bip84(
153168
secret_key: &DescriptorSecretKey,
154169
keychain_kind: KeychainKind,
@@ -175,6 +190,8 @@ impl Descriptor {
175190
}
176191
}
177192

193+
/// Pay to witness PKH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
194+
#[uniffi::constructor]
178195
pub fn new_bip84_public(
179196
public_key: &DescriptorPublicKey,
180197
fingerprint: String,
@@ -206,6 +223,8 @@ impl Descriptor {
206223
}
207224
}
208225

226+
/// Single key P2TR descriptor: https://github.com/bitcoin/bips/blob/master/bip-0086.mediawiki
227+
#[uniffi::constructor]
209228
pub fn new_bip86(
210229
secret_key: &DescriptorSecretKey,
211230
keychain_kind: KeychainKind,
@@ -232,6 +251,8 @@ impl Descriptor {
232251
}
233252
}
234253

254+
/// Single key P2TR descriptor: https://github.com/bitcoin/bips/blob/master/bip-0086.mediawiki
255+
#[uniffi::constructor]
235256
pub fn new_bip86_public(
236257
public_key: &DescriptorPublicKey,
237258
fingerprint: String,
@@ -263,16 +284,19 @@ impl Descriptor {
263284
}
264285
}
265286

287+
/// Dangerously convert the descriptor to a string.
266288
pub fn to_string_with_secret(&self) -> String {
267289
let descriptor = &self.extended_descriptor;
268290
let key_map = &self.key_map;
269291
descriptor.to_string_with_secret(key_map)
270292
}
271293

294+
/// Does this descriptor contain paths: https://github.com/bitcoin/bips/blob/master/bip-0389.mediawiki
272295
pub fn is_multipath(&self) -> bool {
273296
self.extended_descriptor.is_multipath()
274297
}
275298

299+
/// Return descriptors for all valid paths.
276300
pub fn to_single_descriptors(&self) -> Result<Vec<Arc<Descriptor>>, MiniscriptError> {
277301
self.extended_descriptor
278302
.clone()
@@ -300,7 +324,8 @@ impl Display for Descriptor {
300324

301325
#[cfg(test)]
302326
mod test {
303-
use crate::*;
327+
use super::*;
328+
use crate::keys::{DerivationPath, Mnemonic};
304329
use assert_matches::assert_matches;
305330
use bdk_wallet::bitcoin::Network;
306331
use bdk_wallet::KeychainKind;

bdk-ffi/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::bitcoin::FeeRate;
1515
use crate::bitcoin::OutPoint;
1616
use crate::bitcoin::Script;
1717
use crate::bitcoin::TxOut;
18-
use crate::descriptor::Descriptor;
1918
use crate::error::AddressParseError;
2019
use crate::error::Bip32Error;
2120
use crate::error::Bip39Error;

bdk-swift/Tests/BitcoinDevKitTests/OfflineDescriptorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class OfflineDescriptorTests: XCTestCase {
1111
)
1212
let descriptor: Descriptor = Descriptor.newBip86(
1313
secretKey: descriptorSecretKey,
14-
keychain: KeychainKind.external,
14+
keychainKind: KeychainKind.external,
1515
network: Network.testnet
1616
)
1717

0 commit comments

Comments
 (0)