From ecd7d61dfb7949dc2899d58c77cbc5c173ed10b8 Mon Sep 17 00:00:00 2001 From: Andrea Scartabelli Date: Wed, 14 Feb 2024 10:11:58 +0100 Subject: [PATCH] web-wallet: Update dusk-wallet-js to 0.4.1 Resolves #1315 --- web-wallet/CHANGELOG.md | 1 + web-wallet/__mocks__/Wallet.js | 7 +++++++ web-wallet/__mocks__/initDuskWalletCore.js | 12 ------------ web-wallet/package-lock.json | 8 ++++---- web-wallet/package.json | 2 +- .../src/lib/components/__tests__/ScanQR.spec.js | 1 - web-wallet/src/lib/components/__tests__/Send.spec.js | 1 - .../lib/services/wallet/__tests__/getWallet.spec.js | 12 ++++++++---- .../src/lib/stores/__tests__/walletStore.spec.js | 6 +++++- web-wallet/src/lib/stores/walletStore.js | 2 +- web-wallet/src/routes/(app)/__tests__/layout.spec.js | 4 ++++ web-wallet/src/routes/(welcome)/login/+page.svelte | 6 ++---- .../routes/(welcome)/login/__tests__/page.spec.js | 8 ++++++-- web-wallet/vite-setup.js | 8 +------- 14 files changed, 40 insertions(+), 38 deletions(-) delete mode 100644 web-wallet/__mocks__/initDuskWalletCore.js diff --git a/web-wallet/CHANGELOG.md b/web-wallet/CHANGELOG.md index 8a79307f3a..8840d8382f 100644 --- a/web-wallet/CHANGELOG.md +++ b/web-wallet/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update deprecated Node actions in CI [#1343](https://github.com/dusk-network/rusk/issues/1343) - Change `setGasSettings` event to `gasSettings` and include `isValidGas` property in event data [#1354](https://github.com/dusk-network/rusk/issues/1354) - Change "withdraw stake" label to "unstake" [#1403](https://github.com/dusk-network/rusk/issues/1403) +- Update dusk-wallet-js to 0.4.1 [#1315](https://github.com/dusk-network/rusk/issues/1315) ### Removed diff --git a/web-wallet/__mocks__/Wallet.js b/web-wallet/__mocks__/Wallet.js index fb1e3fef06..96c610d579 100644 --- a/web-wallet/__mocks__/Wallet.js +++ b/web-wallet/__mocks__/Wallet.js @@ -1,4 +1,11 @@ class Wallet { + constructor (seed, gasLimit = 2900000000, gasPrice = 1) { + this.gasLimit = gasLimit; + this.gasPrice = gasPrice; + this.seed = seed; + this.wasm = {}; + } + gasLimit; gasPrice; seed; diff --git a/web-wallet/__mocks__/initDuskWalletCore.js b/web-wallet/__mocks__/initDuskWalletCore.js deleted file mode 100644 index dbe2a1edcb..0000000000 --- a/web-wallet/__mocks__/initDuskWalletCore.js +++ /dev/null @@ -1,12 +0,0 @@ -import { readFile } from "node:fs/promises"; - -/** - * @param {Record} imports - * @returns {Promise} - */ -const init = async imports => - readFile(require.resolve("@dusk-network/dusk-wallet-core/dusk_wallet_core_bg.wasm")) - .then(buffer => WebAssembly.instantiate(buffer, imports)) - .then(({ instance }) => instance); - -export default init; diff --git a/web-wallet/package-lock.json b/web-wallet/package-lock.json index 3b67b07ce2..331f821cdd 100644 --- a/web-wallet/package-lock.json +++ b/web-wallet/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0-beta", "license": "MPL-2.0", "dependencies": { - "@dusk-network/dusk-wallet-js": "0.3.2", + "@dusk-network/dusk-wallet-js": "0.4.1", "@floating-ui/dom": "1.5.3", "@mdi/js": "7.3.67", "bip39": "3.1.0", @@ -273,9 +273,9 @@ "dev": true }, "node_modules/@dusk-network/dusk-wallet-js": { - "version": "0.3.2", - "resolved": "https://registry.npmjs.org/@dusk-network/dusk-wallet-js/-/dusk-wallet-js-0.3.2.tgz", - "integrity": "sha512-ydu1MlUhy7sLE6S/rbI0JuhjLBKPkI7T4KteveEwJM8cMzwQsVCU2pAkkAtNwiMVFtuaCuQ08PEd0WwTFq+kjw==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@dusk-network/dusk-wallet-js/-/dusk-wallet-js-0.4.1.tgz", + "integrity": "sha512-ODGWWrQsoiGuO/1hARWU0riSyWsDt8eVNPL1yxPF2LiNDw+wi4P/UA1+6up2EfMXVmRJyh59uRZqws5VuxtQpQ==", "dependencies": { "dexie": "3.2.4", "fake-indexeddb": "5.0.1" diff --git a/web-wallet/package.json b/web-wallet/package.json index 24cda622d9..4b63fa2b4d 100644 --- a/web-wallet/package.json +++ b/web-wallet/package.json @@ -33,7 +33,7 @@ "type": "module", "version": "1.0.0-beta", "dependencies": { - "@dusk-network/dusk-wallet-js": "0.3.2", + "@dusk-network/dusk-wallet-js": "0.4.1", "@floating-ui/dom": "1.5.3", "@mdi/js": "7.3.67", "bip39": "3.1.0", diff --git a/web-wallet/src/lib/components/__tests__/ScanQR.spec.js b/web-wallet/src/lib/components/__tests__/ScanQR.spec.js index 3ef227a5dc..aba767eaae 100644 --- a/web-wallet/src/lib/components/__tests__/ScanQR.spec.js +++ b/web-wallet/src/lib/components/__tests__/ScanQR.spec.js @@ -5,7 +5,6 @@ import { it } from "vitest"; import { cleanup, render } from "@testing-library/svelte"; -import "jsdom-worker"; import { ScanQR } from ".."; diff --git a/web-wallet/src/lib/components/__tests__/Send.spec.js b/web-wallet/src/lib/components/__tests__/Send.spec.js index 270ad6adcf..6c3a22f1d0 100644 --- a/web-wallet/src/lib/components/__tests__/Send.spec.js +++ b/web-wallet/src/lib/components/__tests__/Send.spec.js @@ -7,7 +7,6 @@ import { vi } from "vitest"; import { cleanup, fireEvent, render } from "@testing-library/svelte"; -import "jsdom-worker"; import { deductLuxFeeFrom } from "$lib/contracts"; import { createCurrencyFormatter } from "$lib/dusk/currency"; diff --git a/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js b/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js index 36c4ad9a89..acc10e4d77 100644 --- a/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js +++ b/web-wallet/src/lib/services/wallet/__tests__/getWallet.spec.js @@ -8,23 +8,27 @@ import { enumerables } from "lamb"; import { generateMnemonic } from "bip39"; import { getSeedFromMnemonic } from "$lib/wallet"; - import { getWallet } from ".."; vi.unmock("@dusk-network/dusk-wallet-js"); describe("getWallet", () => { - it("should get a Wallet instance using a seed", async () => { + it("should get a Wallet instance using a seed", () => { const mnemonic = generateMnemonic(); const seed = getSeedFromMnemonic(mnemonic); - const wallet = await getWallet(seed); + const wallet = getWallet(seed); + const walletPublicMembers = [ + ...enumerables(wallet), + ...Object.getOwnPropertyNames(Object.getPrototypeOf(wallet)) + ]; - expect(enumerables(wallet)).toMatchInlineSnapshot(` + expect(walletPublicMembers).toMatchInlineSnapshot(` [ "wasm", "seed", "gasLimit", "gasPrice", + "constructor", "getBalance", "getPsks", "sync", diff --git a/web-wallet/src/lib/stores/__tests__/walletStore.spec.js b/web-wallet/src/lib/stores/__tests__/walletStore.spec.js index 30d3058560..1a1abc7441 100644 --- a/web-wallet/src/lib/stores/__tests__/walletStore.spec.js +++ b/web-wallet/src/lib/stores/__tests__/walletStore.spec.js @@ -22,7 +22,7 @@ describe("walletStore", async () => { const wallet = new Wallet([]); const getBalanceSpy = vi.spyOn(Wallet.prototype, "getBalance").mockResolvedValue(balance); - const getPsksSpy = vi.spyOn(Wallet.prototype, "getPsks").mockReturnValue(addresses); + const getPsksSpy = vi.spyOn(Wallet.prototype, "getPsks").mockResolvedValue(addresses); const historySpy = vi.spyOn(Wallet.prototype, "history").mockResolvedValue(transactions); const resetSpy = vi.spyOn(Wallet.prototype, "reset").mockResolvedValue(void 0); const stakeInfoSpy = vi.spyOn(Wallet.prototype, "stakeInfo").mockResolvedValue({}); @@ -104,6 +104,7 @@ describe("walletStore", async () => { isSyncing: true, addresses: addresses }); + expect(getPsksSpy).toHaveBeenCalledTimes(1); expect(getBalanceSpy).not.toHaveBeenCalled(); @@ -223,6 +224,9 @@ describe("walletStore", async () => { isSyncing: true, addresses: addresses }); + + await vi.advanceTimersToNextTimerAsync(); + expect(getPsksSpy).toHaveBeenCalledTimes(1); expect(getBalanceSpy).toHaveBeenCalledTimes(1); expect(getBalanceSpy).toHaveBeenCalledWith(addresses[0]); diff --git a/web-wallet/src/lib/stores/walletStore.js b/web-wallet/src/lib/stores/walletStore.js index 2abe8f5e1a..fa5d9025d4 100644 --- a/web-wallet/src/lib/stores/walletStore.js +++ b/web-wallet/src/lib/stores/walletStore.js @@ -91,7 +91,7 @@ async function updateAfterSync () { async function init (wallet) { walletInstance = wallet; - const addresses = walletInstance.getPsks(); + const addresses = await walletInstance.getPsks(); const currentAddress = addresses[0]; set({ diff --git a/web-wallet/src/routes/(app)/__tests__/layout.spec.js b/web-wallet/src/routes/(app)/__tests__/layout.spec.js index f3ae95261b..bdb691e020 100644 --- a/web-wallet/src/routes/(app)/__tests__/layout.spec.js +++ b/web-wallet/src/routes/(app)/__tests__/layout.spec.js @@ -16,19 +16,23 @@ import * as SvelteKit from "@sveltejs/kit"; import { Wallet } from "@dusk-network/dusk-wallet-js"; import * as navigation from "$lib/navigation"; +import { addresses } from "$lib/mock-data"; import { walletStore } from "$lib/stores"; import { load } from "../+layout"; import Layout from "../+layout.svelte"; describe("App layout.js", () => { + const getPsksSpy = vi.spyOn(Wallet.prototype, "getPsks").mockResolvedValue(addresses); const redirectSpy = vi.spyOn(SvelteKit, "redirect"); afterEach(() => { + getPsksSpy.mockClear(); redirectSpy.mockClear(); }); afterAll(() => { + getPsksSpy.mockRestore(); redirectSpy.mockRestore(); }); diff --git a/web-wallet/src/routes/(welcome)/login/+page.svelte b/web-wallet/src/routes/(welcome)/login/+page.svelte index 75da2d0e36..77b0a51adb 100644 --- a/web-wallet/src/routes/(welcome)/login/+page.svelte +++ b/web-wallet/src/routes/(welcome)/login/+page.svelte @@ -22,7 +22,7 @@ /** @type {(wallet: Wallet) => Promise} */ async function checkLocalData (wallet) { - const defaultAddress = wallet.getPsks()[0]; + const defaultAddress = (await wallet.getPsks())[0]; const currentAddress = $settingsStore.userId; if (defaultAddress !== currentAddress) { @@ -64,9 +64,7 @@ getSeed(secretText.trim()) .then(getWallet) .then(checkLocalData) - .then(wallet => { - walletStore.init(wallet); - }) + .then(wallet => walletStore.init(wallet)) .then(() => { goto("/dashboard"); }) diff --git a/web-wallet/src/routes/(welcome)/login/__tests__/page.spec.js b/web-wallet/src/routes/(welcome)/login/__tests__/page.spec.js index 972b902a57..816a1d81be 100644 --- a/web-wallet/src/routes/(welcome)/login/__tests__/page.spec.js +++ b/web-wallet/src/routes/(welcome)/login/__tests__/page.spec.js @@ -14,6 +14,7 @@ import { get } from "svelte/store"; import { Wallet } from "@dusk-network/dusk-wallet-js"; import { setKey } from "lamb"; +import { addresses } from "$lib/mock-data"; import { getAsHTMLElement } from "$lib/dusk/test-helpers"; import { settingsStore, walletStore } from "$lib/stores"; import { encryptMnemonic, getSeedFromMnemonic } from "$lib/wallet"; @@ -29,17 +30,18 @@ function getTextInput (container) { } describe("Login", async () => { + const walletGetPsksSpy = vi.spyOn(Wallet.prototype, "getPsks").mockResolvedValue(addresses); + const walletResetSpy = vi.spyOn(Wallet.prototype, "reset").mockResolvedValue(void 0); const mnemonic = generateMnemonic(); const pwd = "some pwd"; const loginInfo = await encryptMnemonic(mnemonic, pwd); const seed = getSeedFromMnemonic(mnemonic); - const userId = new Wallet(seed).getPsks()[0]; + const userId = (await new Wallet(seed).getPsks())[0]; const getErrorElement = () => document.querySelector(".login__error"); const getWalletSpy = vi.spyOn(walletService, "getWallet"); const gotoSpy = vi.spyOn(appNavigation, "goto"); const initSpy = vi.spyOn(walletStore, "init"); const settingsResetSpy = vi.spyOn(settingsStore, "reset"); - const walletResetSpy = vi.spyOn(Wallet.prototype, "reset").mockResolvedValue(void 0); afterEach(() => { cleanup(); @@ -48,6 +50,7 @@ describe("Login", async () => { initSpy.mockClear(); settingsStore.reset(); settingsResetSpy.mockClear(); + walletGetPsksSpy.mockClear(); walletResetSpy.mockClear(); walletStore.reset(); }); @@ -57,6 +60,7 @@ describe("Login", async () => { gotoSpy.mockRestore(); initSpy.mockRestore(); settingsResetSpy.mockRestore(); + walletGetPsksSpy.mockRestore(); walletResetSpy.mockRestore(); }); diff --git a/web-wallet/vite-setup.js b/web-wallet/vite-setup.js index 79531579d3..98162a55e5 100644 --- a/web-wallet/vite-setup.js +++ b/web-wallet/vite-setup.js @@ -6,17 +6,11 @@ import * as matchers from "@testing-library/jest-dom/matchers"; import { expect, vi } from "vitest"; import { readable } from "svelte/store"; import crypto from "node:crypto"; +import "jsdom-worker"; import { IntersectionObserver, ResizeObserver } from "./src/lib/dusk/mocks"; -import init from "./__mocks__/initDuskWalletCore.js"; import Wallet from "./__mocks__/Wallet.js"; -// Mocking the wallet core wasm -vi.doMock( - "@dusk-network/dusk-wallet-core/dusk_wallet_core_bg.wasm?init", - () => ({ default: vi.fn(init) }) -); - // Mocking the Wallet vi.doMock( "@dusk-network/dusk-wallet-js",