Skip to content

Commit

Permalink
Disable xpub/xpriv serialization for non-bitcoin curves
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkozlik committed Aug 20, 2024
1 parent 2fa9a56 commit bc35191
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions slip10/slip10.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def __init__(self, message):
self.message = message


class SerializationError(ValueError):
def __init__(self, message):
self.message = message


class SLIP10:
def __init__(
self,
Expand Down Expand Up @@ -231,6 +236,11 @@ def get_xpriv_from_path(self, path):
m/x/x'/x notation. (e.g. m/0'/1/2'/2 or m/0H/1/2H/2).
:return: The encoded extended pubkey as str.
"""
if self.curve.name != "secp256k1":
raise SerializationError(
"xpriv serialization is supported only for secp256k1"
)

if self.privkey is None:
raise PrivateDerivationError

Expand Down Expand Up @@ -262,6 +272,11 @@ def get_xpub_from_path(self, path):
m/x/x'/x notation. (e.g. m/0'/1/2'/2 or m/0H/1/2H/2).
:return: The encoded extended pubkey as str.
"""
if self.curve.name != "secp256k1":
raise SerializationError(
"xpub serialization is supported only for secp256k1"
)

if isinstance(path, str):
path = _deriv_path_str_to_list(path)

Expand Down Expand Up @@ -292,6 +307,11 @@ def get_xpriv(self):

def get_xpriv_bytes(self):
"""Get the encoded extended private key."""
if self.curve.name != "secp256k1":
raise SerializationError(
"xpriv serialization is supported only for secp256k1"
)

if self.privkey is None:
raise PrivateDerivationError
return _serialize_extended_key(
Expand All @@ -309,6 +329,11 @@ def get_xpub(self):

def get_xpub_bytes(self):
"""Get the encoded extended public key."""
if self.curve.name != "secp256k1":
raise SerializationError(
"xpub serialization is supported only for secp256k1"
)

return _serialize_extended_key(
self.pubkey,
self.depth,
Expand Down

0 comments on commit bc35191

Please sign in to comment.