如何通过JSON-RPC向以太坊链发送签名交易数据?

时间:2024-07-11 13:59:24

   概述

在以太坊开发当中,通过web3.js、ethers.js等提供的API方法,都可以完成与以太坊的转账交易。那么如何通过以太坊JSON-RPC与以太坊进行交易呢?

在以太坊的JSON-RPC当中,有eth_sendRawTransaction这个方法,可以向以太坊网络提交预签名的交易广播。

curl https://mainnet.infura.io/v3/YOUR-API-KEY \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_sendRawTransaction",
"params": ["0xf869018203e882520894f17f52151ebef6c7334fad080c5704d77216b73
2881bc16d674ec80000801ba02da1c48b670996dcb1f447ef9ef00b33033c48a4fe938f4
20bec3e56bfd24071a062e0aa78a81bf0290afbc3a9d8e9a068e6d74caa66c5e0fa8a46deaae96b0833"],
"id":1}'

其中-d就是发送的交易参数, jsonrpc是:2.0,method是交易的方法:eth_sendRawTransaction,id是1,params是私钥签名后得到的数据。

响应的返回数据是:

{
  "id": 1,
  "jsonrpc": "2.0",