Skip to content

Commit

Permalink
Merge pull request #46 from mikeshultz/cleaning
Browse files Browse the repository at this point in the history
fix: minor docs and type fixes
  • Loading branch information
mikeshultz authored Aug 6, 2023
2 parents 4b3a1db + 3829e3f commit c2504ef
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
11 changes: 8 additions & 3 deletions ledgereth/comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<https://github.com/LedgerHQ/ledger-app-eth/blob/master/doc/ethapp.asc>`_
documentation.
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions ledgereth/constants.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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"",
}
4 changes: 2 additions & 2 deletions ledgereth/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions ledgereth/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
"""
Expand Down
14 changes: 10 additions & 4 deletions ledgereth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,27 @@ 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):
# SKIP!
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

0 comments on commit c2504ef

Please sign in to comment.