Skip to content

Commit

Permalink
Deprecate key_hash of public key in favor of get_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrtj committed Jul 21, 2024
1 parent 49b6a89 commit a51c58a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Revision history for Perl extension Bitcoin::Crypto.
- exception is no longer raised if the default network is among multiple possible networks after deserialization of WIFs and extended keys
- module can now be configured to work in single-network mode, disallowing creation of objects with different networks

[Changes]
[Changes and deprecations]
- codes in Bitcoin::Crypto::Script::Opcode are now integers instead of bytestrings of length 1
- Bitcoin::Crypto::Util::get_path_info now returns instances of Bitcoin::Crypto::DerivationPath (internal structure remains unchanged)
- Bitcoin::Crypto::Key::Public::key_hash is now deprecated, added new method get_hash (consistent naming with other modules)

2.004 Tue Apr 23 2024
[Improvements]
Expand Down
28 changes: 24 additions & 4 deletions lib/Bitcoin/Crypto/Key/Public.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use strict;
use warnings;
use Moo;
use Type::Params -sigs;
use Carp qw(carp);

use Bitcoin::Crypto::Script;
use Bitcoin::Crypto::Base58 qw(encode_base58check);
Expand All @@ -19,18 +20,27 @@ with qw(Bitcoin::Crypto::Role::BasicKey);

sub _is_private { 0 }

signature_for key_hash => (
signature_for get_hash => (
method => Object,
positional => [],
);

sub key_hash
sub get_hash
{
my ($self) = @_;

return hash160($self->to_serialized);
}

sub key_hash
{
my $self = shift;
my $class = ref $self;

carp "$class->key_hash() is now deprecated. Use $class->get_hash() instead";
return $self->get_hash(@_);
}

around from_serialized => sub {
my ($orig, $class, $key) = @_;

Expand All @@ -52,7 +62,7 @@ sub witness_program
my $program = Bitcoin::Crypto::Script->new(network => $self->network);
$program
->add_operation('OP_' . Bitcoin::Crypto::Constants::segwit_witness_version)
->push_bytes($self->key_hash);
->push_bytes($self->get_hash);

return $program;
}
Expand All @@ -70,7 +80,7 @@ sub get_legacy_address
'legacy addresses can only be created with BIP44 in legacy (BIP44) mode'
) unless $self->has_purpose(Bitcoin::Crypto::Constants::bip44_purpose);

my $pkh = $self->network->p2pkh_byte . $self->key_hash;
my $pkh = $self->network->p2pkh_byte . $self->get_hash;
return encode_base58check($pkh);
}

Expand Down Expand Up @@ -218,6 +228,16 @@ Deprecated. Use C<< $class->from_serialized([hex => $data]) >> instead.
Deprecated. Use C<< to_format [hex => $key->to_serialized()] >> instead.
=head2 get_hash
$bytestr = $object->get_hash()
Returns hash160 of the serialized public key.
=head2 key_hash
Deprecated. Use C<< $key->get_hash() >> instead.
=head2 set_compressed
$key_object = $object->set_compressed($val)
Expand Down

0 comments on commit a51c58a

Please sign in to comment.