本文环境:
区块链:以太坊POA联盟链;
出块节点数:3;
操作系统:windows 64;
节点版本:Geth1.9.14;
node版本:v10.14.0
参考文档:https://web3js.readthedocs.io/en/v1.2.8/
本文描述在测试eth.sendSignedTransaction过程中碰到的问题。
在官方文档中,使用示例代码如下:
var Tx = require('ethereumjs-tx').Transaction;
var privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex');
var rawTx = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}
var tx = new Tx(rawTx, {'chain':'ropsten'});
tx.sign(privateKey);
var serializedTx = tx.serialize();
// console.log(serializedTx.toString('hex'));
// 0xf889808609184e72a00082271094000000000000000000000000000000000000000080a47f74657374320000000000000000000000000000000000000000000000000000006000571ca08a8bbf888cfa37bbf0bb965423625641fc956967b81d12e23709cead01446075a01ce999b56a8a88504be365442ea61239198e23d1fce7d00fcfc5cd3b44b7215f
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);
> // see eth.getTransactionReceipt() for details
一、安装ethereumjs-tx
使用如下命令安装:
C:\Users\administrator> npm install -g ethereumjs-tx
安装正常。
二、启动节点测试并解决问题
正常启动本地节点,获取账号私钥正常,获取账号nonce正常;
1、测试过程中一直报错:
(node:10440) UnhandledPromiseRejectionWarning: Error: Returned error: invalid sender"
这个问题卡了好久,本文使用的POA私链,一直找不到原因,网上好多也都是提问;
有些人建议在rawTx增加chainId: '517',本文尝试后,没有变化,还是将该条去掉;
将ethereumjs-tx版本回退到1.3.7,安装方式:
C:\Users\administrator> npm install -g [email protected]
安装正常。此时需要修改一行代码:
var Tx = require('ethereumjs-tx').Transaction;
//因为版本降低,改为
var Tx = require('ethereumjs-tx');
重新运行代码,错误解决。
2、此时出现新的报错:
(node:10520) UnhandledPromiseRejectionWarning: Error: Returned error: intrinsic gas too low
适当增加 gasPrice和gasLimit的值后,错误解决。
3、代码
本文在原官方文档的基础上做了最小修改并跑通的代码:
var Web3= require('web3');
var web3= new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var Tx = require('ethereumjs-tx');
var privateKey = Buffer.from('a6fe45540000bd3fed9a4794166577b6e62eb674004edc22a6554630d8515654', 'hex');
var rawTx = {
nonce: '0x15',
gasPrice: '0x09184e72a000000',
gasLimit: '0x27100',
to: '0x0297a8913cc187d60712f095a7e1bf2421bfcd40',
value: '0x100',
data: '0x010203040506070809'
}
var tx = new Tx(rawTx,{'chain':'ropsten'});
tx.sign(privateKey);
var serializedTx = tx.serialize();
console.log(serializedTx.toString('hex'));
// f87213879184e72b00000083027200940297a8913cc187d60712f095a7e1bf2421bfcd40820100890102030405060708091ca0e2bafcccd3215da81fa21075d52f80ce26abf82f81566bfc6e69f3b75cfc6461a050738216f3088587caa14bb7578923b21bd969a5ccd7b2a77bcc7f4581e0ffe3
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', console.log);
直接在命令行下node运行,结果如下:
{ blockHash:
'0x86048b8c11f9d63eb9eaaa2df01df561d6ba29245bd8ba9b273a09473f174458',
blockNumber: 223581,
contractAddress: null,
cumulativeGasUsed: 21144,
from: '0x38d8b866a1abebca20afc004622f5355eefeb568',
gasUsed: 21144,
logs: [],
logsBloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: true,
to: '0x0297a8913cc187d60712f095a7e1bf2421bfcd40',
transactionHash:
'0xe467cc585d2aefd9479b0ba54228e13f4150651294fe8dc597907f5b2697fe4d',
transactionIndex: 0 }
在这个过程一直遇到invalid sender
,后来才发现是ethereumjs-tx
的版本问题,记录一下,以防大家踩坑。