首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无论gasLimit如何,使用ethrereumjs-tx签名和使用HttpProvider发送会给出“超过区块气体限制”

无论gasLimit如何,使用ethrereumjs-tx签名和使用HttpProvider发送会给出“超过区块气体限制”
EN

Stack Overflow用户
提问于 2019-02-11 01:17:13
回答 1查看 179关注 0票数 0

我正在尝试编写一个保存私钥并对事务进行签名的服务器。我使用ethereumjs-wallet/hdkey生成帐户和私钥,使用ethereumjs-tx对事务进行签名,使用Httprovider使用web3js发送事务。

不幸的是,当我尝试发送事务时,我总是收到错误消息“超过块气体限制”(即使我将gasLimit设置为21000,远远低于我的ganache-cli实例的块气体限制)。

我怀疑原始编码事务的格式是错误的。

你知道真正的问题是什么以及我如何解决它吗?

干杯

代码语言:javascript
复制
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 }
}
EN

回答 1

Stack Overflow用户

发布于 2019-02-12 04:05:34

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54618932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档