0

eror on terminal

 if (!cond) throw new Error(msg)
             ^

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

my Code

its about signing eror. I am absolutely lost as to why this is not working as I have copied every piece of code directly from the guide and now its not working.

const Web3 = require('web3')
const Tx = require('ethereumjs-tx').Transaction
const web3 = new Web3('http://127.0.0.1:8545')

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 be a 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)
tx.sign(privateKey1)

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!
})
})

thats it

TylerH
  • 20,799
  • 66
  • 75
  • 101
rayan
  • 11
  • 1

1 Answers1

0

Did you make sure to set the PRIVATE_KEY_1 and PRIVATE_KEY_2 variables referenced in the example code? That seems to me to be the most obvious possible problem.

Pandapip1
  • 730
  • 5
  • 15