import { delUserFilesByKey, getUserFiles, setUserFiles } from '../app-dsBridge'; export interface ICertResponse { /** 证书内容 */ cert?: string; /** 证书序列号 */ certSn?: number; /** issueCertSn */ issueCertSn?: number; /** 过期时间 */ expirationDate?: number; } export interface LocalStorageAddressObject { pemPrivateKey: string; pemPublicKey: string; pemCertificate: ICertResponse; /** 助记词 */ mnemonic: string; /** 地址 */ address: string; /** 钱包名字 */ walletName: string; } export type LocalStorageKeysG = { /** 密钥 */ password: string; /** 地址 */ addressList: string[]; /** getCommon已选择的地址 */ selectedAddress: string; } /** * 加密数据 * @param data - 数据 */ function encrypto(data: any) { return JSON.stringify(data); } /** * 解密数据 * @param cipherText - 密文 */ function decrypto(cipherText: string | null) { if (typeof cipherText === 'string') { return JSON.parse(cipherText); } return cipherText; } export class LocalStorage { public static async setG(key: K, value: LocalStorageKeysG[K]) { const json = encrypto(value); await setUserFiles(`${key}`, json); return true; } public static async getG(key: K): Promise { let json = null; try { const item = await getUserFiles(`${key}`); json = decrypto(item) as any; } catch (error) { console.log(error); } return json; } public static async getItemByAddress(key: K, address: string): Promise { let json = null; try { const item = await getUserFiles(`${address}`); json = decrypto(item); } catch (error) { console.log(error); } return json && json[key]; } public static async set(item: LocalStorageAddressObject) { const json = encrypto(item); await setUserFiles(item.address, json); return true; } public static async get(address: string): Promise { let json = null; try { const item = await getUserFiles(`${address}`); json = decrypto(item); } catch (error) { console.log(error); } return json; } public static async remove(address: string): Promise { await delUserFilesByKey(address); return } }