我试图实现一种使用@ethereumjs/tx包签署eip-1559事务的方法。在执行他们的文档中提到的步骤时,我在尝试初始化@ethereumjs/common包的类Common时遇到了一个错误。
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/common:2.4.0@ethereumjs/tx:3.3.0如果能在这方面提供任何帮助,我们将不胜感激。
发布于 2021-09-14 07:22:16
这个问题已经解决了。
@ethereumjs/common应该导入为default,如下所示。
const Common = require('@ethereumjs/common').default;
https://ethereum.stackexchange.com/questions/109837
复制相似问题