| 1 | package Bitcoin::Crypto::Key::Public; | ||||||||||
| 2 | 40 | $Bitcoin::Crypto::Key::Public::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; | ||||||||
|
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 Carp qw(carp); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 40 | ▶ | use Bitcoin::Crypto::Script; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 40 | ▶ | use Bitcoin::Crypto::Base58 qw(encode_base58check); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 40 | ▶ | use Bitcoin::Crypto::Bech32 qw(encode_segwit); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 40 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 40 | ▶ | use Bitcoin::Crypto::Constants qw(:bip44 :witness); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 40 | ▶ | use Bitcoin::Crypto::Util::Internal qw(hash160 get_public_key_compressed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 40 | ▶ | use Bitcoin::Crypto::Helpers qw(ecc); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 40 | extends qw(Bitcoin::Crypto::Key::Base); | |||||||||
| 19 | |||||||||||
| 20 | 2772 | ▶ | sub _is_private { 0 } | ||||||||
|
Subroutine
_is_private: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | sub get_hash | ||||||||||
| 23 | 102 | ▶ | { | ||||||||
|
Subroutine
get_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | 102 | my ($self) = @_; | |||||||||
| 25 | |||||||||||
| 26 | 102 | return hash160($self->to_serialized); | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | sub get_xonly_key | ||||||||||
| 30 | 130 | ▶ | { | ||||||||
|
Subroutine
get_xonly_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | 130 | my ($self) = @_; | |||||||||
| 32 | |||||||||||
| 33 | 130 | return $self->raw_key('public_xonly'); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 40 | signature_for from_serialized => ( | |||||||||
| 37 | method => !!1, | ||||||||||
| 38 | positional => [ByteStr], | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | sub from_serialized | ||||||||||
| 42 | 493 | ▶ | { | ||||||||
|
Subroutine
from_serialized: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 493 | my ($class, $key) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 493 | my $self = $class->SUPER::from_serialized($key); | |||||||||
| 46 | 488 | $self->set_compressed(get_public_key_compressed($key)); | |||||||||
| 47 | |||||||||||
| 48 | 485 | return $self; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | signature_for witness_program => ( | ||||||||||
| 52 | method => !!1, | ||||||||||
| 53 | 56 | ▶ | positional => [PositiveOrZeroInt, {default => 0}, HashRef, {default => sub { {} }}], | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | sub witness_program | ||||||||||
| 57 | 84 | ▶ | { | ||||||||
|
Subroutine
witness_program: called CC=2 SCAR=6.9
| |||||||||||
| 58 | state $data_sources = { | ||||||||||
| 59 | 56 | ▶ | (SEGWIT_WITNESS_VERSION) => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 56 | return shift->get_hash; | |||||||||
| 61 | }, | ||||||||||
| 62 | 28 | ▶ | (TAPROOT_WITNESS_VERSION) => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 28 | my ($self, $params) = @_; | |||||||||
| 64 | |||||||||||
| 65 | 28 | $self = $self->get_taproot_output_key($params->{tweak_suffix}); | |||||||||
| 66 | 28 | return $self->get_xonly_key; | |||||||||
| 67 | }, | ||||||||||
| 68 | 84 | }; | |||||||||
| 69 | |||||||||||
| 70 | 84 | my ($self, $version, $source_data) = @_; | |||||||||
| 71 | |||||||||||
| 72 | Bitcoin::Crypto::Exception::SegwitProgram->raise( | ||||||||||
| 73 | "can't get witness program data for version $version" | ||||||||||
| 74 | 84 | ▶ | ) unless exists $data_sources->{$version}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | 84 | my $program = Bitcoin::Crypto::Script->new(network => $self->network); | |||||||||
| 77 | $program | ||||||||||
| 78 | ->add_operation("OP_$version") | ||||||||||
| 79 | 84 | ->push_bytes($data_sources->{$version}->($self, $source_data)); | |||||||||
| 80 | |||||||||||
| 81 | 84 | return $program; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | sub get_legacy_address | ||||||||||
| 85 | 43 | ▶ | { | ||||||||
|
Subroutine
get_legacy_address: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | 43 | my ($self) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 43 | ▶ | Bitcoin::Crypto::Exception::AddressGenerate->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 'legacy addresses can only be created with BIP44 in legacy (BIP44) mode' | ||||||||||
| 90 | ) unless $self->has_purpose(BIP44_PURPOSE); | ||||||||||
| 91 | |||||||||||
| 92 | 40 | my $pkh = $self->network->p2pkh_byte . $self->get_hash; | |||||||||
| 93 | 40 | return encode_base58check($pkh); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | sub get_compat_address | ||||||||||
| 97 | 31 | ▶ | { | ||||||||
|
Subroutine
get_compat_address: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 98 | 31 | my ($self) = @_; | |||||||||
| 99 | |||||||||||
| 100 | # network field is not required, lazy check for completeness | ||||||||||
| 101 | 31 | ▶ | Bitcoin::Crypto::Exception::NetworkConfig->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 'this network does not support segregated witness' | ||||||||||
| 103 | ) unless $self->network->supports_segwit; | ||||||||||
| 104 | |||||||||||
| 105 | 31 | ▶ | Bitcoin::Crypto::Exception::AddressGenerate->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 'compat addresses can only be created with BIP44 in compat (BIP49) mode' | ||||||||||
| 107 | ) unless $self->has_purpose(BIP44_COMPAT_PURPOSE); | ||||||||||
| 108 | |||||||||||
| 109 | 28 | return $self->witness_program->get_legacy_address; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | sub get_segwit_address | ||||||||||
| 113 | 30 | ▶ | { | ||||||||
|
Subroutine
get_segwit_address: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 114 | 30 | my ($self) = @_; | |||||||||
| 115 | |||||||||||
| 116 | # network field is not required, lazy check for completeness | ||||||||||
| 117 | 30 | ▶ | Bitcoin::Crypto::Exception::NetworkConfig->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 'this network does not support segregated witness' | ||||||||||
| 119 | ) unless $self->network->supports_segwit; | ||||||||||
| 120 | |||||||||||
| 121 | 30 | ▶ | Bitcoin::Crypto::Exception::AddressGenerate->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 'segwit addresses can only be created with BIP44 in segwit (BIP84) mode' | ||||||||||
| 123 | ) unless $self->has_purpose(BIP44_SEGWIT_PURPOSE); | ||||||||||
| 124 | |||||||||||
| 125 | 27 | ▶ | Bitcoin::Crypto::Exception::AddressGenerate->raise( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 'segwit addresses must not be generated with uncompressed keys to avoid potential fund loss' | ||||||||||
| 127 | ) unless $self->compressed; | ||||||||||
| 128 | |||||||||||
| 129 | 26 | return encode_segwit($self->network->segwit_hrp, $self->witness_program->run->stack_serialized); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 40 | signature_for get_taproot_address => ( | |||||||||
| 133 | method => !!1, | ||||||||||
| 134 | positional => [Maybe [BitcoinScriptTree], {default => undef}], | ||||||||||
| 135 | ); | ||||||||||
| 136 | |||||||||||
| 137 | sub get_taproot_address | ||||||||||
| 138 | { | ||||||||||
| 139 | 28 | my ($self, $script_tree) = @_; | |||||||||
| 140 | |||||||||||
| 141 | # network field is not required, lazy check for completeness | ||||||||||
| 142 | ▶ | Bitcoin::Crypto::Exception::NetworkConfig->raise( | |||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 'this network does not support segregated witness' | ||||||||||
| 144 | ) unless $self->network->supports_segwit; | ||||||||||
| 145 | |||||||||||
| 146 | ▶ | Bitcoin::Crypto::Exception::AddressGenerate->raise( | |||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 'taproot addresses can only be created with BIP44 in taproot (BIP86) mode' | ||||||||||
| 148 | ) unless $self->has_purpose(BIP44_TAPROOT_PURPOSE); | ||||||||||
| 149 | |||||||||||
| 150 | ▶ | my $taproot_program = $self->witness_program( | |||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | TAPROOT_WITNESS_VERSION, | ||||||||||
| 152 | defined $script_tree ? {tweak_suffix => $script_tree->get_merkle_root} : {} | ||||||||||
| 153 | ); | ||||||||||
| 154 | |||||||||||
| 155 | return encode_segwit($self->network->segwit_hrp, $taproot_program->run->stack_serialized); | ||||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | sub get_address | ||||||||||
| 159 | 26 | ▶ | { | ||||||||
|
Subroutine
get_address: called CC=6 SCAR=19.3
Pod
documented
| |||||||||||
| 160 | 26 | my ($self) = @_; | |||||||||
| 161 | |||||||||||
| 162 | 26 | ▶ | return $self->get_taproot_address | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | if $self->has_purpose(BIP44_TAPROOT_PURPOSE); | ||||||||||
| 164 | |||||||||||
| 165 | 18 | ▶ | return $self->get_segwit_address | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | if $self->has_purpose(BIP44_SEGWIT_PURPOSE); | ||||||||||
| 167 | |||||||||||
| 168 | 10 | ▶ | return $self->get_compat_address | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | if $self->has_purpose(BIP44_COMPAT_PURPOSE); | ||||||||||
| 170 | |||||||||||
| 171 | 2 | ▶ | return $self->get_legacy_address | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | if $self->has_purpose(BIP44_PURPOSE); | ||||||||||
| 173 | |||||||||||
| 174 | 0 | ▶ | return $self->get_taproot_address | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | if $self->network->supports_segwit; | ||||||||||
| 176 | |||||||||||
| 177 | 0 | return $self->get_legacy_address; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | 40 | 1; | |||||||||
| 181 | |||||||||||
| 182 | __END__ | ||||||||||