10
10
from apps .common .writers import write_compact_size
11
11
12
12
from .. import addresses , common , multisig , scripts , writers
13
- from ..common import SigHashType , ecdsa_sign , input_is_external
13
+ from ..common import SigHashType , ecdsa_sign , input_is_external , p2tr_multisig_leaf_hash
14
14
from ..ownership import verify_nonownership
15
15
from ..verification import SignatureVerifier
16
16
from . import helpers
@@ -642,14 +642,22 @@ def sign_bip143_input(self, i: int, txi: TxInput) -> tuple[bytes, bytes]:
642
642
def sign_taproot_input (self , i : int , txi : TxInput ) -> bytes :
643
643
from ..common import bip340_sign
644
644
645
+ if txi .multisig :
646
+ public_keys = multisig .multisig_get_pubkeys (txi .multisig )
647
+ threshold = txi .multisig .m
648
+ leaf_hash = p2tr_multisig_leaf_hash (public_keys , threshold )
649
+ else :
650
+ leaf_hash = None
651
+
645
652
sigmsg_digest = self .tx_info .sig_hasher .hash341 (
646
- i ,
647
- self .tx_info .tx ,
648
- self .get_sighash_type (txi ),
653
+ i , self .tx_info .tx , self .get_sighash_type (txi ), leaf_hash
649
654
)
650
655
651
656
node = self .keychain .derive (txi .address_n )
652
- return bip340_sign (node , sigmsg_digest )
657
+ public_key = node .public_key ()
658
+ signature = bip340_sign (node , sigmsg_digest , not txi .multisig )
659
+
660
+ return public_key , signature
653
661
654
662
async def sign_segwit_input (self , i : int ) -> None :
655
663
# STAGE_REQUEST_SEGWIT_WITNESS in legacy
@@ -660,11 +668,24 @@ async def sign_segwit_input(self, i: int) -> None:
660
668
raise ProcessError ("Transaction has changed during signing" )
661
669
662
670
if txi .script_type == InputScriptType .SPENDTAPROOT :
663
- signature = self .sign_taproot_input (i , txi )
671
+ public_key , signature = self .sign_taproot_input (i , txi )
664
672
if self .serialize :
665
- scripts .write_witness_p2tr (
666
- self .serialized_tx , signature , self .get_sighash_type (txi )
667
- )
673
+ if txi .multisig :
674
+ # find out place of our signature based on the pubkey
675
+ signature_index = multisig .multisig_pubkey_index (
676
+ txi .multisig , public_key
677
+ )
678
+ scripts .write_witness_multisig_taproot (
679
+ self .serialized_tx ,
680
+ txi .multisig ,
681
+ signature ,
682
+ signature_index ,
683
+ self .get_sighash_type (txi ),
684
+ )
685
+ else :
686
+ scripts .write_witness_p2tr (
687
+ self .serialized_tx , signature , self .get_sighash_type (txi )
688
+ )
668
689
else :
669
690
public_key , signature = self .sign_bip143_input (i , txi )
670
691
if self .serialize :
0 commit comments