@@ -618,14 +618,18 @@ impl Actor {
618618 . map ( |s| s. sighash_type ( ) ) ?
619619 . unwrap_or ( TapSighashType :: Default ) ;
620620
621+ // Common calculations across all spend paths
622+ let signature_id = spt. get_signature_id ( ) ;
623+ let sig = Self :: get_saved_signature ( signature_id, signatures) ;
624+ let sighash = calc_sighash ( sighash_type) ?;
625+
621626 match spt. get_spend_path ( ) {
622627 SpendPath :: ScriptSpend ( script_idx) => {
623628 let script = spt
624629 . get_spendable ( )
625630 . get_scripts ( )
626631 . get ( script_idx)
627632 . ok_or ( TxError :: NoScriptAtIndex ( script_idx) ) ?;
628- let sig = Self :: get_saved_signature ( spt. get_signature_id ( ) , signatures) ;
629633
630634 let sig = sig. map ( |sig| taproot:: Signature {
631635 signature : sig,
@@ -638,10 +642,19 @@ impl Actor {
638642 let mut witness: Witness = match script. kind ( ) {
639643 Kind :: BaseDepositScript ( script) => {
640644 match ( sig, script. 0 == self . xonly_public_key ) {
641- ( Some ( sig) , _) => script. generate_script_inputs ( & sig) ,
645+ ( Some ( sig) , _) => {
646+ Self :: verify_signature (
647+ & sig. signature ,
648+ sighash,
649+ script. 0 ,
650+ TapTweakData :: ScriptPath ,
651+ & signature_id,
652+ ) ?;
653+ script. generate_script_inputs ( & sig)
654+ }
642655 ( None , true ) => {
643656 script. generate_script_inputs ( & taproot:: Signature {
644- signature : self . sign ( calc_sighash ( sighash_type ) ? ) ,
657+ signature : self . sign ( sighash ) ,
645658 sighash_type,
646659 } )
647660 }
@@ -652,10 +665,19 @@ impl Actor {
652665 }
653666 Kind :: ReplacementDepositScript ( script) => {
654667 match ( sig, script. 0 == self . xonly_public_key ) {
655- ( Some ( sig) , _) => script. generate_script_inputs ( & sig) ,
668+ ( Some ( sig) , _) => {
669+ Self :: verify_signature (
670+ & sig. signature ,
671+ sighash,
672+ script. 0 ,
673+ TapTweakData :: ScriptPath ,
674+ & signature_id,
675+ ) ?;
676+ script. generate_script_inputs ( & sig)
677+ }
656678 ( None , true ) => {
657679 script. generate_script_inputs ( & taproot:: Signature {
658- signature : self . sign ( calc_sighash ( sighash_type ) ? ) ,
680+ signature : self . sign ( sighash ) ,
659681 sighash_type,
660682 } )
661683 }
@@ -665,10 +687,19 @@ impl Actor {
665687 }
666688 }
667689 Kind :: TimelockScript ( script) => match ( sig, script. 0 ) {
668- ( Some ( sig) , Some ( _) ) => script. generate_script_inputs ( Some ( & sig) ) ,
690+ ( Some ( sig) , Some ( xonly_pk) ) => {
691+ Self :: verify_signature (
692+ & sig. signature ,
693+ sighash,
694+ xonly_pk,
695+ TapTweakData :: ScriptPath ,
696+ & signature_id,
697+ ) ?;
698+ script. generate_script_inputs ( Some ( & sig) )
699+ }
669700 ( None , Some ( xonly_key) ) if xonly_key == self . xonly_public_key => script
670701 . generate_script_inputs ( Some ( & taproot:: Signature {
671- signature : self . sign ( calc_sighash ( sighash_type ) ? ) ,
702+ signature : self . sign ( sighash ) ,
672703 sighash_type,
673704 } ) ) ,
674705 ( None , Some ( _) ) => {
@@ -677,10 +708,19 @@ impl Actor {
677708 ( _, None ) => Witness :: new ( ) ,
678709 } ,
679710 Kind :: CheckSig ( script) => match ( sig, script. 0 == self . xonly_public_key ) {
680- ( Some ( sig) , _) => script. generate_script_inputs ( & sig) ,
711+ ( Some ( sig) , _) => {
712+ Self :: verify_signature (
713+ & sig. signature ,
714+ sighash,
715+ script. 0 ,
716+ TapTweakData :: ScriptPath ,
717+ & signature_id,
718+ ) ?;
719+ script. generate_script_inputs ( & sig)
720+ }
681721
682722 ( None , true ) => script. generate_script_inputs ( & taproot:: Signature {
683- signature : self . sign ( calc_sighash ( sighash_type ) ? ) ,
723+ signature : self . sign ( sighash ) ,
684724 sighash_type,
685725 } ) ,
686726 ( None , false ) => return Err ( TxError :: SignatureNotFound ( tx_type) . into ( ) ) ,
@@ -701,14 +741,20 @@ impl Actor {
701741 }
702742 SpendPath :: KeySpend => {
703743 let xonly_public_key = spendinfo. internal_key ( ) ;
704-
705- let sighash = calc_sighash ( sighash_type) ?;
706- let sig = Self :: get_saved_signature ( spt. get_signature_id ( ) , signatures) ;
707744 let sig = match sig {
708- Some ( sig) => taproot:: Signature {
709- signature : sig,
710- sighash_type,
711- } ,
745+ Some ( sig) => {
746+ Self :: verify_signature (
747+ & sig,
748+ sighash,
749+ xonly_public_key,
750+ TapTweakData :: KeyPath ( spendinfo. merkle_root ( ) ) ,
751+ & signature_id,
752+ ) ?;
753+ taproot:: Signature {
754+ signature : sig,
755+ sighash_type,
756+ }
757+ }
712758 None => {
713759 if xonly_public_key == self . xonly_public_key {
714760 taproot:: Signature {
@@ -733,6 +779,28 @@ impl Actor {
733779 txhandler. sign_txins ( signer) ?;
734780 Ok ( ( ) )
735781 }
782+
783+ /// Verifies a schnorr signature with the given parameters and wraps errors with signature ID context.
784+ fn verify_signature (
785+ sig : & schnorr:: Signature ,
786+ sighash : TapSighash ,
787+ xonly_public_key : XOnlyPublicKey ,
788+ tweak_data : TapTweakData ,
789+ signature_id : & SignatureId ,
790+ ) -> Result < ( ) , BridgeError > {
791+ verify_schnorr (
792+ sig,
793+ & Message :: from ( sighash) ,
794+ xonly_public_key,
795+ tweak_data,
796+ None ,
797+ )
798+ . wrap_err ( format ! (
799+ "Failed to verify signature from DB for signature {:?} for signer xonly pk {}" ,
800+ signature_id, xonly_public_key
801+ ) )
802+ . map_err ( Into :: into)
803+ }
736804}
737805
738806#[ cfg( test) ]
0 commit comments