-
Notifications
You must be signed in to change notification settings - Fork 6
/
EIP712_test.ts
50 lines (42 loc) · 1.37 KB
/
EIP712_test.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 {TypedDataDomain} from 'ethers'
import {writeFileSync} from 'fs'
const domain: TypedDataDomain = {
name: 'mock name',
version: '1',
chainId: 1024,
verifyingContract: '0xfc1a36B3eF056c2eC89FdeB4251e90F7935F1b51'
}
const types = {
'NameCard': [
{'name': 'name', 'type': 'string'},
{'name': 'salary', 'type': 'uint256'},
{'name': 'personalAddress', 'type': 'address'},
]
}
// typed data
const value = {
name: 'Michael.W',
salary: 1024,
personalAddress: '0x0000000000000000000000000000000000000400',
}
async function main() {
const domainSeparator = ethers.TypedDataEncoder.hashDomain(domain)
const wallet = new ethers.Wallet(ethers.toBeHex(1024, 32))
const signature = await wallet.signTypedData(domain, types, value)
// generate domain separator with chain id changed
domain.chainId = 2048
const domainSeparatorWithChainIdChanged = ethers.TypedDataEncoder.hashDomain(domain)
const output = {
domain_separator: domainSeparator,
domain_separator_with_chain_id_changed: domainSeparatorWithChainIdChanged,
value,
signature,
signer_address: wallet.address
}
writeFileSync('test/utils/cryptography/data/EIP712_test.json', JSON.stringify(output))
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})