从Trezor硬件钱包发送已签名的交易

问题描述

我一直在尝试编写一个简单的web3.py程序,以从Trezor发送交易。我可以在我的Trezor上签署交易,执行该功能函数(ethereum.sign_tx())返回交易的V,R和S签名的元组,如下所示:

(42,b"\xa1\xd8blablablax883",b'<\x7f\xd0Eblablabla6\xe7\xe2Fc')

我的问题是如何将这些签名转换为可以使用Web3.eth.sendRawTransaction()函数发送的序列化形式。完整的代码是:

from trezorlib.client import get_default_client
from trezorlib.tools import parse_path
from trezorlib import ethereum
from web3 import Web3


def main():
    # Use first connected device
    client = get_default_client()
    ropsten = Web3(Web3.HTTPProvider("https://ropsten.infura.io/v3/7xxxxxxxxx23dee70e4aa"))


    # Get the first address of first BIP44 account
    # (should be the same address as shown in wallet.trezor.io)
    bip32_path = parse_path("44'/60'/0'/0/0")
    address = ethereum.get_address(client,bip32_path)
    nonce = ropsten.eth.getTransactionCount(address)
    tx = ethereum.sign_tx(client,bip32_path,nonce,Web3.toWei(1,'gwei'),21000,"0x7ccc4a67eB76b5B1C8Efc62672A6884A9B7bfdb7",'ether'),chain_id=3)
    #sent = ropsten.eth.sendRawTransaction(tx)


if __name__ == "__main__":
    main()

解决方法

您可以执行trezorctl的操作并使用rlp.encode

import rlp

gas_price = Web3.toWei(1,'gwei')
gas_limit = 21000
to_addr = "0x7ccc4a67eB76b5B1C8Efc62672A6884A9B7bFDb7"
amount = Web3.toWei(1,'ether')
data = b""
rlp_prefix = (nonce,gas_price,gas_limit,to_addr,amount,data)

sent = ropsten.eth.sendRawTransaction(rlp.encode(rlp_prefix + tx))

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...