File Coverage

blib/lib/Bitcoin/Crypto/Role/BasicKey.pm
Criterion Covered Total %
statement 43 47 91.4
branch 3 8 37.5
condition 4 7 57.1
subroutine 15 15 100.0
pod 0 6 0.0
total 65 83 78.3


line stmt bran cond sub pod time code
1             package Bitcoin::Crypto::Role::BasicKey;
2             $Bitcoin::Crypto::Role::BasicKey::VERSION = '1.008_01'; # TRIAL
3             $Bitcoin::Crypto::Role::BasicKey::VERSION = '1.00801';
4 10     10   5900 use v5.10;
  10         43  
5 10     10   72 use strict;
  10         26  
  10         225  
6 10     10   47 use warnings;
  10         20  
  10         312  
7 10     10   61 use Carp qw(carp);
  10         25  
  10         573  
8              
9 10     10   79 use Bitcoin::Crypto::Helpers qw(pad_hex verify_bytestring);
  10         55  
  10         576  
10 10     10   77 use Bitcoin::Crypto::Exception;
  10         37  
  10         295  
11 10     10   65 use Moo::Role;
  10         21  
  10         90  
12              
13             with qw(
14             Bitcoin::Crypto::Role::Key
15             Bitcoin::Crypto::Role::Compressed
16             );
17              
18             around BUILDARGS => sub {
19             my ($orig, $class, @params) = @_;
20              
21             if (@params == 1) {
22             carp "$class->new(\$bytes) is now deprecated. Use $class->from_bytes(\$bytes) instead";
23             unshift @params, 'key_instance';
24             }
25              
26             return $class->$orig(@params);
27             };
28              
29             sub sign_message
30             {
31 4     4 0 1878 my ($self, $message, $algorithm) = @_;
32              
33 4 50       15 Bitcoin::Crypto::Exception::Sign->raise(
34             'cannot sign a message with a public key'
35             ) unless $self->_is_private;
36              
37 4   50     24 $algorithm //= 'sha256';
38 4 50 33     13 if (eval { require Crypt::Perl } && Crypt::Perl->VERSION gt '0.33') {
  4         670  
39 0         0 require Crypt::Perl::ECDSA::Parse;
40             $self->{_crypt_perl_prv} = Crypt::Perl::ECDSA::Parse::private($self->key_instance->export_key_der('private'))
41 0 0       0 if !exists $self->{_crypt_perl_prv};
42             }
43             else {
44 4         60 warn(
45             'Current implementation of CryptX signature generation does not produce deterministic results. For better security, install the Crypt::Perl module.'
46             );
47             }
48              
49             return Bitcoin::Crypto::Exception::Sign->trap_into(
50             sub {
51 4 50   4   16 if (exists $self->{_crypt_perl_prv}) {
52 0         0 my $sub = "sign_${algorithm}";
53 0         0 return $self->{_crypt_perl_prv}->$sub($message);
54             }
55             else {
56 4         12118 return $self->key_instance->sign_message($message, $algorithm);
57             }
58             }
59 4         56 );
60             }
61              
62             sub verify_message
63             {
64 16     16 0 181 my ($self, $message, $signature, $algorithm) = @_;
65 16         95 verify_bytestring($signature);
66              
67 16   100     79 $algorithm //= 'sha256';
68             return Bitcoin::Crypto::Exception::Verify->trap_into(
69             sub {
70 16     16   28404 $self->key_instance->verify_message($signature, $message, $algorithm);
71             }
72 16         135 );
73             }
74              
75             sub from_hex
76             {
77 26     26 0 8444 my ($class, $val) = @_;
78 26         127 return $class->from_bytes(pack 'H*', pad_hex($val));
79             }
80              
81             sub to_hex
82             {
83 12     12 0 283 my ($self) = @_;
84 12         38 return unpack 'H*', $self->to_bytes();
85             }
86              
87             sub from_bytes
88             {
89 37     37 0 94 my ($class, $bytes) = @_;
90 37         126 verify_bytestring($bytes);
91              
92 35         811 return $class->new(key_instance => $bytes);
93             }
94              
95             sub to_bytes
96             {
97 93     93 0 190 my ($self) = @_;
98 93         245 return $self->raw_key;
99             }
100              
101             1;
102