使用 ethrereumjs-tx 签名并使用 HttpProvider 发送,无论 gasLimit 如何,都可以"Exceeds block gas limit"



我正在尝试编写一个保存私钥并签署交易的服务器。我使用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 }
}

问题是 txParams 中的值需要十六进制编码并以 0x 为前缀

最新更新