-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
275 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import functools | ||
|
||
from trezorlib.exceptions import ( | ||
Cancelled, | ||
OutdatedFirmwareError, | ||
PinException, | ||
TrezorFailure, | ||
) | ||
from trezorlib.transport import TransportException | ||
|
||
from safe_cli.operators.exceptions import HardwareWalletException | ||
from safe_cli.operators.hw_accounts.hw_account import InvalidDerivationPath | ||
|
||
|
||
class UnsupportedHwWalletException(Exception): | ||
pass | ||
|
||
|
||
def raise_trezor_exception_as_hw_account_exception(function): | ||
@functools.wraps(function) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return function(*args, **kwargs) | ||
except TrezorFailure as e: | ||
raise HardwareWalletException(e.message) | ||
except OutdatedFirmwareError: | ||
raise HardwareWalletException("Trezor firmware version is not supported") | ||
except PinException: | ||
raise HardwareWalletException("Wrong PIN") | ||
except Cancelled: | ||
raise HardwareWalletException("Trezor operation was cancelled") | ||
except TransportException: | ||
raise HardwareWalletException("Trezor device is not connected") | ||
except InvalidDerivationPath as e: | ||
raise HardwareWalletException(e.message) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,51 @@ | ||
from typing import Tuple | ||
from functools import lru_cache | ||
|
||
from eth_typing import ChecksumAddress | ||
from trezorlib import tools | ||
from trezorlib.client import TrezorClient, get_default_client | ||
from trezorlib.ethereum import get_address, sign_typed_data_hash | ||
from trezorlib.ui import ClickUI | ||
|
||
from safe_cli.operators.hw_accounts.hw_account import HwAccount | ||
from safe_cli.operators.hw_accounts.trezor_exceptions import ( | ||
raise_trezor_exception_as_hw_account_exception, | ||
) | ||
|
||
|
||
@lru_cache(maxsize=None) | ||
@raise_trezor_exception_as_hw_account_exception | ||
def get_trezor_client() -> TrezorClient: | ||
""" | ||
Return default trezor configuration that store passphrase on host. | ||
This method is cached to share the same configuration between trezor calls while the class is not instantiated. | ||
:return: | ||
""" | ||
ui = ClickUI(passphrase_on_host=True) | ||
client = get_default_client(ui=ui) | ||
return client | ||
|
||
|
||
class TrezorManager(HwAccount): | ||
def __init__(self, derivation_path: str, address: ChecksumAddress): | ||
self.client = None | ||
self.client = get_trezor_client() | ||
super().__init__(derivation_path, address) | ||
|
||
@raise_trezor_exception_as_hw_account_exception | ||
def get_address_by_derivation_path(derivation_path: str) -> ChecksumAddress: | ||
""" | ||
:param derivation_path: | ||
:return: public address for provided derivation_path | ||
""" | ||
raise NotImplementedError | ||
if TrezorManager.is_valid_derivation_path(derivation_path): | ||
client = get_trezor_client() | ||
address_n = tools.parse_path(derivation_path) | ||
return get_address(client=client, n=address_n) | ||
|
||
def sign_typed_hash(self, domain_hash, message_hash) -> Tuple[bytes, bytes, bytes]: | ||
raise NotImplementedError | ||
@raise_trezor_exception_as_hw_account_exception | ||
def sign_typed_hash(self, domain_hash, message_hash) -> bytes: | ||
address_n = tools.parse_path(self.derivation_path) | ||
signed = sign_typed_data_hash( | ||
self.client, n=address_n, domain_hash=domain_hash, message_hash=message_hash | ||
) | ||
return signed.signature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.