Crypto/Transaction.pm

filter: Statementstmt 94.4% 374 / 396 Branchbran 69.4% 93 / 134 Conditioncond 65.9% 62 / 94 MC/DCmcdc 40.7% 22 / 54 Subroutinesub 96.7% 59 / 61 Podpod 94.4% 17 / 18 totaltotal 82.8% 627 / 757 CC 113 SCAR 25.1
CC
113
Cov
85%
_verify_coinbase
42.0
verify
38.8
_verify_script_default
13.0
CRAP 12.3
« Crypto/Script/Recognition.pm ↑ Summary Crypto/Helpers.pm »
1 package Bitcoin::Crypto::Transaction;
2 34 $Bitcoin::Crypto::Transaction::VERSION = '4.004';
3 34 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 34 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use List::Util qw(any uniqstr);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 34 ▶use Bitcoin::Crypto qw(btc_pub btc_script btc_tapscript btc_script_tree btc_utxo);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use Bitcoin::Crypto::Constants qw(:script :transaction :coin);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use Bitcoin::Crypto::Transaction::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 34 ▶use Bitcoin::Crypto::Transaction::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 34 ▶use Bitcoin::Crypto::Transaction::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 34 ▶use Bitcoin::Crypto::Util::Internal qw(pack_compactsize unpack_compactsize hash256 to_format lift_x has_even_y);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 34 ▶use Bitcoin::Crypto::Helpers qw(die_no_trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 34 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 34 ▶use Bitcoin::Crypto::Script::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 34 ▶use Bitcoin::Crypto::Script::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 34 ▶use Bitcoin::Crypto::Transaction::ControlBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 34 ▶use Bitcoin::Crypto::Transaction::Flags;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 34 ▶use Bitcoin::Crypto::Transaction::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 34 has param 'version' => (
29 isa => IntMaxBits [32],
30 default => 1,
31 );
32  
33 34 has option 'block' => (
34 isa => InstanceOf ['Bitcoin::Crypto::Block'],
35 weak_ref => 1,
36 writer => 1,
37 );
38  
39 has field 'inputs' => (
40 isa => ArrayRef [InstanceOf ['Bitcoin::Crypto::Transaction::Input']],
41 558 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 34 );
43  
44 has field 'outputs' => (
45 isa => ArrayRef [InstanceOf ['Bitcoin::Crypto::Transaction::Output']],
46 558 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 34 );
48  
49 34 has param 'locktime' => (
50 isa => IntMaxBits [32],
51 default => 0,
52 );
53  
54 # used to mark whether the transaction was serialized with witness data, even
55 # if no actual witness data was present (it was empty)
56 34 has param 'had_witness_flag' => (
57 isa => Bool,
58 default => !!0,
59 );
60  
61 has field '_digest_object' => (
62 isa => InstanceOf ['Bitcoin::Crypto::Transaction::Digest'],
63 lazy => sub {
64 98 ▶ Bitcoin::Crypto::Transaction::Digest->new(transaction => shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
65 },
66 34 clearer => -public,
67 );
68  
69 34 with qw(
70 Bitcoin::Crypto::Role::ShallowClone
71 );
72  
73 after _cloned => sub {
74 118 ▶ shift->clear_digest_object;
Subroutine __ANON__: called CC=1 SCAR=0.0
75 34 };
76  
77 sub add_input
78 {
79 77 ▶ my ($self, @data) = @_;
Subroutine add_input: called CC=4 SCAR=17.3
Pod documented
80  
81 77 my $input;
82 77 ▶ if (@data == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data == 1) { } 0 77 77
83 0 $input = $data[0];
84  
85 0 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($input) and $input->isa("Bitcoin::Crypto::Transaction::Input") 0 0 0
Condition0%0/3
builtin::blessed($input) and $input->isa("Bitcoin::Crypto::Transaction::Input")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($input) and $input->isa("Bitcoin::Crypto::Transaction::Input")
builtin::blessed($input) $input->isa("Bitcoin::Crypto::Transaction::Input")
Truth table0%0/3
A and B
A= builtin::blessed($input)B= $input->isa("Bitcoin::Crypto::Transaction::Input")
ABresult
0X0
100
111
86 'expected an input object'
87 ) unless blessed $input && $input->isa('Bitcoin::Crypto::Transaction::Input');
88 }
89 else {
90 77 $input = Bitcoin::Crypto::Transaction::Input->new(@data);
91 }
92  
93 77 push @{$self->inputs}, $input;
94 77 $self->clear_digest_object;
95 77 return $self;
96 }
97  
98 sub add_output
99 {
100 73 ▶ my ($self, @data) = @_;
Subroutine add_output: called CC=4 SCAR=17.3
Pod documented
101  
102 73 my $output;
103 73 ▶ if (@data == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data == 1) { } 0 73 73
104 0 $output = $data[0];
105  
106 0 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($output) and $output->isa("Bitcoin::Crypto::Transaction::Output") 0 0 0
Condition0%0/3
builtin::blessed($output) and $output->isa("Bitcoin::Crypto::Transaction::Output")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($output) and $output->isa("Bitcoin::Crypto::Transaction::Output")
builtin::blessed($output) $output->isa("Bitcoin::Crypto::Transaction::Output")
Truth table0%0/3
A and B
A= builtin::blessed($output)B= $output->isa("Bitcoin::Crypto::Transaction::Output")
ABresult
0X0
100
111
107 'expected an output object'
108 ) unless blessed $output && $output->isa('Bitcoin::Crypto::Transaction::Output');
109 }
110 else {
111 73 $output = Bitcoin::Crypto::Transaction::Output->new(@data);
112 }
113  
114 73 push @{$self->outputs}, $output;
115 73 $self->clear_digest_object;
116 73 return $self;
117 }
118  
119 34 signature_for to_serialized => (
120 method => !!1,
121 named => [
122 witness => Bool,
123 {default => 1},
124 ],
125 bless => !!0,
126 );
127  
128 sub to_serialized
129 {
130 348 ▶ my ($self, $args) = @_;
Subroutine to_serialized: called CC=5 SCAR=16.1
131  
132 348 my $inputs = $self->inputs;
133 348 my $outputs = $self->outputs;
134  
135 my $with_witness = $args->{witness}
136 348 ▶ && ($self->had_witness_flag || any { $_->has_witness } @{$inputs});
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%6/6
$args->{'witness'} && ($self->had_witness_flag || &any(sub { $_->has_witness; } , @{$inputs;}))
!ll&&!rl&&r
19147110
$self->had_witness_flag or &any(sub { $_->has_witness; } , @{$inputs;})
l!l&&r!l&&!r
104647
MC/DC100%3/3
$args->{'witness'} && ($self->had_witness_flag || &any(sub { $_->has_witness; } , @{$inputs;}))
$args->{'witness'} $self->had_witness_flag &any(sub { $_->has_witness; } , @{$inputs;})
Truth table100%4/4
A && B or C
A= $args->{'witness'}B= $self->had_witness_flagC= &any(sub { $_->has_witness; } , @{$inputs;})
ABCresult
0XX0
1000
11X1
1011
137  
138 # transaction should be serialized as follows:
139 # - version, 4 bytes
140 # - number of inputs, 1-9 bytes
141 # - serialized inputs
142 # - number of outputs, 1-9 bytes
143 # - serialized outputs
144 # - lock time, 4 bytes
145  
146 # segwit transaction should be serialized as follows:
147 # - version, 4 bytes
148 # - 0x0001, if witness data is present
149 # - number of inputs, 1-9 bytes
150 # - serialized inputs
151 # - number of outputs, 1-9 bytes
152 # - serialized outputs
153 # - witness data
154 # - lock time, 4 bytes
155  
156 348 my $serialized = pack 'V', $self->version;
157 348 ▶ $serialized .= pack 'n', 0x0001 if $with_witness;
Branch: 1 branch
BranchTrueFalseTotal
if $with_witness 110 238 348
158  
159 # Process inputs
160 348 $serialized .= pack_compactsize(scalar @{$inputs});
161 348 $serialized .= join '', map { $_->to_serialized } @{$inputs};
162  
163 # Process outputs
164 348 $serialized .= pack_compactsize(scalar @{$outputs});
165 348 $serialized .= join '', map { $_->to_serialized } @{$outputs};
166  
167 348 ▶ $serialized .= join '', map { $_->serialized_witness } @{$inputs}
Branch: 1 branch
BranchTrueFalseTotal
if $with_witness 110 238 348
168 if $with_witness;
169  
170 348 $serialized .= pack 'V', $self->locktime;
171  
172 348 return $serialized;
173 }
174  
175 34 signature_for from_serialized => (
176 method => !!1,
177 head => [ByteStr],
178 named => [
179 pos => Maybe [ScalarRef [PositiveOrZeroInt]],
180 {default => undef},
181 ],
182 bless => !!0,
183 );
184  
185 sub from_serialized
186 {
187 493 ▶ my ($class, $serialized, $args) = @_;
Subroutine from_serialized: called CC=11 SCAR=24.0
188 493 my $partial = !!$args->{pos};
189 493 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 46 447 493
190  
191 # optimization - no need to keep checking bytestrings on every level. It
192 # has already been checked.
193 493 local $Bitcoin::Crypto::Types::CHECK_BYTESTRINGS = !!0;
194  
195 493 my ($version, $witness_flag) = unpack "\@$pos Vn", $serialized;
196 493 $pos += 6;
197  
198 # back off if no witness equal to 0x0001
199 493 ▶ if ($witness_flag != 0x0001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($witness_flag != 1) 421 72 493
200 421 $pos -= 2;
201 421 $witness_flag = 0;
202 }
203  
204 493 my $input_count = unpack_compactsize $serialized, \$pos;
205 493 my @inputs;
206 493 for (1 .. $input_count) {
207 619 push @inputs, Bitcoin::Crypto::Transaction::Input->from_serialized(
208 $serialized, pos => \$pos
209 );
210 }
211  
212 493 my $output_count = unpack_compactsize $serialized, \$pos;
213 493 my @outputs;
214 493 for (1 .. $output_count) {
215 783 push @outputs, Bitcoin::Crypto::Transaction::Output->from_serialized(
216 $serialized, pos => \$pos
217 );
218 }
219  
220 493 ▶ if ($witness_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($witness_flag) 72 421 493
221 72 foreach my $input (@inputs) {
222 135 my $input_witness = unpack_compactsize $serialized, \$pos;
223 135 my @witness;
224 135 for (1 .. $input_witness) {
225 225 my $witness_count = unpack_compactsize $serialized, \$pos;
226  
227 225 push @witness, substr $serialized, $pos, $witness_count;
228 225 $pos += $witness_count;
229 }
230  
231 135 $input->set_witness(\@witness);
232 }
233 }
234  
235 493 my $locktime = unpack "\@$pos V", $serialized;
236 493 $pos += 4;
237  
238 493 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $serialized 0 493 493
Condition66%2/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
464470
MC/DC0%0/2
not $partial and $pos != length $serialized
not $partial $pos != length $serialized
Truth table66%2/3
not(A) and B
A= not $partialB= $pos != length $serialized
ABresult
0X0
100
111
239 'serialized transaction data is corrupted'
240 ) if !$partial && $pos != length $serialized;
241  
242 493 ▶ ${$args->{pos}} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if $partial 46 447 493
243 if $partial;
244  
245 493 my $tx = $class->new(
246 version => $version,
247 locktime => $locktime,
248 had_witness_flag => $witness_flag,
249 );
250  
251 493 @{$tx->inputs} = @inputs;
252 493 @{$tx->outputs} = @outputs;
253  
254 493 return $tx;
255 }
256  
257 34 signature_for get_hash => (
258 method => !!1,
259 named => [
260 witness => Bool,
261 {default => 0},
262 ],
263 bless => !!0,
264 );
265  
266 sub get_hash
267 {
268 146 ▶ my ($self, $args) = @_;
Subroutine get_hash: called CC=1 SCAR=0.0
269  
270 146 return scalar reverse hash256($self->to_serialized(%$args));
271 }
272  
273 sub txid
274 {
275 1 ▶ my ($self) = @_;
Subroutine txid: called CC=1 SCAR=0.0
Pod documented
276  
277 1 return $self->get_hash(witness => 0);
278 }
279  
280 sub wtxid
281 {
282 0 ▶ my ($self) = @_;
Subroutine wtxid: not called CC=1 SCAR=6.9
Pod documented
283  
284 0 return $self->get_hash(witness => 1);
285 }
286  
287 sub get_digest
288 {
289 19 ▶ my ($self, %params) = @_;
Subroutine get_digest: called CC=1 SCAR=0.0
Pod documented
290  
291 19 return $self->get_digest_object(%params)->get_digest;
292 }
293  
294 sub get_digest_object
295 {
296 209 ▶ my ($self, %params) = @_;
Subroutine get_digest_object: called CC=1 SCAR=0.0
Pod documented
297  
298 209 my $digest = $self->_digest_object;
299 209 $digest->set_config(\%params);
300 209 return $digest;
301 }
302  
303 sub fee
304 {
305 118 ▶ my ($self, $require_value) = @_;
Subroutine fee: called CC=4 SCAR=13.9
Pod documented
306  
307 118 my $value = 0;
308  
309 118 try {
310 118 foreach my $input (@{$self->inputs}) {
311 162 $value += $input->utxo->output->value;
312 }
313 }
314 catch ($e) {
315  
316 # utxos are unregistered - cannot calculate
317 2 ▶ die $e if $require_value;
Branch: 1 branch
BranchTrueFalseTotal
if $require_value 1 1 2
318 1 return undef;
319 }
320  
321 116 foreach my $output (@{$self->outputs}) {
322 165 $value -= $output->value;
323 }
324  
325 116 return $value;
326 }
327  
328 sub fee_rate
329 {
330 2 ▶ my ($self) = @_;
Subroutine fee_rate: called CC=2 SCAR=7.0
Pod documented
331  
332 2 my $fee = $self->fee;
333 2 ▶ return undef unless defined $fee;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fee 0 2 2
334  
335 # assume $fee will be small enough to be numified from BigInt on 32 bit.
336 # This may be false, but we do not focus on 32 bit support
337 2 my $size = $self->virtual_size;
338 2 return "$fee" / $size;
339 }
340  
341 sub set_rbf
342 {
343 1 ▶ my ($self) = @_;
Subroutine set_rbf: called CC=2 SCAR=7.0
Pod documented
344  
345 # rules according to BIP125
346 # https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
347 1 ▶ if (!$self->has_rbf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_rbf) 1 0 1
348 1 $self->inputs->[0]->set_sequence_no(RBF_SEQUENCE_NO_THRESHOLD);
349 }
350  
351 1 return $self;
352 }
353  
354 sub has_rbf
355 {
356 4 ▶ my ($self) = @_;
Subroutine has_rbf: called CC=3 SCAR=11.0
Pod documented
357  
358 4 foreach my $input (@{$self->inputs}) {
359 4 ▶ return !!1
Branch: 1 branch
BranchTrueFalseTotal
if $input->sequence_no <= 4294967293 1 3 4
360 if $input->sequence_no <= RBF_SEQUENCE_NO_THRESHOLD;
361 }
362  
363 3 return !!0;
364 }
365  
366 sub virtual_size
367 {
368 4 ▶ my ($self) = @_;
Subroutine virtual_size: called CC=1 SCAR=0.0
Pod documented
369  
370 4 my $base = length $self->to_serialized(witness => 0);
371 4 my $with_witness = length $self->to_serialized;
372 4 my $witness = $with_witness - $base;
373  
374 4 return $base + $witness / 4;
375 }
376  
377 sub weight
378 {
379 2 ▶ my ($self) = @_;
Subroutine weight: called CC=1 SCAR=0.0
Pod documented
380  
381 2 my $base = length $self->to_serialized(witness => 0);
382 2 my $with_witness = length $self->to_serialized;
383 2 my $witness = $with_witness - $base;
384  
385 2 return $base * 4 + $witness;
386 }
387  
388 sub update_utxos
389 {
390 4 ▶ my ($self) = @_;
Subroutine update_utxos: called CC=5 SCAR=16.1
Pod documented
391  
392 4 foreach my $input (@{$self->inputs}) {
393 7 ▶ $input->utxo->unregister if $input->utxo_registered;
Branch: 1 branch
BranchTrueFalseTotal
if $input->utxo_registered 2 5 7
394 }
395  
396 4 foreach my $output_index (0 .. $#{$self->outputs}) {
397 8 my $output = $self->outputs->[$output_index];
398  
399 8 ▶ btc_utxo->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_block ? : 0 8 8
400 txid => $self->get_hash,
401 output_index => $output_index,
402 output => $output,
403 ($self->has_block ? (block => $self->block) : ()),
404 )->register;
405 }
406  
407 4 return $self;
408 }
409  
410 sub is_coinbase
411 {
412 123 ▶ my ($self) = @_;
Subroutine is_coinbase: called CC=3 SCAR=11.0
Pod documented
413 123 my $inputs = $self->inputs;
414  
415 123 ▶ return !!0 unless @{$inputs} == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$inputs;} == 1 25 98 123
416  
417 98 my $null_prevout = NULL_UTXO;
418 98 my $utxo_prevout = $inputs->[0]->utxo_location;
419 98 ▶ return $null_prevout->[0] eq $utxo_prevout->[0] && $null_prevout->[1] == $utxo_prevout->[1];
Condition66%2/3
$null_prevout->[0] eq $utxo_prevout->[0] && $null_prevout->[1] == $utxo_prevout->[1]
!ll&&!rl&&r
87011
MC/DC50%1/2
$null_prevout->[0] eq $utxo_prevout->[0] && $null_prevout->[1] == $utxo_prevout->[1]
$null_prevout->[0] eq $utxo_prevout->[0] $null_prevout->[1] == $utxo_prevout->[1]
Truth table66%2/3
A && B
A= $null_prevout->[0] eq $utxo_prevout->[0]B= $null_prevout->[1] == $utxo_prevout->[1]
ABresult
0X0
100
111
420 }
421  
422 sub _verify_script_default
423 {
424 88 ▶ my ($self, $input, $script_runner) = @_;
Subroutine _verify_script_default: called CC=13 SCAR=25.7
425 88 my $locking_script = $input->utxo->output->locking_script;
426  
427 88 ▶ my $is_p2sh = $script_runner->flags->p2sh && ($locking_script->type // '') eq 'P2SH';
Condition100%5/5
$script_runner->flags->p2sh && ($locking_script->type // '') eq 'P2SH'
!ll&&!rl&&r
95623
$locking_script->type // ""
l!l
4930
MC/DC100%2/2
$script_runner->flags->p2sh && ($locking_script->type // '') eq 'P2SH'
$script_runner->flags->p2sh $locking_script->type
Truth table100%3/3
A && B
A= $script_runner->flags->p2shB= $locking_script->type
ABresult
0X0
100
111
428 88 ▶ my $is_pushes_only = $is_p2sh || $script_runner->flags->signature_pushes_only;
Condition100%3/3
$is_p2sh || $script_runner->flags->signature_pushes_only
l!l&&r!l&&!r
23263
MC/DC100%2/2
$is_p2sh || $script_runner->flags->signature_pushes_only
$is_p2sh $script_runner->flags->signature_pushes_only
Truth table100%3/3
A || B
A= $is_p2shB= $script_runner->flags->signature_pushes_only
ABresult
1X1
011
000
429  
430 88 ▶ Bitcoin::Crypto::Exception::TransactionScript->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $is_pushes_only and not $input->signature_script->is_pushes_only 1 87 88
Condition100%3/3
$is_pushes_only and not $input->signature_script->is_pushes_only
!ll&&!rl&&r
63241
MC/DC100%2/2
$is_pushes_only and not $input->signature_script->is_pushes_only
$is_pushes_only not $input->signature_script->is_pushes_only
Truth table100%3/3
A and not(B)
A= $is_pushes_onlyB= not $input->signature_script->is_pushes_only
ABresult
0X0
100
111
431 'signature script must only contain push opcodes'
432 ) if $is_pushes_only && !$input->signature_script->is_pushes_only;
433  
434 # execute input to get initial stack
435 87 $script_runner->execute($input->signature_script);
436 86 my @stack = @{$script_runner->stack};
437 86 my @locking_stack;
438 my $redeem_script;
439  
440 86 ▶ if ($is_p2sh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_p2sh) { } 23 63 86
441 23 $redeem_script = pop @stack;
442 23 ▶ @locking_stack = ($redeem_script)
Branch: 1 branch
BranchTrueFalseTotal
if defined $redeem_script 23 0 23
443 if defined $redeem_script;
444 }
445 else {
446 63 @locking_stack = @stack;
447 }
448  
449 # execute previous output
450 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
451 sub {
452 86 ▶ $script_runner->execute($locking_script, \@locking_stack);
Subroutine __ANON__: called CC=2 SCAR=6.9
453 66 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 2 64 66
454 unless $script_runner->success;
455 },
456 86 'locking script'
457 );
458  
459 64 ▶ if (defined $redeem_script) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redeem_script) 23 41 64
460 23 $redeem_script = btc_script->from_serialized($redeem_script);
461  
462 23 ▶ my $type = $redeem_script->type // '';
Condition100%2/2
$redeem_script->type // ""
l!l
221
MC/DC100%1/1
$redeem_script->type // ""
$redeem_script->type
Truth table100%2/2
A
A= $redeem_script->type
Aresult
11
00
463 23 ▶ if ($script_runner->flags->segwit && ($type eq 'P2WPKH' || $type eq 'P2WSH')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_runner->flags->segwit and $type eq 'P2WPKH' || $type eq 'P2WSH') { } 16 7 23
Condition100%6/6
$script_runner->flags->segwit and $type eq "P2WPKH" || $type eq "P2WSH"
!ll&&!rl&&r
1616
$type eq "P2WPKH" or $type eq "P2WSH"
l!l&&r!l&&!r
1066
MC/DC100%3/3
$script_runner->flags->segwit and $type eq "P2WPKH" || $type eq "P2WSH"
$script_runner->flags->segwit $type eq "P2WPKH" $type eq "P2WSH"
Truth table100%4/4
A and B or C
A= $script_runner->flags->segwitB= $type eq "P2WPKH"C= $type eq "P2WSH"
ABCresult
0XX0
1000
11X1
1011
464 16 $self->_verify_script_segwit($input, $script_runner, $redeem_script);
465 }
466 else {
467 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
468 sub {
469 7 ▶ $script_runner->execute($redeem_script, \@stack);
Subroutine __ANON__: called CC=2 SCAR=6.9
470 6 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 1 5 6
471 unless $script_runner->success;
472 },
473 7 'redeem script'
474 );
475 }
476 }
477 }
478  
479 sub _verify_script_segwit
480 {
481 41 ▶ my ($self, $input, $script_runner, $compat_script) = @_;
Subroutine _verify_script_segwit: called CC=11 SCAR=24.4
482  
483 41 ▶ die_no_trace 'signature script is not empty in segwit input'
Branch: 1 branch
BranchTrueFalseTotal
unless $compat_script or $input->signature_script->is_empty 0 41 41
Condition66%2/3
$compat_script or $input->signature_script->is_empty
l!l&&r!l&&!r
16250
MC/DC0%0/2
$compat_script or $input->signature_script->is_empty
$compat_script $input->signature_script->is_empty
Truth table66%2/3
A or B
A= $compat_scriptB= $input->signature_script->is_empty
ABresult
1X1
011
000
484 unless $compat_script || $input->signature_script->is_empty;
485  
486 # use shallow copy of witness as initial stack
487 41 ▶ my @stack = @{$input->witness // []};
Condition50%1/2
$input->witness // []
l!l
410
MC/DC0%0/1
$input->witness // []
$input->witness
Truth table50%1/2
A
A= $input->witness
Aresult
11
00
488 41 my $redeem_script;
489 my @locking_stack;
490  
491 41 ▶ my $locking_script = $compat_script // $input->utxo->output->locking_script;
Condition66%2/3
$compat_script // $input->utxo->output->locking_script
l!l&&r!l&&!r
16250
MC/DC0%0/2
$compat_script // $input->utxo->output->locking_script
$compat_script $input->utxo->output->locking_script
Truth table66%2/3
A // B
A= $compat_scriptB= $input->utxo->output->locking_script
ABresult
1X1
011
000
492 41 my $hash = substr $locking_script->to_serialized, 2;
493 41 my $actual_locking_script;
494 41 ▶ if ($locking_script->type eq 'P2WPKH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($locking_script->type eq 'P2WPKH') { } 17 24 41
elsif ($locking_script->type eq 'P2WSH') { } 21 3 24
495 17 $actual_locking_script = Bitcoin::Crypto::Script::Common->new(PKH => $hash);
496 17 @locking_stack = @stack;
497 }
498 elsif ($locking_script->type eq 'P2WSH') {
499 21 $actual_locking_script = Bitcoin::Crypto::Script::Common->new(WSH => $hash);
500 21 $redeem_script = pop @stack;
501 21 ▶ @locking_stack = ($redeem_script) if defined $redeem_script;
Branch: 1 branch
BranchTrueFalseTotal
if defined $redeem_script 21 0 21
502 }
503 else {
504 # not a known segwit version 0 output
505  
506 3 ▶ die_no_trace 'unknown witness v0 program' if $locking_script->segwit_version == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $locking_script->segwit_version == 0 0 3 3
507 3 ▶ die_no_trace 'unknown witness program' if $script_runner->flags->known_witness;
Branch: 1 branch
BranchTrueFalseTotal
if $script_runner->flags->known_witness 1 2 3
508 2 return;
509 }
510  
511 # execute locking script
512 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
513 sub {
514 38 ▶ $script_runner->execute($actual_locking_script, \@locking_stack);
Subroutine __ANON__: called CC=2 SCAR=7.2
515 34 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 0 34 34
516 unless $script_runner->success;
517 },
518 38 'segwit locking script'
519 );
520  
521 # execute redeem script
522 34 ▶ if (defined $redeem_script) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redeem_script) 21 13 34
523 21 $redeem_script = btc_script->from_serialized($redeem_script);
524  
525 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
526 sub {
527 21 ▶ $script_runner->execute($redeem_script, \@stack);
Subroutine __ANON__: called CC=2 SCAR=7.2
528 16 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 0 16 16
529 unless $script_runner->success;
530 },
531 21 'segwit redeem script'
532 );
533 }
534 }
535  
536 sub _verify_script_taproot
537 {
538 28 ▶ my ($self, $input, $script_runner) = @_;
Subroutine _verify_script_taproot: called CC=10 SCAR=24.1
539  
540 28 ▶ die_no_trace 'signature script is not empty in taproot input'
Branch: 1 branch
BranchTrueFalseTotal
unless $input->signature_script->is_empty 0 28 28
541 unless $input->signature_script->is_empty;
542  
543 28 ▶ die_no_trace 'witness stack has 0 elements'
Branch: 1 branch
BranchTrueFalseTotal
unless $input->has_witness 0 28 28
544 unless $input->has_witness;
545  
546 # shallow copy of the witness - avoid modifying the transaction
547 28 my @witness_stack = @{$input->witness};
548 28 my $pubkey = $input->utxo->output->locking_script->get_raw_address;
549  
550 # consensus rules from https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#script-validation-rules
551  
552 # remove the annex from the witness stack - annex first byte is 0x50
553 28 ▶ $script_runner->transaction->set_taproot_annex(pop @witness_stack)
Branch: 1 branch
BranchTrueFalseTotal
if @witness_stack >= 2 and (unpack("C", $witness_stack[-1]) // 0) == 80 0 28 28
Condition60%3/5
@witness_stack >= 2 and (unpack("C", $witness_stack[-1]) // 0) == 80
!ll&&!rl&&r
2080
unpack("C", $witness_stack[-1]) // 0
l!l
80
MC/DC50%1/2
@witness_stack >= 2 and (unpack("C", $witness_stack[-1]) // 0) == 80
@witness_stack >= 2 unpack("C", $witness_stack[-1])
Truth table66%2/3
A and B
A= @witness_stack >= 2B= unpack("C", $witness_stack[-1])
ABresult
0X0
100
111
554 if @witness_stack >= 2 && (unpack('C', $witness_stack[-1]) // 0) == 0x50;
555  
556 28 my $script;
557 28 ▶ if (@witness_stack == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@witness_stack == 1) { } 20 8 28
558 20 $script = Bitcoin::Crypto::Script::Common->new(TR => $pubkey);
559 }
560 else {
561 my $control_block = Bitcoin::Crypto::Exception->trap_into(
562 sub {
563 8 ▶ return Bitcoin::Crypto::Transaction::ControlBlock->from_serialized(pop @witness_stack);
Subroutine __ANON__: called CC=1 SCAR=0.0
564 },
565 8 'invalid control block'
566 );
567  
568 8 my $raw_script = pop @witness_stack;
569  
570 8 my $leaf_version = $control_block->get_leaf_version;
571 8 ▶ if ($leaf_version == TAPSCRIPT_LEAF_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf_version == 192) { } 8 0 8
572 8 $script = btc_tapscript->from_serialized($raw_script);
573 8 $script_runner->transaction->set_taproot_ext_flag(1);
574 }
575 else {
576 # future compatibility - script must succeed
577 # https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-12
578 0 return;
579 }
580  
581 # TODO: for now, leaf must have id 0 to get recognized by runner (see
582 # OP_CHECKSIG for tapscript)
583 8 my $tree = btc_script_tree->from_path(
584 {
585 id => 0,
586 leaf_version => $leaf_version,
587 script => $script,
588 },
589 $control_block->script_blocks
590 );
591  
592 8 $script_runner->transaction->set_script_tree($tree);
593  
594 8 my $tweaked = $control_block->public_key->get_taproot_output_key($tree->get_merkle_root);
595 8 my $expected_parity = !has_even_y($tweaked);
596 8 ▶ die_no_trace 'invalid public key or control block'
Branch: 1 branch
BranchTrueFalseTotal
unless $tweaked->get_xonly_key eq $pubkey and $expected_parity == ($control_block->control_byte & 1) 0 8 8
Condition33%1/3
$tweaked->get_xonly_key eq $pubkey and $expected_parity == ($control_block->control_byte & 1)
!ll&&!rl&&r
008
MC/DC0%0/2
$tweaked->get_xonly_key eq $pubkey and $expected_parity == ($control_block->control_byte & 1)
$tweaked->get_xonly_key eq $pubkey $expected_parity == ($control_block->control_byte & 1)
Truth table33%1/3
A and B
A= $tweaked->get_xonly_key eq $pubkeyB= $expected_parity == ($control_block->control_byte & 1)
ABresult
0X0
100
111
597 unless $tweaked->get_xonly_key eq $pubkey
598 && $expected_parity == ($control_block->control_byte & 1);
599 }
600  
601 # execute script
602 # use remaining witness elements as initial stack
603 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
604 sub {
605 28 ▶ $script_runner->transaction->set_sigop_budget(length $input->serialized_witness);
Subroutine __ANON__: called CC=2 SCAR=7.1
606 28 $script_runner->execute($script, \@witness_stack);
607 25 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 0 25 25
608 unless $script_runner->success;
609 },
610 28 'taproot script'
611 );
612 }
613  
614 sub verify_script
615 {
616 141 ▶ my ($self, $input_index, $script_runner) = @_;
Subroutine verify_script: called CC=5 SCAR=16.1
Pod undocumented
617 141 $script_runner->transaction->_clear;
618 141 $script_runner->transaction->set_input_index($input_index);
619  
620 141 my $input = $self->inputs->[$input_index];
621 141 my $utxo = $input->utxo;
622  
623 # run bitcoin script
624 141 my $procedure = '_verify_script_default';
625 141 ▶ if ($script_runner->flags->taproot && $utxo->output->locking_script->is_taproot) {
Branch: 2 branches
BranchTrueFalseTotal
if ($script_runner->flags->taproot and $utxo->output->locking_script->is_taproot) { } 28 113 141
elsif ($script_runner->flags->segwit and $utxo->output->locking_script->is_native_segwit) { } 25 96 121
Condition100%3/3
$script_runner->flags->taproot and $utxo->output->locking_script->is_taproot
!ll&&!rl&&r
810528
MC/DC100%2/2
$script_runner->flags->taproot and $utxo->output->locking_script->is_taproot
$script_runner->flags->taproot $utxo->output->locking_script->is_taproot
Truth table100%3/3
A and B
A= $script_runner->flags->taprootB= $utxo->output->locking_script->is_taproot
ABresult
0X0
100
111
626 28 $procedure = '_verify_script_taproot';
627 }
628 ▶ elsif ($script_runner->flags->segwit && $utxo->output->locking_script->is_native_segwit) {
Condition100%3/3
$script_runner->flags->segwit and $utxo->output->locking_script->is_native_segwit
!ll&&!rl&&r
88025
MC/DC100%2/2
$script_runner->flags->segwit and $utxo->output->locking_script->is_native_segwit
$script_runner->flags->segwit $utxo->output->locking_script->is_native_segwit
Truth table100%3/3
A and B
A= $script_runner->flags->segwitB= $utxo->output->locking_script->is_native_segwit
ABresult
0X0
100
111
629 25 $procedure = '_verify_script_segwit';
630 }
631  
632 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
633 sub {
634 141 ▶ die_no_trace 'witness in non-witness input'
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
if not $input->is_segwit and $input->has_witness 0 141 141
Condition66%2/3
not $input->is_segwit and $input->has_witness
!ll&&!rl&&r
70710
MC/DC0%0/2
not $input->is_segwit and $input->has_witness
not $input->is_segwit $input->has_witness
Truth table66%2/3
not(A) and B
A= not $input->is_segwitB= $input->has_witness
ABresult
0X0
100
111
635 if !$input->is_segwit && $input->has_witness;
636  
637 141 $self->$procedure($input, $script_runner);
638 },
639 141 "transaction input $input_index verification has failed"
640 );
641 }
642  
643 sub _verify_coinbase
644 {
645 0 ▶ my ($self, $script_runner) = @_;
Subroutine _verify_coinbase: not called CC=6 SCAR=37.4
646 0 my $block = $self->block;
647  
648 Bitcoin::Crypto::Exception::Transaction->raise(
649 'coinbase transaction must have one input'
650 0 ▶ ) if @{$self->inputs} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->inputs;} != 1 0 0 0
651  
652 0 my $coinbase_data = $self->inputs->[0]->signature_script->to_serialized;
653  
654 0 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $coinbase_data < 2 or length $coinbase_data > 100 0 0 0
Condition0%0/3
length $coinbase_data < 2 or length $coinbase_data > 100
l!l&&r!l&&!r
000
MC/DC0%0/2
length $coinbase_data < 2 or length $coinbase_data > 100
length $coinbase_data < 2 length $coinbase_data > 100
Truth table0%0/3
A or B
A= length $coinbase_data < 2B= length $coinbase_data > 100
ABresult
1X1
011
000
655 'coinbase data not between 2 and 100 bytes'
656 ) if length $coinbase_data < 2 || length $coinbase_data > 100;
657  
658 0 ▶ if (!defined $block) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $block) 0 0 0
659 0 carp 'trying to verify coinbase transaction but block was not set';
660 0 return;
661 }
662  
663 Bitcoin::Crypto::Exception::Transaction->raise(
664 0 ▶ 'coinbase must be the first transaction in a block'
Branch: 1 branch
BranchTrueFalseTotal
unless $block->transactions->[0] == $self 0 0 0
665 ) unless $block->transactions->[0] == $self;
666  
667 # NOTE: most of other coinbase verification (like block reward checking)
668 # will be done in a block
669  
670 0 return;
671 }
672  
673 34 signature_for verify => (
674 method => !!1,
675 named => [
676 block => Maybe [InstanceOf ['Bitcoin::Crypto::Block']],
677 {default => undef},
678 flags => TransactionFlags,
679 {default => undef},
680 ],
681 bless => !!0,
682 );
683  
684 sub verify
685 {
686 0 ▶ my ($self, $args) = @_;
Subroutine verify: called CC=28 SCAR=36.6
687  
688 0 ▶ if ($args->{block}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"block"}) 0 112 112
689 112 carp 'passing block parameter to verify method is deprecated: call set_block on transaction instead';
690 112 $self->set_block($args->{block});
691 }
692  
693 112 my $block = $self->block;
694 112 my $inputs = $self->inputs;
695 112 my $outputs = $self->outputs;
696  
697 112 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @$inputs 0 112 112
698 'transaction has no inputs'
699 ) if !@$inputs;
700  
701 112 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @$outputs 0 112 112
702 'transaction has no outputs'
703 ) if !@$outputs;
704  
705 my $script_runner = Bitcoin::Crypto::Script::Runner->new(
706 transaction => $self,
707 flags => $args->{flags},
708 112 );
709  
710 # check output values
711 Bitcoin::Crypto::Exception::Transaction->raise(
712 'output value is out of range'
713 112 ▶ ) if any { $_->value > MAX_MONEY } @$outputs;
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_->value > '2100000000000000'; } , @$outputs) 0 112 112
714  
715 # use special procedure if coinbase
716 156 ▶ return $self->_verify_coinbase($script_runner)
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_coinbase 0 112 112
717 if $self->is_coinbase;
718  
719 Bitcoin::Crypto::Exception::Transaction->raise(
720 'transaction has duplicate inputs'
721 112 ▶ ) if @$inputs != uniqstr map { $_->prevout } @$inputs;
Branch: 1 branch
BranchTrueFalseTotal
if @$inputs != &uniqstr(map({$_->prevout;} @$inputs)) 0 112 112
722  
723 18 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if 1 110 111
724 'output value exceeds input'
725 ) if $self->fee(!!1) < 0;
726  
727 # locktime checking
728 18 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->locktime > 0 and &any(sub { $_->sequence_no != 4294967295; } , @$inputs)) 18 92 110
Condition66%2/3
$self->locktime > 0 and &any(sub { $_->sequence_no != 4294967295; } , @$inputs)
!ll&&!rl&&r
92018
MC/DC50%1/2
$self->locktime > 0 and &any(sub { $_->sequence_no != 4294967295; } , @$inputs)
$self->locktime > 0 &any(sub { $_->sequence_no != 4294967295; } , @$inputs)
Truth table66%2/3
A and B
A= $self->locktime > 0B= &any(sub { $_->sequence_no != 4294967295; } , @$inputs)
ABresult
0X0
100
111
729 $self->locktime > 0 && any {
730 $_->sequence_no != MAX_SEQUENCE_NO
731 } @$inputs
732 )
733 {
734 18 my $locktime = $self->locktime;
735 18 my $is_timestamp = $locktime >= LOCKTIME_HEIGHT_THRESHOLD;
736 0 ▶ if (defined $block && ($is_timestamp || $block->has_height)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $block and $is_timestamp || $block->has_height) { } 18 0 18
Condition50%3/6
defined $block and $is_timestamp || $block->has_height
!ll&&!rl&&r
0018
$is_timestamp or $block->has_height
l!l&&r!l&&!r
7110
MC/DC0%0/3
defined $block and $is_timestamp || $block->has_height
defined $block $is_timestamp $block->has_height
Truth table50%2/4
A and B or C
A= defined $blockB= $is_timestampC= $block->has_height
ABCresult
0XX0
1000
11X1
1011
737 108 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 2 branches
BranchTrueFalseTotal
if $locktime > ($is_timestamp ? $block->median_time_past : $block->height) 2 16 18
$is_timestamp ? : 7 11 18
738 'locktime was not satisfied'
739 ) if $locktime > ($is_timestamp ? $block->median_time_past : $block->height);
740 }
741 else {
742 108 carp 'trying to verify locktime but block was not set';
743 }
744 }
745  
746 # per-input verification
747 141 foreach my $input_index (keys @{$inputs}) {
748 7 $self->verify_script($input_index, $script_runner);
749  
750 # check sequence (BIP 68)
751 7 ▶ if ($self->version >= 2 && !($inputs->[$input_index]->sequence_no & (1 << 31))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version >= 2 and not $inputs->[$input_index]->sequence_no & 2147483648) 7 95 102
Condition100%3/3
$self->version >= 2 and not $inputs->[$input_index]->sequence_no & 2147483648
!ll&&!rl&&r
66297
MC/DC100%2/2
$self->version >= 2 and not $inputs->[$input_index]->sequence_no & 2147483648
$self->version >= 2 not $inputs->[$input_index]->sequence_no & 2147483648
Truth table100%3/3
A and not(B)
A= $self->version >= 2B= not $inputs->[$input_index]->sequence_no & 2147483648
ABresult
0X0
100
111
752 7 my $sequence = $inputs->[$input_index]->sequence_no;
753 7 my $utxo = $inputs->[$input_index]->utxo;
754 7 my $time_based = $sequence & (1 << 22);
755 7 my $relative_locktime = $sequence & 0x0000ffff;
756 7 ▶ my $has_block = defined $block && ($time_based || $block->has_height);
Condition50%3/6
defined $block && ($time_based || $block->has_height)
!ll&&!rl&&r
007
$time_based or $block->has_height
l!l&&r!l&&!r
250
MC/DC0%0/3
defined $block && ($time_based || $block->has_height)
defined $block $time_based $block->has_height
Truth table50%2/4
A && B or C
A= defined $blockB= $time_basedC= $block->has_height
ABCresult
0XX0
1000
11X1
1011
757 7 ▶ my $has_utxo_block = $utxo->has_block && ($time_based || $utxo->block->has_height);
Condition50%3/6
$utxo->has_block && ($time_based || $utxo->block->has_height)
!ll&&!rl&&r
007
$time_based or $utxo->block->has_height
l!l&&r!l&&!r
250
MC/DC0%0/3
$utxo->has_block && ($time_based || $utxo->block->has_height)
$utxo->has_block $time_based $utxo->block->has_height
Truth table50%2/4
A && B or C
A= $utxo->has_blockB= $time_basedC= $utxo->block->has_height
ABCresult
0XX0
1000
11X1
1011
758  
759 7 ▶ if ($has_block && $has_utxo_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_block and $has_utxo_block) { } 7 0 7
Condition33%1/3
$has_block and $has_utxo_block
!ll&&!rl&&r
007
MC/DC0%0/2
$has_block and $has_utxo_block
$has_block $has_utxo_block
Truth table33%1/3
A and B
A= $has_blockB= $has_utxo_block
ABresult
0X0
100
111
760 7 my $utxo_block = $utxo->block;
761 7 ▶ my $now = $time_based ? $block->median_time_past : $block->height;
Branch: 1 branch
BranchTrueFalseTotal
$time_based ? : 2 5 7
762 7 ▶ my $then = $time_based ? $utxo_block->median_time_past : $utxo_block->height;
Branch: 1 branch
BranchTrueFalseTotal
$time_based ? : 2 5 7
763 0 ▶ $relative_locktime <<= 9 if $time_based;
Branch: 1 branch
BranchTrueFalseTotal
if $time_based 2 5 7
764  
765 67 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $now < $then + $relative_locktime 2 5 7
766 'relative locktime was not satisfied'
767 ) if $now < $then + $relative_locktime;
768 }
769 else {
770 161 carp 'trying to verify relative locktime but block or utxo block was not set';
771 }
772 }
773 }
774  
775 # TODO: check if coinbase transaction within 100 blocks is spent in any input
776  
777 # TODO: check outputs
778 # - do they have non-zero values (other than OP_RETURN)?
779 # - do OP_RETURN scripts keep data limit
780 # - do they have valid script
781  
782 18 return;
783 }
784  
785 sub verify_standard
786 {
787 1 ▶ my ($self, @args) = @_;
Subroutine verify_standard: called CC=1 SCAR=0.0
Pod documented
788  
789 1 return $self->verify(
790 @args,
791 flags => Bitcoin::Crypto::Transaction::Flags->new_full
792 );
793 }
794  
795 sub sign
796 {
797 48 ▶ my ($self, %args) = @_;
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
798  
799 48 return Bitcoin::Crypto::Transaction::Signer->new_impl($self, \%args);
800 }
801  
802 sub dump
803 {
804 1 ▶ my ($self) = @_;
Subroutine dump: called CC=6 SCAR=18.0
Pod documented
805  
806 1 my $fee = $self->fee;
807 1 ▶ my $fee_rate = defined $fee ? int($self->fee_rate * 100) / 100 : '??';
Branch: 1 branch
BranchTrueFalseTotal
defined $fee ? : 1 0 1
808 1 ▶ $fee //= '??';
Condition50%1/2
$fee //= "??"
l!l
10
MC/DC0%0/1
$fee //= "??"
$fee
Truth table50%1/2
A
A= $fee
Aresult
11
00
809  
810 1 my @result;
811 1 push @result, 'Transaction ' . to_format [hex => $self->get_hash];
812 1 push @result, 'version: ' . $self->version;
813 1 push @result, 'size: ' . $self->virtual_size . 'vB, ' . $self->weight . 'WU';
814 1 push @result, "fee: $fee sat (~$fee_rate sat/vB)";
815 1 ▶ push @result, 'replace-by-fee: ' . ($self->has_rbf ? 'yes' : 'no');
Branch: 1 branch
BranchTrueFalseTotal
$self->has_rbf ? : 0 1 1
816 1 push @result, 'locktime: ' . $self->locktime;
817 1 push @result, '';
818  
819 1 push @result, @{$self->inputs} . ' inputs:';
820 1 foreach my $input (@{$self->inputs}) {
821 1 push @result, $input->dump;
822 1 push @result, '';
823 }
824  
825 1 push @result, @{$self->outputs} . ' outputs:';
826 1 foreach my $output (@{$self->outputs}) {
827 1 push @result, $output->dump;
828 1 push @result, '';
829 }
830  
831 1 return join "\n", @result;
832 }
833  
834 34 1;
835  
836 __END__
« Crypto/Script/Recognition.pm ↑ Summary Crypto/Helpers.pm »