Crypto/PSBT/Map.pm

filter: Statementstmt 100% 93 / 93 Branchbran 81.8% 18 / 22 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 20 / 20 Podpod 80.0% 4 / 5 totaltotal 92.5% 148 / 160 CC 22 SCAR 14.6
CC
22
Cov
94%
from_serialized
11.6
_find
6.0
dump
4.0
CRAP 4.3
« Crypto/Script/Tree.pm ↑ Summary Crypto/Bech32.pm »
1 package Bitcoin::Crypto::PSBT::Map;
2 9 $Bitcoin::Crypto::PSBT::Map::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  
10 9 ▶use Bitcoin::Crypto::PSBT::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Bitcoin::Crypto::Constants qw(:psbt);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 has param 'type' => (
16 isa => PSBTMapType,
17 );
18  
19 9 has param 'index' => (
20 isa => Maybe [PositiveOrZeroInt],
21 default => undef,
22 );
23  
24 has field 'fields' => (
25 371 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 9 );
27  
28 sub BUILD
29 371 ▶{
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
30 371 my ($self) = @_;
31  
32 371 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $self->need_index and not defined $self->index 0 371 371
Condition66%2/3
$self->need_index and not defined $self->index
!ll&&!rl&&r
1152560
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 10 ▶{
Subroutine name: called CC=2 SCAR=6.9
Pod documented
39 10 my ($self) = @_;
40 10 my %dispatch = (
41 (PSBT_GLOBAL_MAP) => 'Global',
42 (PSBT_INPUT_MAP) => 'Input',
43 (PSBT_OUTPUT_MAP) => 'Output',
44 );
45  
46 10 my $name = $dispatch{$self->type};
47 10 ▶ if ($self->need_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->need_index) 8 2 10
48 8 $name .= '[' . $self->index . ']';
49 }
50  
51 10 return $name;
52 }
53  
54 sub need_index
55 2477 ▶{
Subroutine need_index: called CC=1 SCAR=0.0
Pod documented
56 2477 my ($self) = @_;
57 2477 my $type = $self->type;
58  
59 4478 ▶ return any { $type eq $_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
60 2477 PSBT_INPUT_MAP,
61 PSBT_OUTPUT_MAP,
62 ;
63 }
64  
65 9 signature_for add => (
66 method => !!1,
67 positional => [InstanceOf ['Bitcoin::Crypto::PSBT::Field']],
68 );
69  
70 sub add
71 906 ▶{
Subroutine add: called CC=2 SCAR=6.9
72 906 my ($self, $field) = @_;
73  
74 906 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $field->map 256 906 1162
75 'This field is already used in another map'
76 ) if $field->map;
77  
78 906 $field->set_map($self);
79 887 push @{$self->fields}, $field;
80  
81 887 return $self;
82 }
83  
84 sub _integrity_violation
85 1 ▶{
Subroutine _integrity_violation: called CC=1 SCAR=0.0
86 1 my ($self, $type, $desc) = @_;
87 1 my $map_name = $self->name;
88 1 my $type_name = $type->name;
89  
90 1 Bitcoin::Crypto::Exception::PSBT->raise(
91 "Map $map_name, type $type_name: $desc"
92 );
93 }
94  
95 sub _check_integrity
96 890 ▶{
Subroutine _check_integrity: called CC=2 SCAR=6.9
97 890 my ($self, $field) = @_;
98 890 my @results = grep { $_ != $field } $self->_find($field->type, $field->raw_key);
99  
100 890 ▶ $self->_integrity_violation($field->type, 'duplicate field')
Branch: 1 branch
BranchTrueFalseTotal
if @results > 0 1 889 890
101 if @results > 0;
102  
103 889 return;
104 }
105  
106 sub _find
107 2471 ▶{
Subroutine _find: called CC=6 SCAR=17.9
108 2471 my ($self, $type, $key) = @_;
109 2471 my $code = $type->code;
110 2471 ▶ my $has_key = $type->has_key_data && defined $key;
Condition100%3/3
$type->has_key_data && defined $key
!ll&&!rl&&r
220969193
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
111  
112 2471 my @found;
113 2471 foreach my $field (@{$self->fields}) {
114 6144 ▶ next unless $field->type->code == $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->type->code == $code 4773 1371 6144
115 1371 ▶ push @found, $field
Branch: 1 branch
BranchTrueFalseTotal
if not $has_key or $key eq $field->raw_key 1301 70 1371
Condition100%3/3
not $has_key or $key eq $field->raw_key
l!l&&r!l&&!r
1296570
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
116 if !$has_key || $key eq $field->raw_key;
117 }
118  
119 # force list to have it return the last item in the scalar context instead
120 # of number of items
121 2471 return @found[0 .. $#found];
122 }
123  
124 9 signature_for find => (
125 method => !!1,
126 positional => [
127 InstanceOf ['Bitcoin::Crypto::PSBT::FieldType'],
128 Maybe [ByteStr], {default => undef}
129 ],
130 );
131  
132 sub find
133 {
134 my ($self, $type, $key) = @_;
135  
136 return $self->_find($type, $key);
137 }
138  
139 9 signature_for from_serialized => (
140 method => !!1,
141 head => [ByteStr],
142 named => [
143 map_type => PSBTMapType,
144 index => Maybe [PositiveOrZeroInt],
145 {default => undef},
146 pos => Maybe [ScalarRef [PositiveOrZeroInt]],
147 {default => undef},
148 ],
149 bless => !!0,
150 );
151  
152 sub from_serialized
153 {
154 my ($class, $serialized, $args) = @_;
155 my $partial = !!$args->{pos};
156 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 1 255 256
157  
158 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < length $serialized 817 0 817
159 'map expected but end of stream was reached'
160 ) unless $pos < length $serialized;
161  
162 my $self = $class->new(
163 type => $args->{map_type},
164 index => $args->{index},
165 );
166  
167 ▶ while ($pos < length $serialized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $serialized) 226 591 817
168 ▶ if (substr($serialized, $pos, 1) eq PSBT_SEPARATOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($serialized, $pos, 1) eq "\0") 0 226 226
169 $pos += 1;
170 last;
171 }
172  
173 my $field = Bitcoin::Crypto::PSBT::Field->from_serialized(
174 $serialized,
175 map_type => $args->{map_type},
176 pos => \$pos,
177 );
178  
179 $self->add($field);
180 }
181  
182 Bitcoin::Crypto::Exception::PSBT->raise(
183 ▶ 'serialized map data is corrupted'
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $serialized 226 0 226
Condition33%1/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
22600
MC/DC0%0/2
not $partial and $pos != length $serialized
not $partial $pos != length $serialized
Truth table33%1/3
not(A) and B
A= not $partialB= $pos != length $serialized
ABresult
0X0
100
111
184 ) if !$partial && $pos != length $serialized;
185  
186 ${$args->{pos}} = $pos
187 if $partial;
188  
189 return $self;
190 }
191  
192 sub to_serialized
193 1694 ▶{
Subroutine to_serialized: called CC=2 SCAR=6.9
Pod documented
194 1694 my ($self) = @_;
195  
196 1694 my %encoded;
197 369 foreach my $item (@{$self->fields}) {
198 577 $encoded{$item->serialized_key} = $item->to_serialized;
199 }
200  
201 369 my @sorted = map { $encoded{$_} } sort keys %encoded;
202 368 return join('', @sorted) . PSBT_SEPARATOR;
203 }
204  
205 sub dump
206 265 ▶{
Subroutine dump: called CC=4 SCAR=13.9
Pod documented
207 264 my ($self) = @_;
208  
209 826 my @result;
210  
211 my %fields;
212 235 foreach my $item (@{$self->fields}) {
213 602 push @{$fields{$item->type->name}}, $item;
214 }
215  
216 235 foreach my $key (sort keys %fields) {
217 237 push @result, "> ${key}:";
218  
219 237 foreach my $item (@{$fields{$key}}) {
220 11 my $dumped = $item->dump;
221 11 $dumped =~ s/^/> > /g;
222  
223 11 push @result, $dumped;
224 }
225 }
226  
227 9 return join "\n", @result;
228 }
229  
230 9 1;
231  
232 __END__
« Crypto/Script/Tree.pm ↑ Summary Crypto/Bech32.pm »