MetaMask / MetaMask/metamask-extension
Seems to have a wrong signature with Personal sign.
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
Hi, I want a user be able to sign a transaction and then send it to a private ethereum network.
I have a problem recovering my signer public key using "web3.personal_sign".
```js
const web3 = new Web3(window.web3.currentProvider); // Metamask provider
const transaction = Contract.methods.putData('any', 'value');
const from = '0x627306090abab3a6e1400e9345bc60c78a8bef57';
const [nonce, chainId, gasLimit] = await Promise.all([
web3.eth.getTransactionCount(from),
web3.eth.net.getId(),
transaction.estimateGas(),
]);
const rawTx = {
nonce: web3.utils.toHex(nonce),
gasLimit: web3.utils.toHex(gasLimit * 2),
gasPrice: web3.utils.toHex(1),
from,
to: '0x765...ef57',
value: web3.utils.toHex(0),
data: transaction.encodeABI(),
chainId, // 4445
};
const tx = new Tx(rawTx);
tx.v = rawTx.chainId;
const unsignedTX = tx.serialize().toString('hex');
const rsv = await new Promise((resolve, reject) => this.web3.eth.personal.sign(unsignedTX, rawTx.from, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
}));
const recovered = sigUtil.recoverPersonalSignature({ data: unsignedTX, sig: rsv });
console.log(recovered.toLowerCase() === rawTx.from.toLowerCase()); // no problem here !
const [r, s, v] = rsv.slice(2).match(/.{1,64}/g);
const rawTxWithRSV = {
...rawTx,
r: `0x${r}`,
s: `0x${s}`,
v: ((parseFloat(rawTx.chainId) * 2) + 8 + v),
};
const signedTx = new Tx(rawTxWithRSV);
signedTx.v = (parseFloat(rawTx.chainId) * 2) + 8 + v;
const txSender = this.web3.eth.accounts.recoverTransaction(`0x${signedTx.serialize().toString('hex')}`);
console.log(txSender === rawTx.from); // There is the issue !
```
I can't find the proper way to sign my transaction without sending.
If any one can help ..
thanks.
Contributor guide
Research direction
Start by reviewing the web3.eth.personal.sign and web3.eth.accounts.recoverTransaction calls alongside the Tx and sigUtil usage shown in the report. Reproduce the signing and recovery flow and determine whether the issue is an incorrect signature interpretation or a MetaMask behavior; done means the expected transaction sender is recovered or the limitation is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- blockchain
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 18/100