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