Crypto/PSBT/Map.pm

filter: Statementstmt 100% 103 / 103 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.9% 158 / 170 CC 22 SCAR 13.3
CC
22
Cov
95%
from_serialized
8.9
_find
6.0
dump
4.0
CRAP 3.8
« Crypto/Script.pm ↑ Summary Crypto/DerivationPath.pm »
1 package Bitcoin::Crypto::PSBT::Map;
2 9 $Bitcoin::Crypto::PSBT::Map::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  
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 365 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 9 );
27  
28 sub BUILD
29 {
30 365 ▶ my ($self) = @_;
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
31  
32 365 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $self->need_index and not defined $self->index 0 365 365
Condition66%2/3
$self->need_index and not defined $self->index
!ll&&!rl&&r
1132520
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 {
39 10 ▶ my ($self) = @_;
Subroutine name: called CC=2 SCAR=6.9
Pod documented
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 {
56 2430 ▶ my ($self) = @_;
Subroutine need_index: called CC=1 SCAR=0.0
Pod documented
57 2430 my $type = $self->type;
58  
59 4388 ▶ return any { $type eq $_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
60 2430 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 {
72 1150 ▶ my ($self, $field) = @_;
Subroutine add: called CC=2 SCAR=6.9
73  
74 1150 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $field->map 253 897 1150
75 'This field is already used in another map'
76 ) if $field->map;
77  
78 1150 $field->set_map($self);
79 1131 push @{$self->fields}, $field;
80  
81 1130 return $self;
82 }
83  
84 sub _integrity_violation
85 {
86 1 ▶ my ($self, $type, $desc) = @_;
Subroutine _integrity_violation: called CC=1 SCAR=0.0
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 {
97 881 ▶ my ($self, $field) = @_;
Subroutine _check_integrity: called CC=2 SCAR=6.9
98 881 my @results = grep { $_ != $field } $self->_find($field->type, $field->raw_key);
99  
100 881 ▶ $self->_integrity_violation($field->type, 'duplicate field')
Branch: 1 branch
BranchTrueFalseTotal
if @results > 0 1 880 881
101 if @results > 0;
102  
103 880 return;
104 }
105  
106 sub _find
107 {
108 2426 ▶ my ($self, $type, $key) = @_;
Subroutine _find: called CC=6 SCAR=17.9
109 2426 my $code = $type->code;
110 2426 ▶ my $has_key = $type->has_key_data && defined $key;
Condition100%3/3
$type->has_key_data && defined $key
!ll&&!rl&&r
217565186
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 2426 my @found;
113 2426 foreach my $field (@{$self->fields}) {
114 6063 ▶ next unless $field->type->code == $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->type->code == $code 4714 1349 6063
115 1349 ▶ push @found, $field
Branch: 1 branch
BranchTrueFalseTotal
if not $has_key or $key eq $field->raw_key 1280 69 1349
Condition100%3/3
not $has_key or $key eq $field->raw_key
l!l&&r!l&&!r
1275569
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 2426 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 252 my ($self, $type, $key) = @_;
135  
136 810 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 223 my ($class, $serialized, $args) = @_;
155 223 my $partial = !!$args->{pos};
156 587 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 1 252 253
157  
158 223 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < length $serialized 810 0 810
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 223 );
166  
167 223 ▶ while ($pos < length $serialized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $serialized) 223 587 810
168 223 ▶ if (substr($serialized, $pos, 1) eq PSBT_SEPARATOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($serialized, $pos, 1) eq "\0") 0 223 223
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 223 0 223
Condition33%1/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
22300
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 {
194 1655 ▶ my ($self) = @_;
Subroutine to_serialized: called CC=2 SCAR=6.9
Pod documented
195  
196 1655 my %encoded;
197 110 foreach my $item (@{$self->fields}) {
198 317 $encoded{$item->serialized_key} = $item->to_serialized;
199 }
200  
201 110 my @sorted = map { $encoded{$_} } sort keys %encoded;
202 110 return join('', @sorted) . PSBT_SEPARATOR;
203 }
204  
205 sub dump
206 {
207 9 ▶ my ($self) = @_;
Subroutine dump: called CC=4 SCAR=13.9
Pod documented
208  
209 9 my @result;
210  
211 my %fields;
212 9 foreach my $item (@{$self->fields}) {
213 11 push @{$fields{$item->type->name}}, $item;
214 }
215  
216 9 foreach my $key (sort keys %fields) {
217 11 push @result, "> ${key}:";
218  
219 11 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.pm ↑ Summary Crypto/DerivationPath.pm »