|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Dict, List, Optional |
| 4 | + |
| 5 | +from eth_typing import HexStr |
| 6 | +from ledgerblue.Dongle import Dongle |
| 7 | +from ledgereth import find_account, get_account_by_path, get_accounts |
| 8 | +from ledgereth.comms import init_dongle |
| 9 | +from ledgereth.messages import sign_message |
| 10 | +from ledgereth.objects import LedgerAccount, SignedMessage |
| 11 | + |
| 12 | +from ...chains.common import BaseAccount, get_verification_buffer |
| 13 | + |
| 14 | + |
| 15 | +class LedgerETHAccount(BaseAccount): |
| 16 | + """Account using the Ethereum app on Ledger hardware wallets.""" |
| 17 | + |
| 18 | + CHAIN = "ETH" |
| 19 | + CURVE = "secp256k1" |
| 20 | + _account: LedgerAccount |
| 21 | + _device: Dongle |
| 22 | + |
| 23 | + def __init__(self, account: LedgerAccount, device: Dongle): |
| 24 | + """Initialize an aleph.im account instance that relies on a LedgerHQ |
| 25 | + device and the Ethereum Ledger application for signatures. |
| 26 | +
|
| 27 | + See the static methods `self.from_address(...)` and `self.from_path(...)` |
| 28 | + for an easier method of instantiation. |
| 29 | + """ |
| 30 | + self._account = account |
| 31 | + self._device = device |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def from_address( |
| 35 | + address: str, device: Optional[Dongle] = None |
| 36 | + ) -> Optional[LedgerETHAccount]: |
| 37 | + """Initialize an aleph.im account from a LedgerHQ device from |
| 38 | + a known wallet address. |
| 39 | + """ |
| 40 | + device = device or init_dongle() |
| 41 | + account = find_account(address=address, dongle=device, count=5) |
| 42 | + return LedgerETHAccount( |
| 43 | + account=account, |
| 44 | + device=device, |
| 45 | + ) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def from_path(path: str, device: Optional[Dongle] = None) -> LedgerETHAccount: |
| 49 | + """Initialize an aleph.im account from a LedgerHQ device from |
| 50 | + a known wallet account path.""" |
| 51 | + device = device or init_dongle() |
| 52 | + account = get_account_by_path(path_string=path, dongle=device) |
| 53 | + return LedgerETHAccount( |
| 54 | + account=account, |
| 55 | + device=device, |
| 56 | + ) |
| 57 | + |
| 58 | + async def sign_message(self, message: Dict) -> Dict: |
| 59 | + """Sign a message inplace.""" |
| 60 | + message: Dict = self._setup_sender(message) |
| 61 | + |
| 62 | + # TODO: Check why the code without a wallet uses `encode_defunct`. |
| 63 | + msghash: bytes = get_verification_buffer(message) |
| 64 | + sig: SignedMessage = sign_message(msghash, dongle=self._device) |
| 65 | + |
| 66 | + signature: HexStr = sig.signature |
| 67 | + |
| 68 | + message["signature"] = signature |
| 69 | + return message |
| 70 | + |
| 71 | + def get_address(self) -> str: |
| 72 | + return self._account.address |
| 73 | + |
| 74 | + def get_public_key(self) -> str: |
| 75 | + """Obtaining the public key is not supported by the ledgereth library |
| 76 | + we use, and may not be supported by LedgerHQ devices at all. |
| 77 | + """ |
| 78 | + raise NotImplementedError() |
| 79 | + |
| 80 | + |
| 81 | +def get_fallback_account() -> LedgerETHAccount: |
| 82 | + """Returns the first account available on the device first device found.""" |
| 83 | + device: Dongle = init_dongle() |
| 84 | + accounts: List[LedgerAccount] = get_accounts(dongle=device, count=1) |
| 85 | + if not accounts: |
| 86 | + raise ValueError("No account found on device") |
| 87 | + account = accounts[0] |
| 88 | + return LedgerETHAccount(account=account, device=device) |
0 commit comments