/
eip-5267.ts
51 lines (43 loc) · 1.43 KB
/
eip-5267.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { ethers } from "ethers";
import type { BigNumber } from "ethers";
const iface = new ethers.utils.Interface([
`function eip712Domain() view returns (
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extension
)`,
`function DOMAIN_SEPARATOR() view returns (bytes32)`,
`function name() view returns (string)`,
]);
export async function getEIP712Domain(address: string, provider: ethers.providers.Provider) {
const contract = new ethers.Contract(address, iface, provider);
const domainDescriptor: Parameters<typeof buildDomain> = await contract.eip712Domain();
return buildDomain(...domainDescriptor);
}
const fieldNames = ['name', 'version', 'chainId', 'verifyingContract', 'salt'] as const;
/** Builds a domain object based on the values obtained by calling `eip712Domain()` in a contract. */
function buildDomain(
fieldsString: string,
name: string,
version: string,
chainId: BigNumber,
verifyingContract: string,
salt: string,
extensions: unknown[],
) {
if (extensions.length > 0) {
throw Error("extensions not implemented");
}
const fields = Number(fieldsString);
const domain = { name, version, chainId, verifyingContract, salt };
for (const [i, field] of fieldNames.entries()) {
if (!(fields & (1 << i))) {
delete domain[field];
}
}
return domain;
}