与Ledger Nano S/X和Speculos通信的库

ledgercomm的Python项目详细描述


LedgerCOMM公司

概述

通过HID或TCP套接字发送和接收APDU的Python库。 它可以与Ledger Nano S/X或Speculos模拟器一起使用。在

安装

如果您只想通过TCP套接字进行通信,则没有依赖关系

$ pip install ledgercomm

否则,hidapi必须像这样作为额外的依赖项安装

^{pr2}$

入门

fromledgercommimportTransport# Nano S/X using HID interfacetransport=Transport(interface="hid",debug=True)# or Speculos through TCP sockettransport=Transport(interface="tcp",server="127.0.0.1",port=9999,debug=True)## send/recv APDUs## send method for structured APDUstransport.send(cla=0xe0,ins=0x03,p1=0,p2=0,cdata=b"")# send b"\xe0\x03\x00\x00\x00"# or send_raw method for hexadecimal stringtransport.send_raw("E003000000")# send b"\xe0\x03\x00\x00\x00"# or with bytes typetransport.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 APDUssw,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 stringsw,reponse=transport.exchange_raw("E003000000")# send b"\xe0\x03\x00\x00\x00"# or with bytes typesw,response=transport.exchange_raw(b"\xe0\x03\x00\x00\x00")

CLI

用法

安装后,ledgercomm提供名为ledgercomm-send的CLI工具

$ 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)

示例

如果Speculos是使用默认参数启动的,或者Nano S/X使用正确的udev规则插入,那么可以从stdin发送apdu

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

或者,您可以使用以下名为apdus.txt的文本文件在某些条件下重放apdu

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

那么

$ ledgercomm-send --startswith "=>" file apdus.txt

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将字符串转换为自定义对象   java如何从socket方法获取数据?   Java中的soap读取回车和新行   java在单击时替换图像   java推荐的使用RXJava执行异步任务的方法   java MySql连接器JDBC驱动程序不支持连接池吗?   java将活动堆栈清理到顶部   java计数用户输入的数量   java从webservice下载大文件导致应用程序性能问题   JavaLocalDate。EPOCH不可用   java如何在使用Selenium等待一定时间后,在页面无法加载(get(url))时自动刷新页面   java Calendar setLenient方法不允许检查年份字段的健全性   java Eclipse和intelliJ 安卓 SDK问题   java为什么我可以在没有super关键字的情况下调用父方法?   java iText的PDF格式不好