From 3829e3fa7f701ebcdc9ce48c7f4d273f5f907fa7 Mon Sep 17 00:00:00 2001 From: Mike Shultz Date: Sun, 6 Aug 2023 00:05:23 -0600 Subject: [PATCH] fix: minor docs and type fixes --- DEVELOPMENT.md | 2 +- ledgereth/comms.py | 11 ++++++++--- ledgereth/constants.py | 8 +++----- ledgereth/messages.py | 4 ++-- ledgereth/objects.py | 12 ++++++------ ledgereth/utils.py | 14 ++++++++++---- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 573cd59..3765cab 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -2,7 +2,7 @@ ## Docs -A goo dway to work on docs is to use `sphinx-autobuild` that will build the docs and start a local Web server that will auto-reload on save. +A good way to work on docs is to use `sphinx-autobuild` that will build the docs and start a local Web server that will auto-reload on save. sphinx-autobuild --watch ledgereth/ docs/ build/docs/ diff --git a/ledgereth/comms.py b/ledgereth/comms.py index 6424eeb..8348e28 100644 --- a/ledgereth/comms.py +++ b/ledgereth/comms.py @@ -14,7 +14,8 @@ class LedgerCommands: - """APDU commands for communication with ledger-app-eth. Tested only on a Ledger Nano S. + """APDU commands for communication with Ledger's app-ethereum. Tested on + Ledger Nano S and Nano X. See `ledger-app-eth`_ documentation. @@ -86,7 +87,7 @@ def get(name: str) -> bytes: @staticmethod def get_with_data( - name: str, data: bytes, Lc: bytes = None, Le: bytes = None + name: str, data: bytes, Lc: Optional[bytes] = None, Le: Optional[bytes] = None ) -> bytes: if not hasattr(LedgerCommands, name): raise ValueError("Command not available") @@ -109,7 +110,11 @@ def dongle_send(dongle: Dongle, command_string: str) -> bytes: def dongle_send_data( - dongle: Dongle, command_string: str, data: bytes, Lc: bytes = None, Le: bytes = None + dongle: Dongle, + command_string: str, + data: bytes, + Lc: Optional[bytes] = None, + Le: Optional[bytes] = None, ) -> bytes: """Send a command with data to the dongle""" hex_command = LedgerCommands.get_with_data(command_string, data, Lc=Lc, Le=Le) diff --git a/ledgereth/constants.py b/ledgereth/constants.py index e206c8a..8d81781 100644 --- a/ledgereth/constants.py +++ b/ledgereth/constants.py @@ -1,7 +1,5 @@ import os -from typing import Any, Dict, Type - -from eth_utils import remove_0x_prefix +from typing import Any, Dict def getenvint(key, default=0): @@ -31,14 +29,14 @@ def getenvint(key, default=0): if LEGACY_ACCOUNTS: DEFAULT_PATH_STRING = "44'/60'/0'/0" DEFAULT_PATH_ENCODED = b"\x80\x00\x00,\x80\x00\x00<\x80\x00\x00\x00\x00\x00\x00\x00" -DEFAULT_PATH = remove_0x_prefix(DEFAULT_PATH_ENCODED.hex()) +DEFAULT_PATH = DEFAULT_PATH_ENCODED.hex() VRS_RETURN_LENGTH = int(65).to_bytes(1, "big") # Data size expected from Ledger DATA_CHUNK_SIZE = 255 # Default "zero" values in EVM/Solidity -DEFAULTS: Dict[Type, Any] = { +DEFAULTS: Dict[type, Any] = { int: 0, bytes: b"", } diff --git a/ledgereth/messages.py b/ledgereth/messages.py index f9abd2e..bc36415 100644 --- a/ledgereth/messages.py +++ b/ledgereth/messages.py @@ -25,7 +25,7 @@ def sign_message( app on the Ledger device according to `EIP-191`_. :param message: (:code:`str|bytes`) - A bit of text to sign - :param sender_path: (:code:`str`) - HID derivation path for the account to + :param sender_path: (:code:`str`) - HD derivation path for the account to sign with. :param dongle: (:class:`ledgerblue.Dongle.Dongle`) - The Web3 instance to use :return: :class:`ledgereth.objects.SignedMessage` @@ -101,7 +101,7 @@ def sign_typed_data_draft( :param domain_hash: (:code:`str`) - Hash of the EIP-712 domain :param message_hash: (:code:`str`) - Hash of the message - :param sender_path: (:code:`str`) - HID derivation path for the account to + :param sender_path: (:code:`str`) - HD derivation path for the account to sign with. Defaults to first account in the derivation path. :param dongle: (:class:`ledgerblue.Dongle.Dongle`) - The Dongle instance to use to communicate with the Ledger device diff --git a/ledgereth/objects.py b/ledgereth/objects.py index 2b5e382..f8b63ad 100644 --- a/ledgereth/objects.py +++ b/ledgereth/objects.py @@ -80,9 +80,9 @@ def __init__( INS: bytes, P1: bytes, P2: bytes, - Lc: bytes = None, - Le: bytes = None, - data: bytes = None, + Lc: Optional[bytes] = None, + Le: Optional[bytes] = None, + data: Optional[bytes] = None, ): if not ( is_bytes(CLA) @@ -103,7 +103,7 @@ def __init__( self.Le = Le self.data = data - def set_data(self, data: bytes, Lc: bytes = None) -> None: + def set_data(self, data: bytes, Lc: Optional[bytes] = None) -> None: """Set the command data and its length :param data: (:class:`bytes`) - The raw ``bytes`` data. This should not @@ -243,8 +243,8 @@ class Transaction(SerializableTransaction): .. note:: A chain_id is set by default (``1``). It is not required to be a valid legacy transaction, but without it your transaction is - suceptible to replay attack. If for some reason you absolutely do not want it in your - tx, set it to ``None``. + suceptible to replay attack. If for some reason you absolutely do not + want it in your tx, set it to ``None``. .. _`EIP-155`: https://eips.ethereum.org/EIPS/eip-155 """ diff --git a/ledgereth/utils.py b/ledgereth/utils.py index 24c424a..f00d4ce 100644 --- a/ledgereth/utils.py +++ b/ledgereth/utils.py @@ -191,7 +191,9 @@ def coerce_access_list(access_list): return access_list -def coerce_list_types(types: List[Type], to_coerce: List[Any]) -> List[Any]: +def coerce_list_types( + types: List[Optional[type]], to_coerce: List[Union[Any, None]] +) -> List[Any]: """Coerce types of a list to given types in order""" for i, v in enumerate(to_coerce): @@ -199,13 +201,17 @@ def coerce_list_types(types: List[Type], to_coerce: List[Any]) -> List[Any]: if types[i] is None: continue + # Only way to get mypy to chill was to assign to its own var + this_type = types[i] + assert this_type is not None + # Some things don't transalate, like b'' being 0 if not v: - to_coerce[i] = DEFAULTS[types[i]] + to_coerce[i] = DEFAULTS[this_type] else: if types[i] in COERCERS: - to_coerce[i] = COERCERS[types[i]](v) + to_coerce[i] = COERCERS[this_type](v) else: - to_coerce[i] = types[i](v) + to_coerce[i] = this_type(v) return to_coerce