参数 TypedData
-
domain object
-
message object
-
primaryType string
-
types string
返回值
- Signature string
- hex encoded bytes
Request
Copy
const signTypedData = async () => {
const address = "0xYourEthereumAddress";
const msgParams = {
domain: {
chainId: 97, // Binance Smart Chain 测试网
name: "Ether Mail",
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
version: "1", // 版本应为字符串
},
message: {
from: {
name: 'Alice',
wallet: "0x58F401144bbd45031b9937Ec93A8b2aac8bB8972" // 确保地址为小写
},
to: {
name: 'Bob',
wallet: '0x58F401144bbd45031b9937Ec93A8b2aac8bB8972' // 确保地址为小写
},
contents: 'Hello, Bob!'
},
primaryType: "Mail",
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' }
]
},
};
try {
const signature = await window.ethereum.request({
method: "eth_signTypedData_v4",
params: [address, JSON.stringify(msgParams)],
});
console.log("签名结果:", signature);
} catch (error) {
console.log("签名失败:", error);
}
};