forked from purefiprotocol/signer-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (50 loc) · 1.24 KB
/
index.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
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import { Wallet } from 'ethers';
import * as dotenv from 'dotenv';
dotenv.config();
const PORT = process.env.PORT || 5000;
const SIGNER_PRIVATE_KEY = process.env.SIGNER_PRIVATE_KEY;
if (!SIGNER_PRIVATE_KEY) {
throw new Error(
'Create and set up .env file in the root folder using .env.sample as an example'
);
}
const signer = new Wallet(SIGNER_PRIVATE_KEY);
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.get('/', (req, res) =>
res.json({
success: true,
})
);
app.post('/sign', async (req, res) => {
const message = req?.body?.message;
if (!message) {
return res.status(400).json({
error: 'message required',
});
}
if (typeof message !== 'string') {
return res.status(400).json({
error: 'message must be a string',
});
}
try {
const signature = await signer.signMessage(message);
const response = {
message,
signature,
};
return res.status(200).json(response);
} catch (err) {
return res.status(400).json({
error: err.message,
});
}
});
app.listen(PORT, () => {
console.log(`Signer backend app is listening on port ${PORT}`);
});