Crypto/Transaction/Signer.pm

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