forked from rarible/protocol-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEIP712.js
73 lines (69 loc) · 1.63 KB
/
EIP712.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const DOMAIN_TYPE = [
{
type: "string",
name: "name"
},
{
type: "string",
name: "version"
},
{
type: "uint256",
name: "chainId"
},
{
type: "address",
name: "verifyingContract"
}
];
module.exports = {
createTypeData: function (domainData, primaryType, message, types) {
return {
types: Object.assign({
EIP712Domain: DOMAIN_TYPE,
}, types),
domain: domainData,
primaryType: primaryType,
message: message
};
},
signTypedData: function (web3, from, data) {
return new Promise(async (resolve, reject) => {
function cb(err, result) {
if (err) {
return reject(err);
}
if (result.error) {
return reject(result.error);
}
const sig = result.result;
const sig0 = sig.substring(2);
const r = "0x" + sig0.substring(0, 64);
const s = "0x" + sig0.substring(64, 128);
const v = parseInt(sig0.substring(128, 130), 16);
resolve({
data,
sig,
v, r, s
});
}
if (web3.currentProvider.isMetaMask) {
web3.currentProvider.sendAsync({
jsonrpc: "2.0",
method: "eth_signTypedData_v3",
params: [from, JSON.stringify(data)],
id: new Date().getTime()
}, cb);
} else {
let send = web3.currentProvider.sendAsync;
if (!send) send = web3.currentProvider.send;
send.bind(web3.currentProvider)({
jsonrpc: "2.0",
method: "eth_signTypedData",
params: [from, data],
id: new Date().getTime()
}, cb);
}
});
}
};