0

I was trying to implement a method for signing an eip-1559 transaction using @ethereumjs/tx package. While following the steps mentioned in their documentation , I am encountering an error when trying to initialize the class Common of the @ethereumjs/common package.

Method :

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;
}

Error : UnhandledPromiseRejectionWarning: TypeError: Common is not a constructor

Package versions :

  • @ethereumjs/common : 2.4.0
  • @ethereumjs/tx: 3.3.0

Any help on this would be highly appreciated..

2 Answers 2

0

This issue has been resolved.

@ethereumjs/common should be imported as default like below.

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

2
  • This is for CommonJs, how to use the lib in ES?
    – Gutan
    Commented Oct 11, 2021 at 22:13
  • @Gutyn Can you please elaborate ? Commented Oct 27, 2021 at 20:07
0

If you are using import statement a workaround looks like this :

import {default as common} from '@ethereumjs/common';
const Common = common.default

const c = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.