File Coverage

blib/lib/Bitcoin/Crypto/Role/BasicKey.pm
Criterion Covered Total %
statement 40 44 90.9
branch 3 8 37.5
condition 4 7 57.1
subroutine 14 14 100.0
pod 0 6 0.0
total 61 79 77.2


line stmt bran cond sub pod time code
1             package Bitcoin::Crypto::Role::BasicKey;
2             $Bitcoin::Crypto::Role::BasicKey::VERSION = '1.008';
3 10     10   5558 use v5.10;
  10         86  
4 10     10   117 use strict;
  10         34  
  10         216  
5 10     10   62 use warnings;
  10         29  
  10         312  
6              
7 10     10   58 use Bitcoin::Crypto::Helpers qw(pad_hex verify_bytestring);
  10         18  
  10         503  
8 10     10   66 use Bitcoin::Crypto::Exception;
  10         22  
  10         286  
9 10     10   55 use Moo::Role;
  10         26  
  10         81  
10              
11             with "Bitcoin::Crypto::Role::Key",
12             "Bitcoin::Crypto::Role::Compressed";
13              
14             sub sign_message
15             {
16 4     4 0 1927 my ($self, $message, $algorithm) = @_;
17              
18 4 50       17 Bitcoin::Crypto::Exception::Sign->raise(
19             "cannot sign a message with a public key"
20             ) unless $self->_is_private;
21              
22 4   50     23 $algorithm //= "sha256";
23 4 50 33     9 if (eval { require Crypt::Perl } && Crypt::Perl->VERSION gt '0.33') {
  4         663  
24 0         0 require Crypt::Perl::ECDSA::Parse;
25             $self->{_crypt_perl_prv} = Crypt::Perl::ECDSA::Parse::private($self->key_instance->export_key_der('private'))
26 0 0       0 if !exists $self->{_crypt_perl_prv};
27             }
28             else {
29 4         59 warn(
30             'Current implementation of CryptX signature generation does not produce deterministic results. For better security, install the Crypt::Perl module.'
31             );
32             }
33              
34             return Bitcoin::Crypto::Exception::Sign->trap_into(
35             sub {
36 4 50   4   16 if (exists $self->{_crypt_perl_prv}) {
37 0         0 my $sub = "sign_${algorithm}";
38 0         0 return $self->{_crypt_perl_prv}->$sub($message);
39             }
40             else {
41 4         12187 return $self->key_instance->sign_message($message, $algorithm);
42             }
43             }
44 4         59 );
45             }
46              
47             sub verify_message
48             {
49 16     16 0 163 my ($self, $message, $signature, $algorithm) = @_;
50 16         60 verify_bytestring($signature);
51              
52 16   100     78 $algorithm //= "sha256";
53             return Bitcoin::Crypto::Exception::Verify->trap_into(
54             sub {
55 16     16   28129 $self->key_instance->verify_message($signature, $message, $algorithm);
56             }
57 16         126 );
58             }
59              
60             sub from_hex
61             {
62 25     25 0 7884 my ($class, $val) = @_;
63 25         96 return $class->from_bytes(pack "H*", pad_hex($val));
64             }
65              
66             sub to_hex
67             {
68 12     12 0 275 my ($self) = @_;
69 12         34 return unpack "H*", $self->to_bytes();
70             }
71              
72             sub from_bytes
73             {
74 36     36 0 86 my ($class, $bytes) = @_;
75 36         114 verify_bytestring($bytes);
76              
77 34         756 return $class->new($bytes);
78             }
79              
80             sub to_bytes
81             {
82 93     93 0 275 my ($self) = @_;
83 93         354 return $self->raw_key;
84             }
85              
86             1;
87