-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
44 lines (36 loc) · 1.19 KB
/
main.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
// Libraries
const express = require('express')
const nacl = require('tweetnacl')
nacl.util = require('tweetnacl-util')
const toUint8Array = require('base64-to-uint8array')
// Consts
const app = express()
const port = 6060
// Router
app.get('/signature/:privateKey', (req, res) => {
try {
const signature = Sign(req.params.privateKey);
res.json({ "signature": signature });
} catch {
res.json({ "error": "Invalid or missing key" });
}
})
function decodePrivateKey(string) {
return toUint8Array(string)
}
function keyPairFromPrivateKey(privateKey) {
return nacl.sign.keyPair.fromSecretKey(privateKey)
}
function generateTimeSignature(keyPair, period) {
const now = Math.floor(Date.now() / 1000)
const counter = Math.floor(now / period)
const message = nacl.util.decodeUTF8(counter.toString())
const signature = nacl.sign.detached(message, keyPair.secretKey)
return nacl.util.encodeBase64(signature)
}
function Sign(privateKey) {
const keyPair = keyPairFromPrivateKey(decodePrivateKey(privateKey))
return generateTimeSignature(keyPair, 30)
}
// Main
app.listen(port, () => console.log(`Econic Developer Tools: listening on port ${port}!`))