11import axios from 'axios' ;
22import * as fs from 'fs' ;
3- import { join } from 'path' ;
43import { APP_CONSTANTS , DEFAULT_CONFIG , FIAT_RATE_API , FIAT_VENUES , HttpStatusCode , } from '../shared/consts.js' ;
54import { logger } from '../shared/logger.js' ;
65import handleError from '../shared/error-handler.js' ;
76import { 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' ;
108import { CLNService } from '../service/lightning.service.js' ;
119class SharedController {
1210 getApplicationSettings ( req , res , next ) {
1311 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' ) ;
1715 }
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' ) ) ;
1917 config = overrideSettingsWithEnvVariables ( config ) ;
2018 setSharedApplicationConfig ( config ) ;
2119 delete config . password ;
@@ -28,9 +26,9 @@ class SharedController {
2826 setApplicationSettings ( req , res , next ) {
2927 try {
3028 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' ) ) ;
3230 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' ) ;
3432 res . status ( 201 ) . json ( { message : 'Application Settings Updated Successfully' } ) ;
3533 }
3634 catch ( error ) {
@@ -40,60 +38,8 @@ class SharedController {
4038 getWalletConnectSettings ( req , res , next ) {
4139 try {
4240 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 ) ;
9743 }
9844 catch ( error ) {
9945 handleError ( error , req , res , next ) ;
@@ -130,9 +76,9 @@ class SharedController {
13076 const showRunes = await CLNService . call ( 'showrunes' , [ ] ) ;
13177 const invoiceRune = showRunes . runes . find ( rune => rune . restrictions . some ( restriction => restriction . alternatives . some ( alternative => alternative . value === 'invoice' ) ) &&
13278 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 ) ) {
13480 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' ) ;
13682 res . status ( 201 ) . send ( ) ;
13783 }
13884 else {
0 commit comments