@@ -6,6 +6,8 @@ use warnings;
66use Mooish::Base -standard, -role;
77use Types::Common -sigs;
88
9+ use Bitcoin::Crypto qw( btc_script_tree) ;
10+
911sub _add_partial_signature
1012{
1113 my ($self , $input_index , $key , $signature ) = @_ ;
@@ -57,12 +59,56 @@ sub _do_sign_P2WSH
5759 return 0;
5860}
5961
62+ sub _do_sign_P2TR_keypath
63+ {
64+ my ($self , $key , $tx , $input , $input_index ) = @_ ;
65+
66+ my $tree_root = $self -> get_all_fields(' PSBT_IN_TAP_MERKLE_ROOT' , $input_index );
67+ my $tree = $tree_root ? btc_script_tree-> new(tree => [{hash => $tree_root -> value}]) : undef ;
68+
69+ my $output_key = $key -> get_public_key-> get_taproot_output_key($tree ? $tree -> get_merkle_root : ());
70+ return 0 unless $output_key -> get_xonly_key eq $input -> utxo-> output-> locking_script-> get_raw_address;
71+
72+ my $signer = $tx -> sign(
73+ signing_index => $input_index ,
74+ ($tree ? (script_tree => $tree ) : ()),
75+ );
76+ my $sighash = $self -> get_all_fields(' PSBT_IN_SIGHASH_TYPE' , $input_index );
77+
78+ # use transaction signer's ability to give us the signature
79+ my $signature = $signer -> add_signature($key , sighash => $sighash ? $sighash -> value : undef )
80+ -> signature-> [-1];
81+
82+ $self -> add_field(
83+ type => ' PSBT_IN_TAP_KEY_SIG' ,
84+ index => $input_index ,
85+ value => $signature ,
86+ );
87+
88+ return 1;
89+ }
90+
91+ sub _do_sign_P2TR_scriptpath
92+ {
93+ my ($self , $key , $tx , $input , $input_index ) = @_ ;
94+
95+ my $script = $self -> get_all_fields(' PSBT_IN_TAP_LEAF_SCRIPT' , $input_index );
96+ return 0 unless $script ;
97+
98+ # TODO: detect simple P2MS scripts with checksigadd
99+
100+ return 1;
101+ }
102+
60103sub _do_sign_P2TR
61104{
62105 my ($self , $key , $tx , $input , $input_index ) = @_ ;
63106
64- # TODO
65- return 0;
107+ my $res = $self -> _do_sign_P2TR_scriptpath($key , $tx , $input , $input_index );
108+ $res = $self -> _do_sign_P2TR_keypath($key , $tx , $input , $input_index )
109+ unless $res ;
110+
111+ return $res ;
66112}
67113
68114sub _do_sign
0 commit comments