@@ -2,6 +2,7 @@ import superagent from 'superagent';
22import https from 'https' ;
33import debug from 'debug' ;
44import { MasterExpressConfig } from '../types' ;
5+ import { TlsMode } from '../types' ;
56
67const debugLogger = debug ( 'bitgo:express:enclavedExpressClient' ) ;
78
@@ -24,16 +25,17 @@ export interface IndependentKeychainResponse {
2425export class EnclavedExpressClient {
2526 private readonly baseUrl : string ;
2627 private readonly enclavedExpressCert : string ;
27- private readonly tlsKey : string ;
28- private readonly tlsCert : string ;
28+ private readonly tlsKey ? : string ;
29+ private readonly tlsCert ? : string ;
2930 private readonly allowSelfSigned : boolean ;
3031 private readonly coin ?: string ;
32+ private readonly tlsMode : TlsMode ;
3133
3234 constructor ( cfg : MasterExpressConfig , coin ?: string ) {
3335 if ( ! cfg . enclavedExpressUrl || ! cfg . enclavedExpressCert ) {
3436 throw new Error ( 'enclavedExpressUrl and enclavedExpressCert are required' ) ;
3537 }
36- if ( ! cfg . tlsKey || ! cfg . tlsCert ) {
38+ if ( cfg . tlsMode === TlsMode . MTLS && ( ! cfg . tlsKey || ! cfg . tlsCert ) ) {
3739 throw new Error ( 'tlsKey and tlsCert are required for mTLS communication' ) ;
3840 }
3941
@@ -43,10 +45,14 @@ export class EnclavedExpressClient {
4345 this . tlsCert = cfg . tlsCert ;
4446 this . allowSelfSigned = cfg . allowSelfSigned ?? false ;
4547 this . coin = coin ;
48+ this . tlsMode = cfg . tlsMode ;
4649 debugLogger ( 'EnclavedExpressClient initialized with URL: %s' , this . baseUrl ) ;
4750 }
4851
4952 private createHttpsAgent ( ) : https . Agent {
53+ if ( ! this . tlsKey || ! this . tlsCert ) {
54+ throw new Error ( 'TLS key and certificate are required for HTTPS agent' ) ;
55+ }
5056 return new https . Agent ( {
5157 rejectUnauthorized : ! this . allowSelfSigned ,
5258 ca : this . enclavedExpressCert ,
@@ -59,7 +65,12 @@ export class EnclavedExpressClient {
5965 async ping ( ) : Promise < void > {
6066 try {
6167 debugLogger ( 'Pinging enclaved express at %s' , this . baseUrl ) ;
62- await superagent . get ( `${ this . baseUrl } /ping` ) . agent ( this . createHttpsAgent ( ) ) . send ( ) ;
68+ if ( this . tlsMode === TlsMode . MTLS ) {
69+ await superagent . get ( `${ this . baseUrl } /ping` ) . agent ( this . createHttpsAgent ( ) ) . send ( ) ;
70+ } else {
71+ // When TLS is disabled, use plain HTTP without any TLS configuration
72+ await superagent . get ( `${ this . baseUrl } /ping` ) . send ( ) ;
73+ }
6374 } catch ( error ) {
6475 const err = error as Error ;
6576 debugLogger ( 'Failed to ping enclaved express: %s' , err . message ) ;
@@ -79,13 +90,22 @@ export class EnclavedExpressClient {
7990
8091 try {
8192 debugLogger ( 'Creating independent keychain for coin: %s' , this . coin ) ;
82- const { body : keychain } = await superagent
83- . post ( `${ this . baseUrl } /api/${ this . coin } /key/independent` )
84- . agent ( this . createHttpsAgent ( ) )
85- . type ( 'json' )
86- . send ( params ) ;
93+ let response ;
94+ if ( this . tlsMode === TlsMode . MTLS ) {
95+ response = await superagent
96+ . post ( `${ this . baseUrl } /api/${ this . coin } /key/independent` )
97+ . agent ( this . createHttpsAgent ( ) )
98+ . type ( 'json' )
99+ . send ( params ) ;
100+ } else {
101+ // When TLS is disabled, use plain HTTP without any TLS configuration
102+ response = await superagent
103+ . post ( `${ this . baseUrl } /api/${ this . coin } /key/independent` )
104+ . type ( 'json' )
105+ . send ( params ) ;
106+ }
87107
88- return keychain ;
108+ return response . body ;
89109 } catch ( error ) {
90110 const err = error as Error ;
91111 debugLogger ( 'Failed to create independent keychain: %s' , err . message ) ;
0 commit comments