@@ -49,8 +49,6 @@ import {
4949 CreateFactorParams ,
5050 EnableMFAParams ,
5151 ICoreKit ,
52- ICustomDklsSignParams ,
53- ICustomFrostSignParams ,
5452 IFactorKey ,
5553 IMPCContext ,
5654 InitParams ,
@@ -71,6 +69,7 @@ import {
7169 Web3AuthState ,
7270} from "./interfaces" ;
7371import { DefaultSessionSigGeneratorPlugin } from "./plugins/DefaultSessionSigGenerator" ;
72+ import { ICustomDklsSignParams , ICustomFrostSignParams } from "./plugins/IRemoteSigner" ;
7473import { ISessionSigGenerator } from "./plugins/ISessionSigGenerator" ;
7574import {
7675 deriveShareCoefficients ,
@@ -732,6 +731,20 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
732731 return ed25519 ( ) . keyFromPublic ( p ) . getPublic ( ) ;
733732 }
734733
734+ /**
735+ * Get public key in bip340 format.
736+ *
737+ * Throws an error if signature type is not bip340.
738+ */
739+ public getPubKeyBip340 ( ) : Buffer {
740+ if ( this . _sigType !== "bip340" ) {
741+ throw CoreKitError . default ( `getPubKeyBip340 not supported for signature type ${ this . sigType } ` ) ;
742+ }
743+
744+ const p = this . tkey . tssCurve . keyFromPublic ( this . getPubKey ( ) ) . getPublic ( ) ;
745+ return p . getX ( ) . toBuffer ( "be" , 32 ) ;
746+ }
747+
735748 public async preSetupSigning ( ) : Promise < ICustomDklsSignParams > {
736749 const { torusNodeTSSEndpoints } = fetchLocalConfig ( this . options . web3AuthNetwork , this . keyType ) ;
737750
@@ -742,7 +755,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
742755 }
743756
744757 const tssNonce = this . getTssNonce ( ) || 0 ;
745-
746758 const vid = `${ this . verifier } ${ DELIMITERS . Delimiter1 } ${ this . verifierId } ` ;
747759 const sessionId = `${ vid } ${ DELIMITERS . Delimiter2 } default${ DELIMITERS . Delimiter3 } ${ tssNonce } ${ DELIMITERS . Delimiter4 } ` ;
748760
@@ -764,7 +776,9 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
764776 nodeIndexesReturned : participatingServerDKGIndexes ,
765777 } = generateTSSEndpoints ( torusNodeTSSEndpoints , parties , clientIndex , nodeIndexes ) ;
766778
767- const factor = Point . fromSEC1 ( secp256k1 , this . state . remoteClient ?. remoteFactorPub ) ;
779+ const factor = this . state . remoteClient ?. remoteFactorPub
780+ ? Point . fromSEC1 ( secp256k1 , this . state . remoteClient ?. remoteFactorPub )
781+ : Point . fromScalar ( this . state . factorKey , secp256k1 ) ;
768782 const factorEnc = this . tKey . getFactorEncs ( factor ) ;
769783
770784 // Compute account nonce only supported for secp256k1
@@ -1061,6 +1075,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
10611075
10621076 async setupRemoteSigning ( params : IRemoteClientState , rehydrate : boolean = false ) : Promise < void > {
10631077 const { remoteFactorPub, metadataShare } = params ;
1078+
1079+ // rehydrate session
1080+ if ( rehydrate ) {
1081+ this . updateState ( { remoteClient : params } ) ;
1082+ const sessionResult = await this . sessionManager . authorizeSession ( ) . catch ( async ( err ) => {
1083+ log . error ( "rehydrate session error" , err ) ;
1084+ } ) ;
1085+ if ( sessionResult ) {
1086+ await this . rehydrateSession ( sessionResult ) ;
1087+ }
1088+ }
1089+
10641090 const details = this . getKeyDetails ( ) . shareDescriptions [ remoteFactorPub ] ;
10651091 if ( ! details ) throw CoreKitError . default ( "factor description not found" ) ;
10661092
@@ -1198,7 +1224,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
11981224 tssPubKey : Buffer . from ( tssPubKey ) . toString ( "hex" ) ,
11991225 signatures : await this . getSessionSignatures ( ) ,
12001226 userInfo,
1201- remoteClientState : this . state . remoteClient ,
12021227 } ;
12031228 await this . sessionManager . createSession ( payload ) ;
12041229 // to accommodate async storage
@@ -1337,7 +1362,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13371362 }
13381363
13391364 const factorKey = new BN ( result . factorKey , "hex" ) ;
1340- if ( ! factorKey && ! result . remoteClientState ? .metadataShare ) {
1365+ if ( ! result . factorKey && ! this . state . remoteClient . metadataShare ) {
13411366 throw CoreKitError . providedFactorKeyInvalid ( ) ;
13421367 }
13431368
@@ -1347,13 +1372,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13471372
13481373 await this . tKey . initialize ( { neverInitializeNewKey : true } ) ;
13491374
1350- // skip input share store if factor key is not present
1351- // tkey will be at state initalized
1352- if ( ! result . factorKey ) {
1353- return ;
1354- }
1375+ const metadataShareStore = this . state . remoteClient ?. metadataShare
1376+ ? ShareStore . fromJSON ( JSON . parse ( this . state . remoteClient . metadataShare ) )
1377+ : await this . getFactorKeyMetadata ( factorKey ) ;
13551378
1356- const metadataShareStore = await this . getFactorKeyMetadata ( factorKey ) ;
13571379 await this . tKey . inputShareStoreSafe ( metadataShareStore , true ) ;
13581380 await this . tKey . reconstructKey ( ) ;
13591381
@@ -1365,7 +1387,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13651387 tssPubKey : this . tkey . getTSSPub ( ) . toSEC1 ( this . tKey . tssCurve , false ) ,
13661388 signatures : result . signatures ,
13671389 userInfo : result . userInfo ,
1368- remoteClient : result . remoteClientState ,
13691390 } ) ;
13701391 } catch ( err ) {
13711392 log . warn ( "failed to authorize session" , err ) ;
0 commit comments