1
var Tx= require('ethereumjs-tx').Transaction
const Web3=require('web3')
const web3= new Web3('HTTP://127.0.0.1:7545') 
const account1='0x06e61BDdC2a45A7523C62c0DcC4E15D842BBa9BA'
const account2='0x886ffD053C1eC72C10fA6FE1c916cb5A23313A76'
const privateKey_acc1=Buffer.from('b1292b419cb954157ba5a9f3385fc57a6ff31cdf80afe96d13af7335b5c55436')
// const privateKey_acc2=Buffer.from('d7adcf5175859f7244eeff812d456600bc938ff0ad8f7fa4fdfde946481f968f')
console.log(privateKey_acc1);
web3.eth.getBalance(account1,(err,res)=>{
    console.log(web3.utils.fromWei(res));
})
web3.eth.getTransactionCount(account1,(err,txCount)=>{
    // build the transction
    const obej={
        nonce:web3.utils.toHex(txCount),
        to:account1,
        value:web3.utils.toHex(web3.utils.toWei("1")),
        gassLimit:web3.utils.toHex(2100),
        gasPrice:web3.utils.toHex(2100)
    }
    // sign 
    const tx= new Tx(obej) 
    // tx.sign(privateKey_acc1)
    const serialtrans=tx.serialize()
    const raw='0x'+serialtrans.toString('hex')
    // console.log(raw);
    // broadcast transaction
    web3.eth.sendSignedTransaction(raw,(err,txhash)=>{
        // console.log('hash',txhash);
    })
}) 
if (!cond) throw new Error(msg)
             ^

Error: Expected private key to be an Uint8Array with length 32
    at assert (/Users/yaksheshgupta/node_modules/secp256k1/lib/index.js:18:20)
    at isUint8Array (/Users/yaksheshgupta/node_modules/secp256k1/lib/index.js:31:7)
    at Object.ecdsaSign (/Users/yaksheshgupta/node_modules/secp256k1/lib/index.js:254:7)
    at Object.exports.sign (/Users/yaksheshgupta/node_modules/ethereumjs-tx/node_modules/ethereumjs-util/dist/secp256k1v3-adapter.js:247:25)
    at Object.exports.ecsign (/Users/yaksheshgupta/node_modules/ethereumjs-tx/node_modules/ethereumjs-util/dist/signature.js:12:25)
    at Transaction.sign (/Users/yaksheshgupta/node_modules/ethereumjs-tx/dist/transaction.js:230:37)
    at Object.callback (/Users/yaksheshgupta/Desktop/JavaScript/web3_app.js:24:8)
    at sendTxCallback (/Users/yaksheshgupta/node_modules/web3-core-method/lib/index.js:541:29)
    at /Users/yaksheshgupta/node_modules/web3-core-requestmanager/lib/index.js:305:9
    at XMLHttpRequest.request.onreadystatechange (/Users/yaksheshgupta/node_modules/web3-providers-http/lib/index.js:98:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/yaksheshgupta/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/Users/yaksheshgupta/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/Users/yaksheshgupta/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/Users/yaksheshgupta/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at IncomingMessage.emit (node:events:402:35)
    at endReadableNT (node:internal/streams/readable:1343:12)

I have Tried many things but it stays the same

1
  • Have u tried to add 0x at the start of your private key ?
    – Majd TL
    May 31, 2022 at 19:26

1 Answer 1

2

the default encoding in Buffer.from is

enter image description here

try this:

const privateKey = Buffer.from('b1292b419cb954157ba5a9f3385fc57a6ff31cdf80afe96d13af7335b5c55436', 'hex')

as you can see I'm sending 'hex' to the encoding parameter

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.