MetaMask 签名请求中的消息格式已关闭

Formatting of message in MetaMask Signature Request is off

我正在使用 MetaMask 签署随机数以进行服务器端身份验证。一切都按预期工作,但 MetaMask window 中显示的消息是神秘符号而不是实际消息。

这就是我触发 MetaMask 对消息 (n) 签名的方式:

console.log(n); //0x123456789...123456789
const sig = await web3.eth.personal.sign(n, address, "password!");

结果:

有人知道为什么会这样吗?

感谢您的帮助!

您必须像这样将“n”变量转换为十六进制:

let message = web3.utils.utf8ToHex(n);
const sig = await web3.eth.personal.sign(message, address, "password!");

Link 到文档 https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html#utils-utf8tohex