@@ -123,6 +123,7 @@ export class BitGoAPI implements BitGoBase {
123123 protected _extensionKey ?: ECPairInterface ;
124124 protected _reqId ?: IRequestTracer ;
125125 protected _token ?: string ;
126+ protected _tokenId ?: string ; // V4: separate token identifier
126127 protected _version = pjson . version ;
127128 protected _userAgent ?: string ;
128129 protected _ecdhXprv ?: string ;
@@ -735,6 +736,7 @@ export class BitGoAPI implements BitGoBase {
735736 return {
736737 user : this . _user ,
737738 token : this . _token ,
739+ tokenId : this . _tokenId ,
738740 extensionKey : this . _extensionKey ? this . _extensionKey . toWIF ( ) : undefined ,
739741 ecdhXprv : this . _ecdhXprv ,
740742 } ;
@@ -758,6 +760,7 @@ export class BitGoAPI implements BitGoBase {
758760 fromJSON ( json : BitGoJson ) : void {
759761 this . _user = json . user ;
760762 this . _token = json . token ;
763+ this . _tokenId = json . tokenId ;
761764 this . _ecdhXprv = json . ecdhXprv ;
762765 if ( json . extensionKey ) {
763766 const network = common . Environments [ this . getEnv ( ) ] . network ;
@@ -980,6 +983,11 @@ export class BitGoAPI implements BitGoBase {
980983 this . _token = responseDetails . token ;
981984 this . _ecdhXprv = responseDetails . ecdhXprv ;
982985
986+ // V4: store separate token identifier
987+ if ( this . _authVersion === 4 && body . id ) {
988+ this . _tokenId = body . id ;
989+ }
990+
983991 // verify the response's authenticity
984992 verifyResponse ( this , responseDetails . token , 'post' , request , response , this . _authVersion ) ;
985993
@@ -1131,6 +1139,7 @@ export class BitGoAPI implements BitGoBase {
11311139 // TODO: are there any other fields which should be cleared?
11321140 this . _user = undefined ;
11331141 this . _token = undefined ;
1142+ this . _tokenId = undefined ;
11341143 this . _refreshToken = undefined ;
11351144 this . _ecdhXprv = undefined ;
11361145 }
@@ -1271,9 +1280,18 @@ export class BitGoAPI implements BitGoBase {
12711280 // verify the authenticity of the server's response before proceeding any further
12721281 verifyResponse ( this , this . _token , 'post' , request , response , this . _authVersion ) ;
12731282
1283+ // Decrypt token using ECDH (same for V2/V3/V4)
12741284 const responseDetails = this . handleTokenIssuance ( response . body ) ;
12751285 response . body . token = responseDetails . token ;
12761286
1287+ // V4: Store separate tokenId alongside signing key
1288+ if ( this . _authVersion === 4 ) {
1289+ if ( ! response . body . id ) {
1290+ throw new Error ( 'Invalid V4 token issuance response: missing id field' ) ;
1291+ }
1292+ response . body . tokenId = response . body . id ;
1293+ }
1294+
12771295 return handleResponseResult < AddAccessTokenResponse > ( ) ( response ) ;
12781296 } catch ( e ) {
12791297 handleResponseError ( e ) ;
0 commit comments