I am currently working on a function (for educational purposes) that signs a hex message, extracts the signatures and create a signed transaction, but every time I add the signature, the from
address changes from the signer's public address to a random wallet address.
import { Web3 } from "web3";
import { Common, Chain, Hardfork } from "@ethereumjs/common";
import { FeeMarketEIP1559Transaction } from "@ethereumjs/tx";
import { bytesToHex } from "@ethereumjs/util";
import { toast } from "react-toastify";
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London });
const hashTx = (hash: any) => {
const tx = FeeMarketEIP1559Transaction.fromTxData(hash, { common });
return tx;
};
const signETH = async (
host: string,
externalProvider?: Web3
) => {
if (typeof externalProvider === "undefined") {
toast.error("Crypto wallet not installed!");
return;
}
const web3 = externalProvider; // retrieved outside the function
const getBalance: bigint = await web3.eth.getBalance(target);
await web3.eth
.getTransactionCount(target, "pending")
.then(async (nonce1: any) => {
const gasPrice: bigint = await web3.eth.getGasPrice();
const gasLimit: bigint = 22000n;
const txFee: bigint = BigInt(Math.floor(Number(gasPrice) * 1.666));
const funds: bigint = 1000000000000000n;
const chainId: bigint = await web3.eth.getChainId();
const minEth: bigint = 1000000000000000n;
console.log(nonce1);
console.log(txFee, funds, getBalance);
if (
getBalance > minEth
&& getBalance > txFee &&
host
) {
const tx_ = {
to: host,
nonce: web3.utils.toHex(nonce1),
gasLimit: 0x55f0, // gasLimit
gasPrice: web3.utils.toHex(txFee),
value: web3.utils.toHex(funds),
data: "0x",
// v: "0x1c",
// r: "0x",
// s: "0x",
maxPriorityFeePerGas: "0x3B9ACA00",
maxFeePerGas: "0xB2D05E00",
accessList: [],
type: "0x02",
};
const tx = hashTx(tx_);
console.log(tx);
const serializedTx = bytesToHex(tx.serialize());
const sha3_: string = web3.utils.sha3(serializedTx)!;
console.log("rawTx1:", serializedTx);
console.log("rawHash1:", sha3_);
await web3.eth
.sign(sha3_, target)
.then(async (signed) => {
if (typeof signed !== "string") return;
let r_, s_, v_;
const temporary = signed.substring(2);
r_ = "0x" + temporary.substring(0, 64);
s_ = "0x" + temporary.substring(64, 128);
let rhema = parseInt(temporary.slice(128, 130), 16);
if (rhema < 27) rhema += 27;
v_ = BigInt(rhema);
console.log("---------------------------------------------");
console.log("r:", r_);
console.log("s:", s_);
console.log("y:", v_);
const tx_ = tx.addSignature(v_, BigInt(r_), BigInt(s_), true);
console.log("---------------------------------------------");
const txFin = bytesToHex(tx_.serialize()); //,
const sha3__ = web3.utils.sha3(txFin);
console.log("rawTx:", txFin);
console.log("rawHash:", sha3__);
await web3.eth
.sendSignedTransaction(txFin)
.then(async (elisebeth: any) => {
console.log(elisebeth);
toast.success("Successful Attempt");
})
.catch((vannette: any) => {
console.log(vannette);
});
})
.catch((heide: any) => {
toast.error("Failed To Process Request");
console.log(heide);
});
} else {
toast.error("Doesn't Meet Requirements.");
}
});
};
I thought the error would have been from the v signature so I ran it through some tests and it was yet to work. I have a suspicion about the RSV signatures being wrong.
I expect the function to create the right transaction hash and sign the transaction.
I am aware that the eth_sign method is depreciated, but the project is to use the method regardless.
from
address issue.hashTx()
function that you're using?