11import { EnvironmentName , V1Network } from 'bitgo' ;
22import { isNil , isNumber } from 'lodash' ;
3+ import { readFileSync } from 'fs' ;
34import 'dotenv/config' ;
45
56import { args } from './args' ;
@@ -38,6 +39,8 @@ export interface Config {
3839 customBitcoinNetwork ?: V1Network ;
3940 authVersion : number ;
4041 externalSignerUrl ?: string ;
42+ enclavedExpressUrl ?: string ;
43+ enclavedExpressSSLCert ?: string ;
4144 signerMode ?: boolean ;
4245 signerFileSystemPath ?: string ;
4346 lightningSignerFileSystemPath ?: string ;
@@ -64,6 +67,8 @@ export const ArgConfig = (args): Partial<Config> => ({
6467 customBitcoinNetwork : args . custombitcoinnetwork ,
6568 authVersion : args . authVersion ,
6669 externalSignerUrl : args . externalSignerUrl ,
70+ enclavedExpressUrl : args . enclavedExpressUrl ,
71+ enclavedExpressSSLCert : args . enclavedExpressSSLCert ,
6772 signerMode : args . signerMode ,
6873 signerFileSystemPath : args . signerFileSystemPath ,
6974 lightningSignerFileSystemPath : args . lightningSignerFileSystemPath ,
@@ -90,6 +95,8 @@ export const EnvConfig = (): Partial<Config> => ({
9095 customBitcoinNetwork : readEnvVar ( 'BITGO_CUSTOM_BITCOIN_NETWORK' ) as V1Network ,
9196 authVersion : Number ( readEnvVar ( 'BITGO_AUTH_VERSION' ) ) ,
9297 externalSignerUrl : readEnvVar ( 'BITGO_EXTERNAL_SIGNER_URL' ) ,
98+ enclavedExpressUrl : readEnvVar ( 'BITGO_ENCLAVED_EXPRESS_URL' ) ,
99+ enclavedExpressSSLCert : readEnvVar ( 'BITGO_ENCLAVED_EXPRESS_SSL_CERT' ) ,
93100 signerMode : readEnvVar ( 'BITGO_SIGNER_MODE' ) ? true : undefined ,
94101 signerFileSystemPath : readEnvVar ( 'BITGO_SIGNER_FILE_SYSTEM_PATH' ) ,
95102 lightningSignerFileSystemPath : readEnvVar ( 'BITGO_LIGHTNING_SIGNER_FILE_SYSTEM_PATH' ) ,
@@ -110,6 +117,8 @@ export const DefaultConfig: Config = {
110117 disableEnvCheck : true ,
111118 timeout : 305 * 1000 ,
112119 authVersion : 2 ,
120+ enclavedExpressUrl : undefined ,
121+ enclavedExpressSSLCert : undefined ,
113122} ;
114123
115124/**
@@ -147,6 +156,8 @@ function mergeConfigs(...configs: Partial<Config>[]): Config {
147156 const disableSSL = get ( 'disableSSL' ) || false ;
148157 let customRootUri = get ( 'customRootUri' ) ;
149158 let externalSignerUrl = get ( 'externalSignerUrl' ) ;
159+ let enclavedExpressUrl = get ( 'enclavedExpressUrl' ) ;
160+ let enclavedExpressSSLCert : string | undefined ;
150161
151162 if ( disableSSL !== true ) {
152163 if ( customRootUri ) {
@@ -155,6 +166,19 @@ function mergeConfigs(...configs: Partial<Config>[]): Config {
155166 if ( externalSignerUrl ) {
156167 externalSignerUrl = forceSecureUrl ( externalSignerUrl ) ;
157168 }
169+ if ( enclavedExpressUrl ) {
170+ enclavedExpressUrl = forceSecureUrl ( enclavedExpressUrl ) ;
171+ console . log ( 'Using secure enclaved express URL:' , enclavedExpressUrl ) ;
172+ }
173+ const enclavedExpressSSLCertPath = get ( 'enclavedExpressSSLCert' ) ;
174+ if ( enclavedExpressSSLCertPath ) {
175+ try {
176+ enclavedExpressSSLCert = readFileSync ( enclavedExpressSSLCertPath , { encoding : 'utf8' } ) ;
177+ console . log ( 'Successfully loaded SSL cert from:' , enclavedExpressSSLCertPath ) ;
178+ } catch ( e ) {
179+ console . error ( `Failed to load enclaved express SSL cert from path: ${ enclavedExpressSSLCertPath } ` , e ) ;
180+ }
181+ }
158182 }
159183
160184 return {
@@ -176,6 +200,8 @@ function mergeConfigs(...configs: Partial<Config>[]): Config {
176200 customBitcoinNetwork : get ( 'customBitcoinNetwork' ) ,
177201 authVersion : get ( 'authVersion' ) ,
178202 externalSignerUrl,
203+ enclavedExpressUrl,
204+ enclavedExpressSSLCert,
179205 signerMode : get ( 'signerMode' ) ,
180206 signerFileSystemPath : get ( 'signerFileSystemPath' ) ,
181207 lightningSignerFileSystemPath : get ( 'lightningSignerFileSystemPath' ) ,
@@ -184,8 +210,8 @@ function mergeConfigs(...configs: Partial<Config>[]): Config {
184210 } ;
185211}
186212
187- export const config = ( ) => {
213+ export function config ( ) : Config {
188214 const arg = ArgConfig ( args ( ) ) ;
189215 const env = EnvConfig ( ) ;
190216 return mergeConfigs ( env , arg ) ;
191- } ;
217+ }
0 commit comments