| 1 | package Bitcoin::Crypto::Role::Key; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 35 | $Bitcoin::Crypto::Role::Key::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 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Mooish::Base -standard, -role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use Types::Common -sigs; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use Bitcoin::Crypto::Constants qw(:key); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use Bitcoin::Crypto::Util::Internal qw(get_key_type); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use Bitcoin::Crypto::Helpers qw(ensure_length ecc); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 40 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 40 | ▶ | use Bitcoin::Crypto::Secret; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # ByteStr or BitcoinSecret or SecretBuffer | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 35 | has param '_key_instance' => ( | |||||||||||||||||||||||||||||||||||||||||
| 19 | writer => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 35 | has param 'purpose' => ( | |||||||||||||||||||||||||||||||||||||||||
| 23 | isa => BIP44Purpose, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | writer => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | clearer => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 35 | with qw(Bitcoin::Crypto::Role::Network); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 35 | requires qw( | |||||||||||||||||||||||||||||||||||||||||
| 32 | _is_private | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | sub _run_with_key | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 2436 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_with_key: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 2436 | my ($self, $sub_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 2436 | ▶ | if ($self->_is_private) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1614 | return $self->_key_instance->unmask_to($sub_ref); | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 822 | return $sub_ref->($self->_key_instance); | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | sub _validate_key | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 1328 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_key: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1328 | state $sig = signature(method => !!1, positional => [ByteStr]); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1328 | my ($self, $entropy) = $sig->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1328 | my $is_private = get_key_type $entropy; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1328 | ▶ | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 'invalid entropy data passed to key creation method' | ||||||||||||||||||||||||||||||||||||||||||
| 55 | ) unless defined $is_private; | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1327 | ▶ | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 'trying to create key from unknown key data' | ||||||||||||||||||||||||||||||||||||||||||
| 59 | ) unless $is_private == $self->_is_private; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1321 | ▶ | if ($is_private) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 504 | ▶ | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 'private key is not valid' | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ) unless ecc->verify_private_key(ensure_length $entropy, KEY_MAX_LENGTH); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 817 | try { | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | # keep public keys in compressed form always | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 817 | $self->_set_key_instance(ecc->compress_public_key($entropy)); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | catch ($e) { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | Bitcoin::Crypto::Exception::KeyCreate->raise( | |||||||||||||||||||||||||||||||||||||||||
| 74 | 'public key is not valid' | ||||||||||||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | sub BUILD | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 1328 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1328 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1328 | state $type_secret = BitcoinSecret; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1328 | ▶ | $self->_set_key_instance($type_secret->assert_coerce($self->_key_instance)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | if $self->_is_private; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | $self->_run_with_key( | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1328 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 1328 | $self->_validate_key($_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 1328 | ); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 35 | signature_for has_purpose => ( | |||||||||||||||||||||||||||||||||||||||||
| 96 | method => !!1, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | positional => [BIP44Purpose], | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | sub has_purpose | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 191 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_purpose: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 191 | my ($self, $purpose) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 191 | ▶ | return !$self->purpose || $self->purpose == $purpose; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
!$self->purpose || $self->purpose == $purpose
MC/DC100%2/2
!$self->purpose || $self->purpose == $purpose
!$self->purpose
$self->purpose == $purpose
Truth table100%3/3
not(A) || B
A= !$self->purposeB= $self->purpose == $purpose
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 35 | signature_for raw_key => ( | |||||||||||||||||||||||||||||||||||||||||
| 108 | method => !!1, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | positional => [Maybe [Enum [qw(private public public_compressed public_xonly)]], {default => undef}], | ||||||||||||||||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # helpers for raw_key | ||||||||||||||||||||||||||||||||||||||||||
| 113 | sub __full_private | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 1108 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__full_private: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1108 | state $sig = signature(positional => [ByteStr]); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 1108 | my ($key) = $sig->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 1108 | return ensure_length $key, KEY_MAX_LENGTH; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | sub __private_to_public | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 632 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__private_to_public: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 632 | my ($key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 632 | return ecc->create_public_key(__full_private($key)); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | sub raw_key | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 1818 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
raw_key: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 1823 | my ($self, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1823 | my $is_private = $self->_is_private; | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 1823 | ▶ | $type //= $is_private ? 'private' : 'public'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$type //= $is_private ? "private" : "public"
MC/DC100%1/1
$type //= $is_private ? "private" : "public"
$type
Truth table100%2/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 1823 | ▶ | if ($type eq 'public' && (!$self->can('compressed') || $self->compressed)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$type eq "public" and !$self->can("compressed") || $self->compressed
not $self->can("compressed") or $self->compressed
MC/DC100%3/3
$type eq "public" and !$self->can("compressed") || $self->compressed
$type eq "public"
not $self->can("compressed")
$self->compressed
Truth table100%4/4
A and not(B) or C
A= $type eq "public"B= not $self->can("compressed")C= $self->compressed
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 639 | $type = 'public_compressed'; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 1823 | ▶ | if ($type eq 'private') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 481 | ▶ | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 'cannot create private key from a public key' | ||||||||||||||||||||||||||||||||||||||||||
| 139 | ) unless $is_private; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # CAUTION: exposing the secret! | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 481 | return $self->_run_with_key(\&__full_private); | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 1342 | ▶ | my $key = $is_private ? $self->_run_with_key(\&__private_to_public) : $self->_key_instance; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1342 | ▶ | if ($type eq 'public_xonly') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 196 | return ecc->xonly_public_key($key); | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 1146 | ▶ | return $key if $type eq 'public_compressed'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 40 | return ecc->compress_public_key($key, !!0); | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 35 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||