#python #ethereum #web3 #web3py
#python #ethereum #web3py
Вопрос:
Я пытался закодировать простой web3.py программа для отправки транзакции из моего Trezor. Я могу подписать транзакцию на моем Trezor, и функция, которая это делает (ethereum.sign_tx()), возвращает кортеж подписей V, R и S транзакции, который выглядит как:
(42, b"", b')
Мой вопрос в том, как я могу преобразовать эти подписи в сериализованную форму, которую я могу отправить с помощью функции 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", Web3.toWei(1, 'ether'), chain_id=3)
#sent = ropsten.eth.sendRawTransaction(tx)
if __name__ == "__main__":
main()
Ответ №1:
вы можете делать то, что 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))