Skip to content

Commit

Permalink
Redo raw_key method
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrtj committed Sep 24, 2024
1 parent 4bcbfc8 commit 1678fa9
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/Bitcoin/Crypto/Role/Key.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,48 @@ signature_for raw_key => (
positional => [Maybe [Enum [qw(private public public_compressed)]], {default => undef}],
);

# helpers for raw_key
sub __full_private
{
my ($self, $key) = @_;
return ensure_length $key, Bitcoin::Crypto::Constants::key_max_length;
}

sub __private_to_public
{
my ($self, $key) = @_;
return ecc->create_public_key($self->__full_private($key));
}

sub __public_compressed
{
my ($self, $key, $compressed) = @_;
return ecc->compress_public_key($key, $compressed);
}

sub raw_key
{
my ($self, $type) = @_;
my $is_private = $self->_is_private;

$type = $is_private ? 'private' : 'public'
unless defined $type;
$type //= $is_private ? 'private' : 'public';
if ($type eq 'public' && (!$self->does('Bitcoin::Crypto::Role::Compressed') || $self->compressed)) {
$type = 'public_compressed';
}

if ($type eq 'private') {
Bitcoin::Crypto::Exception::KeyCreate->raise(
'cannot create private key from a public key'
) unless $is_private;

return ensure_length $self->key_instance, Bitcoin::Crypto::Constants::key_max_length;
return $self->__full_private($self->key_instance);
}
else {
my $compressed = $self->does('Bitcoin::Crypto::Role::Compressed') ? $self->compressed : !!1;
$compressed = !!1 if $type eq 'public_compressed';

my $key = $self->key_instance;
$key = ecc->create_public_key($key)
$key = $self->__private_to_public($key)
if $is_private;

return ecc->compress_public_key($key, $compressed);
return $self->__public_compressed($key, $type eq 'public_compressed');
}

# no need to check for invalid input, since we have a signature with enum
Expand Down

0 comments on commit 1678fa9

Please sign in to comment.