11import { ShielderTransaction } from '@cardinal-cryptography/shielder-sdk' ;
22import { openDB , IDBPDatabase } from 'idb' ;
3+ import { Address , sha256 } from 'viem' ;
34import { z } from 'zod' ;
45
56const DB_NAME = 'SHIELDER_STORAGE' ;
@@ -8,7 +9,7 @@ const DB_VERSION = 2;
89const STORE_CLIENTS = 'clients' ;
910const STORE_TRANSACTIONS = 'transactions' ;
1011
11- type ShielderClientData = Record < string , string > ;
12+ type ShielderClientData = Record < string , Record < string , string > > ;
1213
1314const 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} ;
0 commit comments