-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.js
188 lines (157 loc) · 5.07 KB
/
sign.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import '@metamask/legacy-web3';
const { web3 } = window
function parseSignature(signature) {
var r = signature.substring(0, 64);
var s = signature.substring(64, 128);
var v = signature.substring(128, 130);
return {
r: "0x" + r,
s: "0x" + s,
v: parseInt(v, 16)
}
}
function genSolidityVerifier(signature, signer, chainId) {
return solidityCode
.replace("<CHAINID>", chainId)
.replace("<SIGR>", signature.r)
.replace("<SIGS>", signature.s)
.replace("<SIGV>", signature.v)
.replace("<SIGNER>", signer);
}
window.onload = function (e) {
var res = document.getElementById("response");
res.style.display = "none";
// force the user to unlock their MetaMask
if (web3.eth.accounts[0] == null) {
alert("Please unlock MetaMask first");
// Trigger login request with MetaMask
web3.currentProvider.enable().catch(alert)
}
var signBtn = document.getElementById("signBtn");
signBtn.onclick = function(e) {
if (web3.eth.accounts[0] == null) {
return;
}
const domain = [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
{ name: "salt", type: "bytes32" },
];
const bid = [
{ name: "amount", type: "uint256" },
{ name: "bidder", type: "Identity" },
];
const identity = [
{ name: "userId", type: "uint256" },
{ name: "wallet", type: "address" },
];
const chainId = parseInt(web3.version.network, 10);
const domainData = {
name: "My amazing dApp",
version: "2",
chainId: chainId,
verifyingContract: "0x1C56346CD2A2Bf3202F771f50d3D14a367B48070",
salt: "0xf2d857f4a3edcb9b78b4d503bfe733db1e3f6cdc2b7971ee739626c97e86a558"
};
var message = {
amount: 100,
bidder: {
userId: 323,
wallet: "0x3333333333333333333333333333333333333333"
}
};
const data = JSON.stringify({
types: {
EIP712Domain: domain,
Bid: bid,
Identity: identity,
},
domain: domainData,
primaryType: "Bid",
message: message
});
const signer = web3.toChecksumAddress(web3.eth.accounts[0]);
web3.currentProvider.sendAsync(
{
method: "eth_signTypedData_v3",
params: [signer, data],
from: signer
},
function(err, result) {
if (err || result.error) {
return console.error(result);
}
const signature = parseSignature(result.result.substring(2));
res.style.display = "block";
res.value = genSolidityVerifier(signature, signer, chainId);
}
);
};
}
const solidityCode =
`
pragma experimental ABIEncoderV2;
pragma solidity ^0.5.0;
contract Verifier {
uint256 constant chainId = <CHAINID>;
address constant verifyingContract = 0x1C56346CD2A2Bf3202F771f50d3D14a367B48070;
bytes32 constant salt = 0xf2d857f4a3edcb9b78b4d503bfe733db1e3f6cdc2b7971ee739626c97e86a558;
string private constant EIP712_DOMAIN = "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)";
string private constant IDENTITY_TYPE = "Identity(uint256 userId,address wallet)";
string private constant BID_TYPE = "Bid(uint256 amount,Identity bidder)Identity(uint256 userId,address wallet)";
bytes32 private constant EIP712_DOMAIN_TYPEHASH = keccak256(abi.encodePacked(EIP712_DOMAIN));
bytes32 private constant IDENTITY_TYPEHASH = keccak256(abi.encodePacked(IDENTITY_TYPE));
bytes32 private constant BID_TYPEHASH = keccak256(abi.encodePacked(BID_TYPE));
bytes32 private constant DOMAIN_SEPARATOR = keccak256(abi.encode(
EIP712_DOMAIN_TYPEHASH,
keccak256("My amazing dApp"),
keccak256("2"),
chainId,
verifyingContract,
salt
));
struct Identity {
uint256 userId;
address wallet;
}
struct Bid {
uint256 amount;
Identity bidder;
}
function hashIdentity(Identity memory identity) private pure returns (bytes32) {
return keccak256(abi.encode(
IDENTITY_TYPEHASH,
identity.userId,
identity.wallet
));
}
function hashBid(Bid memory bid) private pure returns (bytes32){
return keccak256(abi.encodePacked(
"\\x19\\x01",
DOMAIN_SEPARATOR,
keccak256(abi.encode(
BID_TYPEHASH,
bid.amount,
hashIdentity(bid.bidder)
))
));
}
function verify() public pure returns (bool) {
Identity memory bidder = Identity({
userId: 323,
wallet: 0x3333333333333333333333333333333333333333
});
Bid memory bid = Bid({
amount: 100,
bidder: bidder
});
bytes32 sigR = <SIGR>;
bytes32 sigS = <SIGS>;
uint8 sigV = <SIGV>;
address signer = <SIGNER>;
return signer == ecrecover(hashBid(bid), sigV, sigR, sigS);
}
}
`.trim();