Crypto/PSBT.pm

filter: Statementstmt 100% 159 / 159 Branchbran 88.0% 44 / 50 Conditioncond 76.1% 16 / 21 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 25 / 25 Podpod 100% 8 / 8 totaltotal 94.1% 258 / 274 CC 47 SCAR 18.1
CC
47
Cov
95%
check
15.1
_get_map
8.0
add_field
5.1
CRAP 6.1
« Crypto/Key/ExtBase.pm ↑ Summary Crypto/Transaction/AutoSigner.pm »
1 package Bitcoin::Crypto::PSBT;
2 9 $Bitcoin::Crypto::PSBT::VERSION = '4.004';
3 9 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use Bitcoin::Crypto::PSBT::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Bitcoin::Crypto::PSBT::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Bitcoin::Crypto::PSBT::FieldType;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Bitcoin::Crypto::Constants qw(:psbt);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 has field 'maps' => (
19 isa => ArrayRef [InstanceOf ['Bitcoin::Crypto::PSBT::Map']],
20 115 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 9 );
22  
23 sub _get_map
24 {
25 1982 ▶ my ($self, $maptype, %args) = @_;
Subroutine _get_map: called CC=8 SCAR=20.8
26  
27 1982 my $found_map;
28 1982 foreach my $map (@{$self->maps}) {
29 3199 ▶ next unless $map->type eq $maptype;
Branch: 1 branch
BranchTrueFalseTotal
unless $map->type eq $maptype 1147 2052 3199
30 2052 ▶ 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"} 188 1864 2052
Condition83%5/6
$map->need_index and !defined($args{"index"}) || $map->index ne $args{"index"}
!ll&&!rl&&r
1196668188
not defined $args{"index"} or $map->index ne $args{"index"}
l!l&&r!l&&!r
0188668
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
31  
32 1864 $found_map = $map;
33 1864 last;
34 }
35  
36 1982 ▶ if (!$found_map && $args{set}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found_map and $args{"set"}) 113 1869 1982
Condition100%3/3
not $found_map and $args{"set"}
!ll&&!rl&&r
18645113
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
37 $found_map = Bitcoin::Crypto::PSBT::Map::->new(
38 type => $maptype,
39 index => $args{index},
40 113 );
41  
42 113 push @{$self->maps}, $found_map;
43 }
44  
45 1982 return $found_map;
46 }
47  
48 sub input_count
49 {
50 207 ▶ my ($self) = @_;
Subroutine input_count: called CC=3 SCAR=11.0
Pod documented
51 207 my $version = $self->version;
52  
53 207 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 130 77 207
elsif ($version == 2) { } 77 0 77
54 130 my $tx = $self->get_field('PSBT_GLOBAL_UNSIGNED_TX')->value;
55 128 return scalar @{$tx->inputs};
56 }
57 elsif ($version == 2) {
58 77 return $self->get_field('PSBT_GLOBAL_INPUT_COUNT')->value;
59 }
60 }
61  
62 sub output_count
63 {
64 179 ▶ my ($self) = @_;
Subroutine output_count: called CC=3 SCAR=11.0
Pod documented
65 179 my $version = $self->version;
66  
67 179 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 109 70 179
elsif ($version == 2) { } 70 0 70
68 109 my $tx = $self->get_field('PSBT_GLOBAL_UNSIGNED_TX')->value;
69 109 return scalar @{$tx->outputs};
70 }
71 elsif ($version == 2) {
72 70 return $self->get_field('PSBT_GLOBAL_OUTPUT_COUNT')->value;
73 }
74 }
75  
76 9 signature_for get_field => (
77 method => !!1,
78 positional => [
79 PSBTFieldType,
80 Maybe [PositiveOrZeroInt], {default => undef},
81 Maybe [ByteStr], {default => undef}
82 ],
83 );
84  
85 sub get_field
86 {
87 530 ▶ my ($self, $type, $index, $key) = @_;
Subroutine get_field: called CC=2 SCAR=6.9
88  
89 530 my @values = $self->get_all_fields($type, $index, $key);
90 530 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @values == 1 6 524 530
91 'Could not get value for field ' . $type->name . ': found ' . @values . ' values in PSBT'
92 ) unless @values == 1;
93  
94 525 return $values[0];
95 }
96  
97 9 signature_for get_all_fields => (
98 method => !!1,
99 positional => [
100 PSBTFieldType,
101 Maybe [PositiveOrZeroInt], {default => undef},
102 Maybe [ByteStr], {default => undef}
103 ],
104 );
105  
106 sub get_all_fields
107 {
108 1296 ▶ my ($self, $type, $index, $key) = @_;
Subroutine get_all_fields: called CC=2 SCAR=6.9
109  
110 1295 my $map = $self->_get_map($type->map_type, index => $index);
111 1295 ▶ return () unless $map;
Branch: 1 branch
BranchTrueFalseTotal
unless $map 5 1256 1261
112 1287 return $map->find($type, $key);
113 }
114  
115 sub add_field
116 {
117 320 ▶ my ($self, @data) = @_;
Subroutine add_field: called CC=5 SCAR=16.2
Pod documented
118 320 my $field;
119 my $index;
120  
121 320 ▶ 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')) { } 309 11 320
Condition66%6/9
@data == 1 || @data == 2 and builtin::blessed($data[0]) and $data[0]->isa("Bitcoin::Crypto::PSBT::Field")
!ll&&!rl&&r
110309
@data == 1 || @data == 2 and builtin::blessed($data[0])
!ll&&!rl&&r
110309
@data == 1 or @data == 2
l!l&&r!l&&!r
030911
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
122 309 ($field, $index) = @data;
123 }
124 else {
125 11 my %data = @data;
126 11 $index = delete $data{index};
127 11 $field = Bitcoin::Crypto::PSBT::Field->new(%data);
128 }
129  
130 320 my $map = $self->_get_map($field->type->map_type, index => $index, set => !!1);
131 320 $map->add($field);
132  
133 320 return $self;
134 }
135  
136 sub list_fields
137 {
138 29 ▶ my ($self) = @_;
Subroutine list_fields: called CC=4 SCAR=13.9
Pod documented
139  
140 29 my @results;
141 29 foreach my $map (@{$self->maps}) {
142 107 my $index = $map->index;
143 107 my %seen;
144  
145 107 foreach my $field (@{$map->fields}) {
146 309 ▶ next if $seen{$field->type->code}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$field->type->code}++ 18 291 309
147  
148 291 push @results, [$field->type, $index];
149 }
150 }
151  
152 # force list context to have same UI as get_all_fields
153 29 return @results[0 .. $#results];
154 }
155  
156 sub version
157 {
158 467 ▶ my ($self) = @_;
Subroutine version: called CC=3 SCAR=11.0
Pod documented
159  
160 467 my $version = $self->get_all_fields('PSBT_GLOBAL_VERSION');
161 467 ▶ $version = $version ? $version->value : 0;
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 182 285 467
162  
163 Bitcoin::Crypto::Exception::PSBT->raise(
164 "PSBT version $version is not supported"
165 467 ▶ ) unless any { $_ == $version } 0, 2;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ == $version; } , 0, 2) 0 467 467
166  
167 467 return $version;
168 }
169  
170 9 signature_for from_serialized => (
171 method => !!1,
172 positional => [ByteStr],
173 );
174  
175 sub from_serialized
176 {
177 80 my ($class, $serialized) = @_;
178 80 my $self = $class->new;
179  
180 54 my $pos = length PSBT_MAGIC;
181 92 my $magic = substr $serialized, 0, $pos;
182  
183 92 Bitcoin::Crypto::Exception::PSBT->raise(
184 'serialized string does not contain the PSBT header'
185 ) unless $magic eq PSBT_MAGIC;
186  
187 47 push @{$self->maps}, Bitcoin::Crypto::PSBT::Map::->from_serialized(
188 $serialized,
189 map_type => PSBT_GLOBAL_MAP,
190 pos => \$pos,
191 );
192  
193 31 foreach my $index (0 .. $self->input_count - 1) {
194 push @{$self->maps}, Bitcoin::Crypto::PSBT::Map::->from_serialized(
195 $serialized,
196 map_type => PSBT_INPUT_MAP,
197 pos => \$pos,
198 index => $index,
199 );
200 }
201  
202 foreach my $index (0 .. $self->output_count - 1) {
203 push @{$self->maps}, Bitcoin::Crypto::PSBT::Map::->from_serialized(
204 $serialized,
205 map_type => PSBT_OUTPUT_MAP,
206 pos => \$pos,
207 index => $index,
208 );
209 }
210  
211 Bitcoin::Crypto::Exception::PSBT->raise(
212 'serialized PSBT data is corrupted'
213 ) if $pos != length $serialized;
214  
215 $self->check;
216  
217 return $self;
218 }
219  
220 sub to_serialized
221 {
222 30 ▶ my ($self) = @_;
Subroutine to_serialized: called CC=3 SCAR=11.0
Pod documented
223  
224 30 $self->check;
225  
226 30 my $serialized = PSBT_MAGIC;
227 30 $serialized .= $self->_get_map(PSBT_GLOBAL_MAP, set => !!1)->to_serialized;
228  
229 30 for my $input_index (0 .. $self->input_count - 1) {
230 32 $serialized .= $self->_get_map(PSBT_INPUT_MAP, index => $input_index, set => !!1)
231 ->to_serialized;
232 }
233  
234 30 for my $output_index (0 .. $self->output_count - 1) {
235 48 $serialized .= $self->_get_map(PSBT_OUTPUT_MAP, index => $output_index, set => !!1)
236 ->to_serialized;
237 }
238  
239 30 return $serialized;
240 }
241  
242 sub check
243 {
244 80 ▶ my ($self) = @_;
Subroutine check: called CC=15 SCAR=27.1
Pod documented
245 80 my $version = $self->version;
246 80 my $input_count = $self->input_count;
247 79 my $output_count = $self->output_count;
248  
249 79 my $required_fields = Bitcoin::Crypto::PSBT::FieldType->get_fields_required_in_version($version);
250 79 foreach my $field_type (@{$required_fields}) {
251 277 my @maps;
252  
253 277 ▶ if ($field_type->map_type eq PSBT_GLOBAL_MAP) {
Branch: 3 branches
BranchTrueFalseTotal
if ($field_type->map_type eq 'global') { } 147 130 277
elsif ($field_type->map_type eq 'in') { } 67 63 130
elsif ($field_type->map_type eq 'out') { } 63 0 63
254 147 @maps = ($self->_get_map($field_type->map_type));
255 }
256 elsif ($field_type->map_type eq PSBT_INPUT_MAP) {
257 67 @maps = map { $self->_get_map($field_type->map_type, index => $_) } 0 .. $input_count - 1;
258 }
259 elsif ($field_type->map_type eq PSBT_OUTPUT_MAP) {
260 63 @maps = map { $self->_get_map($field_type->map_type, index => $_) } 0 .. $output_count - 1;
261 }
262  
263 277 foreach my $map (@maps) {
264 336 ▶ my @values = defined $map ? $map->find($field_type) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $map ? : 336 0 336
265 336 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @values == 1 4 332 336
266 "PSBT field " . $field_type->name . " is required in version $version"
267 ) unless @values == 1;
268 }
269 }
270  
271 75 foreach my $map (@{$self->maps}) {
272  
273 241 ▶ if ($map->type eq PSBT_INPUT_MAP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($map->type eq 'in') { } 74 167 241
elsif ($map->type eq 'out') { } 92 75 167
274 74 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $map->index < $input_count 1 73 74
275 "PSBT input map index " . $map->index . " out of range"
276 ) unless $map->index < $input_count;
277 }
278 elsif ($map->type eq PSBT_OUTPUT_MAP) {
279 92 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $map->index < $output_count 0 92 92
280 "PSBT output map index " . $map->index . " out of range"
281 ) unless $map->index < $output_count;
282 }
283  
284 240 foreach my $field (@{$map->fields}) {
285 686 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $field->type->available_in_version($version) 12 674 686
286 "PSBT field " . $field->type->name . " is not available in version $version"
287 ) unless $field->type->available_in_version($version);
288 }
289 }
290  
291 62 return $self;
292 }
293  
294 sub dump
295 {
296 2 ▶ my ($self) = @_;
Subroutine dump: called CC=5 SCAR=16.1
Pod documented
297 2 my @result;
298  
299 my @maps = sort {
300 12 my $ret = $a->type cmp $b->type;
301 12 ▶ if ($ret == 0 && $a->need_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret == 0 and $a->need_index) 3 9 12
Condition66%2/3
$ret == 0 and $a->need_index
!ll&&!rl&&r
903
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
302 3 $ret = $a->index <=> $b->index;
303 }
304  
305 $ret;
306 2 } @{$self->maps};
307  
308 2 foreach my $map (@maps) {
309 9 push @result, $map->name . ' map:';
310  
311 9 my $dumped = $map->dump;
312 9 ▶ push @result, $dumped
Branch: 1 branch
BranchTrueFalseTotal
if length $dumped 5 4 9
313 if length $dumped;
314 }
315  
316 2 return join "\n", @result;
317 }
318  
319 9 1;
320  
321 __END__
« Crypto/Key/ExtBase.pm ↑ Summary Crypto/Transaction/AutoSigner.pm »