Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions packages/web-storage/src/WebStorageModule.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -40,15 +40,11 @@ class WebStorageModule implements IModule {
} catch (error) {}
}

setModuleReferences(tbSDK: ITKeyApi): void {
this.tbSDK = tbSDK;
}

// eslint-disable-next-line
async initialize(): Promise<void> {}

async storeDeviceShare(deviceShareStore: ShareStore, customDeviceInfo?: StringifiedType): Promise<void> {
const metadata = this.tbSDK.getMetadata();
async storeDeviceShare(tkey: ITKeyApi, deviceShareStore: ShareStore, customDeviceInfo?: StringifiedType): Promise<void> {
const metadata = tkey.getMetadata();
const tkeypubx = metadata.pubKey.x.toString("hex");
await storeShareOnLocalStorage(deviceShareStore, tkeypubx);
const shareDescription: DeviceShareDescription = {
Expand All @@ -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<void> {
const metadata = this.tbSDK.getMetadata();
async storeDeviceShareOnFileStorage(tkey: ITKeyApi, shareIndex: BNString): Promise<void> {
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<ShareStore> {
const metadata = this.tbSDK.getMetadata();
async getDeviceShare(tkey: ITKeyApi): Promise<ShareStore> {
const metadata = tkey.getMetadata();
const tkeypubx = metadata.pubKey.x.toString("hex");
let shareStore: ShareStore;
try {
Expand All @@ -94,16 +90,16 @@ class WebStorageModule implements IModule {
return shareStore;
}

async inputShareFromWebStorage(): Promise<void> {
const shareStore = await this.getDeviceShare();
async inputShareFromWebStorage(tkey: ITKeyApi): Promise<void> {
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);
}
}

Expand Down