我正在尝试编写一个保存私钥并签署交易的服务器。我使用 ethereumjs-wallet/hdkey 生成帐户和私钥,使用 ethereumjs-tx 签署交易,使用 Httprovider 发送交易的 web3js。
不幸的是,当我尝试发送交易时,我总是收到错误消息“超过块气体限制”(即使我将我的 gasLimit 设置为 21000,远低于我的 ganache-cli 实例的块气体限制)。
我怀疑原始编码交易的格式错误。
任何想法实际问题是什么以及如何解决它?
干杯
const hdkey = require('ethereumjs-wallet/hdkey');
const Transaction = require('ethereumjs-tx');
const walletHdpath = "m/44'/60'/0'/0/";
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(process.env.KEYSTORE_SEED));
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
async function generateAccount() {
const wallet = hdwallet.derivePath(walletHdpath + nextAccountIndex).getWallet();
nextAccountIndex += 1;
const addr = '0x' + wallet.getAddress().toString('hex');
accounts[addr] = wallet;
await fundAccount(addr);
return addr;
}
async function fundAccount(address) {
const txParams = {
gasPrice: '20000000000',
gasLimit: '21000',
from: process.env.KEYSTORE_ADDRESS_0,
to: address,
value: web3.utils.toWei('0.1', 'ether'),
data: ''
}
const signed = signTransaction(txParams);
// this line throws exception: "exceeds block gas limit"
await web3.eth.sendSignedTransaction(signed.signed_transaction);
}
function signTransaction(txParams) {
const from = txParams.from.toLowerCase();
const wallet = accounts[from];
if (wallet === undefined) {
return {sucess: false, message: "unknown from account" }
}
const tx = new Transaction(txParams);
const pkey = wallet.getPrivateKey();
tx.sign(pkey);
const rawTx = '0x' + tx.serialize().toString('hex');
return { success: true, signed_transaction: rawTx }
}