@@ -2,6 +2,7 @@ import debug from 'debug';
22import * as superagent from 'superagent' ;
33import { config , isMasterExpressConfig } from '../config' ;
44import { PostKeyKmsSchema , PostKeyParams , PostKeyResponse } from './types/postKey' ;
5+ import { GetKeyKmsSchema , GetKeyParams , GetKeyResponse } from './types/getKey' ;
56
67const debugLogger = debug ( 'bitgo:express:kmsClient' ) ;
78
@@ -25,6 +26,7 @@ export class KmsClient {
2526 async postKey ( params : PostKeyParams ) : Promise < PostKeyResponse > {
2627 debugLogger ( 'Posting key to KMS: %O' , params ) ;
2728
29+ // Call KMS to post the key
2830 let kmsResponse : any ;
2931 try {
3032 kmsResponse = await superagent . post ( `${ this . url } /key` ) . set ( 'x-api-key' , 'abc' ) . send ( params ) ;
@@ -33,6 +35,7 @@ export class KmsClient {
3335 throw error ;
3436 }
3537
38+ // validate the response
3639 try {
3740 PostKeyKmsSchema . parse ( kmsResponse . body ) ;
3841 } catch ( error : any ) {
@@ -44,4 +47,31 @@ export class KmsClient {
4447 const { pub, coin, source } = kmsResponse . body ;
4548 return { pub, coin, source } as PostKeyResponse ;
4649 }
50+
51+ async getKey ( params : GetKeyParams ) : Promise < GetKeyResponse > {
52+ debugLogger ( 'Getting key from KMS: %O' , params ) ;
53+
54+ // Call KMS to get the key
55+ let kmsResponse : any ;
56+ try {
57+ kmsResponse = await superagent
58+ . get ( `${ this . url } /key/${ params . pub } ` )
59+ . set ( 'x-api-key' , 'abc' )
60+ . query ( { source : params . source } ) ;
61+ } catch ( error : any ) {
62+ console . log ( 'Error getting key from KMS' , error ) ;
63+ throw error ;
64+ }
65+
66+ // validate the response
67+ try {
68+ GetKeyKmsSchema . parse ( kmsResponse . body ) ;
69+ } catch ( error : any ) {
70+ throw new Error (
71+ `KMS returned unexpected response${ error . message ? `: ${ error . message } ` : '' } ` ,
72+ ) ;
73+ }
74+
75+ return kmsResponse . body as GetKeyResponse ;
76+ }
4777}
0 commit comments