Skip to content

Commit

Permalink
Inverse of uncompressed public key does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Mar 2, 2024
1 parent 1855ada commit 5c6b2cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions bitcoinlib/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,12 +1680,15 @@ def inverse(self):
:return Key:
"""
if self.is_private:
return HDKey(secp256k1_n - self.secret, network=self.network, compressed=self.compressed,
return HDKey(secp256k1_n - self.secret, network=self.network.name, compressed=self.compressed,
witness_type=self.witness_type, multisig=self.multisig, encoding=self.encoding)
else:
# Inverse y in init: self._y = secp256k1_p - self._y
return HDKey(('02' if self._y % 2 else '03') + self.x_hex, network=self.network, compressed=self.compressed,
witness_type=self.witness_type, multisig=self.multisig, encoding=self.encoding)
if not self.compressed:
return self
return HDKey(('02' if self._y % 2 else '03') + self.x_hex, network=self.network.name,
compressed=self.compressed, witness_type=self.witness_type, multisig=self.multisig,
encoding=self.encoding)

def info(self):
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ def test_keys_inverse2(self):
self.assertEqual((-k).address(), pub_k.inverse().address())
self.assertEqual((-k).address(), k.inverse().address())

pkwif = 'Mtpv7L6Q8tPadPv8iUDKAXk1wyCmdJ6q2y2d3AixyoGVMH3WeoCDwkLbpUBXXB5HHbueeqTikkeBGTBV7tCcgJtEfm1wCt4ZcQixz7TtV5CAXfd'
k = HDKey(pkwif, network='litecoin', compressed=False, witness_type='p2sh-segwit')
pub_k = k.public()
self.assertEqual(pub_k, pub_k.inverse())

k = HDKey(pkwif, network='litecoin', witness_type='p2sh-segwit')
pub_k = k.public()
kpi = pub_k.inverse()
self.assertEqual(kpi.address(), "MQVYsZ5o5uhN2X6QMbu9RVu5YADiq859MY")
self.assertEqual(kpi.witness_type, 'p2sh-segwit')
self.assertEqual(kpi.network.name, 'litecoin')

def test_dict_and_json_outputs(self):
k = HDKey()
k.address(script_type='p2wsh', encoding='bech32')
Expand Down

0 comments on commit 5c6b2cc

Please sign in to comment.