Crypto/Transaction.pm

filter: Statementstmt 94.4% 376 / 398 Branchbran 69.8% 95 / 136 Conditioncond 65.9% 62 / 94 MC/DCmcdc 40.7% 22 / 54 Subroutinesub 96.7% 59 / 61 Podpod 94.4% 17 / 18 totaltotal 82.9% 631 / 761 CC 114 SCAR 25.0
CC
114
Cov
85%
_verify_coinbase
42.0
verify
38.8
_verify_script_default
13.0
CRAP 12.2
« Crypto/Script/Recognition.pm ↑ Summary Crypto/Helpers.pm »
1 package Bitcoin::Crypto::Transaction;
2 34 $Bitcoin::Crypto::Transaction::VERSION = '4.005';
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 574 ▶ 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 574 ▶ 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 99 ▶ lazy => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 99 Bitcoin::Crypto::Transaction::Digest->new(transaction => shift);
65 },
66 34 clearer => -public,
67 );
68  
69 34 with qw(
70 Bitcoin::Crypto::Role::ShallowClone
71 );
72  
73 122 ▶after _cloned => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 122 shift->clear_digest_object;
75 34 };
76  
77 sub add_input
78 78 ▶{
Subroutine add_input: called CC=4 SCAR=17.3
Pod documented
79 78 my ($self, @data) = @_;
80  
81 78 my $input;
82 78 ▶ if (@data == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data == 1) { } 0 78 78
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 78 $input = Bitcoin::Crypto::Transaction::Input->new(@data);
91 }
92  
93 78 push @{$self->inputs}, $input;
94 78 $self->clear_digest_object;
95 78 return $self;
96 }
97  
98 sub add_output
99 74 ▶{
Subroutine add_output: called CC=4 SCAR=17.3
Pod documented
100 74 my ($self, @data) = @_;
101  
102 74 my $output;
103 74 ▶ if (@data == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data == 1) { } 0 74 74
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 74 $output = Bitcoin::Crypto::Transaction::Output->new(@data);
112 }
113  
114 74 push @{$self->outputs}, $output;
115 74 $self->clear_digest_object;
116 74 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 352 ▶{
Subroutine to_serialized: called CC=5 SCAR=16.1
130 352 my ($self, $args) = @_;
131  
132 352 my $inputs = $self->inputs;
133 352 my $outputs = $self->outputs;
134  
135 my $with_witness = $args->{witness}
136 352 ▶ && ($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
19448110
$self->had_witness_flag or &any(sub { $_->has_witness; } , @{$inputs;})
l!l&&r!l&&!r
104648
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 352 my $serialized = pack 'V', $self->version;
157 352 ▶ $serialized .= pack 'n', 0x0001 if $with_witness;
Branch: 1 branch
BranchTrueFalseTotal
if $with_witness 110 242 352
158  
159 # Process inputs
160 352 $serialized .= pack_compactsize(scalar @{$inputs});
161 352 $serialized .= join '', map { $_->to_serialized } @{$inputs};
162  
163 # Process outputs
164 352 $serialized .= pack_compactsize(scalar @{$outputs});
165 352 $serialized .= join '', map { $_->to_serialized } @{$outputs};
166  
167 352 ▶ $serialized .= join '', map { $_->serialized_witness } @{$inputs}
Branch: 1 branch
BranchTrueFalseTotal
if $with_witness 110 242 352
168 if $with_witness;
169  
170 352 $serialized .= pack 'V', $self->locktime;
171  
172 352 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 507 ▶{
Subroutine from_serialized: called CC=11 SCAR=24.0
187 507 my ($class, $serialized, $args) = @_;
188 507 my $partial = !!$args->{pos};
189 507 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 46 461 507
190  
191 # optimization - no need to keep checking bytestrings on every level. It
192 # has already been checked.
193 507 local $Bitcoin::Crypto::Types::CHECK_BYTESTRINGS = !!0;
194  
195 507 my ($version, $witness_flag) = unpack "\@$pos Vn", $serialized;
196 507 $pos += 6;
197  
198 # back off if no witness equal to 0x0001
199 507 ▶ if ($witness_flag != 0x0001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($witness_flag != 1) 435 72 507
200 435 $pos -= 2;
201 435 $witness_flag = 0;
202 }
203  
204 507 my $input_count = unpack_compactsize $serialized, \$pos;
205 507 my @inputs;
206 507 for (1 .. $input_count) {
207 633 push @inputs, Bitcoin::Crypto::Transaction::Input->from_serialized(
208 $serialized, pos => \$pos
209 );
210 }
211  
212 507 my $output_count = unpack_compactsize $serialized, \$pos;
213 507 my @outputs;
214 507 for (1 .. $output_count) {
215 797 push @outputs, Bitcoin::Crypto::Transaction::Output->from_serialized(
216 $serialized, pos => \$pos
217 );
218 }
219  
220 507 ▶ if ($witness_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($witness_flag) 72 435 507
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 507 my $locktime = unpack "\@$pos V", $serialized;
236 507 $pos += 4;
237  
238 507 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $serialized 0 507 507
Condition66%2/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
464610
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 507 ▶ ${$args->{pos}} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if $partial 46 461 507
243 if $partial;
244  
245 507 my $tx = $class->new(
246 version => $version,
247 locktime => $locktime,
248 had_witness_flag => $witness_flag,
249 );
250  
251 507 @{$tx->inputs} = @inputs;
252 507 @{$tx->outputs} = @outputs;
253  
254 507 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 147 ▶{
Subroutine get_hash: called CC=1 SCAR=0.0
268 147 my ($self, $args) = @_;
269  
270 147 return scalar reverse hash256($self->to_serialized(%$args));
271 }
272  
273 sub txid
274 1 ▶{
Subroutine txid: called CC=1 SCAR=0.0
Pod documented
275 1 my ($self) = @_;
276  
277 1 return $self->get_hash(witness => 0);
278 }
279  
280 sub wtxid
281 0 ▶{
Subroutine wtxid: not called CC=1 SCAR=6.9
Pod documented
282 0 my ($self) = @_;
283  
284 0 return $self->get_hash(witness => 1);
285 }
286  
287 sub get_digest
288 19 ▶{
Subroutine get_digest: called CC=1 SCAR=0.0
Pod documented
289 19 my ($self, %params) = @_;
290  
291 19 return $self->get_digest_object(%params)->get_digest;
292 }
293  
294 sub get_digest_object
295 213 ▶{
Subroutine get_digest_object: called CC=1 SCAR=0.0
Pod documented
296 213 my ($self, %params) = @_;
297  
298 213 my $digest = $self->_digest_object;
299 213 $digest->set_config(\%params);
300 213 return $digest;
301 }
302  
303 sub fee
304 119 ▶{
Subroutine fee: called CC=4 SCAR=13.9
Pod documented
305 119 my ($self, $require_value) = @_;
306  
307 119 my $value = 0;
308  
309 119 try {
310 119 foreach my $input (@{$self->inputs}) {
311 164 $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 117 foreach my $output (@{$self->outputs}) {
322 166 $value -= $output->value;
323 }
324  
325 117 return $value;
326 }
327  
328 sub fee_rate
329 2 ▶{
Subroutine fee_rate: called CC=2 SCAR=7.0
Pod documented
330 2 my ($self) = @_;
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 2 ▶{
Subroutine set_rbf: called CC=3 SCAR=11.0
Pod documented
343 2 my ($self) = @_;
344  
345 # rules according to BIP125
346 # https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
347 2 ▶ if (!$self->has_rbf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_rbf) 2 0 2
348 2 my $input = $self->inputs->[0];
349  
350 2 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 1 1 2
351 'set_rbf can only be called on a transaction with at least one input'
352 ) unless defined $input;
353  
354 1 $input->set_sequence_no(RBF_SEQUENCE_NO_THRESHOLD);
355 }
356  
357 1 return $self;
358 }
359  
360 sub has_rbf
361 5 ▶{
Subroutine has_rbf: called CC=3 SCAR=11.0
Pod documented
362 5 my ($self) = @_;
363  
364 5 foreach my $input (@{$self->inputs}) {
365 4 ▶ return !!1
Branch: 1 branch
BranchTrueFalseTotal
if $input->sequence_no <= 4294967293 1 3 4
366 if $input->sequence_no <= RBF_SEQUENCE_NO_THRESHOLD;
367 }
368  
369 4 return !!0;
370 }
371  
372 sub virtual_size
373 4 ▶{
Subroutine virtual_size: called CC=1 SCAR=0.0
Pod documented
374 4 my ($self) = @_;
375  
376 4 my $base = length $self->to_serialized(witness => 0);
377 4 my $with_witness = length $self->to_serialized;
378 4 my $witness = $with_witness - $base;
379  
380 4 return $base + $witness / 4;
381 }
382  
383 sub weight
384 2 ▶{
Subroutine weight: called CC=1 SCAR=0.0
Pod documented
385 2 my ($self) = @_;
386  
387 2 my $base = length $self->to_serialized(witness => 0);
388 2 my $with_witness = length $self->to_serialized;
389 2 my $witness = $with_witness - $base;
390  
391 2 return $base * 4 + $witness;
392 }
393  
394 sub update_utxos
395 4 ▶{
Subroutine update_utxos: called CC=5 SCAR=16.1
Pod documented
396 4 my ($self) = @_;
397  
398 4 foreach my $input (@{$self->inputs}) {
399 7 ▶ $input->utxo->unregister if $input->utxo_registered;
Branch: 1 branch
BranchTrueFalseTotal
if $input->utxo_registered 2 5 7
400 }
401  
402 4 foreach my $output_index (0 .. $#{$self->outputs}) {
403 8 my $output = $self->outputs->[$output_index];
404  
405 8 ▶ btc_utxo->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_block ? : 0 8 8
406 txid => $self->get_hash,
407 output_index => $output_index,
408 output => $output,
409 ($self->has_block ? (block => $self->block) : ()),
410 )->register;
411 }
412  
413 4 return $self;
414 }
415  
416 sub is_coinbase
417 124 ▶{
Subroutine is_coinbase: called CC=3 SCAR=11.0
Pod documented
418 124 my ($self) = @_;
419 124 my $inputs = $self->inputs;
420  
421 124 ▶ return !!0 unless @{$inputs} == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$inputs;} == 1 26 98 124
422  
423 98 my $null_prevout = NULL_UTXO;
424 98 my $utxo_prevout = $inputs->[0]->utxo_location;
425 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
426 }
427  
428 sub _verify_script_default
429 90 ▶{
Subroutine _verify_script_default: called CC=13 SCAR=25.7
430 90 my ($self, $input, $script_runner) = @_;
431 90 my $locking_script = $input->utxo->output->locking_script;
432  
433 90 ▶ 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
95724
$locking_script->type // ""
l!l
5130
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
434 90 ▶ 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
24264
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
435  
436 90 ▶ Bitcoin::Crypto::Exception::TransactionScript->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $is_pushes_only and not $input->signature_script->is_pushes_only 1 89 90
Condition100%3/3
$is_pushes_only and not $input->signature_script->is_pushes_only
!ll&&!rl&&r
64251
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
437 'signature script must only contain push opcodes'
438 ) if $is_pushes_only && !$input->signature_script->is_pushes_only;
439  
440 # execute input to get initial stack
441 89 $script_runner->execute($input->signature_script);
442 88 my @stack = @{$script_runner->stack};
443 88 my @locking_stack;
444 my $redeem_script;
445  
446 88 ▶ if ($is_p2sh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_p2sh) { } 24 64 88
447 24 $redeem_script = pop @stack;
448 24 ▶ @locking_stack = ($redeem_script)
Branch: 1 branch
BranchTrueFalseTotal
if defined $redeem_script 24 0 24
449 if defined $redeem_script;
450 }
451 else {
452 64 @locking_stack = @stack;
453 }
454  
455 # execute previous output
456 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
457 88 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
458 88 $script_runner->execute($locking_script, \@locking_stack);
459 68 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 2 66 68
460 unless $script_runner->success;
461 },
462 88 'locking script'
463 );
464  
465 66 ▶ if (defined $redeem_script) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redeem_script) 24 42 66
466 24 $redeem_script = btc_script->from_serialized($redeem_script);
467  
468 24 ▶ my $type = $redeem_script->type // '';
Condition100%2/2
$redeem_script->type // ""
l!l
231
MC/DC100%1/1
$redeem_script->type // ""
$redeem_script->type
Truth table100%2/2
A
A= $redeem_script->type
Aresult
11
00
469 24 ▶ 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 8 24
Condition100%6/6
$script_runner->flags->segwit and $type eq "P2WPKH" || $type eq "P2WSH"
!ll&&!rl&&r
1716
$type eq "P2WPKH" or $type eq "P2WSH"
l!l&&r!l&&!r
1067
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
470 16 $self->_verify_script_segwit($input, $script_runner, $redeem_script);
471 }
472 else {
473 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
474 8 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
475 8 $script_runner->execute($redeem_script, \@stack);
476 7 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 1 6 7
477 unless $script_runner->success;
478 },
479 8 'redeem script'
480 );
481 }
482 }
483 }
484  
485 sub _verify_script_segwit
486 41 ▶{
Subroutine _verify_script_segwit: called CC=11 SCAR=24.4
487 41 my ($self, $input, $script_runner, $compat_script) = @_;
488  
489 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
490 unless $compat_script || $input->signature_script->is_empty;
491  
492 # use shallow copy of witness as initial stack
493 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
494 41 my $redeem_script;
495 my @locking_stack;
496  
497 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
498 41 my $hash = substr $locking_script->to_serialized, 2;
499 41 my $actual_locking_script;
500 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
501 17 $actual_locking_script = Bitcoin::Crypto::Script::Common->new(PKH => $hash);
502 17 @locking_stack = @stack;
503 }
504 elsif ($locking_script->type eq 'P2WSH') {
505 21 $actual_locking_script = Bitcoin::Crypto::Script::Common->new(WSH => $hash);
506 21 $redeem_script = pop @stack;
507 21 ▶ @locking_stack = ($redeem_script) if defined $redeem_script;
Branch: 1 branch
BranchTrueFalseTotal
if defined $redeem_script 21 0 21
508 }
509 else {
510 # not a known segwit version 0 output
511  
512 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
513 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
514 2 return;
515 }
516  
517 # execute locking script
518 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
519 38 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
520 38 $script_runner->execute($actual_locking_script, \@locking_stack);
521 34 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 0 34 34
522 unless $script_runner->success;
523 },
524 38 'segwit locking script'
525 );
526  
527 # execute redeem script
528 34 ▶ if (defined $redeem_script) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redeem_script) 21 13 34
529 21 $redeem_script = btc_script->from_serialized($redeem_script);
530  
531 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
532 21 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
533 21 $script_runner->execute($redeem_script, \@stack);
534 16 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 0 16 16
535 unless $script_runner->success;
536 },
537 21 'segwit redeem script'
538 );
539 }
540 }
541  
542 sub _verify_script_taproot
543 28 ▶{
Subroutine _verify_script_taproot: called CC=10 SCAR=24.1
544 28 my ($self, $input, $script_runner) = @_;
545  
546 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
547 unless $input->signature_script->is_empty;
548  
549 28 ▶ die_no_trace 'witness stack has 0 elements'
Branch: 1 branch
BranchTrueFalseTotal
unless $input->has_witness 0 28 28
550 unless $input->has_witness;
551  
552 # shallow copy of the witness - avoid modifying the transaction
553 28 my @witness_stack = @{$input->witness};
554 28 my $pubkey = $input->utxo->output->locking_script->get_raw_address;
555  
556 # consensus rules from https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#script-validation-rules
557  
558 # remove the annex from the witness stack - annex first byte is 0x50
559 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
560 if @witness_stack >= 2 && (unpack('C', $witness_stack[-1]) // 0) == 0x50;
561  
562 28 my $script;
563 28 ▶ if (@witness_stack == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@witness_stack == 1) { } 20 8 28
564 20 $script = Bitcoin::Crypto::Script::Common->new(TR => $pubkey);
565 }
566 else {
567 my $control_block = Bitcoin::Crypto::Exception->trap_into(
568 8 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
569 8 return Bitcoin::Crypto::Transaction::ControlBlock->from_serialized(pop @witness_stack);
570 },
571 8 'invalid control block'
572 );
573  
574 8 my $raw_script = pop @witness_stack;
575  
576 8 my $leaf_version = $control_block->get_leaf_version;
577 8 ▶ if ($leaf_version == TAPSCRIPT_LEAF_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leaf_version == 192) { } 8 0 8
578 8 $script = btc_tapscript->from_serialized($raw_script);
579 8 $script_runner->transaction->set_taproot_ext_flag(1);
580 }
581 else {
582 # future compatibility - script must succeed
583 # https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-12
584 0 return;
585 }
586  
587 # TODO: for now, leaf must have id 0 to get recognized by runner (see
588 # OP_CHECKSIG for tapscript)
589 8 my $tree = btc_script_tree->from_path(
590 {
591 id => 0,
592 leaf_version => $leaf_version,
593 script => $script,
594 },
595 $control_block->script_blocks
596 );
597  
598 8 $script_runner->transaction->set_script_tree($tree);
599  
600 8 my $tweaked = $control_block->public_key->get_taproot_output_key($tree->get_merkle_root);
601 8 my $expected_parity = !has_even_y($tweaked);
602 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
603 unless $tweaked->get_xonly_key eq $pubkey
604 && $expected_parity == ($control_block->control_byte & 1);
605 }
606  
607 # execute script
608 # use remaining witness elements as initial stack
609 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
610 28 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
611 28 $script_runner->transaction->set_sigop_budget(length $input->serialized_witness);
612 28 $script_runner->execute($script, \@witness_stack);
613 25 ▶ die_no_trace 'execution yielded failure'
Branch: 1 branch
BranchTrueFalseTotal
unless $script_runner->success 0 25 25
614 unless $script_runner->success;
615 },
616 28 'taproot script'
617 );
618 }
619  
620 sub verify_script
621 143 ▶{
Subroutine verify_script: called CC=5 SCAR=16.1
Pod undocumented
622 143 my ($self, $input_index, $script_runner) = @_;
623 143 $script_runner->transaction->_clear;
624 143 $script_runner->transaction->set_input_index($input_index);
625  
626 143 my $input = $self->inputs->[$input_index];
627 143 my $utxo = $input->utxo;
628  
629 # run bitcoin script
630 143 my $procedure = '_verify_script_default';
631 143 ▶ 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 115 143
elsif ($script_runner->flags->segwit and $utxo->output->locking_script->is_native_segwit) { } 25 98 123
Condition100%3/3
$script_runner->flags->taproot and $utxo->output->locking_script->is_taproot
!ll&&!rl&&r
810728
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
632 28 $procedure = '_verify_script_taproot';
633 }
634 ▶ 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
88225
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
635 25 $procedure = '_verify_script_segwit';
636 }
637  
638 Bitcoin::Crypto::Exception::TransactionScript->trap_into(
639 143 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
640 143 ▶ die_no_trace 'witness in non-witness input'
Branch: 1 branch
BranchTrueFalseTotal
if not $input->is_segwit and $input->has_witness 0 143 143
Condition66%2/3
not $input->is_segwit and $input->has_witness
!ll&&!rl&&r
70730
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
641 if !$input->is_segwit && $input->has_witness;
642  
643 143 $self->$procedure($input, $script_runner);
644 },
645 143 "transaction input $input_index verification has failed"
646 );
647 }
648  
649 sub _verify_coinbase
650 0 ▶{
Subroutine _verify_coinbase: not called CC=6 SCAR=37.4
651 0 my ($self, $script_runner) = @_;
652 0 my $block = $self->block;
653  
654 Bitcoin::Crypto::Exception::Transaction->raise(
655 'coinbase transaction must have one input'
656 0 ▶ ) if @{$self->inputs} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->inputs;} != 1 0 0 0
657  
658 0 my $coinbase_data = $self->inputs->[0]->signature_script->to_serialized;
659  
660 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
661 'coinbase data not between 2 and 100 bytes'
662 ) if length $coinbase_data < 2 || length $coinbase_data > 100;
663  
664 0 ▶ if (!defined $block) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $block) 0 0 0
665 0 carp 'trying to verify coinbase transaction but block was not set';
666 0 return;
667 }
668  
669 Bitcoin::Crypto::Exception::Transaction->raise(
670 0 ▶ 'coinbase must be the first transaction in a block'
Branch: 1 branch
BranchTrueFalseTotal
unless $block->transactions->[0] == $self 0 0 0
671 ) unless $block->transactions->[0] == $self;
672  
673 # NOTE: most of other coinbase verification (like block reward checking)
674 # will be done in a block
675  
676 0 return;
677 }
678  
679 34 signature_for verify => (
680 method => !!1,
681 named => [
682 block => Maybe [InstanceOf ['Bitcoin::Crypto::Block']],
683 {default => undef},
684 flags => TransactionFlags,
685 {default => undef},
686 ],
687 bless => !!0,
688 );
689  
690 sub verify
691 162 ▶{
Subroutine verify: called CC=28 SCAR=36.6
692 0 my ($self, $args) = @_;
693  
694 0 ▶ if ($args->{block}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"block"}) 0 113 113
695 113 carp 'passing block parameter to verify method is deprecated: call set_block on transaction instead';
696 113 $self->set_block($args->{block});
697 }
698  
699 113 my $block = $self->block;
700 113 my $inputs = $self->inputs;
701 113 my $outputs = $self->outputs;
702  
703 113 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @$inputs 0 113 113
704 'transaction has no inputs'
705 ) if !@$inputs;
706  
707 113 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @$outputs 0 113 113
708 'transaction has no outputs'
709 ) if !@$outputs;
710  
711 my $script_runner = Bitcoin::Crypto::Script::Runner->new(
712 transaction => $self,
713 flags => $args->{flags},
714 113 );
715  
716 # check output values
717 Bitcoin::Crypto::Exception::Transaction->raise(
718 'output value is out of range'
719 113 ▶ ) 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 113 113
720  
721 # use special procedure if coinbase
722 158 ▶ return $self->_verify_coinbase($script_runner)
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_coinbase 0 113 113
723 if $self->is_coinbase;
724  
725 Bitcoin::Crypto::Exception::Transaction->raise(
726 'transaction has duplicate inputs'
727 113 ▶ ) if @$inputs != uniqstr map { $_->prevout } @$inputs;
Branch: 1 branch
BranchTrueFalseTotal
if @$inputs != &uniqstr(map({$_->prevout;} @$inputs)) 0 113 113
728  
729 18 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if 1 111 112
730 'output value exceeds input'
731 ) if $self->fee(!!1) < 0;
732  
733 # locktime checking
734 18 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->locktime > 0 and &any(sub { $_->sequence_no != 4294967295; } , @$inputs)) 18 93 111
Condition66%2/3
$self->locktime > 0 and &any(sub { $_->sequence_no != 4294967295; } , @$inputs)
!ll&&!rl&&r
93018
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
735 $self->locktime > 0 && any {
736 $_->sequence_no != MAX_SEQUENCE_NO
737 } @$inputs
738 )
739 {
740 18 my $locktime = $self->locktime;
741 18 my $is_timestamp = $locktime >= LOCKTIME_HEIGHT_THRESHOLD;
742 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
743 109 ▶ 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
744 'locktime was not satisfied'
745 ) if $locktime > ($is_timestamp ? $block->median_time_past : $block->height);
746 }
747 else {
748 109 carp 'trying to verify locktime but block was not set';
749 }
750 }
751  
752 # per-input verification
753 143 foreach my $input_index (keys @{$inputs}) {
754 7 $self->verify_script($input_index, $script_runner);
755  
756 # check sequence (BIP 68)
757 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 97 104
Condition100%3/3
$self->version >= 2 and not $inputs->[$input_index]->sequence_no & 2147483648
!ll&&!rl&&r
68297
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
758 7 my $sequence = $inputs->[$input_index]->sequence_no;
759 7 my $utxo = $inputs->[$input_index]->utxo;
760 7 my $time_based = $sequence & (1 << 22);
761 7 my $relative_locktime = $sequence & 0x0000ffff;
762 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
763 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
764  
765 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
766 7 my $utxo_block = $utxo->block;
767 7 ▶ my $now = $time_based ? $block->median_time_past : $block->height;
Branch: 1 branch
BranchTrueFalseTotal
$time_based ? : 2 5 7
768 7 ▶ my $then = $time_based ? $utxo_block->median_time_past : $utxo_block->height;
Branch: 1 branch
BranchTrueFalseTotal
$time_based ? : 2 5 7
769 0 ▶ $relative_locktime <<= 9 if $time_based;
Branch: 1 branch
BranchTrueFalseTotal
if $time_based 2 5 7
770  
771 68 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $now < $then + $relative_locktime 2 5 7
772 'relative locktime was not satisfied'
773 ) if $now < $then + $relative_locktime;
774 }
775 else {
776 162 carp 'trying to verify relative locktime but block or utxo block was not set';
777 }
778 }
779 }
780  
781 # TODO: check if coinbase transaction within 100 blocks is spent in any input
782  
783 # TODO: check outputs
784 # - do they have non-zero values (other than OP_RETURN)?
785 # - do OP_RETURN scripts keep data limit
786 # - do they have valid script
787  
788 18 return;
789 }
790  
791 sub verify_standard
792 1 ▶{
Subroutine verify_standard: called CC=1 SCAR=0.0
Pod documented
793 1 my ($self, @args) = @_;
794  
795 1 return $self->verify(
796 @args,
797 flags => Bitcoin::Crypto::Transaction::Flags->new_full
798 );
799 }
800  
801 sub sign
802 48 ▶{
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
803 48 my ($self, %args) = @_;
804  
805 48 return Bitcoin::Crypto::Transaction::Signer->new_impl($self, \%args);
806 }
807  
808 sub dump
809 1 ▶{
Subroutine dump: called CC=6 SCAR=18.0
Pod documented
810 1 my ($self) = @_;
811  
812 1 my $fee = $self->fee;
813 1 ▶ my $fee_rate = defined $fee ? int($self->fee_rate * 100) / 100 : '??';
Branch: 1 branch
BranchTrueFalseTotal
defined $fee ? : 1 0 1
814 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
815  
816 1 my @result;
817 1 push @result, 'Transaction ' . to_format [hex => $self->get_hash];
818 1 push @result, 'version: ' . $self->version;
819 1 push @result, 'size: ' . $self->virtual_size . 'vB, ' . $self->weight . 'WU';
820 1 push @result, "fee: $fee sat (~$fee_rate sat/vB)";
821 1 ▶ push @result, 'replace-by-fee: ' . ($self->has_rbf ? 'yes' : 'no');
Branch: 1 branch
BranchTrueFalseTotal
$self->has_rbf ? : 0 1 1
822 1 push @result, 'locktime: ' . $self->locktime;
823 1 push @result, '';
824  
825 1 push @result, @{$self->inputs} . ' inputs:';
826 1 foreach my $input (@{$self->inputs}) {
827 1 push @result, $input->dump;
828 1 push @result, '';
829 }
830  
831 1 push @result, @{$self->outputs} . ' outputs:';
832 1 foreach my $output (@{$self->outputs}) {
833 1 push @result, $output->dump;
834 1 push @result, '';
835 }
836  
837 1 return join "\n", @result;
838 }
839  
840 34 1;
841  
842 __END__
« Crypto/Script/Recognition.pm ↑ Summary Crypto/Helpers.pm »