Crypto/PSBT.pm

filter: Statementstmt 100% 151 / 151 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 93.9% 250 / 266 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.005';
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 117 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 9 );
22  
23 sub _get_map
24 2026 ▶{
Subroutine _get_map: called CC=8 SCAR=20.8
25 2026 my ($self, $maptype, %args) = @_;
26  
27 2026 my $found_map;
28 2026 foreach my $map (@{$self->maps}) {
29 3249 ▶ next unless $map->type eq $maptype;
Branch: 1 branch
BranchTrueFalseTotal
unless $map->type eq $maptype 1156 2093 3249
30 2093 ▶ 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 1905 2093
Condition83%5/6
$map->need_index and !defined($args{"index"}) || $map->index ne $args{"index"}
!ll&&!rl&&r
1233672188
not defined $args{"index"} or $map->index ne $args{"index"}
l!l&&r!l&&!r
0188672
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 1905 $found_map = $map;
33 1905 last;
34 }
35  
36 2026 ▶ if (!$found_map && $args{set}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found_map and $args{"set"}) 116 1910 2026
Condition100%3/3
not $found_map and $args{"set"}
!ll&&!rl&&r
19055116
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 116 );
41  
42 116 push @{$self->maps}, $found_map;
43 }
44  
45 2026 return $found_map;
46 }
47  
48 sub input_count
49 213 ▶{
Subroutine input_count: called CC=3 SCAR=11.0
Pod documented
50 213 my ($self) = @_;
51 213 my $version = $self->version;
52  
53 213 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 136 77 213
elsif ($version == 2) { } 77 0 77
54 136 my $tx = $self->get_field('PSBT_GLOBAL_UNSIGNED_TX')->value;
55 134 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 185 ▶{
Subroutine output_count: called CC=3 SCAR=11.0
Pod documented
64 185 my ($self) = @_;
65 185 my $version = $self->version;
66  
67 185 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 115 70 185
elsif ($version == 2) { } 70 0 70
68 115 my $tx = $self->get_field('PSBT_GLOBAL_UNSIGNED_TX')->value;
69 115 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 460 ▶{
Subroutine get_field: called CC=2 SCAR=6.9
87 460 my ($self, $type, $index, $key) = @_;
88  
89 460 my @values = $self->get_all_fields($type, $index, $key);
90 460 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @values == 1 5 455 460
91 'Could not get value for field ' . $type->name . ': found ' . @values . ' values in PSBT'
92 ) unless @values == 1;
93  
94 455 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 1246 ▶{
Subroutine get_all_fields: called CC=2 SCAR=6.9
108 1246 my ($self, $type, $index, $key) = @_;
109  
110 1246 my $map = $self->_get_map($type->map_type, index => $index);
111 1246 ▶ return () unless $map;
Branch: 1 branch
BranchTrueFalseTotal
unless $map 5 1241 1246
112 1241 return $map->find($type, $key);
113 }
114  
115 sub add_field
116 325 ▶{
Subroutine add_field: called CC=5 SCAR=16.2
Pod documented
117 325 my ($self, @data) = @_;
118 325 my $field;
119 my $index;
120  
121 325 ▶ 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 16 325
Condition66%6/9
@data == 1 || @data == 2 and builtin::blessed($data[0]) and $data[0]->isa("Bitcoin::Crypto::PSBT::Field")
!ll&&!rl&&r
160309
@data == 1 || @data == 2 and builtin::blessed($data[0])
!ll&&!rl&&r
160309
@data == 1 or @data == 2
l!l&&r!l&&!r
030916
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 16 my %data = @data;
126 16 $index = delete $data{index};
127 16 $field = Bitcoin::Crypto::PSBT::Field->new(%data);
128 }
129  
130 325 my $map = $self->_get_map($field->type->map_type, index => $index, set => !!1);
131 325 $map->add($field);
132  
133 325 return $self;
134 }
135  
136 sub list_fields
137 29 ▶{
Subroutine list_fields: called CC=4 SCAR=13.9
Pod documented
138 29 my ($self) = @_;
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 483 ▶{
Subroutine version: called CC=3 SCAR=11.0
Pod documented
158 483 my ($self) = @_;
159  
160 483 my $version = $self->get_all_fields('PSBT_GLOBAL_VERSION');
161 483 ▶ $version = $version ? $version->value : 0;
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 182 301 483
162  
163 Bitcoin::Crypto::Exception::PSBT->raise(
164 "PSBT version $version is not supported"
165 483 ▶ ) unless any { $_ == $version } 0, 2;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ == $version; } , 0, 2) 0 483 483
166  
167 483 return $version;
168 }
169  
170 9 signature_for from_serialized => (
171 method => !!1,
172 positional => [ByteStr],
173 );
174  
175 sub from_serialized
176 {
177 my ($class, $serialized) = @_;
178 my $self = $class->new;
179  
180 my $pos = length PSBT_MAGIC;
181 my $magic = substr $serialized, 0, $pos;
182  
183 Bitcoin::Crypto::Exception::PSBT->raise(
184 'serialized string does not contain the PSBT header'
185 ) unless $magic eq PSBT_MAGIC;
186  
187 push @{$self->maps}, Bitcoin::Crypto::PSBT::Map::->from_serialized(
188 $serialized,
189 map_type => PSBT_GLOBAL_MAP,
190 pos => \$pos,
191 );
192  
193 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 114 ▶{
Subroutine to_serialized: called CC=3 SCAR=11.0
Pod documented
222 114 my ($self) = @_;
223  
224 114 $self->check;
225  
226 114 my $serialized = PSBT_MAGIC;
227 114 $serialized .= $self->_get_map(PSBT_GLOBAL_MAP, set => !!1)->to_serialized;
228  
229 114 for my $input_index (0 .. $self->input_count - 1) {
230 115 $serialized .= $self->_get_map(PSBT_INPUT_MAP, index => $input_index, set => !!1)
231 ->to_serialized;
232 }
233  
234 113 for my $output_index (0 .. $self->output_count - 1) {
235 128 $serialized .= $self->_get_map(PSBT_OUTPUT_MAP, index => $output_index, set => !!1)
236 ->to_serialized;
237 }
238  
239 112 return $serialized;
240 }
241  
242 sub check
243 84 ▶{
Subroutine check: called CC=15 SCAR=27.1
Pod documented
244 165 my ($self) = @_;
245 139 my $version = $self->version;
246 177 my $input_count = $self->input_count;
247 176 my $output_count = $self->output_count;
248  
249 131 my $required_fields = Bitcoin::Crypto::PSBT::FieldType->get_fields_required_in_version($version);
250 131 foreach my $field_type (@{$required_fields}) {
251 281 my @maps;
252  
253 281 ▶ if ($field_type->map_type eq PSBT_GLOBAL_MAP) {
Branch: 3 branches
BranchTrueFalseTotal
if ($field_type->map_type eq 'global') { } 152 212 364
elsif ($field_type->map_type eq 'in') { } 67 111 178
elsif ($field_type->map_type eq 'out') { } 63 0 63
254 151 @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 281 foreach my $map (@maps) {
264 340 ▶ my @values = defined $map ? $map->find($field_type) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $map ? : 340 0 340
265 340 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @values == 1 4 336 340
266 "PSBT field " . $field_type->name . " is required in version $version"
267 ) unless @values == 1;
268 }
269 }
270  
271 79 foreach my $map (@{$self->maps}) {
272  
273 253 ▶ if ($map->type eq PSBT_INPUT_MAP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($map->type eq 'in') { } 78 175 253
elsif ($map->type eq 'out') { } 96 79 175
274 78 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $map->index < $input_count 1 77 78
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 96 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $map->index < $output_count 0 96 96
280 "PSBT output map index " . $map->index . " out of range"
281 ) unless $map->index < $output_count;
282 }
283  
284 252 foreach my $field (@{$map->fields}) {
285 703 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $field->type->available_in_version($version) 12 691 703
286 "PSBT field " . $field->type->name . " is not available in version $version"
287 ) unless $field->type->available_in_version($version);
288 }
289 }
290  
291 66 return $self;
292 }
293  
294 sub dump
295 2 ▶{
Subroutine dump: called CC=5 SCAR=16.1
Pod documented
296 2 my ($self) = @_;
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 »