Skip to main content

Library to interface with ledger-app-eth on Ledger hardware wallets

Project description

ledger-eth-lib

This is a WIP. Consider it alpha

This is a library to interact with ledger-app-eth, the Ethereum app for the Ledger hardware wallets. It's goal is to make interfacing with the Ledger nice and simple with well known Ethereum+Python tools.

NOTE: Tested to work on Ledger Nano S and Nano X. Will probalby work with Ledger Blue

WARNING: The Ledger apps have changed the way accounts are derived with the release of Ledger Live. If you created your Ledger account(s) with the older Chrome app and want to use those account(s) with this library, you will need to set the LEDGER_LEGACY_ACCOUNTS env var. You can only use one or the other at a time. See the notes in source for more information.

Environment Configuration

There are a couple of environment variables that can affect the behavior of ledger-eth-lib, documented below:

  • MAX_ACCOUNTS_FETCH[default: 5]: The maximum accounts that will be fetched when looking up by address. If you created more than 5 accounts on your Ledger device, you may want to adjust this.
  • LEDGER_LEGACY_ACCOUNTS: If set (to anything), ledger-eth-lib will use the legacy Ledger bip32 derivation that was used to create accounts before Ledger Live.

CLI Usage

python -m ledgereth [command]

Get Accounts

To get the available accounts from your Ledger:

python -m ledgereth accounts

Create and Sign Transaction

You can use the CLI util to create and sign a transaction as well.

python -m ledgereth send FROM_ADDRESS TO_ADDRESS VALUE_WEI -n NONCE -p GAS_PRICE_WEI -c CHAIN_ID

Web3.py Integration

ledger-eth-lib provides a Web3.py middleware. It will automatically intercept the relevant JSON-RPC calls and respond with data from your Ledger device.

from web3.auto import w3
from ledgereth.web3 import LedgerSignerMiddleware
w3.middleware_onion.add(LedgerSignerMiddleware)

Intercepted JSON-RPC methods:

  • eth_sendTransaction
  • eth_accounts

Quickstart

Get Accounts

Fetch the availible accounts.

from ledgereth import get_accounts
accounts = get_accounts()
my_account = accounts[0].address

Create and Sign a Transaction

Create a transaction object and sign it with the default account.

from ledgereth import create_transaction
tx = create_transaction(
    '0xb78f53524ae9d465279e7c3495f71d5db2419e13',  # to
    '0x4dae53ee778a324bd317bd270d6227406b6bd4ec',  # from
    int(1e18),  # value
    int(1e5),  # gas limit
    int(1e9),  # gas price
    1,  # nonce
)
signature = '0x{}{}{}'.format(
    hex(tx.v)[2:],
    hex(tx.r)[2:],
    hex(tx.s)[2:],
)

Sign an Existing Transaction Object

Sign a Transaction object from pyethereum(or similar RLP serializable):

from ledgereth import sign_transaction
tx = sign_transaction(tx)
signature = '0x{}{}{}'.format(
    hex(tx.v)[2:],
    hex(tx.r)[2:],
    hex(tx.s)[2:],
)

TODO

  • Add fake dongle support to pytest suite so tests can be run without a real Ledger and human interaction
  • Fill out tests
  • Add messaging signing support
  • Add type 1 transactions (access list)
  • Add EIP-1559 support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ledgereth-0.2.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

ledgereth-0.2.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file ledgereth-0.2.0.tar.gz.

File metadata

  • Download URL: ledgereth-0.2.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for ledgereth-0.2.0.tar.gz
Algorithm Hash digest
SHA256 64bc2d3884bac14036a109df6dfae260ff5d7af1ab581db4538a552cd08afd1c
MD5 9d453d4ca7088e0e3d82353b74232588
BLAKE2b-256 3f42a9ff575c9ef4dea8f86205d649e8e5bded1e73c028479f9cf3e4c2a874da

See more details on using hashes here.

File details

Details for the file ledgereth-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ledgereth-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for ledgereth-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 725c377180087b4907bdda4ac820b75929d5d77149605742bd73419ad24b2612
MD5 15fa695ebdb5e6760ae9f30f2163da97
BLAKE2b-256 753c7ed67e0e6a71953d56a31402b16206dfdd9e5533cfb2ba16ec098155b5a9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page