Crypto/PSBT/FieldType.pm

filter: Statementstmt 100% 287 / 287 Branchbran 81.8% 36 / 44 Conditioncond 68.4% 13 / 19 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 78 / 78 Podpod n/a totaltotal 95.4% 419 / 439 CC 31 SCAR 8.2
CC
31
Cov
96%
__ANON__
8.0
__ANON__
5.3
__ANON__
5.0
CRAP 2.3
« Crypto/Transaction/Signer/Legacy.pm ↑ Summary Crypto/Role/SignVerify.pm »
1 package Bitcoin::Crypto::PSBT::FieldType;
2 1 $Bitcoin::Crypto::PSBT::FieldType::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;
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 notall);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Bitcoin::Crypto qw(btc_extpub btc_pub btc_transaction btc_script btc_tapscript btc_script_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Bitcoin::Crypto::Transaction::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Bitcoin::Crypto::Script::Tree::Leaf;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Bitcoin::Crypto::Transaction::ControlBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Bitcoin::Crypto::DerivationPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Bitcoin::Crypto::Constants qw(:transaction);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use Bitcoin::Crypto::Util::Internal qw(pack_compactsize unpack_compactsize pack_array unpack_array lift_x);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Bitcoin::Crypto::Helpers qw(encode_64bit decode_64bit die_no_trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 REQUIRED => 'required',
23 AVAILABLE => 'available',
24 12 };
25  
26 1 has param 'name' => (
27 isa => Str,
28 );
29  
30 1 has param 'code' => (
31 isa => PositiveOrZeroInt,
32 );
33  
34 has param 'map_type' => (
35 isa => PSBTMapType,
36 528 ▶ lazy => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
37 528 my $self = shift;
38 528 my $name = $self->name;
39 528 ▶ die unless $name =~ /^PSBT_([A-Z]+)_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^PSBT_([A-Z]+)_/u 0 528 528
40 528 return lc $1;
41 }
42 1 );
43  
44 has param 'serializer' => (
45 isa => CodeRef,
46 145 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 34 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 34 state $sig = signature(positional => [ByteStr]);
49 34 my $value = ($sig->(@_))[0];
50 34 return $value;
51 }
52 145 },
53 1 );
54  
55 has param 'deserializer' => (
56 isa => CodeRef,
57 145 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 92 ▶ sub { $_[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
59 145 },
60 1 );
61  
62 has param 'key_serializer' => (
63 isa => CodeRef,
64 397 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 state $sig = signature(positional => [ByteStr]);
67 1 my $value = ($sig->(@_))[0];
68 1 return $value;
69 }
70 397 },
71 1 );
72  
73 has param 'key_deserializer' => (
74 isa => CodeRef,
75 397 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 240 ▶ sub { $_[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
77 397 },
78 1 );
79  
80 1 has option 'validator' => (
81 isa => CodeRef,
82 );
83  
84 1 has option 'key_data' => (
85 isa => Str,
86 );
87  
88 1 has param 'value_data' => (
89 isa => Str,
90 );
91  
92 1 has param 'version_status' => (
93 isa => HashRef,
94 );
95  
96 # REUSABLE SERIALIZERS
97  
98 my %transaction_serializers = (
99 value_data => "Bitcoin::Crypto::Transaction object",
100 43 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 43 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Transaction']]);
102 43 my $value = ($sig->(@_))[0];
103 43 return $value->to_serialized;
104 },
105 479 ▶ deserializer => sub { btc_transaction->from_serialized(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
106 1 );
107  
108 my %uint_32bitLE_serializers = (
109 value_data => "32-bit positive integer value",
110 216 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 216 state $sig = signature(positional => [IntMaxBits [32]]);
112 216 my $value = ($sig->(@_))[0];
113 216 return pack 'V', $value;
114 },
115 1395 ▶ deserializer => sub { unpack 'V', shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 1 );
117  
118 my %uint_compactsize_serializers = (
119 value_data => "Positive integer value",
120 98 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 98 state $sig = signature(positional => [PositiveOrZeroInt]);
122 98 my $value = ($sig->(@_))[0];
123 98 return pack_compactsize $value;
124 },
125 763 ▶ deserializer => sub { unpack_compactsize shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 1 );
127  
128 my %fingerprint_and_path_serializers = (
129 value_data =>
130 "Array reference, where the first item is a fingerprint and the second item is Bitcoin::Crypto::DerivationPath",
131 62 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 62 state $sig = signature(positional => [ByteStr, DerivationPath]);
133 62 my ($fingerprint, $path) = $sig->(@{$_[0]});
134  
135 62 return $fingerprint . pack 'V*', @{$path->path};
136 },
137 233 ▶ deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 233 my $val = shift;
139 233 my $fingerprint = substr $val, 0, 4, '';
140 return [
141 233 $fingerprint,
142 Bitcoin::Crypto::DerivationPath->new(private => 1, path => [unpack 'V*', $val]),
143 ];
144 },
145 1 );
146  
147 my %script_serializers = (
148 value_data => "Bitcoin::Crypto::Script object",
149 81 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
150 81 state $sig = signature(positional => [BitcoinScript]);
151 81 my $value = ($sig->(@_))[0];
152 81 return $value->to_serialized;
153 },
154 252 ▶ deserializer => sub { btc_script->from_serialized(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 1 );
156  
157 my %proprietary_key_serializers = (
158 key_data => "Array reference with three items: bytestring, unsigned integer, bytestring",
159 4 ▶ key_serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
160 4 state $sig = signature(positional => [Tuple [ByteStr, PositiveOrZeroInt, ByteStr]]);
161 4 my ($ident, $subtype, $subkey) = @{($sig->(@_))[0]};
162  
163 4 my $result = '';
164 4 $result .= pack_compactsize(length $ident);
165 4 $result .= $ident;
166 4 $result .= pack_compactsize($subtype);
167 4 $result .= $subkey;
168  
169 4 return $result;
170 },
171 10 ▶ key_deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 10 my $val = shift;
173 10 my $pos = 0;
174  
175 10 my $ident_len = unpack_compactsize($val, \$pos);
176 10 my $ident = substr $val, $pos, $ident_len;
177 10 $pos += $ident_len;
178  
179 10 my $subtype = unpack_compactsize($val, \$pos);
180 10 my $subkey = substr $val, $pos;
181  
182 10 return [$ident, $subtype, $subkey];
183 },
184 1 );
185  
186 my %public_key_serializers = (
187 key_data => "Bitcoin::Crypto::Key::Public object",
188 55 ▶ key_serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 55 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Key::Public']]);
190 55 my $value = ($sig->(@_))[0];
191 55 return $value->to_serialized;
192 },
193 205 ▶ key_deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
194 205 return btc_pub->from_serialized(shift);
195 },
196 1 );
197  
198 my %taproot_public_key_serializers = (
199 key_data => "Bitcoin::Crypto::Key::Public object",
200 35 ▶ key_serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
201 35 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Key::Public']]);
202 35 my $value = ($sig->(@_))[0];
203 35 return $value->get_xonly_key;
204 },
205 127 ▶ key_deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 127 my $pubkey = btc_pub->from_serialized(lift_x shift);
207 123 return $pubkey;
208 },
209 1 );
210  
211 my %taproot_fingerprint_and_path_serializers = (
212 value_data =>
213 "Array reference, where first item is an array of leaf hashes, second element is a fingerprint and the third element is Bitcoin::Crypto::DerivationPath",
214 19 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 19 state $sig = signature(positional => [ArrayRef [ByteStr], ArrayRef, {slurpy => !!1}]);
216 19 my ($hashes, $rest) = $sig->(@{$_[0]});
217 19 my $hashes_raw = pack_compactsize(scalar @$hashes) . join '', @$hashes;
218  
219 19 return $hashes_raw . $fingerprint_and_path_serializers{serializer}->($rest);
220 },
221 69 ▶ deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 69 my $val = shift;
223  
224 69 my $pos = 0;
225 69 my $hash_count = unpack_compactsize($val, \$pos);
226 69 my @hashes = unpack "(a32)$hash_count", substr $val, $pos;
227 69 $pos += $hash_count * 32;
228  
229 69 my $arr = $fingerprint_and_path_serializers{deserializer}->(substr $val, $pos);
230 69 unshift @$arr, \@hashes;
231 69 return $arr;
232 },
233 1 );
234  
235 # TYPES
236  
237 my %types = (
238  
239 # GLOBAL
240  
241 PSBT_GLOBAL_UNSIGNED_TX => {
242 code => 0x00,
243 %transaction_serializers,
244 83 ▶ validator => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
245 83 my ($tx) = @_;
246  
247 die_no_trace 'must not have signatures'
248 83 ▶ if notall { $_->signature_script->is_empty } @{$tx->inputs};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &notall(sub { $_->signature_script->is_empty; } , @{$tx->inputs;}) 1 82 83
249  
250 die_no_trace 'must be in non-witness format'
251 82 ▶ if $tx->had_witness_flag || any { $_->has_witness } @{$tx->inputs};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $tx->had_witness_flag or &any(sub { $_->has_witness; } , @{$tx->inputs;}) 1 81 82
Condition66%2/3
$tx->had_witness_flag or &any(sub { $_->has_witness; } , @{$tx->inputs;})
l!l&&r!l&&!r
1081
MC/DC50%1/2
$tx->had_witness_flag or &any(sub { $_->has_witness; } , @{$tx->inputs;})
$tx->had_witness_flag &any(sub { $_->has_witness; } , @{$tx->inputs;})
Truth table66%2/3
A or B
A= $tx->had_witness_flagB= &any(sub { $_->has_witness; } , @{$tx->inputs;})
ABresult
1X1
011
000
252 },
253 version_status => {
254 0 => REQUIRED,
255 },
256 },
257  
258 PSBT_GLOBAL_XPUB => {
259 code => 0x01,
260 key_data => "Bitcoin::Crypto::Key::ExtPublic object",
261 3 ▶ key_serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
262 3 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Key::ExtPublic']]);
263 3 my $value = ($sig->(@_))[0];
264 3 return $value->to_serialized;
265 },
266 10 ▶ key_deserializer => sub { btc_extpub->from_serialized(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
267 %fingerprint_and_path_serializers,
268 version_status => {
269 0 => AVAILABLE,
270 2 => AVAILABLE,
271 },
272 },
273  
274 PSBT_GLOBAL_TX_VERSION => {
275 code => 0x02,
276 %uint_32bitLE_serializers,
277 version_status => {
278 2 => REQUIRED,
279 },
280 },
281  
282 PSBT_GLOBAL_FALLBACK_LOCKTIME => {
283 code => 0x03,
284 %uint_32bitLE_serializers,
285 version_status => {
286 2 => AVAILABLE,
287 },
288 },
289  
290 PSBT_GLOBAL_INPUT_COUNT => {
291 code => 0x04,
292 %uint_compactsize_serializers,
293 version_status => {
294 2 => REQUIRED,
295 },
296 },
297  
298 PSBT_GLOBAL_OUTPUT_COUNT => {
299 code => 0x05,
300 %uint_compactsize_serializers,
301 version_status => {
302 2 => REQUIRED,
303 },
304 },
305  
306 PSBT_GLOBAL_TX_MODIFIABLE => {
307 code => 0x06,
308 value_data => "Hash reference with flags: inputs_modifiable, outputs_modifiable, has_sighash_single",
309 51 ▶ serializer => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
310 51 state $sig = signature(positional => [HashRef]);
311 51 my $hash = ($sig->(@_))[0];
312  
313 51 ▶ my $raw = $hash->{raw_value} // 0;
Condition100%2/2
$hash->{"raw_value"} // 0
l!l
3219
MC/DC100%1/1
$hash->{"raw_value"} // 0
$hash->{"raw_value"}
Truth table100%2/2
A
A= $hash->{"raw_value"}
Aresult
11
00
314 51 ▶ $raw = $hash->{inputs_modifiable} ? $raw | 0x01 : $raw & ~0x01;
Branch: 1 branch
BranchTrueFalseTotal
$hash->{'inputs_modifiable'} ? : 27 24 51
315 51 ▶ $raw = $hash->{outputs_modifiable} ? $raw | 0x02 : $raw & ~0x02;
Branch: 1 branch
BranchTrueFalseTotal
$hash->{'outputs_modifiable'} ? : 27 24 51
316 51 ▶ $raw = $hash->{has_sighash_single} ? $raw | 0x04 : $raw & ~0x04;
Branch: 1 branch
BranchTrueFalseTotal
$hash->{'has_sighash_single'} ? : 13 38 51
317  
318 51 return pack 'C', $raw;
319 },
320 91 ▶ deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 91 my $raw = unpack 'C', shift;
322  
323 return {
324 91 raw_value => $raw,
325 inputs_modifiable => !!($raw & 0x01),
326 outputs_modifiable => !!($raw & 0x02),
327 has_sighash_single => !!($raw & 0x04),
328 };
329 },
330 version_status => {
331 2 => AVAILABLE
332 },
333 },
334  
335 PSBT_GLOBAL_VERSION => {
336 code => 0xfb,
337 %uint_32bitLE_serializers,
338 version_status => {
339 0 => AVAILABLE,
340 2 => AVAILABLE,
341 },
342 },
343  
344 PSBT_GLOBAL_PROPRIETARY => {
345 code => 0xfc,
346 value_data => "Bytestring value",
347 %proprietary_key_serializers,
348 version_status => {
349 0 => AVAILABLE,
350 2 => AVAILABLE,
351 },
352 },
353  
354 # INPUT
355  
356 PSBT_IN_NON_WITNESS_UTXO => {
357 code => 0x00,
358 %transaction_serializers,
359 version_status => {
360 0 => AVAILABLE,
361 2 => AVAILABLE,
362 },
363 },
364  
365 PSBT_IN_WITNESS_UTXO => {
366 code => 0x01,
367 value_data => "Bitcoin::Crypto::Transaction::Output object",
368 40 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
369 40 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Transaction::Output']]);
370 40 my $value = ($sig->(@_))[0];
371 40 return $value->to_serialized;
372 },
373 179 ▶ deserializer => sub { Bitcoin::Crypto::Transaction::Output->from_serialized(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
374 version_status => {
375 0 => AVAILABLE,
376 2 => AVAILABLE,
377 },
378 },
379  
380 PSBT_IN_PARTIAL_SIG => {
381 code => 0x02,
382 value_data => "Bytestring value",
383 %public_key_serializers,
384 version_status => {
385 0 => AVAILABLE,
386 2 => AVAILABLE,
387 },
388 },
389  
390 PSBT_IN_SIGHASH_TYPE => {
391 code => 0x03,
392 %uint_32bitLE_serializers,
393 version_status => {
394 0 => AVAILABLE,
395 2 => AVAILABLE,
396 },
397 },
398  
399 PSBT_IN_REDEEM_SCRIPT => {
400 code => 0x04,
401 %script_serializers,
402 version_status => {
403 0 => AVAILABLE,
404 2 => AVAILABLE,
405 },
406 },
407  
408 PSBT_IN_WITNESS_SCRIPT => {
409 code => 0x05,
410 %script_serializers,
411 version_status => {
412 0 => AVAILABLE,
413 2 => AVAILABLE,
414 },
415 },
416  
417 PSBT_IN_BIP32_DERIVATION => {
418 code => 0x06,
419 %public_key_serializers,
420 %fingerprint_and_path_serializers,
421 version_status => {
422 0 => AVAILABLE,
423 2 => AVAILABLE,
424 },
425 },
426  
427 PSBT_IN_FINAL_SCRIPTSIG => {
428 code => 0x07,
429 %script_serializers,
430 version_status => {
431 0 => AVAILABLE,
432 2 => AVAILABLE,
433 },
434 },
435  
436 PSBT_IN_FINAL_SCRIPTWITNESS => {
437 code => 0x08,
438 value_data => "Array reference, representing witness stack",
439 10 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
440 10 state $sig = signature(positional => [ArrayRef [ByteStr]]);
441 10 my ($array) = $sig->(@_);
442  
443 10 return pack_array $array;
444 },
445 32 ▶ deserializer => sub { unpack_array shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
446 version_status => {
447 0 => AVAILABLE,
448 2 => AVAILABLE,
449 },
450 },
451  
452 # NOTE: this comes from BIP127 which isn't technically implemented, but no
453 # harm in keeping it anyway
454 PSBT_IN_POR_COMMITMENT => {
455 code => 0x09,
456 value_data => "Bytestring value",
457 version_status => {
458 0 => AVAILABLE,
459 2 => AVAILABLE,
460 },
461 },
462  
463 PSBT_IN_RIPEMD160 => {
464 code => 0x0a,
465 key_data => "Bytestring value",
466 value_data => "Bytestring value",
467 version_status => {
468 0 => AVAILABLE,
469 2 => AVAILABLE,
470 },
471 },
472  
473 PSBT_IN_SHA256 => {
474 code => 0x0b,
475 key_data => "Bytestring value",
476 value_data => "Bytestring value",
477 version_status => {
478 0 => AVAILABLE,
479 2 => AVAILABLE,
480 },
481 },
482  
483 PSBT_IN_HASH160 => {
484 code => 0x0c,
485 key_data => "Bytestring value",
486 value_data => "Bytestring value",
487 version_status => {
488 0 => AVAILABLE,
489 2 => AVAILABLE,
490 },
491 },
492  
493 PSBT_IN_HASH256 => {
494 code => 0x0d,
495 key_data => "Bytestring value",
496 value_data => "Bytestring value",
497 version_status => {
498 0 => AVAILABLE,
499 2 => AVAILABLE,
500 },
501 },
502  
503 # NOTE: as usual, txids are represented in different byte order when serialized
504 PSBT_IN_PREVIOUS_TXID => {
505 code => 0x0e,
506 value_data => "Bytestring value",
507 59 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
508 59 state $sig = signature(positional => [ByteStr]);
509 59 my $value = ($sig->(@_))[0];
510 59 return scalar reverse $value;
511 },
512 159 ▶ deserializer => sub { scalar reverse shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
513 version_status => {
514 2 => REQUIRED,
515 },
516 },
517  
518 PSBT_IN_OUTPUT_INDEX => {
519 code => 0x0f,
520 %uint_32bitLE_serializers,
521 version_status => {
522 2 => REQUIRED,
523 },
524 },
525  
526 PSBT_IN_SEQUENCE => {
527 code => 0x10,
528 %uint_32bitLE_serializers,
529 version_status => {
530 2 => AVAILABLE,
531 },
532 },
533  
534 PSBT_IN_REQUIRED_TIME_LOCKTIME => {
535 code => 0x11,
536 %uint_32bitLE_serializers,
537 13 ▶ validator => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
538 13 my ($value) = @_;
539 13 ▶ die_no_trace 'must be greater than or equal to '
Branch: 1 branch
BranchTrueFalseTotal
if $value < 500000000 1 12 13
540 . LOCKTIME_HEIGHT_THRESHOLD
541 if $value < LOCKTIME_HEIGHT_THRESHOLD;
542 },
543 version_status => {
544 2 => AVAILABLE,
545 },
546 },
547  
548 PSBT_IN_REQUIRED_HEIGHT_LOCKTIME => {
549 code => 0x12,
550 %uint_32bitLE_serializers,
551 13 ▶ validator => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
552 13 my ($value) = @_;
553 13 ▶ die_no_trace 'must be less than ' . LOCKTIME_HEIGHT_THRESHOLD
Branch: 1 branch
BranchTrueFalseTotal
unless $value < 500000000 1 12 13
554 unless $value < LOCKTIME_HEIGHT_THRESHOLD;
555 },
556 version_status => {
557 2 => AVAILABLE,
558 },
559 },
560  
561 PSBT_IN_TAP_KEY_SIG => {
562 code => 0x13,
563 value_data => "Bytestring value",
564 7 ▶ validator => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
565 7 my ($value) = @_;
566 7 state $validator = ByteStrLen [64] | ByteStrLen [65];
567 7 ▶ die_no_trace 'invalid signature length'
Branch: 1 branch
BranchTrueFalseTotal
unless $validator->check($value) 2 5 7
568 unless $validator->check($value);
569 },
570 version_status => {
571 0 => AVAILABLE,
572 2 => AVAILABLE,
573 },
574 },
575  
576 PSBT_IN_TAP_SCRIPT_SIG => {
577 code => 0x14,
578 key_data =>
579 "Array reference, where the first item is Bitcoin::Crypto::Key::Public and second element is a leaf hash bytestring",
580 6 ▶ key_serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
581 6 state $sig = signature(positional => [Any, ByteStr]);
582 6 my ($pubkey, $leaf_hash) = $sig->(@{$_[0]});
583  
584 6 return $taproot_public_key_serializers{key_serializer}->($pubkey) . $leaf_hash;
585 },
586 26 ▶ key_deserializer => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
587 26 my $val = shift;
588 26 ▶ die_no_trace 'invalid length'
Branch: 1 branch
BranchTrueFalseTotal
unless length $val == 64 1 25 26
589 unless length $val == 64;
590  
591 25 my $leaf_hash = substr $val, 32, 32;
592  
593 return [
594 25 $taproot_public_key_serializers{key_deserializer}->(substr $val, 0, 32),
595 $leaf_hash,
596 ];
597 },
598 value_data => "Bytestring value",
599 11 ▶ validator => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
600 11 my ($key, $value) = @_;
601 11 state $validator = ByteStrLen [64] | ByteStrLen [65];
602 11 ▶ die_no_trace 'invalid signature length'
Branch: 1 branch
BranchTrueFalseTotal
unless $validator->check($value) 2 9 11
603 unless $validator->check($value);
604 },
605 version_status => {
606 0 => AVAILABLE,
607 2 => AVAILABLE,
608 },
609 },
610  
611 PSBT_IN_TAP_LEAF_SCRIPT => {
612 code => 0x15,
613 key_data => "Instance of Bitcoin::Crypto::Transaction::ControlBlock",
614 8 ▶ key_serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
615 8 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Transaction::ControlBlock']]);
616 8 my $block = ($sig->(@_))[0];
617  
618 8 return $block->to_serialized;
619 },
620 29 ▶ key_deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
621 29 my $val = shift;
622  
623 29 return Bitcoin::Crypto::Transaction::ControlBlock->from_serialized($val);
624 },
625 value_data =>
626 "Instance of Bitcoin::Crypto::Script::Tree::Leaf (with script and leaf version)",
627 8 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
628 8 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Script::Tree::Leaf']]);
629 8 my ($leaf) = $sig->(@_);
630  
631 8 return $leaf->script->to_serialized . pack 'C', $leaf->leaf_version;
632 },
633 29 ▶ deserializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
634 29 my $val = shift;
635  
636 29 my $leaf_version = unpack 'C', substr $val, -1, 1, '';
637 29 my $script_raw = $val;
638  
639 # NOTE: we assume here that future leaf versions will also use tapscript
640 29 return Bitcoin::Crypto::Script::Tree::Leaf->new(
641 script => btc_tapscript->from_serialized($script_raw),
642 leaf_version => $leaf_version,
643 );
644 },
645 14 ▶ validator => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
646 14 my ($key, $value) = @_;
647  
648 14 ▶ die_no_trace 'leaf object must have leaf_version'
Branch: 1 branch
BranchTrueFalseTotal
unless $value->has_leaf_version 0 14 14
649 unless $value->has_leaf_version;
650  
651 14 ▶ die_no_trace 'leaf object must have script'
Branch: 1 branch
BranchTrueFalseTotal
unless $value->has_script 0 14 14
652 unless $value->has_script;
653 },
654 version_status => {
655 0 => AVAILABLE,
656 2 => AVAILABLE,
657 },
658 },
659  
660 PSBT_IN_TAP_BIP32_DERIVATION => {
661 code => 0x16,
662 %taproot_fingerprint_and_path_serializers,
663 %taproot_public_key_serializers,
664 version_status => {
665 0 => AVAILABLE,
666 2 => AVAILABLE,
667 },
668 },
669  
670 PSBT_IN_TAP_INTERNAL_KEY => {
671 code => 0x17,
672 value_data => $taproot_public_key_serializers{key_data},
673 serializer => $taproot_public_key_serializers{key_serializer},
674 deserializer => $taproot_public_key_serializers{key_deserializer},
675 version_status => {
676 0 => AVAILABLE,
677 2 => AVAILABLE,
678 },
679 },
680  
681 PSBT_IN_TAP_MERKLE_ROOT => {
682 code => 0x18,
683 value_data => "Bytestring value",
684 5 ▶ validator => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
685 5 my ($value) = @_;
686 5 state $validator = ByteStrLen [32];
687 5 ▶ die_no_trace 'invalid merkle root length'
Branch: 1 branch
BranchTrueFalseTotal
unless $validator->check($value) 0 5 5
688 unless $validator->check($value);
689 },
690 version_status => {
691 0 => AVAILABLE,
692 2 => AVAILABLE,
693 },
694 },
695  
696 PSBT_IN_PROPRIETARY => {
697 code => 0xfc,
698 value_data => "Bytestring value",
699 %proprietary_key_serializers,
700 version_status => {
701 0 => AVAILABLE,
702 2 => AVAILABLE,
703 },
704 },
705  
706 # OUTPUT
707 PSBT_OUT_REDEEM_SCRIPT => {
708 code => 0x00,
709 %script_serializers,
710 version_status => {
711 0 => AVAILABLE,
712 2 => AVAILABLE,
713 },
714 },
715  
716 PSBT_OUT_WITNESS_SCRIPT => {
717 code => 0x01,
718 %script_serializers,
719 version_status => {
720 0 => AVAILABLE,
721 2 => AVAILABLE,
722 },
723 },
724  
725 PSBT_OUT_BIP32_DERIVATION => {
726 code => 0x02,
727 %public_key_serializers,
728 %fingerprint_and_path_serializers,
729 version_status => {
730 0 => AVAILABLE,
731 2 => AVAILABLE,
732 },
733 },
734  
735 PSBT_OUT_AMOUNT => {
736 code => 0x03,
737 value_data => "64 bit number",
738 64 ▶ serializer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
739 64 state $sig = signature(positional => [SatoshiAmount]);
740 64 my $value = ($sig->(@_))[0];
741 64 return encode_64bit($value);
742 },
743 192 ▶ deserializer => sub { decode_64bit(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
744 version_status => {
745 2 => REQUIRED,
746 },
747 },
748  
749 PSBT_OUT_SCRIPT => {
750 code => 0x04,
751 %script_serializers,
752 version_status => {
753 2 => REQUIRED,
754 },
755 },
756  
757 PSBT_OUT_TAP_INTERNAL_KEY => {
758 code => 0x05,
759 value_data => $taproot_public_key_serializers{key_data},
760 serializer => $taproot_public_key_serializers{key_serializer},
761 deserializer => $taproot_public_key_serializers{key_deserializer},
762 version_status => {
763 0 => AVAILABLE,
764 2 => AVAILABLE,
765 },
766 },
767  
768 PSBT_OUT_TAP_TREE => {
769 code => 0x06,
770 value_data => "Bitcoin::Crypto::Script::Tree instance",
771 2 ▶ serializer => sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
772 2 state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Script::Tree']]);
773 2 my $tree = ($sig->(@_))[0];
774  
775 2 my @list;
776 2 foreach my $leaf (@{$tree->get_leaves}) {
777 9 ▶ die_no_trace 'tree must have all its leaves unhashed'
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf->has_script and $leaf->has_leaf_version 0 9 9
Condition33%1/3
$leaf->has_script and $leaf->has_leaf_version
!ll&&!rl&&r
009
MC/DC0%0/2
$leaf->has_script and $leaf->has_leaf_version
$leaf->has_script $leaf->has_leaf_version
Truth table33%1/3
A and B
A= $leaf->has_scriptB= $leaf->has_leaf_version
ABresult
0X0
100
111
778 unless $leaf->has_script && $leaf->has_leaf_version;
779  
780 # this should always be the case, but check nonetheless
781 9 ▶ die_no_trace 'tree must have depth set for its leaves'
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf->has_depth 0 9 9
782 unless $leaf->has_depth;
783  
784 9 my $serialized = $leaf->script->to_serialized;
785  
786 9 push @list, pack('C', $leaf->depth)
787 . pack('C', $leaf->leaf_version)
788 . pack_compactsize(length $serialized)
789 . $serialized;
790 }
791  
792 2 return join '', @list;
793  
794 },
795 6 ▶ deserializer => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
796 6 my $value = shift;
797 6 my $pos = 0;
798  
799 6 my $tree = [];
800 6 my @stack = ($tree);
801  
802 6 ▶ while ($pos < length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $value) 21 6 27
803 21 my $depth = unpack 'C', substr $value, $pos, 1;
804 21 $pos += 1;
805  
806 21 my $leaf_version = unpack 'C', substr $value, $pos, 1;
807 21 $pos += 1;
808  
809 21 my $script_len = unpack_compactsize($value, \$pos);
810 21 my $script_raw = substr $value, $pos, $script_len;
811 21 $pos += $script_len;
812  
813 # NOTE: we assume here that future leaf versions will also use tapscript
814 21 my $leaf = {
815 leaf_version => $leaf_version,
816 script => btc_tapscript->from_serialized($script_raw),
817 };
818  
819 21 ▶ while ($#stack > $depth || (@stack && @{$stack[-1]} == 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#stack > $depth or @stack and @{$stack[-1];} == 2) 8 21 29
Condition83%5/6
$#stack > $depth or @stack and @{$stack[-1];} == 2
l!l&&r!l&&!r
7121
@stack and @{$stack[-1];} == 2
!ll&&!rl&&r
0211
MC/DC66%2/3
$#stack > $depth or @stack and @{$stack[-1];} == 2
$#stack > $depth @stack @{$stack[-1];} == 2
Truth table75%3/4
A or B and C
A= $#stack > $depthB= @stackC= @{$stack[-1];} == 2
ABCresult
1XX1
0111
00X0
0100
820 8 pop @stack;
821 }
822  
823 21 ▶ while ($depth > $#stack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > $#stack) 15 21 36
824 15 my $new_branch = [];
825 15 push @{$stack[-1]}, $new_branch;
826 15 push @stack, $new_branch;
827 }
828  
829 21 push @{$stack[-1]}, $leaf;
830 }
831  
832 # make sure we handle depth 0 without an extra level
833 6 ▶ if (@{$tree} == 1 && ref $tree->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$tree;} == 1 and ref $tree->[0] eq "ARRAY") 6 0 6
Condition33%1/3
@{$tree;} == 1 and ref $tree->[0] eq "ARRAY"
!ll&&!rl&&r
006
MC/DC0%0/2
@{$tree;} == 1 and ref $tree->[0] eq "ARRAY"
@{$tree;} == 1 ref $tree->[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @{$tree;} == 1B= ref $tree->[0] eq "ARRAY"
ABresult
0X0
100
111
834 6 $tree = $tree->[0];
835 }
836  
837 6 $tree = btc_script_tree->new(tree => $tree);
838  
839 # validate tree by getting merkle root (will traverse)
840 Bitcoin::Crypto::Exception::PSBT->trap_into(
841 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
842 6 $tree->get_merkle_root;
843 },
844 6 'invalid tree'
845 );
846  
847 6 return $tree;
848 },
849 1 version_status => {
850 0 => AVAILABLE,
851 2 => AVAILABLE,
852 },
853 },
854  
855 PSBT_OUT_TAP_BIP32_DERIVATION => {
856 code => 0x07,
857 %taproot_fingerprint_and_path_serializers,
858 %taproot_public_key_serializers,
859 version_status => {
860 0 => AVAILABLE,
861 2 => AVAILABLE,
862 },
863 },
864  
865 PSBT_OUT_PROPRIETARY => {
866 code => 0xfc,
867 value_data => "Bytestring value",
868 %proprietary_key_serializers,
869 version_status => {
870 0 => AVAILABLE,
871 2 => AVAILABLE,
872 },
873 },
874 );
875  
876 1 %types = map { $_, __PACKAGE__->new(name => $_, %{$types{$_}}) } keys %types;
877 1 my %types_reverse;
878 1 foreach my $type (values %types) {
879 44 $types_reverse{$type->map_type}{$type->code} = $type->name;
880 }
881  
882 1 signature_for get_field_by_code => (
883 method => !!1,
884 positional => [PSBTMapType, PositiveOrZeroInt],
885 );
886  
887 sub get_field_by_code
888 603 ▶{
Subroutine get_field_by_code: called CC=2 SCAR=6.9
889 603 my ($self, $map_type, $code) = @_;
890  
891 return $types{$types_reverse{$map_type}{$code}}
892 603 ▶ if exists $types_reverse{$map_type}{$code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $types_reverse{$map_type}{$code} 602 1 603
893  
894 1 return $self->new(
895 name => 'UNKNOWN',
896 map_type => $map_type,
897 code => $code,
898 key_data => 'Bytestring value',
899 value_data => 'Bytestring value',
900 version_status => {
901 0 => AVAILABLE,
902 2 => AVAILABLE,
903 },
904 );
905 }
906  
907 1 signature_for get_field_by_name => (
908 method => !!1,
909 positional => [Str],
910 );
911  
912 sub get_field_by_name
913 3586 ▶{
Subroutine get_field_by_name: called CC=2 SCAR=7.1
914 3586 my ($self, $name) = @_;
915  
916 Bitcoin::Crypto::Exception::PSBT->raise(
917 "unknown field type $name"
918 3586 ▶ ) unless exists $types{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $types{$name} 0 3586 3586
919  
920 3586 return $types{$name};
921 }
922  
923 1 signature_for get_fields_available_in_version => (
924 method => !!1,
925 positional => [PositiveOrZeroInt],
926 );
927  
928 sub get_fields_available_in_version
929 {
930 my ($class, $version) = @_;
931  
932 # sort to ensure deterministic order
933 return [
934 grep {
935 $_->available_in_version($version)
936 } map {
937 $types{$_}
938 } sort keys %types
939 ];
940 }
941  
942 1 signature_for get_fields_required_in_version => (
943 method => !!1,
944 positional => [PositiveOrZeroInt],
945 );
946  
947 sub get_fields_required_in_version
948 148 ▶{
Subroutine get_fields_required_in_version: called CC=1 SCAR=0.0
949 151 my ($class, $version) = @_;
950  
951 # sort to have a deterministic error message
952 return [
953 grep {
954 6515 $_->required_in_version($version)
955 } map {
956 151 $types{$_}
957 } sort keys %types
958 ];
959 }
960  
961 1 signature_for available_in_version => (
962 method => !!1,
963 positional => [PositiveOrZeroInt],
964 );
965  
966 sub available_in_version
967 1353 ▶{
Subroutine available_in_version: called CC=1 SCAR=0.0
968 1356 my ($self, $version) = @_;
969  
970 1356 return defined $self->version_status->{$version};
971 }
972  
973 1 signature_for required_in_version => (
974 method => !!1,
975 positional => [PositiveOrZeroInt],
976 );
977  
978 sub required_in_version
979 6512 ▶{
Subroutine required_in_version: called CC=2 SCAR=6.9
980 6515 my ($self, $version) = @_;
981  
982 6515 ▶ return ($self->version_status->{$version} // '') eq REQUIRED;
Condition100%2/2
$self->version_status->{$version} // ""
l!l
5759753
MC/DC100%1/1
$self->version_status->{$version} // ""
$self->version_status->{$version}
Truth table100%2/2
A
A= $self->version_status->{$version}
Aresult
11
00
983 }
984  
985 1 1;
986  
987 __END__
« Crypto/Transaction/Signer/Legacy.pm ↑ Summary Crypto/Role/SignVerify.pm »