11import { EnvironmentName , V1Network } from 'bitgo' ;
22import { isNil , isNumber } from 'lodash' ;
3+ import { readFileSync , existsSync } 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,24 @@ 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 enclavedExpressSSLCertValue = get ( 'enclavedExpressSSLCert' ) ;
174+ if ( enclavedExpressSSLCertValue ) {
175+ try {
176+ // First try to read it as a file path
177+ enclavedExpressSSLCert = existsSync ( enclavedExpressSSLCertValue )
178+ ? readFileSync ( enclavedExpressSSLCertValue , { encoding : 'utf8' } )
179+ : enclavedExpressSSLCertValue ; // If not a file, use the value directly
180+ if ( existsSync ( enclavedExpressSSLCertValue ) ) {
181+ console . log ( 'Successfully loaded SSL cert from:' , enclavedExpressSSLCertValue ) ;
182+ }
183+ } catch ( e ) {
184+ console . error ( `Failed to process enclaved express SSL cert: ${ enclavedExpressSSLCertValue } ` , e ) ;
185+ }
186+ }
158187 }
159188
160189 return {
@@ -176,6 +205,8 @@ function mergeConfigs(...configs: Partial<Config>[]): Config {
176205 customBitcoinNetwork : get ( 'customBitcoinNetwork' ) ,
177206 authVersion : get ( 'authVersion' ) ,
178207 externalSignerUrl,
208+ enclavedExpressUrl,
209+ enclavedExpressSSLCert,
179210 signerMode : get ( 'signerMode' ) ,
180211 signerFileSystemPath : get ( 'signerFileSystemPath' ) ,
181212 lightningSignerFileSystemPath : get ( 'lightningSignerFileSystemPath' ) ,
@@ -184,8 +215,8 @@ function mergeConfigs(...configs: Partial<Config>[]): Config {
184215 } ;
185216}
186217
187- export const config = ( ) => {
218+ export function config ( ) : Config {
188219 const arg = ArgConfig ( args ( ) ) ;
189220 const env = EnvConfig ( ) ;
190221 return mergeConfigs ( env , arg ) ;
191- } ;
222+ }
0 commit comments