eror on terminal if (!cond) throw new Error(msg) ^

Error: Expected private key to be an Uint8Array with length 32

and it's my code

const Web3 = require('web3')
const Tx = require('ethereumjs-tx').Transaction
const web3 = new Web3('')

const account1 = '0x97F70e1a18B6237603e9382920C93853992ab174'
const account2 = '0xA838152E5C21f8d3Df51f696431AEb1D3C2Ff2F7'

const privateKey1 = Buffer.from(process.env.PRIVATE_KEY_1,'hex')
const privateKey2 = Buffer.from(process.env.PRIVATE_KEY_2,'hex')

const contractAddress = '0xd9145CCE52D386f254917e481eB44e9943F39138'
const contractABI = // had to remove to shorten code
var contract = new web3.eth.Contract(contractABI, contractAddress);
const data = contract.methods.transfer(account2,20000).encodeABI();

web3.eth.getTransactionCount(account1, (err, txCount) => {

    //create the transaction object
    const txObject = {
        nonce: web3.utils.toHex(txCount), 
        gasLimit:web3.utils.toHex(210000) ,
        gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
        to: contractAddress,  
        data: data
    //sign the transaction
    const tx = new Tx(txObject)

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

    //broadcast the transaction
    web3.eth.sendSignedTransaction(raw, (err , txHash) =>{
        console.log('err:', err , 'txHash:', txHash);
        //use this txHash to find contract on Etherscan!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.