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