Crypto/Transaction/Input.pm

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