@@ -21,17 +21,15 @@ import {
2121 makeRandomKey ,
2222 sanitizeLegacyPath ,
2323} from '@bitgo/sdk-core' ;
24- import * as sjcl from '@bitgo/sjcl ' ;
24+ import * as sdkHmac from '@bitgo/sdk-hmac ' ;
2525import * as utxolib from '@bitgo/utxo-lib' ;
2626import { bip32 , ECPairInterface } from '@bitgo/utxo-lib' ;
2727import * as bitcoinMessage from 'bitcoinjs-message' ;
28- import { createHmac } from 'crypto' ;
2928import { type Agent } from 'http' ;
3029import debugLib from 'debug' ;
3130import * as _ from 'lodash' ;
3231import * as secp256k1 from 'secp256k1' ;
3332import * as superagent from 'superagent' ;
34- import * as urlLib from 'url' ;
3533import {
3634 handleResponseError ,
3735 handleResponseResult ,
@@ -396,6 +394,7 @@ export class BitGoAPI implements BitGoBase {
396394 token : this . _token ,
397395 method,
398396 text : data || '' ,
397+ authVersion : this . _authVersion ,
399398 } ) ;
400399 req . set ( 'Auth-Timestamp' , requestProperties . timestamp . toString ( ) ) ;
401400
@@ -420,7 +419,7 @@ export class BitGoAPI implements BitGoBase {
420419 return onfulfilled ( response ) ;
421420 }
422421
423- const verifiedResponse = verifyResponse ( this , this . _token , method , req , response ) ;
422+ const verifiedResponse = verifyResponse ( this , this . _token , method , req , response , this . _authVersion ) ;
424423 return onfulfilled ( verifiedResponse ) ;
425424 }
426425 : null ;
@@ -455,7 +454,7 @@ export class BitGoAPI implements BitGoBase {
455454 * @returns {* } - the result of the HMAC operation
456455 */
457456 calculateHMAC ( key : string , message : string ) : string {
458- return createHmac ( 'sha256' , key ) . update ( message ) . digest ( 'hex' ) ;
457+ return sdkHmac . calculateHMAC ( key , message ) ;
459458 }
460459
461460 /**
@@ -467,83 +466,29 @@ export class BitGoAPI implements BitGoBase {
467466 * @param method request method
468467 * @returns {string }
469468 */
470- calculateHMACSubject ( { urlPath, text, timestamp, statusCode, method } : CalculateHmacSubjectOptions ) : string {
471- const urlDetails = urlLib . parse ( urlPath ) ;
472- const queryPath = urlDetails . query && urlDetails . query . length > 0 ? urlDetails . path : urlDetails . pathname ;
473- if ( ! _ . isUndefined ( statusCode ) && _ . isInteger ( statusCode ) && _ . isFinite ( statusCode ) ) {
474- if ( this . _authVersion === 3 ) {
475- return [ method . toUpperCase ( ) , timestamp , queryPath , statusCode , text ] . join ( '|' ) ;
476- }
477- return [ timestamp , queryPath , statusCode , text ] . join ( '|' ) ;
478- }
479- if ( this . _authVersion === 3 ) {
480- return [ method . toUpperCase ( ) , timestamp , '3.0' , queryPath , text ] . join ( '|' ) ;
481- }
482- return [ timestamp , queryPath , text ] . join ( '|' ) ;
469+ calculateHMACSubject ( params : CalculateHmacSubjectOptions ) : string {
470+ return sdkHmac . calculateHMACSubject ( { ...params , authVersion : this . _authVersion } ) ;
483471 }
484472
485473 /**
486474 * Calculate the HMAC for an HTTP request
487475 */
488- calculateRequestHMAC ( { url : urlPath , text, timestamp, token, method } : CalculateRequestHmacOptions ) : string {
489- const signatureSubject = this . calculateHMACSubject ( { urlPath, text, timestamp, method } ) ;
490-
491- // calculate the HMAC
492- return this . calculateHMAC ( token , signatureSubject ) ;
476+ calculateRequestHMAC ( params : CalculateRequestHmacOptions ) : string {
477+ return sdkHmac . calculateRequestHMAC ( { ...params , authVersion : this . _authVersion } ) ;
493478 }
494479
495480 /**
496481 * Calculate request headers with HMAC
497482 */
498- calculateRequestHeaders ( { url, text, token, method } : CalculateRequestHeadersOptions ) : RequestHeaders {
499- const timestamp = Date . now ( ) ;
500- const hmac = this . calculateRequestHMAC ( { url, text, timestamp, token, method } ) ;
501-
502- // calculate the SHA256 hash of the token
503- const hashDigest = sjcl . hash . sha256 . hash ( token ) ;
504- const tokenHash = sjcl . codec . hex . fromBits ( hashDigest ) ;
505- return {
506- hmac,
507- timestamp,
508- tokenHash,
509- } ;
483+ calculateRequestHeaders ( params : CalculateRequestHeadersOptions ) : RequestHeaders {
484+ return sdkHmac . calculateRequestHeaders ( { ...params , authVersion : this . _authVersion } ) ;
510485 }
511486
512487 /**
513488 * Verify the HMAC for an HTTP response
514489 */
515- verifyResponse ( {
516- url : urlPath ,
517- statusCode,
518- text,
519- timestamp,
520- token,
521- hmac,
522- method,
523- } : VerifyResponseOptions ) : VerifyResponseInfo {
524- const signatureSubject = this . calculateHMACSubject ( {
525- urlPath,
526- text,
527- timestamp,
528- statusCode,
529- method,
530- } ) ;
531-
532- // calculate the HMAC
533- const expectedHmac = this . calculateHMAC ( token , signatureSubject ) ;
534-
535- // determine if the response is still within the validity window (5 minute window)
536- const now = Date . now ( ) ;
537- const isInResponseValidityWindow = timestamp >= now - 1000 * 60 * 5 && timestamp <= now ;
538-
539- // verify the HMAC and timestamp
540- return {
541- isValid : expectedHmac === hmac ,
542- expectedHmac,
543- signatureSubject,
544- isInResponseValidityWindow,
545- verificationTime : now ,
546- } ;
490+ verifyResponse ( params : VerifyResponseOptions ) : VerifyResponseInfo {
491+ return sdkHmac . verifyResponse ( { ...params , authVersion : this . _authVersion } ) ;
547492 }
548493
549494 /**
@@ -904,7 +849,7 @@ export class BitGoAPI implements BitGoBase {
904849 this . _ecdhXprv = responseDetails . ecdhXprv ;
905850
906851 // verify the response's authenticity
907- verifyResponse ( this , responseDetails . token , 'post' , request , response ) ;
852+ verifyResponse ( this , responseDetails . token , 'post' , request , response , this . _authVersion ) ;
908853
909854 // add the remaining component for easier access
910855 response . body . access_token = this . _token ;
@@ -1186,7 +1131,7 @@ export class BitGoAPI implements BitGoBase {
11861131 }
11871132
11881133 // verify the authenticity of the server's response before proceeding any further
1189- verifyResponse ( this , this . _token , 'post' , request , response ) ;
1134+ verifyResponse ( this , this . _token , 'post' , request , response , this . _authVersion ) ;
11901135
11911136 const responseDetails = this . handleTokenIssuance ( response . body ) ;
11921137 response . body . token = responseDetails . token ;
0 commit comments