Skip to content

Commit

Permalink
web-wallet: Add local w3sper types
Browse files Browse the repository at this point in the history
- removed local w3sper's copy

Resolves #3465
  • Loading branch information
ascartabelli committed Feb 4, 2025
1 parent 8cf2703 commit c204aa1
Show file tree
Hide file tree
Showing 68 changed files with 671 additions and 4,559 deletions.
1 change: 1 addition & 0 deletions web-wallet/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
$app: "node_modules/@sveltejs/kit/src/runtime/app",
$config: "./src/config",
$lib: "./src/lib",
"@dusk/w3sper": "node_modules/@dusk/w3sper/src/mod.js",
"@sveltejs/kit": "node_modules/@sveltejs/kit/src/exports/index.js",
"@testing-library/svelte":
"node_modules/@testing-library/svelte/src/index.js",
Expand Down
1 change: 1 addition & 0 deletions web-wallet/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
engine-strict=true
@jsr:registry=https://npm.jsr.io
16 changes: 15 additions & 1 deletion web-wallet/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"type": "module",
"version": "1.3.0",
"dependencies": {
"@dusk/w3sper": "npm:@jsr/dusk__w3sper@^0.1.0",
"@floating-ui/dom": "1.6.5",
"@mdi/js": "7.4.47",
"@reown/appkit": "1.5.3",
Expand Down
13 changes: 13 additions & 0 deletions web-wallet/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ declare namespace svelteHTML {
) => void;
}
}

/* Aliases for common w3sper's types */

type AccountBalance = import("@dusk/w3sper").AccountBalance;
type AccountSyncer = import("@dusk/w3sper").AccountSyncer;
type AddressBalance = import("@dusk/w3sper").AddressBalance;
type AddressSyncer = import("@dusk/w3sper").AddressSyncer;
type Bookmark = import("@dusk/w3sper").Bookmark;
type Gas = import("@dusk/w3sper").Gas;
type Network = import("@dusk/w3sper").Network;
type Profile = import("@dusk/w3sper").Profile;
type ProfileGenerator = import("@dusk/w3sper").ProfileGenerator;
type StakeInfo = import("@dusk/w3sper").StakeInfo;
8 changes: 4 additions & 4 deletions web-wallet/src/lib/__mocks__/AccountSyncer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { AccountSyncer } from "$lib/vendor/w3sper.js/src/network/syncer/account";
import { AccountSyncer } from "@dusk/w3sper";

import { stakeInfo } from "$lib/mock-data";

class AccountSyncerMock extends AccountSyncer {
/**
* @param {import("$lib/vendor/w3sper.js/src/mod").Network} network
* @param {Network} network
*/
constructor(network) {
super(network);
}

/**
* @param {Array<import("$lib/vendor/w3sper.js/src/mod").Profile>} profiles
* @param {Array<Profile>} profiles
* @returns {Promise<AccountBalance[]>}
*/
async balances(profiles) {
Expand All @@ -22,7 +22,7 @@ class AccountSyncerMock extends AccountSyncer {
}

/**
* @param {Array<import("$lib/vendor/w3sper.js/src/mod").Profile>} profiles
* @param {Array<Profile>} profiles
* @returns {Promise<StakeInfo[]>}
*/
async stakes(profiles) {
Expand Down
16 changes: 4 additions & 12 deletions web-wallet/src/lib/__mocks__/AddressSyncer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressSyncer } from "$lib/vendor/w3sper.js/src/network/syncer/address";
import { AddressSyncer } from "@dusk/w3sper";

import { cacheUnspentNotes } from "$lib/mock-data";

Expand Down Expand Up @@ -33,17 +33,9 @@ class SyncEvent extends CustomEvent {

class AddressSyncerMock extends AddressSyncer {
/**
* @param {import("$lib/vendor/w3sper.js/src/mod").Network} network
* @param {Array<Profile>} profiles
* @param {Record<string, any>} [options={}]
*/
constructor(network, options = {}) {
super(network, options);
}

/**
* @param {Array<import("$lib/vendor/w3sper.js/src/mod").Profile>} profiles
* @param {Record<string, any>} [options={}]
* @returns {Promise<ReadableStream<any>>}
* @returns {Promise<ReadableStream<[Array<Map<Uint8Array, Uint8Array>>, { blockHeight: bigint; bookmark: bigint; }]>>}
*/
// eslint-disable-next-line no-unused-vars
async notes(profiles, options = {}) {
Expand Down Expand Up @@ -96,7 +88,7 @@ class AddressSyncerMock extends AddressSyncer {

/**
* @param {Uint8Array[]} nullifiers
* @returns {Promise<Uint8Array[]>}
* @returns {Promise<ArrayBuffer[]>}
*/
// eslint-disable-next-line no-unused-vars
async spent(nullifiers) {
Expand Down
15 changes: 11 additions & 4 deletions web-wallet/src/lib/__mocks__/Network.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Network } from "$lib/vendor/w3sper.js/src/network/mod";
import { Network } from "@dusk/w3sper";

class NetworkMock extends Network {
/** @type {boolean} */
Expand Down Expand Up @@ -36,11 +36,18 @@ class NetworkMock extends Network {
this.#connected = false;
}

async execute() {
return {
/**
* @param {import("@dusk/w3sper").BasicTransfer} tx
* @returns {Promise<import("@dusk/w3sper").ShieldedTransferResult>}
*/
// @ts-ignore for our current tests we always return a `ShieldedTransferResult`
// eslint-disable-next-line no-unused-vars
async execute(tx) {
return Object.freeze({
buffer: new Uint8Array(),
hash: "821a88f10f823b74fa3489c5acc6e31b7e2e96d7adff47137f20f4af61612415",
nullifiers: [],
};
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions web-wallet/src/lib/__mocks__/mockedWalletStore.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ProfileGenerator } from "$lib/vendor/w3sper.js/src/profile";
import { ProfileGenerator } from "@dusk/w3sper";

import { stakeInfo } from "$lib/mock-data";

import { mockReadableStore } from "$lib/dusk/test-helpers";

const seed = new Uint8Array(64);
const seeder = () => seed;
const seeder = async () => seed;
const profileGenerator = new ProfileGenerator(seeder);
const profiles = [
await profileGenerator.default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import "./AddressPicker.css";
/** @type {import("$lib/vendor/w3sper.js/src/mod").Profile | null} */
/** @type {Profile | null} */
export let currentProfile;
/** @type {import("$lib/vendor/w3sper.js/src/mod").Profile[]} */
/** @type {Profile[]} */
export let profiles;
/** @type {boolean} */
Expand Down
2 changes: 1 addition & 1 deletion web-wallet/src/lib/components/Allocate/Allocate.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
} from "$lib/dusk/components";
import { GasFee, GasSettings, OperationResult } from "$lib/components";
import { walletStore } from "$lib/stores";
import { Gas } from "$lib/vendor/w3sper.js/src/mod";
import { Gas } from "@dusk/w3sper";
import { MESSAGES } from "$lib/constants";
import Banner from "../Banner/Banner.svelte";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<script>
import { getKey, hasKeyValue, map, mapWith, setKey, when } from "lamb";
import { Gas } from "$lib/vendor/w3sper.js/src/mod";
import { Gas } from "@dusk/w3sper";
import {
gasStore,
operationsStore,
Expand Down
2 changes: 1 addition & 1 deletion web-wallet/src/lib/contracts/executeSend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getKey } from "lamb";
import { Gas } from "$lib/vendor/w3sper.js/src/mod";
import { Gas } from "@dusk/w3sper";

import { walletStore } from "$lib/stores";

Expand Down
2 changes: 1 addition & 1 deletion web-wallet/src/lib/dusk/string/getAddressInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProfileGenerator } from "$lib/vendor/w3sper.js/src/mod";
import { ProfileGenerator } from "@dusk/w3sper";

/**
* Validates if an input is a shielded or an public address, with feedback on failure or success.
Expand Down
6 changes: 1 addition & 5 deletions web-wallet/src/lib/stores/__tests__/networkStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
} from "vitest";
import { get } from "svelte/store";

import {
AccountSyncer,
AddressSyncer,
Network,
} from "$lib/vendor/w3sper.js/src/mod";
import { AccountSyncer, AddressSyncer, Network } from "@dusk/w3sper";

describe("Network store", async () => {
const blockHeight = 999_888_777n;
Expand Down
17 changes: 8 additions & 9 deletions web-wallet/src/lib/stores/__tests__/walletStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import {
vi,
} from "vitest";
import { get } from "svelte/store";
import {
Bookkeeper,
Gas,
Network,
ProfileGenerator,
} from "$lib/vendor/w3sper.js/src/mod";
import { Bookkeeper, Gas, Network, ProfileGenerator } from "@dusk/w3sper";
import { generateMnemonic } from "bip39";

import { stakeInfo } from "$lib/mock-data";
Expand Down Expand Up @@ -312,6 +307,7 @@ describe("Wallet store", async () => {
const memo = "";

const phoenixTxResult = {
buffer: new Uint8Array(),
hash: "some-tx-id",
nullifiers: [],
};
Expand Down Expand Up @@ -350,9 +346,11 @@ describe("Wallet store", async () => {
vi.useRealTimers();

const currentlyCachedBalance =
await new WalletTreasury().getCachedBalance(
defaultProfile.address.toString()
);
// TODO verifica perché non avevamo errore con questa
// await new WalletTreasury().getCachedBalance(
// defaultProfile.address.toString()
// );
await new WalletTreasury().getCachedBalance(defaultProfile);
const newNonce = currentlyCachedBalance.unshielded.nonce + 1n;

let expectedTx;
Expand All @@ -371,6 +369,7 @@ describe("Wallet store", async () => {
};
} else {
executeSpy.mockResolvedValueOnce({
buffer: new Uint8Array(),
hash: phoenixTxResult.hash,
nonce: newNonce,
});
Expand Down
21 changes: 14 additions & 7 deletions web-wallet/src/lib/stores/networkStore.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { writable } from "svelte/store";
import { browser } from "$app/environment";
import { always, condition, getKey, getPath, isUndefined, when } from "lamb";

import {
AccountSyncer,
AddressSyncer,
Network,
} from "$lib/vendor/w3sper.js/src/mod";
useAsProtocolDriver,
} from "@dusk/w3sper";

import wasmPath from "$lib/vendor/dusk_wallet_core.wasm?url";
import { rejectAfter } from "$lib/dusk/promise";
import { makeNodeUrl } from "$lib/url";

Expand All @@ -21,7 +23,8 @@ function getNetworkUrl() {
);
}
}

// const wasmPath = import.meta.resolve("$lib/vendor/dusk_wallet_core.wasm");
// console.log(wasmPath);
const networkUrl = getNetworkUrl();

/** @type {Network} */
Expand Down Expand Up @@ -55,7 +58,12 @@ const connect = async () =>
network.connected
? network
: Promise.race([
network.connect(),
fetch(wasmPath)
.then((response) => response.arrayBuffer())
.then((buffer) => {
useAsProtocolDriver(new Uint8Array(buffer));
network.connect();
}),
rejectAfter(
10000,
new Error("Timed out while connecting to the network")
Expand All @@ -68,9 +76,8 @@ const disconnect = () => network.disconnect();
/** @type {() => Promise<AccountSyncer>} */
const getAccountSyncer = () => connect().then(() => new AccountSyncer(network));

/** @type {(options?: NetworkSyncerOptions) => Promise<AddressSyncer>} */
const getAddressSyncer = (options) =>
connect().then(() => new AddressSyncer(network, options));
/** @type {() => Promise<AddressSyncer>} */
const getAddressSyncer = () => connect().then(() => new AddressSyncer(network));

/** @type {NetworkStoreServices["getBlockHashByHeight"]} */
const getBlockHashByHeight = (height) =>
Expand Down
Loading

0 comments on commit c204aa1

Please sign in to comment.