Crypto/Transaction/AutoSigner.pm

filter: Statementstmt 98.8% 84 / 85 Branchbran 73.3% 22 / 30 Conditioncond 53.8% 7 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 1 totaltotal 85.7% 132 / 154 CC 23 SCAR 17.4
CC
23
Cov
88%
_sign_script
12.7
_sign_P2MS
6.0
_get_old_signature
5.1
CRAP 5.7
« Crypto/PSBT.pm ↑ Summary Crypto/Block.pm »
1 package Bitcoin::Crypto::Transaction::AutoSigner;
2 40 $Bitcoin::Crypto::Transaction::AutoSigner::VERSION = '4.005';
3 40 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 40 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Bitcoin::Crypto qw(btc_script);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Bitcoin::Crypto::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Bitcoin::Crypto::Helpers qw(die_no_trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 has param 'transaction' => (
15 isa => InstanceOf ['Bitcoin::Crypto::Transaction'],
16 );
17  
18 40 has param 'key' => (
19 isa => InstanceOf ['Bitcoin::Crypto::Key::Private'],
20 );
21  
22 40 has param 'signing_index' => (
23 isa => PositiveOrZeroInt,
24 );
25  
26 40 has option 'redeem_script' => (
27 coerce => BitcoinScript,
28 );
29  
30 40 has option 'multisig' => (
31 isa => Tuple [PositiveInt, PositiveInt],
32 );
33  
34 40 has option 'sighash' => (
35 isa => PositiveOrZeroInt,
36 writer => -hidden,
37 );
38  
39 40 has option 'script_tree' => (
40 coerce => BitcoinScriptTree,
41 );
42  
43 40 has field '_compat' => (
44 writer => 1,
45 default => !!0,
46 );
47  
48 has field 'input' => (
49 23 ▶ lazy => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 23 my $self = shift;
51 23 return $self->transaction->inputs->[$self->signing_index];
52 },
53 40 );
54  
55 sub _get_old_signature
56 7 ▶{
Subroutine _get_old_signature: called CC=5 SCAR=16.2
57 7 my ($self) = @_;
58  
59 7 ▶ if ($self->input->is_segwit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->input->is_segwit) { } 4 3 7
60 4 ▶ return [@{$self->input->witness // []}];
Condition50%1/2
$self->input->witness // []
l!l
40
MC/DC0%0/1
$self->input->witness // []
$self->input->witness
Truth table50%1/2
A
A= $self->input->witness
Aresult
11
00
61 }
62 else {
63 3 my $old_script = $self->input->signature_script->operations;
64 3 my @result;
65 3 foreach my $part (@$old_script) {
66 4 ▶ push @result, $part->[2]
Branch: 1 branch
BranchTrueFalseTotal
if $part->[0]->pushop 4 0 4
67 if $part->[0]->pushop;
68 }
69  
70 3 return \@result;
71 }
72 }
73  
74 sub _sign_P2PK
75 8 ▶{
Subroutine _sign_P2PK: called CC=1 SCAR=0.0
76 8 my ($self, $signature) = @_;
77  
78 8 $self->transaction
79 ->sign(
80 signing_index => $self->signing_index,
81 compat => $self->_compat,
82 )
83 ->add_signature($self->key, sighash => $self->sighash)
84 ->finalize;
85 }
86  
87 sub _sign_P2PKH
88 2 ▶{
Subroutine _sign_P2PKH: called CC=1 SCAR=0.0
89 2 my ($self) = @_;
90  
91 2 return $self->_sign_P2PK;
92 }
93  
94 sub _sign_P2MS
95 7 ▶{
Subroutine _sign_P2MS: called CC=6 SCAR=18.0
96 7 my ($self, $signature) = @_;
97  
98 7 ▶ die_no_trace 'trying to sign payout from P2MS but no multisig was specified'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_multisig 0 7 7
99 unless $self->has_multisig;
100  
101 7 my ($this_signature, $total_signatures) = @{$self->multisig};
102  
103 7 ▶ my $signer = $self->transaction
Branch: 1 branch
BranchTrueFalseTotal
$self->redeem_script ? : 7 0 7
104 ->sign(
105 signing_index => $self->signing_index,
106 compat => $self->_compat,
107 ($self->redeem_script ? (script => $self->redeem_script) : ()),
108 );
109  
110 7 my $sig = $self->_get_old_signature;
111  
112 # process signatures in reverse order, since add_signature works that way
113 7 foreach my $sig_num (reverse 1 .. $total_signatures) {
114 13 ▶ if ($sig_num == $this_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_num == $this_signature) { } 7 6 13
115  
116 # set this signature
117 7 $signer->add_signature($self->key, sighash => $self->sighash);
118 }
119 else {
120  
121 # Do not touch other signatures if they exist at all
122 6 ▶ $signer->add_signature($sig->[$sig_num] // '');
Condition100%2/2
$sig->[$sig_num] // ""
l!l
33
MC/DC100%1/1
$sig->[$sig_num] // ""
$sig->[$sig_num]
Truth table100%2/2
A
A= $sig->[$sig_num]
Aresult
11
00
123 }
124 }
125  
126 $signer
127 ->finalize_multisignature
128 7 ->finalize;
129 }
130  
131 sub _sign_P2WPKH
132 3 ▶{
Subroutine _sign_P2WPKH: called CC=1 SCAR=0.0
133 3 my ($self) = @_;
134  
135 3 return $self->_sign_P2PK;
136 }
137  
138 sub _sign_P2TR
139 8 ▶{
Subroutine _sign_P2TR: called CC=2 SCAR=6.9
140 8 my ($self) = @_;
141 8 my $script_tree = $self->script_tree;
142  
143 # signs key path only
144 8 ▶ $self->transaction
Branch: 1 branch
BranchTrueFalseTotal
$script_tree ? : 6 2 8
145 ->sign(
146 signing_index => $self->signing_index,
147 ($script_tree ? (script_tree => $script_tree) : ()),
148 )
149 ->add_signature($self->key, sighash => $self->sighash)
150 ->finalize;
151 }
152  
153 sub _sign_script
154 30 ▶{
Subroutine _sign_script: called CC=11 SCAR=25.4
155 30 my ($self, $script, $nested) = @_;
156 30 my $type = $script->type;
157  
158 30 my $method = $self->can("_sign_$type");
159 30 ▶ return $self->$method if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 22 8 30
160  
161 8 ▶ Bitcoin::Crypto::Exception::ScriptType->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $nested 0 8 8
162 "don't know how to sign nested standard script type $type"
163 ) if $nested;
164  
165 8 ▶ if ($type eq 'P2SH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "P2SH") 5 3 8
166 5 ▶ if (!$self->has_redeem_script) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->has_redeem_script) { } 1 4 5
elsif (not $self->_compat) { } 4 0 4
167  
168 # P2SH but no redeem script - must be compat P2WPKH
169 1 $self->_set_compat(!!1);
170 1 return $self->_sign_P2WPKH;
171 }
172 elsif (!$self->_compat) {
173  
174 # this may be compat P2WSH - let further code handle it
175 4 my $witness_el = $self->redeem_script->witness_program->to_serialized;
176 4 ▶ $self->_set_compat(!!1) if $script->run([$witness_el])->success;
Branch: 1 branch
BranchTrueFalseTotal
if $script->run([$witness_el])->success 2 2 4
177 }
178 }
179  
180 7 ▶ my $redeem_type = $self->has_redeem_script && $self->redeem_script->type;
Condition33%1/3
$self->has_redeem_script && $self->redeem_script->type
!ll&&!rl&&r
007
MC/DC0%0/2
$self->has_redeem_script && $self->redeem_script->type
$self->has_redeem_script $self->redeem_script->type
Truth table33%1/3
A && B
A= $self->has_redeem_scriptB= $self->redeem_script->type
ABresult
0X0
100
111
181 7 ▶ return $self->_sign_script($self->redeem_script, -nested)
Branch: 1 branch
BranchTrueFalseTotal
if $redeem_type and $type eq "P2SH" || $type eq "P2WSH" 7 0 7
Condition50%3/6
$redeem_type and $type eq "P2SH" || $type eq "P2WSH"
!ll&&!rl&&r
007
$type eq "P2SH" or $type eq "P2WSH"
l!l&&r!l&&!r
430
MC/DC0%0/3
$redeem_type and $type eq "P2SH" || $type eq "P2WSH"
$redeem_type $type eq "P2SH" $type eq "P2WSH"
Truth table50%2/4
A and B or C
A= $redeem_typeB= $type eq "P2SH"C= $type eq "P2WSH"
ABCresult
0XX0
1000
11X1
1011
182 if $redeem_type && ($type eq 'P2SH' || $type eq 'P2WSH');
183  
184 0 Bitcoin::Crypto::Exception::ScriptType->raise(
185 "don't know how to sign standard script type $type"
186 );
187 }
188  
189 sub sign
190 23 ▶{
Subroutine sign: called CC=1 SCAR=0.1
Pod undocumented
191 23 my ($self) = @_;
192  
193 Bitcoin::Crypto::Exception::Sign->trap_into(
194 23 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
195 23 ▶ die_no_trace 'no such input' if !$self->input;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->input 0 23 23
196  
197 23 my $utxo = $self->input->utxo->output;
198  
199 23 ▶ die_no_trace 'cannot automatically sign a non-standard locking script'
Branch: 1 branch
BranchTrueFalseTotal
unless $utxo->is_standard 0 23 23
200 if !$utxo->is_standard;
201  
202 23 $self->_sign_script($utxo->locking_script);
203 },
204 23 "Can't sign transaction input " . $self->signing_index
205 );
206 }
207  
208 40 1;
209  
« Crypto/PSBT.pm ↑ Summary Crypto/Block.pm »