import { NotifiEnvironment, NotifiFrontendConfiguration, checkIsConfigWithDelegate, checkIsConfigWithOidc, checkIsConfigWithPublicKeyAndAddress, } from '../configuration'; import { StorageDriver } from './NotifiFrontendStorage'; const getEnvPrefix = (env?: NotifiEnvironment): string => { if (!env) env = 'Production'; switch (env) { case 'Production': return 'notifi-jwt'; case 'Development': return 'notifi-jwt:dev'; case 'Staging': return 'notifi-jwt:stg'; case 'Local': return 'notifi-jwt:local'; } }; export const createInMemoryStorageDriver = ( config: NotifiFrontendConfiguration, ): StorageDriver => { let keyPrefix = `${getEnvPrefix(config.env || 'Production')}:${ config.tenantId }:${config.walletBlockchain}`; if (checkIsConfigWithOidc(config)) { keyPrefix += `:${config.userAccount}`; } else if (checkIsConfigWithPublicKeyAndAddress(config)) { keyPrefix += `:${config.accountAddress}:${config.walletPublicKey}`; } else if (checkIsConfigWithDelegate(config)) { keyPrefix += `:${config.delegatorAddress}`; } else { keyPrefix += `:${config.walletPublicKey}`; } const storageBackend: Record = {}; const storageDriver: StorageDriver = { get: (key: string): Promise => { const newKey = `${keyPrefix}:${key}`; let result: T | null = null; if (newKey in storageBackend) { const json = storageBackend[newKey]; result = JSON.parse(json) as T; } return Promise.resolve(result); }, set: (key: string, newValue: T | null): Promise => { const newKey = `${keyPrefix}:${key}`; if (newValue === null) { delete storageBackend[newKey]; } else { storageBackend[newKey] = JSON.stringify(newValue); } return Promise.resolve(); }, has: (key: string): Promise => { const newKey = `${keyPrefix}:${key}`; return Promise.resolve(newKey in storageBackend); }, }; return storageDriver; };