Skip to content

Commit

Permalink
Merge pull request #3352 from dusk-network/feature-3351
Browse files Browse the repository at this point in the history
web-wallet: Simplify criteria creation for cache queries
  • Loading branch information
ascartabelli authored Jan 13, 2025
2 parents 36130b3 + 20fa7d9 commit 2503ae0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
81 changes: 32 additions & 49 deletions web-wallet/src/lib/wallet-cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import {
head,
isUndefined,
mapWith,
pairs,
pipe,
skipIf,
unless,
updateKey,
when,
} from "lamb";

/** @typedef {{ nullifiers?: Uint8Array[] } | { addresses?: string[] } | { accounts?: string[] }} RawCriteria */
/** @typedef {{ field: "nullifier", values: Uint8Array[] } | { field: "address", values: string[]} | { field: "account", values: string[]} | undefined} Criteria */

/** @type {(buffer: ArrayBuffer) => Uint8Array} */
const bufferToUint8Array = (buffer) => new Uint8Array(buffer);

Expand All @@ -39,22 +32,6 @@ const restoreNotes = mapWith(compose(updateNullifier, updateNote));

const restoreNullifiers = mapWith(bufferToUint8Array);

/** @type {(rawCriteria: RawCriteria) => Criteria} */
const toCriteria = pipe([
skipIf(isUndefined),
pairs,
head,
unless(isUndefined, (pair) => ({
field:
pair[0] === "nullifiers"
? "nullifier"
: pair[0] === "addresses"
? "address"
: "account",
values: pair[1],
})),
]);

class WalletCache {
/** @type {Dexie} */
#db;
Expand Down Expand Up @@ -88,15 +65,14 @@ class WalletCache {
* @template {boolean} PK
* @param {TName} tableName
* @param {PK} primaryKeysOnly
* @param {RawCriteria} [rawCriteria]
* @param {WalletCacheCriteria} [criteria]
* @returns {Promise<WalletCacheGetEntriesReturnType<TName, PK>>}
*/
async #getEntriesFrom(tableName, primaryKeysOnly, rawCriteria) {
const criteria = rawCriteria && toCriteria(rawCriteria);
async #getEntriesFrom(tableName, primaryKeysOnly, criteria) {
const table = this.#db.table(tableName);
const data =
/** @type {import("dexie").PromiseExtended<WalletCacheGetEntriesReturnType<TName, PK>>} */ (
criteria && criteria.values.length
criteria?.values.length
? table
.where(criteria.field)
.anyOf(criteria.values)
Expand Down Expand Up @@ -176,7 +152,8 @@ class WalletCache {
*/
getBalanceInfo(address) {
return this.#getEntriesFrom("balancesInfo", false, {
addresses: [address],
field: "address",
values: [address],
})
.then(getPath("0.balance"))
.then(when(isUndefined, () => this.#emptyBalanceInfo));
Expand All @@ -186,30 +163,33 @@ class WalletCache {
* @param {Uint8Array[]} [nullifiers]
* @returns {Promise<WalletCachePendingNoteInfo[]>}
*/
getPendingNotesInfo(nullifiers) {
return this.#getEntriesFrom("pendingNotesInfo", false, { nullifiers }).then(
restorePendingInfo
);
getPendingNotesInfo(nullifiers = []) {
return this.#getEntriesFrom("pendingNotesInfo", false, {
field: "nullifier",
values: nullifiers,
}).then(restorePendingInfo);
}

/**
* @param {string[]} [addresses] Base58 encoded addresses to fetch the spent notes of
* @returns {Promise<WalletCacheNote[]>}
*/
getSpentNotes(addresses) {
return this.#getEntriesFrom("spentNotes", false, { addresses }).then(
restoreNotes
);
getSpentNotes(addresses = []) {
return this.#getEntriesFrom("spentNotes", false, {
field: "address",
values: addresses,
}).then(restoreNotes);
}

/**
* @param {string[]} [addresses] Base58 encoded addresses to fetch the spent notes of
* @returns {Promise<Uint8Array[]>}
*/
getSpentNotesNullifiers(addresses) {
return this.#getEntriesFrom("spentNotes", true, { addresses }).then(
restoreNullifiers
);
getSpentNotesNullifiers(addresses = []) {
return this.#getEntriesFrom("spentNotes", true, {
field: "address",
values: addresses,
}).then(restoreNullifiers);
}

/**
Expand All @@ -218,7 +198,8 @@ class WalletCache {
*/
getStakeInfo(account) {
return this.#getEntriesFrom("stakeInfo", false, {
accounts: [account],
field: "account",
values: [account],
})
.then(getPath("0.stakeInfo"))
.then(
Expand Down Expand Up @@ -254,20 +235,22 @@ class WalletCache {
* @param {string[]} [addresses] Base58 encoded addresses to fetch the unspent notes of
* @returns {Promise<WalletCacheNote[]>}
*/
getUnspentNotes(addresses) {
return this.#getEntriesFrom("unspentNotes", false, { addresses }).then(
restoreNotes
);
getUnspentNotes(addresses = []) {
return this.#getEntriesFrom("unspentNotes", false, {
field: "address",
values: addresses,
}).then(restoreNotes);
}

/**
* @param {string[]} [addresses] Base58 encoded addresses to fetch the unspent notes of
* @returns {Promise<Uint8Array[]>}
*/
getUnspentNotesNullifiers(addresses) {
return this.#getEntriesFrom("unspentNotes", true, { addresses }).then(
restoreNullifiers
);
getUnspentNotesNullifiers(addresses = []) {
return this.#getEntriesFrom("unspentNotes", true, {
field: "address",
values: addresses,
}).then(restoreNullifiers);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions web-wallet/src/lib/wallet-cache/wallet-cache.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type WalletCacheBalanceInfo = {
};
};

type WalletCacheCriteria =
| { field: "address"; values: string[] }
| { field: "account"; values: string[] }
| { field: "nullifier"; values: Uint8Array[] }
| undefined;

type WalletCacheNote = {
address: string;
note: Uint8Array;
Expand Down

0 comments on commit 2503ae0

Please sign in to comment.