Migrate EIP712 to ethersjs (#4750)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Renan Souza
2023-11-23 04:40:12 +00:00
committed by GitHub
parent 9702b67ce1
commit 6a56b3b08d
9 changed files with 145 additions and 123 deletions

View File

@ -1,20 +1,5 @@
const { ethers } = require('ethers');
const EIP712Domain = [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
{ name: 'salt', type: 'bytes32' },
];
const Permit = [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' },
];
const types = require('./eip712-types');
async function getDomain(contract) {
const { fields, name, version, chainId, verifyingContract, salt, extensions } = await contract.eip712Domain();
@ -32,7 +17,7 @@ async function getDomain(contract) {
salt,
};
for (const [i, { name }] of EIP712Domain.entries()) {
for (const [i, { name }] of types.EIP712Domain.entries()) {
if (!(fields & (1 << i))) {
delete domain[name];
}
@ -42,7 +27,7 @@ async function getDomain(contract) {
}
function domainType(domain) {
return EIP712Domain.filter(({ name }) => domain[name] !== undefined);
return types.EIP712Domain.filter(({ name }) => domain[name] !== undefined);
}
function hashTypedData(domain, structHash) {
@ -53,7 +38,7 @@ function hashTypedData(domain, structHash) {
}
module.exports = {
Permit,
types,
getDomain,
domainType,
domainSeparator: ethers.TypedDataEncoder.hashDomain,