| 1 | package Bitcoin::Crypto::Key::Private; | ||||||||||||||||||||||||||||
| 2 | 27 | $Bitcoin::Crypto::Key::Private::VERSION = '4.005'; | |||||||||||||||||||||||||||
| 3 | 27 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 27 | ▶ | use Mooish::Base -standard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 27 | ▶ | use Bitcoin::BIP39 qw(bip39_mnemonic_to_entropy entropy_to_bip39_mnemonic); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 27 | ▶ | use Types::Common -sigs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 27 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 27 | ▶ | use Bitcoin::Crypto::Key::Public; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 27 | ▶ | use Bitcoin::Crypto::Base58 qw(encode_base58check decode_base58check); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 27 | ▶ | use Bitcoin::Crypto::Constants qw(:key); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 27 | ▶ | use Bitcoin::Crypto::Network; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 27 | ▶ | use Bitcoin::Crypto::Util::Internal qw(validate_wif); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 27 | ▶ | use Bitcoin::Crypto::Helpers qw(ensure_length); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 27 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 27 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 27 | extends qw(Bitcoin::Crypto::Key::Base); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 958 | ▶ | sub _is_private { 1 } | ||||||||||||||||||||||||||
|
Subroutine
_is_private: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub to_wif | ||||||||||||||||||||||||||||
| 25 | 21 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
to_wif: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||
| 27 | 21 | my $bytes = $self->to_serialized(); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # wif network - 1B | ||||||||||||||||||||||||||||
| 30 | 21 | my $wifdata = $self->network->wif_byte; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # key entropy - 32B | ||||||||||||||||||||||||||||
| 33 | 21 | $wifdata .= ensure_length $bytes, KEY_MAX_LENGTH; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # additional byte for compressed key - 1B | ||||||||||||||||||||||||||||
| 36 | 21 | ▶ | $wifdata .= WIF_COMPRESSED_BYTE if $self->compressed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 21 | return encode_base58check($wifdata); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 27 | signature_for from_serialized => ( | |||||||||||||||||||||||||||
| 42 | method => !!1, | ||||||||||||||||||||||||||||
| 43 | positional => [BitcoinSecret], | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 27 | signature_for from_wif => ( | |||||||||||||||||||||||||||
| 47 | method => !!1, | ||||||||||||||||||||||||||||
| 48 | positional => [BitcoinSecret, Maybe [Str], {default => undef}], | ||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub from_wif | ||||||||||||||||||||||||||||
| 52 | 17 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
from_wif: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 17 | my ($class, $secret, $network) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | return $secret->unmask_to( | ||||||||||||||||||||||||||||
| 56 | 17 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 57 | 17 | my ($wif) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 17 | ▶ | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 'base58 string is not valid WIF' | ||||||||||||||||||||||||||||
| 61 | ) unless validate_wif($wif); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 17 | my $decoded = decode_base58check($wif); | |||||||||||||||||||||||||||
| 64 | 17 | my $private = substr $decoded, 1; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 17 | my $compressed = 0; | |||||||||||||||||||||||||||
| 67 | 17 | ▶ | if (length($private) > KEY_MAX_LENGTH) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 8 | chop $private; | |||||||||||||||||||||||||||
| 69 | 8 | $compressed = 1; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 17 | my $wif_network_byte = substr $decoded, 0, 1; | |||||||||||||||||||||||||||
| 73 | my @found_networks = | ||||||||||||||||||||||||||||
| 74 | 17 | ▶ | Bitcoin::Crypto::Network->find(sub { shift->wif_byte eq $wif_network_byte }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 17 | ▶ | @found_networks = grep { $_ eq $network } @found_networks | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | if defined $network; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 17 | ▶ | if (@found_networks > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 7 | my $default_network = Bitcoin::Crypto::Network->get->id; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||||
| 82 | 'found multiple networks possible for given WIF: ' . join ', ', @found_networks | ||||||||||||||||||||||||||||
| 83 | 7 | ▶ | ) if none { $_ eq $default_network } @found_networks; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | @found_networks = ($default_network); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Bitcoin::Crypto::Exception::KeyCreate->raise( | ||||||||||||||||||||||||||||
| 89 | 16 | ▶ | "network name $network cannot be used for given WIF" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@found_networks == 0 and defined $network
MC/DC100%2/2
@found_networks == 0 and defined $network
@found_networks == 0
defined $network
Truth table100%3/3
A and B
A= @found_networks == 0B= defined $network
| |||||||||||||||||||||||||||||
| 90 | ) if @found_networks == 0 && defined $network; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 15 | ▶ | Bitcoin::Crypto::Exception::NetworkConfig->raise( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | "couldn't find network for WIF byte $wif_network_byte" | ||||||||||||||||||||||||||||
| 94 | ) if @found_networks == 0; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 14 | my $instance = $class->from_serialized($private); | |||||||||||||||||||||||||||
| 97 | 14 | $instance->set_compressed($compressed); | |||||||||||||||||||||||||||
| 98 | 14 | $instance->set_network(@found_networks); | |||||||||||||||||||||||||||
| 99 | 14 | return $instance; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 17 | ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub get_public_key | ||||||||||||||||||||||||||||
| 105 | 146 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_public_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 146 | my ($self) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 146 | my $public = Bitcoin::Crypto::Key::Public->new( | |||||||||||||||||||||||||||
| 109 | _key_instance => $self->raw_key('public'), | ||||||||||||||||||||||||||||
| 110 | compressed => $self->compressed, | ||||||||||||||||||||||||||||
| 111 | network => $self->network, | ||||||||||||||||||||||||||||
| 112 | purpose => $self->purpose, | ||||||||||||||||||||||||||||
| 113 | taproot_output => $self->taproot_output, | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 146 | return $public; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 27 | 1; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||