Expected private key to be an Uint8Array with length 32 1


This error occurs while sending a signedTransaction.

Solution: 

Step 1: Remove 0x from your private key. It's not required in Infura Ropsten. You may keep it while testing with Ganache.

It should look like this :

 

const privateKey1 = 'b829601ee7a6167356f15baa70fd8fe17b0325dab7047a658a31039e5384beea'
const privateKey2 = '9a495127495614d5aadb1f4561a3989d6636cbe55ada58c685ef28cff01bd8be'

Step 2: 

Add the following codes:

 

const privateKey1Buffer = Buffer.from(privateKey1, 'hex')
const privateKey2Buffer = Buffer.from(privateKey2, 'hex')

Your complete code structure should look like this:

 

var Tx = require('ethereumjs-tx').Transaction

const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/v3/90363e8d106c428aa485b8c6ee5e62ea')

const account1 = '0x90d30ef950015Ab1a03e30ED5d5F2A26de1968ed'
const account2 = '0x23bCD71C67656cFda695F14a9E9Bf8B6064b8e41'

const privateKey1 = 'b829601ee7a6167356f15baa70fd8fe17b0325dab7047a658a31039e5384beea'
const privateKey2 = '9a495127495614d5aadb1f4561a3989d6636cbe55ada58c685ef28cff01bd8be'

const privateKey1Buffer = Buffer.from(privateKey1, 'hex')
const privateKey2Buffer = Buffer.from(privateKey2, 'hex')

web3.eth.getTransactionCount(account1, (err, txCount) => {
    const txObject = {
      nonce:    web3.utils.toHex(txCount),
      to:       account2,
      value:    web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
      gasLimit: web3.utils.toHex(21000),
      gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei'))
    }

    const tx = new Tx.Transaction(txObject)
    tx.sign(privateKey1Buffer,{'chain':'ropsten'})

    const serializedTx = tx.serialize().toString('hex')
    // const raw = '0x' + serializedTx.toString('hex')

    console.log('tx :', tx)
    console.log('serializedTx :', serializedTx)
    console.log('raw :', raw)
  })

Hope it worked!