Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion packages/common-types/src/baseTypes/aggregateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export type KeyDetails = {
threshold: number;
totalShares: number;
shareDescriptions: ShareDescriptionMap;
deviceShare?: ShareStore;
userShare?: ShareStore;
};

export type TKeyArgs = {
Expand Down Expand Up @@ -249,7 +251,6 @@ export interface ITKeyApi {
}): Promise<CatchupToLatestShareResult>;
_syncShareMetadata(adjustScopedStore?: (ss: unknown) => unknown): Promise<void>;
inputShareStoreSafe(shareStore: ShareStore, autoUpdateMetadata?: boolean): Promise<void>;
_setDeviceStorage(storeDeviceStorage: (deviceShareStore: ShareStore) => Promise<void>): void;
addShareDescription(shareIndex: string, description: string, updateMetadata?: boolean): Promise<void>;
_addRefreshMiddleware(
moduleName: string,
Expand Down
38 changes: 21 additions & 17 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ class ThresholdKey implements ITKey {

_shareSerializationMiddleware: ShareSerializationMiddleware;

storeDeviceShare: (deviceShareStore: ShareStore, customDeviceInfo?: StringifiedType) => Promise<void>;

haveWriteMetadataLock: string;

serverTimeOffset?: number = 0;
Expand All @@ -99,7 +97,6 @@ class ThresholdKey implements ITKey {
this._refreshMiddleware = {};
this._reconstructKeyMiddleware = {};
this._shareSerializationMiddleware = undefined;
this.storeDeviceShare = undefined;
this._localMetadataTransitions = [[], []];
this.setModuleReferences(); // Providing ITKeyApi access to modules
this.haveWriteMetadataLock = "";
Expand Down Expand Up @@ -252,16 +249,35 @@ class ThresholdKey implements ITKey {
if (neverInitializeNewKey) {
throw CoreError.default("key has not been generated yet");
}

// no metadata set, assumes new user
await this._initializeNewKey({ initializeModules: true, importedKey: importKey, delete1OutOf1: p.delete1OutOf1 });
return this.getKeyDetails();
const newKeyDetails = await this._initializeNewKey({ initializeModules: true, importedKey: importKey, delete1OutOf1: p.delete1OutOf1 });
const keyDetail = this.getKeyDetails();
keyDetail.deviceShare = newKeyDetails.deviceShare;
keyDetail.userShare = newKeyDetails.userShare;
return keyDetail;
}
// else we continue with catching up share and metadata
shareStore = ShareStore.fromJSON(rawServiceProviderShare);
} else {
throw CoreError.default("Input is not supported");
}

await this.postInit(shareStore, transitionMetadata, previouslyFetchedCloudMetadata, previousLocalMetadataTransitions);
}

async postInit(
shareStore: ShareStore,
transitionMetadata?: Metadata,
previouslyFetchedCloudMetadata?: Metadata,
previousLocalMetadataTransitions?: LocalMetadataTransitions
) {
const previousLocalMetadataTransitionsExists =
previousLocalMetadataTransitions && previousLocalMetadataTransitions[0].length > 0 && previousLocalMetadataTransitions[1].length > 0;
const reinitializing = transitionMetadata && previousLocalMetadataTransitionsExists; // are we reinitializing the SDK?
// in the case we're reinitializing whilst newKeyAssign has not been synced
const reinitializingWithNewKeyAssign = reinitializing && previouslyFetchedCloudMetadata === undefined;

// We determine the latest metadata on the SDK and if there has been
// needed transitions to include
let currentMetadata: Metadata;
Expand Down Expand Up @@ -690,10 +706,6 @@ class ThresholdKey implements ITKey {
this.inputShareStore(new ShareStore(shares[shareIndex.toString("hex")], poly.getPolynomialID()));
}

if (this.storeDeviceShare) {
await this.storeDeviceShare(new ShareStore(shares[shareIndexes[1].toString("hex")], poly.getPolynomialID()));
}

const result = {
privKey: this.privKey,
deviceShare: new ShareStore(shares[shareIndexes[1].toString("hex")], poly.getPolynomialID()),
Expand Down Expand Up @@ -1081,13 +1093,6 @@ class ThresholdKey implements ITKey {
};
}

_setDeviceStorage(storeDeviceStorage: (deviceShareStore: ShareStore) => Promise<void>): void {
if (this.storeDeviceShare) {
throw CoreError.default("storeDeviceShare already set");
}
this.storeDeviceShare = storeDeviceStorage;
}

async addShareDescription(shareIndex: string, description: string, updateMetadata?: boolean): Promise<void> {
if (!this.metadata) {
throw CoreError.metadataUndefined();
Expand Down Expand Up @@ -1305,7 +1310,6 @@ class ThresholdKey implements ITKey {
outputShareStore: this.outputShareStore.bind(this),
inputShare: this.inputShare.bind(this),
outputShare: this.outputShare.bind(this),
_setDeviceStorage: this._setDeviceStorage.bind(this),
encrypt: this.encrypt.bind(this),
decrypt: this.decrypt.bind(this),
getTKeyStore: this.getTKeyStore.bind(this),
Expand Down
1 change: 0 additions & 1 deletion packages/web-storage/src/WebStorageModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class WebStorageModule implements IModule {

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

// eslint-disable-next-line
Expand Down