Crypto/PSBT/FieldType.pm

filter: Statementstmt 93.5% 259 / 277 Branchbran 86.8% 33 / 38 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 97.3% 73 / 75 Podpod n/a totaltotal 92.3% 375 / 406 CC 24 SCAR 6.3
CC
24
Cov
92%
__ANON__
6.0
__ANON__
5.0
__ANON__
4.0
CRAP 1.9
« Crypto/Role/SignVerify.pm ↑ Summary Crypto/Transaction/Digest/Config.pm »
1 package Bitcoin::Crypto::PSBT::FieldType;
2 1 $Bitcoin::Crypto::PSBT::FieldType::VERSION = '4.004';
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 use constant {
21 9 REQUIRED => 'required',
22 AVAILABLE => 'available',
23 9 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
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 lazy => sub {
36 396 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
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 default => sub {
46 sub {
47 11 ▶ state $sig = signature(positional => [ByteStr]);
Subroutine __ANON__: called CC=1 SCAR=0.0
48 11 my $value = ($sig->(@_))[0];
49 11 return $value;
50 }
51 118 ▶ },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 );
53  
54 has param 'deserializer' => (
55 isa => CodeRef,
56 default => sub {
57 59 ▶ sub { $_[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
58 118 ▶ },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 );
60  
61 has param 'key_serializer' => (
62 isa => CodeRef,
63 default => sub {
64 sub {
65 1 ▶ state $sig = signature(positional => [ByteStr]);
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 my $value = ($sig->(@_))[0];
67 1 return $value;
68 }
69 298 ▶ },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 1 );
71  
72 has param 'key_deserializer' => (
73 isa => CodeRef,
74 default => sub {
75 240 ▶ sub { $_[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
76 298 ▶ },
Subroutine __ANON__: called CC=1 SCAR=0.0
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 serializer => sub {
100 34 ▶ state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Transaction']]);
Subroutine __ANON__: called CC=1 SCAR=0.0
101 34 my $value = ($sig->(@_))[0];
102 34 return $value->to_serialized;
103 },
104 411 ▶ 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 serializer => sub {
110 62 ▶ state $sig = signature(positional => [IntMaxBits [32]]);
Subroutine __ANON__: called CC=1 SCAR=0.0
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 serializer => sub {
120 32 ▶ state $sig = signature(positional => [PositiveOrZeroInt]);
Subroutine __ANON__: called CC=1 SCAR=0.0
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 serializer => sub {
131 62 ▶ state $sig = signature(positional => [ByteStr, DerivationPath]);
Subroutine __ANON__: called CC=1 SCAR=0.0
132 62 my ($fingerprint, $path) = $sig->(@{$_[0]});
133  
134 62 return $fingerprint . pack 'V*', @{$path->path};
135 },
136 deserializer => sub {
137 228 ▶ my $val = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
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 serializer => sub {
149 37 ▶ state $sig = signature(positional => [BitcoinScript]);
Subroutine __ANON__: called CC=1 SCAR=0.0
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 two bytestring items",
158 key_serializer => sub {
159 0 ▶ state $sig = signature(positional => [Tuple [ByteStr, ByteStr]]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 my ($ident, $subkey) = @{($sig->(@_))[0]};
161  
162 0 my $result = '';
163 0 $result .= pack_compactsize(length $ident);
164 0 $result .= $ident;
165 0 $result .= pack_compactsize(length $subkey);
166 0 $result .= $subkey;
167  
168 0 return $result;
169 },
170 key_deserializer => sub {
171 0 ▶ my $val = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 my $pos = 0;
173  
174 0 my $ident_len = unpack_compactsize($val, \$pos);
175 0 my $ident = substr $val, $pos, $ident_len;
176 0 $pos += $ident_len;
177  
178 0 my $subkey_len = unpack_compactsize($val, \$pos);
179 0 my $subkey = substr $val, $pos, $subkey_len;
180 0 $pos += $subkey_len;
181  
182 0 return [$ident, $subkey];
183 },
184 1 );
185  
186 my %public_key_serializers = (
187 key_data => "Bitcoin::Crypto::Key::Public object",
188 key_serializer => sub {
189 43 ▶ state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Key::Public']]);
Subroutine __ANON__: called CC=1 SCAR=0.0
190 43 my $value = ($sig->(@_))[0];
191 43 return $value->to_serialized;
192 },
193 key_deserializer => sub {
194 167 ▶ return btc_pub->from_serialized(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
195 },
196 1 );
197  
198 my %taproot_public_key_serializers = (
199 key_data => "Bitcoin::Crypto::Key::Public object",
200 key_serializer => sub {
201 32 ▶ state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Key::Public']]);
Subroutine __ANON__: called CC=1 SCAR=0.0
202 32 my $value = ($sig->(@_))[0];
203 32 return $value->get_xonly_key;
204 },
205 key_deserializer => sub {
206 112 ▶ my $pubkey = btc_pub->from_serialized(lift_x shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
207 108 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 serializer => sub {
215 19 ▶ state $sig = signature(positional => [ArrayRef [ByteStr], ArrayRef, {slurpy => !!1}]);
Subroutine __ANON__: called CC=1 SCAR=0.0
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 deserializer => sub {
222 64 ▶ my $val = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
223  
224 64 my $pos = 0;
225 64 my $hash_count = unpack_compactsize($val, \$pos);
226 64 my @hashes = unpack "(a32)$hash_count", substr $val, $pos;
227 64 $pos += $hash_count * 32;
228  
229 64 my $arr = $fingerprint_and_path_serializers{deserializer}->(substr $val, $pos);
230 64 unshift @$arr, \@hashes;
231 64 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 validator => sub {
245 73 ▶ my ($tx) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
246  
247 die_no_trace 'must not have signatures'
248 73 ▶ 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 72 73
249  
250 die_no_trace 'must be in non-witness format'
251 72 ▶ 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 71 72
Condition66%2/3
$tx->had_witness_flag or &any(sub { $_->has_witness; } , @{$tx->inputs;})
l!l&&r!l&&!r
1071
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 key_serializer => sub {
262 3 ▶ state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Key::ExtPublic']]);
Subroutine __ANON__: called CC=1 SCAR=0.0
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 serializer => sub {
310 14 ▶ state $sig = signature(positional => [HashRef]);
Subroutine __ANON__: called CC=5 SCAR=16.1
311 14 my $hash = ($sig->(@_))[0];
312  
313 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
314 14 ▶ $raw |= 0x01 if $hash->{inputs_modifiable};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"inputs_modifiable"} 9 5 14
315 14 ▶ $raw |= 0x02 if $hash->{outputs_modifiable};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"outputs_modifiable"} 9 5 14
316 14 ▶ $raw |= 0x04 if $hash->{has_sighash_single};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"has_sighash_single"} 8 6 14
317  
318 14 return pack 'C', $raw;
319 },
320 deserializer => sub {
321 42 ▶ my $raw = unpack 'C', shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
322  
323 return {
324 42 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 serializer => sub {
369 26 ▶ state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Transaction::Output']]);
Subroutine __ANON__: called CC=1 SCAR=0.0
370 26 my $value = ($sig->(@_))[0];
371 26 return $value->to_serialized;
372 },
373 119 ▶ 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 => "Bytestring value",
439 version_status => {
440 0 => AVAILABLE,
441 2 => AVAILABLE,
442 },
443 },
444  
445 PSBT_IN_POR_COMMITMENT => {
446 code => 0x09,
447 value_data => "Bytestring value",
448 version_status => {
449 0 => AVAILABLE,
450 2 => AVAILABLE,
451 },
452 },
453  
454 PSBT_IN_RIPEMD160 => {
455 code => 0x0a,
456 key_data => "Bytestring value",
457 value_data => "Bytestring value",
458 version_status => {
459 0 => AVAILABLE,
460 2 => AVAILABLE,
461 },
462 },
463  
464 PSBT_IN_SHA256 => {
465 code => 0x0b,
466 key_data => "Bytestring value",
467 value_data => "Bytestring value",
468 version_status => {
469 0 => AVAILABLE,
470 2 => AVAILABLE,
471 },
472 },
473  
474 PSBT_IN_HASH160 => {
475 code => 0x0c,
476 key_data => "Bytestring value",
477 value_data => "Bytestring value",
478 version_status => {
479 0 => AVAILABLE,
480 2 => AVAILABLE,
481 },
482 },
483  
484 PSBT_IN_HASH256 => {
485 code => 0x0d,
486 key_data => "Bytestring value",
487 value_data => "Bytestring value",
488 version_status => {
489 0 => AVAILABLE,
490 2 => AVAILABLE,
491 },
492 },
493  
494 # NOTE: as usual, txids are represented in different byte order when serialized
495 PSBT_IN_PREVIOUS_TXID => {
496 code => 0x0e,
497 value_data => "Bytestring value",
498 serializer => sub {
499 16 ▶ state $sig = signature(positional => [ByteStr]);
Subroutine __ANON__: called CC=1 SCAR=0.0
500 16 my $value = ($sig->(@_))[0];
501 16 return scalar reverse $value;
502 },
503 55 ▶ deserializer => sub { scalar reverse shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
504 version_status => {
505 2 => REQUIRED,
506 },
507 },
508  
509 PSBT_IN_OUTPUT_INDEX => {
510 code => 0x0f,
511 %uint_32bitLE_serializers,
512 version_status => {
513 2 => REQUIRED,
514 },
515 },
516  
517 PSBT_IN_SEQUENCE => {
518 code => 0x10,
519 %uint_32bitLE_serializers,
520 version_status => {
521 2 => AVAILABLE,
522 },
523 },
524  
525 PSBT_IN_REQUIRED_TIME_LOCKTIME => {
526 code => 0x11,
527 %uint_32bitLE_serializers,
528 validator => sub {
529 6 ▶ my ($value) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
530 6 ▶ die_no_trace 'must be greather than or equal to '
Branch: 1 branch
BranchTrueFalseTotal
if $value < 500000000 1 5 6
531 . LOCKTIME_HEIGHT_THRESHOLD
532 if $value < LOCKTIME_HEIGHT_THRESHOLD;
533 },
534 version_status => {
535 2 => AVAILABLE,
536 },
537 },
538  
539 PSBT_IN_REQUIRED_HEIGHT_LOCKTIME => {
540 code => 0x12,
541 %uint_32bitLE_serializers,
542 validator => sub {
543 6 ▶ my ($value) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
544 6 ▶ die_no_trace 'must be less than ' . LOCKTIME_HEIGHT_THRESHOLD
Branch: 1 branch
BranchTrueFalseTotal
unless $value < 500000000 1 5 6
545 unless $value < LOCKTIME_HEIGHT_THRESHOLD;
546 },
547 version_status => {
548 2 => AVAILABLE,
549 },
550 },
551  
552 PSBT_IN_TAP_KEY_SIG => {
553 code => 0x13,
554 value_data => "Bytestring value",
555 validator => sub {
556 4 ▶ my ($value) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
557 4 state $validator = ByteStrLen [64] | ByteStrLen [65];
558 4 ▶ die_no_trace 'invalid signature length'
Branch: 1 branch
BranchTrueFalseTotal
unless $validator->check($value) 2 2 4
559 unless $validator->check($value);
560 },
561 version_status => {
562 0 => AVAILABLE,
563 2 => AVAILABLE,
564 },
565 },
566  
567 PSBT_IN_TAP_SCRIPT_SIG => {
568 code => 0x14,
569 key_data =>
570 "Array reference, where the first item is Bitcoin::Crypto::Key::Public and second element is a leaf hash bytestring",
571 key_serializer => sub {
572 3 ▶ state $sig = signature(positional => [Any, ByteStr]);
Subroutine __ANON__: called CC=1 SCAR=0.0
573 3 my ($pubkey, $leaf_hash) = $sig->(@{$_[0]});
574  
575 3 return $taproot_public_key_serializers{key_serializer}->($pubkey) . $leaf_hash;
576 },
577 key_deserializer => sub {
578 18 ▶ my $val = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
579 18 ▶ die_no_trace 'invalid length'
Branch: 1 branch
BranchTrueFalseTotal
unless length $val == 64 1 17 18
580 unless length $val == 64;
581  
582 17 my $leaf_hash = substr $val, 32, 32;
583  
584 return [
585 17 $taproot_public_key_serializers{key_deserializer}->(substr $val, 0, 32),
586 $leaf_hash,
587 ];
588 },
589 value_data => "Bytestring value",
590 validator => sub {
591 8 ▶ my ($key, $value) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
592 8 state $validator = ByteStrLen [64] | ByteStrLen [65];
593 8 ▶ die_no_trace 'invalid signature length'
Branch: 1 branch
BranchTrueFalseTotal
unless $validator->check($value) 2 6 8
594 unless $validator->check($value);
595 },
596 version_status => {
597 0 => AVAILABLE,
598 2 => AVAILABLE,
599 },
600 },
601  
602 PSBT_IN_TAP_LEAF_SCRIPT => {
603 code => 0x15,
604 key_data => "Instance of Bitcoin::Crypto::Transaction::ControlBlock",
605 key_serializer => sub {
606 6 ▶ state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Transaction::ControlBlock']]);
Subroutine __ANON__: called CC=1 SCAR=0.0
607 6 my $block = ($sig->(@_))[0];
608  
609 6 return $block->to_serialized;
610 },
611 key_deserializer => sub {
612 23 ▶ my $val = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
613  
614 23 return Bitcoin::Crypto::Transaction::ControlBlock->from_serialized($val);
615 },
616 value_data =>
617 "Array reference, where the first item is Bitcoin::Crypto::Script and second element is a leaf version number",
618 serializer => sub {
619 6 ▶ state $sig = signature(positional => [BitcoinScript, PositiveOrZeroInt]);
Subroutine __ANON__: called CC=1 SCAR=0.0
620 6 my ($script, $leaf_version) = $sig->(@{$_[0]});
621  
622 6 return $script->to_serialized . pack 'C', $leaf_version;
623 },
624 deserializer => sub {
625 23 ▶ my $val = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
626  
627 23 my $leaf_version = unpack 'C', substr $val, -1, 1, '';
628 23 my $script_raw = $val;
629  
630 # NOTE: we assume here that future leaf versions will also use tapscript
631 return [
632 23 btc_tapscript->from_serialized($script_raw),
633 $leaf_version,
634 ];
635 },
636 version_status => {
637 0 => AVAILABLE,
638 2 => AVAILABLE,
639 },
640 },
641  
642 PSBT_IN_TAP_BIP32_DERIVATION => {
643 code => 0x16,
644 %taproot_fingerprint_and_path_serializers,
645 %taproot_public_key_serializers,
646 version_status => {
647 0 => AVAILABLE,
648 2 => AVAILABLE,
649 },
650 },
651  
652 PSBT_IN_TAP_INTERNAL_KEY => {
653 code => 0x17,
654 value_data => $taproot_public_key_serializers{key_data},
655 serializer => $taproot_public_key_serializers{key_serializer},
656 deserializer => $taproot_public_key_serializers{key_deserializer},
657 version_status => {
658 0 => AVAILABLE,
659 2 => AVAILABLE,
660 },
661 },
662  
663 PSBT_IN_TAP_MERKLE_ROOT => {
664 code => 0x18,
665 value_data => "Bytestring value",
666 validator => sub {
667 4 ▶ my ($value) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
668 4 state $validator = ByteStrLen [32];
669 4 ▶ die_no_trace 'invalid merkle root length'
Branch: 1 branch
BranchTrueFalseTotal
unless $validator->check($value) 0 4 4
670 unless $validator->check($value);
671 },
672 version_status => {
673 0 => AVAILABLE,
674 2 => AVAILABLE,
675 },
676 },
677  
678 PSBT_IN_PROPRIETARY => {
679 code => 0xfc,
680 value_data => "Bytestring value",
681 %proprietary_key_serializers,
682 version_status => {
683 0 => AVAILABLE,
684 2 => AVAILABLE,
685 },
686 },
687  
688 # OUTPUT
689 PSBT_OUT_REDEEM_SCRIPT => {
690 code => 0x00,
691 %script_serializers,
692 version_status => {
693 0 => AVAILABLE,
694 2 => AVAILABLE,
695 },
696 },
697  
698 PSBT_OUT_WITNESS_SCRIPT => {
699 code => 0x01,
700 %script_serializers,
701 version_status => {
702 0 => AVAILABLE,
703 2 => AVAILABLE,
704 },
705 },
706  
707 PSBT_OUT_BIP32_DERIVATION => {
708 code => 0x02,
709 %public_key_serializers,
710 %fingerprint_and_path_serializers,
711 version_status => {
712 0 => AVAILABLE,
713 2 => AVAILABLE,
714 },
715 },
716  
717 PSBT_OUT_AMOUNT => {
718 code => 0x03,
719 value_data => "64 bit number",
720 serializer => sub {
721 31 ▶ state $sig = signature(positional => [SatoshiAmount]);
Subroutine __ANON__: called CC=1 SCAR=0.0
722 31 my $value = ($sig->(@_))[0];
723 31 return encode_64bit($value);
724 },
725 100 ▶ deserializer => sub { decode_64bit(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
726 version_status => {
727 2 => REQUIRED,
728 },
729 },
730  
731 PSBT_OUT_SCRIPT => {
732 code => 0x04,
733 %script_serializers,
734 version_status => {
735 2 => REQUIRED,
736 },
737 },
738  
739 PSBT_OUT_TAP_INTERNAL_KEY => {
740 code => 0x05,
741 value_data => $taproot_public_key_serializers{key_data},
742 serializer => $taproot_public_key_serializers{key_serializer},
743 deserializer => $taproot_public_key_serializers{key_deserializer},
744 version_status => {
745 0 => AVAILABLE,
746 2 => AVAILABLE,
747 },
748 },
749  
750 PSBT_OUT_TAP_TREE => {
751 code => 0x06,
752 value_data => "Bitcoin::Crypto::Script::Tree instance",
753 serializer => sub {
754 1 ▶ state $sig = signature(positional => [InstanceOf ['Bitcoin::Crypto::Script::Tree']]);
Subroutine __ANON__: called CC=1 SCAR=0.0
755 1 my $tree = ($sig->(@_))[0];
756  
757 1 my @list;
758 my $action = sub {
759 3 ▶ my ($value, $depth) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
760  
761 die_no_trace 'tree must have all its leaves unhashed'
762 3 ▶ unless defined $value->{script};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value->{"script"} 0 3 3
763  
764 3 my $serialized = $value->{script}->to_serialized;
765  
766 push @list, pack('C', $depth)
767 . pack('C', $value->{leaf_version})
768 3 . pack_compactsize(length $serialized)
769 . $serialized;
770 1 };
771  
772 1 $tree->_traverse(undef, $action);
773  
774 1 return join '', @list;
775  
776 },
777 deserializer => sub {
778 4 ▶ my $value = shift;
Subroutine __ANON__: called CC=6 SCAR=17.9
779 4 my $pos = 0;
780  
781 4 my $tree = [];
782 4 my @stack = ($tree);
783  
784 4 ▶ while ($pos < length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $value) 12 4 16
785 12 my $depth = unpack 'C', substr $value, $pos, 1;
786 12 $pos += 1;
787  
788 12 my $leaf_version = unpack 'C', substr $value, $pos, 1;
789 12 $pos += 1;
790  
791 12 my $script_len = unpack_compactsize($value, \$pos);
792 12 my $script_raw = substr $value, $pos, $script_len;
793 12 $pos += $script_len;
794  
795 # NOTE: we assume here that future leaf versions will also use tapscript
796 12 my $leaf = {
797 leaf_version => $leaf_version,
798 script => btc_tapscript->from_serialized($script_raw),
799 };
800  
801 12 ▶ while ($#stack > $depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#stack > $depth) 4 12 16
802 4 pop @stack;
803 }
804  
805 12 ▶ while ($depth > $#stack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > $#stack) 8 12 20
806 8 my $new_branch = [];
807 8 push @{$stack[-1]}, $new_branch;
808 8 push @stack, $new_branch;
809 }
810  
811 12 push @{$stack[-1]}, $leaf;
812 }
813  
814 # make sure we handle depth 0 without an extra level
815 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
816 4 $tree = $tree->[0];
817 }
818  
819 4 $tree = btc_script_tree->new(tree => $tree);
820  
821 # validate tree by getting merkle root (will traverse)
822 Bitcoin::Crypto::Exception::PSBT->trap_into(
823 sub {
824 4 ▶ $tree->get_merkle_root;
Subroutine __ANON__: called CC=1 SCAR=0.0
825 },
826 4 'invalid tree'
827 );
828  
829 4 return $tree;
830 },
831 1 version_status => {
832 0 => AVAILABLE,
833 2 => AVAILABLE,
834 },
835 },
836  
837 PSBT_OUT_TAP_BIP32_DERIVATION => {
838 code => 0x07,
839 %taproot_fingerprint_and_path_serializers,
840 %taproot_public_key_serializers,
841 version_status => {
842 0 => AVAILABLE,
843 2 => AVAILABLE,
844 },
845 },
846  
847 PSBT_OUT_PROPRIETARY => {
848 code => 0xfc,
849 value_data => "Bytestring value",
850 %proprietary_key_serializers,
851 version_status => {
852 0 => AVAILABLE,
853 2 => AVAILABLE,
854 },
855 },
856 );
857  
858 1 %types = map { $_, __PACKAGE__->new(name => $_, %{$types{$_}}) } keys %types;
859 1 my %types_reverse;
860 1 foreach my $type (values %types) {
861 44 $types_reverse{$type->map_type}{$type->code} = $type->name;
862 }
863  
864 1 signature_for get_field_by_code => (
865 method => !!1,
866 positional => [PSBTMapType, PositiveOrZeroInt],
867 );
868  
869 sub get_field_by_code
870 {
871 8 ▶ my ($self, $map_type, $code) = @_;
Subroutine get_field_by_code: called CC=2 SCAR=6.9
872  
873 return $types{$types_reverse{$map_type}{$code}}
874 8 ▶ if exists $types_reverse{$map_type}{$code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $types_reverse{$map_type}{$code} 586 1 587
875  
876 8 return $self->new(
877 name => 'UNKNOWN',
878 map_type => $map_type,
879 code => $code,
880 key_data => 'Bytestring value',
881 value_data => 'Bytestring value',
882 version_status => {
883 0 => AVAILABLE,
884 2 => AVAILABLE,
885 },
886 );
887 }
888  
889 1 signature_for get_field_by_name => (
890 method => !!1,
891 positional => [Str],
892 );
893  
894 sub get_field_by_name
895 {
896 934 ▶ my ($self, $name) = @_;
Subroutine get_field_by_name: called CC=2 SCAR=7.1
897  
898 Bitcoin::Crypto::Exception::PSBT->raise(
899 "unknown field type $name"
900 934 ▶ ) unless exists $types{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $types{$name} 0 934 934
901  
902 934 return $types{$name};
903 }
904  
905 1 signature_for get_fields_required_in_version => (
906 method => !!1,
907 positional => [PositiveOrZeroInt],
908 );
909  
910 sub get_fields_required_in_version
911 {
912 81 ▶ my ($class, $version) = @_;
Subroutine get_fields_required_in_version: called CC=1 SCAR=0.0
913  
914 # sort to have a deterministic error message
915 return [
916 grep {
917 3478 $_->required_in_version($version)
918 } map {
919 81 $types{$_}
920 } sort keys %types
921 ];
922 }
923  
924 1 signature_for available_in_version => (
925 method => !!1,
926 positional => [PositiveOrZeroInt],
927 );
928  
929 sub available_in_version
930 {
931 688 ▶ my ($self, $version) = @_;
Subroutine available_in_version: called CC=1 SCAR=0.0
932  
933 688 return defined $self->version_status->{$version};
934 }
935  
936 1 signature_for required_in_version => (
937 method => !!1,
938 positional => [PositiveOrZeroInt],
939 );
940  
941 sub required_in_version
942 {
943 3478 ▶ my ($self, $version) = @_;
Subroutine required_in_version: called CC=2 SCAR=6.9
944  
945 3478 ▶ return ($self->version_status->{$version} // '') eq REQUIRED;
Condition100%2/2
$self->version_status->{$version} // ""
l!l
2902574
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
946 }
947  
948 1 1;
949  
950 __END__
« Crypto/Role/SignVerify.pm ↑ Summary Crypto/Transaction/Digest/Config.pm »