-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.js
67 lines (58 loc) · 2.05 KB
/
providers.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
const { ethers } = require('ethers');
const { SiweMessage } = require('siwe');
const loginButton = document.querySelector('#login');
loginButton.addEventListener('click', async () => {
/**
* Get the provider and signer from the browser window
*/
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
/**
* Get the active account
*/
const [address] = await provider.listAccounts();
console.log(`Address used to construct SIWE message: ${address}`);
/**
* Gets a randomly generated nonce from the SIWE library. This nonce is added
* to the session so we can check it on sign in. For security purposes, this
* is generated on the server
*/
const nonce = await fetch('/api/nonce').then(res => res.text());
console.log(`Nonce returned from server stored on client: ${nonce}`);
/**
* Get the chain id
*/
const chainId = (await provider.getNetwork()).chainId;
console.debug(chainId);
/**
* Creates the message object
*/
const message = new SiweMessage({
domain: document.location.host,
address,
chainId,
uri: document.location.origin,
version: '1',
statement: 'Metamask SIWE Example',
nonce,
});
console.log(`SIWE message constructed in the client:`)
console.debug(message);
/**
* Generates the message to be signed and uses the provider to ask for a signature
*/
const signature = await signer.signMessage(message.prepareMessage());
console.log(`Signed message signature: ${signature}`);
/**
* Calls the sign_in endpoint to validate the message. On success, the
* console woill display the message returned from server
*/
await fetch('/api/sign_in', {
method: 'POST',
body: JSON.stringify({message, signature}),
headers: { 'Content-Type': 'application/json' }
}).then(async (res) => {
const message = await res.text();
console.log(JSON.parse(message).message);
})
})