-
Notifications
You must be signed in to change notification settings - Fork 24
/
delegate_by_signature.html
192 lines (167 loc) · 6.86 KB
/
delegate_by_signature.html
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
189
190
191
192
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../dependencies/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Delegate By Signature</title>
</head>
<body>
<h1>Delegate By Signature</h1>
<h2>Write Signature</h2>
<div id="web3-warning" class="hidden warning">
Make sure the example app is being served with an HTTP server. <br />
Please install MetaMask: <a href="https://metamask.io/">https://metamask.io/</a>
</div>
<div class="card">
<label>My Address: </label> <span id="my-address"></span>
<br />
<br />
<label>Current Delegatee: </label> <span id="my-delegatee"></span>
<br />
<br />
<label>Delegate To: </label> <input id="delegate-to" type="text" value="0x1234567890000000000000000000000123456789" />
<br />
<input id="sign" type="submit" value="Create Delegation Signature" />
</div>
<h2>Post Transaction</h2>
<div class="card">
<label>Delegatee: </label> <input id="delegatee" type="text" />
<br />
<label>Nonce: </label> <input id="nonce" type="text" />
<br />
<label>Expiry: </label> <input id="expiry" type="text" />
<br />
<label>Signature: </label> <input id="signature" type="text" />
<br />
<input id="delegate" type="submit" value="Delegate" />
</div>
</body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.4.0/dist/web3.min.js"></script>
<script type="text/javascript" src="../dependencies/comp-abi.js"></script>
<script type="text/javascript">
const createDelegateBySigMessage = (compAddress, delegatee, expiry = 10e9, chainId = 1, nonce = 0) => {
const types = {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Delegation: [
{ name: 'delegatee', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'expiry', type: 'uint256' }
]
};
const primaryType = 'Delegation';
const domain = { name: 'Compound', chainId, verifyingContract: compAddress };
const message = { delegatee, nonce, expiry };
return JSON.stringify({ types, primaryType, domain, message });
};
window.addEventListener('load', (event) => {
let web3;
const web3Warning = document.getElementById('web3-warning');
const myAddress = document.getElementById('my-address');
const myDelegatee = document.getElementById('my-delegatee');
const delegateTo = document.getElementById('delegate-to');
const sign = document.getElementById('sign');
const signature = document.getElementById('signature');
const delegate = document.getElementById('delegate');
const delegatee = document.getElementById('delegatee');
const nonce = document.getElementById('nonce');
const expiry = document.getElementById('expiry');
const chainId = document.getElementById('chainId');
const urlParamDelegate = new URLSearchParams(window.location.search).get('delegatee');
if (urlParamDelegate) {
delegateTo.value = urlParamDelegate;
}
if (typeof window.ethereum === 'undefined') {
console.error('Client does not have an active Web3 provider or the example app is not being run from an HTTP server.');
console.log('Go here to install MetaMask: https://metamask.io/');
alert(
'You need a Web3 provider to run this page. ' +
'Go here to install MetaMask:\n\n' +
'https://metamask.io/'
);
web3Warning.classList.remove('hidden');
} else {
main();
}
async function main() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); // enable ethereum
web3 = new Web3(window.ethereum);
const net = +window.ethereum.chainId;
// This app only works with Ropsten or Main
if (net !== 1 && net !== 3) {
alert('Please select the Main or Ropsten network.');
}
if (net === 1) {
compAddress = '0xc00e94cb662c3520282e6f5717214004a7f26888';
} else if (net === 3) {
compAddress = '0xf76d4a441e4ba86a923ce32b89aff89dbccaa075';
}
const compAbi = window.compAbi;
const comp = new web3.eth.Contract(compAbi, compAddress);
const myAccount = accounts[0];
let currentDelegatee = await comp.methods.delegates(myAccount).call();
myAddress.innerText = myAccount;
myDelegatee.innerText = currentDelegatee;
sign.onclick = async () => {
const _delegatee = delegateTo.value;
const _nonce = await comp.methods.nonces(myAccount).call();
const _expiry = 10e9; // expiration of signature, in seconds since unix epoch
const _chainId = window.ethereum.chainId.toString();
const msgParams = createDelegateBySigMessage(compAddress, _delegatee, _expiry, _chainId, _nonce);
window.ethereum.sendAsync({
method: 'eth_signTypedData_v4',
params: [ myAccount, msgParams ],
from: myAccount
}, async (err, result) => {
if (err) {
console.error('ERROR', err);
alert(err);
return;
} else if (result.error) {
console.error('ERROR', result.error.message);
alert(result.error.message);
return;
}
const sig = result.result;
delegatee.value = _delegatee;
nonce.value = _nonce;
expiry.value = _expiry;
signature.value = sig;
console.log('signature', sig);
console.log('msgParams', JSON.parse(msgParams));
});
};
delegate.onclick = async () => {
const _delegatee = delegatee.value;
const _nonce = nonce.value;
const _expiry = expiry.value;
const sig = signature.value;
const r = '0x' + sig.substring(2).substring(0, 64);
const s = '0x' + sig.substring(2).substring(64, 128);
const v = '0x' + sig.substring(2).substring(128, 130);
currentDelegatee = await comp.methods.delegates(myAccount).call();
myDelegatee.innerText = currentDelegatee;
console.log(`Now delegated to: ${currentDelegatee}`);
console.log('delegatee', _delegatee);
console.log('nonce', _nonce);
console.log('expiry', _expiry);
console.log('v', v);
console.log('r', r);
console.log('s', s);
const tx = await comp.methods.delegateBySig(_delegatee, _nonce, _expiry, v, r, s).send({
from: myAccount,
gasLimit: web3.utils.toHex(1000000),
gasPrice: web3.utils.toHex(25000000000),
});
console.log('delegateBySig Transaction', tx);
currentDelegatee = await comp.methods.delegates(myAccount).call();
myDelegatee.innerText = currentDelegatee;
console.log(`Now delegated to: ${currentDelegatee}`);
};
}
});
</script>
</html>