function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
)
先按照 EIP712 拼接数据,可以参考这个代码:
export function signPremit(tokenName, verifyAddr, owner, spender, value, deadline, chainid, nonce) {
let types = {
EIP712Domain: [{
name: 'name',
type: 'string'
},
{
name: 'version',
type: 'string'
},
{
name: 'chainId',
type: 'uint256'
},
{
name: 'verifyingContract',
type: 'address'
},
],
Permit: [{
name: 'owner',
type: 'address'
},
{
name: 'spender',
type: 'address'
},
{
name: 'value',
type: 'uint256'
},
{
name: 'nonce',
type: 'uint256'
},
{
name: 'deadline',
type: 'uint256'
}
]
};
let primaryType = 'Permit';
let domain = {
name: tokenName,
version: '1',
chainId: chainid,
verifyingContract: verifyAddr
};
let message = {
owner,
spender,
value,
nonce,
deadline
};
return JSON.stringify({
types,
primaryType,
domain,
message
});
}
然后把拼接数据转给 metamask 的 sign-typed-data-v4 进行签名,可参考文档:
https://docs.metamask.io/guide/signing-data.html#sign-typed-data-v4