Der erwartete private Schlüssel ist ein Uint8Array mit der Länge 32

Ich versuche, eine Transaktion zu senden, aber es wird ein Fehler ausgegeben. Trotzdem sende ich keine Transaktion, sondern unterschreibe sie nur ...

13) bei XMLHttpRequestEventTarget.dispatchEvent (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22) bei XMLHttpRequest._setReadyState (C:\web3\node_modules\xhr2- cookies\dist\xml-http-request.js:208:14) unter XMLHttpRequest._onHttpResponseEnd (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14) unter IncomingMessage. (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61) bei IncomingMessage.emit (events.js:327:22) bei 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)
  })

Antworten (2)

Dieser Teil war falsch.

tx.sign(privateKey1)

Repariere es so:

tx.sign(privateKey1Buffer)

Ich hatte diesen Fehler, weil ich 0xdas Präfix des privaten Schlüssels nicht entfernt hatte.