Skip to content

Commit cd4332d

Browse files
authored
Merge pull request #306 from tkey/feat/multicurve-json
fix: add stringified value check
2 parents 5e037af + a7ecc46 commit cd4332d

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

packages/core/src/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export class Metadata implements IMetadata {
354354
version: METADATA_VERSION,
355355
};
356356

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

360360
/**

packages/core/src/tssMetadata.ts

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
import { FactorEnc, ISerializable, ITssMetadata, KeyType, Point, StringifiedType } from "@tkey/common-types";
1+
import { EncryptedMessage, FactorEnc, FactorEncType, ISerializable, ITssMetadata, KeyType, Point, StringifiedType } from "@tkey/common-types";
2+
3+
export const FromJsonEncryptedMessage = (value: StringifiedType): EncryptedMessage => {
4+
const { ciphertext, ephemPublicKey, iv, mac } = value;
5+
if (typeof ciphertext !== "string") throw new Error("ciphertext is not a string");
6+
if (typeof ephemPublicKey !== "string") throw new Error("ephemPublicKey is not a string");
7+
if (typeof iv !== "string") throw new Error("iv is not a string");
8+
if (typeof mac !== "string") throw new Error("mac is not a string");
9+
10+
return {
11+
ciphertext,
12+
ephemPublicKey,
13+
iv,
14+
mac,
15+
};
16+
};
17+
18+
export const FromJsonFactorEnc = (value: StringifiedType): FactorEnc => {
19+
const { tssIndex, type, userEnc, serverEncs } = value;
20+
if (typeof tssIndex !== "number") throw new Error("tssIndex is not a number");
21+
if (typeof type !== "string") throw new Error("type is not a string");
22+
if (typeof userEnc !== "object") throw new Error("userEnc is not a string");
23+
if (!Array.isArray(serverEncs)) throw new Error("serverEncs is not an array");
24+
25+
return { type: type as FactorEncType, tssIndex, userEnc: FromJsonEncryptedMessage(userEnc), serverEncs: serverEncs.map(FromJsonEncryptedMessage) };
26+
};
227

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

55+
if (typeof tssTag !== "string") throw new Error("tssTag is not a string");
56+
if (typeof tssNonce !== "number") throw new Error("tssNonce is not a number");
57+
if (typeof factorEncs !== "object") throw new Error("factorEncs is not an object");
58+
59+
if (!(tssKeyType in KeyType)) {
60+
throw new Error("tssKeyType is not a valid KeyType");
61+
}
62+
63+
if (!Array.isArray(tssPolyCommits)) {
64+
throw new Error("tssPolyCommits is not an array");
65+
}
66+
67+
if (!Array.isArray(factorPubs)) {
68+
throw new Error("factorPubs is not an array");
69+
}
70+
71+
for (const key in factorEncs) {
72+
const factorEnc = factorEncs[key];
73+
factorEncs[key] = FromJsonFactorEnc(factorEnc);
74+
}
75+
3076
const tssMetadata = new TssMetadata({
3177
tssTag,
3278
tssKeyType,
3379
tssNonce,
34-
tssPolyCommits,
80+
tssPolyCommits: (tssPolyCommits as Point[]).map((obj) => Point.fromJSON(obj)),
81+
factorPubs: (factorPubs as Point[]).map((obj) => Point.fromJSON(obj)),
3582
factorEncs,
36-
factorPubs,
3783
});
3884

39-
if (tssPolyCommits) {
40-
tssMetadata.tssPolyCommits = (tssPolyCommits as Point[]).map((obj) => new Point(obj.x, obj.y));
41-
}
42-
if (factorPubs) {
43-
tssMetadata.factorPubs = (factorPubs as Point[]).map((obj) => new Point(obj.x, obj.y));
44-
}
45-
46-
if (factorEncs) tssMetadata.factorEncs = factorEncs;
47-
4885
return tssMetadata;
4986
}
5087

@@ -53,8 +90,8 @@ export class TssMetadata implements ITssMetadata, ISerializable {
5390
tssTag: this.tssTag,
5491
tssKeyType: this.tssKeyType,
5592
tssNonce: this.tssNonce,
56-
tssPolyCommits: this.tssPolyCommits,
57-
factorPubs: this.factorPubs,
93+
tssPolyCommits: this.tssPolyCommits.map((pub) => pub.toJSON()),
94+
factorPubs: this.factorPubs.map((pub) => pub.toJSON()),
5895
factorEncs: this.factorEncs,
5996
};
6097
}

packages/core/test/metadataFormat.test.js

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)