| 1 | package Bitcoin::Crypto::Role::PSBT::Signer; | ||||||||||||||||||||||||||||||||
| 2 | 11 | $Bitcoin::Crypto::Role::PSBT::Signer::VERSION = '4.006'; | |||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Mooish::Base -standard, -role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Types::Common -sigs; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use List::Util qw(any first); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Bitcoin::Crypto qw(btc_script_tree); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Bitcoin::Crypto::Constants qw(:sighash :script); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Bitcoin::Crypto::Transaction::Flags; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | sub _input_has_witness_utxo | ||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_input_has_witness_utxo: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 3 | my ($self, $input_index) = @_; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 3 | my $witness_utxo = $self->get_all_fields('PSBT_IN_WITNESS_UTXO', $input_index); | |||||||||||||||||||||||||||||||
| 19 | 3 | return !!$witness_utxo; | |||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | sub _update_modifiable_flags | ||||||||||||||||||||||||||||||||
| 23 | 18 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_update_modifiable_flags: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 24 | 18 | my ($self, $sighash) = @_; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 18 | ▶ | return unless $self->version == 2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | # NOTE: SIGHASH_DEFAULT may not be correct for pre-taproot, but it's only | ||||||||||||||||||||||||||||||||
| 29 | # used to set modifiable flags, which will be the same for SIGHASH_ALL | ||||||||||||||||||||||||||||||||
| 30 | 18 | ▶ | $sighash = $sighash ? $sighash->value : SIGHASH_ALL; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 18 | my $modifiable = $self->get_all_fields('PSBT_GLOBAL_TX_MODIFIABLE'); | |||||||||||||||||||||||||||||||
| 33 | 18 | ▶ | $modifiable //= $self | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$modifiable //=
MC/DC0%0/2
$modifiable //=
$modifiable
Truth table66%2/3
A //= B
A= $modifiableB=
| |||||||||||||||||||||||||||||||||
| 34 | ->add_field( | ||||||||||||||||||||||||||||||||
| 35 | type => 'PSBT_GLOBAL_TX_MODIFIABLE', | ||||||||||||||||||||||||||||||||
| 36 | value => { | ||||||||||||||||||||||||||||||||
| 37 | inputs_modifiable => !!1, | ||||||||||||||||||||||||||||||||
| 38 | outputs_modifiable => !!1, | ||||||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||||||
| 40 | ) | ||||||||||||||||||||||||||||||||
| 41 | ->get_field('PSBT_GLOBAL_TX_MODIFIABLE'); | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 18 | my $value = $modifiable->value; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 18 | ▶ | if (!($sighash & SIGHASH_ANYONECANPAY)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 16 | $value->{inputs_modifiable} = !!0; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 18 | my $sighash_type = $sighash & 31; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 18 | ▶ | if ($sighash_type != SIGHASH_NONE) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 15 | $value->{outputs_modifiable} = !!0; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 18 | ▶ | if ($sighash_type == SIGHASH_SINGLE) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 2 | $value->{has_sighash_single} = !!1; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | # update the value back in the PSBT | ||||||||||||||||||||||||||||||||
| 60 | 18 | $modifiable->set_value($value); | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | sub _add_partial_signature_legacy | ||||||||||||||||||||||||||||||||
| 64 | 12 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_add_partial_signature_legacy: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 65 | 12 | my ($self, $input_index, $key, $signature, $sighash) = @_; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 12 | $self->add_field( | |||||||||||||||||||||||||||||||
| 68 | type => 'PSBT_IN_PARTIAL_SIG', | ||||||||||||||||||||||||||||||||
| 69 | index => $input_index, | ||||||||||||||||||||||||||||||||
| 70 | key => $key->get_public_key, | ||||||||||||||||||||||||||||||||
| 71 | value => $signature, | ||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 12 | $self->_update_modifiable_flags($sighash); | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | sub _has_partial_signature_legacy | ||||||||||||||||||||||||||||||||
| 78 | 16 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_has_partial_signature_legacy: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 79 | 16 | my ($self, $input_index, $key) = @_; | |||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 16 | my $public_hash = $key->get_public_key->get_hash; | |||||||||||||||||||||||||||||||
| 82 | 16 | my @sigs = $self->get_all_fields('PSBT_IN_PARTIAL_SIG', $input_index); | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 16 | ▶ | return any { $_->key->get_hash eq $public_hash } @sigs; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | sub _add_partial_signature_taproot | ||||||||||||||||||||||||||||||||
| 88 | 6 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_add_partial_signature_taproot: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 89 | 6 | my ($self, $input_index, $key, $signature, $sighash, $leaf_hash) = @_; | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 6 | ▶ | if (defined $leaf_hash) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 3 | $self->add_field( | |||||||||||||||||||||||||||||||
| 93 | type => 'PSBT_IN_TAP_SCRIPT_SIG', | ||||||||||||||||||||||||||||||||
| 94 | index => $input_index, | ||||||||||||||||||||||||||||||||
| 95 | key => [$key->get_public_key, $leaf_hash], | ||||||||||||||||||||||||||||||||
| 96 | value => $signature, | ||||||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||||||
| 100 | 3 | $self->add_field( | |||||||||||||||||||||||||||||||
| 101 | type => 'PSBT_IN_TAP_KEY_SIG', | ||||||||||||||||||||||||||||||||
| 102 | index => $input_index, | ||||||||||||||||||||||||||||||||
| 103 | value => $signature, | ||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 6 | $self->_update_modifiable_flags($sighash); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | sub _has_partial_signature_taproot | ||||||||||||||||||||||||||||||||
| 111 | 8 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_has_partial_signature_taproot: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 112 | 8 | my ($self, $input_index, $key) = @_; | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 8 | my $key_sig = $self->get_all_fields('PSBT_IN_TAP_KEY_SIG', $input_index); | |||||||||||||||||||||||||||||||
| 115 | 8 | ▶ | return !!1 if $key_sig; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 7 | my $public = $key->get_taproot_output_key->get_public_key->get_xonly_key; | |||||||||||||||||||||||||||||||
| 118 | 7 | my @sigs = $self->get_all_fields('PSBT_IN_TAP_SCRIPT_SIG', $input_index); | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 7 | ▶ | return any { $_->key->[0]->get_xonly_key eq $public } @sigs; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | sub _do_sign_P2PKH | ||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign_P2PKH: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||||||
| 125 | 1 | my ($self, $key, $tx, $input, $input_index) = @_; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | return 0 if $self->_has_partial_signature_legacy($input_index, $key); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | return 0 if $self->_input_has_witness_utxo($input_index); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | return 0 unless $key->get_public_key->get_hash eq $input->utxo->output->locking_script->get_raw_address; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 1 | my $signer = $tx->sign(signing_index => $input_index, flags => Bitcoin::Crypto::Transaction::Flags->new_full); | |||||||||||||||||||||||||||||||
| 132 | 1 | my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index); | |||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | # use transaction signer's ability to give us the signature | ||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | my $signature = $signer->add_signature($key, sighash => $sighash ? $sighash->value : undef) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | ->signature->[-1]; | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 1 | $self->_add_partial_signature_legacy($input_index, $key, $signature, $sighash); | |||||||||||||||||||||||||||||||
| 139 | 1 | return 1; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | sub _do_sign_P2SH | ||||||||||||||||||||||||||||||||
| 143 | 5 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign_P2SH: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||||||
| 144 | 5 | my ($self, $key, $tx, $input, $input_index) = @_; | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 5 | ▶ | return 0 if $self->_has_partial_signature_legacy($input_index, $key); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | # is there a matching redeem script? | ||||||||||||||||||||||||||||||||
| 149 | 5 | my $redeem = $self->get_all_fields('PSBT_IN_REDEEM_SCRIPT', $input_index); | |||||||||||||||||||||||||||||||
| 150 | 5 | ▶ | return 0 unless $redeem; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 5 | my $script = $redeem->value; | |||||||||||||||||||||||||||||||
| 153 | 5 | ▶ | return 0 unless $script->get_hash eq $input->utxo->output->locking_script->get_raw_address; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 5 | my $type = $script->type; | |||||||||||||||||||||||||||||||
| 156 | 5 | ▶ | if ($type eq 'P2WPKH' || $type eq 'P2WSH') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$type eq "P2WPKH" or $type eq "P2WSH"
MC/DC100%2/2
$type eq "P2WPKH" or $type eq "P2WSH"
$type eq "P2WPKH"
$type eq "P2WSH"
Truth table100%3/3
A or B
A= $type eq "P2WPKH"B= $type eq "P2WSH"
| |||||||||||||||||||||||||||||||||
| 157 | 3 | my $method = "_do_sign_$type"; | |||||||||||||||||||||||||||||||
| 158 | 3 | return $self->$method($key, $tx, $input, $input_index, $script); | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | elsif ($type eq 'P2MS') { | ||||||||||||||||||||||||||||||||
| 161 | 2 | ▶ | return 0 if $self->_input_has_witness_utxo($input_index); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 2 | my $pubkeys = $script->get_raw_address; | |||||||||||||||||||||||||||||||
| 164 | 2 | my $this_pubkey = $key->get_public_key->to_serialized; | |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 2 | ▶ | my $index = first { $_ eq $this_pubkey } @{$pubkeys}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 167 | 2 | ▶ | return 0 unless defined $index; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 2 | my $signer = $tx->sign( | |||||||||||||||||||||||||||||||
| 170 | signing_index => $input_index, | ||||||||||||||||||||||||||||||||
| 171 | script => $script, | ||||||||||||||||||||||||||||||||
| 172 | flags => Bitcoin::Crypto::Transaction::Flags->new_full, | ||||||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||||||
| 174 | 2 | my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index); | |||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | # use transaction signer's ability to give us the signature | ||||||||||||||||||||||||||||||||
| 177 | 2 | ▶ | my $signature = $signer->add_signature($key, sighash => $sighash ? $sighash->value : undef) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | ->signature->[-1]; | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 2 | $self->_add_partial_signature_legacy($input_index, $key, $signature, $sighash); | |||||||||||||||||||||||||||||||
| 181 | 2 | return 1; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | sub _do_sign_P2WPKH | ||||||||||||||||||||||||||||||||
| 188 | 6 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign_P2WPKH: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 189 | 6 | my ($self, $key, $tx, $input, $input_index, $compat_script) = @_; | |||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | 6 | ▶ | return 0 if $self->_has_partial_signature_legacy($input_index, $key); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 5 | ▶ | my $to_compare = $compat_script | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 194 | ? $compat_script->get_raw_address | ||||||||||||||||||||||||||||||||
| 195 | : $input->utxo->output->locking_script->get_raw_address | ||||||||||||||||||||||||||||||||
| 196 | ; | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 5 | ▶ | return 0 unless $key->get_public_key->get_hash eq $to_compare; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 5 | ▶ | my $signer = $tx->sign( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | signing_index => $input_index, | ||||||||||||||||||||||||||||||||
| 202 | flags => Bitcoin::Crypto::Transaction::Flags->new_full, | ||||||||||||||||||||||||||||||||
| 203 | ($compat_script ? (compat => !!1) : ()), | ||||||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||||||
| 205 | 5 | my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index); | |||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | # use transaction signer's ability to give us the signature | ||||||||||||||||||||||||||||||||
| 208 | 5 | ▶ | my $signature = $signer->add_signature($key, sighash => $sighash ? $sighash->value : undef) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | ->signature->[-1]; | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 5 | $self->_add_partial_signature_legacy($input_index, $key, $signature, $sighash); | |||||||||||||||||||||||||||||||
| 212 | 5 | return 1; | |||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | sub _do_sign_P2WSH | ||||||||||||||||||||||||||||||||
| 216 | 4 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign_P2WSH: called CC=9 SCAR=22.8
| |||||||||||||||||||||||||||||||||
| 217 | 4 | my ($self, $key, $tx, $input, $input_index, $compat_script) = @_; | |||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 4 | ▶ | return 0 if $self->_has_partial_signature_legacy($input_index, $key); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | # is there a matching witness redeem script? | ||||||||||||||||||||||||||||||||
| 222 | 4 | my $redeem = $self->get_all_fields('PSBT_IN_WITNESS_SCRIPT', $input_index); | |||||||||||||||||||||||||||||||
| 223 | 4 | ▶ | return 0 unless $redeem; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | 4 | my $script = $redeem->value; | |||||||||||||||||||||||||||||||
| 226 | 4 | ▶ | my $compare_script = $compat_script // $input->utxo->output->locking_script; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$compat_script // $input->utxo->output->locking_script
MC/DC0%0/2
$compat_script // $input->utxo->output->locking_script
$compat_script
$input->utxo->output->locking_script
Truth table66%2/3
A // B
A= $compat_scriptB= $input->utxo->output->locking_script
| |||||||||||||||||||||||||||||||||
| 227 | 4 | ▶ | return 0 unless $script->get_witness_hash eq $compare_script->get_raw_address; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 4 | my $type = $script->type; | |||||||||||||||||||||||||||||||
| 230 | 4 | ▶ | if ($type eq 'P2MS') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | 4 | my $pubkeys = $script->get_raw_address; | |||||||||||||||||||||||||||||||
| 232 | 4 | my $this_pubkey = $key->get_public_key->to_serialized; | |||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | 4 | ▶ | my $index = first { $_ eq $this_pubkey } @{$pubkeys}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 235 | 4 | ▶ | return 0 unless defined $index; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | 4 | ▶ | my $signer = $tx->sign( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | signing_index => $input_index, | ||||||||||||||||||||||||||||||||
| 239 | script => $script, | ||||||||||||||||||||||||||||||||
| 240 | flags => Bitcoin::Crypto::Transaction::Flags->new_full, | ||||||||||||||||||||||||||||||||
| 241 | ($compat_script ? (compat => !!1) : ()), | ||||||||||||||||||||||||||||||||
| 242 | ); | ||||||||||||||||||||||||||||||||
| 243 | 4 | my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index); | |||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | # use transaction signer's ability to give us the signature | ||||||||||||||||||||||||||||||||
| 246 | 4 | ▶ | my $signature = $signer->add_signature($key, sighash => $sighash ? $sighash->value : undef) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 247 | ->signature->[-1]; | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 4 | $self->_add_partial_signature_legacy($input_index, $key, $signature, $sighash); | |||||||||||||||||||||||||||||||
| 250 | 4 | return 1; | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | sub _do_sign_P2TR_keypath | ||||||||||||||||||||||||||||||||
| 257 | 3 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign_P2TR_keypath: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||
| 258 | 3 | my ($self, $key, $tx, $input, $input_index) = @_; | |||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 3 | my $tree_root = $self->get_all_fields('PSBT_IN_TAP_MERKLE_ROOT', $input_index); | |||||||||||||||||||||||||||||||
| 261 | 3 | ▶ | my $tree = $tree_root ? btc_script_tree->new(tree => [{hash => $tree_root->value}]) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 3 | ▶ | my $output_key = $key->get_public_key->get_taproot_output_key($tree ? $tree->get_merkle_root : ()); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | 3 | ▶ | return 0 unless $output_key->get_xonly_key eq $input->utxo->output->locking_script->get_raw_address; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | 3 | ▶ | my $signer = $tx->sign( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | signing_index => $input_index, | ||||||||||||||||||||||||||||||||
| 268 | ($tree ? (script_tree => $tree) : ()), | ||||||||||||||||||||||||||||||||
| 269 | ); | ||||||||||||||||||||||||||||||||
| 270 | 3 | my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index); | |||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | # use transaction signer's ability to give us the signature | ||||||||||||||||||||||||||||||||
| 273 | 3 | ▶ | my $signature = $signer->add_signature($key, sighash => $sighash ? $sighash->value : undef) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | ->signature->[-1]; | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | 3 | $self->_add_partial_signature_taproot($input_index, $key, $signature, $sighash); | |||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 3 | return 1; | |||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | sub _do_sign_P2TR_scriptpath | ||||||||||||||||||||||||||||||||
| 282 | 6 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign_P2TR_scriptpath: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||
| 283 | 6 | my ($self, $key, $tx, $input, $input_index) = @_; | |||||||||||||||||||||||||||||||
| 284 | 6 | my $signer_key = $key->get_taproot_output_key; | |||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 6 | my @leaves = $self->get_all_fields('PSBT_IN_TAP_LEAF_SCRIPT', $input_index); | |||||||||||||||||||||||||||||||
| 287 | 6 | ▶ | return 0 unless @leaves; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | 3 | my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index); | |||||||||||||||||||||||||||||||
| 290 | 3 | my $signed = 0; | |||||||||||||||||||||||||||||||
| 291 | 3 | foreach my $leaf (@leaves) { | |||||||||||||||||||||||||||||||
| 292 | 3 | my $leaf_obj = $leaf->value; | |||||||||||||||||||||||||||||||
| 293 | 3 | my $script = $leaf_obj->script; | |||||||||||||||||||||||||||||||
| 294 | 3 | ▶ | next if $script->type ne 'TAPSCRIPT_MULTISIG'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 295 | 3 | ▶ | next if $leaf_obj->leaf_version != TAPSCRIPT_LEAF_VERSION; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 3 | my $pubkeys = $script->get_raw_address; | |||||||||||||||||||||||||||||||
| 298 | 3 | my $this_pubkey = $signer_key->get_public_key->get_xonly_key; | |||||||||||||||||||||||||||||||
| 299 | 3 | ▶ | my $index = first { $pubkeys->[$_] eq $this_pubkey } keys @{$pubkeys}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 300 | 3 | ▶ | next unless defined $index; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | 3 | my $control_block = $leaf->key; | |||||||||||||||||||||||||||||||
| 303 | 3 | my $tree = btc_script_tree->from_path($leaf_obj, $control_block->script_blocks); | |||||||||||||||||||||||||||||||
| 304 | 3 | my $signer = $tx->sign( | |||||||||||||||||||||||||||||||
| 305 | signing_index => $input_index, | ||||||||||||||||||||||||||||||||
| 306 | script_tree => $tree, | ||||||||||||||||||||||||||||||||
| 307 | leaf_id => $leaf_obj->id, | ||||||||||||||||||||||||||||||||
| 308 | public_key => $control_block->public_key, | ||||||||||||||||||||||||||||||||
| 309 | ); | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 3 | ▶ | while ($index-- > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 312 | 1 | $signer->add_signature(''); | |||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | 3 | ▶ | my $signature = $signer->add_signature($signer_key, sighash => $sighash ? $sighash->value : undef) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 316 | ->signature->[-1]; | ||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | 3 | $self->_add_partial_signature_taproot($input_index, $signer_key, $signature, $sighash, $leaf_obj->hash); | |||||||||||||||||||||||||||||||
| 319 | 3 | $signed = 1; | |||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | # NOTE: we count inputs signed, so it's either 1 or 0 | ||||||||||||||||||||||||||||||||
| 323 | 3 | return $signed; | |||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | sub _do_sign_P2TR | ||||||||||||||||||||||||||||||||
| 327 | 8 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign_P2TR: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 328 | 8 | my ($self, $key, $tx, $input, $input_index) = @_; | |||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | 8 | ▶ | return 0 if $self->_has_partial_signature_taproot($input_index, $key); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | 6 | ▶ | return $self->_do_sign_P2TR_scriptpath($key, $tx, $input, $input_index) | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->_do_sign_P2TR_scriptpath($key, $tx, $input, $input_index) || $self->_do_sign_P2TR_keypath($key, $tx, $input, $input_index)
MC/DC0%0/2
$self->_do_sign_P2TR_scriptpath($key, $tx, $input, $input_index) || $self->_do_sign_P2TR_keypath($key, $tx, $input, $input_index)
$self->_do_sign_P2TR_scriptpath($key, $tx, $input, $input_index)
$self->_do_sign_P2TR_keypath($key, $tx, $input, $input_index)
Truth table66%2/3
A || B
A= $self->_do_sign_P2TR_scriptpath($key, $tx, $input, $input_index)B= $self->_do_sign_P2TR_keypath($key, $tx, $input, $input_index)
| |||||||||||||||||||||||||||||||||
| 333 | || $self->_do_sign_P2TR_keypath($key, $tx, $input, $input_index); | ||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||
| 336 | sub _do_sign | ||||||||||||||||||||||||||||||||
| 337 | 22 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
_do_sign: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||
| 338 | 22 | my ($self, $key, $tx, $input, $input_index) = @_; | |||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||
| 340 | # is this input finalized already? | ||||||||||||||||||||||||||||||||
| 341 | 22 | my $signature_field = $self->get_all_fields('PSBT_IN_FINAL_SCRIPTSIG', $input_index); | |||||||||||||||||||||||||||||||
| 342 | 22 | my $witness_field = $self->get_all_fields('PSBT_IN_FINAL_SCRIPTWITNESS', $input_index); | |||||||||||||||||||||||||||||||
| 343 | 22 | ▶ | return 0 if $signature_field || $witness_field; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$signature_field or $witness_field
MC/DC50%1/2
$signature_field or $witness_field
$signature_field
$witness_field
Truth table66%2/3
A or B
A= $signature_fieldB= $witness_field
| |||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | # custom script - unsupported | ||||||||||||||||||||||||||||||||
| 346 | 21 | my $type = $input->utxo->output->locking_script->type; | |||||||||||||||||||||||||||||||
| 347 | 21 | ▶ | return 0 unless defined $type; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | # some types are defined but unsupported (P2PK, nulldata) | ||||||||||||||||||||||||||||||||
| 350 | 21 | my $method = $self->can("_do_sign_$type"); | |||||||||||||||||||||||||||||||
| 351 | 21 | ▶ | return 0 unless $method; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | 21 | return $self->$method($key, $tx, $input, $input_index); | |||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | 11 | signature_for sign => ( | |||||||||||||||||||||||||||||||
| 357 | method => !!1, | ||||||||||||||||||||||||||||||||
| 358 | positional => [ | ||||||||||||||||||||||||||||||||
| 359 | InstanceOf ['Bitcoin::Crypto::Key::Private'] | ||||||||||||||||||||||||||||||||
| 360 | ], | ||||||||||||||||||||||||||||||||
| 361 | ); | ||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | sub sign | ||||||||||||||||||||||||||||||||
| 364 | 21 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
sign: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 365 | 21 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||
| 366 | 21 | my $tx = $self->get_transaction; | |||||||||||||||||||||||||||||||
| 367 | 21 | my $inputs = $tx->inputs; | |||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | 21 | my $signed = 0; | |||||||||||||||||||||||||||||||
| 370 | 21 | foreach my $input_index (0 .. $#{$inputs}) { | |||||||||||||||||||||||||||||||
| 371 | 22 | $signed += $self->_do_sign($key, $tx, $inputs->[$input_index], $input_index); | |||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | 21 | return $signed; | |||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | 11 | 1; | |||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||