-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.test.ts
More file actions
3265 lines (2895 loc) · 107 KB
/
index.test.ts
File metadata and controls
3265 lines (2895 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Pool } from "pg";
import type {
TriplesStep10Request,
TriplesStep11Request,
TriplesStep1Request,
TriplesStep2Request,
TriplesStep3Request,
TriplesStep4Request,
TriplesStep5Request,
TriplesStep6Request,
TriplesStep7Request,
TriplesStep8Request,
TriplesStep9Request,
} from "@oko-wallet/oko-types/tss";
import {
TriplesStageStatus,
TssSessionState,
TssStageType,
} from "@oko-wallet/oko-types/tss";
import {
Participant,
type TECDSATriplesState,
} from "@oko-wallet/tecdsa-interface";
import {
napiRunKeygenClientCentralized,
napiRunTriples2ClientStep1,
napiRunTriples2ClientStep10,
napiRunTriples2ClientStep11,
napiRunTriples2ClientStep2,
napiRunTriples2ClientStep3,
napiRunTriples2ClientStep4,
napiRunTriples2ClientStep5,
napiRunTriples2ClientStep6,
napiRunTriples2ClientStep7,
napiRunTriples2ClientStep8,
napiRunTriples2ClientStep9,
} from "@oko-wallet/cait-sith-keplr-addon/addon";
import {
createTssSession,
createTssStage,
getTssStageWithSessionData,
} from "@oko-wallet/oko-pg-interface/tss";
import { createPgConn } from "@oko-wallet/postgres-lib";
import type { WalletStatus } from "@oko-wallet/oko-types/wallets";
import { createWallet } from "@oko-wallet/oko-pg-interface/oko_wallets";
import { createUser } from "@oko-wallet/oko-pg-interface/oko_users";
import { insertCustomer } from "@oko-wallet/oko-pg-interface/customers";
import { insertKeyShareNodeMeta } from "@oko-wallet/oko-pg-interface/key_share_node_meta";
import { resetPgDatabase } from "@oko-wallet-tss-api/testing/database";
import { testPgConfig } from "@oko-wallet-tss-api/database/test_config";
import {
runTriplesStep1,
runTriplesStep10,
runTriplesStep11,
runTriplesStep2,
runTriplesStep3,
runTriplesStep4,
runTriplesStep5,
runTriplesStep6,
runTriplesStep7,
runTriplesStep8,
runTriplesStep9,
} from ".";
import { TEST_CUSTOMER } from "@oko-wallet-tss-api/api/tests";
const SSS_THRESHOLD = 2;
describe("triples_test", () => {
let pool: Pool;
beforeAll(async () => {
const config = testPgConfig;
const createPostgresRes = await createPgConn({
database: config.database,
host: config.host,
password: config.password,
user: config.user,
port: config.port,
ssl: config.ssl,
});
if (createPostgresRes.success === false) {
console.error(createPostgresRes.err);
throw new Error("Failed to create postgres database");
}
pool = createPostgresRes.data;
});
beforeEach(async () => {
await resetPgDatabase(pool);
await insertKeyShareNodeMeta(pool, {
sss_threshold: SSS_THRESHOLD,
});
});
it("run triples success", async () => {
const email = "test@test.com";
const clientKeygenResult = napiRunKeygenClientCentralized();
const { keygen_outputs } = clientKeygenResult;
const keygen_2 = keygen_outputs[Participant.P1];
const createUserRes = await createUser(pool, email, "google");
if (createUserRes.success === false) {
console.error(createUserRes.err);
throw new Error("Failed to create user");
}
const createWalletRes = await createWallet(pool, {
user_id: createUserRes.data.user_id,
curve_type: "secp256k1",
public_key: Buffer.from(keygen_2.public_key, "hex"),
enc_tss_share: Buffer.from(keygen_2.private_share, "hex"),
status: "ACTIVE" as WalletStatus,
sss_threshold: SSS_THRESHOLD,
});
if (createWalletRes.success === false) {
console.error(createWalletRes.err);
throw new Error("Failed to create wallet");
}
const wallet = createWalletRes.data;
const insertCustomerRes = await insertCustomer(pool, TEST_CUSTOMER);
if (insertCustomerRes.success === false) {
console.error(insertCustomerRes.err);
throw new Error("Failed to insert customer");
}
const customerId = insertCustomerRes.data.customer_id;
const clientTriplesState: TECDSATriplesState = {
triplesState: null,
triplesMessages: null,
triple0Pub: null,
triple1Pub: null,
triple0Share: null,
triple1Share: null,
};
// step 1
const clientTriplesResult1 = napiRunTriples2ClientStep1();
clientTriplesState.triplesState = clientTriplesResult1.st_0;
const triplesStep1Request: TriplesStep1Request = {
email,
wallet_id: wallet.wallet_id,
customer_id: customerId,
msgs_1: clientTriplesResult1.msgs_1,
};
const triplesStep1Response = await runTriplesStep1(
pool,
triplesStep1Request,
);
if (triplesStep1Response.success === false) {
console.error(triplesStep1Response);
throw new Error("Failed to run triples step 1");
}
clientTriplesState.triplesMessages = triplesStep1Response.data?.msgs_0;
const sessionId = triplesStep1Response.data?.session_id;
expect(triplesStep1Response.success).toBe(true);
expect(sessionId).toBeDefined();
expect(clientTriplesState.triplesMessages).toBeDefined();
const getTssStageWithSessionDataRes = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage = getTssStageWithSessionDataRes.data;
expect(triplesStage).toBeDefined();
expect(triplesStage?.session_id).toEqual(sessionId);
expect(triplesStage?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage?.stage_status).toEqual(TriplesStageStatus.STEP_1);
expect(triplesStage?.stage_data).toBeDefined();
expect(triplesStage?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage?.wallet_id).toEqual(wallet.wallet_id);
// step 2
const clientTriplesResult2 = napiRunTriples2ClientStep2(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult2.st_0;
const triplesStep2Request: TriplesStep2Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
wait_1: clientTriplesResult2.msgs_1.wait_1[Participant.P0],
};
const triplesStep2Response = await runTriplesStep2(
pool,
triplesStep2Request,
);
if (triplesStep2Response.success === false) {
console.error(triplesStep2Response);
throw new Error("Failed to run triples step 2");
}
clientTriplesState.triplesMessages!.wait_1[Participant.P1] =
triplesStep2Response.data?.wait_1;
expect(triplesStep2Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.wait_1[Participant.P1],
).toBeDefined();
const getTssStageWithSessionDataRes2 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes2.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes2.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage2 = getTssStageWithSessionDataRes2.data;
expect(triplesStage2).toBeDefined();
expect(triplesStage2?.session_id).toEqual(sessionId);
expect(triplesStage2?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage2?.stage_status).toEqual(TriplesStageStatus.STEP_2);
expect(triplesStage2?.stage_data).toBeDefined();
expect(triplesStage2?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage2?.wallet_id).toEqual(wallet.wallet_id);
// step 3
const clientTriplesResult3 = napiRunTriples2ClientStep3(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult3.st_0;
const triplesStep3Request: TriplesStep3Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
wait_2: clientTriplesResult3.msgs_1.wait_2[Participant.P0],
};
const triplesStep3Response = await runTriplesStep3(
pool,
triplesStep3Request,
);
if (triplesStep3Response.success === false) {
console.error(triplesStep3Response);
throw new Error("Failed to run triples step 3");
}
clientTriplesState.triplesMessages!.wait_2[Participant.P1] =
triplesStep3Response.data?.wait_2;
expect(triplesStep3Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.wait_2[Participant.P1],
).toBeDefined();
const getTssStageWithSessionDataRes3 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes3.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes3.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage3 = getTssStageWithSessionDataRes3.data;
expect(triplesStage3).toBeDefined();
expect(triplesStage3?.session_id).toEqual(sessionId);
expect(triplesStage3?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage3?.stage_status).toEqual(TriplesStageStatus.STEP_3);
expect(triplesStage3?.stage_data).toBeDefined();
expect(triplesStage3?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage3?.wallet_id).toEqual(wallet.wallet_id);
// step 4
const clientTriplesResult4 = napiRunTriples2ClientStep4(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult4.st_0;
const triplesStep4Request: TriplesStep4Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
wait_3: clientTriplesResult4.msgs_1.wait_3[Participant.P0],
};
const triplesStep4Response = await runTriplesStep4(
pool,
triplesStep4Request,
);
if (triplesStep4Response.success === false) {
console.error(triplesStep4Response);
throw new Error("Failed to run triples step 4");
}
clientTriplesState.triplesMessages!.wait_3[Participant.P1] =
triplesStep4Response.data?.wait_3;
expect(triplesStep4Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.wait_3[Participant.P1],
).toBeDefined();
const getTssStageWithSessionDataRes4 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes4.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes4.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage4 = getTssStageWithSessionDataRes4.data;
expect(triplesStage4).toBeDefined();
expect(triplesStage4?.session_id).toEqual(sessionId);
expect(triplesStage4?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage4?.stage_status).toEqual(TriplesStageStatus.STEP_4);
expect(triplesStage4?.stage_data).toBeDefined();
expect(triplesStage4?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage4?.wallet_id).toEqual(wallet.wallet_id);
// step 5
const clientTriplesResult5 = napiRunTriples2ClientStep5(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult5.st_0;
const triplesStep5Request: TriplesStep5Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
wait_4: clientTriplesResult5.msgs_1.wait_4[Participant.P0],
};
const triplesStep5Response = await runTriplesStep5(
pool,
triplesStep5Request,
);
if (triplesStep5Response.success === false) {
console.error(triplesStep5Response);
throw new Error("Failed to run triples step 5");
}
clientTriplesState.triplesMessages!.wait_4[Participant.P1] =
triplesStep5Response.data?.wait_4;
expect(triplesStep5Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.wait_4[Participant.P1],
).toBeDefined();
const getTssStageWithSessionDataRes5 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes5.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes5.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage5 = getTssStageWithSessionDataRes5.data;
expect(triplesStage5).toBeDefined();
expect(triplesStage5?.session_id).toEqual(sessionId);
expect(triplesStage5?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage5?.stage_status).toEqual(TriplesStageStatus.STEP_5);
expect(triplesStage5?.stage_data).toBeDefined();
expect(triplesStage5?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage5?.wallet_id).toEqual(wallet.wallet_id);
// step 6
const clientTriplesResult6 = napiRunTriples2ClientStep6(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult6.st_0;
const triplesStep6Request: TriplesStep6Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
batch_random_ot_wait_0:
clientTriplesResult6.msgs_1.batch_random_ot_wait_0[Participant.P0],
};
const triplesStep6Response = await runTriplesStep6(
pool,
triplesStep6Request,
);
if (triplesStep6Response.success === false) {
console.error(triplesStep6Response);
throw new Error("Failed to run triples step 6");
}
clientTriplesState.triplesMessages!.batch_random_ot_wait_0[Participant.P1] =
triplesStep6Response.data?.batch_random_ot_wait_0;
expect(triplesStep6Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.batch_random_ot_wait_0[
Participant.P1
],
).toBeDefined();
const getTssStageWithSessionDataRes6 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes6.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes6.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage6 = getTssStageWithSessionDataRes6.data;
expect(triplesStage6).toBeDefined();
expect(triplesStage6?.session_id).toEqual(sessionId);
expect(triplesStage6?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage6?.stage_status).toEqual(TriplesStageStatus.STEP_6);
expect(triplesStage6?.stage_data).toBeDefined();
expect(triplesStage6?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage6?.wallet_id).toEqual(wallet.wallet_id);
// step 7
const clientTriplesResult7 = napiRunTriples2ClientStep7(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult7.st_0;
const triplesStep7Request: TriplesStep7Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
correlated_ot_wait_0:
clientTriplesResult7.msgs_1.correlated_ot_wait_0[Participant.P0],
};
const triplesStep7Response = await runTriplesStep7(
pool,
triplesStep7Request,
);
if (triplesStep7Response.success === false) {
console.error(triplesStep7Response);
throw new Error("Failed to run triples step 7");
}
clientTriplesState.triplesMessages!.random_ot_extension_wait_0[
Participant.P1
] = triplesStep7Response.data?.random_ot_extension_wait_0;
expect(triplesStep7Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.random_ot_extension_wait_0[
Participant.P1
],
).toBeDefined();
const getTssStageWithSessionDataRes7 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes7.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes7.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage7 = getTssStageWithSessionDataRes7.data;
expect(triplesStage7).toBeDefined();
expect(triplesStage7?.session_id).toEqual(sessionId);
expect(triplesStage7?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage7?.stage_status).toEqual(TriplesStageStatus.STEP_7);
expect(triplesStage7?.stage_data).toBeDefined();
expect(triplesStage7?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage7?.wallet_id).toEqual(wallet.wallet_id);
// step 8
const clientTriplesResult8 = napiRunTriples2ClientStep8(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult8.st_0;
const triplesStep8Request: TriplesStep8Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
random_ot_extension_wait_1:
clientTriplesResult8.msgs_1.random_ot_extension_wait_1[Participant.P0],
};
const triplesStep8Response = await runTriplesStep8(
pool,
triplesStep8Request,
);
if (triplesStep8Response.success === false) {
console.error(triplesStep8Response);
throw new Error("Failed to run triples step 8");
}
clientTriplesState.triplesMessages!.mta_wait_0[Participant.P1] =
triplesStep8Response.data?.mta_wait_0;
expect(triplesStep8Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.mta_wait_0[Participant.P1],
).toBeDefined();
const getTssStageWithSessionDataRes8 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes8.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes8.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage8 = getTssStageWithSessionDataRes8.data;
expect(triplesStage8).toBeDefined();
expect(triplesStage8?.session_id).toEqual(sessionId);
expect(triplesStage8?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage8?.stage_status).toEqual(TriplesStageStatus.STEP_8);
expect(triplesStage8?.stage_data).toBeDefined();
expect(triplesStage8?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage8?.wallet_id).toEqual(wallet.wallet_id);
// step 9
const clientTriplesResult9 = napiRunTriples2ClientStep9(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult9.st_0;
const triplesStep9Request: TriplesStep9Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
mta_wait_1: clientTriplesResult9.msgs_1.mta_wait_1[Participant.P0],
};
const triplesStep9Response = await runTriplesStep9(
pool,
triplesStep9Request,
);
if (triplesStep9Response.success === false) {
console.error(triplesStep9Response);
throw new Error("Failed to run triples step 9");
}
expect(triplesStep9Response.success).toBe(true);
const getTssStageWithSessionDataRes9 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes9.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes9.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage9 = getTssStageWithSessionDataRes9.data;
expect(triplesStage9).toBeDefined();
expect(triplesStage9?.session_id).toEqual(sessionId);
expect(triplesStage9?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage9?.stage_status).toEqual(TriplesStageStatus.STEP_9);
expect(triplesStage9?.stage_data).toBeDefined();
expect(triplesStage9?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage9?.wallet_id).toEqual(wallet.wallet_id);
// step 10
const clientTriplesResult10 = napiRunTriples2ClientStep10(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult10.st_0;
const triplesStep10Request: TriplesStep10Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
wait_5: clientTriplesResult10.msgs_1.wait_5[Participant.P0],
wait_6: clientTriplesResult10.msgs_1.wait_6[Participant.P0],
};
const triplesStep10Response = await runTriplesStep10(
pool,
triplesStep10Request,
);
if (triplesStep10Response.success === false) {
console.error(triplesStep10Response);
throw new Error("Failed to run triples step 10");
}
clientTriplesState.triplesMessages!.wait_5[Participant.P1] =
triplesStep10Response.data?.wait_5;
clientTriplesState.triplesMessages!.wait_6[Participant.P1] =
triplesStep10Response.data?.wait_6;
expect(triplesStep10Response.success).toBe(true);
expect(
clientTriplesState.triplesMessages?.wait_5[Participant.P1],
).toBeDefined();
expect(
clientTriplesState.triplesMessages?.wait_6[Participant.P1],
).toBeDefined();
const getTssStageWithSessionDataRes10 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes10.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes10.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage10 = getTssStageWithSessionDataRes10.data;
expect(triplesStage10).toBeDefined();
expect(triplesStage10?.session_id).toEqual(sessionId);
expect(triplesStage10?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage10?.stage_status).toEqual(TriplesStageStatus.STEP_10);
expect(triplesStage10?.stage_data).toBeDefined();
expect(triplesStage10?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage10?.wallet_id).toEqual(wallet.wallet_id);
// step 11
const clientTriplesResult11 = napiRunTriples2ClientStep11(
clientTriplesState.triplesState,
clientTriplesState.triplesMessages,
);
clientTriplesState.triplesState = clientTriplesResult11.st_0;
const triplesStep11Request: TriplesStep11Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
pub_v: clientTriplesResult11.pub_v,
};
const triplesStep11Response = await runTriplesStep11(
pool,
triplesStep11Request,
);
if (triplesStep11Response.success === false) {
console.error(triplesStep11Response);
throw new Error("Failed to run triples step 11");
}
expect(triplesStep11Response.success).toBe(true);
expect(triplesStep11Response.data?.pub_v).toBeDefined();
const getTssStageWithSessionDataRes11 = await getTssStageWithSessionData(
pool,
sessionId,
TssStageType.TRIPLES,
);
if (getTssStageWithSessionDataRes11.success === false) {
console.error(
`getTssStageWithSessionData error: ${getTssStageWithSessionDataRes11.err}`,
);
throw new Error("Failed to get tss stage with session data");
}
const triplesStage11 = getTssStageWithSessionDataRes11.data;
expect(triplesStage11).toBeDefined();
expect(triplesStage11?.session_id).toEqual(sessionId);
expect(triplesStage11?.stage_type).toEqual(TssStageType.TRIPLES);
expect(triplesStage11?.stage_status).toEqual(TriplesStageStatus.COMPLETED);
expect(triplesStage11?.stage_data).toBeDefined();
expect(triplesStage11?.session_state).toEqual(TssSessionState.IN_PROGRESS);
expect(triplesStage11?.wallet_id).toEqual(wallet.wallet_id);
});
it("run triples step 1 failure - unauthorized", async () => {
const walletId = "550e8400-e29b-41d4-a716-446655440000";
const insertCustomerRes = await insertCustomer(pool, TEST_CUSTOMER);
if (insertCustomerRes.success === false) {
console.error(insertCustomerRes.err);
throw new Error("Failed to insert customer");
}
const customerId = insertCustomerRes.data.customer_id;
const clientTriplesResult = napiRunTriples2ClientStep1();
const { msgs_1 } = clientTriplesResult;
const triplesStep1Request: TriplesStep1Request = {
email: "test@test.com",
wallet_id: walletId,
customer_id: customerId,
msgs_1,
};
const triplesStep1Response = await runTriplesStep1(
pool,
triplesStep1Request,
);
if (triplesStep1Response.success === true) {
throw new Error("triples step 1 should fail");
}
expect(triplesStep1Response.success).toBe(false);
expect(triplesStep1Response.code).toBe("UNAUTHORIZED");
});
it("run triples step 2 failure - invalid tss session: not found session", async () => {
const email = "test@test.com";
const clientKeygenResult = napiRunKeygenClientCentralized();
const { keygen_outputs } = clientKeygenResult;
const keygen_2 = keygen_outputs[Participant.P1];
const createUserRes = await createUser(pool, email, "google");
if (createUserRes.success === false) {
console.error(createUserRes.err);
throw new Error("Failed to create user");
}
const user = createUserRes.data;
const createWalletRes = await createWallet(pool, {
user_id: user.user_id,
curve_type: "secp256k1",
public_key: Buffer.from(keygen_2.public_key, "hex"),
enc_tss_share: Buffer.from(keygen_2.private_share, "hex"),
status: "ACTIVE" as WalletStatus,
sss_threshold: SSS_THRESHOLD,
});
if (createWalletRes.success === false) {
console.error(createWalletRes.err);
throw new Error("Failed to create wallet");
}
const wallet = createWalletRes.data;
const sessionId = "110e8400-e29b-41d4-a716-446655440001";
const triplesStep2Request: TriplesStep2Request = {
email,
wallet_id: wallet.wallet_id,
session_id: sessionId,
wait_1: [],
};
const triplesStep2Response = await runTriplesStep2(
pool,
triplesStep2Request,
);
if (triplesStep2Response.success === true) {
throw new Error("triples step 2 should fail");
}
expect(triplesStep2Response.success).toBe(false);
expect(triplesStep2Response.code).toBe("INVALID_TSS_SESSION");
});
it("run triples step 2 failure - invalid tss session: not match wallet", async () => {
const email = "test@test.com";
const clientKeygenResult = napiRunKeygenClientCentralized();
const { keygen_outputs } = clientKeygenResult;
const keygen_2 = keygen_outputs[Participant.P1];
const createUserRes = await createUser(pool, email, "google");
if (createUserRes.success === false) {
console.error(createUserRes.err);
throw new Error("Failed to create user");
}
const user = createUserRes.data;
const createWalletRes = await createWallet(pool, {
user_id: user.user_id,
curve_type: "secp256k1",
public_key: Buffer.from(keygen_2.public_key, "hex"),
enc_tss_share: Buffer.from(keygen_2.private_share, "hex"),
status: "ACTIVE" as WalletStatus,
sss_threshold: SSS_THRESHOLD,
});
if (createWalletRes.success === false) {
console.error(createWalletRes.err);
throw new Error("Failed to create wallet");
}
const wallet = createWalletRes.data;
const walletId = wallet.wallet_id;
const invalidWalletId = "110e8400-e29b-41d4-a716-446655440001";
const insertCustomerRes = await insertCustomer(pool, TEST_CUSTOMER);
if (insertCustomerRes.success === false) {
console.error(insertCustomerRes.err);
throw new Error("Failed to insert customer");
}
const customerId = insertCustomerRes.data.customer_id;
const createTssSessionRes = await createTssSession(pool, {
wallet_id: invalidWalletId,
customer_id: customerId,
});
if (createTssSessionRes.success === false) {
console.error(createTssSessionRes.err);
throw new Error("Failed to create tss session");
}
const session = createTssSessionRes.data;
const createTssStageRes = await createTssStage(pool, {
session_id: session.session_id,
stage_type: TssStageType.TRIPLES,
stage_status: TriplesStageStatus.STEP_1,
stage_data: {
triple_state: null,
triple_messages: null,
triple_pub_0: null,
triple_pub_1: null,
triple_share_0: null,
triple_share_1: null,
},
});
if (createTssStageRes.success === false) {
console.error(createTssStageRes.err);
throw new Error("Failed to create tss stage");
}
const triplesStep2Request: TriplesStep2Request = {
email,
wallet_id: walletId,
session_id: session.session_id,
wait_1: [],
};
const triplesStep2Response = await runTriplesStep2(
pool,
triplesStep2Request,
);
if (triplesStep2Response.success === true) {
throw new Error("triples step 2 should fail");
}
expect(triplesStep2Response.success).toBe(false);
expect(triplesStep2Response.code).toBe("INVALID_TSS_SESSION");
});
it("run triples step 2 failure - invalid tss stage", async () => {
const email = "test@test.com";
const clientKeygenResult = napiRunKeygenClientCentralized();
const { keygen_outputs } = clientKeygenResult;
const keygen_2 = keygen_outputs[Participant.P1];
const createUserRes = await createUser(pool, email, "google");
if (createUserRes.success === false) {
console.error(createUserRes.err);
throw new Error("Failed to create user");
}
const user = createUserRes.data;
const createWalletRes = await createWallet(pool, {
user_id: user.user_id,
curve_type: "secp256k1",
public_key: Buffer.from(keygen_2.public_key, "hex"),
enc_tss_share: Buffer.from(keygen_2.private_share, "hex"),
status: "ACTIVE" as WalletStatus,
sss_threshold: SSS_THRESHOLD,
});
if (createWalletRes.success === false) {
console.error(createWalletRes.err);
throw new Error("Failed to create wallet");
}
const wallet = createWalletRes.data;
const walletId = wallet.wallet_id;
const insertCustomerRes = await insertCustomer(pool, TEST_CUSTOMER);
if (insertCustomerRes.success === false) {
console.error(insertCustomerRes.err);
throw new Error("Failed to insert customer");
}
const customerId = insertCustomerRes.data.customer_id;
const createTssSessionRes = await createTssSession(pool, {
wallet_id: walletId,
customer_id: customerId,
});
if (createTssSessionRes.success === false) {
console.error(createTssSessionRes.err);
throw new Error("Failed to create tss session");
}
const session = createTssSessionRes.data;
const createTssStageRes = await createTssStage(pool, {
session_id: session.session_id,
stage_type: TssStageType.TRIPLES,
stage_status: TriplesStageStatus.STEP_2,
stage_data: {
triple_state: null,
triple_messages: null,
triple_pub_0: null,
triple_pub_1: null,
triple_share_0: null,
triple_share_1: null,
},
});
if (createTssStageRes.success === false) {
console.error(createTssStageRes.err);
throw new Error("Failed to create tss stage");
}
const triplesStep2Request: TriplesStep2Request = {
email,
wallet_id: walletId,
session_id: session.session_id,
wait_1: [],
};
const triplesStep2Response = await runTriplesStep2(
pool,
triplesStep2Request,
);
if (triplesStep2Response.success === true) {
throw new Error("triples step 2 should fail");
}
expect(triplesStep2Response.success).toBe(false);
expect(triplesStep2Response.code).toBe("INVALID_TSS_STAGE");
});
it("run triples step 3 failure - invalid tss session: not found session", async () => {
const email = "test@test.com";
const clientKeygenResult = napiRunKeygenClientCentralized();
const { keygen_outputs } = clientKeygenResult;
const keygen_2 = keygen_outputs[Participant.P1];
const createUserRes = await createUser(pool, email, "google");
if (createUserRes.success === false) {
console.error(createUserRes.err);
throw new Error("Failed to create user");
}
const user = createUserRes.data;
const createWalletRes = await createWallet(pool, {
user_id: user.user_id,
curve_type: "secp256k1",
public_key: Buffer.from(keygen_2.public_key, "hex"),
enc_tss_share: Buffer.from(keygen_2.private_share, "hex"),
status: "ACTIVE" as WalletStatus,
sss_threshold: SSS_THRESHOLD,
});
if (createWalletRes.success === false) {
console.error(createWalletRes.err);
throw new Error("Failed to create wallet");
}
const wallet = createWalletRes.data;
const walletId = wallet.wallet_id;
const sessionId = "110e8400-e29b-41d4-a716-446655440001";
const triplesStep3Request: TriplesStep3Request = {
email,
wallet_id: walletId,
session_id: sessionId,
wait_2: {
big_e_i_v: [],
big_f_i_v: [],
big_l_i_v: [],
my_randomizers: [],
my_phi_proof0v: [],
my_phi_proof1v: [],
},
};
const triplesStep3Response = await runTriplesStep3(
pool,
triplesStep3Request,
);
if (triplesStep3Response.success === true) {
throw new Error("triples step 3 should fail");
}
expect(triplesStep3Response.success).toBe(false);
expect(triplesStep3Response.code).toBe("INVALID_TSS_SESSION");
});