@@ -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
@@ -1088,6 +1102,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
10881102
10891103 async setupRemoteSigning ( params : IRemoteClientState , rehydrate : boolean = false ) : Promise < void > {
10901104 const { remoteFactorPub, metadataShare } = params ;
1105+
1106+ // rehydrate session
1107+ if ( rehydrate ) {
1108+ this . updateState ( { remoteClient : params } ) ;
1109+ const sessionResult = await this . sessionManager . authorizeSession ( ) . catch ( async ( err ) => {
1110+ log . error ( "rehydrate session error" , err ) ;
1111+ } ) ;
1112+ if ( sessionResult ) {
1113+ await this . rehydrateSession ( sessionResult ) ;
1114+ }
1115+ }
1116+
10911117 const details = this . getKeyDetails ( ) . shareDescriptions [ remoteFactorPub ] ;
10921118 if ( ! details ) throw CoreKitError . default ( "factor description not found" ) ;
10931119
@@ -1225,7 +1251,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
12251251 tssPubKey : Buffer . from ( tssPubKey ) . toString ( "hex" ) ,
12261252 signatures : await this . getSessionSignatures ( ) ,
12271253 userInfo,
1228- remoteClientState : this . state . remoteClient ,
12291254 } ;
12301255 await this . sessionManager . createSession ( payload ) ;
12311256 // to accommodate async storage
@@ -1364,7 +1389,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13641389 }
13651390
13661391 const factorKey = new BN ( result . factorKey , "hex" ) ;
1367- if ( ! factorKey && ! result . remoteClientState ? .metadataShare ) {
1392+ if ( ! result . factorKey && ! this . state . remoteClient . metadataShare ) {
13681393 throw CoreKitError . providedFactorKeyInvalid ( ) ;
13691394 }
13701395
@@ -1374,13 +1399,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13741399
13751400 await this . tKey . initialize ( { neverInitializeNewKey : true } ) ;
13761401
1377- // skip input share store if factor key is not present
1378- // tkey will be at state initalized
1379- if ( ! result . factorKey ) {
1380- return ;
1381- }
1402+ const metadataShareStore = this . state . remoteClient ?. metadataShare
1403+ ? ShareStore . fromJSON ( JSON . parse ( this . state . remoteClient . metadataShare ) )
1404+ : await this . getFactorKeyMetadata ( factorKey ) ;
13821405
1383- const metadataShareStore = await this . getFactorKeyMetadata ( factorKey ) ;
13841406 await this . tKey . inputShareStoreSafe ( metadataShareStore , true ) ;
13851407 await this . tKey . reconstructKey ( ) ;
13861408
@@ -1392,7 +1414,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit, IMPCContext {
13921414 tssPubKey : this . tkey . getTSSPub ( ) . toSEC1 ( this . tKey . tssCurve , false ) ,
13931415 signatures : result . signatures ,
13941416 userInfo : result . userInfo ,
1395- remoteClient : result . remoteClientState ,
13961417 } ) ;
13971418 } catch ( err ) {
13981419 log . warn ( "failed to authorize session" , err ) ;
0 commit comments