Skip to content
Draft
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
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/common-types/src/baseTypes/aggregateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export type RefreshMiddlewareMap = {
};

export type ReconstructKeyMiddlewareMap = {
[moduleName: string]: () => Promise<BN[]>;
// eslint-disable-next-line no-use-before-define
[moduleName: string]: (tkey: ITKeyApi) => Promise<BN[]>;
};

export type ShareSerializationMiddleware = {
Expand Down Expand Up @@ -267,7 +268,7 @@ export interface ITKeyApi {
moduleName: string,
middleware: (generalStore: unknown, oldShareStores: ShareStoreMap, newShareStores: ShareStoreMap) => unknown
): void;
_addReconstructKeyMiddleware(moduleName: string, middleware: () => Promise<Array<BN>>): void;
_addReconstructKeyMiddleware(moduleName: string, middleware: (tkey: ITKeyApi) => Promise<Array<BN>>): void;
_addShareSerializationMiddleware(
serialize: (share: BN, type: string) => Promise<unknown>,
deserialize: (serializedShare: unknown, type: string) => Promise<BN>
Expand Down
8 changes: 8 additions & 0 deletions packages/common-types/src/baseTypes/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export interface TorusServiceProviderArgs extends ServiceProviderArgs {
nodePubKeys?: PointHex[];
}

export enum TkeyStatus {
NOT_INITIALIZED = "NOT_INITIALIZED",
// READ MODE
INITIALIZED = "INITIALIZED",
// WRITE MODE
RECONSTRUCTED = "RECONSTRUCTED",
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type StringifiedType = Record<string, any>;

Expand Down
55 changes: 50 additions & 5 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
ShareStorePolyIDShareIndexMap,
StringifiedType,
TKeyArgs,
TkeyStatus,
TkeyStoreItemType,
toPrivKeyECC,
} from "@tkey/common-types";
Expand Down Expand Up @@ -220,6 +221,20 @@ class ThresholdKey implements ITKey {
throw CoreError.metadataUndefined();
}

getTkeyStatus(): TkeyStatus {
try {
const keyDetails = this.getKeyDetails();
if (keyDetails.totalShares < keyDetails.threshold) {
// READ MODE
return TkeyStatus.INITIALIZED;
}
// WRITE MODE
return TkeyStatus.RECONSTRUCTED;
} catch {
return TkeyStatus.NOT_INITIALIZED;
}
}

async initialize(params?: {
withShare?: ShareStore;
importKey?: BN;
Expand All @@ -232,7 +247,7 @@ class ThresholdKey implements ITKey {
deviceTSSShare?: BN;
deviceTSSIndex?: number;
factorPub?: Point;
}): Promise<KeyDetails> {
}): Promise<KeyDetails & { deviceShare?: ShareStore; userShare?: ShareStore }> {
// setup initial params/states
const p = params || {};

Expand Down Expand Up @@ -287,7 +302,7 @@ class ThresholdKey implements ITKey {
throw CoreError.default("key has not been generated yet");
}
// no metadata set, assumes new user
await this._initializeNewKey({
const result = await this._initializeNewKey({
initializeModules: true,
importedKey: importKey,
delete1OutOf1: p.delete1OutOf1,
Expand All @@ -296,14 +311,44 @@ class ThresholdKey implements ITKey {
const { factorEncs, factorPubs, tssPolyCommits } = await this._initializeNewTSSKey(this.tssTag, deviceTSSShare, factorPub, deviceTSSIndex);
this.metadata.addTSSData({ tssTag: this.tssTag, tssNonce: 0, tssPolyCommits, factorPubs, factorEncs });
}
return this.getKeyDetails();
const keyDetails = this.getKeyDetails();
return { ...keyDetails, deviceShare: result.deviceShare, userShare: result.userShare };
}
// else we continue with catching up share and metadata
shareStore = ShareStore.fromJSON(rawServiceProviderShare);
} else {
throw CoreError.default("Input is not supported");
}

return this.initializeWithShareStore({
shareStore,
transitionMetadata,
previouslyFetchedCloudMetadata,
previousLocalMetadataTransitions,
useTSS,
deviceTSSShare,
factorPub,
});
}

async initializeWithShareStore(params: {
shareStore: ShareStore;
transitionMetadata?: Metadata;
previouslyFetchedCloudMetadata?: Metadata;
previousLocalMetadataTransitions?: LocalMetadataTransitions;
useTSS?: boolean;
deviceTSSShare?: BN;
factorPub?: Point;
}) {
const { shareStore, transitionMetadata, previouslyFetchedCloudMetadata, previousLocalMetadataTransitions, useTSS, deviceTSSShare, factorPub } =
params;

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 @@ -578,7 +623,7 @@ class ThresholdKey implements ITKey {
await Promise.all(
Object.keys(this._reconstructKeyMiddleware).map(async (x) => {
if (Object.prototype.hasOwnProperty.call(this._reconstructKeyMiddleware, x)) {
const extraKeys = await this._reconstructKeyMiddleware[x]();
const extraKeys = await this._reconstructKeyMiddleware[x](this);
returnObject[x] = extraKeys;
returnObject.allKeys.push(...extraKeys);
}
Expand Down Expand Up @@ -1471,7 +1516,7 @@ class ThresholdKey implements ITKey {
this._refreshMiddleware[moduleName] = middleware;
}

_addReconstructKeyMiddleware(moduleName: string, middleware: () => Promise<BN[]>): void {
_addReconstructKeyMiddleware(moduleName: string, middleware: (tkey: ITKeyApi) => Promise<BN[]>): void {
this._reconstructKeyMiddleware[moduleName] = middleware;
}

Expand Down
Loading