I am a beginner in Ethereum development. I am using Web3 js 1.0
to build a simple node js application that sends ether from one wallet to another on Mainnet.
I am having problem in creating buffer from private key, keep getting this error-
Error: Expected private key to be an Uint8Array with length 32.
This is my code:
//Private keys
const key1 = "0xb7bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//Generate Byte Buffer for key
const buffer1 = Buffer.from(key1, "hex");
const rawTx = {
nonce: web3.utils.toHex(count),
to: addTo,
value: web3.utils.toHex(bal - 10000000000 * 21000),
gasLimit: web3.utils.toHex(21000),
gasPrice: web3.utils.toHex(10000000000),
};
const txn = new Tx(rawTx);
txn.sign(buffer1);
const serialTx = txn.serialize();
const raw = "0x" + serialTx.toString("hex");
web3.eth.sendSignedTransaction(raw).on("transactionHash", (txHash) => {
console.log("transactionHash:", txHash);
});
I read somewhere that you have to remove the 0x
from the Private key before passing it to the Buffer method? Is it true?