@@ -67,10 +67,15 @@ impl DerivationPath {
67
67
}
68
68
}
69
69
70
- #[ derive( Debug ) ]
70
+ /// A descriptor containing secret data.
71
+ #[ derive( Debug , uniffi:: Object ) ]
72
+ #[ uniffi:: export( Debug , Display ) ]
71
73
pub struct DescriptorSecretKey ( pub ( crate ) BdkDescriptorSecretKey ) ;
72
74
75
+ #[ uniffi:: export]
73
76
impl DescriptorSecretKey {
77
+ /// Construct a secret descriptor using a mnemonic.
78
+ #[ uniffi:: constructor]
74
79
pub fn new ( network : Network , mnemonic : & Mnemonic , password : Option < String > ) -> Self {
75
80
let mnemonic = mnemonic. 0 . clone ( ) ;
76
81
let xkey: ExtendedKey = ( mnemonic, password) . into_extended_key ( ) . unwrap ( ) ;
@@ -83,12 +88,15 @@ impl DescriptorSecretKey {
83
88
Self ( descriptor_secret_key)
84
89
}
85
90
91
+ /// Attempt to parse a string as a descriptor secret key.
92
+ #[ uniffi:: constructor]
86
93
pub fn from_string ( private_key : String ) -> Result < Self , DescriptorKeyError > {
87
94
let descriptor_secret_key = BdkDescriptorSecretKey :: from_str ( private_key. as_str ( ) )
88
95
. map_err ( DescriptorKeyError :: from) ?;
89
96
Ok ( Self ( descriptor_secret_key) )
90
97
}
91
98
99
+ /// Derive a descriptor secret key at a given derivation path.
92
100
pub fn derive ( & self , path : & DerivationPath ) -> Result < Arc < Self > , DescriptorKeyError > {
93
101
let secp = Secp256k1 :: new ( ) ;
94
102
let descriptor_secret_key = & self . 0 ;
@@ -116,6 +124,7 @@ impl DescriptorSecretKey {
116
124
}
117
125
}
118
126
127
+ /// Extend the descriptor secret key by the derivation path.
119
128
pub fn extend ( & self , path : & DerivationPath ) -> Result < Arc < Self > , DescriptorKeyError > {
120
129
let descriptor_secret_key = & self . 0 ;
121
130
let path = path. inner_mutex . lock ( ) . unwrap ( ) . deref ( ) . clone ( ) ;
@@ -135,12 +144,14 @@ impl DescriptorSecretKey {
135
144
}
136
145
}
137
146
147
+ /// Return the descriptor public key corresponding to this secret.
138
148
pub fn as_public ( & self ) -> Arc < DescriptorPublicKey > {
139
149
let secp = Secp256k1 :: new ( ) ;
140
150
let descriptor_public_key = self . 0 . to_public ( & secp) . unwrap ( ) ;
141
151
Arc :: new ( DescriptorPublicKey ( descriptor_public_key) )
142
152
}
143
153
154
+ /// Return the bytes of this descriptor secret key.
144
155
pub fn secret_bytes ( & self ) -> Vec < u8 > {
145
156
let inner = & self . 0 ;
146
157
let secret_bytes: Vec < u8 > = match inner {
@@ -157,22 +168,30 @@ impl DescriptorSecretKey {
157
168
158
169
secret_bytes
159
170
}
171
+ }
160
172
161
- pub fn as_string ( & self ) -> String {
162
- self . 0 . to_string ( )
173
+ impl Display for DescriptorSecretKey {
174
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
175
+ self . 0 . fmt ( f)
163
176
}
164
177
}
165
178
166
- #[ derive( Debug ) ]
179
+ /// A descriptor public key.
180
+ #[ derive( Debug , uniffi:: Object ) ]
181
+ #[ uniffi:: export( Debug , Display ) ]
167
182
pub struct DescriptorPublicKey ( pub ( crate ) BdkDescriptorPublicKey ) ;
168
183
184
+ #[ uniffi:: export]
169
185
impl DescriptorPublicKey {
186
+ /// Attempt to parse a string as a descriptor public key.
187
+ #[ uniffi:: constructor]
170
188
pub fn from_string ( public_key : String ) -> Result < Self , DescriptorKeyError > {
171
189
let descriptor_public_key = BdkDescriptorPublicKey :: from_str ( public_key. as_str ( ) )
172
190
. map_err ( DescriptorKeyError :: from) ?;
173
191
Ok ( Self ( descriptor_public_key) )
174
192
}
175
193
194
+ /// Derive the descriptor public key at the given derivation path.
176
195
pub fn derive ( & self , path : & DerivationPath ) -> Result < Arc < Self > , DescriptorKeyError > {
177
196
let secp = Secp256k1 :: new ( ) ;
178
197
let descriptor_public_key = & self . 0 ;
@@ -201,6 +220,7 @@ impl DescriptorPublicKey {
201
220
}
202
221
}
203
222
223
+ /// Extend the descriptor public key by the given derivation path.
204
224
pub fn extend ( & self , path : & DerivationPath ) -> Result < Arc < Self > , DescriptorKeyError > {
205
225
let descriptor_public_key = & self . 0 ;
206
226
let path = path. inner_mutex . lock ( ) . unwrap ( ) . deref ( ) . clone ( ) ;
@@ -220,19 +240,23 @@ impl DescriptorPublicKey {
220
240
}
221
241
}
222
242
223
- pub fn as_string ( & self ) -> String {
224
- self . 0 . to_string ( )
225
- }
226
-
243
+ /// Whether or not this key has multiple derivation paths.
227
244
pub fn is_multipath ( & self ) -> bool {
228
245
self . 0 . is_multipath ( )
229
246
}
230
247
248
+ /// The fingerprint of the master key associated with this key, `0x00000000` if none.
231
249
pub fn master_fingerprint ( & self ) -> String {
232
250
self . 0 . master_fingerprint ( ) . to_string ( )
233
251
}
234
252
}
235
253
254
+ impl Display for DescriptorPublicKey {
255
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
256
+ self . 0 . fmt ( f)
257
+ }
258
+ }
259
+
236
260
#[ cfg( test) ]
237
261
mod test {
238
262
use crate :: error:: DescriptorKeyError ;
@@ -280,38 +304,38 @@ mod test {
280
304
#[ test]
281
305
fn test_generate_descriptor_secret_key ( ) {
282
306
let master_dsk = get_inner ( ) ;
283
- assert_eq ! ( master_dsk. as_string ( ) , "tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/*" ) ;
284
- assert_eq ! ( master_dsk. as_public( ) . as_string ( ) , "tpubD6NzVbkrYhZ4WywdEfYbbd62yuvqLjAZuPsNyvzCNV85JekAEMbKHWSHLF9h3j45SxewXDcLv328B1SEZrxg4iwGfmdt1pDFjZiTkGiFqGa/*" ) ;
307
+ assert_eq ! ( master_dsk. to_string ( ) , "tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/*" ) ;
308
+ assert_eq ! ( master_dsk. as_public( ) . to_string ( ) , "tpubD6NzVbkrYhZ4WywdEfYbbd62yuvqLjAZuPsNyvzCNV85JekAEMbKHWSHLF9h3j45SxewXDcLv328B1SEZrxg4iwGfmdt1pDFjZiTkGiFqGa/*" ) ;
285
309
}
286
310
287
311
#[ test]
288
312
fn test_derive_self ( ) {
289
313
let master_dsk = get_inner ( ) ;
290
314
let derived_dsk: & DescriptorSecretKey = & derive_dsk ( & master_dsk, "m" ) . unwrap ( ) ;
291
- assert_eq ! ( derived_dsk. as_string ( ) , "[d1d04177]tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/*" ) ;
315
+ assert_eq ! ( derived_dsk. to_string ( ) , "[d1d04177]tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/*" ) ;
292
316
let master_dpk: & DescriptorPublicKey = & master_dsk. as_public ( ) ;
293
317
let derived_dpk: & DescriptorPublicKey = & derive_dpk ( master_dpk, "m" ) . unwrap ( ) ;
294
- assert_eq ! ( derived_dpk. as_string ( ) , "[d1d04177]tpubD6NzVbkrYhZ4WywdEfYbbd62yuvqLjAZuPsNyvzCNV85JekAEMbKHWSHLF9h3j45SxewXDcLv328B1SEZrxg4iwGfmdt1pDFjZiTkGiFqGa/*" ) ;
318
+ assert_eq ! ( derived_dpk. to_string ( ) , "[d1d04177]tpubD6NzVbkrYhZ4WywdEfYbbd62yuvqLjAZuPsNyvzCNV85JekAEMbKHWSHLF9h3j45SxewXDcLv328B1SEZrxg4iwGfmdt1pDFjZiTkGiFqGa/*" ) ;
295
319
}
296
320
297
321
#[ test]
298
322
fn test_derive_descriptors_keys ( ) {
299
323
let master_dsk = get_inner ( ) ;
300
324
let derived_dsk: & DescriptorSecretKey = & derive_dsk ( & master_dsk, "m/0" ) . unwrap ( ) ;
301
- assert_eq ! ( derived_dsk. as_string ( ) , "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/*" ) ;
325
+ assert_eq ! ( derived_dsk. to_string ( ) , "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/*" ) ;
302
326
let master_dpk: & DescriptorPublicKey = & master_dsk. as_public ( ) ;
303
327
let derived_dpk: & DescriptorPublicKey = & derive_dpk ( master_dpk, "m/0" ) . unwrap ( ) ;
304
- assert_eq ! ( derived_dpk. as_string ( ) , "[d1d04177/0]tpubD9oaCiP1MPmQdndm7DCD3D3QU34pWd6BbKSRedoZF1UJcNhEk3PJwkALNYkhxeTKL29oGNR7psqvT1KZydCGqUDEKXN6dVQJY2R8ooLPy8m/*" ) ;
328
+ assert_eq ! ( derived_dpk. to_string ( ) , "[d1d04177/0]tpubD9oaCiP1MPmQdndm7DCD3D3QU34pWd6BbKSRedoZF1UJcNhEk3PJwkALNYkhxeTKL29oGNR7psqvT1KZydCGqUDEKXN6dVQJY2R8ooLPy8m/*" ) ;
305
329
}
306
330
307
331
#[ test]
308
332
fn test_extend_descriptor_keys ( ) {
309
333
let master_dsk = get_inner ( ) ;
310
334
let extended_dsk: & DescriptorSecretKey = & extend_dsk ( & master_dsk, "m/0" ) . unwrap ( ) ;
311
- assert_eq ! ( extended_dsk. as_string ( ) , "tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/0/*" ) ;
335
+ assert_eq ! ( extended_dsk. to_string ( ) , "tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/0/*" ) ;
312
336
let master_dpk: & DescriptorPublicKey = & master_dsk. as_public ( ) ;
313
337
let extended_dpk: & DescriptorPublicKey = & extend_dpk ( master_dpk, "m/0" ) . unwrap ( ) ;
314
- assert_eq ! ( extended_dpk. as_string ( ) , "tpubD6NzVbkrYhZ4WywdEfYbbd62yuvqLjAZuPsNyvzCNV85JekAEMbKHWSHLF9h3j45SxewXDcLv328B1SEZrxg4iwGfmdt1pDFjZiTkGiFqGa/0/*" ) ;
338
+ assert_eq ! ( extended_dpk. to_string ( ) , "tpubD6NzVbkrYhZ4WywdEfYbbd62yuvqLjAZuPsNyvzCNV85JekAEMbKHWSHLF9h3j45SxewXDcLv328B1SEZrxg4iwGfmdt1pDFjZiTkGiFqGa/0/*" ) ;
315
339
let wif = "L2wTu6hQrnDMiFNWA5na6jB12ErGQqtXwqpSL7aWquJaZG8Ai3ch" ;
316
340
let extended_key = DescriptorSecretKey :: from_string ( wif. to_string ( ) ) . unwrap ( ) ;
317
341
let result = extended_key. derive ( & DerivationPath :: new ( "m/0" . to_string ( ) ) . unwrap ( ) ) ;
@@ -337,10 +361,10 @@ mod test {
337
361
let master_dsk = get_inner ( ) ;
338
362
// derive DescriptorSecretKey with path "m/0" from master
339
363
let derived_dsk: & DescriptorSecretKey = & derive_dsk ( & master_dsk, "m/0" ) . unwrap ( ) ;
340
- assert_eq ! ( derived_dsk. as_string ( ) , "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/*" ) ;
364
+ assert_eq ! ( derived_dsk. to_string ( ) , "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/*" ) ;
341
365
// extend derived_dsk with path "m/0"
342
366
let extended_dsk: & DescriptorSecretKey = & extend_dsk ( derived_dsk, "m/0" ) . unwrap ( ) ;
343
- assert_eq ! ( extended_dsk. as_string ( ) , "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/0/*" ) ;
367
+ assert_eq ! ( extended_dsk. to_string ( ) , "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/0/*" ) ;
344
368
}
345
369
346
370
#[ test]
0 commit comments