Se esperaba que la clave privada fuera un Uint8Array con una longitud de 32

Estoy tratando de enviar una transacción pero arroja un error. Todavía no estoy enviando ninguna transacción solo firmándola ...

13) en XMLHttpRequestEventTarget.dispatchEvent (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22) en XMLHttpRequest._setReadyState (C:\web3\node_modules\xhr2- cookies\dist\xml-http-request.js:208:14) en XMLHttpRequest._onHttpResponseEnd (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14) en IncomingMessage. (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61) en IncomingMessage.emit (events.js:327:22) en endReadableNT (internal/streams/readable.js: 1327:12)

var Tx = require('ethereumjs-tx')

const Web3 = require('web3')
const web3 = new Web3('http://127.0.0.1:7545')

const account1 = '0x67d30ef950015Ab1a03e30ED5d5F2A26de196C4d'
const account2 = '0x04bCD71C67656cFda695F14a9E9Bf8B6064b8AD1'

const privateKey1 = 'c429601ee7a6167356f15baa70fd8fe17b0325dab7047a658a31039e5384bffd'
const privateKey2 = '60495127495614d5aadb1f4561a3989d6636cbe55ada58c685ef28cff01bde21'

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

console.log('Buffer 1: ', privateKey1Buffer)
console.log('Buffer 2: ', privateKey2Buffer)

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(privateKey1)

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

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

Respuestas (2)

Esta parte estaba mal.

tx.sign(privateKey1)

Arréglalo así:

tx.sign(privateKey1Buffer)

Tuve este error porque no había eliminado 0xel prefijo de la clave privada.