I don't know how to create a transaction message for the method eth_sign (to, msg). I know that through eth_sign you can send a request for a transaction signature (in metamask for example), which will be passed in the msg parameter, which will be encoded through rlp. I don't understand how to implement all this.
1 Answers
There is a difference between a message and a transaction.
eth_sign
is used for signing arbitrary messages (either text encoded to binary, serialized objects, or just any binary message), while eth_signTransaction
and eth_sendTransaction
is used for signing transaction objects.
MetaMask allows signing messages and returns the signature that can be used in your web app. But when it signs a transaction, it automatically broadcasts it and doesn't allow returning just the signed transaction.
If you want to sign a simple message, you can use the eth_sign
method. Note that there are also newer methods such as personal_sign
or signTypedData_v4
.
let accounts = await ethereum.request({ method: 'eth_requestAccounts' });
// keccak256 hash of string "hello"
let messageHash = "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8";
let signature = await ethereum.request({ method: 'eth_sign', params: [accounts[0], messageHash] });
console.log(signature);
If you want MetaMask to sign and broadcast a transaction, you need to pass its object to the eth_sendTransaction
method.
let accounts = await ethereum.request({ method: 'eth_requestAccounts' });
let txHash = await ethereum.request({
method: 'eth_sendTransaction',
params: [{
from: accounts[0],
to: "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF",
value: "1" // 1 wei
}],
});
console.log(txHash);

- 40,554
- 8
- 72
- 100
-
I know for sure that it is possible to sign a transaction with eth_sign, which will be encoded through rlp and passed in the msg parameter. You can check this for example in the metamask documentation: https://docs.metamask.io/guide/signing-data.html#a-brief-history (In particular, the method eth_sign...). – Galaxy773 Aug 03 '22 at 12:39