这是一种让我感到困惑的错误。打字稿似乎对这个字段的存在很生气,如果它被删除也会很生气。打字是怎么回事?
import { signTypedData, recoverTypedSignature, TypedMessage, SignTypedDataVersion } from "@metamask/eth-sig-util";
import Web3 from 'web3';
import { toChecksumAddress } from "web3-utils";
const web3 = new Web3((window as any).ethereum);
await (window as any).ethereum.enable();
const msgParams = {
domain: {
domain_typehash: 'f00',
name: 'MintSigner',
chainId: 31337,
verifyingContract: 'f00',
version: '0.0.1',
},
message: {
'MINT_TYPEHASH': 'f00',
'minter': "f00",
'token': "f00",
'to': 'f00',
'amount': 100e18,
'id': "7",
},
primaryType: 'Mint',
types: {
EIP712Domain: [
{ name: 'domain_typehash', type: 'bytes32' },
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Mint: [
{ name: 'MINT_TYPEHASH', type: 'bytes32'},
// bytes32 public constant MINT_TYPEHASH = keccak256("Mint(address minter,address token,address to,uint256 amount,uint256 id)");
{ name: 'minter', type: 'address' },
{ name: 'token', type: 'address' },
{ name: 'to', type: 'address' },
{ name: 'amount', type: 'uint256' },
{ name: 'id', type: 'uint256'},
]
},
}
var msgNoPrimaryType = {
domain: msgParams.domain,
message: msgParams.message,
types: msgParams.types,
}
var from = await web3.eth.getAccounts();
var params = [from[0], msgParams];
var method = 'eth_signTypedData_v4';
const signTypedDataV4VerifyResult = document.getElementById(
'signTypedDataV4VerifyResult',
);
try {
const signature = await window.ethereum.request({
method: 'eth_signTypedData_v4',
params: [from, JSON.stringify(msgParams)],
});
console.log(signature)
const recoveredAddr = recoverTypedSignature({
data: msgParams,
signature: signature,
version: SignTypedDataVersion.V4,
});
错误:
[{
"resource": "pages/sign.tsx",
"owner": "typescript",
"code": "2322",
"severity": 8,
"message": "Type '{ domain: { domain_typehash: string; name: string; chainId: number; verifyingContract: string; version: string; }; message: { MINT_TYPEHASH: string; minter: string; token: string; to: string; amount: number; id: string; }; primaryType: string; types: { ...; }; }' is not assignable to type 'TypedMessage<{ EIP712Domain: { name: string; type: string; }[]; Mint: { name: string; type: string; }[]; }>'.\n Types of property 'primaryType' are incompatible.\n Type 'string' is not assignable to type '\"Mint\" | \"EIP712Domain\"'.",
"source": "ts",
"relatedInformation": [
{
"message": "The expected type comes from property 'data' which is declared here on type '{ data: TypedMessage<{ EIP712Domain: { name: string; type: string; }[]; Mint: { name: string; type: string; }[]; }>; signature: string; version: SignTypedDataVersion.V4; }'",
"resource": "node_modules/@metamask/eth-sig-util/dist/sign-typed-data.d.ts"
}
]
}]
如果我从中删除 primaryType,我会得到这个:
[{
"resource": "/pages/sign.tsx",
"owner": "typescript",
"code": "2741",
"severity": 8,
"message": "Property 'primaryType' is missing in type '{ domain: { domain_typehash: string; name: string; chainId: number; verifyingContract: string; version: string; }; message: { MINT_TYPEHASH: string; minter: string; token: string; to: string; amount: number; id: string; }; types: { ...; }; }' but required in type 'TypedMessage<{ EIP712Domain: { name: string; type: string; }[]; Mint: { name: string; type: string; }[]; }>'.",
"source": "ts",
"relatedInformation": [
{
"message": "'primaryType' is declared here.",
"resource": "node_modules/@metamask/eth-sig-util/dist/sign-typed-data.d.ts"
},
{
"message": "The expected type comes from property 'data' which is declared here on type '{ data: TypedMessage<{ EIP712Domain: { name: string; type: string; }[]; Mint: { name: string; type: string; }[]; }>; signature: string; version: SignTypedDataVersion.V4; }'",
"resource": "node_modules/@metamask/eth-sig-util/dist/sign-typed-data.d.ts"
}
]
}]
输入的问题似乎与@metamask/eth-sig-util 中的 TypedMessage 类型定义有关。
在错误消息中,TypeScript 抱怨 msgParams 对象的 primaryType 属性无法分配给 TypedMessage 类型的 primaryType 属性。具体来说,它表示 msgParams 的 primaryType 属性是字符串类型,不能分配给联合类型“Mint” | “EIP712域”。这意味着 primaryType 必须是这两个字符串文字之一。
如果您从 msgParams 中删除 primaryType 属性,TypeScript 会抱怨该属性丢失,这是预期的,因为 TypedMessage 需要定义 primaryType 属性。
要解决此问题,您需要确保 msgParams 的 primaryType 属性是字符串文字“Mint”或“EIP712Domain”之一。您可以尝试将 primaryType 属性更改为“Mint”,如果 msgParams 对象确实是有效的 Mint 类型消息,这应该会起作用。或者,您可以尝试更改类型对象以定义一个新类型,其中包含具有正确字符串文字值的 primaryType 属性,并使用该类型定义 msgParams。