Crypto/Script.pm

filter: Statementstmt 98.9% 194 / 196 Branchbran 82.2% 51 / 62 Conditioncond 92.3% 12 / 13 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 55 / 55 Podpod 77.7% 14 / 18 totaltotal 94.6% 333 / 352 CC 40 SCAR 12.5
CC
40
Cov
95%
push_bytes
10.3
get_address
8.0
__ANON__
6.0
CRAP 3.5
« Crypto/DerivationPath.pm ↑ Summary Crypto/Key/Private.pm »
1 package Bitcoin::Crypto::Script;
2 68 $Bitcoin::Crypto::Script::VERSION = '4.006';
3 68 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 68 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 68 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 68 ▶use Crypt::Digest::SHA256 qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 68 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 68 ▶use Bitcoin::Crypto::Base58 qw(encode_base58check decode_base58check);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 68 ▶use Bitcoin::Crypto::Bech32 qw(encode_segwit decode_segwit get_hrp);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 68 ▶use Bitcoin::Crypto::Constants qw(:witness :script);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 68 ▶use Bitcoin::Crypto::Util::Internal qw(hash160 get_address_type to_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 68 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 68 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 68 ▶use Bitcoin::Crypto::Script::Opcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 68 ▶use Bitcoin::Crypto::Script::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 68 ▶use Bitcoin::Crypto::Script::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 68 ▶use Bitcoin::Crypto::Script::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 68 ▶use Bitcoin::Crypto::Script::Recognition;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 68 has field '_serialized' => (
25 isa => ByteStr,
26 writer => 1,
27 default => '',
28 );
29  
30 68 has field '_recognition' => (
31 isa => InstanceOf ['Bitcoin::Crypto::Script::Recognition'],
32 lazy => 1,
33 clearer => -hidden,
34 handles => {
35 get_raw_address => 'public_key',
36 type => 'type',
37 segwit_version => 'segwit_version',
38 required_sigs => 'required_sigs',
39 },
40 );
41  
42 68 has field '_compiler' => (
43 isa => InstanceOf ['Bitcoin::Crypto::Script::Compiler'],
44 lazy => 1,
45 clearer => -hidden,
46 handles => {
47 operations => 'operations',
48 has_errors => 'has_errors',
49 assert_valid => 'assert_valid',
50 },
51 );
52  
53 68 with qw(Bitcoin::Crypto::Role::Network);
54  
55 sub _build_recognition
56 475 ▶{
Subroutine _build_recognition: called CC=1 SCAR=0.0
57 475 return Bitcoin::Crypto::Script::Recognition->check(shift);
58 }
59  
60 sub _build_compiler
61 969 ▶{
Subroutine _build_compiler: called CC=1 SCAR=0.0
62 969 return Bitcoin::Crypto::Script::Compiler->compile(shift);
63 }
64  
65 sub _build
66 204 ▶{
Subroutine _build: called CC=2 SCAR=6.9
67 204 my ($self, $type, $address) = @_;
68  
69 204 state $types = do {
70 58 ▶ my $legacy = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
71 58 my ($self, $address, $type) = @_;
72  
73 58 my $decoded = decode_base58check($address);
74 58 my $network_byte = substr $decoded, 0, 1, '';
75  
76 58 ▶ Bitcoin::Crypto::Exception::Address->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless length $decoded == 20 0 58 58
77 "legacy scripts should contain 20 bytes"
78 ) unless length $decoded == 20;
79  
80 58 my $byte_method = lc "p2${type}_byte";
81 58 ▶ Bitcoin::Crypto::Exception::NetworkCheck->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $network_byte ne $self->network->$byte_method 2 56 58
82 "provided address $address is not P2$type on network " . $self->network->name
83 ) if $network_byte ne $self->network->$byte_method;
84  
85 56 Bitcoin::Crypto::Script::Common->fill($type => $self, $decoded);
86 29 };
87  
88 97 ▶ my $witness = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
89 97 my ($self, $address, $name, $version, $length) = @_;
90  
91 97 my $data = decode_segwit $address;
92 97 my $this_version = substr $data, 0, 1, '';
93  
94 97 ▶ Bitcoin::Crypto::Exception::SegwitProgram->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $this_version eq chr $version 1 96 97
95 "$name script only handles witness version $version"
96 ) unless $this_version eq chr $version;
97  
98 96 ▶ Bitcoin::Crypto::Exception::SegwitProgram->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless length $data eq $length 2 94 96
99 "$name script should contain $length bytes"
100 ) unless length $data eq $length;
101  
102 94 ▶ Bitcoin::Crypto::Exception::NetworkCheck->raise(
Branch: 1 branch
BranchTrueFalseTotal
if get_hrp($address) ne $self->network->segwit_hrp 2 92 94
103 "provided address $address does not belong to network " . $self->network->name
104 ) if get_hrp($address) ne $self->network->segwit_hrp;
105  
106 92 $self
107 ->add("OP_$version")
108 ->push($data);
109 29 };
110  
111 {
112 10 ▶ P2PK => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 10 my ($self, $pubkey) = @_;
114  
115 10 $self
116 ->push($pubkey)
117 ->add('OP_CHECKSIG');
118 },
119  
120 30 ▶ P2PKH => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 30 $legacy->(@_, 'PKH');
122 },
123  
124 28 ▶ P2SH => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 28 $legacy->(@_, 'SH');
126 },
127  
128 13 ▶ P2MS => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
129 13 my ($self, $data) = @_;
130  
131 13 ▶ Bitcoin::Crypto::Exception::ScriptPush->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 0 13 13
132 'P2MS script argument must be an array reference'
133 ) unless ref $data eq 'ARRAY';
134  
135 13 my ($signatures_num, @pubkeys) = @$data;
136  
137 13 ▶ Bitcoin::Crypto::Exception::ScriptPush->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @pubkeys <= 20 1 12 13
138 'P2MS script pubkey count should not exceed ' . SCRIPT_MAX_MULTISIG_PUBKEYS
139 ) unless @pubkeys <= SCRIPT_MAX_MULTISIG_PUBKEYS;
140  
141 12 ▶ Bitcoin::Crypto::Exception::ScriptPush->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $signatures_num >= 0 and $signatures_num <= @pubkeys 2 10 12
Condition100%3/3
$signatures_num >= 0 and $signatures_num <= @pubkeys
!ll&&!rl&&r
1110
MC/DC100%2/2
$signatures_num >= 0 and $signatures_num <= @pubkeys
$signatures_num >= 0 $signatures_num <= @pubkeys
Truth table100%3/3
A and B
A= $signatures_num >= 0B= $signatures_num <= @pubkeys
ABresult
0X0
100
111
142 'P2MS script number of signatures should not be negative or exceed the number of pubkeys'
143 ) unless $signatures_num >= 0 && $signatures_num <= @pubkeys;
144  
145 10 $self->push(chr $signatures_num);
146  
147 10 foreach my $pubkey (@pubkeys) {
148 39 $self->push($pubkey);
149 }
150  
151 $self
152 10 ->push(chr scalar @pubkeys)
153 ->add('OP_CHECKMULTISIG');
154 },
155  
156 32 ▶ P2WPKH => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 32 $witness->(@_, 'P2WPKH', 0, 20);
158 },
159  
160 10 ▶ P2WSH => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 10 $witness->(@_, 'P2WSH', 0, 32);
162 },
163  
164 55 ▶ P2TR => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
165 55 $witness->(@_, 'P2TR', 1, 32);
166 },
167  
168 26 ▶ NULLDATA => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
169 26 my ($self, $data) = @_;
170  
171 26 $self
172 ->add('OP_RETURN')
173 ->push($data);
174 },
175 29 };
176 };
177  
178 Bitcoin::Crypto::Exception::ScriptType->raise(
179 "unknown standard script type $type"
180 204 ▶ ) if !$types->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $types->{$type} 0 204 204
181  
182 204 $types->{$type}->($self, $address);
183 194 return;
184 }
185  
186 sub opcode_class
187 2062 ▶{
Subroutine opcode_class: called CC=1 SCAR=0.0
Pod documented
188 2062 return 'Bitcoin::Crypto::Script::Opcode';
189 }
190  
191 sub BUILD
192 3544 ▶{
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
193 3544 my ($self, $args) = @_;
194  
195 3544 ▶ if ($args->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"type"}) 204 3340 3544
196 Bitcoin::Crypto::Exception::ScriptPush->raise(
197 'script with a "type" also requires an "address"'
198 204 ▶ ) unless defined $args->{address};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"address"} 0 204 204
199  
200 204 $self->_build($args->{type}, $args->{address});
201 }
202 }
203  
204 sub is_pushes_only
205 30 ▶{
Subroutine is_pushes_only: called CC=3 SCAR=11.0
Pod documented
206 30 my ($self) = @_;
207  
208 30 foreach my $op (@{$self->operations}) {
209 54 ▶ return !!0 if $op->[0]->non_push_opcode;
Branch: 1 branch
BranchTrueFalseTotal
if $op->[0]->non_push_opcode 1 53 54
210 }
211  
212 29 return !!1;
213 }
214  
215 # same as add_raw, but does not clear the object - to avoid clearing it
216 # multiple times per operation
217 sub _add_raw
218 2182 ▶{
Subroutine _add_raw: called CC=1 SCAR=0.0
219 2182 my ($self, $bytes) = @_;
220  
221 2182 $self->_set_serialized($self->_serialized . $bytes);
222 }
223  
224 68 signature_for add_raw => (
225 method => !!1,
226 positional => [ByteStr],
227 );
228  
229 sub add_raw
230 2174 ▶{
Subroutine add_raw: called CC=1 SCAR=0.0
231 2174 my ($self, $bytes) = @_;
232  
233 2174 $self->_add_raw($bytes);
234 2174 $self->_clear_compiler;
235 2174 $self->_clear_recognition;
236 2174 return $self;
237 }
238  
239 68 signature_for add_operation => (
240 method => !!1,
241 positional => [Str],
242 );
243  
244 sub add_operation
245 1494 ▶{
Subroutine add_operation: called CC=1 SCAR=0.0
246 1494 my ($self, $name) = @_;
247  
248 1494 my $opcode = $self->opcode_class->get_opcode_by_name($name);
249 1494 $self->add_raw(chr $opcode->code);
250  
251 1494 return $self;
252 }
253  
254 sub add
255 1286 ▶{
Subroutine add: called CC=1 SCAR=0.0
Pod documented
256 1286 goto \&add_operation;
257 }
258  
259 68 signature_for push_bytes => (
260 method => !!1,
261 positional => [ByteStr],
262 );
263  
264 sub push_bytes
265 741 ▶{
Subroutine push_bytes: called CC=10 SCAR=23.3
266 741 my ($self, $bytes) = @_;
267  
268 741 my $len = length $bytes;
269  
270 741 ▶ if ($len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len == 0) { } 11 730 741
elsif ($len == 1) { } 67 663 730
271 11 return $self->add_operation('OP_0');
272 }
273 elsif ($len == 1) {
274 67 my $ord = ord($bytes);
275  
276 67 ▶ if ($ord <= 0x10 && $ord != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ord <= 16 and $ord != 0) { } 42 25 67
elsif ($ord == 129) { } 2 23 25
Condition100%3/3
$ord <= 16 and $ord != 0
!ll&&!rl&&r
22342
MC/DC100%2/2
$ord <= 16 and $ord != 0
$ord <= 16 $ord != 0
Truth table100%3/3
A and B
A= $ord <= 16B= $ord != 0
ABresult
0X0
100
111
277 42 return $self->add_operation("OP_$ord");
278 }
279 elsif ($ord == 0x81) {
280 2 return $self->add_operation('OP_1NEGATE');
281 }
282 }
283  
284 686 ▶ if ($len <= 75) {
Branch: 4 branches
BranchTrueFalseTotal
if ($len <= 75) { } 678 8 686
elsif ($len <= 255) { } 6 2 8
elsif ($len <= 65535) { } 2 0 2
elsif ($len <= 4294967295) { } 0 0 0
285  
286 # NOTE: add_raw (no underscore) to force clearing the object, since no
287 # other add_ is called
288 678 $self
289 ->add_raw(pack 'Ca*', $len, $bytes);
290 }
291 elsif ($len <= 0xff) {
292 6 $self
293 ->add_operation('OP_PUSHDATA1')
294 ->_add_raw(pack 'Ca*', $len, $bytes);
295 }
296 elsif ($len <= 0xffff) {
297 2 $self
298 ->add_operation('OP_PUSHDATA2')
299 ->_add_raw(pack 'va*', $len, $bytes);
300 }
301 elsif ($len <= 0xffffffff) {
302 0 $self
303 ->add_operation('OP_PUSHDATA4')
304 ->_add_raw(pack 'Va*', $len, $bytes);
305 }
306 else {
307 0 Bitcoin::Crypto::Exception::ScriptPush->raise(
308 'too much data to push onto stack in one operation'
309 );
310 }
311  
312 686 return $self;
313 }
314  
315 68 signature_for push_number => (
316 method => !!1,
317 positional => [Int | Str | InstanceOf ['Math::BigInt']],
318 );
319  
320 sub push_number
321 {
322 my ($self, $number) = @_;
323  
324 return $self->push_bytes(Bitcoin::Crypto::Script::Runner->from_int($number));
325 }
326  
327 sub push
328 558 ▶{
Subroutine push: called CC=1 SCAR=0.0
Pod documented
329 558 goto \&push_bytes;
330 }
331  
332 # this can only detect native segwit in this context, as P2SH outputs are
333 # indistinguishable from any other P2SH
334 sub is_native_segwit
335 1047 ▶{
Subroutine is_native_segwit: called CC=1 SCAR=0.0
Pod documented
336 1047 return defined shift->segwit_version;
337 }
338  
339 sub is_taproot
340 409 ▶{
Subroutine is_taproot: called CC=2 SCAR=6.9
Pod undocumented
341 409 ▶ return (shift->type // '') eq 'P2TR';
Condition100%2/2
(shift())->type // ""
l!l
36742
MC/DC100%1/1
(shift())->type // ""
(shift())->type
Truth table100%2/2
A
A= (shift())->type
Aresult
11
00
342 }
343  
344 sub get_hash
345 63 ▶{
Subroutine get_hash: called CC=1 SCAR=0.0
Pod documented
346 63 return hash160(shift->_serialized);
347 }
348  
349 sub get_witness_hash
350 40 ▶{
Subroutine get_witness_hash: called CC=1 SCAR=0.0
Pod documented
351 40 return sha256(shift->to_serialized);
352 }
353  
354 sub to_serialized
355 4219 ▶{
Subroutine to_serialized: called CC=1 SCAR=0.0
Pod documented
356 4219 goto \&_serialized;
357 }
358  
359 68 signature_for from_serialized => (
360 method => !!1,
361 positional => [ByteStr],
362 );
363  
364 sub from_serialized
365 2950 ▶{
Subroutine from_serialized: called CC=1 SCAR=0.0
366 2950 my ($class, $bytes) = @_;
367  
368 2950 my $self = $class->new;
369 2797 $self->_set_serialized($bytes);
370 2930 return $self;
371 }
372  
373 68 signature_for from_standard => (
374 method => !!1,
375 positional => [ScriptDesc, {slurpy => !!1}],
376 );
377  
378 sub from_standard
379 {
380 my ($class, $desc) = @_;
381  
382 ▶ if ($desc->[0] eq 'address') {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc->[0] eq "address") 71 133 204
383 $desc->[0] = get_address_type($desc->[1]);
384 }
385  
386 return $class->new(
387 type => $desc->[0],
388 address => $desc->[1],
389 );
390 }
391  
392 68 signature_for run => (
393 method => !!1,
394 positional => [ArrayRef [ByteStr], {default => []}],
395 );
396  
397 sub run
398 311 ▶{
Subroutine run: called CC=1 SCAR=0.0
399 311 my ($self, $initial_stack) = @_;
400  
401 311 my $runner = Bitcoin::Crypto::Script::Runner->new();
402 311 return $runner->execute($self, $initial_stack);
403 }
404  
405 sub witness_program
406 33 ▶{
Subroutine witness_program: called CC=1 SCAR=0.0
Pod undocumented
407 33 my ($self) = @_;
408  
409 33 my $program = Bitcoin::Crypto::Script->new(network => $self->network);
410 33 $program
411 ->add_operation('OP_' . SEGWIT_WITNESS_VERSION)
412 ->push_bytes($self->get_witness_hash);
413  
414 33 return $program;
415 }
416  
417 sub get_legacy_address
418 48 ▶{
Subroutine get_legacy_address: called CC=1 SCAR=0.0
Pod documented
419 48 my ($self) = @_;
420 48 return encode_base58check($self->network->p2sh_byte . $self->get_hash);
421 }
422  
423 sub get_compat_address
424 9 ▶{
Subroutine get_compat_address: called CC=2 SCAR=7.1
Pod documented
425 9 my ($self) = @_;
426  
427 # network field is not required, lazy check for completeness
428 9 ▶ Bitcoin::Crypto::Exception::NetworkConfig->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->network->supports_segwit 0 9 9
429 'this network does not support segregated witness'
430 ) unless $self->network->supports_segwit;
431  
432 9 return $self->witness_program->get_legacy_address;
433 }
434  
435 sub get_segwit_address
436 10 ▶{
Subroutine get_segwit_address: called CC=2 SCAR=7.1
Pod documented
437 10 my ($self) = @_;
438  
439 # network field is not required, lazy check for completeness
440 10 ▶ Bitcoin::Crypto::Exception::NetworkConfig->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->network->supports_segwit 0 10 10
441 'this network does not support segregated witness'
442 ) unless $self->network->supports_segwit;
443  
444 10 return encode_segwit($self->network->segwit_hrp, $self->witness_program->run->stack_serialized);
445 }
446  
447 sub get_address
448 35 ▶{
Subroutine get_address: called CC=8 SCAR=20.8
Pod documented
449 35 my ($self) = @_;
450 35 my $address = $self->get_raw_address;
451  
452 return undef
453 35 ▶ unless $self->has_type && defined $address;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_type and defined $address 10 25 35
Condition66%2/3
$self->has_type and defined $address
!ll&&!rl&&r
10025
MC/DC50%1/2
$self->has_type and defined $address
$self->has_type defined $address
Truth table66%2/3
A and B
A= $self->has_typeB= defined $address
ABresult
0X0
100
111
454  
455 25 ▶ if ($self->is_native_segwit) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->is_native_segwit) { } 13 12 25
elsif ($self->type eq 'P2PKH') { } 1 11 12
elsif ($self->type eq 'P2SH') { } 1 10 11
elsif ($self->type eq 'NULLDATA') { } 2 8 10
456  
457 # network field is not required, lazy check for completeness
458 13 ▶ Bitcoin::Crypto::Exception::NetworkConfig->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->network->supports_segwit 0 13 13
459 'this network does not support segregated witness'
460 ) unless $self->network->supports_segwit;
461  
462 13 my $version = pack 'C', $self->segwit_version;
463 13 return encode_segwit($self->network->segwit_hrp, $version . $address);
464 }
465 elsif ($self->type eq 'P2PKH') {
466 1 return encode_base58check($self->network->p2pkh_byte . $address);
467 }
468 elsif ($self->type eq 'P2SH') {
469 1 return encode_base58check($self->network->p2sh_byte . $address);
470 }
471 elsif ($self->type eq 'NULLDATA') {
472 2 return qq("$address");
473 }
474  
475 8 return undef;
476 }
477  
478 sub has_type
479 182 ▶{
Subroutine has_type: called CC=1 SCAR=0.0
Pod undocumented
480 182 my ($self) = @_;
481  
482 182 return defined $self->type;
483 }
484  
485 sub is_empty
486 154 ▶{
Subroutine is_empty: called CC=1 SCAR=0.0
Pod documented
487 154 my ($self) = @_;
488  
489 154 return length $self->_serialized == 0;
490 }
491  
492 sub dump
493 3 ▶{
Subroutine dump: called CC=5 SCAR=16.1
Pod documented
494 3 my ($self) = @_;
495  
496 3 my $ops = $self->operations;
497 3 my $num = @$ops;
498  
499 3 ▶ return 'Empty script' unless $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 0 3 3
500  
501 3 ▶ my $type = $self->type // 'Custom';
Condition100%2/2
$self->type // "Custom"
l!l
21
MC/DC100%1/1
$self->type // "Custom"
$self->type
Truth table100%2/2
A
A= $self->type
Aresult
11
00
502 3 ▶ my $errors = $self->has_errors ? ' (with errors)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->has_errors ? : 1 2 3
503  
504 3 my @result;
505 3 CORE::push @result, "$type script$errors, $num ops:";
506 3 foreach my $op (@$ops) {
507 5 CORE::push @result, ' ' . $op->opcode->name . ' (' . (to_format [hex => $op->raw_data]) . ')';
508 }
509  
510 3 return join "\n", @result;
511 }
512  
513 68 1;
514  
515 __END__
« Crypto/DerivationPath.pm ↑ Summary Crypto/Key/Private.pm »