Crypto/Transaction/Input.pm

filter: Statementstmt 94.0% 141 / 150 Branchbran 67.3% 31 / 46 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 28 / 28 Podpod 66.6% 6 / 9 totaltotal 83.9% 214 / 255 CC 31 SCAR 19.3
CC
31
Cov
85%
dump
21.9
from_serialized
6.4
is_segwit
6.0
CRAP 6.9
« Crypto/Util/Internal.pm ↑ Summary Crypto/Tapscript/Opcode.pm »
1 package Bitcoin::Crypto::Transaction::Input;
2 64 $Bitcoin::Crypto::Transaction::Input::VERSION = '4.005';
3 64 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 64 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 64 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 64 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 64 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 64 ▶use Bitcoin::Crypto qw(btc_script btc_utxo);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 64 ▶use Bitcoin::Crypto::Constants qw(:transaction);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 64 ▶use Bitcoin::Crypto::Util::Internal qw(to_format pack_compactsize unpack_compactsize);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 64 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 64 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 64 ▶use Bitcoin::Crypto::Script::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 64 has param 'utxo_location' => (
19 coerce => Tuple [ByteStr, PositiveOrZeroInt],
20 );
21  
22 64 has option 'utxo' => (
23 isa => InstanceOf ['Bitcoin::Crypto::Transaction::UTXO'],
24 lazy => 1,
25 );
26  
27 64 has param 'signature_script' => (
28 writer => 1,
29 coerce => BitcoinScript,
30 default => '',
31 );
32  
33 64 has param 'sequence_no' => (
34 isa => IntMaxBits [32],
35 writer => 1,
36 default => MAX_SEQUENCE_NO,
37 );
38  
39 has param 'witness' => (
40 coerce => ArrayRef [ByteStr],
41 writer => 1,
42 709 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 64 );
44  
45 64 with qw(
46 Bitcoin::Crypto::Role::ShallowClone
47 );
48  
49 sub has_witness
50 241 ▶{
Subroutine has_witness: called CC=1 SCAR=0.0
Pod undocumented
51 241 return @{shift->witness} > 0;
52 }
53  
54 sub _nested_script
55 211 ▶{
Subroutine _nested_script: called CC=3 SCAR=11.0
56 211 my ($self) = @_;
57  
58 211 my $input_script = $self->signature_script->to_serialized;
59 211 ▶ return undef unless length $input_script;
Branch: 1 branch
BranchTrueFalseTotal
unless length $input_script 19 192 211
60  
61 192 my $push = substr $input_script, 0, 1, '';
62 192 ▶ return undef unless ord $push == length $input_script;
Branch: 1 branch
BranchTrueFalseTotal
unless ord $push == length $input_script 31 161 192
63  
64 161 my $real_script = btc_script->from_serialized($input_script);
65 161 return $real_script;
66 }
67  
68 sub _build_utxo
69 108 ▶{
Subroutine _build_utxo: called CC=1 SCAR=0.0
70 108 my ($self) = @_;
71  
72 108 return btc_utxo->get(@{$self->utxo_location});
73 }
74  
75 711 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
76 711 my ($orig, $class, %params) = @_;
77 711 my $utxo = $params{utxo};
78  
79 711 ▶ if (ref $utxo eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $utxo eq 'ARRAY') { } 680 31 711
elsif (builtin::blessed($utxo) and $utxo->isa('Bitcoin::Crypto::Transaction::UTXO')) { } 31 0 31
80 680 delete $params{utxo};
81  
82 return {
83 680 %params,
84 utxo_location => $utxo,
85 };
86 }
87 ▶ elsif (blessed $utxo && $utxo->isa('Bitcoin::Crypto::Transaction::UTXO')) {
Condition33%1/3
builtin::blessed($utxo) and $utxo->isa("Bitcoin::Crypto::Transaction::UTXO")
!ll&&!rl&&r
0031
MC/DC0%0/2
builtin::blessed($utxo) and $utxo->isa("Bitcoin::Crypto::Transaction::UTXO")
builtin::blessed($utxo) $utxo->isa("Bitcoin::Crypto::Transaction::UTXO")
Truth table33%1/3
A and B
A= builtin::blessed($utxo)B= $utxo->isa("Bitcoin::Crypto::Transaction::UTXO")
ABresult
0X0
100
111
88 return {
89 31 %params,
90 utxo_location => [$utxo->txid, $utxo->output_index],
91 };
92 }
93  
94 0 return $class->$orig(%params);
95 64 };
96  
97 sub utxo_registered
98 8 ▶{
Subroutine utxo_registered: called CC=2 SCAR=6.9
Pod documented
99 8 my ($self) = @_;
100  
101 8 try {
102 8 ▶ $self->utxo unless $self->has_utxo;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_utxo 5 3 8
103 3 return !!1;
104 }
105 catch ($e) {
106 5 return !!0;
107 }
108 }
109  
110 sub to_serialized
111 571 ▶{
Subroutine to_serialized: called CC=1 SCAR=0.0
Pod documented
112 571 my ($self) = @_;
113  
114 # input should be serialized as follows:
115 # - transaction hash, 32 bytes
116 # - transaction output index, 4 bytes
117 # - signature script length, 1-9 bytes
118 # - signature script
119 # - sequence number, 4 bytes
120 571 my $serialized = $self->prevout;
121  
122 571 my $script = $self->signature_script->to_serialized;
123 571 $serialized .= pack_compactsize(length $script) . $script;
124  
125 571 $serialized .= pack 'V', $self->sequence_no;
126  
127 571 return $serialized;
128 }
129  
130 64 signature_for from_serialized => (
131 method => !!1,
132 head => [ByteStr],
133 named => [
134 pos => Maybe [ScalarRef [PositiveOrZeroInt]],
135 {default => undef},
136 ],
137 bless => !!0,
138 );
139  
140 sub from_serialized
141 633 ▶{
Subroutine from_serialized: called CC=6 SCAR=18.6
142 633 my ($class, $serialized, $args) = @_;
143 633 my $partial = !!$args->{pos};
144 633 ▶ my $pos = $partial ? ${$args->{pos}} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 633 0 633
145  
146 633 my ($transaction_hash, $transaction_output_index) = unpack "\@$pos a32V", $serialized;
147 633 $transaction_hash = reverse $transaction_hash;
148 633 $pos += 36;
149  
150 633 my $script_size = unpack_compactsize $serialized, \$pos;
151  
152 633 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $script_size > length $serialized 0 633 633
153 'serialized input script data is corrupted'
154 ) if $pos + $script_size > length $serialized;
155  
156 633 my ($script, $sequence) = unpack "\@$pos a${script_size}V", $serialized;
157 633 $pos += $script_size + 4;
158  
159 633 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $serialized 0 633 633
Condition33%1/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
63300
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
160 'serialized input data is corrupted'
161 ) if !$partial && $pos != length $serialized;
162  
163 633 ▶ ${$args->{pos}} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if $partial 633 0 633
164 if $partial;
165  
166 633 return $class->new(
167 utxo => [$transaction_hash, $transaction_output_index],
168 signature_script => $script,
169 sequence_no => $sequence,
170 );
171 }
172  
173 sub is_segwit
174 602 ▶{
Subroutine is_segwit: called CC=6 SCAR=18.0
Pod documented
175 602 my ($self) = @_;
176  
177 # Determines whether this script is segwit (including nested variants).
178 # There's no need to verify P2SH hash matching, as it will be checked at a
179 # later stage. It's enough if the input promises the segwit format.
180  
181 602 my $output_script = $self->utxo->output->locking_script;
182 602 ▶ return !!1 if $output_script->is_native_segwit;
Branch: 1 branch
BranchTrueFalseTotal
if $output_script->is_native_segwit 252 350 602
183 350 ▶ return !!0 unless ($output_script->type // '') eq 'P2SH';
Branch: 1 branch
BranchTrueFalseTotal
unless ($output_script->type // "") eq "P2SH" 176 174 350
Condition100%2/2
$output_script->type // ""
l!l
29555
MC/DC100%1/1
$output_script->type // ""
$output_script->type
Truth table100%2/2
A
A= $output_script->type
Aresult
11
00
184  
185 174 my $nested = $self->_nested_script;
186 174 ▶ return !!0 unless defined $nested;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nested 50 124 174
187 124 ▶ return !!1 if $nested->is_native_segwit;
Branch: 1 branch
BranchTrueFalseTotal
if $nested->is_native_segwit 124 0 124
188  
189 0 return !!0;
190 }
191  
192 sub is_taproot
193 213 ▶{
Subroutine is_taproot: called CC=1 SCAR=0.0
Pod documented
194 213 return shift->utxo->output->locking_script->is_taproot;
195 }
196  
197 sub prevout
198 1013 ▶{
Subroutine prevout: called CC=1 SCAR=0.0
Pod documented
199 1013 my ($txid, $index) = @{shift->utxo_location};
200  
201 1013 return pack 'a32V', scalar(reverse $txid), $index;
202 }
203  
204 sub serialized_witness
205 224 ▶{
Subroutine serialized_witness: called CC=1 SCAR=0.0
Pod undocumented
206 224 my ($self) = @_;
207  
208 224 my $witness = $self->witness;
209  
210 return join '',
211 224 pack_compactsize(scalar @{$witness}),
212 224 (map { pack_compactsize(length $_) . $_ } @{$witness}),
213 ;
214 }
215  
216 sub script_base
217 87 ▶{
Subroutine script_base: called CC=3 SCAR=11.0
Pod undocumented
218 87 my ($self) = @_;
219  
220 87 ▶ if ($self->is_segwit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_segwit) { } 83 4 87
221 83 my $utxo = $self->utxo;
222  
223 83 my $locking_script = $utxo->output->locking_script;
224 83 my $program;
225 my %types = (
226 27 ▶ P2WPKH => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
227  
228 # get script hash from P2WPKH (ignore the first two OPs - version and push)
229 27 my $hash = substr $locking_script->to_serialized, 2;
230 27 $program = Bitcoin::Crypto::Script::Common->new(PKH => $hash);
231 },
232 56 ▶ P2WSH => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
233  
234 # NOTE: Transaction::Digest sets witness to signing_subscript,
235 # which takes OP_CODESEPARATORs into account
236 56 ▶ $program = btc_script->from_serialized(($self->witness // [''])->[-1]);
Condition50%1/2
$self->witness // [""]
l!l
560
MC/DC0%0/1
$self->witness // [""]
$self->witness
Truth table50%1/2
A
A= $self->witness
Aresult
11
00
237 },
238 83 );
239  
240 83 my $type = $utxo->output->locking_script->type;
241  
242 83 ▶ if ($type eq 'P2SH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "P2SH") 37 46 83
243  
244 # nested - nothing should get here without checking if nested script is native segwit
245 37 my $nested = $self->_nested_script;
246 37 $type = $nested->type;
247  
248 37 $locking_script = $nested;
249 }
250  
251 83 $types{$type}->();
252 83 return $program;
253 }
254 else {
255 4 return $self->utxo->output->locking_script;
256 }
257 }
258  
259 sub dump
260 1 ▶{
Subroutine dump: called CC=12 SCAR=30.8
Pod documented
261 1 my ($self) = @_;
262  
263 1 ▶ my $utxo = $self->utxo_registered ? $self->utxo : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->utxo_registered ? : 1 0 1
264 1 my $utxo_location = $self->utxo_location;
265  
266 1 my @result;
267  
268 1 ▶ if ($utxo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utxo) { } 1 0 1
269 1 ▶ my $type = $utxo->output->locking_script->type // 'Custom';
Condition50%1/2
$utxo->output->locking_script->type // "Custom"
l!l
10
MC/DC0%0/1
$utxo->output->locking_script->type // "Custom"
$utxo->output->locking_script->type
Truth table50%1/2
A
A= $utxo->output->locking_script->type
Aresult
11
00
270 1 ▶ my $address = $utxo->output->locking_script->get_address // '';
Condition50%1/2
$utxo->output->locking_script->get_address // ""
l!l
10
MC/DC0%0/1
$utxo->output->locking_script->get_address // ""
$utxo->output->locking_script->get_address
Truth table50%1/2
A
A= $utxo->output->locking_script->get_address
Aresult
11
00
271 1 ▶ $address = " from $address" if $address;
Branch: 1 branch
BranchTrueFalseTotal
if $address 1 0 1
272 1 push @result, "$type Input$address";
273 }
274 else {
275 0 push @result, "Unknown Input (UTXO was not registered, data is incomplete)";
276 }
277  
278 1 push @result, 'spending output #' . $utxo_location->[1] . ' from ' . to_format([hex => $utxo_location->[0]]);
279 1 ▶ push @result, 'value: ' . $utxo->output->value
Branch: 1 branch
BranchTrueFalseTotal
if $utxo 1 0 1
280 if $utxo;
281 1 push @result, sprintf 'sequence: 0x%X', $self->sequence_no;
282 1 ▶ push @result, 'locking script: ' . $utxo->output->locking_script->dump
Branch: 1 branch
BranchTrueFalseTotal
if $utxo 1 0 1
283 if $utxo;
284  
285 1 ▶ if (!$self->signature_script->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->signature_script->is_empty) 1 0 1
286 1 push @result, 'signature script: ' . $self->signature_script->dump;
287 }
288  
289 1 ▶ if ($self->has_witness) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_witness) 0 1 1
290 0 push @result, 'witness: ';
291 0 foreach my $witness (@{$self->witness}) {
292 0 my $hex_el = to_format [hex => $witness];
293 0 ▶ $hex_el = '<empty>' unless length $hex_el;
Branch: 1 branch
BranchTrueFalseTotal
unless length $hex_el 0 0 0
294  
295 0 push @result, $hex_el;
296 }
297 }
298  
299 1 return join "\n", @result;
300 }
301  
302 64 1;
303  
304 __END__
« Crypto/Util/Internal.pm ↑ Summary Crypto/Tapscript/Opcode.pm »