@@ -25,7 +25,6 @@ use bitcoin::blockdata::script;
25
25
use bitcoin:: { Address , Network , Script } ;
26
26
27
27
use super :: checksum:: { desc_checksum, verify_checksum} ;
28
- use super :: DescriptorTrait ;
29
28
use crate :: expression:: { self , FromTree } ;
30
29
use crate :: miniscript:: context:: ScriptContext ;
31
30
use crate :: policy:: { semantic, Liftable } ;
@@ -60,25 +59,42 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
60
59
pub fn as_inner ( & self ) -> & Miniscript < Pk , BareCtx > {
61
60
& self . ms
62
61
}
62
+
63
+ /// Checks whether the descriptor is safe.
64
+ pub fn sanity_check ( & self ) -> Result < ( ) , Error > {
65
+ self . ms . sanity_check ( ) ?;
66
+ Ok ( ( ) )
67
+ }
68
+
69
+ /// Computes an upper bound on the weight of a satisfying witness to the
70
+ /// transaction.
71
+ ///
72
+ /// Assumes all ec-signatures are 73 bytes, including push opcode and
73
+ /// sighash suffix. Includes the weight of the VarInts encoding the
74
+ /// scriptSig and witness stack length.
75
+ ///
76
+ /// # Errors
77
+ /// When the descriptor is impossible to safisfy (ex: sh(OP_FALSE)).
78
+ pub fn max_satisfaction_weight ( & self ) -> Result < usize , Error > {
79
+ let scriptsig_len = self . ms . max_satisfaction_size ( ) ?;
80
+ Ok ( 4 * ( varint_len ( scriptsig_len) + scriptsig_len) )
81
+ }
63
82
}
64
83
65
84
impl < Pk : MiniscriptKey + ToPublicKey > Bare < Pk > {
66
- /// Obtain the corresponding script pubkey for this descriptor
67
- /// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
68
- pub fn spk ( & self ) -> Script {
85
+ /// Obtains the corresponding script pubkey for this descriptor.
86
+ pub fn script_pubkey ( & self ) -> Script {
69
87
self . ms . encode ( )
70
88
}
71
89
72
- /// Obtain the underlying miniscript for this descriptor
73
- /// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
90
+ /// Obtains the underlying miniscript for this descriptor.
74
91
pub fn inner_script ( & self ) -> Script {
75
- self . spk ( )
92
+ self . script_pubkey ( )
76
93
}
77
94
78
- /// Obtain the pre bip-340 signature script code for this descriptor
79
- /// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
95
+ /// Obtains the pre bip-340 signature script code for this descriptor.
80
96
pub fn ecdsa_sighash_script_code ( & self ) -> Script {
81
- self . spk ( )
97
+ self . script_pubkey ( )
82
98
}
83
99
}
84
100
@@ -132,43 +148,12 @@ where
132
148
}
133
149
}
134
150
135
- impl < Pk : MiniscriptKey > DescriptorTrait < Pk > for Bare < Pk > {
136
- fn sanity_check ( & self ) -> Result < ( ) , Error > {
137
- self . ms . sanity_check ( ) ?;
138
- Ok ( ( ) )
139
- }
140
-
141
- fn address ( & self , _network : Network ) -> Result < Address , Error >
142
- where
143
- Pk : ToPublicKey ,
144
- {
145
- Err ( Error :: BareDescriptorAddr )
146
- }
147
-
148
- fn script_pubkey ( & self ) -> Script
149
- where
150
- Pk : ToPublicKey ,
151
- {
152
- self . spk ( )
153
- }
154
-
155
- fn unsigned_script_sig ( & self ) -> Script
156
- where
157
- Pk : ToPublicKey ,
158
- {
159
- Script :: new ( )
160
- }
161
-
162
- fn explicit_script ( & self ) -> Result < Script , Error >
163
- where
164
- Pk : ToPublicKey ,
165
- {
166
- Ok ( self . inner_script ( ) )
167
- }
168
-
169
- fn get_satisfaction < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
151
+ impl < Pk : MiniscriptKey + ToPublicKey > Bare < Pk > {
152
+ /// Returns satisfying non-malleable witness and scriptSig with minimum
153
+ /// weight to spend an output controlled by the given descriptor if it is
154
+ /// possible to construct one using the `satisfier`.
155
+ pub fn get_satisfaction < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
170
156
where
171
- Pk : ToPublicKey ,
172
157
S : Satisfier < Pk > ,
173
158
{
174
159
let ms = self . ms . satisfy ( satisfier) ?;
@@ -177,28 +162,18 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
177
162
Ok ( ( witness, script_sig) )
178
163
}
179
164
180
- fn get_satisfaction_mall < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
165
+ /// Returns satisfying, possibly malleable, witness and scriptSig with
166
+ /// minimum weight to spend an output controlled by the given descriptor if
167
+ /// it is possible to construct one using the `satisfier`.
168
+ pub fn get_satisfaction_mall < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
181
169
where
182
- Pk : ToPublicKey ,
183
170
S : Satisfier < Pk > ,
184
171
{
185
172
let ms = self . ms . satisfy_malleable ( satisfier) ?;
186
173
let script_sig = witness_to_scriptsig ( & ms) ;
187
174
let witness = vec ! [ ] ;
188
175
Ok ( ( witness, script_sig) )
189
176
}
190
-
191
- fn max_satisfaction_weight ( & self ) -> Result < usize , Error > {
192
- let scriptsig_len = self . ms . max_satisfaction_size ( ) ?;
193
- Ok ( 4 * ( varint_len ( scriptsig_len) + scriptsig_len) )
194
- }
195
-
196
- fn script_code ( & self ) -> Result < Script , Error >
197
- where
198
- Pk : ToPublicKey ,
199
- {
200
- Ok ( self . ecdsa_sighash_script_code ( ) )
201
- }
202
177
}
203
178
204
179
impl < Pk : MiniscriptKey > ForEachKey < Pk > for Bare < Pk > {
@@ -254,30 +229,40 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
254
229
}
255
230
}
256
231
232
+ impl < Pk : MiniscriptKey > Pkh < Pk > {
233
+ /// Computes an upper bound on the weight of a satisfying witness to the
234
+ /// transaction.
235
+ ///
236
+ /// Assumes all ec-signatures are 73 bytes, including push opcode and
237
+ /// sighash suffix. Includes the weight of the VarInts encoding the
238
+ /// scriptSig and witness stack length.
239
+ pub fn max_satisfaction_weight ( & self ) -> usize {
240
+ 4 * ( 1 + 73 + BareCtx :: pk_len ( & self . pk ) )
241
+ }
242
+ }
243
+
257
244
impl < Pk : MiniscriptKey + ToPublicKey > Pkh < Pk > {
258
- /// Obtain the corresponding script pubkey for this descriptor
259
- /// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
260
- pub fn spk ( & self ) -> Script {
261
- let addr = Address :: p2pkh ( & self . pk . to_public_key ( ) , Network :: Bitcoin ) ;
245
+ /// Obtains the corresponding script pubkey for this descriptor.
246
+ pub fn script_pubkey ( & self ) -> Script {
247
+ // Fine to hard code the `Network` here because we immediately call
248
+ // `script_pubkey` which does not use the `network` field of `Address`.
249
+ let addr = self . address ( Network :: Bitcoin ) ;
262
250
addr. script_pubkey ( )
263
251
}
264
252
265
- /// Obtain the corresponding script pubkey for this descriptor
266
- /// Non failing verion of [`DescriptorTrait::address`] for this descriptor
267
- pub fn addr ( & self , network : Network ) -> Address {
253
+ /// Obtains the corresponding script pubkey for this descriptor.
254
+ pub fn address ( & self , network : Network ) -> Address {
268
255
Address :: p2pkh ( & self . pk . to_public_key ( ) , network)
269
256
}
270
257
271
- /// Obtain the underlying miniscript for this descriptor
272
- /// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
258
+ /// Obtains the underlying miniscript for this descriptor.
273
259
pub fn inner_script ( & self ) -> Script {
274
- self . spk ( )
260
+ self . script_pubkey ( )
275
261
}
276
262
277
- /// Obtain the pre bip-340 signature script code for this descriptor
278
- /// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
263
+ /// Obtains the pre bip-340 signature script code for this descriptor.
279
264
pub fn ecdsa_sighash_script_code ( & self ) -> Script {
280
- self . spk ( )
265
+ self . script_pubkey ( )
281
266
}
282
267
}
283
268
@@ -339,42 +324,12 @@ where
339
324
}
340
325
}
341
326
342
- impl < Pk : MiniscriptKey > DescriptorTrait < Pk > for Pkh < Pk > {
343
- fn sanity_check ( & self ) -> Result < ( ) , Error > {
344
- Ok ( ( ) )
345
- }
346
-
347
- fn address ( & self , network : Network ) -> Result < Address , Error >
348
- where
349
- Pk : ToPublicKey ,
350
- {
351
- Ok ( self . addr ( network) )
352
- }
353
-
354
- fn script_pubkey ( & self ) -> Script
355
- where
356
- Pk : ToPublicKey ,
357
- {
358
- self . spk ( )
359
- }
360
-
361
- fn unsigned_script_sig ( & self ) -> Script
362
- where
363
- Pk : ToPublicKey ,
364
- {
365
- Script :: new ( )
366
- }
367
-
368
- fn explicit_script ( & self ) -> Result < Script , Error >
369
- where
370
- Pk : ToPublicKey ,
371
- {
372
- Ok ( self . inner_script ( ) )
373
- }
374
-
375
- fn get_satisfaction < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
327
+ impl < Pk : MiniscriptKey + ToPublicKey > Pkh < Pk > {
328
+ /// Returns satisfying non-malleable witness and scriptSig with minimum
329
+ /// weight to spend an output controlled by the given descriptor if it is
330
+ /// possible to construct one using the `satisfier`.
331
+ pub fn get_satisfaction < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
376
332
where
377
- Pk : ToPublicKey ,
378
333
S : Satisfier < Pk > ,
379
334
{
380
335
if let Some ( sig) = satisfier. lookup_ecdsa_sig ( & self . pk ) {
@@ -390,24 +345,15 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
390
345
}
391
346
}
392
347
393
- fn get_satisfaction_mall < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
348
+ /// Returns satisfying, possibly malleable, witness and scriptSig with
349
+ /// minimum weight to spend an output controlled by the given descriptor if
350
+ /// it is possible to construct one using the `satisfier`.
351
+ pub fn get_satisfaction_mall < S > ( & self , satisfier : S ) -> Result < ( Vec < Vec < u8 > > , Script ) , Error >
394
352
where
395
- Pk : ToPublicKey ,
396
353
S : Satisfier < Pk > ,
397
354
{
398
355
self . get_satisfaction ( satisfier)
399
356
}
400
-
401
- fn max_satisfaction_weight ( & self ) -> Result < usize , Error > {
402
- Ok ( 4 * ( 1 + 73 + BareCtx :: pk_len ( & self . pk ) ) )
403
- }
404
-
405
- fn script_code ( & self ) -> Result < Script , Error >
406
- where
407
- Pk : ToPublicKey ,
408
- {
409
- Ok ( self . ecdsa_sighash_script_code ( ) )
410
- }
411
357
}
412
358
413
359
impl < Pk : MiniscriptKey > ForEachKey < Pk > for Pkh < Pk > {
0 commit comments