首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@ethereumjs/tx - "TypeError:公用不是构造函数“

@ethereumjs/tx - "TypeError:公用不是构造函数“
EN

Ethereum用户
提问于 2021-09-13 10:08:02
回答 1查看 628关注 0票数 0

我试图实现一种使用@ethereumjs/tx包签署eip-1559事务的方法。在执行他们的文档中提到的步骤时,我在尝试初始化@ethereumjs/common包的类Common时遇到了一个错误。

方法:

代码语言:javascript
复制
const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx');
const Common = require('@ethereumjs/common');
const { Chain, Hardfork } = require('@ethereumjs/common');

async function signTx(to, from, value, gasLimit, nonce) {
    const privateKey = 'private_key'

    const rawTx = { to, from, value, gasLimit, data: '0x00', nonce }

    const pkey = Buffer.from(privateKey, 'hex'); 

    const common =  new Common({ chain: Chain.Ropsten, hardfork: Hardfork.London });

    const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })

    tx.sign(pkey);

    const signedTx = `0x${tx.serialize().toString('hex')}`;

    return signedTx;
}

错误:UnhandledPromiseRejectionWarning: TypeError: Common is not a constructor

包版本:

  • @ethereumjs/common2.4.0
  • @ethereumjs/tx3.3.0

如果能在这方面提供任何帮助,我们将不胜感激。

EN

回答 1

Ethereum用户

发布于 2021-09-14 07:22:16

这个问题已经解决了。

@ethereumjs/common应该导入为default,如下所示。

const Common = require('@ethereumjs/common').default;

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

https://ethereum.stackexchange.com/questions/109837

复制
相关文章

相似问题

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