@@ -34,21 +34,41 @@ use Miniscript;
3434use ScriptContext ;
3535use Terminal ;
3636
37- /// Type alias for a signature/hashtype pair
38- pub type BitcoinECSig = ( secp256k1:: Signature , bitcoin:: SigHashType ) ;
37+ /// Type for a signature/hashtype pair
38+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
39+ pub struct BitcoinECSig {
40+ /// The underlying signature
41+ pub sig : secp256k1:: Signature ,
42+ /// The associated hash type
43+ pub hash_ty : bitcoin:: SigHashType ,
44+ }
3945/// Type alias for 32 byte Preimage.
4046pub type Preimage32 = [ u8 ; 32 ] ;
4147
42- /// Helper function to create BitcoinECSig from Rawsig
43- /// Useful for downstream when implementing Satisfier.
44- /// Returns underlying secp if the Signature is not of correct format
45- pub fn bitcoin_ecsig_from_rawsig ( rawsig : & [ u8 ] ) -> Result < BitcoinECSig , :: interpreter:: Error > {
46- let ( flag, sig) = rawsig. split_last ( ) . unwrap ( ) ;
47- let flag = bitcoin:: SigHashType :: from_u32_standard ( * flag as u32 )
48- . map_err ( |_| :: interpreter:: Error :: NonStandardSigHash ( [ sig, & [ * flag] ] . concat ( ) . to_vec ( ) ) ) ?;
49- let sig = secp256k1:: Signature :: from_der ( sig) ?;
50- Ok ( ( sig, flag) )
48+ impl BitcoinECSig {
49+ /// Helper function to create BitcoinECSig from Rawsig
50+ /// Useful for downstream when implementing Satisfier.
51+ /// Returns underlying secp if the Signature is not of correct format
52+ pub fn from_rawsig ( rawsig : & [ u8 ] ) -> Result < BitcoinECSig , :: interpreter:: Error > {
53+ let ( flag, sig) = rawsig. split_last ( ) . unwrap ( ) ;
54+ let flag = bitcoin:: SigHashType :: from_u32_standard ( * flag as u32 ) . map_err ( |_| {
55+ :: interpreter:: Error :: NonStandardSigHash ( [ sig, & [ * flag] ] . concat ( ) . to_vec ( ) )
56+ } ) ?;
57+ let sig = secp256k1:: Signature :: from_der ( sig) ?;
58+ Ok ( BitcoinECSig {
59+ sig : sig,
60+ hash_ty : flag,
61+ } )
62+ }
63+
64+ /// Serialize BitcoinECSig
65+ pub fn serialize ( & self ) -> Vec < u8 > {
66+ let mut ret = self . sig . serialize_der ( ) . to_vec ( ) ;
67+ ret. push ( self . hash_ty . as_u32 ( ) as u8 ) ;
68+ ret
69+ }
5170}
71+
5272/// Trait describing a lookup table for signatures, hash preimages, etc.
5373/// Every method has a default implementation that simply returns `None`
5474/// on every query. Users are expected to override the methods that they
@@ -403,11 +423,7 @@ impl Witness {
403423 /// Turn a signature into (part of) a satisfaction
404424 fn signature < Pk : ToPublicKey , S : Satisfier < Pk > > ( sat : S , pk : & Pk ) -> Self {
405425 match sat. lookup_ec_sig ( pk) {
406- Some ( ( sig, hashtype) ) => {
407- let mut ret = sig. serialize_der ( ) . to_vec ( ) ;
408- ret. push ( hashtype. as_u32 ( ) as u8 ) ;
409- Witness :: Stack ( vec ! [ ret] )
410- }
426+ Some ( sig) => Witness :: Stack ( vec ! [ sig. serialize( ) ] ) ,
411427 // Signatures cannot be forged
412428 None => Witness :: Impossible ,
413429 }
@@ -426,10 +442,8 @@ impl Witness {
426442 /// Turn a key/signature pair related to a pkh into (part of) a satisfaction
427443 fn pkh_signature < Pk : ToPublicKey , S : Satisfier < Pk > > ( sat : S , pkh : & Pk :: Hash ) -> Self {
428444 match sat. lookup_pkh_ec_sig ( pkh) {
429- Some ( ( pk, ( sig, hashtype) ) ) => {
430- let mut ret = sig. serialize_der ( ) . to_vec ( ) ;
431- ret. push ( hashtype. as_u32 ( ) as u8 ) ;
432- Witness :: Stack ( vec ! [ ret. to_vec( ) , pk. to_public_key( ) . to_bytes( ) ] )
445+ Some ( ( pk, btc_sig) ) => {
446+ Witness :: Stack ( vec ! [ btc_sig. serialize( ) , pk. to_public_key( ) . to_bytes( ) ] )
433447 }
434448 None => Witness :: Impossible ,
435449 }
0 commit comments