-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignERC2612Permit.js
More file actions
52 lines (49 loc) · 1.86 KB
/
Copy pathsignERC2612Permit.js
File metadata and controls
52 lines (49 loc) · 1.86 KB
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
51
52
/**
* @file signERC2612Permit
*/
const MAX_INT = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
async function signERC2612Permit(web3, token, owner, spender, value, deadline, nonce) {
const message = {
owner,
spender,
value,
nonce: nonce || await web3.contract('pair', token).methods.nonces(owner).call(),
deadline: deadline || MAX_INT
}
const typedData = {
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" },
],
},
primaryType: "Permit",
domain: {
name: await web3.contract('erc20', token).methods.name().call(),
version: '1',
chainId: 1,
verifyingContract: token
},
message: message
};
return new Promise((resolutionFunc, rejectionFunc) => {
web3.currentProvider.sendAsync({ method: "eth_signTypedData_v4", params: [owner, JSON.stringify(typedData)], from: owner }, function (error, result) {
if (!error) {
const signature = result.result.substring(2);
const r = "0x" + signature.substring(0, 64);
const s = "0x" + signature.substring(64, 128);
const v = parseInt(signature.substring(128, 130), 16);
resolutionFunc({ r, s, v, deadline: message.deadline });
}
});
});
};