1
1
import axios from 'axios' ;
2
2
import * as fs from 'fs' ;
3
- import { join } from 'path' ;
4
3
import { APP_CONSTANTS , DEFAULT_CONFIG , FIAT_RATE_API , FIAT_VENUES , HttpStatusCode , } from '../shared/consts.js' ;
5
4
import { logger } from '../shared/logger.js' ;
6
5
import handleError from '../shared/error-handler.js' ;
7
6
import { APIError } from '../models/errors.js' ;
8
- import { setSharedApplicationConfig , overrideSettingsWithEnvVariables } from '../shared/utils.js' ;
9
- import { sep } from 'path' ;
7
+ import { setSharedApplicationConfig , overrideSettingsWithEnvVariables , refreshEnvVariables , } from '../shared/utils.js' ;
10
8
import { CLNService } from '../service/lightning.service.js' ;
11
9
class SharedController {
12
10
getApplicationSettings ( req , res , next ) {
13
11
try {
14
- logger . info ( 'Getting Application Settings from ' + APP_CONSTANTS . CONFIG_LOCATION ) ;
15
- if ( ! fs . existsSync ( APP_CONSTANTS . CONFIG_LOCATION ) ) {
16
- fs . writeFileSync ( APP_CONSTANTS . CONFIG_LOCATION , JSON . stringify ( DEFAULT_CONFIG , null , 2 ) , 'utf-8' ) ;
12
+ logger . info ( 'Getting Application Settings from ' + APP_CONSTANTS . APP_CONFIG_FILE ) ;
13
+ if ( ! fs . existsSync ( APP_CONSTANTS . APP_CONFIG_FILE ) ) {
14
+ fs . writeFileSync ( APP_CONSTANTS . APP_CONFIG_FILE , JSON . stringify ( DEFAULT_CONFIG , null , 2 ) , 'utf-8' ) ;
17
15
}
18
- let config = JSON . parse ( fs . readFileSync ( APP_CONSTANTS . CONFIG_LOCATION , 'utf-8' ) ) ;
16
+ let config = JSON . parse ( fs . readFileSync ( APP_CONSTANTS . APP_CONFIG_FILE , 'utf-8' ) ) ;
19
17
config = overrideSettingsWithEnvVariables ( config ) ;
20
18
setSharedApplicationConfig ( config ) ;
21
19
delete config . password ;
@@ -28,9 +26,9 @@ class SharedController {
28
26
setApplicationSettings ( req , res , next ) {
29
27
try {
30
28
logger . info ( 'Updating Application Settings: ' + JSON . stringify ( req . body ) ) ;
31
- const config = JSON . parse ( fs . readFileSync ( APP_CONSTANTS . CONFIG_LOCATION , 'utf-8' ) ) ;
29
+ const config = JSON . parse ( fs . readFileSync ( APP_CONSTANTS . APP_CONFIG_FILE , 'utf-8' ) ) ;
32
30
req . body . password = config . password ; // Before saving, add password in the config received from frontend
33
- fs . writeFileSync ( APP_CONSTANTS . CONFIG_LOCATION , JSON . stringify ( req . body , null , 2 ) , 'utf-8' ) ;
31
+ fs . writeFileSync ( APP_CONSTANTS . APP_CONFIG_FILE , JSON . stringify ( req . body , null , 2 ) , 'utf-8' ) ;
34
32
res . status ( 201 ) . json ( { message : 'Application Settings Updated Successfully' } ) ;
35
33
}
36
34
catch ( error ) {
@@ -40,60 +38,8 @@ class SharedController {
40
38
getWalletConnectSettings ( req , res , next ) {
41
39
try {
42
40
logger . info ( 'Getting Connection Settings' ) ;
43
- const CERTS_PATH = process . env . CORE_LIGHTNING_PATH + sep + process . env . APP_BITCOIN_NETWORK + sep ;
44
- let macaroon = '' ;
45
- let clientKey = '' ;
46
- let clientCert = '' ;
47
- let caCert = '' ;
48
- let packageData = '{ version: "0.0.4" }' ;
49
- let macaroonFile = join ( APP_CONSTANTS . CERT_PATH || '.' , 'access.macaroon' ) ;
50
- if ( fs . existsSync ( macaroonFile ) ) {
51
- logger . info ( 'Getting REST Access Macaroon from ' + APP_CONSTANTS . CERT_PATH ) ;
52
- macaroon = Buffer . from ( fs . readFileSync ( macaroonFile ) ) . toString ( 'hex' ) ;
53
- }
54
- if ( fs . existsSync ( 'package.json' ) ) {
55
- packageData = Buffer . from ( fs . readFileSync ( 'package.json' ) ) . toString ( ) ;
56
- }
57
- if ( fs . existsSync ( CERTS_PATH + 'client-key.pem' ) ) {
58
- clientKey = fs . readFileSync ( CERTS_PATH + 'client-key.pem' ) . toString ( ) ;
59
- clientKey = clientKey
60
- . replace ( / ( \r \n | \n | \r ) / gm, '' )
61
- . replace ( '-----BEGIN PRIVATE KEY-----' , '' )
62
- . replace ( '-----END PRIVATE KEY-----' , '' ) ;
63
- }
64
- if ( fs . existsSync ( CERTS_PATH + 'client.pem' ) ) {
65
- clientCert = fs . readFileSync ( CERTS_PATH + 'client.pem' ) . toString ( ) ;
66
- clientCert = clientCert
67
- . replace ( / ( \r \n | \n | \r ) / gm, '' )
68
- . replace ( '-----BEGIN CERTIFICATE-----' , '' )
69
- . replace ( '-----END CERTIFICATE-----' , '' ) ;
70
- }
71
- if ( fs . existsSync ( CERTS_PATH + 'ca.pem' ) ) {
72
- caCert = fs . readFileSync ( CERTS_PATH + 'ca.pem' ) . toString ( ) ;
73
- caCert = caCert
74
- . replace ( / ( \r \n | \n | \r ) / gm, '' )
75
- . replace ( '-----BEGIN CERTIFICATE-----' , '' )
76
- . replace ( '-----END CERTIFICATE-----' , '' ) ;
77
- }
78
- CLNService . refreshEnvVariables ( ) ;
79
- const CONNECT_WALLET_SETTINGS = {
80
- LOCAL_HOST : process . env . LOCAL_HOST || '' ,
81
- DEVICE_DOMAIN_NAME : process . env . DEVICE_DOMAIN_NAME || '' ,
82
- TOR_HOST : process . env . APP_CORE_LIGHTNING_REST_HIDDEN_SERVICE || '' ,
83
- WS_PORT : process . env . APP_CORE_LIGHTNING_WEBSOCKET_PORT || '' ,
84
- GRPC_PORT : process . env . APP_CORE_LIGHTNING_DAEMON_GRPC_PORT || '' ,
85
- CLIENT_KEY : clientKey ,
86
- CLIENT_CERT : clientCert ,
87
- CA_CERT : caCert ,
88
- REST_PORT : process . env . APP_CORE_LIGHTNING_REST_PORT || '' ,
89
- REST_MACAROON : macaroon ,
90
- CLN_NODE_IP : process . env . APP_CORE_LIGHTNING_DAEMON_IP || '' ,
91
- NODE_PUBKEY : process . env . LIGHTNING_PUBKEY || '' ,
92
- COMMANDO_RUNE : process . env . COMMANDO_RUNE ,
93
- APP_VERSION : JSON . parse ( packageData ) . version || '' ,
94
- INVOICE_RUNE : process . env . INVOICE_RUNE || '' ,
95
- } ;
96
- res . status ( 200 ) . json ( CONNECT_WALLET_SETTINGS ) ;
41
+ refreshEnvVariables ( ) ;
42
+ res . status ( 200 ) . json ( APP_CONSTANTS ) ;
97
43
}
98
44
catch ( error ) {
99
45
handleError ( error , req , res , next ) ;
@@ -130,9 +76,9 @@ class SharedController {
130
76
const showRunes = await CLNService . call ( 'showrunes' , [ ] ) ;
131
77
const invoiceRune = showRunes . runes . find ( rune => rune . restrictions . some ( restriction => restriction . alternatives . some ( alternative => alternative . value === 'invoice' ) ) &&
132
78
rune . restrictions . some ( restriction => restriction . alternatives . some ( alternative => alternative . value === 'listinvoices' ) ) ) ;
133
- if ( invoiceRune && fs . existsSync ( APP_CONSTANTS . COMMANDO_ENV_LOCATION ) ) {
79
+ if ( invoiceRune && fs . existsSync ( APP_CONSTANTS . COMMANDO_CONFIG ) ) {
134
80
const invoiceRuneString = `INVOICE_RUNE="${ invoiceRune . rune } "\n` ;
135
- fs . appendFileSync ( APP_CONSTANTS . COMMANDO_ENV_LOCATION , invoiceRuneString , 'utf-8' ) ;
81
+ fs . appendFileSync ( APP_CONSTANTS . COMMANDO_CONFIG , invoiceRuneString , 'utf-8' ) ;
136
82
res . status ( 201 ) . send ( ) ;
137
83
}
138
84
else {
0 commit comments