Skip to content

Commit

Permalink
fix: refactor process of reset-password-on-ui.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Nov 29, 2024
1 parent 900c6d6 commit 2d775f3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
18 changes: 16 additions & 2 deletions apps/mobile/src/core/apis/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/hooks/address/useNewUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/screens/Address/SetPassword2024.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 19 additions & 14 deletions packages/service-keyring/src/keyringService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down

0 comments on commit 2d775f3

Please sign in to comment.