|
1 | 1 | import BN from "bn.js"; |
2 | 2 |
|
3 | 3 | import { FIELD_ELEMENT_HEX_LEN } from "../constants"; |
4 | | -import { ICoreKit, IStorage, SupportedStorageType, TkeyLocalStoreData } from "../interfaces"; |
| 4 | +import { IAsyncStorage, ICoreKit, IStorage, SupportedStorageType, TkeyLocalStoreData } from "../interfaces"; |
5 | 5 | import { storageAvailable } from "../utils"; |
6 | 6 |
|
7 | 7 | export class MemoryStorage implements IStorage { |
@@ -99,6 +99,87 @@ export class BrowserStorage { |
99 | 99 | } |
100 | 100 | } |
101 | 101 |
|
| 102 | +export class AsyncStorage { |
| 103 | + // eslint-disable-next-line no-use-before-define |
| 104 | + private static instance: AsyncStorage; |
| 105 | + |
| 106 | + public storage: IAsyncStorage; |
| 107 | + |
| 108 | + private _storeKey: string; |
| 109 | + |
| 110 | + private constructor(storeKey: string, storage: IAsyncStorage) { |
| 111 | + this.storage = storage; |
| 112 | + this._storeKey = storeKey; |
| 113 | + } |
| 114 | + |
| 115 | + static getInstance(key: string, storageKey: IAsyncStorage): AsyncStorage { |
| 116 | + if (!this.instance) { |
| 117 | + const storage: IAsyncStorage = storageKey; |
| 118 | + if (!storage) { |
| 119 | + throw new Error("No valid storage available"); |
| 120 | + } |
| 121 | + this.instance = new this(key, storage); |
| 122 | + } |
| 123 | + return this.instance; |
| 124 | + } |
| 125 | + |
| 126 | + async toJSON(): Promise<string> { |
| 127 | + const result = await this.storage.getItem(this._storeKey); |
| 128 | + if (!result) throw new Error(`storage ${this._storeKey} is null`); |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | + async resetStore(): Promise<Record<string, unknown>> { |
| 133 | + const currStore = await this.getStore(); |
| 134 | + await this.storage.setItem(this._storeKey, JSON.stringify({})); |
| 135 | + return currStore; |
| 136 | + } |
| 137 | + |
| 138 | + async getStore(): Promise<Record<string, unknown>> { |
| 139 | + return JSON.parse((await this.storage.getItem(this._storeKey)) || "{}"); |
| 140 | + } |
| 141 | + |
| 142 | + async get<T>(key: string): Promise<T> { |
| 143 | + const store = JSON.parse((await this.storage.getItem(this._storeKey)) || "{}"); |
| 144 | + return store[key]; |
| 145 | + } |
| 146 | + |
| 147 | + async set<T>(key: string, value: T): Promise<void> { |
| 148 | + const store = JSON.parse((await this.storage.getItem(this._storeKey)) || "{}"); |
| 149 | + store[key] = value; |
| 150 | + await this.storage.setItem(this._storeKey, JSON.stringify(store)); |
| 151 | + } |
| 152 | + |
| 153 | + async remove(key: string): Promise<void> { |
| 154 | + const store = JSON.parse((await this.storage.getItem(this._storeKey)) || "{}"); |
| 155 | + delete store[key]; |
| 156 | + await this.storage.setItem(this._storeKey, JSON.stringify(store)); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +export async function asyncStoreFactor(factorKey: BN, mpcCoreKit: ICoreKit, storageKey: IAsyncStorage): Promise<void> { |
| 161 | + const metadata = mpcCoreKit.tKey.getMetadata(); |
| 162 | + const currentStorage = AsyncStorage.getInstance("mpc_corekit_store", storageKey); |
| 163 | + |
| 164 | + const tkeyPubX = metadata.pubKey.x.toString(16, FIELD_ELEMENT_HEX_LEN); |
| 165 | + await currentStorage.set( |
| 166 | + tkeyPubX, |
| 167 | + JSON.stringify({ |
| 168 | + factorKey: factorKey.toString("hex").padStart(64, "0"), |
| 169 | + } as TkeyLocalStoreData) |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +export async function asyncGetFactor(mpcCoreKit: ICoreKit, storageKey: IAsyncStorage): Promise<string | undefined> { |
| 174 | + const metadata = mpcCoreKit.tKey.getMetadata(); |
| 175 | + const currentStorage = AsyncStorage.getInstance("mpc_corekit_store", storageKey); |
| 176 | + |
| 177 | + const tkeyPubX = metadata.pubKey.x.toString(16, FIELD_ELEMENT_HEX_LEN); |
| 178 | + const tKeyLocalStoreString = await currentStorage.get<string>(tkeyPubX); |
| 179 | + const tKeyLocalStore = JSON.parse(tKeyLocalStoreString || "{}") as TkeyLocalStoreData; |
| 180 | + return tKeyLocalStore.factorKey; |
| 181 | +} |
| 182 | + |
102 | 183 | export async function storeWebBrowserFactor(factorKey: BN, mpcCoreKit: ICoreKit, storageKey: SupportedStorageType = "local"): Promise<void> { |
103 | 184 | const metadata = mpcCoreKit.tKey.getMetadata(); |
104 | 185 | const currentStorage = BrowserStorage.getInstance("mpc_corekit_store", storageKey); |
|
0 commit comments