Skip to content

Commit

Permalink
Add Key#decompress_pubkey that return uncompressed public key with he…
Browse files Browse the repository at this point in the history
…x format
  • Loading branch information
azuchi committed Dec 15, 2023
1 parent 5586b79 commit 6fbf16b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/bitcoin/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ def xonly_pubkey
pubkey[2..65]
end

# Convert this key to decompress key.
# @return [String] decompress public key with hex format.
def decompress_pubkey
pubkey.htb.bytesize == PUBLIC_KEY_SIZE ? pubkey : to_point.to_hex(false)
end

# check +pubkey+ (hex) is compress or uncompress pubkey.
def self.compress_or_uncompress_pubkey?(pubkey)
p = pubkey.htb
Expand Down
22 changes: 22 additions & 0 deletions spec/bitcoin/key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,26 @@ def test_generate
end
end

describe '#decompress_pubkey' do
context 'pure ruby' do
it 'return decompressed public key' do
test_decompress
end
end

context 'libsecp256k1', use_secp256k1: true do
it 'return decompressed public key' do
test_decompress
end
end

def test_decompress
compress_key = Bitcoin::Key.generate(Bitcoin::Key::TYPES[:compressed])
uncompressed_pubkey = Bitcoin::Key.new(priv_key: compress_key.priv_key, key_type: Bitcoin::Key::TYPES[:uncompressed])
expect(compress_key.decompress_pubkey).to eq(uncompressed_pubkey.pubkey)

uncompressed_key = Bitcoin::Key.generate(Bitcoin::Key::TYPES[:uncompressed])
expect(uncompressed_key.decompress_pubkey).to eq(uncompressed_key.pubkey)
end
end
end

0 comments on commit 6fbf16b

Please sign in to comment.