Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/core/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class Metadata implements IMetadata {
version: METADATA_VERSION,
};

return Object.keys(this.tss ?? {}).length > 0 ? { ...jsonObject, tss: this.tss } : jsonObject;
return Object.keys(this.tss ?? {}).length > 0 ? { ...jsonObject, tss: JSON.parse(JSON.stringify(this.tss)) } : jsonObject;
}

/**
Expand Down
65 changes: 51 additions & 14 deletions packages/core/src/tssMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import { FactorEnc, ISerializable, ITssMetadata, KeyType, Point, StringifiedType } from "@tkey/common-types";
import { EncryptedMessage, FactorEnc, FactorEncType, ISerializable, ITssMetadata, KeyType, Point, StringifiedType } from "@tkey/common-types";

export const FromJsonEncryptedMessage = (value: StringifiedType): EncryptedMessage => {
const { ciphertext, ephemPublicKey, iv, mac } = value;
if (typeof ciphertext !== "string") throw new Error("ciphertext is not a string");
if (typeof ephemPublicKey !== "string") throw new Error("ephemPublicKey is not a string");
if (typeof iv !== "string") throw new Error("iv is not a string");
if (typeof mac !== "string") throw new Error("mac is not a string");

return {
ciphertext,
ephemPublicKey,
iv,
mac,
};
};

export const FromJsonFactorEnc = (value: StringifiedType): FactorEnc => {
const { tssIndex, type, userEnc, serverEncs } = value;
if (typeof tssIndex !== "number") throw new Error("tssIndex is not a number");
if (typeof type !== "string") throw new Error("type is not a string");
if (typeof userEnc !== "object") throw new Error("userEnc is not a string");
if (!Array.isArray(serverEncs)) throw new Error("serverEncs is not an array");

return { type: type as FactorEncType, tssIndex, userEnc: FromJsonEncryptedMessage(userEnc), serverEncs: serverEncs.map(FromJsonEncryptedMessage) };
};

export class TssMetadata implements ITssMetadata, ISerializable {
tssTag: string;
Expand Down Expand Up @@ -27,24 +52,36 @@ export class TssMetadata implements ITssMetadata, ISerializable {
static fromJSON(value: StringifiedType): TssMetadata {
const { tssTag, tssKeyType, tssPolyCommits, tssNonce, factorPubs, factorEncs } = value;

if (typeof tssTag !== "string") throw new Error("tssTag is not a string");
if (typeof tssNonce !== "number") throw new Error("tssNonce is not a number");
if (typeof factorEncs !== "object") throw new Error("factorEncs is not an object");

if (!(tssKeyType in KeyType)) {
throw new Error("tssKeyType is not a valid KeyType");
}

if (!Array.isArray(tssPolyCommits)) {
throw new Error("tssPolyCommits is not an array");
}

if (!Array.isArray(factorPubs)) {
throw new Error("factorPubs is not an array");
}

for (const key in factorEncs) {
const factorEnc = factorEncs[key];
factorEncs[key] = FromJsonFactorEnc(factorEnc);
}

const tssMetadata = new TssMetadata({
tssTag,
tssKeyType,
tssNonce,
tssPolyCommits,
tssPolyCommits: (tssPolyCommits as Point[]).map((obj) => Point.fromJSON(obj)),
factorPubs: (factorPubs as Point[]).map((obj) => Point.fromJSON(obj)),
factorEncs,
factorPubs,
});

if (tssPolyCommits) {
tssMetadata.tssPolyCommits = (tssPolyCommits as Point[]).map((obj) => new Point(obj.x, obj.y));
}
if (factorPubs) {
tssMetadata.factorPubs = (factorPubs as Point[]).map((obj) => new Point(obj.x, obj.y));
}

if (factorEncs) tssMetadata.factorEncs = factorEncs;

return tssMetadata;
}

Expand All @@ -53,8 +90,8 @@ export class TssMetadata implements ITssMetadata, ISerializable {
tssTag: this.tssTag,
tssKeyType: this.tssKeyType,
tssNonce: this.tssNonce,
tssPolyCommits: this.tssPolyCommits,
factorPubs: this.factorPubs,
tssPolyCommits: this.tssPolyCommits.map((pub) => pub.toJSON()),
factorPubs: this.factorPubs.map((pub) => pub.toJSON()),
factorEncs: this.factorEncs,
};
}
Expand Down
9 changes: 6 additions & 3 deletions packages/core/test/metadataFormat.test.js

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

Loading