Crypto/Role/PSBT/Finalizer.pm

filter: Statementstmt 97.0% 166 / 171 Branchbran 75.5% 65 / 86 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 2 totaltotal 87.2% 261 / 299 CC 63 SCAR 20.8
CC
63
Cov
88%
finalize
14.0
_should_finalize_P2SH
11.2
_should_finalize_P2TR_scriptpath
10.9
CRAP 8.0
« Crypto/Key/ExtBase.pm ↑ Summary Crypto/Util/Internal.pm »
1 package Bitcoin::Crypto::Role::PSBT::Finalizer;
2 12 $Bitcoin::Crypto::Role::PSBT::Finalizer::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 List::Util qw(any all first);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Bitcoin::Crypto qw(btc_script);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Bitcoin::Crypto::Constants qw(:psbt);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 requires qw(
12 version
13 get_all_fields
14 get_transaction
15 );
16  
17 sub _should_finalize_P2PKH
18 1 ▶{
Subroutine _should_finalize_P2PKH: called CC=4 SCAR=14.6
19 1 my ($self, $input, $input_index) = @_;
20  
21 # are there any partial signatures?
22 1 my @partials = $self->get_all_fields('PSBT_IN_PARTIAL_SIG', $input_index);
23 1 ▶ return () unless @partials;
Branch: 1 branch
BranchTrueFalseTotal
unless @partials 0 1 1
24  
25 1 my $pkh = $input->utxo->output->locking_script->get_raw_address;
26 1 foreach my $sig (@partials) {
27 1 ▶ return ({type => 'signature', sigs => [$sig->value], more => [$sig->key->to_serialized]})
Branch: 1 branch
BranchTrueFalseTotal
if $sig->key->get_hash eq $pkh 1 0 1
28 if $sig->key->get_hash eq $pkh;
29 }
30  
31 0 return ();
32 }
33  
34 sub _should_finalize_P2SH
35 3 ▶{
Subroutine _should_finalize_P2SH: called CC=11 SCAR=24.2
36 3 my ($self, $input, $input_index) = @_;
37  
38 # is there a redeem script?
39 3 my $redeem = $self->get_all_fields('PSBT_IN_REDEEM_SCRIPT', $input_index);
40 3 ▶ return () unless $redeem;
Branch: 1 branch
BranchTrueFalseTotal
unless $redeem 0 3 3
41  
42 3 my $script = $redeem->value;
43 3 my @result = ({type => 'signature', more => [$script->to_serialized]});
44  
45 3 my $type = $script->type;
46 3 ▶ if ($type eq 'P2WPKH' || $type eq 'P2WSH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'P2WPKH' or $type eq 'P2WSH') { } 2 1 3
elsif ($type eq 'P2MS') { } 1 0 1
Condition100%3/3
$type eq "P2WPKH" or $type eq "P2WSH"
l!l&&r!l&&!r
111
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"
ABresult
1X1
011
000
47 2 my $method = "_should_finalize_$type";
48  
49 2 push @result, $self->$method($input, $input_index, $script);
50 }
51 elsif ($type eq 'P2MS') {
52  
53 # are there any partial signatures?
54 1 my @partials = $self->get_all_fields('PSBT_IN_PARTIAL_SIG', $input_index);
55 1 ▶ return () unless @partials;
Branch: 1 branch
BranchTrueFalseTotal
unless @partials 0 1 1
56  
57 1 my $required_sigs = $script->required_sigs;
58 1 my $pubkeys = $script->get_raw_address;
59 1 my @sigs;
60  
61 1 foreach my $pubkey (@{$pubkeys}) {
62 2 foreach my $sig (@partials) {
63 3 ▶ next unless $sig->key->to_serialized eq $pubkey;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig->key->to_serialized eq $pubkey 1 2 3
64  
65 2 push @sigs, $sig->value;
66 2 last;
67 }
68 }
69  
70 1 ▶ return () unless @sigs == $required_sigs;
Branch: 1 branch
BranchTrueFalseTotal
unless @sigs == $required_sigs 0 1 1
71  
72 1 unshift @result, (
73 {type => 'signature', more => ['']},
74 {type => 'signature', sigs => [@sigs]},
75 );
76 }
77  
78 3 ▶ return () unless @result > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @result > 1 0 3 3
79 3 return @result;
80 }
81  
82 sub _should_finalize_P2WPKH
83 5 ▶{
Subroutine _should_finalize_P2WPKH: called CC=5 SCAR=16.7
84 5 my ($self, $input, $input_index, $compat_script) = @_;
85  
86 # are there any partial signatures?
87 5 my @partials = $self->get_all_fields('PSBT_IN_PARTIAL_SIG', $input_index);
88 5 ▶ return () unless @partials;
Branch: 1 branch
BranchTrueFalseTotal
unless @partials 0 5 5
89  
90 5 ▶ my $to_compare = $compat_script
Branch: 1 branch
BranchTrueFalseTotal
$compat_script ? : 1 4 5
91 ? $compat_script->get_raw_address
92 : $input->utxo->output->locking_script->get_raw_address
93 ;
94  
95 5 foreach my $sig (@partials) {
96 5 ▶ return ({type => 'witness', sigs => [$sig->value], more => [$sig->key->to_serialized]})
Branch: 1 branch
BranchTrueFalseTotal
if $sig->key->get_hash eq $to_compare 5 0 5
97 if $sig->key->get_hash eq $to_compare;
98 }
99  
100 0 return ();
101 }
102  
103 sub _should_finalize_P2WSH
104 2 ▶{
Subroutine _should_finalize_P2WSH: called CC=8 SCAR=21.2
105 2 my ($self, $input, $input_index, $compat_script) = @_;
106  
107 2 my $redeem = $self->get_all_fields('PSBT_IN_WITNESS_SCRIPT', $input_index);
108 2 ▶ return () unless $redeem;
Branch: 1 branch
BranchTrueFalseTotal
unless $redeem 0 2 2
109  
110 # are there any partial signatures?
111 2 my @partials = $self->get_all_fields('PSBT_IN_PARTIAL_SIG', $input_index);
112 2 ▶ return () unless @partials;
Branch: 1 branch
BranchTrueFalseTotal
unless @partials 0 2 2
113  
114 2 my $script = $redeem->value;
115 2 ▶ if ($script->type eq 'P2MS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($script->type eq "P2MS") 2 0 2
116 2 my $required_sigs = $script->required_sigs;
117 2 my $pubkeys = $script->get_raw_address;
118 2 my @sigs;
119  
120 2 foreach my $pubkey (@{$pubkeys}) {
121 4 foreach my $sig (@partials) {
122 6 ▶ next unless $sig->key->to_serialized eq $pubkey;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig->key->to_serialized eq $pubkey 2 4 6
123  
124 4 push @sigs, $sig->value;
125 4 last;
126 }
127 }
128  
129 2 ▶ return () unless @sigs == $required_sigs;
Branch: 1 branch
BranchTrueFalseTotal
unless @sigs == $required_sigs 0 2 2
130  
131 return (
132 2 {type => 'witness', more => ['']},
133 {type => 'witness', sigs => [@sigs], more => [$script->to_serialized]},
134 );
135 }
136  
137 0 return ();
138 }
139  
140 sub _should_finalize_P2TR_keypath
141 4 ▶{
Subroutine _should_finalize_P2TR_keypath: called CC=2 SCAR=6.9
142 4 my ($self, $input, $input_index) = @_;
143  
144 4 my $sig = $self->get_all_fields('PSBT_IN_TAP_KEY_SIG', $input_index);
145 4 ▶ return () unless $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig 2 2 4
146  
147 return (
148 2 {type => 'witness', sigs => [$sig->value]},
149 );
150 }
151  
152 sub _should_finalize_P2TR_scriptpath
153 2 ▶{
Subroutine _should_finalize_P2TR_scriptpath: called CC=10 SCAR=23.9
154 2 my ($self, $input, $input_index) = @_;
155  
156 2 my @leaves = $self->get_all_fields('PSBT_IN_TAP_LEAF_SCRIPT', $input_index);
157 2 ▶ return () unless @leaves;
Branch: 1 branch
BranchTrueFalseTotal
unless @leaves 1 1 2
158  
159 1 my @partial_sigs = $self->get_all_fields('PSBT_IN_TAP_SCRIPT_SIG', $input_index);
160 1 ▶ return () unless @partial_sigs;
Branch: 1 branch
BranchTrueFalseTotal
unless @partial_sigs 0 1 1
161  
162 # first leaf found to be fully signed is served
163 1 foreach my $leaf (@leaves) {
164 1 my $leaf_obj = $leaf->value;
165 1 my $script = $leaf_obj->script;
166 1 ▶ next if $script->type ne 'TAPSCRIPT_MULTISIG';
Branch: 1 branch
BranchTrueFalseTotal
if $script->type ne "TAPSCRIPT_MULTISIG" 0 1 1
167  
168 1 my $pubkeys = $script->get_raw_address;
169 1 my $required_sigs = $script->required_sigs;
170  
171 1 my @sigs;
172 1 foreach my $pubkey (@{$pubkeys}) {
173 3 ▶ my $sig = first {
Subroutine __ANON__: called CC=2 SCAR=6.9
174 3 my $key = $_->key;
175 3 ▶ $key->[0]->get_xonly_key eq $pubkey && $key->[1] eq $leaf_obj->hash;
Branch: 1 branch
BranchTrueFalseTotal
$key->[0]->get_xonly_key eq $pubkey and $key->[1] eq $leaf_obj->hash 2 1 3
176 2 } @partial_sigs;
177  
178 2 ▶ push @sigs, $sig ? $sig->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$sig ? : 2 0 2
179 }
180  
181 1 foreach my $sig (@sigs) {
182 2 ▶ if (defined $sig && $required_sigs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sig and $required_sigs > 0) { } 2 0 2
Condition33%1/3
defined $sig and $required_sigs > 0
!ll&&!rl&&r
002
MC/DC0%0/2
defined $sig and $required_sigs > 0
defined $sig $required_sigs > 0
Truth table33%1/3
A and B
A= defined $sigB= $required_sigs > 0
ABresult
0X0
100
111
183 2 $required_sigs -= 1;
184 }
185 else {
186 0 $sig = '';
187 }
188 }
189  
190 return (
191 {
192 1 type => 'witness',
193 sigs => [reverse @sigs],
194 more => [$script->to_serialized, $leaf->key->to_serialized]
195 },
196 );
197 }
198  
199 0 return ();
200 }
201  
202 sub _should_finalize_P2TR
203 4 ▶{
Subroutine _should_finalize_P2TR: called CC=2 SCAR=6.9
204 4 my ($self, $input, $input_index) = @_;
205  
206 4 my @res = $self->_should_finalize_P2TR_keypath($input, $input_index);
207 4 ▶ @res = $self->_should_finalize_P2TR_scriptpath($input, $input_index)
Branch: 1 branch
BranchTrueFalseTotal
unless @res 2 2 4
208 unless @res;
209  
210 4 return @res;
211 }
212  
213 sub _should_finalize
214 13 ▶{
Subroutine _should_finalize: called CC=5 SCAR=17.1
215 13 my ($self, $input, $input_index) = @_;
216  
217 # is this input finalized already?
218 13 my $signature_field = $self->get_all_fields('PSBT_IN_FINAL_SCRIPTSIG', $input_index);
219 13 my $witness_field = $self->get_all_fields('PSBT_IN_FINAL_SCRIPTWITNESS', $input_index);
220 13 ▶ return () if $signature_field || $witness_field;
Branch: 1 branch
BranchTrueFalseTotal
if $signature_field or $witness_field 0 13 13
Condition33%1/3
$signature_field or $witness_field
l!l&&r!l&&!r
0013
MC/DC0%0/2
$signature_field or $witness_field
$signature_field $witness_field
Truth table33%1/3
A or B
A= $signature_fieldB= $witness_field
ABresult
1X1
011
000
221  
222 # custom script - unsupported
223 13 my $type = $input->utxo->output->locking_script->type;
224 13 ▶ return () unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 13 13
225  
226 # some types are defined but unsupported (P2PK, nulldata)
227 13 my $method = $self->can("_should_finalize_$type");
228 13 ▶ return () unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 13 13
229  
230 13 return $self->$method($input, $input_index);
231 }
232  
233 sub _finalize
234 11 ▶{
Subroutine _finalize: called CC=8 SCAR=20.8
235 11 my ($self, $input_index, $signature, $witness) = @_;
236  
237 # according to BIP174 - don't set SCRIPTSIG or SCRIPTWITNESS at all if
238 # there are no signatures in each one of them
239  
240 11 ▶ if (@$signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$signature) 4 7 11
241 4 my $script = btc_script->new;
242  
243 4 for my $part (@$signature) {
244 8 $script->push($part);
245 }
246  
247 $self->add_field(
248 4 type => 'PSBT_IN_FINAL_SCRIPTSIG',
249 index => $input_index,
250 value => $script,
251 );
252 }
253  
254 11 ▶ if (@$witness) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$witness) 9 2 11
255 9 $self->add_field(
256 type => 'PSBT_IN_FINAL_SCRIPTWITNESS',
257 index => $input_index,
258 value => $witness,
259 );
260 }
261  
262 # list all fields to remove, since according to BIP174, unknown fields
263 # should not be removed. Treat all propertiary fields as unknown.
264 # this list is complete for BIPs 174. 370 and 371
265 state $to_remove = {
266 11 map { $_ => 1 }
267 qw(
268 PSBT_IN_PARTIAL_SIG
269 PSBT_IN_SIGHASH_TYPE
270 PSBT_IN_REDEEM_SCRIPT
271 PSBT_IN_WITNESS_SCRIPT
272 PSBT_IN_BIP32_DERIVATION
273 PSBT_IN_POR_COMMITMENT
274 PSBT_IN_RIPEMD160
275 PSBT_IN_SHA256
276 PSBT_IN_HASH160
277 PSBT_IN_HASH256
278 PSBT_IN_TAP_KEY_SIG
279 PSBT_IN_TAP_SCRIPT_SIG
280 PSBT_IN_TAP_LEAF_SCRIPT
281 PSBT_IN_TAP_BIP32_DERIVATION
282 PSBT_IN_TAP_INTERNAL_KEY
283 PSBT_IN_TAP_MERKLE_ROOT
284 )
285 };
286  
287 11 foreach my $field_info ($self->list_fields) {
288 153 ▶ next unless $field_info->[0]->map_type eq PSBT_INPUT_MAP;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_info->[0]->map_type eq "in" 77 76 153
289 76 ▶ next unless $field_info->[1] == $input_index;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_info->[1] == $input_index 9 67 76
290 67 ▶ next unless $to_remove->{$field_info->[0]->name};
Branch: 1 branch
BranchTrueFalseTotal
unless $$to_remove{$field_info->[0]->name} 46 21 67
291  
292 21 $self->remove_all_fields(@{$field_info});
293 }
294 }
295  
296 sub finalize
297 12 ▶{
Subroutine finalize: called CC=14 SCAR=26.4
Pod undocumented
298 12 my ($self) = @_;
299  
300 12 my $version = $self->version;
301 12 my $tx = $self->get_transaction;
302 12 my $inputs = $tx->inputs;
303  
304 12 foreach my $input_index (0 .. $#{$inputs}) {
305 13 my @sig_parts = $self->_should_finalize($inputs->[$input_index], $input_index);
306 13 ▶ next unless @sig_parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @sig_parts 1 12 13
307  
308 12 my @to_check;
309 my @signature;
310 12 my @witness;
311 12 foreach my $part (@sig_parts) {
312 my $to_push = $part->{type} eq 'signature'
313 ? \@signature
314 18 ▶ : $part->{type} eq 'witness' ? \@witness
Branch: 2 branches
BranchTrueFalseTotal
$part->{'type'} eq 'signature' ? : 6 12 18
$part->{'type'} eq 'witness' ? : 12 0 12
315 : die "bad PSBT finalizer part type $part->{type}"
316 ;
317  
318 12 push @to_check, @{$part->{sigs}}
319 18 ▶ if $part->{sigs};
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"sigs"} 12 6 18
320  
321 12 push @{$to_push}, @{$part->{sigs}}
322 18 ▶ if $part->{sigs};
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"sigs"} 12 6 18
323  
324 15 push @{$to_push}, @{$part->{more}}
325 18 ▶ if $part->{more};
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"more"} 15 3 18
326 }
327  
328 # check sighashes in signatures
329 # NOTE: no checking for SIGHASH_DEFAULT (0x00) - may be a problem, but it
330 # is never present in the signature anyway
331 12 my $sighash = $self->get_all_fields('PSBT_IN_SIGHASH_TYPE', $input_index);
332 12 ▶ if ($sighash && $sighash->value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sighash and $sighash->value) 4 8 12
Condition66%2/3
$sighash and $sighash->value
!ll&&!rl&&r
804
MC/DC50%1/2
$sighash and $sighash->value
$sighash $sighash->value
Truth table66%2/3
A and B
A= $sighashB= $sighash->value
ABresult
0X0
100
111
333 4 my $sighash_byte = chr $sighash->value;
334 4 foreach my $item (@to_check) {
335 6 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless substr($item, -1) eq $sighash_byte 1 5 6
336 "bad sighash in PSBT input $input_index"
337 ) unless substr($item, -1) eq $sighash_byte;
338 }
339 }
340  
341 11 $self->_finalize($input_index, \@signature, \@witness);
342 }
343  
344 Bitcoin::Crypto::Exception::PSBT->raise(
345 11 ▶ 'could not finalize the PSBT: unable to finalize all inputs'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_final 1 10 11
346 ) unless $self->is_final;
347  
348 10 return $self;
349 }
350  
351 sub is_final
352 13 ▶{
Subroutine is_final: called CC=3 SCAR=11.0
Pod undocumented
353 13 my ($self) = @_;
354  
355 13 my %final_map;
356 13 foreach my $field_info ($self->list_fields) {
357 148 my $name = $field_info->[0]->name;
358 148 ▶ next unless any { $name eq $_ } qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $name eq $_; } , "PSBT_IN_FINAL_SCRIPTSIG", "PSBT_IN_FINAL_SCRIPTWITNESS") 134 14 148
359 PSBT_IN_FINAL_SCRIPTSIG
360 PSBT_IN_FINAL_SCRIPTWITNESS
361 );
362  
363 14 $final_map{$field_info->[1]} = 1;
364 }
365  
366 # PSBT is considered final if all inputs have a final field
367 13 ▶ return all { $final_map{$_} } 0 .. $self->input_count - 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
368 }
369  
370 12 1;
371  
« Crypto/Key/ExtBase.pm ↑ Summary Crypto/Util/Internal.pm »