Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use account id in wallet key for idb #1108

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/frontend/src/lib/workers/_stores/wallet-worker.store.ts
Original file line number Diff line number Diff line change
@@ -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<string, CertifiedData<TransactionWithId>>;
Expand All @@ -12,16 +13,31 @@ interface WalletState {
transactions: IndexedTransactions;
}

interface WalletTokenAccount {
ledgerId: PrincipalText;
account: IcrcAccount;
}

type WalletIdbKey = string;

export class WalletStore {
private static EMPTY_STORE: WalletState = {
balance: undefined,
transactions: {}
};

#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<bigint> | undefined {
Expand Down Expand Up @@ -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<void> {
// 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<WalletStore> {
const state = await get(ICP_LEDGER_CANISTER_ID, walletIdbStore);
return new WalletStore(state);
static async init(params: WalletTokenAccount): Promise<WalletStore> {
const idbKey = WalletStore.toIdbKey(params);
const state = await get(idbKey, walletIdbStore);
return new WalletStore({ state, idbKey });
}
}
11 changes: 9 additions & 2 deletions src/frontend/src/lib/workers/wallet.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 });

Expand Down
Loading