@@ -19,13 +19,18 @@ use std::fmt::Display;
19
19
use std:: str:: FromStr ;
20
20
use std:: sync:: Arc ;
21
21
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 ) ]
23
25
pub struct Descriptor {
24
26
pub extended_descriptor : ExtendedDescriptor ,
25
27
pub key_map : KeyMap ,
26
28
}
27
29
30
+ #[ uniffi:: export]
28
31
impl Descriptor {
32
+ /// Parse a string as a descriptor for the given network.
33
+ #[ uniffi:: constructor]
29
34
pub fn new ( descriptor : String , network : Network ) -> Result < Self , DescriptorError > {
30
35
let secp = Secp256k1 :: new ( ) ;
31
36
let ( extended_descriptor, key_map) = descriptor. into_wallet_descriptor ( & secp, network) ?;
@@ -35,6 +40,8 @@ impl Descriptor {
35
40
} )
36
41
}
37
42
43
+ /// Multi-account hierarchy descriptor: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
44
+ #[ uniffi:: constructor]
38
45
pub fn new_bip44 (
39
46
secret_key : & DescriptorSecretKey ,
40
47
keychain_kind : KeychainKind ,
@@ -61,6 +68,8 @@ impl Descriptor {
61
68
}
62
69
}
63
70
71
+ /// Multi-account hierarchy descriptor: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
72
+ #[ uniffi:: constructor]
64
73
pub fn new_bip44_public (
65
74
public_key : & DescriptorPublicKey ,
66
75
fingerprint : String ,
@@ -92,6 +101,8 @@ impl Descriptor {
92
101
}
93
102
}
94
103
104
+ /// P2SH nested P2WSH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
105
+ #[ uniffi:: constructor]
95
106
pub fn new_bip49 (
96
107
secret_key : & DescriptorSecretKey ,
97
108
keychain_kind : KeychainKind ,
@@ -118,6 +129,8 @@ impl Descriptor {
118
129
}
119
130
}
120
131
132
+ /// P2SH nested P2WSH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
133
+ #[ uniffi:: constructor]
121
134
pub fn new_bip49_public (
122
135
public_key : & DescriptorPublicKey ,
123
136
fingerprint : String ,
@@ -149,6 +162,8 @@ impl Descriptor {
149
162
}
150
163
}
151
164
165
+ /// Pay to witness PKH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
166
+ #[ uniffi:: constructor]
152
167
pub fn new_bip84 (
153
168
secret_key : & DescriptorSecretKey ,
154
169
keychain_kind : KeychainKind ,
@@ -175,6 +190,8 @@ impl Descriptor {
175
190
}
176
191
}
177
192
193
+ /// Pay to witness PKH descriptor: https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
194
+ #[ uniffi:: constructor]
178
195
pub fn new_bip84_public (
179
196
public_key : & DescriptorPublicKey ,
180
197
fingerprint : String ,
@@ -206,6 +223,8 @@ impl Descriptor {
206
223
}
207
224
}
208
225
226
+ /// Single key P2TR descriptor: https://github.com/bitcoin/bips/blob/master/bip-0086.mediawiki
227
+ #[ uniffi:: constructor]
209
228
pub fn new_bip86 (
210
229
secret_key : & DescriptorSecretKey ,
211
230
keychain_kind : KeychainKind ,
@@ -232,6 +251,8 @@ impl Descriptor {
232
251
}
233
252
}
234
253
254
+ /// Single key P2TR descriptor: https://github.com/bitcoin/bips/blob/master/bip-0086.mediawiki
255
+ #[ uniffi:: constructor]
235
256
pub fn new_bip86_public (
236
257
public_key : & DescriptorPublicKey ,
237
258
fingerprint : String ,
@@ -263,16 +284,19 @@ impl Descriptor {
263
284
}
264
285
}
265
286
287
+ /// Dangerously convert the descriptor to a string.
266
288
pub fn to_string_with_secret ( & self ) -> String {
267
289
let descriptor = & self . extended_descriptor ;
268
290
let key_map = & self . key_map ;
269
291
descriptor. to_string_with_secret ( key_map)
270
292
}
271
293
294
+ /// Does this descriptor contain paths: https://github.com/bitcoin/bips/blob/master/bip-0389.mediawiki
272
295
pub fn is_multipath ( & self ) -> bool {
273
296
self . extended_descriptor . is_multipath ( )
274
297
}
275
298
299
+ /// Return descriptors for all valid paths.
276
300
pub fn to_single_descriptors ( & self ) -> Result < Vec < Arc < Descriptor > > , MiniscriptError > {
277
301
self . extended_descriptor
278
302
. clone ( )
@@ -300,7 +324,8 @@ impl Display for Descriptor {
300
324
301
325
#[ cfg( test) ]
302
326
mod test {
303
- use crate :: * ;
327
+ use super :: * ;
328
+ use crate :: keys:: { DerivationPath , Mnemonic } ;
304
329
use assert_matches:: assert_matches;
305
330
use bdk_wallet:: bitcoin:: Network ;
306
331
use bdk_wallet:: KeychainKind ;
0 commit comments