diff --git a/apps/mobile/src/core/apis/lock.ts b/apps/mobile/src/core/apis/lock.ts index fdf304820..8b3611e86 100644 --- a/apps/mobile/src/core/apis/lock.ts +++ b/apps/mobile/src/core/apis/lock.ts @@ -131,12 +131,26 @@ export async function updateWalletPassword( return result; } -export async function forceOverwritePassword(newPassword: string) { +export async function resetPasswordOnUI(newPassword: string) { const result = getInitError(newPassword); if (result.error) return result; try { - await keyringService.resetPassword(newPassword); + const hasRestAccount = keyringService.getRestAccountsCount() > 0; + + if (hasRestAccount) { + const lockInfo = await getRabbyLockInfo(); + if (!lockInfo.isUseCustomPwd) { + await setupWalletPassword(newPassword); + } else { + throw new Error( + 'Cannot reset password when using custom password and have rest accounts', + ); + } + // await updateWalletPassword(RABBY_MOBILE_KR_PWD, newPassword); + } else { + await keyringService.resetPassword(newPassword); + } } catch (error) { console.error(error); result.error = 'Failed to reset password'; diff --git a/apps/mobile/src/hooks/address/useNewUser.ts b/apps/mobile/src/hooks/address/useNewUser.ts index 27990bbb8..41841578d 100644 --- a/apps/mobile/src/hooks/address/useNewUser.ts +++ b/apps/mobile/src/hooks/address/useNewUser.ts @@ -102,7 +102,7 @@ export function useCreateAddressProc() { const confirmPassword = useCallback(async () => { const { password, enableBiometrics } = createAddressProc.passwordForm; - const result = await apisLock.forceOverwritePassword(password); + const result = await apisLock.resetPasswordOnUI(password); if (result.error) { toast.show(result.error); return false; diff --git a/apps/mobile/src/screens/Address/SetPassword2024.tsx b/apps/mobile/src/screens/Address/SetPassword2024.tsx index e38df43bd..6f2872563 100644 --- a/apps/mobile/src/screens/Address/SetPassword2024.tsx +++ b/apps/mobile/src/screens/Address/SetPassword2024.tsx @@ -116,7 +116,7 @@ function useSetupPasswordForm( }); const updatePassword = async () => { - const result = await apisLock.forceOverwritePassword(values.password); + const result = await apisLock.resetPasswordOnUI(values.password); if (result.error) { toast.show(result.error); return false; diff --git a/packages/service-keyring/src/keyringService.ts b/packages/service-keyring/src/keyringService.ts index e7ede08b5..cb24cd61e 100644 --- a/packages/service-keyring/src/keyringService.ts +++ b/packages/service-keyring/src/keyringService.ts @@ -146,17 +146,23 @@ export class KeyringService extends RNEventEmitter { await this.persistAllKeyrings(); } - #filterAllKeyringsNeedPassword() { - return this.keyrings.filter( - keyring => - ![ - KEYRING_TYPE.WatchAddressKeyring, - KEYRING_TYPE.WalletConnectKeyring, - // some hardware keyrings which will create keyrings right away on bootstrap - KEYRING_TYPE.OneKeyKeyring, - KEYRING_TYPE.LedgerKeyring, - ].includes(keyring.type as any), - ); + // #filterAllKeyringsNeedPassword() { + // return this.keyrings.filter( + // keyring => + // ![ + // KEYRING_TYPE.WatchAddressKeyring, + // KEYRING_TYPE.WalletConnectKeyring, + // // some hardware keyrings which will create keyrings right away on bootstrap + // KEYRING_TYPE.OneKeyKeyring, + // KEYRING_TYPE.LedgerKeyring, + // ].includes(keyring.type as any), + // ); + // } + + getRestAccountsCount() { + return this.keyrings.reduce((accu, kr) => { + return accu + kr.accounts.length; + }, 0); } /** @@ -165,9 +171,8 @@ export class KeyringService extends RNEventEmitter { * @param newPassword */ async resetPassword(newPassword: string) { - const restSensitiveKeyrings = this.#filterAllKeyringsNeedPassword(); - if (restSensitiveKeyrings.length) { - console.warn("You're trying to overwrite password on existing keyrings."); + if (this.getRestAccountsCount()) { + throw new Error("You're trying to overwrite password on existing keyrings."); } await this._setupBoot(newPassword);