From 668bfe24ff6605ed358a53e5dd01192393f2f482 Mon Sep 17 00:00:00 2001 From: Andrea Scartabelli Date: Thu, 19 Dec 2024 11:37:35 +0100 Subject: [PATCH] web-wallet: Fix wrong error shown in the login screen Resolves #3226 Resolves #3097 --- web-wallet/CHANGELOG.md | 4 ++ web-wallet/src/lib/errors/index.js | 3 ++ .../src/lib/errors/invalid-mnemonic-error.js | 8 ++++ .../src/lib/errors/invalid-password-error.js | 8 ++++ .../src/lib/errors/mismatched-wallet-error.js | 8 ++++ .../src/routes/(welcome)/login/+page.svelte | 42 ++++++++++++------- .../__tests__/__snapshots__/page.spec.js.snap | 2 - 7 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 web-wallet/src/lib/errors/index.js create mode 100644 web-wallet/src/lib/errors/invalid-mnemonic-error.js create mode 100644 web-wallet/src/lib/errors/invalid-password-error.js create mode 100644 web-wallet/src/lib/errors/mismatched-wallet-error.js diff --git a/web-wallet/CHANGELOG.md b/web-wallet/CHANGELOG.md index 3118dcbfd..3b9984467 100644 --- a/web-wallet/CHANGELOG.md +++ b/web-wallet/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fix wrong error shown in the login screen [#3226] [#3097] + ## [0.10.1] - 2024-12-18 ### Fixed @@ -452,6 +454,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#3073]: https://github.com/dusk-network/rusk/issues/3073 [#3076]: https://github.com/dusk-network/rusk/issues/3076 [#3081]: https://github.com/dusk-network/rusk/issues/3081 +[#3097]: https://github.com/dusk-network/rusk/issues/3097 [#3098]: https://github.com/dusk-network/rusk/issues/3098 [#3099]: https://github.com/dusk-network/rusk/issues/3099 [#3113]: https://github.com/dusk-network/rusk/issues/3113 @@ -462,6 +465,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#3179]: https://github.com/dusk-network/rusk/issues/3179 [#3203]: https://github.com/dusk-network/rusk/issues/3203 [#3212]: https://github.com/dusk-network/rusk/issues/3212 +[#3226]: https://github.com/dusk-network/rusk/issues/3226 diff --git a/web-wallet/src/lib/errors/index.js b/web-wallet/src/lib/errors/index.js new file mode 100644 index 000000000..365935545 --- /dev/null +++ b/web-wallet/src/lib/errors/index.js @@ -0,0 +1,3 @@ +export { default as InvalidMnemonicError } from "./invalid-mnemonic-error"; +export { default as InvalidPasswordError } from "./invalid-password-error"; +export { default as MismatchedWalletError } from "./mismatched-wallet-error"; diff --git a/web-wallet/src/lib/errors/invalid-mnemonic-error.js b/web-wallet/src/lib/errors/invalid-mnemonic-error.js new file mode 100644 index 000000000..e9c3fef50 --- /dev/null +++ b/web-wallet/src/lib/errors/invalid-mnemonic-error.js @@ -0,0 +1,8 @@ +class InvalidMnemonicError extends Error { + constructor() { + super("Invalid mnemonic"); + this.name = "InvalidMnemonicError"; + } +} + +export default InvalidMnemonicError; diff --git a/web-wallet/src/lib/errors/invalid-password-error.js b/web-wallet/src/lib/errors/invalid-password-error.js new file mode 100644 index 000000000..f6683b066 --- /dev/null +++ b/web-wallet/src/lib/errors/invalid-password-error.js @@ -0,0 +1,8 @@ +class InvalidPasswordError extends Error { + constructor() { + super("Wrong password"); + this.name = "InvalidPasswordError"; + } +} + +export default InvalidPasswordError; diff --git a/web-wallet/src/lib/errors/mismatched-wallet-error.js b/web-wallet/src/lib/errors/mismatched-wallet-error.js new file mode 100644 index 000000000..23d43510b --- /dev/null +++ b/web-wallet/src/lib/errors/mismatched-wallet-error.js @@ -0,0 +1,8 @@ +class MismatchedWalletError extends Error { + constructor() { + super("Mismatched wallet address or no existing wallet"); + this.name = "MismatchedWalletError"; + } +} + +export default MismatchedWalletError; diff --git a/web-wallet/src/routes/(welcome)/login/+page.svelte b/web-wallet/src/routes/(welcome)/login/+page.svelte index 23085dd43..c592b0fdf 100644 --- a/web-wallet/src/routes/(welcome)/login/+page.svelte +++ b/web-wallet/src/routes/(welcome)/login/+page.svelte @@ -4,7 +4,14 @@ import { mdiArrowLeft, mdiKeyOutline } from "@mdi/js"; import { validateMnemonic } from "bip39"; + import { getErrorFrom } from "$lib/dusk/error"; import { Button, Textbox } from "$lib/dusk/components"; + import { + InvalidMnemonicError, + InvalidPasswordError, + MismatchedWalletError, + } from "$lib/errors"; + import { AppAnchor, AppAnchorButton, Banner } from "$lib/components"; import { IconHeadingCard } from "$lib/containers/Cards"; import { goto } from "$lib/navigation"; @@ -20,9 +27,6 @@ } from "$lib/wallet"; import loginInfoStorage from "$lib/services/loginInfoStorage"; - const localDataCheckErrorMsg = - "Mismatched wallet address or no existing wallet"; - /** @type {(seed: Uint8Array) => Promise} */ async function checkLocalData(seed) { const profileGenerator = profileGeneratorFrom(seed); @@ -30,7 +34,7 @@ const currentAddress = $settingsStore.userId; if (!currentAddress || currentAddress !== defaultAddress) { - throw new Error(localDataCheckErrorMsg); + throw new MismatchedWalletError(); } return profileGenerator; @@ -40,12 +44,12 @@ const getSeedFromMnemonicAsync = async (mnemonic) => validateMnemonic(mnemonic) ? getSeedFromMnemonic(mnemonic) - : Promise.reject(new Error("Invalid mnemonic")); + : Promise.reject(new InvalidMnemonicError()); /** @type {(loginInfo: MnemonicEncryptInfo) => (pwd: string) => Promise} */ const getSeedFromInfo = (loginInfo) => (pwd) => decryptMnemonic(loginInfo, pwd).then(getSeedFromMnemonic, () => - Promise.reject(new Error("Wrong password")) + Promise.reject(new InvalidPasswordError()) ); const loginInfo = loginInfoStorage.get(); @@ -57,8 +61,8 @@ /** @type {string} */ let secretText = ""; - /** @type {null|"invalid-password"|"invalid-mnemonic"} */ - let error = null; + /** @type {Error} */ + let error; /** @type {import("svelte/elements").FormEventHandler} */ function handleUnlockWalletSubmit() { @@ -72,16 +76,16 @@ .then((profileGenerator) => walletStore.init(profileGenerator)) .then(() => goto("/dashboard")) .catch((err) => { - if (err.message === localDataCheckErrorMsg) { + if (err instanceof MismatchedWalletError) { const enteredMnemonicPhrase = secretText.split(" "); mnemonicPhraseResetStore.set(enteredMnemonicPhrase); goto("/setup/restore"); + return; + } else { + error = err instanceof Error ? err : getErrorFrom(err); } - error = - err.message === "Wrong password" - ? "invalid-password" - : "invalid-mnemonic"; + fldSecret.focus(); fldSecret.select(); }); @@ -113,15 +117,14 @@ type="password" autocomplete="current-password" /> - {#if error === "invalid-mnemonic"} + {#if error instanceof InvalidMnemonicError}

Please ensure you have entered your 12-word mnemonic phrase, with a space separating each word.

- {/if} - {#if error === "invalid-password"} + {:else if error instanceof InvalidPasswordError}

Please ensure the password entered matches the one you have set up @@ -129,6 +132,13 @@ you can restore your wallet.

+ {:else if error} + +

{error.message}

+
{/if}