Skip to content

Commit 684b618

Browse files
committed
Merge branch '1ma-fix-xonly-pubkey-creation'
2 parents a24914e + e91ebcb commit 684b618

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

bitcoinutils/keys.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def __init__(self, hex_str: Optional[str] = None, message: Optional[str] = None,
583583

584584
# compressed - SEC FORMAT: 0x02|0x03 + x coordinate (if 02 then y
585585
# is even else y is odd. Calculate y and then instantiate the ecdsa key
586-
x_coord = int(hex_str[2:], 16)
586+
x_coord = int(hex_str if taproot else hex_str[2:], 16)
587587

588588
# y = modulo_square_root( (x**3 + 7) mod p ) -- there will be 2 y values
589589
y_values = sqrt_mod(
@@ -1411,4 +1411,4 @@ def main():
14111411

14121412

14131413
if __name__ == "__main__":
1414-
main()
1414+
main()

tests/test_keys.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,18 @@ def setUp(self):
7272
b"\xd9Y\xf2\x81[\x16\xf8\x17\x98H:\xdaw&\xa3\xc4e]\xa4\xfb\xfc\x0e\x11"
7373
b"\x08\xa8\xfd\x17\xb4H\xa6\x85T\x19\x9cG\xd0\x8f\xfb\x10\xd4\xb8"
7474
)
75+
76+
self.public_key_xonly = (
77+
"1baf348d377cf9a73b39807b051bd7e6921bd9e5f4e78e5640803f1fd9b90501"
78+
)
79+
self.public_key_xonly_bytes = (
80+
b"\x1b\xaf4\x8d7|\xf9\xa7;9\x80{\x05\x1b\xd7\xe6\x92\x1b\xd9\xe5\xf4\xe7"
81+
b"\x8eV@\x80?\x1f\xd9\xb9\x05\x016\xee2\x0c\xfa\xb9\xaa\x93\xb5\xbf\x82"
82+
b",'\xa9\xe7\xd5\xc3cN\xb9\xad~rh\xc5\xb0\x14\xaeY\xa0Y\xc4"
83+
)
84+
7585
self.address = "1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm"
76-
86+
7787
# Message public key recovery tests
7888
self.valid_message = "Hello, Bitcoin!"
7989
# 65-byte Bitcoin signature (1-byte recovery ID + 64-byte ECDSA signature)
@@ -87,6 +97,10 @@ def test_pubkey_creation(self):
8797
pub2 = PublicKey(self.public_key_hexc)
8898
self.assertEqual(pub2.to_bytes(), self.public_key_bytes)
8999

100+
def test_pubkey_creation_xonly(self):
101+
xonly = PublicKey(self.public_key_xonly)
102+
self.assertEqual(xonly.to_bytes(), self.public_key_xonly_bytes)
103+
90104
def test_pubkey_uncompressed(self):
91105
pub = PublicKey(self.public_key_hex)
92106
self.assertEqual(pub.to_hex(compressed=False), self.public_key_hex)
@@ -106,7 +120,7 @@ def test_pubkey_to_hash160(self):
106120
def test_pubkey_x_only(self):
107121
pub = PublicKey(self.public_key_hex)
108122
self.assertEqual(pub.to_x_only_hex(), self.public_key_hex[2:66])
109-
123+
110124
#Tests for PublicKey recovery from message and signature
111125
def test_public_key_recovery_valid(self):
112126
"""Test successful public key recovery from a valid message and signature"""
@@ -351,6 +365,6 @@ def test_legacy_address_from_mnemonic(self):
351365
hdw.from_path("m/44'/1'/0'/0/3")
352366
address = hdw.get_private_key().get_public_key().get_address()
353367
self.assertTrue(address.to_string(), self.legacy_address_m_44_1h_0h_0_3)
354-
368+
355369
if __name__ == "__main__":
356370
unittest.main()

0 commit comments

Comments
 (0)