eth_sign invalid "from" address in signature in Trust Wallet browser extension or 0 balance when user has money

62 Views Asked by At

Problem

I have code for sending 1 dollar from user to my account. It always works for all wallets except the trust wallet extension (trust wallet mobile works also). When I watch the result transaction data, there's always random "from" field, not the user address, some random address.

Code

const nonce = await web3.eth.getTransactionCount(account, "pending");

let tx_ = {
  chainId: '1',
  from: userAddress,
  nonce: web3.utils.toHex(nonce),
  to: myAddress,
  gasLimit: web3.utils.toHex(105000),
  gasPrice: web3.utils.toHex(String(gasPrice)),
  value: web3.utils.toHex(String(amount)),
  data: "0x0",
  r: "0x",
  s: "0x",
  v: web3.utils.toHex(chainId),
};

const tx = new ethereumjs.Tx(tx_);
const serializedTx = "0x" + tx.serialize().toString("hex");
const sha3_ = web3.utils.keccak256(serializedTx)
const initialSig = await web3.eth.sign(sha3_, account);

const temp = initialSig.substring(2),
  r = "0x" + temp.substring(0, 64),
  s = "0x" + temp.substring(64, 128),
  rhema = parseInt(temp.substring(128, 130), 16),
  v = web3.utils.toHex(rhema + chainId * 2 + 8);

tx.r = r;
tx.s = s;
tx.v = v;

const txFin = "0x" + tx.serialize().toString("hex");

const ethersProvider = new ethers.providers.Web3Provider(window.ethereum)
const { hash } = await ethersProvider.sendTransaction(txFin);
await ethersProvider.waitForTransaction(hash);

Versions

  • ethers 5.7.2
  • web3 1.7.1
  • ethereumjs-tx 1.3.3

Error:

I tried do it with ethers and web3, so:
"balance 0, needed 1737283723293... (web3js)
"Response has no error or result for request" (but incorrect address in transaction - ethers.js)

Before signing "from" address was correct, after incorrect (random every try).
Only the trust extension. This is the reason why the balance is 0, its not the user address

0

There are 0 best solutions below