Crypto/PSBT/Map.pm

filter: Statementstmt 100% 98 / 98 Branchbran 90.9% 20 / 22 Conditioncond 73.3% 11 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 21 / 21 Podpod 80.0% 4 / 5 totaltotal 93.5% 159 / 170 CC 24 SCAR 13.7
CC
24
Cov
96%
from_serialized
9.9
_find
6.0
dump
4.0
CRAP 3.9
« Crypto/Script/Compiler.pm ↑ Summary Crypto/DerivationPath.pm »
1 package Bitcoin::Crypto::PSBT::Map;
2 12 $Bitcoin::Crypto::PSBT::Map::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 first);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Bitcoin::Crypto::PSBT::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Bitcoin::Crypto::Constants qw(:psbt);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 12 has param 'type' => (
16 isa => PSBTMapType,
17 );
18  
19 12 has param 'index' => (
20 isa => Maybe [PositiveOrZeroInt],
21 default => undef,
22 );
23  
24 has field 'fields' => (
25 513 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 12 );
27  
28 sub BUILD
29 513 ▶{
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
30 513 my ($self) = @_;
31  
32 513 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $self->need_index and not defined $self->index 0 513 513
Condition66%2/3
$self->need_index and not defined $self->index
!ll&&!rl&&r
1603530
MC/DC0%0/2
$self->need_index and not defined $self->index
$self->need_index not defined $self->index
Truth table66%2/3
A and not(B)
A= $self->need_indexB= not defined $self->index
ABresult
0X0
100
111
33 "Map of type '" . $self->type . "' requires an index"
34 ) if $self->need_index && !defined $self->index;
35 }
36  
37 sub name
38 13 ▶{
Subroutine name: called CC=2 SCAR=6.9
Pod documented
39 13 my ($self) = @_;
40 13 my %dispatch = (
41 (PSBT_GLOBAL_MAP) => 'Global',
42 (PSBT_INPUT_MAP) => 'Input',
43 (PSBT_OUTPUT_MAP) => 'Output',
44 );
45  
46 13 my $name = $dispatch{$self->type};
47 13 ▶ if ($self->need_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->need_index) 10 3 13
48 10 $name .= '[' . $self->index . ']';
49 }
50  
51 13 return $name;
52 }
53  
54 sub need_index
55 5756 ▶{
Subroutine need_index: called CC=1 SCAR=0.0
Pod documented
56 5756 my ($self) = @_;
57 5756 my $type = $self->type;
58  
59 9819 ▶ return any { $type eq $_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
60 5756 PSBT_INPUT_MAP,
61 PSBT_OUTPUT_MAP,
62 ;
63 }
64  
65 12 signature_for add => (
66 method => !!1,
67 positional => [InstanceOf ['Bitcoin::Crypto::PSBT::Field']],
68 );
69  
70 sub add
71 1379 ▶{
Subroutine add: called CC=2 SCAR=6.9
72 1379 my ($self, $field) = @_;
73  
74 1379 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $field->map 262 1379 1641
75 'This field is already used in another map'
76 ) if $field->map;
77  
78 1379 $field->set_map($self);
79 1360 push @{$self->fields}, $field;
80  
81 1360 return $self;
82 }
83  
84 12 signature_for remove => (
85 method => !!1,
86 positional => [InstanceOf ['Bitcoin::Crypto::PSBT::Field']],
87 );
88  
89 sub remove
90 {
91 my ($self, $field) = @_;
92  
93 my $fields = $self->fields;
94 my $index = first { $fields->[$_] == $field } keys @{$fields};
95  
96 Bitcoin::Crypto::Exception::PSBT->raise(
97 'This field is not used in this map'
98 ) unless defined $index;
99  
100 $field->clear_map;
101 splice @{$fields}, $index, 1;
102  
103 return $self;
104 }
105  
106 sub _integrity_violation
107 1 ▶{
Subroutine _integrity_violation: called CC=1 SCAR=0.0
108 1 my ($self, $type, $desc) = @_;
109 1 my $map_name = $self->name;
110 1 my $type_name = $type->name;
111  
112 1 Bitcoin::Crypto::Exception::PSBT->raise(
113 "Map $map_name, type $type_name: $desc"
114 );
115 }
116  
117 sub _check_integrity
118 1338 ▶{
Subroutine _check_integrity: called CC=2 SCAR=6.9
119 1338 my ($self, $field) = @_;
120 1338 my @results = grep { $_ != $field } $self->_find($field->type, $field->raw_key);
121  
122 1338 ▶ $self->_integrity_violation($field->type, 'duplicate field')
Branch: 1 branch
BranchTrueFalseTotal
if @results > 0 1 1337 1338
123 if @results > 0;
124  
125 1337 return;
126 }
127  
128 sub _find
129 5647 ▶{
Subroutine _find: called CC=6 SCAR=17.9
130 5647 my ($self, $type, $key) = @_;
131 5647 my $code = $type->code;
132 5647 ▶ my $has_key = $type->has_key_data && defined $key;
Condition100%3/3
$type->has_key_data && defined $key
!ll&&!rl&&r
5311121215
MC/DC100%2/2
$type->has_key_data && defined $key
$type->has_key_data defined $key
Truth table100%3/3
A && B
A= $type->has_key_dataB= defined $key
ABresult
0X0
100
111
133  
134 5647 my @found;
135 5647 foreach my $field (@{$self->fields}) {
136 17888 ▶ next unless $field->type->code == $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->type->code == $code 14392 3496 17888
137 3496 ▶ push @found, $field
Branch: 1 branch
BranchTrueFalseTotal
if not $has_key or $key eq $field->raw_key 3416 80 3496
Condition100%3/3
not $has_key or $key eq $field->raw_key
l!l&&r!l&&!r
3411580
MC/DC100%2/2
not $has_key or $key eq $field->raw_key
not $has_key $key eq $field->raw_key
Truth table100%3/3
not(A) or B
A= not $has_keyB= $key eq $field->raw_key
ABresult
1X1
011
000
138 if !$has_key || $key eq $field->raw_key;
139 }
140  
141 # force list to have it return the last item in the scalar context instead
142 # of number of items
143 5647 return @found[0 .. $#found];
144 }
145  
146 12 signature_for find => (
147 method => !!1,
148 positional => [
149 InstanceOf ['Bitcoin::Crypto::PSBT::FieldType'],
150 Maybe [ByteStr], {default => undef}
151 ],
152 );
153  
154 sub find
155 100 ▶{
Subroutine find: called CC=1 SCAR=0.0
156 25 my ($self, $type, $key) = @_;
157  
158 25 return $self->_find($type, $key);
159 }
160  
161 12 signature_for from_serialized => (
162 method => !!1,
163 head => [ByteStr],
164 named => [
165 map_type => PSBTMapType,
166 index => Maybe [PositiveOrZeroInt],
167 {default => undef},
168 pos => Maybe [ScalarRef [PositiveOrZeroInt]],
169 {default => undef},
170 ],
171 bless => !!0,
172 );
173  
174 sub from_serialized
175 {
176 25 my ($class, $serialized, $args) = @_;
177 100 my $partial = !!$args->{pos};
178 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 1 261 262
179  
180 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < length $serialized 834 1 835
181 'map expected but end of stream was reached'
182 ) unless $pos < length $serialized;
183  
184 my $self = $class->new(
185 type => $args->{map_type},
186 index => $args->{index},
187 );
188  
189 my $finished = !!0;
190 ▶ while ($pos < length $serialized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $serialized) 231 603 834
191 ▶ if (substr($serialized, $pos, 1) eq PSBT_SEPARATOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($serialized, $pos, 1) eq "\0") 1 231 232
192 $pos += 1;
193 $finished = !!1;
194 last;
195 }
196  
197 my $field = Bitcoin::Crypto::PSBT::Field->from_serialized(
198 $serialized,
199 map_type => $args->{map_type},
200 pos => \$pos,
201 );
202  
203 $self->add($field);
204 }
205  
206 Bitcoin::Crypto::Exception::PSBT->raise(
207 ▶ 'serialized map data is corrupted'
Branch: 1 branch
BranchTrueFalseTotal
if not $finished or not $partial and $pos != length $serialized 231 0 231
Condition50%3/6
not $finished or not $partial and $pos != length $serialized
l!l&&r!l&&!r
10231
not $partial and $pos != length $serialized
!ll&&!rl&&r
23100
MC/DC33%1/3
not $finished or not $partial and $pos != length $serialized
not $finished not $partial $pos != length $serialized
Truth table50%2/4
not(A) or not(B) and C
A= not $finishedB= not $partialC= $pos != length $serialized
ABCresult
1XX1
0111
00X0
0100
208 ) if !$finished || (!$partial && $pos != length $serialized);
209  
210 ${$args->{pos}} = $pos
211 if $partial;
212  
213 return $self;
214 }
215  
216 sub to_serialized
217 4422 ▶{
Subroutine to_serialized: called CC=2 SCAR=6.9
Pod documented
218 4422 my ($self) = @_;
219  
220 4422 my %encoded;
221 375 foreach my $item (@{$self->fields}) {
222 583 $encoded{$item->serialized_key} = $item->to_serialized;
223 }
224  
225 375 my @sorted = map { $encoded{$_} } sort keys %encoded;
226 374 return join('', @sorted) . PSBT_SEPARATOR;
227 }
228  
229 sub dump
230 274 ▶{
Subroutine dump: called CC=4 SCAR=13.9
Pod documented
231 273 my ($self) = @_;
232  
233 273 my @result;
234  
235 my %fields;
236 846 foreach my $item (@{$self->fields}) {
237 252 push @{$fields{$item->type->name}}, $item;
238 }
239  
240 615 foreach my $key (sort keys %fields) {
241 611 push @result, "> ${key}:";
242  
243 250 foreach my $item (@{$fields{$key}}) {
244 252 my $dumped = $item->dump;
245 252 $dumped =~ s/^/> > /gm;
246  
247 21 push @result, $dumped;
248 }
249 }
250  
251 12 return join "\n", @result;
252 }
253  
254 12 1;
255  
256 __END__
« Crypto/Script/Compiler.pm ↑ Summary Crypto/DerivationPath.pm »