Web3.py Integration

You can also integrate ledgereth directly into Web3.py to use it with your Ledger hardware wallet.

Web3.py middleware for Ledger devices.

exception ledgereth.web3.AccountNotFoundError[source]

An account with the given address was not found on the Ledger device.

Warning

This might raise if the account is not found within MAX_ACCOUNTS_FETCH iterations on the derivation path.

class ledgereth.web3.LedgerSignerMiddleware(w3: AsyncWeb3 | Web3)[source]

Web3.py middleware to leverage the Ledger device as a signer.

It will automatically intercept the relevant JSON-RPC calls and respond with data from your Ledger device.

Intercepted JSON-RPC methods:

  • eth_sendTransaction (web3.eth.send_transaction())

  • eth_accounts (web3.eth.accounts)

  • eth_sign (web3.eth.sign())

  • eth_signTypedData (web3.eth.sign_typed_data())

Example:

>>> from web3.auto import w3
>>> from ledgereth.web3 import LedgerSignerMiddleware
>>> w3.middleware_onion.add(LedgerSignerMiddleware)
>>> w3.eth.accounts
['0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', '0x8C8d35429F74ec245F8Ef2f4Fd1e551cFF97d650', '0x98e503f35D0a019cB0a251aD243a4cCFCF371F46']
wrap_make_request(make_request: Callable[[RPCEndpoint, Any], RPCResponse])[source]

Intercept some JSON-RPC requests and forward them to the Ledger device.