3

i am trying to send a transaction but it throws an error. still i am not sending any transaction just signing it...

Error: Expected private key to be an Uint8Array with length 32 at assert (C:\web3\node_modules\secp256k1\lib\index.js:18:20) at isUint8Array (C:\web3\node_modules\secp256k1\lib\index.js:31:7) at Object.ecdsaSign (C:\web3\node_modules\secp256k1\lib\index.js:254:7) at Object.exports.sign (C:\web3\node_modules\ethereumjs-util\dist\secp256k1v3-adapter.js:247:25) at Object.exports.ecsign (C:\web3\node_modules\ethereumjs-util\dist\signature.js:12:25) at Transaction.sign (C:\web3\node_modules\ethereumjs-tx\dist\transaction.js:230:37) at Object.callback (C:\web3\transaction1.js:28:8) at sendTxCallback (C:\web3\node_modules\web3-core-method\lib\index.js:533:29) at C:\web3\node_modules\web3-core-requestmanager\lib\index.js:308:9 at XMLHttpRequest.request.onreadystatechange (C:\web3\node_modules\web3-providers-http\lib\index.js:98:13) at XMLHttpRequestEventTarget.dispatchEvent (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22) at XMLHttpRequest._setReadyState (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14) at XMLHttpRequest._onHttpResponseEnd (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14) at IncomingMessage. (C:\web3\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61) at IncomingMessage.emit (events.js:327:22) at 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)
  })

2 Answers 2

3

This part was wrong.

tx.sign(privateKey1)

Fix it like this:

tx.sign(privateKey1Buffer)

0
7

I had this error because I hadn't removed 0x from the prefix of the private key.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.