From dc161a790c9462ce349a5a33f04e4c08e02e14d1 Mon Sep 17 00:00:00 2001 From: ieow Date: Tue, 12 Mar 2024 15:52:27 +0800 Subject: [PATCH 1/4] feat: add enableMFA in to the flow sequence --- demo/redirect-flow-example/package-lock.json | 2 +- demo/redirect-flow-example/src/App.tsx | 23 ++++++++++-- demo/redirect-flow-example/src/flow.ts | 39 ++++++++++++++++---- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/demo/redirect-flow-example/package-lock.json b/demo/redirect-flow-example/package-lock.json index 199179dd..b89eea7e 100644 --- a/demo/redirect-flow-example/package-lock.json +++ b/demo/redirect-flow-example/package-lock.json @@ -47,7 +47,7 @@ }, "../..": { "name": "@web3auth/mpc-core-kit", - "version": "2.1.0", + "version": "2.2.0", "license": "ISC", "dependencies": { "@metamask/swappable-obj-proxy": "^2.1.0", diff --git a/demo/redirect-flow-example/src/App.tsx b/demo/redirect-flow-example/src/App.tsx index 50662cf3..1819d793 100644 --- a/demo/redirect-flow-example/src/App.tsx +++ b/demo/redirect-flow-example/src/App.tsx @@ -164,12 +164,23 @@ function App() { setProvider(coreKitInstance.provider); } else { - coreKitInstance.setupProvider({ chainConfig: DEFAULT_CHAIN_CONFIG }).then((provider) => { + if (coreKitInstance.status === COREKIT_STATUS.LOGGED_IN) { + coreKitInstance.setupProvider({ chainConfig: DEFAULT_CHAIN_CONFIG }).then((provider) => { - setProvider(coreKitInstance.provider); - }); + setProvider(coreKitInstance.provider); + }); + } } setCoreKitStatus(coreKitInstance.status); + + + try { + let result = securityQuestion.getQuestion(coreKitInstance!); + setQuestion(result); + } catch (e) { + setQuestion(undefined); + uiConsole(e); + } } catch (error: unknown) { console.error(error); } @@ -225,7 +236,11 @@ function App() { throw new Error("backupFactorKey not found"); } const factorKey = new BN(backupFactorKey, "hex") + console.log(factorKey) await coreKitInstance.inputFactorKey(factorKey); + console.log(coreKitInstance) + + setCoreKitStatus(coreKitInstance.status); if (coreKitInstance.status === COREKIT_STATUS.REQUIRED_SHARE) { uiConsole("required more shares even after inputing backup factor key, please enter your backup/ device factor key, or reset account [unrecoverable once reset, please use it with caution]"); @@ -233,6 +248,8 @@ function App() { if (coreKitInstance.provider) { setProvider(coreKitInstance.provider); + } else if (coreKitInstance.status === COREKIT_STATUS.LOGGED_IN) { + await coreKitInstance.setupProvider({ chainConfig: DEFAULT_CHAIN_CONFIG }) } } diff --git a/demo/redirect-flow-example/src/flow.ts b/demo/redirect-flow-example/src/flow.ts index 4e277318..d705a156 100644 --- a/demo/redirect-flow-example/src/flow.ts +++ b/demo/redirect-flow-example/src/flow.ts @@ -1,27 +1,36 @@ -import { WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit, TssSecurityQuestion } from "@web3auth/mpc-core-kit"; +import { getPubKeyPoint } from "@tkey-mpc/common-types"; +import { WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit, TssSecurityQuestion, getHashedPrivateKey } from "@web3auth/mpc-core-kit"; +import BN from "bn.js"; export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, manualSync: boolean, setupProviderOnInit: boolean, verifier: string, verifierId: string, idToken: string }) => { - const startTime = Date.now(); - console.log("startTime", startTime); + const initStart = Date.now(); + console.log("initStart", initStart); + const web3AuthClientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; const coreKitInstance = new Web3AuthMPCCoreKit( { - web3AuthClientId: 'BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ', + web3AuthClientId, web3AuthNetwork: params.selectedNetwork, uxMode: 'redirect', manualSync: params.manualSync, setupProviderOnInit: params.setupProviderOnInit, } ); - + // init can be called before login is triggered, should it is excluded from the login time await coreKitInstance.init({ handleRedirectResult: false, rehydrate: false }); + const startTime = Date.now(); + console.log("done init", startTime); + + + console.log("init", startTime - initStart); + console.log("startTime", startTime); await coreKitInstance.loginWithJWT({ verifier: params.verifier, verifierId: params.verifierId, idToken: params.idToken, - }, { prefetchTssPublicKeys: 2 }); + }, { prefetchTssPublicKeys: 3}); let loggedInTime = Date.now(); console.log("logged Time :", loggedInTime); @@ -39,13 +48,27 @@ export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, man console.log("SQ time", SqFactorTime); console.log(SqFactorTime - loggedInTime); + let securityFactor = await securityQuestion.recoverFactor(coreKitInstance, "answer"); + + + // update current Factor key with security factor + await coreKitInstance.inputFactorKey(new BN(securityFactor, "hex")); + // enable mfa ( by deleting Hash Factor ) + const hashFactor = getHashedPrivateKey(coreKitInstance.state.oAuthKey!, web3AuthClientId) + const hashedFactorPub = getPubKeyPoint(hashFactor); + await coreKitInstance.deleteFactor(hashedFactorPub, hashFactor); + + let deletedFactorTime = Date.now(); + console.log("deleted hash factor (enableMFA)", deletedFactorTime); + console.log(deletedFactorTime - SqFactorTime); + await coreKitInstance.commitChanges(); let commitTime = Date.now(); console.log("commit :", commitTime); - console.log(commitTime - SqFactorTime); + console.log(commitTime - deletedFactorTime); - console.log("total time", commitTime - startTime); + console.log("total login time", commitTime - startTime); } \ No newline at end of file From e246d49ff30166d0f8f2f4a94df436c262d114d0 Mon Sep 17 00:00:00 2001 From: ieow Date: Tue, 12 Mar 2024 18:36:51 +0800 Subject: [PATCH 2/4] feat: mfa setup for flow test --- demo/redirect-flow-example/src/flow.ts | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/demo/redirect-flow-example/src/flow.ts b/demo/redirect-flow-example/src/flow.ts index d705a156..a8ea9c3d 100644 --- a/demo/redirect-flow-example/src/flow.ts +++ b/demo/redirect-flow-example/src/flow.ts @@ -1,6 +1,5 @@ -import { getPubKeyPoint } from "@tkey-mpc/common-types"; -import { WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit, TssSecurityQuestion, getHashedPrivateKey } from "@web3auth/mpc-core-kit"; +import { WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit, TssSecurityQuestion } from "@web3auth/mpc-core-kit"; import BN from "bn.js"; export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, manualSync: boolean, setupProviderOnInit: boolean, verifier: string, verifierId: string, idToken: string }) => { @@ -15,6 +14,7 @@ export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, man uxMode: 'redirect', manualSync: params.manualSync, setupProviderOnInit: params.setupProviderOnInit, + disableHashedFactorKey: true, } ); // init can be called before login is triggered, should it is excluded from the login time @@ -48,25 +48,11 @@ export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, man console.log("SQ time", SqFactorTime); console.log(SqFactorTime - loggedInTime); - let securityFactor = await securityQuestion.recoverFactor(coreKitInstance, "answer"); - - - // update current Factor key with security factor - await coreKitInstance.inputFactorKey(new BN(securityFactor, "hex")); - // enable mfa ( by deleting Hash Factor ) - const hashFactor = getHashedPrivateKey(coreKitInstance.state.oAuthKey!, web3AuthClientId) - const hashedFactorPub = getPubKeyPoint(hashFactor); - await coreKitInstance.deleteFactor(hashedFactorPub, hashFactor); - - let deletedFactorTime = Date.now(); - console.log("deleted hash factor (enableMFA)", deletedFactorTime); - console.log(deletedFactorTime - SqFactorTime); - await coreKitInstance.commitChanges(); let commitTime = Date.now(); console.log("commit :", commitTime); - console.log(commitTime - deletedFactorTime); + console.log(commitTime - SqFactorTime); console.log("total login time", commitTime - startTime); From c15b83d614efd6eef37ceda603546939261fa5a3 Mon Sep 17 00:00:00 2001 From: ieow Date: Tue, 12 Mar 2024 18:42:40 +0800 Subject: [PATCH 3/4] feat: reduce prefetch --- demo/redirect-flow-example/src/flow.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/redirect-flow-example/src/flow.ts b/demo/redirect-flow-example/src/flow.ts index a8ea9c3d..a196a14e 100644 --- a/demo/redirect-flow-example/src/flow.ts +++ b/demo/redirect-flow-example/src/flow.ts @@ -30,7 +30,7 @@ export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, man verifier: params.verifier, verifierId: params.verifierId, idToken: params.idToken, - }, { prefetchTssPublicKeys: 3}); + }, { prefetchTssPublicKeys: 2}); let loggedInTime = Date.now(); console.log("logged Time :", loggedInTime); From 2dd92b8cc95e1e4adf77d5f995c5754331be6cd5 Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 15 Mar 2024 12:11:10 +0800 Subject: [PATCH 4/4] feat: add relogin --- demo/redirect-flow-example/src/App.tsx | 3 +-- demo/redirect-flow-example/src/flow.ts | 33 +++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/demo/redirect-flow-example/src/App.tsx b/demo/redirect-flow-example/src/App.tsx index 1819d793..c48b2103 100644 --- a/demo/redirect-flow-example/src/App.tsx +++ b/demo/redirect-flow-example/src/App.tsx @@ -191,8 +191,7 @@ function App() { if (!mockEmail) { throw new Error('mockEmail not found'); } - const { idToken, parsedToken } = await mockLogin(mockEmail); - await flow({ selectedNetwork, manualSync: true, setupProviderOnInit: false, verifier: 'torus-test-health', verifierId: parsedToken.email, idToken }); + await flow({ selectedNetwork, manualSync: true, setupProviderOnInit: false, verifier: 'torus-test-health', verifierId: mockEmail }); } catch (error: unknown) { console.error(error); diff --git a/demo/redirect-flow-example/src/flow.ts b/demo/redirect-flow-example/src/flow.ts index a196a14e..8704ce82 100644 --- a/demo/redirect-flow-example/src/flow.ts +++ b/demo/redirect-flow-example/src/flow.ts @@ -1,9 +1,10 @@ import { WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit, TssSecurityQuestion } from "@web3auth/mpc-core-kit"; import BN from "bn.js"; +import { mockLogin } from "./App"; -export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, manualSync: boolean, setupProviderOnInit: boolean, verifier: string, verifierId: string, idToken: string }) => { - +export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, manualSync: boolean, setupProviderOnInit: boolean, verifier: string, verifierId: string }) => { + const { idToken } = await mockLogin(params.verifierId); const initStart = Date.now(); console.log("initStart", initStart); const web3AuthClientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; @@ -29,7 +30,7 @@ export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, man await coreKitInstance.loginWithJWT({ verifier: params.verifier, verifierId: params.verifierId, - idToken: params.idToken, + idToken: idToken, }, { prefetchTssPublicKeys: 2}); let loggedInTime = Date.now(); @@ -55,6 +56,32 @@ export const flow = async (params: { selectedNetwork: WEB3AUTH_NETWORK_TYPE, man console.log(commitTime - SqFactorTime); console.log("total login time", commitTime - startTime); + + + + // relogin on new instance ( device ) + const instance2 = new Web3AuthMPCCoreKit( + { + web3AuthClientId, + web3AuthNetwork: params.selectedNetwork, + uxMode: 'redirect', + manualSync: params.manualSync, + setupProviderOnInit: params.setupProviderOnInit, + disableHashedFactorKey: true, + } + ); + + const { idToken: idToken2 } = await mockLogin(params.verifierId); + await instance2.init({ handleRedirectResult: false, rehydrate: false }); + await instance2.loginWithJWT({ + verifier: params.verifier, + verifierId: params.verifierId, + idToken: idToken2, + }); + const sqInstance2 = new TssSecurityQuestion(); + const sqFactor = await sqInstance2.recoverFactor(instance2, "answer"); + await instance2.inputFactorKey(new BN(sqFactor, "hex")); + console.log(instance2.status); } \ No newline at end of file