-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor, remove console.logs, new test
- Loading branch information
Showing
8 changed files
with
243 additions
and
147 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
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 |
---|---|---|
|
@@ -2,7 +2,13 @@ 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 { | ||
COREKIT_STATUS, | ||
type UserInfo, | ||
type OauthLoginParams, | ||
type Web3AuthMPCCoreKit, | ||
type TssSecurityQuestion, | ||
} 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' | ||
|
@@ -54,6 +60,7 @@ describe('useMPCWallet', () => { | |
}) | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
setMPCCoreKitInstance(undefined) | ||
}) | ||
afterAll(() => { | ||
jest.useRealTimers() | ||
|
@@ -98,7 +105,9 @@ describe('useMPCWallet', () => { | |
expect(result.current.walletState === MPCWalletState.AUTHENTICATING) | ||
expect(connectWalletSpy).not.toBeCalled() | ||
|
||
jest.advanceTimersByTime(MOCK_LOGIN_TIME) | ||
act(() => { | ||
jest.advanceTimersByTime(MOCK_LOGIN_TIME) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.current.walletState === MPCWalletState.READY) | ||
|
@@ -137,8 +146,9 @@ describe('useMPCWallet', () => { | |
|
||
expect(result.current.walletState === MPCWalletState.AUTHENTICATING) | ||
expect(connectWalletSpy).not.toBeCalled() | ||
|
||
jest.advanceTimersByTime(MOCK_LOGIN_TIME) | ||
act(() => { | ||
jest.advanceTimersByTime(MOCK_LOGIN_TIME) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.current.walletState === MPCWalletState.READY) | ||
|
@@ -150,6 +160,42 @@ describe('useMPCWallet', () => { | |
}) | ||
}) | ||
}) | ||
|
||
it('should require manual share for MFA account without device share', 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.REQUIRED_SHARE, { | ||
email: '[email protected]', | ||
name: 'Test', | ||
} as unknown as UserInfo) as unknown as Web3AuthMPCCoreKit, | ||
) | ||
|
||
// TODO: remove unnecessary cast if mpc core sdk gets updated | ||
jest.spyOn(mpcCoreKit, 'getWebBrowserFactor').mockReturnValue(Promise.resolve(undefined as unknown as string)) | ||
jest.spyOn(mpcCoreKit, 'TssSecurityQuestion').mockReturnValue({ | ||
getQuestion: () => 'SOME RANDOM QUESTION', | ||
} as unknown as TssSecurityQuestion) | ||
|
||
const { result } = renderHook(() => useMPCWallet()) | ||
|
||
act(() => { | ||
result.current.triggerLogin() | ||
}) | ||
|
||
expect(result.current.walletState === MPCWalletState.AUTHENTICATING) | ||
expect(connectWalletSpy).not.toBeCalled() | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(MOCK_LOGIN_TIME) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.current.walletState === MPCWalletState.MANUAL_RECOVERY) | ||
expect(connectWalletSpy).not.toBeCalled() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('resetAccount', () => { | ||
|
@@ -185,4 +231,66 @@ describe('useMPCWallet', () => { | |
}) | ||
}) | ||
}) | ||
|
||
describe('recoverFactorWithPassword', () => { | ||
it('should throw if mpcCoreKit is not initialized', () => { | ||
const { result } = renderHook(() => useMPCWallet()) | ||
expect(result.current.recoverFactorWithPassword('test', false)).rejects.toEqual( | ||
new Error('MPC Core Kit is not initialized'), | ||
) | ||
}) | ||
|
||
it('should not recover if wrong password is entered', () => { | ||
setMPCCoreKitInstance({ | ||
state: { | ||
userInfo: undefined, | ||
}, | ||
} as unknown as Web3AuthMPCCoreKit) | ||
const { result } = renderHook(() => useMPCWallet()) | ||
jest.spyOn(mpcCoreKit, 'TssSecurityQuestion').mockReturnValue({ | ||
getQuestion: () => 'SOME RANDOM QUESTION', | ||
recoverFactor: () => { | ||
throw new Error('Invalid answer') | ||
}, | ||
} as unknown as TssSecurityQuestion) | ||
|
||
expect(result.current.recoverFactorWithPassword('test', false)).rejects.toEqual(new Error('Invalid answer')) | ||
}) | ||
|
||
it.only('should input recovered factor if correct password is entered', async () => { | ||
const mockSecurityQuestionFactor = ethers.Wallet.createRandom().privateKey.slice(2) | ||
const connectWalletSpy = jest.fn().mockImplementation(() => Promise.resolve()) | ||
jest.spyOn(useOnboard, 'default').mockReturnValue({} as unknown as OnboardAPI) | ||
jest.spyOn(useOnboard, 'connectWallet').mockImplementation(connectWalletSpy) | ||
|
||
setMPCCoreKitInstance( | ||
new MockMPCCoreKit( | ||
COREKIT_STATUS.REQUIRED_SHARE, | ||
{ | ||
email: '[email protected]', | ||
name: 'Test', | ||
} as unknown as UserInfo, | ||
new BN(mockSecurityQuestionFactor, 'hex'), | ||
) as unknown as Web3AuthMPCCoreKit, | ||
) | ||
|
||
const { result } = renderHook(() => useMPCWallet()) | ||
jest.spyOn(mpcCoreKit, 'TssSecurityQuestion').mockReturnValue({ | ||
getQuestion: () => 'SOME RANDOM QUESTION', | ||
recoverFactor: () => Promise.resolve(mockSecurityQuestionFactor), | ||
} as unknown as TssSecurityQuestion) | ||
|
||
act(() => result.current.recoverFactorWithPassword('test', false)) | ||
|
||
await waitFor(() => { | ||
expect(result.current.walletState === MPCWalletState.READY) | ||
expect(connectWalletSpy).toBeCalledWith(expect.anything(), { | ||
autoSelect: { | ||
label: ONBOARD_MPC_MODULE_LABEL, | ||
disableModals: true, | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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,45 @@ | ||
import { | ||
BrowserStorage, | ||
getWebBrowserFactor, | ||
storeWebBrowserFactor, | ||
TssShareType, | ||
type Web3AuthMPCCoreKit, | ||
} from '@web3auth/mpc-core-kit' | ||
import BN from 'bn.js' | ||
import { getPubKeyPoint } from '@tkey-mpc/common-types' | ||
|
||
export class DeviceShareRecovery { | ||
private mpcCoreKit: Web3AuthMPCCoreKit | ||
|
||
constructor(mpcCoreKit: Web3AuthMPCCoreKit) { | ||
this.mpcCoreKit = mpcCoreKit | ||
} | ||
|
||
async isEnabled() { | ||
if (!this.mpcCoreKit.tKey.metadata) { | ||
return false | ||
} | ||
return !!(await getWebBrowserFactor(this.mpcCoreKit)) | ||
} | ||
|
||
async createAndStoreDeviceFactor() { | ||
const userAgent = navigator.userAgent | ||
|
||
const deviceFactorKey = new BN( | ||
await this.mpcCoreKit.createFactor({ shareType: TssShareType.DEVICE, additionalMetadata: { userAgent } }), | ||
'hex', | ||
) | ||
await storeWebBrowserFactor(deviceFactorKey, this.mpcCoreKit) | ||
} | ||
|
||
async removeDeviceFactor() { | ||
const deviceFactor = await getWebBrowserFactor(this.mpcCoreKit) | ||
const key = new BN(deviceFactor, 'hex') | ||
const pubKey = getPubKeyPoint(key) | ||
const pubKeyX = pubKey.x.toString('hex', 64) | ||
await this.mpcCoreKit.deleteFactor(pubKey) | ||
const currentStorage = BrowserStorage.getInstance('mpc_corekit_store') | ||
debugger | ||
currentStorage.set(pubKeyX, undefined) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/hooks/wallets/mpc/recovery/SecurityQuestionRecovery.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,48 @@ | ||
import { TssSecurityQuestion, TssShareType, type Web3AuthMPCCoreKit } from '@web3auth/mpc-core-kit' | ||
|
||
const DEFAULT_SECURITY_QUESTION = 'ENTER PASSWORD' | ||
|
||
export class SecurityQuestionRecovery { | ||
private mpcCoreKit: Web3AuthMPCCoreKit | ||
private securityQuestions = new TssSecurityQuestion() | ||
|
||
constructor(mpcCoreKit: Web3AuthMPCCoreKit) { | ||
this.mpcCoreKit = mpcCoreKit | ||
} | ||
|
||
isEnabled(): boolean { | ||
try { | ||
const question = this.securityQuestions.getQuestion(this.mpcCoreKit) | ||
return !!question | ||
} catch (error) { | ||
console.error(error) | ||
// It errors out if recovery is not setup currently | ||
return false | ||
} | ||
} | ||
|
||
async upsertPassword(newPassword: string, oldPassword?: string) { | ||
if (this.isEnabled()) { | ||
if (!oldPassword) { | ||
throw Error('To change the password you need to provide the old password') | ||
} | ||
await this.securityQuestions.changeSecurityQuestion({ | ||
answer: oldPassword, | ||
mpcCoreKit: this.mpcCoreKit, | ||
newAnswer: newPassword, | ||
newQuestion: DEFAULT_SECURITY_QUESTION, | ||
}) | ||
} else { | ||
await this.securityQuestions.setSecurityQuestion({ | ||
question: DEFAULT_SECURITY_QUESTION, | ||
answer: newPassword, | ||
mpcCoreKit: this.mpcCoreKit, | ||
shareType: TssShareType.DEVICE, | ||
}) | ||
} | ||
} | ||
|
||
async recoverWithPassword(password: string) { | ||
return this.securityQuestions.recoverFactor(this.mpcCoreKit, password) | ||
} | ||
} |
Oops, something went wrong.