Skip to main content

LedgerCOMM

Overview

Python library to send and receive APDU through HID or TCP socket. It could be used with a Ledger Nano S/X or with the Speculos emulator.

Install

If you just want to communicate through TCP socket, there is no dependency

$ pip install ledgercomm

otherwise, hidapi must be installed as an extra dependency

$ pip install ledgercomm[hid]

Getting started

Library

from ledgercomm import Transport

# Nano S/X using HID interface
transport = Transport(interface="hid", debug=True)
# or Speculos through TCP socket
transport = Transport(interface="tcp", server="127.0.0.1", port=9999, debug=True)

#
# send/recv APDUs
#

# send method for structured APDUs
transport.send(cla=0xe0, ins=0x03, p1=0, p2=0, payload=b"")  # send b"\xe0\x03\x00\x00\x00"
# or send_raw method for hexadecimal string
transport.send_raw("E003000000")  # send b"\xe0\x03\x00\x00\x00"
# or with bytes
transport.send_raw(b"\xe0\x03\x00\x00\x00")

# Waiting for a response (blocking IO)
sw, response = transport.recv()  # type: int, bytes

#
# exchange APDUs (one time send/recv)
#

# exchange method for structured APDUs
sw, response = transport.exchange(cla=0xe0, ins=0x03, p1=0, p2=0, payload=b"")  # send b"\xe0\x03\x00\x00\x00"
# or exchange_raw method for hexadecimal string
sw, reponse = transport.exchange_raw("E003000000")  # send b"\xe0\x03\x00\x00\x00"
# or with bytes
sw, response = transport.exchange_raw(b"\xe0\x03\x00\x00\x00")

CLI

Usage

When installed, ledgercomm provides a CLI tools named ledgercomm-parser

$ ledgercomm-parser --help
usage: ledgercomm-parser [-h] [--hid] [--server SERVER] [--port PORT] [--condition CONDITION]
                         {file,stdin,log} ...

positional arguments:
  {file,stdin,log}      sub-command help
    file                to read APDUs from file
    stdin               to read APDUs from stdin
    log                 to read APDUs from Ledger Live logs

optional arguments:
  -h, --help            show this help message and exit
  --hid                 Use HID instead of TCP client
  --server SERVER       IP server of the TCP client (default: 127.0.0.1)
  --port PORT           Port of the TCP client (default: 9999)
  --condition CONDITION
                        Only send APDUs starting with 'condition' (default: None)

Example

If Speculos is launched with default parameters or your Nano S/X is plugged with correct udev rules, you can send APDUs from stdin

$ echo "E003000000" | ledgercomm-parser stdin  # Speculos
$ echo "E003000000" | ledgercomm-parser --hid stdin  # Nano S/X

Or you can replay APDUs using the following text file named apdus.txt with some condition

# this line won't be send if you've the right 'condition'
=> E003000000
# another APDU to send
=> E004000000

then

$ ledgercomm-parser --condition "=>" file apdus.txt

Download files

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

Source Distribution

ledgercomm-1.0.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ledgercomm-1.0.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file ledgercomm-1.0.1.tar.gz.

File metadata

  • Download URL: ledgercomm-1.0.1.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for ledgercomm-1.0.1.tar.gz
Algorithm Hash digest
SHA256 54d6b1ec5f630013019a7a5c38ed5b94065e10350ab23fb13569733697c783a5
MD5 b297c63ca8fc69286ff9bd6925e9a239
BLAKE2b-256 31b3c2487799f4b6b186eba0baa8a2cad1e68def5debb6fe69cb63752735f4e6

See more details on using hashes here.

File details

Details for the file ledgercomm-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: ledgercomm-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for ledgercomm-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f0974bc05db87c43b4e30ec17a41d7315f8fede3dae57eb29b013ffcd0eae5a5
MD5 1db4a4e51e2aa492ccfcf2dd2f871ead
BLAKE2b-256 12e547f3e3ea1c9e9516b06ddbb479b8372c34ba964c2560e74c70a929857531

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page