Crypto/Role/SignVerify.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 80.0% 8 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 1 totaltotal 90.3% 84 / 93 CC 8 SCAR 7.0
CC
8
Cov
93%
__ANON__
4.0
verify_message
3.1
sign_message
3.0
CRAP 2.0
« Crypto/Transaction/Signer/P2TR.pm ↑ Summary Crypto/Transaction/Digest/Config.pm »
1 package Bitcoin::Crypto::Role::SignVerify;
2 166 $Bitcoin::Crypto::Role::SignVerify::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 40 ▶use Mooish::Base -standard, -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Types::Common -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Bitcoin::Crypto::Helpers qw(ecc make_strict_der_signature);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Bitcoin::Crypto::Transaction::AutoSigner;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Bitcoin::Crypto::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Bitcoin::Crypto::Transaction::Flags;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 166 requires qw(
16 raw_key
17 taproot_output
18 _is_private
19 );
20  
21 my %algorithms = (
22 default => {
23 34 ▶ sign => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 34 my ($key, $digest) = @_;
25  
26 34 return ecc->sign_digest($key->raw_key, $digest);
27 },
28 142 ▶ verify => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
29 142 my ($key, $signature, $digest, $flags) = @_;
30  
31 142 ▶ if (!$flags->strict_signatures) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags->strict_signatures) 3 139 142
32 3 $signature = make_strict_der_signature($signature);
33 }
34  
35 142 ▶ if ($flags->low_s_signatures) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->low_s_signatures) 2 140 142
36 2 my $normalized = ecc->normalize_signature($signature);
37 2 ▶ return !!0 if $normalized ne $signature;
Branch: 1 branch
BranchTrueFalseTotal
if $normalized ne $signature 2 0 2
38 }
39  
40 140 return ecc->verify_digest($key->raw_key('public'), $signature, $digest);
41 },
42 },
43 schnorr => {
44 14 ▶ sign => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 14 my ($key, $digest) = @_;
46  
47 14 return ecc->sign_digest_schnorr($key->raw_key, $digest);
48 },
49 0 ▶ verify => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my ($key, $signature, $digest) = @_;
51  
52 0 return ecc->verify_digest_schnorr($key->raw_key('public_xonly'), $signature, $digest);
53 },
54 },
55 166 );
56  
57 166 signature_for sign_message => (
58 method => !!1,
59 positional => [BitcoinDigest],
60 );
61  
62 sub sign_message
63 190 ▶{
Subroutine sign_message: called CC=3 SCAR=11.0
64 64 my ($self, $digest_result) = @_;
65 64 ▶ my $algorithm = $self->taproot_output ? 'schnorr' : 'default';
Branch: 1 branch
BranchTrueFalseTotal
$self->taproot_output ? : 14 176 190
66  
67 64 ▶ Bitcoin::Crypto::Exception::Sign->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_private 0 48 48
68 'cannot sign a message with a public key'
69 ) unless $self->_is_private;
70  
71 return Bitcoin::Crypto::Exception::Sign->trap_into(
72 48 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 64 return $algorithms{$algorithm}{sign}->($self, $digest_result->hash);
74 }
75 64 );
76 }
77  
78 sub sign_transaction
79 23 ▶{
Subroutine sign_transaction: called CC=1 SCAR=0.0
Pod undocumented
80 23 my ($self, $transaction, %args) = @_;
81  
82 23 $args{transaction} = $transaction;
83 23 $args{key} = $self;
84 23 my $signer = Bitcoin::Crypto::Transaction::AutoSigner->new(%args);
85 23 $signer->sign;
86  
87 23 return;
88 }
89  
90 166 signature_for verify_message => (
91 method => !!1,
92 head => [BitcoinDigest, ByteStr],
93 named => [
94 flags => Maybe [InstanceOf ['Bitcoin::Crypto::Transaction::Flags']],
95 {default => undef},
96 ],
97 bless => !!0,
98 );
99  
100 sub verify_message
101 {
102 16 my ($self, $digest_result, $signature, $args) = @_;
103 my $algorithm = $self->taproot_output ? 'schnorr' : 'default';
104 ▶ my $flags = $args->{flags} // Bitcoin::Crypto::Transaction::Flags->new;
Condition66%2/3
$args->{"flags"} // "Bitcoin::Crypto::Transaction::Flags"->new
l!l&&r!l&&!r
127150
MC/DC0%0/2
$args->{"flags"} // "Bitcoin::Crypto::Transaction::Flags"->new
$args->{"flags"} "Bitcoin::Crypto::Transaction::Flags"->new
Truth table66%2/3
A // B
A= $args->{"flags"}B= "Bitcoin::Crypto::Transaction::Flags"->new
ABresult
1X1
011
000
105  
106 try {
107 return $algorithms{$algorithm}{verify}->(
108 $self,
109 $signature,
110 $digest_result->hash,
111 $flags,
112 );
113 }
114 catch ($e) {
115 return !!0;
116 }
117 }
118  
119 35 1;
120  
« Crypto/Transaction/Signer/P2TR.pm ↑ Summary Crypto/Transaction/Digest/Config.pm »