Crypto/Transaction/Signer.pm

filter: Statementstmt 95.7% 113 / 118 Branchbran 70.8% 17 / 24 Conditioncond 80.0% 8 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 88.8% 24 / 27 Podpod 50.0% 2 / 4 totaltotal 88.8% 168 / 189 CC 17 SCAR 12.1
CC
17
Cov
91%
new_impl
9.1
__ANON__
4.9
add_signature
3.0
CRAP 3.4
« Crypto/Key/Private.pm ↑ Summary Crypto/PSBT/Field.pm »
1 package Bitcoin::Crypto::Transaction::Signer;
2 38 $Bitcoin::Crypto::Transaction::Signer::VERSION = '4.006';
3 38 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 38 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use Types::Common qw(signature_for); # frees up 'signature' for our field
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 38 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 38 ▶use Bitcoin::Crypto::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 38 ▶use Bitcoin::Crypto::Script::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 38 ▶use Bitcoin::Crypto::Util::Internal qw(to_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 38 ▶use Bitcoin::Crypto::Helpers qw(die_no_trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 38 has param 'flags' => (
18 coerce => TransactionFlags,
19 default => undef,
20 );
21  
22 38 has param 'transaction' => (
23 isa => InstanceOf ['Bitcoin::Crypto::Transaction'],
24 );
25  
26 38 has param 'signing_index' => (
27 isa => PositiveOrZeroInt,
28 );
29  
30 38 has param 'script' => (
31 coerce => BitcoinScript,
32 );
33  
34 has field 'signature' => (
35 isa => ArrayRef,
36 73 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 38 );
38  
39 38 has field '_runner' => (
40 isa => InstanceOf ['Bitcoin::Crypto::Script::Runner'],
41 lazy => 1,
42 );
43  
44 sub _build_runner
45 73 ▶{
Subroutine _build_runner: called CC=1 SCAR=0.0
46 73 my ($self) = @_;
47  
48 # clone a transaction and input. Some subclasses may want to modify it
49 73 my $ind = $self->signing_index;
50 73 my $temp_tx = $self->transaction->clone;
51 73 $temp_tx->inputs->[$ind] = $temp_tx->inputs->[$ind]->clone;
52  
53 73 my $runner = Bitcoin::Crypto::Script::Runner->new(
54 transaction => $temp_tx,
55 flags => $self->flags,
56 );
57  
58 73 $runner->transaction->set_input_index($ind);
59 73 $runner->start($self->script);
60 73 return $runner;
61 }
62  
63 sub _find_next_sigop
64 85 ▶{
Subroutine _find_next_sigop: called CC=2 SCAR=7.4
65 85 my ($self, $error) = @_;
66 85 ▶ $error //= !!1;
Condition50%1/2
$error //=
l!l
085
MC/DC0%0/1
$error //=
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
67  
68 85 my $runner = $self->_runner;
69 85 my $ops = $runner->operations;
70 Bitcoin::Crypto::Exception::Sign->trap_into(
71 85 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.9
72 85 while ('finding sigop') {
73 275 my $pos = $runner->pos;
74  
75 275 ▶ if ($pos > $#$ops) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > $ops->$#*) 0 275 275
76 0 ▶ die_no_trace 'could not find a sigop' if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
77 0 last;
78 }
79  
80 275 ▶ last if $ops->[$pos][0]->sigop;
Branch: 1 branch
BranchTrueFalseTotal
if $ops->[$pos][0]->sigop 84 191 275
81 191 $runner->step;
82 }
83 },
84 85 'finding next sigop failed'
85 );
86  
87 84 return $runner;
88 }
89  
90 sub _step_over_sigop
91 67 ▶{
Subroutine _step_over_sigop: called CC=1 SCAR=0.0
92 67 my ($self) = @_;
93 Bitcoin::Crypto::Exception::Sign->trap_into(
94 67 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 67 $self->_runner->step;
96 },
97 67 'stepping over sigop failed'
98 );
99 }
100  
101 sub _multisigop
102 105 ▶{
Subroutine _multisigop: called CC=2 SCAR=7.0
103 105 my ($self) = @_;
104  
105 105 my $runner = $self->_runner;
106 105 my $op = $runner->operations->[$runner->pos];
107  
108 105 ▶ return $op && $op->[0]->name =~ /OP_CHECKMULTISIG/;
Condition66%2/3
$op && $op->[0]->name =~ /OP_CHECKMULTISIG/u
!ll&&!rl&&r
05055
MC/DC50%1/2
$op && $op->[0]->name =~ /OP_CHECKMULTISIG/u
$op $op->[0]->name =~ /OP_CHECKMULTISIG/u
Truth table66%2/3
A && B
A= $opB= $op->[0]->name =~ /OP_CHECKMULTISIG/u
ABresult
0X0
100
111
109 }
110  
111 sub _get_signature
112 0 ▶{
Subroutine _get_signature: not called CC=1 SCAR=6.9
113 0 die 'unimplemented';
114 }
115  
116 sub _initialize
117 0 ▶{
Subroutine _initialize: not called CC=1 SCAR=6.9
118 0 die 'unimplemented';
119 }
120  
121 sub _finalize
122 0 ▶{
Subroutine _finalize: not called CC=1 SCAR=6.9
123 0 die 'unimplemented';
124 }
125  
126 sub BUILD
127 73 ▶{
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
128 73 my ($self) = @_;
129  
130 73 $self->_initialize;
131 }
132  
133 sub new_impl
134 73 ▶{
Subroutine new_impl: called CC=9 SCAR=22.0
Pod undocumented
135 73 my ($class, $transaction, $args) = @_;
136  
137 73 $args->{transaction} = $transaction;
138 Bitcoin::Crypto::Exception::Sign->raise(
139 'signing_index is required'
140 73 ▶ ) unless defined $args->{signing_index};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"signing_index"} 0 73 73
141  
142 73 my $input = $transaction->inputs->[$args->{signing_index}];
143  
144 73 ▶ Bitcoin::Crypto::Exception::Sign->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 73 73
145 'no such input'
146 ) unless defined $input;
147  
148 73 ▶ my $type = $input->utxo->output->locking_script->type // '';
Condition100%2/2
$input->utxo->output->locking_script->type // ""
l!l
712
MC/DC100%1/1
$input->utxo->output->locking_script->type // ""
$input->utxo->output->locking_script->type
Truth table100%2/2
A
A= $input->utxo->output->locking_script->type
Aresult
11
00
149  
150 73 state $known_types = {map { $_ => 1 } qw(P2PKH P2SH P2WPKH P2WSH P2TR)};
151  
152 73 my $impl_class;
153 73 ▶ if ($type eq 'P2SH' && $args->{compat}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'P2SH' and $args->{'compat'}) { } 10 63 73
elsif ($known_types->{$type}) { } 57 6 63
Condition100%3/3
$type eq "P2SH" and $args->{"compat"}
!ll&&!rl&&r
57610
MC/DC100%2/2
$type eq "P2SH" and $args->{"compat"}
$type eq "P2SH" $args->{"compat"}
Truth table100%3/3
A and B
A= $type eq "P2SH"B= $args->{"compat"}
ABresult
0X0
100
111
154 10 ▶ $impl_class = $args->{script} ? 'CompatP2WSH' : 'CompatP2WPKH';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'script'} ? : 7 3 10
155 }
156 elsif ($known_types->{$type}) {
157 57 $impl_class = $type;
158 }
159 else {
160 6 $impl_class = 'CustomLegacy';
161 }
162  
163 73 $impl_class = "Bitcoin::Crypto::Transaction::Signer::$impl_class";
164  
165 73 ▶ eval "require $impl_class; 1" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $impl_class; 1" or die $@ 73 0 73
166 73 return $impl_class->new($args);
167 }
168  
169 38 signature_for add_bytes => (
170 method => !!1,
171 positional => [ByteStr],
172 );
173  
174 sub add_bytes
175 126 ▶{
Subroutine add_bytes: called CC=1 SCAR=0.0
176 126 my ($self, $bytes) = @_;
177  
178 126 unshift @{$self->_runner->stack}, $bytes;
179 119 push @{$self->signature}, $bytes;
180  
181 119 return $self;
182 }
183  
184 38 signature_for add_number => (
185 method => !!1,
186 positional => [Int | Str | InstanceOf ['Math::BigInt']],
187 );
188  
189 sub add_number
190 {
191 my ($self, $number) = @_;
192  
193 return $self->add_bytes(Bitcoin::Crypto::Script::Runner->from_int($number));
194 }
195  
196 38 signature_for add_signature => (
197 method => !!1,
198 head => [ByteStr | InstanceOf ['Bitcoin::Crypto::Key::Private']],
199 named => [
200 sighash => Maybe [PositiveOrZeroInt],
201 {default => undef},
202 ],
203 bless => !!0,
204 );
205  
206 sub add_signature
207 85 ▶{
Subroutine add_signature: called CC=3 SCAR=11.0
208 85 my ($self, $privkey_or_signature, $args) = @_;
209 85 my $signature;
210 85 my $runner = $self->_find_next_sigop;
211  
212 84 ▶ if (!ref $privkey_or_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $privkey_or_signature) { } 22 62 84
213 22 $signature = $privkey_or_signature;
214 }
215 else {
216 62 $signature = $self->_get_signature($privkey_or_signature, $args);
217 }
218  
219 83 $self->add_bytes($signature);
220 83 ▶ $self->_step_over_sigop
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_multisigop 56 27 83
221 unless $self->_multisigop;
222  
223 81 return $self;
224 }
225  
226 sub finalize_multisignature
227 11 ▶{
Subroutine finalize_multisignature: called CC=2 SCAR=7.0
Pod documented
228 11 my ($self) = @_;
229  
230 11 ▶ Bitcoin::Crypto::Exception::Sign->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_multisigop 0 11 11
231 'not on multisignature opcode'
232 ) unless $self->_multisigop;
233  
234 # add mandatory null dummy element
235 11 $self->add_bytes('');
236 11 $self->_step_over_sigop;
237  
238 11 return $self;
239 }
240  
241 sub finalize
242 49 ▶{
Subroutine finalize: called CC=1 SCAR=0.0
Pod documented
243 49 my ($self) = @_;
244  
245 49 $self->_finalize;
246  
247 # no $self returned anymore - this object is done
248 49 return;
249 }
250  
251 38 signature_for dump => (
252 method => !!1,
253 positional => [Bool, {default => !!1}],
254 );
255  
256 sub dump
257 {
258 my ($self, $find_sigop) = @_;
259  
260 my $runner = $self->_runner;
261 $self->_find_next_sigop(!!0)
262 if $find_sigop;
263  
264 my $pos = $runner->pos;
265 my $ops = $runner->operations;
266  
267 my @result;
268 push @result, "at position $pos (marked by **)";
269  
270 my @ops_dump;
271 for my $this_pos ($pos - 2 .. $pos + 2) {
272 next unless $this_pos >= 0;
273  
274 my $op = $ops->[$this_pos];
275 push @ops_dump, '**'
276 if $this_pos == $pos;
277  
278 next unless $op;
279  
280 push @ops_dump, $op->[0]->name;
281 }
282 push @result, join ' ', @ops_dump;
283  
284 push @result, 'stack contents:';
285 foreach my $item (@{$runner->stack}) {
286 push @result, '> ' . to_format [hex => $item];
287 }
288  
289 return join "\n", @result;
290 }
291  
292 38 signature_for dump_abort => (
293 method => !!1,
294 positional => [Bool, {default => !!1}],
295 );
296  
297 sub dump_abort
298 {
299 my ($self, $find_sigop) = @_;
300  
301 say $self->dump($find_sigop);
302 croak 'signing aborted';
303 }
304  
305 38 1;
306  
307 __END__
« Crypto/Key/Private.pm ↑ Summary Crypto/PSBT/Field.pm »