Skip to content

Commit 6faf4e2

Browse files
authored
Merge pull request #23 from Cardinal-Cryptography/SD-35-update-shielder-indexed-db-structure
SD-35: Update shielder indexeddb structure
2 parents 8255128 + a65a7d5 commit 6faf4e2

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/domains/chains/components/WalletProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type WalletContextType = {
1515
isConnected: boolean,
1616
disconnect: () => Promise<void>,
1717
address?: Address,
18-
privateKey: string | undefined,
18+
privateKey: Address | undefined,
1919
};
2020

2121
const WalletContext = createContext<WalletContextType | undefined>(undefined);

src/domains/shielder/stores/getShielderIndexedDB.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ShielderTransaction } from '@cardinal-cryptography/shielder-sdk';
22
import { openDB, IDBPDatabase } from 'idb';
3+
import { Address, sha256 } from 'viem';
34
import { z } from 'zod';
45

56
const DB_NAME = 'SHIELDER_STORAGE';
@@ -8,7 +9,7 @@ const DB_VERSION = 2;
89
const STORE_CLIENTS = 'clients';
910
const STORE_TRANSACTIONS = 'transactions';
1011

11-
type ShielderClientData = Record<string, string>;
12+
type ShielderClientData = Record<string, Record<string, string>>;
1213

1314
const transactionSchema = z.object({
1415
type: z.enum(['NewAccount', 'Deposit', 'Withdraw']),
@@ -68,27 +69,34 @@ const initDB = async (): Promise<IDBPDatabase<DBSchema>> => {
6869
});
6970
};
7071

71-
export const getShielderIndexedDB = (chainId: number) => {
72-
const key = chainId.toString();
72+
export const getShielderIndexedDB = (chainId: number, privateKey: Address) => {
73+
const chainKey = chainId.toString();
74+
const hashedPrivateKey = sha256(privateKey);
7375
const initDbPromise = initDB();
7476

7577
return {
7678
getItem: async (itemKey: string): Promise<string | null> => {
7779
const db = await initDbPromise;
78-
const clientData = (await db.get(STORE_CLIENTS, key)) ?? {};
79-
return clientData[itemKey] ?? null;
80+
const allDataForAddress = await db.get(STORE_CLIENTS, hashedPrivateKey);
81+
const chainData = allDataForAddress?.[chainKey];
82+
return chainData?.[itemKey] ?? null;
8083
},
81-
8284
setItem: async (itemKey: string, value: string): Promise<void> => {
8385
const db = await initDbPromise;
84-
const existingData = (await db.get(STORE_CLIENTS, key)) ?? {};
86+
const allDataForAddress = (await db.get(STORE_CLIENTS, hashedPrivateKey)) ?? {};
87+
const existingChainData = allDataForAddress[chainKey] ?? {};
8588

86-
const updatedData = {
87-
...existingData,
89+
const updatedChainData = {
90+
...existingChainData,
8891
[itemKey]: value,
8992
};
9093

91-
await db.put(STORE_CLIENTS, updatedData, key);
94+
const updatedAllData = {
95+
...allDataForAddress,
96+
[chainKey]: updatedChainData,
97+
};
98+
99+
await db.put(STORE_CLIENTS, updatedAllData, hashedPrivateKey);
92100
},
93101
};
94102
};

src/domains/shielder/utils/useShielderClient.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,42 @@ import useChain from 'src/domains/chains/utils/useChain';
1010
import getQueryKey from 'src/domains/misc/utils/getQueryKey';
1111
import { getShielderIndexedDB } from 'src/domains/shielder/stores/getShielderIndexedDB';
1212
import { getTransactionsIndexedDB } from 'src/domains/shielder/stores/getShielderIndexedDB';
13-
import { useShielderStore } from 'src/domains/shielder/stores/shielder';
1413
import { useWasm } from 'src/domains/shielder/utils/WasmProvider';
1514

1615
const useShielderClient = () => {
1716
const chainConfig = useChain();
1817
const { wasmCryptoClient, wasmLoaded } = useWasm();
1918

2019
const publicClient = usePublicClient({ chainId: chainConfig?.id });
21-
const { address: account } = useWallet();
22-
const { shielderPrivateKey } = useShielderStore(account);
20+
const { address: accountAddress, privateKey } = useWallet();
2321

2422
const isQueryDisabled =
25-
!publicClient || !chainConfig || !wasmLoaded || !account || !wasmCryptoClient || !shielderPrivateKey;
23+
!publicClient || !chainConfig || !wasmLoaded || !accountAddress || !wasmCryptoClient || !privateKey;
2624

2725
return useQuery({
28-
queryKey: chainConfig && shielderPrivateKey ? getQueryKey.shielderClient(chainConfig.id, shielderPrivateKey) : [],
26+
queryKey: chainConfig && privateKey ? getQueryKey.shielderClient(chainConfig.id, privateKey) : [],
2927
staleTime: Infinity,
30-
retry: false,
3128
queryFn: isQueryDisabled ? skipToken : async () => {
32-
const { shielderConfig, id } = chainConfig;
29+
const { shielderConfig, id: chainId } = chainConfig;
3330

3431
if (!shielderConfig) {
3532
throw new Error('Shielder config is not available');
3633
}
3734

38-
const storage = getTransactionsIndexedDB(account);
35+
const transactionsStorage = getTransactionsIndexedDB(accountAddress);
36+
const shielderStorage = getShielderIndexedDB(chainId, privateKey);
3937

4038
const client = createShielderClient({
41-
shielderSeedPrivateKey: shielderPrivateKey,
42-
chainId: BigInt(id),
39+
shielderSeedPrivateKey: privateKey,
40+
chainId: BigInt(chainId),
4341
publicClient,
4442
contractAddress: shielderConfig.shielderContractAddress,
4543
relayerUrl: shielderConfig.relayerUrl,
46-
storage: getShielderIndexedDB(id),
44+
storage: shielderStorage,
4745
cryptoClient: wasmCryptoClient,
4846
callbacks: {
4947
onNewTransaction: async (tx: ShielderTransaction) => {
50-
await storage.addItem(
48+
await transactionsStorage.addItem(
5149
chainConfig.id,
5250
tx
5351
);

0 commit comments

Comments
 (0)