diff --git a/lib/bitcoin/key.rb b/lib/bitcoin/key.rb index 956d7d5..db8016a 100644 --- a/lib/bitcoin/key.rb +++ b/lib/bitcoin/key.rb @@ -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. diff --git a/spec/bitcoin/key_spec.rb b/spec/bitcoin/key_spec.rb index 7d908b0..ca911e5 100644 --- a/spec/bitcoin/key_spec.rb +++ b/spec/bitcoin/key_spec.rb @@ -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 @@ -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'