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..ddc89f27c 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 { + InvalidPasswordError, + InvalidMnemonicError, + 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}