From 97acac3943082801850c48eba6bd9bca467bd118 Mon Sep 17 00:00:00 2001 From: ieow Date: Mon, 4 Dec 2023 17:35:37 +0800 Subject: [PATCH 1/3] feat: isMFAEnabled sync --- src/interfaces.ts | 2 ++ src/mpcCoreKit.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 2a77d3af..e0db789f 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -136,6 +136,7 @@ export interface Web3AuthState { tssShareIndex?: number; tssPubKey?: Buffer; factorKey?: BN; + isMFAEnabled?: boolean; } export interface ICoreKit { @@ -382,6 +383,7 @@ export interface SessionData { tssPubKey: string; signatures: string[]; userInfo: UserInfo; + isMFAEnabled: boolean; } export interface TkeyLocalStoreData { diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 90956ee0..5f5314df 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -445,6 +445,11 @@ export class Web3AuthMPCCoreKit implements ICoreKit { return this.tKey.getTSSPub(); } + public isMFAEnabled(): boolean { + this.checkReady(); + return !!this.state.isMFAEnabled; + } + public async enableMFA(enableMFAParams: EnableMFAParams, recoveryFactor = true): Promise { this.checkReady(); @@ -769,6 +774,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { if (!this.state.oAuthKey) { throw new Error("user not logged in"); } + this.updateState({ isMFAEnabled: true }); const existingUser = await this.isMetadataPresent(this.state.oAuthKey); if (!existingUser) { @@ -807,6 +813,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit { await this.tKey.inputShareStoreSafe(factorKeyMetadata, true); await this.tKey.reconstructKey(); await this.finalizeTkey(hashedFactorKey); + } else { + this.updateState({ isMFAEnabled: false }); } } } @@ -856,6 +864,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { tssPubKey: Buffer.from(result.tssPubKey.padStart(FIELD_ELEMENT_HEX_LEN, "0"), "hex"), signatures: result.signatures, userInfo: result.userInfo, + isMFAEnabled: result.isMFAEnabled, }); } catch (err) { log.error("error trying to authorize session", err); @@ -866,7 +875,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { try { const sessionId = OpenloginSessionManager.generateRandomSessionKey(); this.sessionManager.sessionId = sessionId; - const { oAuthKey, factorKey, userInfo, tssShareIndex, tssPubKey } = this.state; + const { oAuthKey, factorKey, userInfo, tssShareIndex, tssPubKey, isMFAEnabled } = this.state; if (!this.state.factorKey) throw new Error("factorKey not present"); const { tssShare } = await this.tKey.getTSSShare(this.state.factorKey); if (!oAuthKey || !factorKey || !tssShare || !tssPubKey || !userInfo) { @@ -879,6 +888,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { tssPubKey: Buffer.from(tssPubKey).toString("hex"), signatures: this.signatures, userInfo, + isMFAEnabled, }; await this.sessionManager.createSession(payload); this.currentStorage.set("sessionId", sessionId); From 7a357e42ba01126d872262d73c2ff16d64e8d9cd Mon Sep 17 00:00:00 2001 From: ieow Date: Mon, 4 Dec 2023 17:48:10 +0800 Subject: [PATCH 2/3] fix: correct flag value --- src/mpcCoreKit.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 5f5314df..282405a7 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -774,7 +774,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { if (!this.state.oAuthKey) { throw new Error("user not logged in"); } - this.updateState({ isMFAEnabled: true }); + this.updateState({ isMFAEnabled: false }); const existingUser = await this.isMetadataPresent(this.state.oAuthKey); if (!existingUser) { @@ -814,7 +814,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit { await this.tKey.reconstructKey(); await this.finalizeTkey(hashedFactorKey); } else { - this.updateState({ isMFAEnabled: false }); + this.updateState({ isMFAEnabled: true }); } } } From 16660c6642a3f68c10ccef369c8000c099e6de04 Mon Sep 17 00:00:00 2001 From: ieow Date: Mon, 4 Dec 2023 18:16:03 +0800 Subject: [PATCH 3/3] fix: updateState -> update session --- src/mpcCoreKit.ts | 61 ++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/mpcCoreKit.ts b/src/mpcCoreKit.ts index 282405a7..0bddf29c 100644 --- a/src/mpcCoreKit.ts +++ b/src/mpcCoreKit.ts @@ -486,6 +486,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit { await this.deleteFactor(hashedFactorPub, hashedFactorKey); await this.deleteMetadataShareBackup(hashedFactorKey); + this.updateState({ isMFAEnabled: true }); + // only recovery factor = true if (recoveryFactor) { const backupFactorKey = await this.createFactor({ shareType: TssShareType.RECOVERY, ...enableMFAParams }); @@ -857,15 +859,18 @@ export class Web3AuthMPCCoreKit implements ICoreKit { await this.tKey.inputShareStoreSafe(factorKeyMetadata, true); await this.tKey.reconstructKey(); - this.updateState({ - factorKey: new BN(result.factorKey, "hex"), - oAuthKey: result.oAuthKey, - tssShareIndex: result.tssShareIndex, - tssPubKey: Buffer.from(result.tssPubKey.padStart(FIELD_ELEMENT_HEX_LEN, "0"), "hex"), - signatures: result.signatures, - userInfo: result.userInfo, - isMFAEnabled: result.isMFAEnabled, - }); + this.updateState( + { + factorKey: new BN(result.factorKey, "hex"), + oAuthKey: result.oAuthKey, + tssShareIndex: result.tssShareIndex, + tssPubKey: Buffer.from(result.tssPubKey.padStart(FIELD_ELEMENT_HEX_LEN, "0"), "hex"), + signatures: result.signatures, + userInfo: result.userInfo, + isMFAEnabled: result.isMFAEnabled, + }, + false + ); } catch (err) { log.error("error trying to authorize session", err); } @@ -875,21 +880,14 @@ export class Web3AuthMPCCoreKit implements ICoreKit { try { const sessionId = OpenloginSessionManager.generateRandomSessionKey(); this.sessionManager.sessionId = sessionId; - const { oAuthKey, factorKey, userInfo, tssShareIndex, tssPubKey, isMFAEnabled } = this.state; + const { oAuthKey, factorKey, userInfo, tssPubKey } = this.state; if (!this.state.factorKey) throw new Error("factorKey not present"); const { tssShare } = await this.tKey.getTSSShare(this.state.factorKey); if (!oAuthKey || !factorKey || !tssShare || !tssPubKey || !userInfo) { throw new Error("User not logged in"); } - const payload: SessionData = { - oAuthKey, - factorKey: factorKey?.toString("hex"), - tssShareIndex: tssShareIndex as number, - tssPubKey: Buffer.from(tssPubKey).toString("hex"), - signatures: this.signatures, - userInfo, - isMFAEnabled, - }; + const payload: SessionData = this.createSessionData(); + await this.sessionManager.createSession(payload); this.currentStorage.set("sessionId", sessionId); } catch (err) { @@ -897,6 +895,20 @@ export class Web3AuthMPCCoreKit implements ICoreKit { } } + private createSessionData(): SessionData { + const { oAuthKey, factorKey, userInfo, tssShareIndex, tssPubKey, isMFAEnabled } = this.state; + const payload: SessionData = { + oAuthKey, + factorKey: factorKey?.toString("hex"), + tssShareIndex: tssShareIndex as number, + tssPubKey: Buffer.from(tssPubKey).toString("hex"), + signatures: this.signatures, + userInfo, + isMFAEnabled, + }; + return payload; + } + private async isMetadataPresent(privateKey: string) { const privateKeyBN = new BN(privateKey, "hex"); const metadata = await this.tKey?.storageLayer.getMetadata<{ message: string }>({ privKey: privateKeyBN }); @@ -955,11 +967,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit { // Generate new share. await addFactorAndRefresh(this.tKey, newFactorPub, newFactorTSSIndex, this.state.factorKey, this.signatures); - // Update local share. - const { tssIndex } = await this.tKey.getTSSShare(this.state.factorKey); - this.updateState({ - tssShareIndex: tssIndex, - }); return; } @@ -1042,8 +1049,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit { this.privKeyProvider = signingProvider; } - private updateState(newState: Partial): void { + private updateState(newState: Partial, updateSession = true): void { this.state = { ...this.state, ...newState }; + if (this.sessionManager.sessionId && updateSession) { + const payload: SessionData = this.createSessionData(); + this.sessionManager.updateSession(payload); + } } private resetState(): void {