This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
71 lines (60 loc) · 1.62 KB
/
server.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
const express = require('express')
const jwt = require('express-jwt')
const cors = require('cors')
const ethers = require('ethers')
const app = express()
app.use(cors())
app.use(express.static(__dirname + '/public'))
app.use(jwt({
// NOTE: we're using signature based authentication so this secret doesn't matter.
secret: 'somesecret',
requestProperty: 'auth',
credentialsRequired: false
}))
const challenge = 'sign this string'
app.get('/challenge', (req, res) => {
res.json({ challenge })
})
app.get('/verify', async (req, res) => {
const { address, signature } = req.auth
const data = '0x' + Buffer.from(challenge).toString('hex')
const verified = await verifySignature(address, data, signature)
res.json({ verified })
})
const port = process.env.PORT || 8000
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
async function verifySignature (account, data, signature) {
const eip1271Abi = [
{
constant: true,
inputs: [
{
name: '_messageHash',
type: 'bytes'
},
{
name: '_signature',
type: 'bytes'
}
],
name: 'isValidSignature',
outputs: [
{
name: 'magicValue',
type: 'bytes4'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
]
const magicValue = '0x20c13b0b'
const provider = ethers.getDefaultProvider('kovan')
const instance = new ethers.Contract(account, eip1271Abi, provider)
const result = await instance.isValidSignature(data, signature)
const verified = (result === magicValue)
return verified
}