forked from ElementsProject/cln-application
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshared.ts
More file actions
135 lines (127 loc) · 4.94 KB
/
shared.ts
File metadata and controls
135 lines (127 loc) · 4.94 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import axios from 'axios';
import * as fs from 'fs';
import { Request, Response, NextFunction } from 'express';
import {
APP_CONSTANTS,
DEFAULT_CONFIG,
FIAT_RATE_API,
FIAT_VENUES,
HttpStatusCode,
} from '../shared/consts.js';
import { logger } from '../shared/logger.js';
import handleError from '../shared/error-handler.js';
import { APIError } from '../models/errors.js';
import { addServerConfig, setEnvVariables } from '../shared/utils.js';
import { ShowRunes } from '../models/showrunes.type.js';
import { LightningService } from '../service/lightning.service.js';
export class SharedController {
private clnService: LightningService;
constructor(clnService: LightningService) {
this.clnService = clnService;
}
getApplicationSettings = async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Getting Application Settings from ' + APP_CONSTANTS.APP_CONFIG_FILE);
if (!fs.existsSync(APP_CONSTANTS.APP_CONFIG_FILE)) {
logger.warn(
`Config file ${APP_CONSTANTS.APP_CONFIG_FILE} not found. Creating default config.`,
);
fs.writeFileSync(
APP_CONSTANTS.APP_CONFIG_FILE,
JSON.stringify(DEFAULT_CONFIG, null, 2),
'utf-8',
);
}
let config = {
uiConfig: JSON.parse(fs.readFileSync(APP_CONSTANTS.APP_CONFIG_FILE, 'utf-8')),
};
delete config.uiConfig.password;
delete config.uiConfig.isLoading;
delete config.uiConfig.error;
delete config.uiConfig.singleSignOn;
config = addServerConfig(config);
res.status(200).json(config);
} catch (error: any) {
handleError(error, req, res, next);
}
};
setApplicationSettings = async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Updating Application Settings: ' + JSON.stringify(req.body));
const config = JSON.parse(fs.readFileSync(APP_CONSTANTS.APP_CONFIG_FILE, 'utf-8'));
req.body.uiConfig.password = config.password; // Before saving, add password in the config received from frontend
fs.writeFileSync(
APP_CONSTANTS.APP_CONFIG_FILE,
JSON.stringify(req.body.uiConfig, null, 2),
'utf-8',
);
res.status(201).json({ message: 'Application Settings Updated Successfully' });
} catch (error: any) {
handleError(error, req, res, next);
}
};
getWalletConnectSettings = async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Getting Connection Settings');
setEnvVariables();
res.status(200).json(APP_CONSTANTS);
} catch (error: any) {
handleError(error, req, res, next);
}
};
getFiatRate = async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Getting Fiat Rate for: ' + req.params.fiatCurrency);
const FIAT_VENUE = FIAT_VENUES.hasOwnProperty(req.params.fiatCurrency)
? FIAT_VENUES[req.params.fiatCurrency]
: 'COINGECKO';
logger.info('Fiat URL: ' + FIAT_RATE_API + FIAT_VENUE + '/pairs/XBT/' + req.params.fiatCurrency);
return axios
.get(FIAT_RATE_API + FIAT_VENUE + '/pairs/XBT/' + req.params.fiatCurrency)
.then((response: any) => {
logger.info('Fiat Response: ' + JSON.stringify(response?.data));
if (response.data?.rate) {
return res.status(200).json({ venue: FIAT_VENUE, rate: response.data?.rate });
} else {
return handleError(
new APIError(HttpStatusCode.NOT_FOUND, 'Price Not Found'),
req,
res,
next,
);
}
})
.catch(err => {
logger.error('Fiat Error Response: ' + JSON.stringify(err));
res.status(200).json({ venue: "NONE", rate: "0" });
});
} catch (error: any) {
logger.error('Error from Fiat Rate: ' + JSON.stringify(error));
res.status(200).json({ venue: "NONE", rate: "0" });
}
};
saveInvoiceRune = async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Saving Invoice Rune');
const showRunes: ShowRunes = await this.clnService.call('showrunes', []);
const invoiceRune = showRunes.runes.find(
rune =>
rune.restrictions.some(restriction =>
restriction.alternatives.some(alternative => alternative.value === 'invoice'),
) &&
rune.restrictions.some(restriction =>
restriction.alternatives.some(alternative => alternative.value === 'listinvoices'),
),
);
if (invoiceRune && fs.existsSync(APP_CONSTANTS.LIGHTNING_VARS_FILE)) {
const invoiceRuneString = `INVOICE_RUNE="${invoiceRune.rune}"\n`;
fs.appendFileSync(APP_CONSTANTS.LIGHTNING_VARS_FILE, invoiceRuneString, 'utf-8');
res.status(201).send();
} else {
throw new Error('Invoice rune not found or .commando-env does not exist.');
}
} catch (error: any) {
handleError(error, req, res, next);
}
};
}