Crypto/PSBT.pm

filter: Statementstmt 99.6% 258 / 259 Branchbran 84.1% 101 / 120 Conditioncond 81.6% 40 / 49 MC/DCmcdc 67.8% 19 / 28 Subroutinesub 100% 32 / 32 Podpod 100% 11 / 11 totaltotal 92.3% 461 / 499 CC 99 SCAR 20.5
CC
99
Cov
93%
check
15.1
get_locktime
14.0
_check_can_modify
11.1
CRAP 7.8
« Crypto/Util/Internal.pm ↑ Summary Crypto/Transaction/Input.pm »
1 package Bitcoin::Crypto::PSBT;
2 12 $Bitcoin::Crypto::PSBT::VERSION = '4.006';
3 12 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶use Bitcoin::Crypto qw(btc_transaction btc_utxo);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Bitcoin::Crypto::PSBT::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Bitcoin::Crypto::PSBT::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Bitcoin::Crypto::PSBT::FieldType;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Bitcoin::Crypto::Constants qw(:psbt);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use Bitcoin::Crypto::Util qw(to_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 12 has param 'strict' => (
21 isa => Bool,
22 default => !!0,
23 writer => 1,
24 );
25  
26 has field 'maps' => (
27 isa => ArrayRef [InstanceOf ['Bitcoin::Crypto::PSBT::Map']],
28 163 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 12 );
30  
31 12 with qw(
32 Bitcoin::Crypto::Role::PSBT::Signer
33 Bitcoin::Crypto::Role::PSBT::Finalizer
34 Bitcoin::Crypto::Role::PSBT::Combiner
35 );
36  
37 sub _check_can_modify
38 243 ▶{
Subroutine _check_can_modify: called CC=11 SCAR=24.1
39 243 my ($self, $type, $index) = @_;
40  
41 243 ▶ return unless $type eq PSBT_INPUT_MAP || $type eq PSBT_OUTPUT_MAP;
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "in" or $type eq "out" 76 167 243
Condition100%3/3
$type eq "in" or $type eq "out"
l!l&&r!l&&!r
887976
MC/DC100%2/2
$type eq "in" or $type eq "out"
$type eq "in" $type eq "out"
Truth table100%3/3
A or B
A= $type eq "in"B= $type eq "out"
ABresult
1X1
011
000
42 167 ▶ return unless $self->strict;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->strict 159 8 167
43  
44 8 ▶ my $count = $type eq PSBT_INPUT_MAP ? $self->input_count : $self->output_count;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'in' ? : 6 2 8
45 8 ▶ return unless $index >= $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $index >= $count 3 5 8
46  
47 5 ▶ if ($self->version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->version == 0) { } 2 3 5
elsif ($self->version == 2) { } 3 0 3
48 2 Bitcoin::Crypto::Exception::PSBT->raise(
49 "cannot add a new map entry because index is out of range of PSBT_GLOBAL_UNSIGNED_TX ($count)"
50 );
51 }
52 elsif ($self->version == 2) {
53 3 my $modifiable = $self->get_all_fields('PSBT_GLOBAL_TX_MODIFIABLE');
54 3 ▶ my $field_name = $type eq PSBT_INPUT_MAP ? 'inputs_modifiable' : 'outputs_modifiable';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'in' ? : 3 0 3
55  
56 Bitcoin::Crypto::Exception::PSBT->raise(
57 "cannot add a new map entry because PSBT_GLOBAL_TX_MODIFIABLE ($field_name) is off"
58 3 ▶ ) unless $modifiable && $modifiable->value->{$field_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $modifiable and $modifiable->value->{$field_name} 2 1 3
Condition66%2/3
$modifiable and $modifiable->value->{$field_name}
!ll&&!rl&&r
201
MC/DC50%1/2
$modifiable and $modifiable->value->{$field_name}
$modifiable $modifiable->value->{$field_name}
Truth table66%2/3
A and B
A= $modifiableB= $modifiable->value->{$field_name}
ABresult
0X0
100
111
59 }
60 }
61  
62 sub _get_map
63 5211 ▶{
Subroutine _get_map: called CC=9 SCAR=22.0
64 5211 my ($self, $maptype, %args) = @_;
65  
66 5211 my $found_map;
67 5211 foreach my $map (@{$self->maps}) {
68 8194 ▶ next unless $map->type eq $maptype;
Branch: 1 branch
BranchTrueFalseTotal
unless $map->type eq $maptype 2967 5227 8194
69 5227 ▶ next if $map->need_index && (!defined $args{index} || $map->index ne $args{index});
Branch: 1 branch
BranchTrueFalseTotal
if $map->need_index and !defined($args{"index"}) || $map->index ne $args{"index"} 290 4937 5227
Condition83%5/6
$map->need_index and !defined($args{"index"}) || $map->index ne $args{"index"}
!ll&&!rl&&r
29192018290
not defined $args{"index"} or $map->index ne $args{"index"}
l!l&&r!l&&!r
02902018
MC/DC66%2/3
$map->need_index and !defined($args{"index"}) || $map->index ne $args{"index"}
$map->need_index not defined $args{"index"} $map->index ne $args{"index"}
Truth table75%3/4
A and not(B) or C
A= $map->need_indexB= not defined $args{"index"}C= $map->index ne $args{"index"}
ABCresult
0XX0
1000
11X1
1011
70  
71 4937 $found_map = $map;
72 4937 last;
73 }
74  
75 5211 ▶ if (!$found_map && $args{set}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found_map and $args{"set"}) 252 4959 5211
Condition100%3/3
not $found_map and $args{"set"}
!ll&&!rl&&r
493722252
MC/DC100%2/2
not $found_map and $args{"set"}
not $found_map $args{"set"}
Truth table100%3/3
not(A) and B
A= not $found_mapB= $args{"set"}
ABresult
0X0
100
111
76 $found_map = Bitcoin::Crypto::PSBT::Map::->new(
77 type => $maptype,
78 index => $args{index},
79 252 );
80  
81 $self->_check_can_modify($found_map->type, $args{index})
82 252 ▶ if $args{check};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"check"} 243 9 252
83  
84 248 push @{$self->maps}, $found_map;
85 }
86  
87 5207 return $found_map;
88 }
89  
90 sub input_count
91 552 ▶{
Subroutine input_count: called CC=5 SCAR=16.3
Pod documented
92 552 my ($self) = @_;
93 552 my $version = $self->version;
94  
95 552 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 151 401 552
elsif ($version == 2) { } 401 0 401
96 151 my $tx = $self->get_field('PSBT_GLOBAL_UNSIGNED_TX');
97 149 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $tx 0 149 149
98 'cannot calculate input_count: no PSBT_GLOBAL_UNSIGNED_TX field'
99 ) unless $tx;
100  
101 149 return scalar @{$tx->value->inputs};
102 }
103 elsif ($version == 2) {
104 401 my $count = $self->get_field('PSBT_GLOBAL_INPUT_COUNT');
105 399 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 399 399
106 'cannot calculate input_count: no PSBT_GLOBAL_INPUT_COUNT field'
107 ) unless $count;
108 399 return $count->value;
109 }
110 }
111  
112 sub output_count
113 310 ▶{
Subroutine output_count: called CC=5 SCAR=16.3
Pod documented
114 310 my ($self) = @_;
115 310 my $version = $self->version;
116  
117 310 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 125 185 310
elsif ($version == 2) { } 185 0 185
118 125 my $tx = $self->get_field('PSBT_GLOBAL_UNSIGNED_TX');
119 125 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $tx 0 125 125
120 'cannot calculate output_count: no PSBT_GLOBAL_UNSIGNED_TX field'
121 ) unless $tx;
122  
123 125 return scalar @{$tx->value->outputs};
124 }
125 elsif ($version == 2) {
126 185 my $count = $self->get_field('PSBT_GLOBAL_OUTPUT_COUNT');
127 184 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 184 184
128 'cannot calculate output_count: no PSBT_GLOBAL_OUTPUT_COUNT field'
129 ) unless $count;
130 184 return $count->value;
131 }
132 }
133  
134 12 signature_for get_field => (
135 method => !!1,
136 positional => [
137 PSBTFieldType,
138 Maybe [PositiveOrZeroInt], {default => undef},
139 Maybe [ByteStr], {default => undef}
140 ],
141 );
142  
143 sub get_field
144 1254 ▶{
Subroutine get_field: called CC=4 SCAR=14.0
145 1254 my ($self, $type, $index, $key) = @_;
146  
147 1254 my @values = $self->get_all_fields($type, $index, $key);
148 1254 ▶ if (@values != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values != 1) 26 1228 1254
149 26 my $id = $type->name;
150 30 ▶ $id .= ", index $index" if defined $index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $index 0 5 5
151 26 ▶ $id .= ', key ' . to_format [hex => $key] if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 5 5
152  
153 5 my $count = @values;
154  
155 5 Bitcoin::Crypto::Exception::PSBT->raise(
156 "cannot get value for field $id: found $count values in PSBT"
157 );
158 }
159  
160 1228 return $values[0];
161 }
162  
163 12 signature_for get_all_fields => (
164 method => !!1,
165 positional => [
166 PSBTFieldType,
167 Maybe [PositiveOrZeroInt], {default => undef},
168 Maybe [ByteStr], {default => undef}
169 ],
170 );
171  
172 sub get_all_fields
173 3545 ▶{
Subroutine get_all_fields: called CC=2 SCAR=6.9
174 3545 my ($self, $type, $index, $key) = @_;
175  
176 3545 my $map = $self->_get_map($type->map_type, index => $index);
177 3545 ▶ return () unless $map;
Branch: 1 branch
BranchTrueFalseTotal
unless $map 22 3523 3545
178 3523 return $map->find($type, $key);
179 }
180  
181 12 signature_for remove_all_fields => (
182 method => !!1,
183 positional => [
184 PSBTFieldType,
185 Maybe [PositiveOrZeroInt], {default => undef},
186 Maybe [ByteStr], {default => undef}
187 ],
188 );
189  
190 sub remove_all_fields
191 {
192 my ($self, $type, $index, $key) = @_;
193  
194 my $map = $self->_get_map($type->map_type, index => $index);
195 if ($map) {
196 foreach my $field ($map->find($type, $key)) {
197 $map->remove($field);
198 }
199 }
200  
201 return $self;
202 }
203  
204 sub add_field
205 765 ▶{
Subroutine add_field: called CC=5 SCAR=16.2
Pod documented
206 765 my ($self, @data) = @_;
207 765 my $field;
208 my $index;
209  
210 765 ▶ if ((@data == 1 || @data == 2) && blessed $data[0] && $data[0]->isa('Bitcoin::Crypto::PSBT::Field')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data == 1 || @data == 2 and builtin::blessed($data[0]) and $data[0]->isa('Bitcoin::Crypto::PSBT::Field')) { } 336 429 765
Condition66%6/9
@data == 1 || @data == 2 and builtin::blessed($data[0]) and $data[0]->isa("Bitcoin::Crypto::PSBT::Field")
!ll&&!rl&&r
4290336
@data == 1 || @data == 2 and builtin::blessed($data[0])
!ll&&!rl&&r
4290336
@data == 1 or @data == 2
l!l&&r!l&&!r
0336429
MC/DC25%1/4
@data == 1 || @data == 2 and builtin::blessed($data[0]) and $data[0]->isa("Bitcoin::Crypto::PSBT::Field")
@data == 1 @data == 2 builtin::blessed($data[0]) $data[0]->isa("Bitcoin::Crypto::PSBT::Field")
Truth table28%2/7
A or B and C and D
A= @data == 1B= @data == 2C= builtin::blessed($data[0])D= $data[0]->isa("Bitcoin::Crypto::PSBT::Field")
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
211 336 ($field, $index) = @data;
212 }
213 else {
214 429 my %data = @data;
215 429 $index = delete $data{index};
216 429 $field = Bitcoin::Crypto::PSBT::Field->new(%data);
217 }
218  
219 765 my $map = $self->_get_map($field->type->map_type, index => $index, set => !!1, check => !!1);
220 761 $map->add($field);
221  
222 761 return $self;
223 }
224  
225 sub list_fields
226 61 ▶{
Subroutine list_fields: called CC=7 SCAR=19.6
Pod documented
227 61 my ($self) = @_;
228  
229 61 state $order = {
230 (PSBT_GLOBAL_MAP) => 0,
231 (PSBT_INPUT_MAP) => 1,
232 (PSBT_OUTPUT_MAP) => 2,
233 };
234  
235 # make sure the order of the maps will always be the same: global, input,
236 # output, sorted by index
237 my @maps = sort {
238 216 ▶ $order->{$a->type} <=> $order->{$b->type}
Condition57%4/7
$$order{$a->type} <=> $$order{$b->type} || ($a->index // 0) <=> ($b->index // 0)
l!l&&r!l&&!r
189270
$a->index // 0
l!l
270
$b->index // 0
l!l
270
MC/DC0%0/2
$$order{$a->type} <=> $$order{$b->type} || ($a->index // 0) <=> ($b->index // 0)
$$order{$a->type} <=> $$order{$b->type} $a->index
MC/DC0%0/1
$b->index // 0
$b->index
Truth table66%2/3
A || B
A= $$order{$a->type} <=> $$order{$b->type}B= $a->index
ABresult
1X1
011
000
Truth table50%1/2
A
A= $b->index
Aresult
11
00
239 || ($a->index // 0) <=> ($b->index // 0)
240 61 } @{$self->maps};
241  
242 61 my @results;
243 61 foreach my $map (@maps) {
244 208 my $index = $map->index;
245 208 my %seen;
246  
247 208 foreach my $field (@{$map->fields}) {
248 681 ▶ next if $seen{$field->type->code}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$field->type->code}++ 22 659 681
249  
250 659 push @results, [$field->type, $index];
251 }
252 }
253  
254 # force list context to have same UI as get_all_fields
255 61 return @results[0 .. $#results];
256 }
257  
258 sub version
259 1185 ▶{
Subroutine version: called CC=3 SCAR=11.0
Pod documented
260 1185 my ($self) = @_;
261  
262 1185 my $version = $self->get_all_fields('PSBT_GLOBAL_VERSION');
263 1185 ▶ $version = $version ? $version->value : 0;
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 845 340 1185
264  
265 Bitcoin::Crypto::Exception::PSBT->raise(
266 "PSBT version $version is not supported"
267 1185 ▶ ) unless any { $_ == $version } 0, 2;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ == $version; } , 0, 2) 0 1185 1185
268  
269 1185 return $version;
270 }
271  
272 sub get_id
273 11 ▶{
Subroutine get_id: called CC=3 SCAR=11.0
Pod documented
274 11 my ($self) = @_;
275  
276 11 my $version = $self->version;
277 11 ▶ if ($version == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 0) { } 1 10 11
278 1 return $self->get_field('PSBT_GLOBAL_UNSIGNED_TX')->value->txid;
279 }
280 else {
281 10 my $tx = $self->get_transaction;
282  
283 # remove signature and sequence number. Witness is irrelevant, as we
284 # calculate txid, not wtxid
285 10 foreach my $input (@{$tx->inputs}) {
286 10 $input->set_signature_script('');
287 10 $input->set_sequence_no(0);
288 }
289  
290 10 return $tx->txid;
291 }
292 }
293  
294 12 signature_for from_serialized => (
295 method => !!1,
296 positional => [ByteStr],
297 );
298  
299 sub from_serialized
300 {
301 my ($class, $serialized) = @_;
302 my $self = $class->new;
303  
304 my $pos = length PSBT_MAGIC;
305 my $magic = substr $serialized, 0, $pos;
306  
307 Bitcoin::Crypto::Exception::PSBT->raise(
308 'serialized string does not contain the PSBT header'
309 ) unless $magic eq PSBT_MAGIC;
310  
311 push @{$self->maps}, Bitcoin::Crypto::PSBT::Map::->from_serialized(
312 $serialized,
313 map_type => PSBT_GLOBAL_MAP,
314 pos => \$pos,
315 );
316  
317 foreach my $index (0 .. $self->input_count - 1) {
318 push @{$self->maps}, Bitcoin::Crypto::PSBT::Map::->from_serialized(
319 $serialized,
320 map_type => PSBT_INPUT_MAP,
321 pos => \$pos,
322 index => $index,
323 );
324 }
325  
326 foreach my $index (0 .. $self->output_count - 1) {
327 push @{$self->maps}, Bitcoin::Crypto::PSBT::Map::->from_serialized(
328 $serialized,
329 map_type => PSBT_OUTPUT_MAP,
330 pos => \$pos,
331 index => $index,
332 );
333 }
334  
335 Bitcoin::Crypto::Exception::PSBT->raise(
336 'serialized PSBT data is corrupted'
337 ) if $pos != length $serialized;
338  
339 $self->check;
340  
341 return $self;
342 }
343  
344 sub to_serialized
345 116 ▶{
Subroutine to_serialized: called CC=3 SCAR=11.0
Pod documented
346 116 my ($self) = @_;
347  
348 116 $self->check;
349  
350 116 my $serialized = PSBT_MAGIC;
351 116 $serialized .= $self->_get_map(PSBT_GLOBAL_MAP, set => !!1)->to_serialized;
352  
353 116 for my $input_index (0 .. $self->input_count - 1) {
354 117 $serialized .= $self->_get_map(PSBT_INPUT_MAP, index => $input_index, set => !!1)
355 ->to_serialized;
356 }
357  
358 115 for my $output_index (0 .. $self->output_count - 1) {
359 130 $serialized .= $self->_get_map(PSBT_OUTPUT_MAP, index => $output_index, set => !!1)
360 ->to_serialized;
361 }
362  
363 114 return $serialized;
364 }
365  
366 sub check
367 149 ▶{
Subroutine check: called CC=15 SCAR=27.1
Pod documented
368 232 my ($self) = @_;
369 206 my $version = $self->version;
370 244 my $input_count = $self->input_count;
371 243 my $output_count = $self->output_count;
372  
373 197 my $required_fields = Bitcoin::Crypto::PSBT::FieldType->get_fields_required_in_version($version);
374 197 foreach my $field_type (@{$required_fields}) {
375 700 my @maps;
376  
377 700 ▶ if ($field_type->map_type eq PSBT_GLOBAL_MAP) {
Branch: 3 branches
BranchTrueFalseTotal
if ($field_type->map_type eq 'global') { } 335 450 785
elsif ($field_type->map_type eq 'in') { } 185 230 415
elsif ($field_type->map_type eq 'out') { } 181 0 181
378 334 @maps = ($self->_get_map($field_type->map_type));
379 }
380 elsif ($field_type->map_type eq PSBT_INPUT_MAP) {
381 185 @maps = map { $self->_get_map($field_type->map_type, index => $_) } 0 .. $input_count - 1;
382 }
383 elsif ($field_type->map_type eq PSBT_OUTPUT_MAP) {
384 181 @maps = map { $self->_get_map($field_type->map_type, index => $_) } 0 .. $output_count - 1;
385 }
386  
387 700 foreach my $map (@maps) {
388 765 ▶ my @values = defined $map ? $map->find($field_type) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $map ? : 765 0 765
389 765 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @values == 1 4 761 765
390 "PSBT field " . $field_type->name . " is required in version $version"
391 ) unless @values == 1;
392 }
393 }
394  
395 144 foreach my $map (@{$self->maps}) {
396  
397 444 ▶ if ($map->type eq PSBT_INPUT_MAP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($map->type eq 'in') { } 144 300 444
elsif ($map->type eq 'out') { } 156 144 300
398 144 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $map->index < $input_count 1 143 144
399 "PSBT input map index " . $map->index . " out of range"
400 ) unless $map->index < $input_count;
401 }
402 elsif ($map->type eq PSBT_OUTPUT_MAP) {
403 156 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $map->index < $output_count 0 156 156
404 "PSBT output map index " . $map->index . " out of range"
405 ) unless $map->index < $output_count;
406 }
407  
408 443 foreach my $field (@{$map->fields}) {
409 1353 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $field->type->available_in_version($version) 12 1341 1353
410 "PSBT field " . $field->type->name . " is not available in version $version"
411 ) unless $field->type->available_in_version($version);
412 }
413 }
414  
415 131 return $self;
416 }
417  
418 sub get_locktime
419 63 ▶{
Subroutine get_locktime: called CC=14 SCAR=26.4
Pod documented
420 63 my ($self) = @_;
421 63 my $version = $self->version;
422  
423 63 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 0 63 63
elsif ($version == 2) { } 63 0 63
424 0 return $self->get_transaction->locktime;
425 }
426 elsif ($version == 2) {
427  
428 # NOTE: this logic is based on:
429 # https://github.com/bitcoin/bips/blob/master/bip-0370.mediawiki#determining-lock-time
430  
431 63 my $time_locktimes = 0;
432 63 my $height_locktimes = 0;
433 63 my $none_locktimes = 0;
434 63 my $max_time_locktime = 0;
435 63 my $max_height_locktime = 0;
436  
437 63 foreach my $input_index (0 .. $self->input_count - 1) {
438 73 my $time = $self->get_all_fields('PSBT_IN_REQUIRED_TIME_LOCKTIME', $input_index);
439 73 my $height = $self->get_all_fields('PSBT_IN_REQUIRED_HEIGHT_LOCKTIME', $input_index);
440  
441 73 $time_locktimes += defined $time;
442 73 $height_locktimes += defined $height;
443 73 ▶ $none_locktimes += !defined $time && !defined $height;
Condition100%3/3
defined $time || defined $height
l!l&&r!l&&!r
7363
MC/DC100%2/2
defined $time || defined $height
defined $time defined $height
Truth table100%3/3
A || B
A= defined $timeB= defined $height
ABresult
1X1
011
000
444  
445 73 ▶ $max_time_locktime = $time->value
Branch: 1 branch
BranchTrueFalseTotal
if $time and $time->value > $max_time_locktime 6 67 73
Condition100%3/3
$time and $time->value > $max_time_locktime
!ll&&!rl&&r
6616
MC/DC100%2/2
$time and $time->value > $max_time_locktime
$time $time->value > $max_time_locktime
Truth table100%3/3
A and B
A= $timeB= $time->value > $max_time_locktime
ABresult
0X0
100
111
446 if $time && $time->value > $max_time_locktime;
447 73 ▶ $max_height_locktime = $height->value
Branch: 1 branch
BranchTrueFalseTotal
if $height and $height->value > $max_height_locktime 5 68 73
Condition100%3/3
$height and $height->value > $max_height_locktime
!ll&&!rl&&r
6625
MC/DC100%2/2
$height and $height->value > $max_height_locktime
$height $height->value > $max_height_locktime
Truth table100%3/3
A and B
A= $heightB= $height->value > $max_height_locktime
ABresult
0X0
100
111
448 if $height && $height->value > $max_height_locktime;
449 }
450  
451 63 ▶ if ($none_locktimes == $self->input_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($none_locktimes == $self->input_count) 57 6 63
452 57 my $fallback = $self->get_all_fields('PSBT_GLOBAL_FALLBACK_LOCKTIME');
453  
454 57 ▶ return $fallback ? $fallback->value : 0;
Branch: 1 branch
BranchTrueFalseTotal
$fallback ? : 1 56 57
455 }
456  
457 6 $time_locktimes += $none_locktimes;
458 6 $height_locktimes += $none_locktimes;
459  
460 6 ▶ if ($time_locktimes == $self->input_count && $time_locktimes > $height_locktimes) {
Branch: 2 branches
BranchTrueFalseTotal
if ($time_locktimes == $self->input_count and $time_locktimes > $height_locktimes) { } 2 4 6
elsif ($height_locktimes == $self->input_count) { } 3 1 4
Condition100%3/3
$time_locktimes == $self->input_count and $time_locktimes > $height_locktimes
!ll&&!rl&&r
312
MC/DC100%2/2
$time_locktimes == $self->input_count and $time_locktimes > $height_locktimes
$time_locktimes == $self->input_count $time_locktimes > $height_locktimes
Truth table100%3/3
A and B
A= $time_locktimes == $self->input_countB= $time_locktimes > $height_locktimes
ABresult
0X0
100
111
461 2 return $max_time_locktime;
462 }
463 elsif ($height_locktimes == $self->input_count) {
464 3 return $max_height_locktime;
465 }
466 else {
467 1 Bitcoin::Crypto::Exception::PSBT->raise(
468 'cannot determine locktime - no type of locktime is supported by all inputs'
469 );
470 }
471 }
472 }
473  
474 sub _get_utxo
475 64 ▶{
Subroutine _get_utxo: called CC=7 SCAR=19.5
476 64 my ($self, $input_index, $txid, $output_index) = @_;
477  
478 64 my $non_witness_utxo = $self->get_all_fields('PSBT_IN_NON_WITNESS_UTXO', $input_index);
479 64 my $witness_utxo = $self->get_all_fields('PSBT_IN_WITNESS_UTXO', $input_index);
480  
481 64 ▶ return unless $non_witness_utxo || $witness_utxo;
Branch: 1 branch
BranchTrueFalseTotal
unless $non_witness_utxo or $witness_utxo 13 51 64
Condition100%3/3
$non_witness_utxo or $witness_utxo
l!l&&r!l&&!r
104113
MC/DC100%2/2
$non_witness_utxo or $witness_utxo
$non_witness_utxo $witness_utxo
Truth table100%3/3
A or B
A= $non_witness_utxoB= $witness_utxo
ABresult
1X1
011
000
482  
483 51 my $output;
484  
485 51 ▶ if ($non_witness_utxo) {
Branch: 2 branches
BranchTrueFalseTotal
if ($non_witness_utxo) { } 10 41 51
elsif ($witness_utxo) { } 41 0 41
486 10 my $utxo_tx = $non_witness_utxo->value;
487  
488 10 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $utxo_tx->txid eq $txid 1 9 10
489 'invalid PSBT_IN_NON_WITNESS_UTXO - txid mismatch'
490 ) unless $utxo_tx->txid eq $txid;
491  
492 9 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $utxo_tx->outputs->[$output_index] 0 9 9
493 "invalid PSBT_IN_NON_WITNESS_UTXO - no output $output_index"
494 ) unless defined $utxo_tx->outputs->[$output_index];
495  
496 9 $output = $utxo_tx->outputs->[$output_index];
497 }
498 elsif ($witness_utxo) {
499 41 $output = $witness_utxo->value;
500 }
501  
502 50 return btc_utxo->new(
503 txid => $txid,
504 output_index => $output_index,
505 output => $output,
506 );
507 }
508  
509 sub get_transaction
510 61 ▶{
Subroutine get_transaction: called CC=10 SCAR=23.0
Pod documented
511 61 my ($self) = @_;
512  
513 61 $self->check;
514 61 my $version = $self->version;
515  
516 61 my $tx;
517 61 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 5 56 61
elsif ($version == 2) { } 56 0 56
518 5 $tx = $self->get_field('PSBT_GLOBAL_UNSIGNED_TX')->value;
519 }
520 elsif ($version == 2) {
521 56 $tx = btc_transaction->new(
522 version => $self->get_field('PSBT_GLOBAL_TX_VERSION')->value,
523 locktime => $self->get_locktime,
524 );
525  
526 56 foreach my $input_index (0 .. $self->input_count - 1) {
527 59 my $utxo_txid = $self->get_field('PSBT_IN_PREVIOUS_TXID', $input_index)->value;
528 59 my $utxo_output = $self->get_field('PSBT_IN_OUTPUT_INDEX', $input_index)->value;
529 59 my $sequence_field = $self->get_all_fields('PSBT_IN_SEQUENCE', $input_index);
530  
531 59 ▶ $tx->add_input(
Branch: 1 branch
BranchTrueFalseTotal
defined $sequence_field ? : 5 54 59
532 utxo => [$utxo_txid, $utxo_output],
533 (defined $sequence_field ? (sequence_no => $sequence_field->value) : ()),
534 );
535 }
536  
537 56 foreach my $output_index (0 .. $self->output_count - 1) {
538 56 my $value = $self->get_field('PSBT_OUT_AMOUNT', $output_index)->value;
539 56 my $script = $self->get_field('PSBT_OUT_SCRIPT', $output_index)->value;
540  
541 56 $tx->add_output(
542 locking_script => $script,
543 value => $value,
544 );
545 }
546 }
547  
548 61 foreach my $input_index (0 .. $self->input_count - 1) {
549 64 my $input = $tx->inputs->[$input_index];
550  
551 # try to fill utxos - if not present in the PSBT, fallback to simply
552 # leaving the basic UTXO information as they are. UTXO will not be
553 # registered.
554 64 my $utxo = $self->_get_utxo($input_index, @{$input->utxo_location});
555 63 ▶ $input->set_utxo($utxo) if $utxo;
Branch: 1 branch
BranchTrueFalseTotal
if $utxo 50 13 63
556  
557 # fill signatures
558 63 my $signature_field = $self->get_all_fields('PSBT_IN_FINAL_SCRIPTSIG', $input_index);
559 63 my $witness_field = $self->get_all_fields('PSBT_IN_FINAL_SCRIPTWITNESS', $input_index);
560  
561 63 ▶ $input->set_signature_script($signature_field->value)
Branch: 1 branch
BranchTrueFalseTotal
if $signature_field 4 59 63
562 if $signature_field;
563  
564 63 ▶ $input->set_witness($witness_field->value)
Branch: 1 branch
BranchTrueFalseTotal
if $witness_field 9 54 63
565 if $witness_field;
566 }
567  
568 60 return $tx;
569 }
570  
571 sub dump
572 3 ▶{
Subroutine dump: called CC=5 SCAR=16.1
Pod documented
573 3 my ($self) = @_;
574 3 my @result;
575  
576 my @maps = sort {
577 15 my $ret = $a->type cmp $b->type;
578 15 ▶ if ($ret == 0 && $a->need_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret == 0 and $a->need_index) 3 12 15
Condition66%2/3
$ret == 0 and $a->need_index
!ll&&!rl&&r
1203
MC/DC50%1/2
$ret == 0 and $a->need_index
$ret == 0 $a->need_index
Truth table66%2/3
A and B
A= $ret == 0B= $a->need_index
ABresult
0X0
100
111
579 3 $ret = $a->index <=> $b->index;
580 }
581  
582 $ret;
583 3 } @{$self->maps};
584  
585 3 foreach my $map (@maps) {
586 12 push @result, $map->name . ' map:';
587  
588 12 my $dumped = $map->dump;
589 12 ▶ push @result, $dumped
Branch: 1 branch
BranchTrueFalseTotal
if length $dumped 8 4 12
590 if length $dumped;
591 }
592  
593 3 return join "\n", @result;
594 }
595  
596 12 1;
597  
598 __END__
« Crypto/Util/Internal.pm ↑ Summary Crypto/Transaction/Input.pm »