Error: This transaction is not signed
duync2006 opened this issue · comments
I tried to send signed transaction in web3js with raw LegacyTransaction. Below is my code
const { Web3 } = require('web3');
const contractAddress = require('../contracts/Tourism-address.json')
const contractABI = require('../contracts/Tourism.json')
const {LegacyTransaction} = require('@ethereumjs/tx')
const {Common, Hardfork} = require('@ethereumjs/common')
const {hexToBytes, bytesToHex} = require('@ethereumjs/util')
const provider= "my_private_provider"
var web3 = new Web3(provider);
const contract = new web3.eth.Contract(contractABI.abi, contractAddress.Token);
const customCommon = Common.custom({ chainId: 306 , networkId: 306}, {hardfork: Hardfork.Berlin})
async function sendToken(privateKey, firstName, lastName, phoneNumber) {
// get account address
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey).address
// send to account Vibi
// account using for sending VBC to new people
// privateKey: 40cf31902207cedc9a262552fb975403ecbd907d3407140d389922189594a553
const sponsorPrivateKey = hexToBytes('0x' + '40cf31902207cedc9a262552fb975403ecbd907d3407140d389922189594a553')
const sponserAccount = web3.eth.accounts.privateKeyToAccount('0x' + '40cf31902207cedc9a262552fb975403ecbd907d3407140d389922189594a553').address
const sponserNonce = await web3.eth.getTransactionCount(sponserAccount,'latest')
const txObject = {
nonce: web3.utils.toHex(sponserNonce),
gasPrice: web3.utils.toHex(100000),
gasLimit: web3.utils.toHex(300000), // Raise the gas limit to a much higher amount
to: account,
value: '0xDE0B6B3A7640000',
data: '0x0',
}
const tx = LegacyTransaction.fromTxData(txObject,{ common: customCommon })
const signedTx = tx.sign(sponsorPrivateKey)
const serializedTx = tx.serialize()
const raw = '0x' + serializedTx.toString('hex')
const txHash = web3.utils.sha3(serializedTx);
console.log("txHash:", bytesToHex(signedTx.hash()))
console.log("web3 txHash", txHash)
const pTx = await web3.eth.sendSignedTransaction(
Buffer.from(serializedTx).toString("hex"),
function (error, receipt) {
if (!error) {
console.log("receipt ==> ", receipt);
} else {
console.log("error ==> ", error);
}
}
);
}
sendToken('5ab1aeb4c79730ff9d8e31a42244892799df085e412bee158efadfcb9b69279b', 'David', 'John', '0918812313')
I found the problem that the console log in txHash is different with the web3 txHash
and it error at the web3 sendsignedtransaction
Error: This transaction is not signed (tx type=0 hash=not available (unsigned) nonce=660 value=1000000000000000000 signed=false hf=merge gasPrice=100000)
The version that i used is:
"@ethereumjs/common": "^4.3.0",
"@ethereumjs/tx": "^5.3.0",
"@ethereumjs/util": "^9.0.3",
"web3": "^4.6.0"
Yes, this is because you are using:
const serializedTx = tx.serialize()
Which you then try to send:
const pTx = await web3.eth.sendSignedTransaction(
Buffer.from(serializedTx).toString("hex"),
If you change this:
const serializedTx = tx.serialize()
to this:
const serializedTx = signedTx.serialize()
It works :)
Ahh :), I got it, i fixed that and it run okay. Thank you for your time