Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 6 additions & 7 deletions package-lock.json

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

28 changes: 28 additions & 0 deletions packages/common-types/src/baseTypes/aggregateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ISerializable,
IServiceProvider,
IStorageLayer,
KeyType,
PolyIDAndShares,
PolynomialID,
ShareDescriptionMap,
Expand Down Expand Up @@ -62,6 +63,30 @@ export type FactorEnc = {
serverEncs: EncryptedMessage[];
};

export interface ITssMetadata {
Copy link
Copy Markdown
Contributor

@lwin-kyaw lwin-kyaw Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good if we can provide some comments for the interface fields

tssKeyTypes: {
[tssTag: string]: KeyType;
};
Comment thread
ieow marked this conversation as resolved.
Outdated

tssNonces: {
[tssTag: string]: number;
};

tssPolyCommits: {
[tssTag: string]: Point[];
};

factorPubs: {
[tssTag: string]: Point[];
};

factorEncs: {
[tssTag: string]: {
[factorPubID: string]: FactorEnc;
};
};
}

export interface IMetadata extends ISerializable {
pubKey: Point;

Expand All @@ -85,6 +110,8 @@ export interface IMetadata extends ISerializable {

nonce: number;

version: number;

getShareIndexesForPolynomial(polyID: PolynomialID): string[];
getLatestPublicPolynomial(): PublicPolynomial;
addPublicShare(polynomialID: PolynomialID, publicShare: PublicShare): void;
Expand Down Expand Up @@ -112,6 +139,7 @@ export interface IMetadata extends ISerializable {
[factorPubID: string]: FactorEnc;
};
}): void;
getTssData(keyType: KeyType): ITssMetadata;
}

export type InitializeNewKeyResult = {
Expand Down
176 changes: 141 additions & 35 deletions packages/core/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
FactorEnc,
getPubKeyPoint,
IMetadata,
ISerializable,
ITssMetadata,
KeyType,
Point,
PolyIDAndShares,
Expand All @@ -27,6 +29,97 @@ import stringify from "json-stable-stringify";
import CoreError from "./errors";
import { polyCommitmentEval } from "./lagrangeInterpolatePolynomial";

const METADATA_VERSION = 1;

export class TssMetadata implements ITssMetadata, ISerializable {
tssKeyTypes: {
[tssTag: string]: KeyType;
};

tssNonces: {
[tssTag: string]: number;
};

tssPolyCommits: {
[tssTag: string]: Point[];
};

factorPubs: {
[tssTag: string]: Point[];
};

factorEncs: {
[tssTag: string]: {
[factorPubID: string]: FactorEnc;
};
};

constructor() {
this.tssKeyTypes = {};
this.tssNonces = {};
this.tssPolyCommits = {};
this.factorPubs = {};
this.factorEncs = {};
}

static fromJSON(value: StringifiedType): TssMetadata {
const { tssKeyTypes, tssPolyCommits, tssNonces, factorPubs, factorEncs } = value;
const tssMetadata = new TssMetadata();

if (tssKeyTypes) {
for (const key in tssKeyTypes) {
tssMetadata.tssKeyTypes[key] = tssKeyTypes[key];
}
}
if (tssPolyCommits) {
for (const key in tssPolyCommits) {
tssMetadata.tssPolyCommits[key] = (tssPolyCommits as Record<string, Point[]>)[key].map((obj) => new Point(obj.x, obj.y));
}
}
if (tssNonces) {
for (const key in tssNonces) {
tssMetadata.tssNonces[key] = tssNonces[key];
}
}
if (factorPubs) {
for (const key in factorPubs) {
tssMetadata.factorPubs[key] = (factorPubs as Record<string, Point[]>)[key].map((obj) => new Point(obj.x, obj.y));
}
}
if (factorEncs) tssMetadata.factorEncs = factorEncs;

return tssMetadata;
}

toJSON(): StringifiedType {
return {
tssKeyTypes: this.tssKeyTypes,
tssNonces: this.tssNonces,
tssPolyCommits: this.tssPolyCommits,
factorPubs: this.factorPubs,
factorEncs: this.factorEncs,
};
}

update(tssData: {
tssTag: string;
tssKeyType?: KeyType;
tssNonce?: number;
tssPolyCommits?: Point[];
factorPubs?: Point[];
factorEncs?: {
[factorPubID: string]: FactorEnc;
};
}) {
const { tssKeyType, tssTag, tssNonce, tssPolyCommits, factorPubs, factorEncs } = tssData;
if (tssKeyType) this.tssKeyTypes[tssTag] = tssKeyType;
if (tssNonce !== undefined) this.tssNonces[tssTag] = tssNonce;
if (tssPolyCommits) this.tssPolyCommits[tssTag] = tssPolyCommits;
if (factorPubs) this.factorPubs[tssTag] = factorPubs;
if (factorEncs) this.factorEncs[tssTag] = factorEncs;
}
}

class Metadata implements IMetadata {
pubKey: Point;

Expand Down Expand Up @@ -73,6 +166,13 @@ class Metadata implements IMetadata {
};
};

tss?: {
Comment thread
ieow marked this conversation as resolved.
Outdated
secp256k1: TssMetadata;
ed25519: TssMetadata;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a Record of KeyType and TssMetadata instead of fixing the entries here?

};

version = METADATA_VERSION;

constructor(input: Point) {
this.publicPolynomials = {};
this.publicShares = {};
Expand All @@ -90,41 +190,46 @@ class Metadata implements IMetadata {
}

static fromJSON(value: StringifiedType): Metadata {
const { pubKey, polyIDList, generalStore, tkeyStore, scopedStore, nonce, tssKeyTypes, tssPolyCommits, tssNonces, factorPubs, factorEncs } = value;
const {
pubKey,
polyIDList,
generalStore,
tkeyStore,
scopedStore,
nonce,
tssKeyTypes,
tssPolyCommits,
tssNonces,
factorPubs,
factorEncs,
tss,
version,
} = value;
const point = Point.fromSEC1(secp256k1, pubKey);
const metadata = new Metadata(point);
metadata.version = version || METADATA_VERSION;

const unserializedPolyIDList: PolyIDAndShares[] = [];

if (generalStore) metadata.generalStore = generalStore;
if (tkeyStore) metadata.tkeyStore = tkeyStore;
if (scopedStore) metadata.scopedStore = scopedStore;
if (nonce) metadata.nonce = nonce;

if (tssKeyTypes) {
metadata.tssKeyTypes = {};
for (const key in tssKeyTypes) {
metadata.tssKeyTypes[key] = tssKeyTypes[key];
}
}
if (tssPolyCommits) {
metadata.tssPolyCommits = {};
for (const key in tssPolyCommits) {
metadata.tssPolyCommits[key] = (tssPolyCommits as Record<string, Point[]>)[key].map((obj) => new Point(obj.x, obj.y));
}
}
if (tssNonces) {
metadata.tssNonces = {};
for (const key in tssNonces) {
metadata.tssNonces[key] = tssNonces[key];
}
}
if (factorPubs) {
metadata.factorPubs = {};
for (const key in factorPubs) {
metadata.factorPubs[key] = (factorPubs as Record<string, Point[]>)[key].map((obj) => new Point(obj.x, obj.y));
if (version === 1) {
metadata.tss = tss;
} else if (tssKeyTypes) {
const tssData = TssMetadata.fromJSON({
tssKeyTypes,
tssPolyCommits,
tssNonces,
factorPubs,
factorEncs,
});
if (tssData.tssKeyTypes.default) {
metadata.tss[tssData.tssKeyTypes.default] = tssData;
}
}
if (factorEncs) metadata.factorEncs = factorEncs;

for (let i = 0; i < polyIDList.length; i += 1) {
const serializedPolyID: string = polyIDList[i];
Expand Down Expand Up @@ -327,11 +432,8 @@ class Metadata implements IMetadata {
generalStore: this.generalStore,
tkeyStore: this.tkeyStore,
nonce: this.nonce,
...(this.tssKeyTypes && { tssKeyTypes: this.tssKeyTypes }),
...(this.tssNonces && { tssNonces: this.tssNonces }),
...(this.tssPolyCommits && { tssPolyCommits: this.tssPolyCommits }),
...(this.factorPubs && { factorPubs: this.factorPubs }),
...(this.factorEncs && { factorEncs: this.factorEncs }),
tss: this.tss,
version: this.version,
};
}

Expand All @@ -348,12 +450,16 @@ class Metadata implements IMetadata {
[factorPubID: string]: FactorEnc;
};
}): void {
const { tssKeyType, tssTag, tssNonce, tssPolyCommits, factorPubs, factorEncs } = tssData;
if (tssKeyType) this.tssKeyTypes[tssTag] = tssKeyType;
if (tssNonce !== undefined) this.tssNonces[tssTag] = tssNonce;
if (tssPolyCommits) this.tssPolyCommits[tssTag] = tssPolyCommits;
if (factorPubs) this.factorPubs[tssTag] = factorPubs;
if (factorEncs) this.factorEncs[tssTag] = factorEncs;
if (tssData.tssKeyType === KeyType.ed25519) this.tss.ed25519.update(tssData);
else if (tssData.tssKeyType === KeyType.secp256k1) this.tss.secp256k1.update(tssData);
}

getTssData(tssKeyType: KeyType): ITssMetadata {
if (tssKeyType === KeyType.secp256k1) {
return this.tss?.secp256k1;
} else if (tssKeyType === KeyType.ed25519) {
return this.tss?.ed25519;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/tss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@toruslabs/customauth": "^20.3.0",
"@toruslabs/http-helpers": "^7.0.0",
"@toruslabs/rss-client": "^2.0.1",
"@toruslabs/torus.js": "^15.1.0",
"@toruslabs/torus.js": "file:../../../torus.js/toruslabs-torus.js-15.1.1.tgz",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove! This PR should not be marked as ready for review!

"@types/bn.js": "^5.1.5",
"bn.js": "^5.2.1",
"elliptic": "^6.5.5",
Expand Down
6 changes: 4 additions & 2 deletions packages/tss/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Point, StringifiedType } from "@tkey/common-types";
import { KeyType, Point, StringifiedType } from "@tkey/common-types";
import { TorusServiceProvider } from "@tkey/service-provider-torus";
import { AggregateLoginParams, SubVerifierDetails, TorusAggregateLoginResponse, TorusLoginResponse } from "@toruslabs/customauth";
import { PointHex } from "@toruslabs/rss-client";
Expand Down Expand Up @@ -57,7 +57,8 @@ export class TSSTorusServiceProvider extends TorusServiceProvider {

async getTSSPubKey(
tssTag: string,
tssNonce: number
tssNonce: number,
keyType: KeyType
): Promise<{
pubKey: Point;
nodeIndexes?: number[];
Expand All @@ -69,6 +70,7 @@ export class TSSTorusServiceProvider extends TorusServiceProvider {
verifier: this.verifierName,
verifierId: this.verifierId,
extendedVerifierId: getExtendedVerifierId(this.verifierId, tssTag, tssNonce),
keyType,
});

return {
Expand Down
Loading