-
Notifications
You must be signed in to change notification settings - Fork 6
/
Api.ts
111 lines (97 loc) · 3.35 KB
/
Api.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Fastify from 'fastify';
import { BigNumber } from 'ethers';
import { App } from './App.js';
import { toChecksummedAddress, hashOfPubKey, hashOfPrivateKey } from './Utils.js';
import logger from './services/Logger.js';
export async function initApi(app: App, port: number): Promise<() => void> {
const EC = (await import('elliptic')).default.ec;
const elipticCurve = new EC('secp256k1');
const fastify = Fastify({
logger: false,
});
function prettyReply(reply, response) {
reply.type('application/json').send(
JSON.stringify(
response,
(key, value) => {
if (typeof value === 'bigint') {
return value.toString();
} else if (typeof value === 'object' && value instanceof BigNumber) {
return value.toString();
} else {
return value;
}
},
2,
),
);
}
fastify.get('/api/v1', (request, reply) => {
const response = {
config: app.getConfig(),
networks: app.getNetworkList(),
};
prettyReply(reply, response);
});
function getAgentWithWorkerAddress(workerAddress) {
const networkList = app.getNetworkList();
for (const n of networkList) {
const network = app.getNetwork(n);
const agents = network.getAgents();
for (const a of agents) {
if (a.getWorkerSignerAddress().toLowerCase() === workerAddress.toLowerCase()) {
return a;
}
}
}
return null;
}
fastify.get('/api/v1/public-key-hash/:address', (request, reply) => {
const agent = getAgentWithWorkerAddress(request.params['address']);
const wallet = agent.getWorkerSigner();
reply.code(200).send({ hash: hashOfPubKey(wallet, elipticCurve) });
});
fastify.get('/api/v1/private-key-hash/:address', (request, reply) => {
const agent = getAgentWithWorkerAddress(request.params['address']);
const wallet = agent.getWorkerSigner();
reply.code(200).send({ hash: hashOfPrivateKey(wallet) });
});
fastify.get('/api/v1/networks/:networkName', (request, reply) => {
const network = app.getNetwork(request.params['networkName']);
if (!network) {
reply.code(404).send({ error: 'Network not found' });
}
prettyReply(reply, network.getStatusObjectForApi());
});
fastify.get('/api/v1/networks/:networkName/:agentAddress', (request, reply) => {
const agentAddress = request.params['agentAddress'];
const networkName = request.params['networkName'];
const checkSummedAgentAddress = toChecksummedAddress(agentAddress);
if (agentAddress !== checkSummedAgentAddress) {
reply.redirect(`/api/v1/networks/${networkName}/${checkSummedAgentAddress}`);
}
const network = app.getNetwork(networkName);
if (!network) {
reply.code(404).send({ error: 'Network not found' });
}
const agent = network.getAgent(checkSummedAgentAddress);
if (!agent) {
reply.code(404).send({ error: 'Agent not found' });
}
prettyReply(reply, agent.getStatusObjectForApi());
});
fastify.listen(
{
host: process.env.API_HOST || '0.0.0.0',
port: parseInt(process.env.API_PORT) || port || 8099,
},
(err, address) => {
logger.info(`API Server: Listening on ${address}`);
if (err) throw err;
// Server is now listening on ${address}
},
);
return async function stop() {
return fastify.close();
};
}