forked from dilan-dio4/Keagate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
114 lines (99 loc) · 4.47 KB
/
index.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
112
113
114
import config from './config';
import fastify from 'fastify';
import { currencies, availableCoinlibCurrencies, availableNativeCurrencies } from '@keagate/common';
import GenericAdminWallet from './adminWallets/GenericAdminWallet';
import auth from './middlewares/auth';
import createPaymentRoute from './routes/createPayment';
import createActivePaymentsRoute from './routes/activePayments';
import createPaymentStatusRoute from './routes/paymentStatus';
import createInvoiceClientRoute from './routes/invoiceClient';
import createSwaggerRoute from './routes/swagger';
import createInvoiceStatusRoute from './routes/invoiceStatus';
import createPaymentsByExtraIdRoute from './routes/paymentsByExtraId';
import context from './context';
import activityLoop from './activityLoop';
import AdminCoinlibWrapper from './adminWallets/coinlib/AdminCoinlibWrapper';
import devServer from './devServer';
import logger from './logger';
const server = fastify({
trustProxy: true,
ajv: {
customOptions: {
strict: 'log',
keywords: ['kind', 'modifier'],
},
},
});
/**
* Native = currency processed by a wallet built into Keagate
* Coinlib = currency processed by the port of coinlib
*/
async function main() {
await context.init();
server.register(createSwaggerRoute);
// Initialize the admin wallet routes for native currencies
for (const _currency of context.enabledNativeCurrencies) {
const coinName = currencies[_currency].name as typeof availableNativeCurrencies[number];
const publicKey: string = config.getTyped(_currency).ADMIN_PUBLIC_KEY;
const privateKey: string = config.getTyped(_currency).ADMIN_PRIVATE_KEY;
if (!publicKey || !privateKey) {
console.error(`No admin public key and private key found for currency ${_currency}`);
continue;
}
let currentClient: GenericAdminWallet;
if (context.nativeCurrencyToClient[_currency]) {
currentClient = new context.nativeCurrencyToClient[_currency].Admin({
publicKey,
privateKey,
});
} else {
console.error(`No admin wallet found for currency ${_currency}`);
continue;
}
// Get the balance of and send a transaction from the admin wallet
server.get(`/get${coinName}Balance`, { preHandler: auth }, (request, reply) => currentClient.getBalance());
server.post<{ Body: Record<string, any> }>(`/send${coinName}Transaction`, { preHandler: auth }, (request, reply) =>
currentClient.sendTransaction(request.body.destination, request.body.amount),
);
}
// Do the same for coinlib currencies
for (const _currency of context.enabledCoinlibCurrencies) {
const coinName = currencies[_currency].name as typeof availableCoinlibCurrencies[number];
const publicKey: string = config.getTyped(_currency).ADMIN_PUBLIC_KEY;
const privateKey: string = config.getTyped(_currency).ADMIN_PRIVATE_KEY;
if (!publicKey || !privateKey) {
console.error(`No admin public key and private key found for currency ${_currency}`);
continue;
}
const currentClient = new AdminCoinlibWrapper({
currency: _currency,
privateKey: privateKey,
});
server.get(`/get${coinName}Balance`, { preHandler: auth }, (request, reply) => currentClient.getBalance());
server.post<{ Body: Record<string, any> }>(`/send${coinName}Transaction`, { preHandler: auth }, (request, reply) =>
currentClient.sendTransaction(request.body.destination, request.body.amount),
);
}
// Create other routes for API and invoice client
server.register(createInvoiceClientRoute);
server.register(createInvoiceStatusRoute);
server.register(createPaymentRoute);
server.register(createActivePaymentsRoute);
server.register(createPaymentStatusRoute);
server.register(createPaymentsByExtraIdRoute);
// Start the processing intervals
activityLoop.start();
if (config.getTyped('IS_DEV') && config.has('IPN_HMAC_SECRET')) {
server.register(devServer);
}
await server.ready();
server.swagger();
server.listen({ port: config.getTyped('PORT'), host: config.getTyped('BIND_HOST') ?? '127.0.0.1' }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
logger.log(`Keagate backend server listening at ${address}`);
});
}
main();