Crypto/PSBT/Field.pm

filter: Statementstmt 97.9% 94 / 96 Branchbran 76.9% 20 / 26 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 21 / 21 Podpod 36.3% 4 / 11 totaltotal 88.6% 148 / 167 CC 17 SCAR 11.4
CC
17
Cov
92%
from_serialized
6.3
BUILD
6.0
set_raw_key
3.0
CRAP 3.1
« Crypto/Transaction/Signer.pm ↑ Summary Crypto/Constants.pm »
1 package Bitcoin::Crypto::PSBT::Field;
2 9 $Bitcoin::Crypto::PSBT::Field::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 9 ▶use Bitcoin::Crypto::PSBT::FieldType;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Bitcoin::Crypto::Util::Internal qw(to_format pack_compactsize unpack_compactsize);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 has field 'map' => (
15 isa => InstanceOf ['Bitcoin::Crypto::PSBT::Map'],
16 writer => -hidden,
17 weak_ref => 1,
18 );
19  
20 9 has param 'type' => (
21 coerce => PSBTFieldType,
22 );
23  
24 9 has param 'raw_key' => (
25 coerce => Maybe [ByteStr],
26 writer => -hidden,
27 default => undef,
28 );
29  
30 9 has param 'raw_value' => (
31 coerce => ByteStr,
32 writer => 1,
33 default => '',
34 );
35  
36 sub BUILD
37 916 ▶{
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
38 916 my ($self, $args) = @_;
39  
40 916 ▶ if (defined $self->raw_key && !$self->type->has_key_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->raw_key and not $self->type->has_key_data) 470 446 916
Condition100%3/3
defined $self->raw_key and not $self->type->has_key_data
!ll&&!rl&&r
325121470
MC/DC100%2/2
defined $self->raw_key and not $self->type->has_key_data
defined $self->raw_key not $self->type->has_key_data
Truth table100%3/3
A and not(B)
A= defined $self->raw_keyB= not $self->type->has_key_data
ABresult
0X0
100
111
41 470 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $self->raw_key 10 460 470
42 'Field ' . $self->type->name . ' does not define key data'
43 ) if length $self->raw_key;
44  
45 460 $self->_set_raw_key(undef);
46 }
47  
48 906 ▶ if (defined $args->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"value"}) 325 581 906
49 325 $self->set_value($args->{value});
50 }
51  
52 906 ▶ if (defined $args->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"key"}) 77 829 906
53 77 $self->set_key($args->{key});
54 }
55  
56 }
57  
58 sub validate
59 906 ▶{
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
60 906 my ($self) = @_;
61  
62 # at the very least, try deserializing value and key. If there is a
63 # dedicated validator, run that as well with these values
64 Bitcoin::Crypto::Exception::PSBT->trap_into(
65 906 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
66 906 my @args = ($self->value);
67 904 ▶ if ($self->type->has_key_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type->has_key_data) 199 705 904
68 199 unshift @args, $self->key;
69 }
70  
71 896 ▶ $self->type->validator->(@args)
Branch: 1 branch
BranchTrueFalseTotal
if $self->type->has_validator 103 793 896
72 if $self->type->has_validator;
73 },
74 906 'validation failed for type ' . $self->type->name
75 );
76  
77 888 return $self;
78 }
79  
80 sub key
81 529 ▶{
Subroutine key: called CC=1 SCAR=0.0
Pod undocumented
82 529 my ($self) = @_;
83  
84 529 return $self->type->key_deserializer->($self->raw_key);
85 }
86  
87 sub set_raw_key
88 79 ▶{
Subroutine set_raw_key: called CC=3 SCAR=11.0
Pod undocumented
89 79 my ($self, $key) = @_;
90  
91 79 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type->has_key_data 0 79 79
92 'Field ' . $self->type->name . ' does not define key data'
93 ) if !$self->type->has_key_data;
94  
95 79 $self->_set_raw_key($key);
96 79 ▶ $self->map->_check_integrity($self)
Branch: 1 branch
BranchTrueFalseTotal
if $self->map 2 77 79
97 if $self->map;
98  
99 79 return;
100 }
101  
102 sub set_key
103 79 ▶{
Subroutine set_key: called CC=1 SCAR=0.0
Pod undocumented
104 79 my ($self, $key) = @_;
105  
106 79 $self->set_raw_key($self->type->key_serializer->($key));
107 79 return;
108 }
109  
110 sub value
111 1886 ▶{
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
112 1886 my ($self) = @_;
113  
114 1886 return $self->type->deserializer->($self->raw_value);
115 }
116  
117 sub set_value
118 347 ▶{
Subroutine set_value: called CC=1 SCAR=0.0
Pod undocumented
119 347 my ($self, $value) = @_;
120  
121 347 $self->set_raw_value($self->type->serializer->($value));
122 347 return;
123 }
124  
125 sub set_map
126 906 ▶{
Subroutine set_map: called CC=1 SCAR=0.0
Pod undocumented
127 906 my ($self, $map) = @_;
128  
129 906 $self->validate;
130 888 $self->_set_map($map);
131 888 $map->_check_integrity($self);
132 887 return;
133 }
134  
135 sub serialized_key
136 1260 ▶{
Subroutine serialized_key: called CC=2 SCAR=6.9
Pod documented
137 1260 my ($self) = @_;
138  
139 1260 ▶ return pack_compactsize($self->type->code) . ($self->raw_key // '');
Condition100%2/2
$self->raw_key // ""
l!l
298962
MC/DC100%1/1
$self->raw_key // ""
$self->raw_key
Truth table100%2/2
A
A= $self->raw_key
Aresult
11
00
140 }
141  
142 9 signature_for from_serialized => (
143 method => !!1,
144 head => [ByteStr],
145 named => [
146 map_type => PSBTMapType,
147 pos => Maybe [ScalarRef [PositiveOrZeroInt]],
148 {default => undef},
149 ],
150 bless => !!0,
151 );
152  
153 sub from_serialized
154 591 ▶{
Subroutine from_serialized: called CC=6 SCAR=18.4
155 591 my ($class, $serialized, $args) = @_;
156 591 my $partial = !!$args->{pos};
157 591 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 591 0 591
158  
159 591 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < length $serialized 0 591 591
160 'field expected but end of stream was reached'
161 ) unless $pos < length $serialized;
162  
163 591 my $keylen = unpack_compactsize $serialized, \$pos;
164 591 my $keydata = substr $serialized, $pos, $keylen;
165 591 my $keytype = unpack_compactsize $keydata, \(my $keytype_pos = 0);
166 591 $keydata = substr $keydata, $keytype_pos;
167 591 $pos += $keylen;
168  
169 591 my $valuelen = unpack_compactsize $serialized, \$pos;
170 591 my $valuedata = substr $serialized, $pos, $valuelen;
171 591 $pos += $valuelen;
172  
173 my $self = $class->new(
174 591 type => [$args->{map_type}, $keytype],
175 raw_key => $keydata,
176 raw_value => $valuedata,
177 );
178  
179 581 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $serialized 0 581 581
Condition33%1/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
58100
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
180 'serialized field data is corrupted'
181 ) if !$partial && $pos != length $serialized;
182  
183 581 ▶ ${$args->{pos}} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if $partial 581 0 581
184 if $partial;
185  
186 581 return $self;
187 }
188  
189 sub to_serialized
190 939 ▶{
Subroutine to_serialized: called CC=1 SCAR=0.0
Pod documented
191 939 my ($self) = @_;
192  
193 939 my $enckey = $self->serialized_key;
194 939 my $value = $self->raw_value;
195  
196 939 return join '',
197 pack_compactsize(length $enckey),
198 $enckey,
199 pack_compactsize(length $value),
200 $value
201 ;
202 }
203  
204 sub dump
205 11 ▶{
Subroutine dump: called CC=2 SCAR=7.6
Pod documented
206 11 my ($self) = @_;
207  
208 11 my @result;
209  
210 11 ▶ if (defined $self->raw_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->raw_key) { } 0 11 11
211 0 push @result, 'key ' . (to_format [hex => $self->raw_key]) . ':';
212 0 push @result, '> ' . to_format [hex => $self->raw_value];
213 }
214 else {
215 11 push @result, to_format [hex => $self->raw_value];
216 }
217  
218 11 return join "\n", @result;
219 }
220  
221 9 1;
222  
223 __END__
« Crypto/Transaction/Signer.pm ↑ Summary Crypto/Constants.pm »