@@ -17,6 +17,7 @@ import {
1717 generateAddressFromPubKey ,
1818 generateShares ,
1919 getEd25519ExtendedPublicKey ,
20+ getKeyCurve ,
2021 getMetadata ,
2122 getOrSetNonce ,
2223 GetOrSetNonceError ,
@@ -171,10 +172,11 @@ class Torus {
171172 async getPublicAddress (
172173 endpoints : string [ ] ,
173174 torusNodePubs : INodePub [ ] ,
174- { verifier, verifierId, extendedVerifierId } : { verifier : string ; verifierId : string ; extendedVerifierId ?: string }
175+ { verifier, verifierId, extendedVerifierId, keyType } : { verifier : string ; verifierId : string ; extendedVerifierId ?: string ; keyType ?: KeyType }
175176 ) : Promise < TorusPublicKey > {
176177 log . info ( torusNodePubs , { verifier, verifierId, extendedVerifierId } ) ;
177- return this . getNewPublicAddress ( endpoints , { verifier, verifierId, extendedVerifierId } , this . enableOneKey ) ;
178+ const localKeyType = keyType ?? this . keyType ;
179+ return this . getNewPublicAddress ( endpoints , { verifier, verifierId, extendedVerifierId, keyType : localKeyType } , this . enableOneKey ) ;
178180 }
179181
180182 async importPrivateKey ( params : ImportKeyParams ) : Promise < TorusKey > {
@@ -264,15 +266,22 @@ class Torus {
264266
265267 private async getNewPublicAddress (
266268 endpoints : string [ ] ,
267- { verifier, verifierId, extendedVerifierId } : { verifier : string ; verifierId : string ; extendedVerifierId ?: string } ,
269+ { verifier, verifierId, extendedVerifierId, keyType } : { verifier : string ; verifierId : string ; extendedVerifierId ?: string ; keyType ?: KeyType } ,
268270 enableOneKey : boolean
269271 ) : Promise < TorusPublicKey > {
272+ const localKeyType = keyType ?? this . keyType ;
273+ const localEc = getKeyCurve ( localKeyType ) ;
274+
275+ if ( localKeyType === KEY_TYPE . ED25519 && LEGACY_NETWORKS_ROUTE_MAP [ this . network as TORUS_LEGACY_NETWORK_TYPE ] ) {
276+ throw new Error ( `keyType: ${ keyType } is not supported by ${ this . network } network` ) ;
277+ }
278+
270279 const keyAssignResult = await GetPubKeyOrKeyAssign ( {
271280 endpoints,
272281 network : this . network ,
273282 verifier,
274283 verifierId,
275- keyType : this . keyType ,
284+ keyType : localKeyType ,
276285 extendedVerifierId,
277286 } ) ;
278287
@@ -303,7 +312,7 @@ class Torus {
303312 let finalPubKey : curve . base . BasePoint ;
304313 if ( extendedVerifierId ) {
305314 // for tss key no need to add pub nonce
306- finalPubKey = this . ec . keyFromPublic ( { x : X , y : Y } ) . getPublic ( ) ;
315+ finalPubKey = localEc . keyFromPublic ( { x : X , y : Y } ) . getPublic ( ) ;
307316 oAuthPubKey = finalPubKey ;
308317 } else if ( LEGACY_NETWORKS_ROUTE_MAP [ this . network as TORUS_LEGACY_NETWORK_TYPE ] ) {
309318 return this . formatLegacyPublicKeyData ( {
@@ -316,11 +325,11 @@ class Torus {
316325 } ) ;
317326 } else {
318327 const v2NonceResult = nonceResult as v2NonceResultType ;
319- oAuthPubKey = this . ec . keyFromPublic ( { x : X , y : Y } ) . getPublic ( ) ;
320- finalPubKey = this . ec
328+ oAuthPubKey = localEc . keyFromPublic ( { x : X , y : Y } ) . getPublic ( ) ;
329+ finalPubKey = localEc
321330 . keyFromPublic ( { x : X , y : Y } )
322331 . getPublic ( )
323- . add ( this . ec . keyFromPublic ( { x : v2NonceResult . pubNonce . x , y : v2NonceResult . pubNonce . y } ) . getPublic ( ) ) ;
332+ . add ( localEc . keyFromPublic ( { x : v2NonceResult . pubNonce . x , y : v2NonceResult . pubNonce . y } ) . getPublic ( ) ) ;
324333
325334 pubNonce = { X : v2NonceResult . pubNonce . x , Y : v2NonceResult . pubNonce . y } ;
326335 }
@@ -330,14 +339,14 @@ class Torus {
330339 }
331340 const oAuthX = oAuthPubKey . getX ( ) . toString ( 16 , 64 ) ;
332341 const oAuthY = oAuthPubKey . getY ( ) . toString ( 16 , 64 ) ;
333- const oAuthAddress = generateAddressFromPubKey ( this . keyType , oAuthPubKey . getX ( ) , oAuthPubKey . getY ( ) ) ;
342+ const oAuthAddress = generateAddressFromPubKey ( localKeyType , oAuthPubKey . getX ( ) , oAuthPubKey . getY ( ) ) ;
334343
335344 if ( ! finalPubKey ) {
336345 throw new Error ( "Unable to derive finalPubKey" ) ;
337346 }
338347 const finalX = finalPubKey ? finalPubKey . getX ( ) . toString ( 16 , 64 ) : "" ;
339348 const finalY = finalPubKey ? finalPubKey . getY ( ) . toString ( 16 , 64 ) : "" ;
340- const finalAddress = finalPubKey ? generateAddressFromPubKey ( this . keyType , finalPubKey . getX ( ) , finalPubKey . getY ( ) ) : "" ;
349+ const finalAddress = finalPubKey ? generateAddressFromPubKey ( localKeyType , finalPubKey . getX ( ) , finalPubKey . getY ( ) ) : "" ;
341350 return {
342351 oAuthKeyData : {
343352 walletAddress : oAuthAddress ,
@@ -367,63 +376,67 @@ class Torus {
367376 enableOneKey : boolean ;
368377 isNewKey : boolean ;
369378 serverTimeOffset : number ;
379+ keyType ?: KeyType ;
370380 } ) : Promise < TorusPublicKey > {
371- const { finalKeyResult, enableOneKey, isNewKey, serverTimeOffset } = params ;
381+ const { finalKeyResult, enableOneKey, isNewKey, serverTimeOffset, keyType } = params ;
382+ const localKeyType = keyType ?? this . keyType ;
383+ const localEc = getKeyCurve ( localKeyType ) ;
384+
372385 const { pub_key_X : X , pub_key_Y : Y } = finalKeyResult . keys [ 0 ] ;
373386 let nonceResult : GetOrSetNonceResult ;
374387 let nonce : BN ;
375388 let finalPubKey : curve . base . BasePoint ;
376389 let typeOfUser : GetOrSetNonceResult [ "typeOfUser" ] ;
377390 let pubNonce : { X : string ; Y : string } | undefined ;
378391
379- const oAuthPubKey = this . ec . keyFromPublic ( { x : X , y : Y } ) . getPublic ( ) ;
392+ const oAuthPubKey = localEc . keyFromPublic ( { x : X , y : Y } ) . getPublic ( ) ;
380393
381394 const finalServerTimeOffset = this . serverTimeOffset || serverTimeOffset ;
382395 if ( enableOneKey ) {
383396 try {
384- nonceResult = await getOrSetNonce ( this . legacyMetadataHost , this . ec , finalServerTimeOffset , X , Y , undefined , ! isNewKey ) ;
397+ nonceResult = await getOrSetNonce ( this . legacyMetadataHost , localEc , finalServerTimeOffset , X , Y , undefined , ! isNewKey ) ;
385398 nonce = new BN ( nonceResult . nonce || "0" , 16 ) ;
386399 typeOfUser = nonceResult . typeOfUser ;
387400 } catch {
388401 throw new GetOrSetNonceError ( ) ;
389402 }
390403 if ( nonceResult . typeOfUser === "v1" ) {
391404 nonce = await getMetadata ( this . legacyMetadataHost , { pub_key_X : X , pub_key_Y : Y } ) ;
392- finalPubKey = this . ec
405+ finalPubKey = localEc
393406 . keyFromPublic ( { x : X , y : Y } )
394407 . getPublic ( )
395- . add ( this . ec . keyFromPrivate ( nonce . toString ( 16 , 64 ) , "hex" ) . getPublic ( ) ) ;
408+ . add ( localEc . keyFromPrivate ( nonce . toString ( 16 , 64 ) , "hex" ) . getPublic ( ) ) ;
396409 } else if ( nonceResult . typeOfUser === "v2" ) {
397- finalPubKey = this . ec
410+ finalPubKey = localEc
398411 . keyFromPublic ( { x : X , y : Y } )
399412 . getPublic ( )
400- . add ( this . ec . keyFromPublic ( { x : nonceResult . pubNonce . x , y : nonceResult . pubNonce . y } ) . getPublic ( ) ) ;
413+ . add ( localEc . keyFromPublic ( { x : nonceResult . pubNonce . x , y : nonceResult . pubNonce . y } ) . getPublic ( ) ) ;
401414 pubNonce = { X : nonceResult . pubNonce . x , Y : nonceResult . pubNonce . y } ;
402415 } else {
403416 throw new Error ( "getOrSetNonce should always return typeOfUser." ) ;
404417 }
405418 } else {
406419 typeOfUser = "v1" ;
407420 nonce = await getMetadata ( this . legacyMetadataHost , { pub_key_X : X , pub_key_Y : Y } ) ;
408- finalPubKey = this . ec
421+ finalPubKey = localEc
409422 . keyFromPublic ( { x : X , y : Y } )
410423 . getPublic ( )
411- . add ( this . ec . keyFromPrivate ( nonce . toString ( 16 , 64 ) , "hex" ) . getPublic ( ) ) ;
424+ . add ( localEc . keyFromPrivate ( nonce . toString ( 16 , 64 ) , "hex" ) . getPublic ( ) ) ;
412425 }
413426
414427 if ( ! oAuthPubKey ) {
415428 throw new Error ( "Unable to derive oAuthPubKey" ) ;
416429 }
417430 const oAuthX = oAuthPubKey . getX ( ) . toString ( 16 , 64 ) ;
418431 const oAuthY = oAuthPubKey . getY ( ) . toString ( 16 , 64 ) ;
419- const oAuthAddress = generateAddressFromPubKey ( this . keyType , oAuthPubKey . getX ( ) , oAuthPubKey . getY ( ) ) ;
432+ const oAuthAddress = generateAddressFromPubKey ( localKeyType , oAuthPubKey . getX ( ) , oAuthPubKey . getY ( ) ) ;
420433
421434 if ( typeOfUser === "v2" && ! finalPubKey ) {
422435 throw new Error ( "Unable to derive finalPubKey" ) ;
423436 }
424437 const finalX = finalPubKey ? finalPubKey . getX ( ) . toString ( 16 , 64 ) : "" ;
425438 const finalY = finalPubKey ? finalPubKey . getY ( ) . toString ( 16 , 64 ) : "" ;
426- const finalAddress = finalPubKey ? generateAddressFromPubKey ( this . keyType , finalPubKey . getX ( ) , finalPubKey . getY ( ) ) : "" ;
439+ const finalAddress = finalPubKey ? generateAddressFromPubKey ( localKeyType , finalPubKey . getX ( ) , finalPubKey . getY ( ) ) : "" ;
427440 return {
428441 oAuthKeyData : {
429442 walletAddress : oAuthAddress ,
0 commit comments