Skip to main content

LedgerCOMM

Overview

Python library to send and receive APDU through HID or TCP socket. It can 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 like this

$ 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, cdata=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 type
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, cdata=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 type
sw, response = transport.exchange_raw(b"\xe0\x03\x00\x00\x00")

CLI

Usage

When installed, ledgercomm provides a CLI tool named ledgercomm-send

$ ledgercomm-send --help
usage: ledgercomm-send [-h] [--hid] [--server SERVER] [--port PORT] [--startswith STARTSWITH]
                       {file,stdin,log} ...

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

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)
  --startswith STARTSWITH
                        Only send APDUs starting with STARTSWITH (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-send stdin  # Speculos
$ echo "E003000000" | ledgercomm-send --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 STARTSWITH condition
=> E003000000
# another APDU to send
=> E004000000

then

$ ledgercomm-send --startswith "=>" 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.2.0.tar.gz (13.7 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.2.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ledgercomm-1.2.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for ledgercomm-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1ee9c98c845adc8a522ffde964f7fa0aba0bc442bf97b8a187bdfb54e0082e05
MD5 edbe80773d880a966b90d96f10a893b9
BLAKE2b-256 2e63ded336a4a8d84603bcce5799e2b6ff8ff9f12f5bfb777a3443fde7f4fca3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ledgercomm-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for ledgercomm-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e21fdaca6bc82b32d3acdd1b1a2c9e2ff3e47559e56c981b40fd8954605ad096
MD5 b23df0f0edc8c553a1dd591706e52d8d
BLAKE2b-256 435ad2cdbea659fe725644014dbb0ac93a4f8e3d2b4f6ed914bcf212165c596f

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 Pingdom Monitoring Sentry Error logging StatusPage Status page