-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.html
372 lines (272 loc) · 11.3 KB
/
sign.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<html>
<head>
<title>Signing data</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<!-- Boostrap stuff, has to come before JQuery is imported -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<!-- Bootstrap JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://unpkg.com/web3@latest/dist/web3.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/web3modal@1.9.4/dist/index.js"></script>
<script type="text/javascript" src="https://unpkg.com/@walletconnect/web3-provider@1.6.5/dist/umd/index.min.js"></script>
<script type="module">
import SafeAppsSDK from 'https://cdn.skypack.dev/@gnosis.pm/safe-apps-sdk';
import { SafeAppWeb3Modal } from 'https://cdn.skypack.dev/@gnosis.pm/safe-apps-web3modal';
const obscureAddress = (address) => {
return address.substring(0, 6) + '...'
+ address.substring(address.length - 4, address.length);
}
class WalletClass {
// Web3modal instance
web3Modal;
// Chosen wallet provider given by the dialog window
provider;
// Address of the selected account
selectedAccount;
// The button attaches a function to this variable
changeButton = () => {};
web3;
constructor () {
this.Web3Modal = SafeAppWeb3Modal;
this.WalletConnectProvider = window.WalletConnectProvider.default;
const providerOptions = {
walletconnect: {
package: this.WalletConnectProvider,
options: {
infuraId: "9aa3d95b3bc440fa88ea12eaa4456161",
}
}
};
this.web3Modal = new this.Web3Modal({
//network: "matic",
cacheProvider: true, // optional
providerOptions, // required
disableInjectedProvider: false, // optional. For MetaMask / Brave / Opera.
});
this.web3Modal.isSafeApp().then(async (isGnosis) => {
if(isGnosis) {
this.web3 = new Web3(await this.web3Modal.requestProvider());
} else if (window.ethereum) {
this.web3 = new Web3(window.ethereum);
}
if(this.web3){
this.web3.eth.getAccounts((accounta,accountb) => {
if(accountb[0]){this.onConnect();}
});
}
});
// check if web wallet is already connected.
if (window.ethereum) {
this.web3 = new Web3(window.ethereum);
this.web3.eth.getAccounts((accounta,accountb) => {
//if(accounta){onConnect();}
//else
if(accountb[0]){this.onConnect();} // <<-- this is what works for me on Firefox Metamask
});
}
}
connected = () => {
this.web3.eth.getAccounts().then((accounts) => {
console.log(accounts);
this.selectedAccount = accounts[0];
console.log(this.selectedAccount);
this.changeButton(obscureAddress(this.selectedAccount));
});
}
disconnected = () => {
this.changeButton('disconnected');
}
fetchAccountData = async () => {
// Get a Web3 instance for the wallet
this.web3 = new Web3(this.provider);
// Get connected chain id from Ethereum node
const chainId = await this.web3.eth.getChainId();
// Get list of accounts of the connected wallet
const accounts = await this.web3.eth.getAccounts();
console.log(accounts);
// MetaMask does not give you all accounts, only the selected account
this.selectedAccount = accounts[0];
if(!accounts[0]){await this.onDisconnect();return;} //if user disconnected wallet
this.connected();
}
updateButton = async () => {
if (this.selectedAccount) {
this.connected();
} else {
this.changeButton('disconnected');
}
}
onConnect = async () => {
try {
console.log('tries to connect');
if(await this.web3Modal.isSafeApp()) {
this.provider = await this.web3Modal.requestProvider();
} else {
this.provider = await this.web3Modal.connect();
}
console.log('finished trying');
} catch(e) {
console.log("Could not get a wallet connection", e);
return;
}
// Subscribe to accounts change
this.provider.on("accountsChanged", (accounts) => {
console.log('event: accountsChanged')
this.fetchAccountData();
});
// Subscribe to chainId change
this.provider.on("chainChanged", (chainId) => {
console.log('event: chainChanged')
this.fetchAccountData();
});
this.fetchAccountData();
//console.log(' this.updateButton();');
//await this.updateButton();
}
Disconnect = async () => {
if(this.provider.disconnect) {
await this.provider.disconnect();
await this.Disconnect();
} else {
alert ("The disconnection button is located in the wallet!");
}
}
onDisconnect = async () => {
// If the cached provider is not cleared,
// WalletConnect will default to the existing session
// and does not allow to re-scan the QR code with a new wallet.
// Depending on your use case you may want or want not his behavir.
await this.web3Modal.clearCachedProvider();
//provider = null; //if set to null, we get an error when reconnecting the wallet
this.selectedAccount = null;
this.changeButton('disconnected');
}
};
$('document').ready(async() => {
window.Wallet = new WalletClass();
window.btn_connect_handleClick = () => {
if (!Wallet.selectedAccount){
Wallet.onConnect();
} else {
alert ("The disconnection button is located in the wallet!");
}
}
Wallet.changeButton = (data) => {
if(data == 'disconnected'){
$('#btn-connect').text('Connect Wallet')
} else {
$('#btn-connect').text(obscureAddress(data))
}
}
window.btn_sign_handleClick = () => {
console.log('SignMessage')
Wallet.onConnect().then( () => {
if(!Wallet.selectedAccount){
alert('Please connect your wallet to continue!');
return;
}
console.log(Wallet.selectedAccount)
let message = document.querySelector("#input-message").value
let walletAddress = Wallet.selectedAccount
let password = '' //not necessary, only used on javascript wallet
Wallet.web3.eth.personal.sign(
message,
walletAddress,
password,
function (err, signature) {
if (err) {
alert('Signature Denied');
return;
}
if (signature) {
$('#signature-output').val(signature);
return;
}
}
);
});
}
window.btn_verify = () => {
console.log('test');
let message = $('#message-toverify').val()
let signature = $('#signature-input').val()
let walletInputed = $('#wallet-input').val()
console.log(message);
console.log(signature);
console.log(walletInputed);
try {
Wallet.web3.eth.personal.ecRecover(message, signature).then((address) => {
console.log('dddddddd'); //.toLowerCase();
console.log(address);
if(walletInputed.toLowerCase() == address.toLowerCase()) {
$('#verify-ok').show()
$('#verify-notok').hide()
} else {
$('#verify-notok').show()
$('#verify-ok').hide()
}
//verify-ok
//$('#doesmatch').text(address)
});
} catch {
$('#verify-notok').show()
$('#verify-ok').hide()
}
}
});
</script>
</head>
<body>
<div class="container" style="margin-top: 50px">
<h2>Sign Custom Message</h2>
<div class="row mb-3">
<div class="col-md-12 g-3">
<div class="row g-3 align-items-center">
<div class="col-auto"><button class="btn btn-primary" id="btn-clear" onclick="localStorage.clear()">Clear cache (local storage)</button></div>
<div class="col-auto"><button class="btn btn-primary" id="btn-connect" onclick="btn_connect_handleClick()">Connect Wallet</button></div>
<div class="col-auto "><button class="btn btn-primary" id="btn-sign" onclick="btn_sign_handleClick()">Sign Message</button></div>
</div>
</div>
</div>
<div class="row mb-3">
<textarea id="input-message" class="form-control" style="height: 204px" placeholder="Write a message you want to sign"></textarea>
</div>
<label for="signature-output" class="form-label">Message Signature</label>
<textarea class="form-control" type="text" name="signature-output" id="signature-output" readonly></textarea>
<h2 style="margin-top: 50px; margin-bottom: 20px">Verify Message Authenticity</h2>
<div class="row mb-3">
<textarea id="message-toverify" class="form-control" style="height: 204px" placeholder="Paste the message that needs verification"></textarea>
</div>
<div class="mb-3">
<label for="wallet-input" class="form-label">Author's Wallet Address</label>
<input class="form-control" type="text" name="wallet-input" id="wallet-input" maxlength="42" placeholder="Paste Author's Wallet Address">
</div>
<div class="mb-3">
<label for="signature-input" class="form-label">Message Signature</label>
<textarea class="form-control" type="text" name="signature-input" id="signature-input" placeholder="Paste Message Signature"></textarea>
</div>
<div class="row mb-3">
<div class="col-md-12 g-3">
<div class="row g-3 align-items-center">
<div class="col-auto">
<button class="btn btn-primary" id="btn-verify" onclick="btn_verify()">Verify!</button>
</div>
<div class="col-auto">
<div class="alert alert-success" role="alert" id="verify-ok" style="display:none">
Message and Signature are authentic for that address!
</div>
<div class="alert alert-danger" role="alert" id="verify-notok" style="display:none">
Data does not match with the signature!
</div>
</div>
</div>
</div>
</div>
</body>
</html>