UNPKG

@broxus/tvm-connect

Version:

TypeScript SDK for connecting to Nekoton-compatible wallets using a unified interface.

41 lines (40 loc) 1.32 kB
import { debug, storage } from '@broxus/js-utils'; import { TVM_RECENT_CONNECTION } from '../constants'; import { ConnectionType } from '../types'; export function isRecentConnectionMeta(value) { const meta = { providerId: value.providerId, type: value.type }; return Boolean(meta.type && ConnectionType[meta.type] && !!meta.providerId); } export function storeRecentConnectionMeta(meta, storageKey = TVM_RECENT_CONNECTION) { try { if (!meta) { storage.remove(storageKey); return; } if (!isRecentConnectionMeta(meta)) { return; } debug('TvmConnect connection meta has been stored', meta); storage.set(storageKey, JSON.stringify(meta)); } catch (e) { debug('Store TvmConnect connection meta failed with an error', meta); } } export function getRecentConnectionMeta(storageKey = TVM_RECENT_CONNECTION) { const value = storage.get(storageKey); if (!value) { return undefined; } try { const json = JSON.parse(value); if (isRecentConnectionMeta(json)) { return json; } } catch (e) { debug('Retrieve TvmConnection meta failed with an error', e); } storeRecentConnectionMeta(undefined, storageKey); return undefined; }