Skip to content

Commit

Permalink
Fixed to set key_type to :p2tr when generated by Bitcoin::Key#from_xo…
Browse files Browse the repository at this point in the history
…nly_pubkey
  • Loading branch information
azuchi committed Jul 7, 2024
1 parent 19a8b6d commit 43688ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/bitcoin/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def self.from_wif(wif)
# @return [Bitcoin::Key] key object has public key.
def self.from_xonly_pubkey(xonly_pubkey)
raise ArgumentError, "xonly_pubkey must be #{X_ONLY_PUBKEY_SIZE} bytes" unless xonly_pubkey.htb.bytesize == X_ONLY_PUBKEY_SIZE
Bitcoin::Key.new(pubkey: "02#{xonly_pubkey}", key_type: TYPES[:compressed])
Bitcoin::Key.new(pubkey: "02#{xonly_pubkey}", key_type: TYPES[:p2tr])
end

# Generate from public key point.
Expand Down
12 changes: 11 additions & 1 deletion spec/bitcoin/key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
it 'should be treated as an Xonly public key.' do
digest = Bitcoin.sha256('message')
key = Bitcoin::Key.new(priv_key: '4f0bdbfda0e7acf1aa44cc5c5e90068096b258089fd8230ed3a7ea5227214038',
key_type: Bitcoin::Key::TYPES[:compressed])
key_type: Bitcoin::Key::TYPES[:p2tr])
aux = 'bf498c6e09a829330f59127ff176e5c6dbe108893fea46cd11e123ba0425a7b2'
sig = key.sign(digest, false, aux.htb, algo: :schnorr)
expect(key.verify(sig, digest, algo: :schnorr)).to be true
Expand Down Expand Up @@ -234,6 +234,16 @@
end
end

describe '#from_xonly_pubkey' do
it do
key = described_class.from_xonly_pubkey('669b8afcec803a0d323e9a17f3ea8e68e8abe5a278020a929adbec52421adbd0')
expect(key.xonly_pubkey).to eq('669b8afcec803a0d323e9a17f3ea8e68e8abe5a278020a929adbec52421adbd0')
expect(key.key_type).to eq(Bitcoin::Key::TYPES[:p2tr])
expect{described_class.from_xonly_pubkey('02669b8afcec803a0d323e9a17f3ea8e68e8abe5a278020a929adbec52421adbd0')}.
to raise_error(ArgumentError, "xonly_pubkey must be 32 bytes")
end
end

describe '#to_addr' do
it 'return address corresponding to key_type' do
p2pkh = 'mmy7BEH1SUGAeSVUR22pt5hPaejo2645F1'
Expand Down

0 comments on commit 43688ca

Please sign in to comment.