From 3de9d4f73a446da5f7c35a060432c9cf4d18c89b Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Fri, 17 Jan 2025 11:52:50 +0100 Subject: [PATCH] feat: use account id in wallet key for idb Signed-off-by: David Dal Busco --- .../workers/_stores/wallet-worker.store.ts | 39 ++++++++++++++++--- src/frontend/src/lib/workers/wallet.worker.ts | 11 +++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/frontend/src/lib/workers/_stores/wallet-worker.store.ts b/src/frontend/src/lib/workers/_stores/wallet-worker.store.ts index efaea605b..5fc91beb0 100644 --- a/src/frontend/src/lib/workers/_stores/wallet-worker.store.ts +++ b/src/frontend/src/lib/workers/_stores/wallet-worker.store.ts @@ -1,7 +1,8 @@ -import { ICP_LEDGER_CANISTER_ID } from '$lib/constants/constants'; import { walletIdbStore } from '$lib/stores/idb.store'; +import type { PrincipalText } from '$lib/types/principal'; import type { CertifiedData } from '$lib/types/store'; import type { TransactionWithId } from '@dfinity/ledger-icp'; +import { encodeIcrcAccount, type IcrcAccount } from '@dfinity/ledger-icrc'; import { get, set } from 'idb-keyval'; export type IndexedTransactions = Record>; @@ -12,6 +13,13 @@ interface WalletState { transactions: IndexedTransactions; } +interface WalletTokenAccount { + ledgerId: PrincipalText; + account: IcrcAccount; +} + +type WalletIdbKey = string; + export class WalletStore { private static EMPTY_STORE: WalletState = { balance: undefined, @@ -19,9 +27,17 @@ export class WalletStore { }; #store: WalletState; + #idbKey: WalletIdbKey; - private constructor(state: WalletState) { + private constructor({ + state, + idbKey: key + }: { + state: WalletState | undefined; + idbKey: WalletIdbKey; + }) { this.#store = state ?? WalletStore.EMPTY_STORE; + this.#idbKey = key; } get balance(): CertifiedData | undefined { @@ -75,13 +91,24 @@ export class WalletStore { this.#store = WalletStore.EMPTY_STORE; } + private static toIdbKey({ + account, + ledgerId + }: { + ledgerId: PrincipalText; + account: IcrcAccount; + }): string { + return `${ledgerId}#${encodeIcrcAccount(account)}`; + } + async save(): Promise { // Save information to improve UX when application is reloaded or returning users. - await set(ICP_LEDGER_CANISTER_ID, this.#store, walletIdbStore); + await set(this.#idbKey, this.#store, walletIdbStore); } - static async init(): Promise { - const state = await get(ICP_LEDGER_CANISTER_ID, walletIdbStore); - return new WalletStore(state); + static async init(params: WalletTokenAccount): Promise { + const idbKey = WalletStore.toIdbKey(params); + const state = await get(idbKey, walletIdbStore); + return new WalletStore({ state, idbKey }); } } diff --git a/src/frontend/src/lib/workers/wallet.worker.ts b/src/frontend/src/lib/workers/wallet.worker.ts index 972570941..c72c8d0a5 100644 --- a/src/frontend/src/lib/workers/wallet.worker.ts +++ b/src/frontend/src/lib/workers/wallet.worker.ts @@ -5,7 +5,11 @@ import { type QueryAndUpdateRequestParams } from '$lib/api/call/query.api'; import { getTransactions } from '$lib/api/icp-index.api'; -import { PAGINATION, SYNC_WALLET_TIMER_INTERVAL } from '$lib/constants/constants'; +import { + ICP_LEDGER_CANISTER_ID, + PAGINATION, + SYNC_WALLET_TIMER_INTERVAL +} from '$lib/constants/constants'; import type { IcTransactionUi } from '$lib/types/ic-transaction'; import type { PostMessageDataRequest, @@ -63,7 +67,10 @@ const startTimer = async ({ data: { missionControlId } }: { data: PostMessageDat return; } - const store = await WalletStore.init(); + const store = await WalletStore.init({ + account: { owner: Principal.fromText(missionControlId) }, + ledgerId: ICP_LEDGER_CANISTER_ID + }); emitSavedWallet({ store, identity });