-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: signer account page to enable MFA, deviceFactor recovery on login"
- Loading branch information
Showing
8 changed files
with
336 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import useMPC from '@/hooks/wallets/mpc/useMPC' | ||
import { Box, Button, Typography } from '@mui/material' | ||
import { COREKIT_STATUS } from '@web3auth/mpc-core-kit' | ||
import { getPubKeyPoint } from '@tkey-mpc/common-types' | ||
import useMFASettings from './useMFASettings' | ||
import { BN } from 'bn.js' | ||
import { useState } from 'react' | ||
|
||
const SignerAccountMFA = () => { | ||
const mpcCoreKit = useMPC() | ||
const mfaSettings = useMFASettings() | ||
|
||
const [enablingMFA, setEnablingMFA] = useState(false) | ||
|
||
const enableMFA = async () => { | ||
setEnablingMFA(true) | ||
if (!mpcCoreKit) { | ||
return | ||
} | ||
try { | ||
// First enable MFA in mpcCoreKit | ||
const recoveryFactor = await mpcCoreKit.enableMFA({}) | ||
|
||
// Then remove the recovery factor the mpcCoreKit creates | ||
const recoverKey = new BN(recoveryFactor, 'hex') | ||
const recoverPubKey = getPubKeyPoint(recoverKey) | ||
await mpcCoreKit.deleteFactor(recoverPubKey, recoverKey) | ||
} catch (error) { | ||
console.error(error) | ||
} finally { | ||
setEnablingMFA(false) | ||
} | ||
} | ||
|
||
if (mpcCoreKit?.status !== COREKIT_STATUS.LOGGED_IN) { | ||
return ( | ||
<Box> | ||
<Typography>You are currently not logged in through a social account</Typography> | ||
</Box> | ||
) | ||
} | ||
|
||
return ( | ||
<Box> | ||
{mfaSettings?.mfaEnabled ? ( | ||
<Typography>MFA is enabled!</Typography> | ||
) : ( | ||
<Button disabled={enablingMFA} onClick={enableMFA}> | ||
Enable MFA | ||
</Button> | ||
)} | ||
</Box> | ||
) | ||
} | ||
|
||
export default SignerAccountMFA |
32 changes: 32 additions & 0 deletions
32
src/components/settings/SignerAccountMFA/useMFASettings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import useMPC from '@/hooks/wallets/mpc/useMPC' | ||
import { COREKIT_STATUS } from '@web3auth/mpc-core-kit' | ||
|
||
export type MFASettings = { | ||
mfaEnabled: boolean | ||
} | null | ||
|
||
const useMFASettings = () => { | ||
const mpcCoreKit = useMPC() | ||
|
||
if (mpcCoreKit?.status !== COREKIT_STATUS.LOGGED_IN) { | ||
return null | ||
} | ||
|
||
const { shareDescriptions } = mpcCoreKit?.getKeyDetails() | ||
|
||
const hashedShareModuleFactor = Object.entries(shareDescriptions).find(([key, value]) => | ||
value[0]?.includes('hashedShare'), | ||
) | ||
|
||
if (hashedShareModuleFactor) { | ||
return { | ||
mfaEnabled: false, | ||
} | ||
} | ||
|
||
return { | ||
mfaEnabled: true, | ||
} | ||
} | ||
|
||
export default useMFASettings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import { act, renderHook, waitFor } from '@/tests/test-utils' | ||
import { MPCWalletState, useMPCWallet } from '../useMPCWallet' | ||
import * as useOnboard from '@/hooks/wallets/useOnboard' | ||
import { type OnboardAPI } from '@web3-onboard/core' | ||
import { COREKIT_STATUS, type UserInfo, type OauthLoginParams, type Web3AuthMPCCoreKit } from '@web3auth/mpc-core-kit' | ||
import * as mpcCoreKit from '@web3auth/mpc-core-kit' | ||
import { setMPCCoreKitInstance } from '../useMPC' | ||
import { ONBOARD_MPC_MODULE_LABEL } from '@/services/mpc/module' | ||
import { ethers } from 'ethers' | ||
import BN from 'bn.js' | ||
|
||
const MOCK_LOGIN_TIME = 1000 | ||
class MockMPCCoreKit { | ||
status: COREKIT_STATUS = COREKIT_STATUS.INITIALIZED | ||
state: { | ||
userInfo: UserInfo | undefined | ||
} = { | ||
userInfo: undefined, | ||
} | ||
|
||
private stateAfterLogin: COREKIT_STATUS | ||
private userInfoAfterLogin: UserInfo | undefined | ||
private expectedFactorKey: BN | ||
constructor(stateAfterLogin: COREKIT_STATUS, userInfoAfterLogin: UserInfo, expectedFactorKey: BN = new BN(-1)) { | ||
this.stateAfterLogin = stateAfterLogin | ||
this.userInfoAfterLogin = userInfoAfterLogin | ||
this.expectedFactorKey = expectedFactorKey | ||
} | ||
|
||
loginWithOauth(params: OauthLoginParams): Promise<void> { | ||
return new Promise((resolve) => { | ||
// Resolve after 1 sec | ||
setTimeout(() => { | ||
this.status = this.stateAfterLogin | ||
this.state.userInfo = this.userInfoAfterLogin | ||
resolve() | ||
}, 1000) | ||
}) | ||
} | ||
|
||
inputFactorKey(factorKey: BN) { | ||
if (factorKey.eq(this.expectedFactorKey)) { | ||
this.status = COREKIT_STATUS.LOGGED_IN | ||
return Promise.resolve() | ||
} else { | ||
Promise.reject() | ||
} | ||
} | ||
} | ||
|
||
describe('useMPCWallet', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers() | ||
}) | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
afterAll(() => { | ||
jest.useRealTimers() | ||
}) | ||
it('should have state NOT_INITIALIZED initially', () => { | ||
const { result } = renderHook(() => useMPCWallet()) | ||
expect(result.current.walletState).toBe(MPCWalletState.NOT_INITIALIZED) | ||
expect(result.current.userInfo.email).toBeUndefined() | ||
}) | ||
|
||
describe('triggerLogin', () => { | ||
it('should throw if Onboard is not initialized', () => { | ||
const { result } = renderHook(() => useMPCWallet()) | ||
expect(result.current.triggerLogin()).rejects.toEqual(new Error('Onboard is not initialized')) | ||
expect(result.current.walletState).toBe(MPCWalletState.NOT_INITIALIZED) | ||
}) | ||
|
||
it('should throw if MPC Core Kit is not initialized', () => { | ||
jest.spyOn(useOnboard, 'default').mockReturnValue({} as unknown as OnboardAPI) | ||
const { result } = renderHook(() => useMPCWallet()) | ||
|
||
expect(result.current.triggerLogin()).rejects.toEqual(new Error('MPC Core Kit is not initialized')) | ||
expect(result.current.walletState).toBe(MPCWalletState.NOT_INITIALIZED) | ||
}) | ||
|
||
it('should handle successful log in for SFA account', async () => { | ||
jest.spyOn(useOnboard, 'default').mockReturnValue({} as unknown as OnboardAPI) | ||
const connectWalletSpy = jest.fn().mockImplementation(() => Promise.resolve()) | ||
jest.spyOn(useOnboard, 'connectWallet').mockImplementation(connectWalletSpy) | ||
setMPCCoreKitInstance( | ||
new MockMPCCoreKit(COREKIT_STATUS.LOGGED_IN, { | ||
email: '[email protected]', | ||
name: 'Test', | ||
} as unknown as UserInfo) as unknown as Web3AuthMPCCoreKit, | ||
) | ||
const { result } = renderHook(() => useMPCWallet()) | ||
|
||
act(() => { | ||
result.current.triggerLogin() | ||
}) | ||
|
||
expect(result.current.walletState === MPCWalletState.AUTHENTICATING) | ||
expect(connectWalletSpy).not.toBeCalled() | ||
|
||
jest.advanceTimersByTime(MOCK_LOGIN_TIME) | ||
|
||
await waitFor(() => { | ||
expect(result.current.walletState === MPCWalletState.READY) | ||
expect(connectWalletSpy).toBeCalledWith(expect.anything(), { | ||
autoSelect: { | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
disableModals: true, | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
it('should handle successful log in for MFA account with device share', async () => { | ||
const mockDeviceFactor = ethers.Wallet.createRandom().privateKey.slice(2) | ||
jest.spyOn(useOnboard, 'default').mockReturnValue({} as unknown as OnboardAPI) | ||
const connectWalletSpy = jest.fn().mockImplementation(() => Promise.resolve()) | ||
jest.spyOn(useOnboard, 'connectWallet').mockImplementation(connectWalletSpy) | ||
setMPCCoreKitInstance( | ||
new MockMPCCoreKit( | ||
COREKIT_STATUS.REQUIRED_SHARE, | ||
{ | ||
email: '[email protected]', | ||
name: 'Test', | ||
} as unknown as UserInfo, | ||
new BN(mockDeviceFactor, 'hex'), | ||
) as unknown as Web3AuthMPCCoreKit, | ||
) | ||
|
||
jest.spyOn(mpcCoreKit, 'getWebBrowserFactor').mockReturnValue(Promise.resolve(mockDeviceFactor)) | ||
|
||
const { result } = renderHook(() => useMPCWallet()) | ||
|
||
act(() => { | ||
result.current.triggerLogin() | ||
}) | ||
|
||
expect(result.current.walletState === MPCWalletState.AUTHENTICATING) | ||
expect(connectWalletSpy).not.toBeCalled() | ||
|
||
jest.advanceTimersByTime(MOCK_LOGIN_TIME) | ||
|
||
await waitFor(() => { | ||
expect(result.current.walletState === MPCWalletState.READY) | ||
expect(connectWalletSpy).toBeCalledWith(expect.anything(), { | ||
autoSelect: { | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
disableModals: true, | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('resetAccount', () => { | ||
it('should throw if mpcCoreKit is not initialized', () => { | ||
const { result } = renderHook(() => useMPCWallet()) | ||
expect(result.current.resetAccount()).rejects.toEqual( | ||
new Error('MPC Core Kit is not initialized or the user is not logged in'), | ||
) | ||
}) | ||
it('should reset an account by overwriting the metadata', async () => { | ||
const mockSetMetadata = jest.fn() | ||
const mockMPCCore = { | ||
metadataKey: ethers.Wallet.createRandom().privateKey.slice(2), | ||
state: { | ||
userInfo: undefined, | ||
}, | ||
tKey: { | ||
storageLayer: { | ||
setMetadata: mockSetMetadata, | ||
}, | ||
}, | ||
} | ||
|
||
setMPCCoreKitInstance(mockMPCCore as unknown as Web3AuthMPCCoreKit) | ||
|
||
const { result } = renderHook(() => useMPCWallet()) | ||
|
||
await result.current.resetAccount() | ||
|
||
expect(mockSetMetadata).toHaveBeenCalledWith({ | ||
privKey: new BN(mockMPCCore.metadataKey, 'hex'), | ||
input: { message: 'KEY_NOT_FOUND' }, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.