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
328export 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 }
0 commit comments