Skip to main content
1 of 2

RangeError: private key length is invalid (In New ETH Transaction)

Followed this Ans Ans1 and Ans2 but no solution got.

const web3 = new Web3('https://ropsten.infura.io/v3/3c52917848e945229c0d33d632b10490');

const account1 = '0x0982081d5a2bba0a9da9fe722c92edc0cb0e9b6a'
const account2 = '0xabc46ddc17a0c37ef70e8a153d4724f199705e8b'

const ac1_pvtkey =Buffer.from('XXXX9F79B587B1DE553E2ADCA3DC8D49BE3E2840083D970C41D83F7FD471XXXX');

const ac2_pvtkey =Buffer.from('XXXXC15FBDB4634400B7F8FA17C9491B2E346AEC1BC86A493B18F5759042XXXX');

//build transaction here ---------------------------

 `web3.eth.getTransactionCount(account2, (err, txCount) => {
    const txObject = {
    nonce: web3.utils.toHex(txCount),
    to: account2,
    value: web3.utils.toHex(web3.utils.toWei('0.2','ether')),
    gaslimit: web3.utils.toHex(2100),
    gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
    }

console.log(txObject)

//signing transaction here ---------------------------

const tx = new Tx(txObject);
tx.sign(ac2_pvtkey); //using account2 private key

------------------------- ERROR GIVING ------------------------

var sig = secp256k1.sign(msgHash, privateKey);
                    ^
RangeError: private key length is invalid `