Crypto/PSBT/FieldType.pm

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