Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: isMFAEnabled sync #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export interface Web3AuthState {
tssShareIndex?: number;
tssPubKey?: Buffer;
factorKey?: BN;
isMFAEnabled?: boolean;
}

export interface ICoreKit {
Expand Down Expand Up @@ -382,6 +383,7 @@ export interface SessionData {
tssPubKey: string;
signatures: string[];
userInfo: UserInfo;
isMFAEnabled: boolean;
}

export interface TkeyLocalStoreData {
Expand Down
67 changes: 44 additions & 23 deletions src/mpcCoreKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
this.checkReady();

Expand Down Expand Up @@ -481,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 });
Expand Down Expand Up @@ -769,6 +776,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
if (!this.state.oAuthKey) {
throw new Error("user not logged in");
}
this.updateState({ isMFAEnabled: false });
const existingUser = await this.isMetadataPresent(this.state.oAuthKey);

if (!existingUser) {
Expand Down Expand Up @@ -807,6 +815,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
await this.tKey.inputShareStoreSafe(factorKeyMetadata, true);
await this.tKey.reconstructKey();
await this.finalizeTkey(hashedFactorKey);
} else {
this.updateState({ isMFAEnabled: true });
}
}
}
Expand Down Expand Up @@ -849,14 +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,
});
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);
}
Expand All @@ -866,27 +880,35 @@ 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, 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,
};
const payload: SessionData = this.createSessionData();

await this.sessionManager.createSession(payload);
this.currentStorage.set("sessionId", sessionId);
} catch (err) {
log.error("error creating session", err);
}
}

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 });
Expand Down Expand Up @@ -945,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;
}

Expand Down Expand Up @@ -1032,8 +1049,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
this.privKeyProvider = signingProvider;
}

private updateState(newState: Partial<Web3AuthState>): void {
private updateState(newState: Partial<Web3AuthState>, 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 {
Expand Down