@@ -39,63 +39,63 @@ use super::taproot::LeafVersion;
39
39
/// Efficiently calculates signature hash message for legacy, segwit and taproot inputs.
40
40
#[ derive( Debug ) ]
41
41
pub struct SighashCache < T : Deref < Target =Transaction > > {
42
- /// Access to transaction required for various introspection, moreover type
43
- /// `T: Deref<Target=Transaction>` allows to accept borrow and mutable borrow, the
44
- /// latter in particular is necessary for [`SighashCache::witness_mut`]
42
+ /// Access to transaction required for transaction introspection. Moreover, type
43
+ /// `T: Deref<Target=Transaction>` allows us to use borrowed and mutable borrowed types,
44
+ /// the latter in particular is necessary for [`SighashCache::witness_mut`].
45
45
tx : T ,
46
46
47
- /// Common cache for taproot and segwit inputs. It's an option because it's not needed for legacy inputs
47
+ /// Common cache for taproot and segwit inputs, `None` for legacy inputs.
48
48
common_cache : Option < CommonCache > ,
49
49
50
- /// Cache for segwit v0 inputs, it's the result of another round of sha256 on `common_cache`
50
+ /// Cache for segwit v0 inputs.
51
51
segwit_cache : Option < SegwitCache > ,
52
52
53
- /// Cache for taproot v1 inputs
53
+ /// Cache for taproot v1 inputs.
54
54
taproot_cache : Option < TaprootCache > ,
55
55
}
56
56
57
- /// Values cached common between segwit and taproot inputs
57
+ /// Common values cached between segwit and taproot inputs.
58
58
#[ derive( Debug ) ]
59
59
struct CommonCache {
60
60
prevouts : sha256:: Hash ,
61
61
sequences : sha256:: Hash ,
62
62
63
- /// in theory, `outputs` could be `Option` since `NONE ` and `SINGLE` doesn't need it, but since
64
- /// `ALL ` is the mostly used variant by large, we don't bother
63
+ /// In theory `outputs` could be an `Option` since `SIGHASH_NONE ` and `SIGHASH_SINGLE` do not
64
+ /// need it, but since `SIGHASH_ALL ` is by far the most used variant we don't bother.
65
65
outputs : sha256:: Hash ,
66
66
}
67
67
68
- /// Values cached for segwit inputs, it's equal to [`CommonCache`] plus another round of `sha256`
68
+ /// Values cached for segwit inputs, equivalent to [`CommonCache`] plus another round of `sha256`.
69
69
#[ derive( Debug ) ]
70
70
struct SegwitCache {
71
71
prevouts : sha256d:: Hash ,
72
72
sequences : sha256d:: Hash ,
73
73
outputs : sha256d:: Hash ,
74
74
}
75
75
76
- /// Values cached for taproot inputs
76
+ /// Values cached for taproot inputs.
77
77
#[ derive( Debug ) ]
78
78
struct TaprootCache {
79
79
amounts : sha256:: Hash ,
80
80
script_pubkeys : sha256:: Hash ,
81
81
}
82
82
83
- /// Contains outputs of previous transactions.
84
- /// In the case [`SchnorrSighashType`] variant is `ANYONECANPAY` , [`Prevouts::One`] may be provided
83
+ /// Contains outputs of previous transactions. In the case [`SchnorrSighashType`] variant is
84
+ /// `SIGHASH_ANYONECANPAY` , [`Prevouts::One`] may be used.
85
85
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
86
86
pub enum Prevouts < ' u , T > where T : ' u + Borrow < TxOut > {
87
- /// `One` variant allows to provide the single Prevout needed. It's useful for example
88
- /// when modifier `ANYONECANPAY ` is provided, only prevout of the current input is needed.
87
+ /// `One` variant allows provision of the single prevout needed. It's useful, for example, when
88
+ /// modifier `SIGHASH_ANYONECANPAY ` is provided, only prevout of the current input is needed.
89
89
/// The first `usize` argument is the input index this [`TxOut`] is referring to.
90
90
One ( usize , T ) ,
91
- /// When `ANYONECANPAY ` is not provided, or the caller is handy giving all prevouts so the same
92
- /// variable can be used for multiple inputs.
91
+ /// When `SIGHASH_ANYONECANPAY ` is not provided, or when the caller is giving all prevouts so
92
+ /// the same variable can be used for multiple inputs.
93
93
All ( & ' u [ T ] ) ,
94
94
}
95
95
96
96
const KEY_VERSION_0 : u8 = 0u8 ;
97
97
98
- /// Information related to the script path spending
98
+ /// Information related to the script path spending.
99
99
///
100
100
/// This can be hashed into a [`TapLeafHash`].
101
101
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
@@ -104,29 +104,29 @@ pub struct ScriptPath<'s> {
104
104
leaf_version : LeafVersion ,
105
105
}
106
106
107
- /// Hashtype of an input's signature, encoded in the last byte of the signature
108
- /// Fixed values so they can be casted as integer types for encoding
107
+ /// Hashtype of an input's signature, encoded in the last byte of the signature. Fixed values so
108
+ /// they can be cast as integer types for encoding.
109
109
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
110
110
pub enum SchnorrSighashType {
111
- /// 0x0: Used when not explicitly specified, defaulting to [`SchnorrSighashType::All`]
111
+ /// 0x0: Used when not explicitly specified, defaults to [`SchnorrSighashType::All`]
112
112
Default = 0x00 ,
113
- /// 0x1: Sign all outputs
113
+ /// 0x1: Sign all outputs.
114
114
All = 0x01 ,
115
- /// 0x2: Sign no outputs --- anyone can choose the destination
115
+ /// 0x2: Sign no outputs --- anyone can choose the destination.
116
116
None = 0x02 ,
117
117
/// 0x3: Sign the output whose index matches this input's index. If none exists,
118
118
/// sign the hash `0000000000000000000000000000000000000000000000000000000000000001`.
119
119
/// (This rule is probably an unintentional C++ism, but it's consensus so we have
120
120
/// to follow it.)
121
121
Single = 0x03 ,
122
- /// 0x81: Sign all outputs but only this input
122
+ /// 0x81: Sign all outputs but only this input.
123
123
AllPlusAnyoneCanPay = 0x81 ,
124
- /// 0x82: Sign no outputs and only this input
124
+ /// 0x82: Sign no outputs and only this input.
125
125
NonePlusAnyoneCanPay = 0x82 ,
126
- /// 0x83: Sign one output and only this input (see `Single` for what "one output" means)
126
+ /// 0x83: Sign one output and only this input (see `Single` for what "one output" means).
127
127
SinglePlusAnyoneCanPay = 0x83 ,
128
128
129
- /// Reserved for future use, `#[non_exhaustive]` is not available with current MSRV
129
+ /// Reserved for future use, `#[non_exhaustive]` is not available with MSRV 1.29.0
130
130
Reserved = 0xFF ,
131
131
}
132
132
serde_string_impl ! ( SchnorrSighashType , "a SchnorrSighashType data" ) ;
@@ -165,45 +165,46 @@ impl str::FromStr for SchnorrSighashType {
165
165
}
166
166
}
167
167
168
- /// Possible errors in computing the signature message
168
+ /// Possible errors in computing the signature message.
169
169
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
170
170
pub enum Error {
171
171
/// Could happen only by using `*_encode_signing_*` methods with custom writers, engines writers
172
- /// like the ones used in methods `*_signature_hash` don't error
172
+ /// like the ones used in methods `*_signature_hash` do not error.
173
173
Io ( io:: ErrorKind ) ,
174
174
175
- /// Requested index is greater or equal than the number of inputs in the transaction
175
+ /// Requested index is greater or equal than the number of inputs in the transaction.
176
176
IndexOutOfInputsBounds {
177
- /// Requested index
177
+ /// Requested index.
178
178
index : usize ,
179
- /// Number of transaction inputs
179
+ /// Number of transaction inputs.
180
180
inputs_size : usize ,
181
181
} ,
182
182
183
- /// Using SIGHASH_SINGLE without a "corresponding output" (an output with the same index as the
184
- /// input being verified) is a validation failure
183
+ /// Using ` SIGHASH_SINGLE` without a "corresponding output" (an output with the same index as
184
+ /// the input being verified) is a validation failure.
185
185
SingleWithoutCorrespondingOutput {
186
- /// Requested index
186
+ /// Requested index.
187
187
index : usize ,
188
- /// Number of transaction outputs
188
+ /// Number of transaction outputs.
189
189
outputs_size : usize ,
190
190
} ,
191
191
192
- /// There are mismatches in the number of prevouts provided compared with the number of
193
- /// inputs in the transaction
192
+ /// There are mismatches in the number of prevouts provided compared to the number of inputs in
193
+ /// the transaction.
194
194
PrevoutsSize ,
195
195
196
196
/// Requested a prevout index which is greater than the number of prevouts provided or a
197
- /// [`Prevouts::One`] with different index
197
+ /// [`Prevouts::One`] with different index.
198
198
PrevoutIndex ,
199
199
200
- /// A single prevout has been provided but all prevouts are needed without `ANYONECANPAY`
200
+ /// A single prevout has been provided but all prevouts are needed unless using
201
+ /// `SIGHASH_ANYONECANPAY`.
201
202
PrevoutKind ,
202
203
203
- /// Annex must be at least one byte long and the first bytes must be `0x50`
204
+ /// Annex must be at least one byte long and the first bytes must be `0x50`.
204
205
WrongAnnex ,
205
206
206
- /// Invalid Sighash type
207
+ /// Invalid Sighash type.
207
208
InvalidSighashType ( u32 ) ,
208
209
}
209
210
@@ -260,18 +261,18 @@ impl<'u, T> Prevouts<'u, T> where T: Borrow<TxOut> {
260
261
}
261
262
262
263
impl < ' s > ScriptPath < ' s > {
263
- /// Create a new ScriptPath structure
264
+ /// Creates a new ` ScriptPath` structure.
264
265
pub fn new ( script : & ' s Script , leaf_version : LeafVersion ) -> Self {
265
266
ScriptPath {
266
267
script,
267
268
leaf_version,
268
269
}
269
270
}
270
- /// Create a new ScriptPath structure using default leaf version value
271
+ /// Creates a new ` ScriptPath` structure using default leaf version value.
271
272
pub fn with_defaults ( script : & ' s Script ) -> Self {
272
273
Self :: new ( script, LeafVersion :: TapScript )
273
274
}
274
- /// Compute the leaf hash
275
+ /// Computes the leaf hash for this `ScriptPath`.
275
276
pub fn leaf_hash ( & self ) -> TapLeafHash {
276
277
let mut enc = TapLeafHash :: engine ( ) ;
277
278
@@ -302,7 +303,7 @@ impl From<EcdsaSighashType> for SchnorrSighashType {
302
303
}
303
304
304
305
impl SchnorrSighashType {
305
- /// Break the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean
306
+ /// Breaks the sighash flag into the "real" sighash flag and the `SIGHASH_ANYONECANPAY` boolean.
306
307
pub ( crate ) fn split_anyonecanpay_flag ( self ) -> ( SchnorrSighashType , bool ) {
307
308
match self {
308
309
SchnorrSighashType :: Default => ( SchnorrSighashType :: Default , false ) ,
@@ -316,7 +317,7 @@ impl SchnorrSighashType {
316
317
}
317
318
}
318
319
319
- /// Create a [`SchnorrSighashType`] from raw `u8`
320
+ /// Creates a [`SchnorrSighashType`] from raw `u8`.
320
321
pub fn from_u8 ( hash_ty : u8 ) -> Result < Self , Error > {
321
322
match hash_ty {
322
323
0x00 => Ok ( SchnorrSighashType :: Default ) ,
@@ -332,11 +333,12 @@ impl SchnorrSighashType {
332
333
}
333
334
}
334
335
335
- impl < R : Deref < Target =Transaction > > SighashCache < R > {
336
- /// Compute the sighash components from an unsigned transaction and auxiliary
337
- /// in a lazy manner when required.
338
- /// For the generated sighashes to be valid, no fields in the transaction may change except for
339
- /// script_sig and witnesses.
336
+ impl < R : Deref < Target = Transaction > > SighashCache < R > {
337
+ /// Constructs a new `SighashCache` from an unsigned transaction.
338
+ ///
339
+ /// The sighash components are computed in a lazy manner when required. For the generated
340
+ /// sighashes to be valid, no fields in the transaction may change except for script_sig and
341
+ /// witness.
340
342
pub fn new ( tx : R ) -> Self {
341
343
SighashCache {
342
344
tx,
@@ -346,8 +348,8 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
346
348
}
347
349
}
348
350
349
- /// Encode the BIP341 signing data for any flag type into a given object implementing a
350
- /// io::Write trait.
351
+ /// Encodes the BIP341 signing data for any flag type into a given object implementing a
352
+ /// ` io::Write` trait.
351
353
pub fn taproot_encode_signing_data_to < Write : io:: Write , T : Borrow < TxOut > > (
352
354
& mut self ,
353
355
mut writer : Write ,
@@ -477,7 +479,7 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
477
479
Ok ( ( ) )
478
480
}
479
481
480
- /// Compute the BIP341 sighash for any flag type.
482
+ /// Computes the BIP341 sighash for any flag type.
481
483
pub fn taproot_signature_hash < T : Borrow < TxOut > > (
482
484
& mut self ,
483
485
input_index : usize ,
@@ -498,7 +500,7 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
498
500
Ok ( TapSighashHash :: from_engine ( enc) )
499
501
}
500
502
501
- /// Compute the BIP341 sighash for a key spend
503
+ /// Computes the BIP341 sighash for a key spend.
502
504
pub fn taproot_key_spend_signature_hash < T : Borrow < TxOut > > (
503
505
& mut self ,
504
506
input_index : usize ,
@@ -517,7 +519,7 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
517
519
Ok ( TapSighashHash :: from_engine ( enc) )
518
520
}
519
521
520
- /// Compute the BIP341 sighash for a script spend
522
+ /// Computes the BIP341 sighash for a script spend.
521
523
///
522
524
/// Assumes the default `OP_CODESEPARATOR` position of `0xFFFFFFFF`. Custom values can be
523
525
/// provided through the more fine-grained API of [`SighashCache::taproot_encode_signing_data_to`].
@@ -540,7 +542,7 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
540
542
Ok ( TapSighashHash :: from_engine ( enc) )
541
543
}
542
544
543
- /// Encode the BIP143 signing data for any flag type into a given object implementing a
545
+ /// Encodes the BIP143 signing data for any flag type into a given object implementing a
544
546
/// [`std::io::Write`] trait.
545
547
pub fn segwit_encode_signing_data_to < Write : io:: Write > (
546
548
& mut self ,
@@ -605,7 +607,7 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
605
607
Ok ( ( ) )
606
608
}
607
609
608
- /// Compute the BIP143 sighash for any flag type.
610
+ /// Computes the BIP143 sighash for any flag type.
609
611
pub fn segwit_signature_hash (
610
612
& mut self ,
611
613
input_index : usize ,
@@ -624,8 +626,8 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
624
626
Ok ( Sighash :: from_engine ( enc) )
625
627
}
626
628
627
- /// Encode the legacy signing data for any flag type into a given object implementing a
628
- /// [`std::io::Write`] trait. Internally calls [`Transaction::encode_signing_data_to`]
629
+ /// Encodes the legacy signing data for any flag type into a given object implementing a
630
+ /// [`std::io::Write`] trait. Internally calls [`Transaction::encode_signing_data_to`].
629
631
pub fn legacy_encode_signing_data_to < Write : io:: Write , U : Into < u32 > > (
630
632
& self ,
631
633
mut writer : Write ,
@@ -645,7 +647,7 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
645
647
Ok ( ( ) )
646
648
}
647
649
648
- /// Computes the legacy sighash for any sighash type .
650
+ /// Computes the legacy sighash for any `sighash_type` .
649
651
pub fn legacy_signature_hash (
650
652
& self ,
651
653
input_index : usize ,
@@ -730,8 +732,8 @@ impl<R: Deref<Target=Transaction>> SighashCache<R> {
730
732
}
731
733
732
734
impl < R : DerefMut < Target =Transaction > > SighashCache < R > {
733
- /// When the SighashCache is initialized with a mutable reference to a transaction instead of a
734
- /// regular reference, this method is available to allow modification to the witnesses.
735
+ /// When the ` SighashCache` is initialized with a mutable reference to a transaction instead of
736
+ /// a regular reference, this method is available to allow modification to the witnesses.
735
737
///
736
738
/// This allows in-line signing such as
737
739
/// ```
@@ -761,12 +763,12 @@ impl From<io::Error> for Error {
761
763
}
762
764
}
763
765
764
- /// The `Annex` struct is a slice wrapper enforcing first byte to be `0x50`.
766
+ /// The `Annex` struct is a slice wrapper enforcing first byte is `0x50`.
765
767
#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
766
768
pub struct Annex < ' a > ( & ' a [ u8 ] ) ;
767
769
768
770
impl < ' a > Annex < ' a > {
769
- /// Creates a new `Annex` struct checking the first byte is `0x50`
771
+ /// Creates a new `Annex` struct checking the first byte is `0x50`.
770
772
pub fn new ( annex_bytes : & ' a [ u8 ] ) -> Result < Self , Error > {
771
773
if annex_bytes. first ( ) == Some ( & TAPROOT_ANNEX_PREFIX ) {
772
774
Ok ( Annex ( annex_bytes) )
@@ -775,7 +777,7 @@ impl<'a> Annex<'a> {
775
777
}
776
778
}
777
779
778
- /// Returns the Annex bytes data (including first byte `0x50`)
780
+ /// Returns the Annex bytes data (including first byte `0x50`).
779
781
pub fn as_bytes ( & self ) -> & [ u8 ] {
780
782
& * self . 0
781
783
}
0 commit comments