-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (53 loc) · 1.7 KB
/
app.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
const express = require('express');
const Web3 = require('web3');
const net = require('net');
const Accounts = require('web3-eth-accounts');
const cors = require('cors');
const app = express();
const port = (process.env.port || 8081);
const ipcLocator = (process.env.nodeAddr || '/root/.silesiacoin/ssc.ipc');
app.get('/', (req, res) => res.json(res.json(indexResponse())));
app.get('/accounts/:address', async(req, res) => res.json(await checkBalance(req)));
app.options('/accounts', cors());
app.put('/accounts', async(req, res) => res.json(await createAccount()));
app.use(cors());
app.listen(port, () => console.log(`Authenticator listening on port ${port}!`));
const web3 = new Web3(new Web3.providers.IpcProvider(ipcLocator, net));
const indexResponse = () => {
return {
routes: app._router.stack
.filter(possibleRoute => possibleRoute.route)
.map(possibleRoute => {
return {
route: possibleRoute.route.path,
methods: possibleRoute.route.methods
}
})
}
};
const createAccount = async() => {
const accounts = new Accounts();
const { address, privateKey } = accounts.create();
return {
address,
privateKey
}
};
const checkBalance = async(req) => {
const { address } = req.params;
try {
const balance = await web3.eth.getBalance(address);
const sscConverted = web3.utils.fromWei(balance);
return {
"accountNumber": address,
"balance": {
"wei": balance,
"ssc": sscConverted
}
}
} catch (error) {
return {
"error": error.message
}
}
};