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.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 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 {
38 907 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
39  
40 907 ▶ 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) 469 438 907
Condition100%3/3
defined $self->raw_key and not $self->type->has_key_data
!ll&&!rl&&r
320118469
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 469 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $self->raw_key 10 459 469
42 'Field ' . $self->type->name . ' does not define key data'
43 ) if length $self->raw_key;
44  
45 459 $self->_set_raw_key(undef);
46 }
47  
48 897 ▶ if (defined $args->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"value"}) 320 577 897
49 320 $self->set_value($args->{value});
50 }
51  
52 897 ▶ if (defined $args->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"key"}) 73 824 897
53 73 $self->set_key($args->{key});
54 }
55  
56 }
57  
58 sub validate
59 {
60 897 ▶ my ($self) = @_;
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
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 sub {
66 897 ▶ my @args = ($self->value);
Subroutine __ANON__: called CC=3 SCAR=11.0
67 895 ▶ if ($self->type->has_key_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type->has_key_data) 192 703 895
68 192 unshift @args, $self->key;
69 }
70  
71 887 ▶ $self->type->validator->(@args)
Branch: 1 branch
BranchTrueFalseTotal
if $self->type->has_validator 101 786 887
72 if $self->type->has_validator;
73 },
74 897 'validation failed for type ' . $self->type->name
75 );
76  
77 879 return $self;
78 }
79  
80 sub key
81 {
82 519 ▶ my ($self) = @_;
Subroutine key: called CC=1 SCAR=0.0
Pod undocumented
83  
84 519 return $self->type->key_deserializer->($self->raw_key);
85 }
86  
87 sub set_raw_key
88 {
89 75 ▶ my ($self, $key) = @_;
Subroutine set_raw_key: called CC=3 SCAR=11.0
Pod undocumented
90  
91 75 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type->has_key_data 0 75 75
92 'Field ' . $self->type->name . ' does not define key data'
93 ) if !$self->type->has_key_data;
94  
95 75 $self->_set_raw_key($key);
96 75 ▶ $self->map->_check_integrity($self)
Branch: 1 branch
BranchTrueFalseTotal
if $self->map 2 73 75
97 if $self->map;
98  
99 75 return;
100 }
101  
102 sub set_key
103 {
104 75 ▶ my ($self, $key) = @_;
Subroutine set_key: called CC=1 SCAR=0.0
Pod undocumented
105  
106 75 $self->set_raw_key($self->type->key_serializer->($key));
107 75 return;
108 }
109  
110 sub value
111 {
112 1862 ▶ my ($self) = @_;
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
113  
114 1862 return $self->type->deserializer->($self->raw_value);
115 }
116  
117 sub set_value
118 {
119 342 ▶ my ($self, $value) = @_;
Subroutine set_value: called CC=1 SCAR=0.0
Pod undocumented
120  
121 342 $self->set_raw_value($self->type->serializer->($value));
122 342 return;
123 }
124  
125 sub set_map
126 {
127 897 ▶ my ($self, $map) = @_;
Subroutine set_map: called CC=1 SCAR=0.0
Pod undocumented
128  
129 897 $self->validate;
130 879 $self->_set_map($map);
131 879 $map->_check_integrity($self);
132 878 return;
133 }
134  
135 sub serialized_key
136 {
137 1252 ▶ my ($self) = @_;
Subroutine serialized_key: called CC=2 SCAR=6.9
Pod documented
138  
139 1252 ▶ return pack_compactsize($self->type->code) . ($self->raw_key // '');
Condition100%2/2
$self->raw_key // ""
l!l
292960
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 {
155 587 ▶ my ($class, $serialized, $args) = @_;
Subroutine from_serialized: called CC=6 SCAR=18.4
156 587 my $partial = !!$args->{pos};
157 587 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 587 0 587
158  
159 587 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < length $serialized 0 587 587
160 'field expected but end of stream was reached'
161 ) unless $pos < length $serialized;
162  
163 587 my $keylen = unpack_compactsize $serialized, \$pos;
164 587 my $keydata = substr $serialized, $pos, $keylen;
165 587 my $keytype = unpack_compactsize $keydata, \(my $keytype_pos = 0);
166 587 $keydata = substr $keydata, $keytype_pos;
167 587 $pos += $keylen;
168  
169 587 my $valuelen = unpack_compactsize $serialized, \$pos;
170 587 my $valuedata = substr $serialized, $pos, $valuelen;
171 587 $pos += $valuelen;
172  
173 my $self = $class->new(
174 587 type => [$args->{map_type}, $keytype],
175 raw_key => $keydata,
176 raw_value => $valuedata,
177 );
178  
179 577 ▶ Bitcoin::Crypto::Exception::PSBT->raise(
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $serialized 0 577 577
Condition33%1/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
57700
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 577 ▶ ${$args->{pos}} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if $partial 577 0 577
184 if $partial;
185  
186 577 return $self;
187 }
188  
189 sub to_serialized
190 {
191 935 ▶ my ($self) = @_;
Subroutine to_serialized: called CC=1 SCAR=0.0
Pod documented
192  
193 935 my $enckey = $self->serialized_key;
194 935 my $value = $self->raw_value;
195  
196 935 return join '',
197 pack_compactsize(length $enckey),
198 $enckey,
199 pack_compactsize(length $value),
200 $value
201 ;
202 }
203  
204 sub dump
205 {
206 11 ▶ my ($self) = @_;
Subroutine dump: called CC=2 SCAR=7.6
Pod documented
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 »