Crypto/Transaction/Digest.pm

filter: Statementstmt 99.4% 173 / 174 Branchbran 89.5% 43 / 48 Conditioncond 89.6% 26 / 29 MC/DCmcdc 77.7% 14 / 18 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 1 totaltotal 95.1% 272 / 286 CC 38 SCAR 22.4
CC
38
Cov
96%
_get_digest_taproot
14.2
_get_digest_default
13.0
_get_digest_segwit
10.0
CRAP 9.3
« Crypto/Helpers.pm ↑ Summary Crypto/Util/Internal.pm »
1 package Bitcoin::Crypto::Transaction::Digest;
2 34 $Bitcoin::Crypto::Transaction::Digest::VERSION = '4.005';
3 34 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 34 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 34 ▶use Crypt::Digest::SHA256 qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Bitcoin::Crypto::Helpers qw(ensure_length);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Bitcoin::Crypto::Util::Internal qw(hash256 pack_compactsize);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Bitcoin::Crypto::Constants qw(:sighash);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use Bitcoin::Crypto::Transaction::Digest::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use Bitcoin::Crypto::Transaction::Digest::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 34 has param 'transaction' => (
18 isa => InstanceOf ['Bitcoin::Crypto::Transaction'],
19 weak_ref => 1,
20 );
21  
22 34 has field 'config' => (
23 coerce => (InstanceOf ['Bitcoin::Crypto::Transaction::Digest::Config'])
24 ->plus_coercions(HashRef, q{Bitcoin::Crypto::Transaction::Digest::Config->new($_)}),
25 writer => 1,
26 handles => [
27 qw(
28 signing_index
29 signing_subscript
30 sighash
31 _default_sighash
32 taproot_ext_flag
33 taproot_ext
34 taproot_annex
35 )
36 ],
37 );
38  
39 has field '_cache' => (
40 isa => HashRef,
41 99 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 34 );
43  
44 sub get_digest
45 213 ▶{
Subroutine get_digest: called CC=4 SCAR=13.9
Pod undocumented
46 213 my ($self) = @_;
47 213 my $input = $self->transaction->inputs->[$self->signing_index];
48  
49 213 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 213 213
50 "can't find input with index " . $self->signing_index
51 ) unless $input;
52  
53 213 my $procedure = '_get_digest_default';
54 213 ▶ if ($input->is_taproot) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input->is_taproot) { } 56 157 213
elsif ($input->is_segwit) { } 83 74 157
55 56 $procedure = '_get_digest_taproot';
56 }
57 elsif ($input->is_segwit) {
58 83 $procedure = '_get_digest_segwit';
59 }
60  
61 213 return $self->$procedure();
62 }
63  
64 sub _get_digest_default
65 74 ▶{
Subroutine _get_digest_default: called CC=13 SCAR=25.6
66 74 my ($self) = @_;
67  
68 74 my $sighash = $self->_default_sighash(SIGHASH_ALL);
69  
70 74 my $sighash_type = $sighash & 31;
71 74 my $anyonecanpay = $sighash & SIGHASH_ANYONECANPAY;
72  
73 74 my $transaction = $self->transaction;
74 74 my $tx_copy = $transaction->clone;
75  
76 74 my $signing_index = $self->signing_index;
77 74 my $inputs = $transaction->inputs;
78 74 my $copy_inputs = $tx_copy->inputs;
79 74 @{$copy_inputs} = ();
80  
81 74 foreach my $input_ind (keys @{$inputs}) {
82  
83 # no other inputs if anyonecanpay - skip cloning and all other work
84 134 ▶ next if $anyonecanpay && $input_ind != $signing_index;
Branch: 1 branch
BranchTrueFalseTotal
if $anyonecanpay and $input_ind != $signing_index 2 132 134
Condition100%3/3
$anyonecanpay and $input_ind != $signing_index
!ll&&!rl&&r
12932
MC/DC100%2/2
$anyonecanpay and $input_ind != $signing_index
$anyonecanpay $input_ind != $signing_index
Truth table100%3/3
A and B
A= $anyonecanpayB= $input_ind != $signing_index
ABresult
0X0
100
111
85  
86 132 my $input = $inputs->[$input_ind]->clone;
87  
88 132 ▶ if ($input_ind == $signing_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_ind == $signing_index) { } 74 58 132
89 74 my $subscript = $self->signing_subscript;
90 74 ▶ if (!$subscript) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscript) 4 70 74
91 4 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $input->utxo->output->is_standard 0 4 4
92 "can't guess the subscript from a non-standard transaction"
93 ) unless $input->utxo->output->is_standard;
94  
95 4 $subscript = $input->script_base;
96 }
97  
98 74 $input->set_signature_script($subscript);
99 }
100 else {
101 58 $input->set_signature_script('');
102 58 ▶ $input->set_sequence_no(0)
Branch: 1 branch
BranchTrueFalseTotal
if $sighash_type == 2 or $sighash_type == 3 27 31 58
Condition100%3/3
$sighash_type == 2 or $sighash_type == 3
l!l&&r!l&&!r
141331
MC/DC100%2/2
$sighash_type == 2 or $sighash_type == 3
$sighash_type == 2 $sighash_type == 3
Truth table100%3/3
A or B
A= $sighash_type == 2B= $sighash_type == 3
ABresult
1X1
011
000
103 if $sighash_type == SIGHASH_NONE
104 || $sighash_type == SIGHASH_SINGLE;
105 }
106  
107 132 push @{$copy_inputs}, $input;
108 }
109  
110 # Handle output work for sighashes
111 74 ▶ if ($sighash_type == SIGHASH_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sighash_type == 2) { } 10 64 74
elsif ($sighash_type == 3) { } 9 55 64
112 10 @{$tx_copy->outputs} = ();
113 }
114 elsif ($sighash_type == SIGHASH_SINGLE) {
115 9 my $outputs = $transaction->outputs;
116 9 my $copy_outputs = $tx_copy->outputs;
117 9 @{$copy_outputs} = ();
118  
119 9 ▶ if ($signing_index >= @{$outputs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signing_index >= @{$outputs;}) 4 5 9
120  
121 # this should verify with constant digest (without hashing)
122 4 return Bitcoin::Crypto::Transaction::Digest::Result->new(
123 hash => scalar reverse ensure_length("\x01", 32),
124 );
125 }
126  
127 5 foreach my $output (@{$outputs}[0 .. $signing_index - 1]) {
128 1 my $output_copy = $output->clone;
129 1 $output_copy->set_locking_script('');
130 1 $output_copy->set_max_value;
131 1 push @{$copy_outputs}, $output_copy;
132 }
133  
134 5 push @{$copy_outputs}, $outputs->[$signing_index];
135 }
136  
137 70 my $serialized = $tx_copy->to_serialized(witness => 0);
138 70 $serialized .= pack 'V', $sighash;
139  
140 70 return Bitcoin::Crypto::Transaction::Digest::Result->new(preimage => $serialized);
141 }
142  
143 sub _get_digest_segwit
144 83 ▶{
Subroutine _get_digest_segwit: called CC=10 SCAR=23.0
145 83 my ($self) = @_;
146  
147 83 my $sighash = $self->_default_sighash(SIGHASH_ALL);
148  
149 83 my $sighash_type = $sighash & 31;
150 83 my $anyonecanpay = $sighash & SIGHASH_ANYONECANPAY;
151  
152 83 my $signing_index = $self->signing_index;
153 83 my $transaction = $self->transaction;
154 83 my @inputs = @{$transaction->inputs};
155 83 my $this_input = $inputs[$signing_index]->clone;
156 83 $inputs[$signing_index] = $this_input;
157  
158 83 my $empty_hash = "\x00" x 32;
159 83 my $single = $sighash_type == SIGHASH_SINGLE;
160 83 my $none = $sighash_type == SIGHASH_NONE;
161  
162 # NOTE: sets witness for proper behavior of _script_code in
163 # Bitcoin::Crypto::Transaction::Input for P2WSH
164 83 ▶ $this_input->set_witness([$self->signing_subscript])
Branch: 1 branch
BranchTrueFalseTotal
if $self->signing_subscript 81 2 83
165 if $self->signing_subscript;
166  
167 # According to https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki
168 # Double SHA256 of the serialization of:
169 # 1. nVersion of the transaction (4-byte little endian)
170 # 2. hashPrevouts (32-byte hash)
171 # 3. hashSequence (32-byte hash)
172 # 4. outpoint (32-byte hash + 4-byte little endian)
173 # 5. scriptCode of the input (serialized as scripts inside CTxOuts)
174 # 6. value of the output spent by this input (8-byte little endian)
175 # 7. nSequence of the input (4-byte little endian)
176 # 8. hashOutputs (32-byte hash)
177 # 9. nLocktime of the transaction (4-byte little endian)
178 # 10. sighash type of the signature (4-byte little endian)
179  
180 83 my $serialized = '';
181 83 $serialized .= pack 'V', $transaction->version;
182  
183 # handle prevouts
184 $serialized .= $anyonecanpay
185 ? $empty_hash
186 83 ▶ : hash256(join '', map { $_->prevout } @inputs)
Branch: 1 branch
BranchTrueFalseTotal
$anyonecanpay ? : 15 68 83
187 ;
188  
189 # handle sequences
190 $serialized .= $anyonecanpay || $single || $none
191 ? $empty_hash
192 83 ▶ : hash256(join '', map { pack 'V', $_->sequence_no } @inputs)
Branch: 1 branch
BranchTrueFalseTotal
$anyonecanpay || $single || $none ? : 25 58 83
Condition100%6/6
$anyonecanpay or $single or $none
l!l&&r!l&&!r
21458
$anyonecanpay or $single
l!l&&r!l&&!r
15662
MC/DC100%3/3
$anyonecanpay or $single or $none
$anyonecanpay $single $none
Truth table100%4/4
A or B or C
A= $anyonecanpayB= $singleC= $none
ABCresult
1XX1
01X1
0011
0000
193 ;
194  
195 83 $serialized .= $this_input->prevout;
196  
197 83 my $script_base = $this_input->script_base->to_serialized;
198 83 $serialized .= pack_compactsize(length $script_base);
199 83 $serialized .= $script_base;
200  
201 83 $serialized .= $this_input->utxo->output->value_serialized;
202 83 $serialized .= pack 'V', $this_input->sequence_no;
203  
204 # handle outputs
205 83 ▶ if (!$single && !$none) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $single || $none) { } 67 16 83
elsif ($single and $signing_index < @{$transaction->outputs;}) { } 9 7 16
Condition100%3/3
$single || $none
l!l&&r!l&&!r
11567
MC/DC100%2/2
$single || $none
$single $none
Truth table100%3/3
A || B
A= $singleB= $none
ABresult
1X1
011
000
206 67 $serialized .= hash256(join '', map { $_->to_serialized } @{$transaction->outputs});
207 }
208 11 ▶ elsif ($single && $signing_index < @{$transaction->outputs}) {
Condition100%3/3
$single and $signing_index < @{$transaction->outputs;}
!ll&&!rl&&r
529
MC/DC100%2/2
$single and $signing_index < @{$transaction->outputs;}
$single $signing_index < @{$transaction->outputs;}
Truth table100%3/3
A and B
A= $singleB= $signing_index < @{$transaction->outputs;}
ABresult
0X0
100
111
209 9 $serialized .= hash256($transaction->outputs->[$signing_index]->to_serialized);
210 }
211 else {
212 7 $serialized .= $empty_hash;
213 }
214  
215 83 $serialized .= pack 'VV', $transaction->locktime, $sighash;
216  
217 83 return Bitcoin::Crypto::Transaction::Digest::Result->new(preimage => $serialized);
218 }
219  
220 sub _get_digest_taproot
221 56 ▶{
Subroutine _get_digest_taproot: called CC=14 SCAR=26.5
222 56 my ($self) = @_;
223  
224 56 my $sighash = $self->_default_sighash(SIGHASH_DEFAULT);
225  
226 56 my $sighash_type = $sighash & 3;
227 56 my $anyonecanpay = $sighash & SIGHASH_ANYONECANPAY;
228  
229 56 my $signing_index = $self->signing_index;
230 56 my $transaction = $self->transaction;
231 56 my $this_input = $transaction->inputs->[$signing_index];
232 56 my $annex = $self->taproot_annex;
233 56 my $ext_flag = $self->taproot_ext_flag;
234  
235 state $allowed_sighash = {
236 56 map { $_ => !!1 } (
237 SIGHASH_DEFAULT,
238 SIGHASH_ALL,
239 SIGHASH_SINGLE,
240 SIGHASH_NONE,
241 )
242 };
243  
244 Bitcoin::Crypto::Exception::Transaction->raise(
245 "can't digest taproot transaction with unknown SIGHASH"
246 56 ▶ ) unless $allowed_sighash->{$sighash_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed_sighash->{$sighash_type} 0 56 56
247  
248 # According to https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki
249 # SHA256 of the serialization of:
250 # hash_type (1).
251 # nVersion (4): the nVersion of the transaction.
252 # nLockTime (4): the nLockTime of the transaction.
253 # If the hash_type & 0x80 does not equal SIGHASH_ANYONECANPAY:
254 # - sha_prevouts (32): the SHA256 of the serialization of all input outpoints.
255 # - sha_amounts (32): the SHA256 of the serialization of all input amounts.
256 # - sha_scriptpubkeys (32): the SHA256 of all spent outputs' scriptPubKeys, serialized as script inside CTxOut.
257 # - sha_sequences (32): the SHA256 of the serialization of all input nSequence.
258 # If hash_type & 3 does not equal SIGHASH_NONE or SIGHASH_SINGLE:
259 # - sha_outputs (32): the SHA256 of the serialization of all outputs in CTxOut format.
260 # spend_type (1): equal to (ext_flag * 2) + annex_present, where annex_present is 0 if no annex is present, or 1 otherwise (the original witness stack has two or more witness elements, and the first byte of the last element is 0x50)
261 # If hash_type & 0x80 equals SIGHASH_ANYONECANPAY:
262 # - outpoint (36): the COutPoint of this input (32-byte hash + 4-byte little-endian).
263 # - amount (8): value of the previous output spent by this input.
264 # - scriptPubKey (35): scriptPubKey of the previous output spent by this input, serialized as script inside CTxOut. Its size is always 35 bytes.
265 # - nSequence (4): nSequence of this input.
266 # If hash_type & 0x80 does not equal SIGHASH_ANYONECANPAY:
267 # - input_index (4): index of this input in the transaction input vector. Index of the first input is 0.
268 # If an annex is present (the lowest bit of spend_type is set):
269 # - sha_annex (32): the SHA256 of (compact_size(size of annex) || annex), where annex includes the mandatory 0x50 prefix.
270 # If hash_type & 3 equals SIGHASH_SINGLE:
271 # - sha_single_output (32): the SHA256 of the corresponding output in CTxOut format.
272  
273 # zero is sighash epoch
274 56 my $serialized = pack 'CCVV', 0, $sighash, $transaction->version, $transaction->locktime;
275  
276 56 ▶ if (!$anyonecanpay) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($anyonecanpay) 42 14 56
277 42 ▶ $serialized .= $self->_cache->{taproot_common_tx_data} //= do {
Condition66%2/3
$self->_cache->{"taproot_common_tx_data"} //= do { my(@prevouts, @amounts); my @pubkeys; my @sequences; foreach my $input (@{$transaction->inputs;}) { my $utxo_output = $input->utxo->output; my $pubkey = $utxo_output->locking_script->to_serialized; push @prevouts, $input->prevout; push @amounts, $utxo_output->value_serialized; push @pubkeys, pack_compactsize(length $pubkey) . $pubkey; push @sequences, pack("V", $input->sequence_no); } ; sha256(join("", @prevouts)) . sha256(join("", @amounts)) . sha256(join("", @pubkeys)) . sha256(join("", @sequences)) }
l!l&&r!l&&!r
21210
MC/DC0%0/2
$self->_cache->{"taproot_common_tx_data"} //= do { my(@prevouts, @amounts); my @pubkeys; my @sequences; foreach my $input (@{$transaction->inputs;}) { my $utxo_output = $input->utxo->output; my $pubkey = $utxo_output->locking_script->to_serialized; push @prevouts, $input->prevout; push @amounts, $utxo_output->value_serialized; push @pubkeys, pack_compactsize(length $pubkey) . $pubkey; push @sequences, pack("V", $input->sequence_no); } ; sha256(join("", @prevouts)) . sha256(join("", @amounts)) . sha256(join("", @pubkeys)) . sha256(join("", @sequences)) }
$self->_cache->{"taproot_common_tx_data"} do { my(@prevouts, @amounts); my @pubkeys; my @sequences; foreach my $input (@{$transaction->inputs;}) { my $utxo_output = $input->utxo->output; my $pubkey = $utxo_output->locking_script->to_serialized; push @prevouts, $input->prevout; push @amounts, $utxo_output->value_serialized; push @pubkeys, pack_compactsize(length $pubkey) . $pubkey; push @sequences, pack("V", $input->sequence_no); } ; sha256(join("", @prevouts)) . sha256(join("", @amounts)) . sha256(join("", @pubkeys)) . sha256(join("", @sequences)) }
Truth table66%2/3
A //= B
A= $self->_cache->{"taproot_common_tx_data"}B= do { my(@prevouts, @amounts); my @pubkeys; my @sequences; foreach my $input (@{$transaction->inputs;}) { my $utxo_output = $input->utxo->output; my $pubkey = $utxo_output->locking_script->to_serialized; push @prevouts, $input->prevout; push @amounts, $utxo_output->value_serialized; push @pubkeys, pack_compactsize(length $pubkey) . $pubkey; push @sequences, pack("V", $input->sequence_no); } ; sha256(join("", @prevouts)) . sha256(join("", @amounts)) . sha256(join("", @pubkeys)) . sha256(join("", @sequences)) }
ABresult
1X1
011
000
278 21 my @prevouts;
279 my @amounts;
280 21 my @pubkeys;
281 21 my @sequences;
282 21 foreach my $input (@{$transaction->inputs}) {
283 80 my $utxo_output = $input->utxo->output;
284 80 my $pubkey = $utxo_output->locking_script->to_serialized;
285  
286 80 push @prevouts, $input->prevout;
287 80 push @amounts, $utxo_output->value_serialized;
288 80 push @pubkeys, pack_compactsize(length $pubkey) . $pubkey;
289 80 push @sequences, pack 'V', $input->sequence_no;
290 }
291  
292 21 sha256(join '', @prevouts)
293 . sha256(join '', @amounts)
294 . sha256(join '', @pubkeys)
295 . sha256(join '', @sequences);
296 };
297 }
298  
299 my $outputs = $self->_cache->{taproot_outputs} //= [
300 56 ▶ map { $_->to_serialized } @{$transaction->outputs}
Condition100%2/2
$self->_cache->{"taproot_outputs"} //= [map({$_->to_serialized;} @{$transaction->outputs;})]
l!l
3125
MC/DC100%1/1
$self->_cache->{"taproot_outputs"} //= [map({$_->to_serialized;} @{$transaction->outputs;})]
$self->_cache->{"taproot_outputs"}
Truth table100%2/2
A
A= $self->_cache->{"taproot_outputs"}
Aresult
11
00
301 ];
302  
303 56 ▶ if ($sighash_type != SIGHASH_NONE && $sighash_type != SIGHASH_SINGLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sighash_type != 2 and $sighash_type != 3) 42 14 56
Condition100%3/3
$sighash_type != 2 and $sighash_type != 3
!ll&&!rl&&r
6842
MC/DC100%2/2
$sighash_type != 2 and $sighash_type != 3
$sighash_type != 2 $sighash_type != 3
Truth table100%3/3
A and B
A= $sighash_type != 2B= $sighash_type != 3
ABresult
0X0
100
111
304 42 $serialized .= sha256(join '', @$outputs);
305 }
306  
307 56 $serialized .= pack 'C', $ext_flag * 2 + defined $annex;
308  
309 56 ▶ if ($anyonecanpay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anyonecanpay) { } 14 42 56
310 14 my $utxo_output = $this_input->utxo->output;
311 14 my $pubkey = $utxo_output->locking_script->to_serialized;
312  
313 14 $serialized .= join '',
314 $this_input->prevout,
315 $utxo_output->value_serialized,
316 pack_compactsize(length $pubkey),
317 $pubkey,
318 pack 'V', $this_input->sequence_no;
319 }
320 else {
321 42 $serialized .= pack 'V', $signing_index;
322 }
323  
324 56 ▶ if (defined $annex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $annex) 0 56 56
325 0 $serialized .= sha256(pack_compactsize(length $annex) . $annex);
326 }
327  
328 56 ▶ if ($sighash_type == SIGHASH_SINGLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sighash_type == 3) 8 48 56
329 8 ▶ Bitcoin::Crypto::Exception::Transaction->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $signing_index < @$outputs 0 8 8
330 "can't digest taproot transaction with SIGHASH_SINGLE without corresponding output"
331 ) unless $signing_index < @$outputs;
332  
333 8 $serialized .= sha256($outputs->[$self->signing_index]);
334 }
335  
336 # BIP342 extension
337 56 ▶ if ($ext_flag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext_flag == 1) 16 40 56
338 16 ▶ $serialized .= $self->taproot_ext
Condition33%1/3
$self->taproot_ext // "Bitcoin::Crypto::Exception::Transaction"->raise("missing taproot extension for ext_flag=1")
l!l&&r!l&&!r
1600
MC/DC0%0/2
$self->taproot_ext // "Bitcoin::Crypto::Exception::Transaction"->raise("missing taproot extension for ext_flag=1")
$self->taproot_ext "Bitcoin::Crypto::Exception::Transaction"->raise("missing taproot extension for ext_flag=1")
Truth table33%1/3
A // B
A= $self->taproot_extB= "Bitcoin::Crypto::Exception::Transaction"->raise("missing taproot extension for ext_flag=1")
ABresult
1X1
011
000
339 // Bitcoin::Crypto::Exception::Transaction->raise('missing taproot extension for ext_flag=1');
340 }
341  
342 56 return Bitcoin::Crypto::Transaction::Digest::Result->new(
343 taproot => !!1,
344 preimage => $serialized,
345 );
346 }
347  
348 34 1;
349  
« Crypto/Helpers.pm ↑ Summary Crypto/Util/Internal.pm »