与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
- 项目
标签: