From 6e5000c9ad6e77a0fbea3f8a097b0c303bbde7db Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 5 May 2023 16:17:18 +0800 Subject: [PATCH] feat: refactor Webstorage --- packages/web-storage/src/WebStorageModule.ts | 34 +++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/web-storage/src/WebStorageModule.ts b/packages/web-storage/src/WebStorageModule.ts index 8c0a7d6e..de454d09 100644 --- a/packages/web-storage/src/WebStorageModule.ts +++ b/packages/web-storage/src/WebStorageModule.ts @@ -1,4 +1,4 @@ -import { BNString, DeviceShareDescription, IModule, ITKeyApi, prettyPrintError, ShareStore, StringifiedType } from "@tkey/common-types"; +import { BNString, DeviceShareDescription, ITKeyApi, prettyPrintError, ShareStore, StringifiedType } from "@tkey/common-types"; import BN from "bn.js"; import WebStorageError from "./errors"; @@ -7,7 +7,7 @@ import { getShareFromLocalStorage, storeShareOnLocalStorage } from "./LocalStora export const WEB_STORAGE_MODULE_NAME = "webStorage"; -class WebStorageModule implements IModule { +class WebStorageModule { moduleName: string; tbSDK: ITKeyApi; @@ -40,15 +40,11 @@ class WebStorageModule implements IModule { } catch (error) {} } - setModuleReferences(tbSDK: ITKeyApi): void { - this.tbSDK = tbSDK; - } - // eslint-disable-next-line async initialize(): Promise {} - async storeDeviceShare(deviceShareStore: ShareStore, customDeviceInfo?: StringifiedType): Promise { - const metadata = this.tbSDK.getMetadata(); + async storeDeviceShare(tkey: ITKeyApi, deviceShareStore: ShareStore, customDeviceInfo?: StringifiedType): Promise { + const metadata = tkey.getMetadata(); const tkeypubx = metadata.pubKey.x.toString("hex"); await storeShareOnLocalStorage(deviceShareStore, tkeypubx); const shareDescription: DeviceShareDescription = { @@ -59,18 +55,18 @@ class WebStorageModule implements IModule { if (customDeviceInfo) { shareDescription.customDeviceInfo = JSON.stringify(customDeviceInfo); } - await this.tbSDK.addShareDescription(deviceShareStore.share.shareIndex.toString("hex"), JSON.stringify(shareDescription), true); + await tkey.addShareDescription(deviceShareStore.share.shareIndex.toString("hex"), JSON.stringify(shareDescription), true); } - async storeDeviceShareOnFileStorage(shareIndex: BNString): Promise { - const metadata = this.tbSDK.getMetadata(); + async storeDeviceShareOnFileStorage(tkey: ITKeyApi, shareIndex: BNString): Promise { + const metadata = tkey.getMetadata(); const tkeypubx = metadata.pubKey.x.toString("hex"); - const shareStore = this.tbSDK.outputShareStore(new BN(shareIndex)); + const shareStore = tkey.outputShareStore(new BN(shareIndex)); return storeShareOnFileStorage(shareStore, tkeypubx); } - async getDeviceShare(): Promise { - const metadata = this.tbSDK.getMetadata(); + async getDeviceShare(tkey: ITKeyApi): Promise { + const metadata = tkey.getMetadata(); const tkeypubx = metadata.pubKey.x.toString("hex"); let shareStore: ShareStore; try { @@ -94,16 +90,16 @@ class WebStorageModule implements IModule { return shareStore; } - async inputShareFromWebStorage(): Promise { - const shareStore = await this.getDeviceShare(); + async inputShareFromWebStorage(tkey: ITKeyApi): Promise { + const shareStore = await this.getDeviceShare(tkey); let latestShareStore = shareStore; - const metadata = this.tbSDK.getMetadata(); + const metadata = tkey.getMetadata(); if (metadata.getLatestPublicPolynomial().getPolynomialID() !== shareStore.polynomialID) { - latestShareStore = (await this.tbSDK.catchupToLatestShare({ shareStore, includeLocalMetadataTransitions: true })).latestShare; + latestShareStore = (await tkey.catchupToLatestShare({ shareStore, includeLocalMetadataTransitions: true })).latestShare; const tkeypubx = metadata.pubKey.x.toString("hex"); await storeShareOnLocalStorage(latestShareStore, tkeypubx); } - this.tbSDK.inputShareStore(latestShareStore); + tkey.inputShareStore(latestShareStore); } }