Skip to content

Commit

Permalink
Deduplicate wallet error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kristapsk committed Dec 1, 2023
1 parent 2d3e90a commit 6ec6308
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions src/jmclient/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,34 @@ class WalletError(Exception):
pass


class WalletCannotGetPrivateKeyFromWatchOnly(WalletError):

def __init__(self):
super().__init__("Cannot get a private key from a watch-only wallet.")


class WalletCacheValidationFailed(WalletError):

def __init__(self):
super().__init__("Wallet cache validation failed.")


class WalletMixdepthOutOfRange(WalletError):

def __init__(self):
super().__init__("Mixdepth outside of wallet's range.")


class WalletInvalidPath(WalletError):

def __init__(self, path: str):
super().__init__(f"Invalid path, unknown root: {path}.")


class UnknownAddressForLabel(Exception):

def __init__(self, addr):
super().__init__('Unknown address for this wallet: ' + addr)
def __init__(self, addr: str):
super().__init__(f"Unknown address for this wallet: {addr}.")


class Mnemonic(MnemonicParent):
Expand Down Expand Up @@ -640,7 +664,7 @@ def get_address_from_path(self, path,
addr = new_addr
cache[b'A'] = addr.encode('ascii')
elif addr != new_addr:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return addr

def get_new_addr(self, mixdepth, address_type,
Expand Down Expand Up @@ -973,7 +997,7 @@ def get_script_from_path(self, path,
if script is None:
cache[b'S'] = script = new_script
elif script != new_script:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return script

def get_script(self, mixdepth, address_type, index,
Expand All @@ -996,7 +1020,7 @@ def _get_keypair_from_path(self, path,
if pubkey is None:
cache[b'P'] = pubkey = new_pubkey
elif pubkey != new_pubkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return privkey, pubkey, engine

def _get_pubkey_from_path(self, path,
Expand Down Expand Up @@ -2219,7 +2243,7 @@ def _check_path(self, path):
md, address_type, index = self.get_details(path)

if not 0 <= md <= self.max_mixdepth:
raise WalletError("Mixdepth outside of wallet's range.")
raise WalletMixdepthOutOfRange()
assert address_type in self._get_supported_address_types()

current_index = self._index_cache[md][address_type]
Expand Down Expand Up @@ -2249,7 +2273,7 @@ def get_path(self, mixdepth=None, address_type=None, index=None):
if mixdepth is not None:
assert isinstance(mixdepth, Integral)
if not 0 <= mixdepth <= self.max_mixdepth:
raise WalletError("Mixdepth outside of wallet's range.")
raise WalletMixdepthOutOfRange()

if address_type is not None:
if mixdepth is None:
Expand Down Expand Up @@ -2303,22 +2327,22 @@ def conv_level(lvl):

def _get_mixdepth_from_path(self, path):
if not self._is_my_bip32_path(path):
raise WalletError("Invalid path, unknown root: {}".format(path))
raise WalletInvalidPath(path)

return path[len(self._get_bip32_base_path())]

def _get_key_from_path(self, path,
validate_cache: bool = False):
if not self._is_my_bip32_path(path):
raise WalletError("Invalid path, unknown root: {}".format(path))
raise WalletInvalidPath(path)
cache = self._get_cache_for_path(path)
privkey = cache.get(b'p')
if privkey is None or validate_cache:
new_privkey = self._ENGINE.derive_bip32_privkey(self._master_key, path)
if privkey is None:
cache[b'p'] = privkey = new_privkey
elif privkey != new_privkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return privkey, self._ENGINE

def _get_keypair_from_path(self, path,
Expand All @@ -2333,14 +2357,14 @@ def _get_keypair_from_path(self, path,
if privkey is None:
cache[b'p'] = privkey = new_privkey
elif privkey != new_privkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
pubkey = cache.get(b'P')
if pubkey is None or validate_cache:
new_pubkey = self._ENGINE.privkey_to_pubkey(privkey)
if pubkey is None:
cache[b'P'] = pubkey = new_pubkey
elif pubkey != new_pubkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return privkey, pubkey, self._ENGINE

def _get_cache_keys_for_path(self, path):
Expand Down Expand Up @@ -2468,7 +2492,7 @@ def _get_bip32_mixdepth_path_level(cls, mixdepth):

def _get_mixdepth_from_path(self, path):
if not self._is_my_bip32_path(path):
raise WalletError("Invalid path, unknown root: {}".format(path))
raise WalletInvalidPath(path)

return path[len(self._get_bip32_base_path())] - 2**31

Expand Down Expand Up @@ -2621,7 +2645,7 @@ def _get_key_from_path(self, path,
if privkey is None:
cache[b'p'] = privkey = new_privkey
elif privkey != new_privkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return (privkey, locktime), engine
else:
return super()._get_key_from_path(path)
Expand All @@ -2641,14 +2665,14 @@ def _get_keypair_from_path(self, path,
if privkey is None:
cache[b'p'] = privkey = new_privkey
elif privkey != new_privkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
pubkey = cache.get(b'P')
if pubkey is None or validate_cache:
new_pubkey = engine.privkey_to_pubkey(privkey)
if pubkey is None:
cache[b'P'] = pubkey = new_pubkey
elif pubkey != new_pubkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return (privkey, locktime), (pubkey, locktime), engine

def _get_cache_for_path(self, path):
Expand Down Expand Up @@ -2802,11 +2826,11 @@ def _get_bip32_export_path(self, mixdepth=None, address_type=None):

def _get_key_from_path(self, path,
validate_cache: bool = False):
raise WalletError("Cannot get a private key from a watch-only wallet")
raise WalletCannotGetPrivateKeyFromWatchOnly()

def _get_keypair_from_path(self, path,
validate_cache: bool = False):
raise WalletError("Cannot get a private key from a watch-only wallet")
raise WalletCannotGetPrivateKeyFromWatchOnly()

def _get_pubkey_from_path(self, path,
validate_cache: bool = False):
Expand All @@ -2824,7 +2848,7 @@ def _get_pubkey_from_path(self, path,
if pubkey is None:
cache[b'P'] = pubkey = new_pubkey
elif pubkey != new_pubkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return (pubkey, locktime), self._TIMELOCK_ENGINE
cache = self._get_cache_for_path(path)
pubkey = cache.get(b'P')
Expand All @@ -2834,7 +2858,7 @@ def _get_pubkey_from_path(self, path,
if pubkey is None:
cache[b'P'] = pubkey = new_pubkey
elif pubkey != new_pubkey:
raise WalletError("Wallet cache validation failed")
raise WalletCacheValidationFailed()
return pubkey, self._ENGINE


Expand Down

0 comments on commit 6ec6308

Please sign in to comment.