@neus/sdk 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -112
- package/cjs/client.cjs +545 -175
- package/cjs/errors.cjs +1 -0
- package/cjs/gates.cjs +1 -0
- package/cjs/index.cjs +725 -180
- package/cjs/utils.cjs +338 -3
- package/client.js +1951 -1729
- package/index.js +75 -64
- package/neus-logo.svg +3 -0
- package/package.json +9 -5
- package/types.d.ts +215 -30
- package/utils.js +407 -4
- package/widgets/README.md +64 -53
- package/widgets/index.js +9 -9
- package/widgets/verify-gate/dist/ProofBadge.js +51 -38
- package/widgets/verify-gate/dist/VerifyGate.js +283 -58
- package/widgets/verify-gate/index.js +13 -13
package/client.js
CHANGED
|
@@ -1,1729 +1,1951 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NEUS SDK Client
|
|
3
|
-
* Create and verify cryptographic proofs across applications
|
|
4
|
-
* @license Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { ApiError, ValidationError, NetworkError, ConfigurationError } from './errors.js';
|
|
8
|
-
import { constructVerificationMessage, validateWalletAddress, NEUS_CONSTANTS } from './utils.js';
|
|
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
|
-
if (
|
|
52
|
-
return { valid: false, error: '
|
|
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
|
-
if (
|
|
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
|
-
if (data.
|
|
285
|
-
return { valid: false, error: '
|
|
286
|
-
}
|
|
287
|
-
break;
|
|
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
|
-
} catch {
|
|
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
|
-
data
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
if (
|
|
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
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
const
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1164
|
-
*
|
|
1165
|
-
*
|
|
1166
|
-
* @
|
|
1167
|
-
*
|
|
1168
|
-
*
|
|
1169
|
-
*
|
|
1170
|
-
* }
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
const
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
}
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
}
|
|
1342
|
-
|
|
1343
|
-
const
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
const
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
}
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
const
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
} else
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
*
|
|
1614
|
-
* @
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
}
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
}
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1
|
+
/**
|
|
2
|
+
* NEUS SDK Client
|
|
3
|
+
* Create and verify cryptographic proofs across applications
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { ApiError, ValidationError, NetworkError, ConfigurationError } from './errors.js';
|
|
8
|
+
import { constructVerificationMessage, validateWalletAddress, validateUniversalAddress, signMessage, NEUS_CONSTANTS } from './utils.js';
|
|
9
|
+
|
|
10
|
+
const FALLBACK_PUBLIC_VERIFIERS = [
|
|
11
|
+
'ownership-basic',
|
|
12
|
+
'ownership-pseudonym',
|
|
13
|
+
'ownership-dns-txt',
|
|
14
|
+
'ownership-social',
|
|
15
|
+
'ownership-org-oauth',
|
|
16
|
+
'contract-ownership',
|
|
17
|
+
'nft-ownership',
|
|
18
|
+
'token-holding',
|
|
19
|
+
'wallet-link',
|
|
20
|
+
'wallet-risk',
|
|
21
|
+
'proof-of-human',
|
|
22
|
+
'agent-identity',
|
|
23
|
+
'agent-delegation',
|
|
24
|
+
'ai-content-moderation'
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const INTERACTIVE_VERIFIERS = new Set([
|
|
28
|
+
'ownership-social',
|
|
29
|
+
'ownership-org-oauth',
|
|
30
|
+
'proof-of-human'
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
const EVM_ADDRESS_RE = /^0x[a-fA-F0-9]{40}$/;
|
|
34
|
+
|
|
35
|
+
// Validation for supported verifiers
|
|
36
|
+
const validateVerifierData = (verifierId, data) => {
|
|
37
|
+
if (!data || typeof data !== 'object') {
|
|
38
|
+
return { valid: false, error: 'Data object is required' };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Format validation for supported verifiers
|
|
42
|
+
switch (verifierId) {
|
|
43
|
+
case 'ownership-basic':
|
|
44
|
+
// Required: owner (must match request walletAddress).
|
|
45
|
+
// Reference is optional when content/contentHash is provided.
|
|
46
|
+
// If neither content nor contentHash is provided, reference.id is required (reference-only proof).
|
|
47
|
+
if (!data.owner || !validateUniversalAddress(data.owner, typeof data.chain === 'string' ? data.chain : undefined)) {
|
|
48
|
+
return { valid: false, error: 'owner (universal wallet address) is required' };
|
|
49
|
+
}
|
|
50
|
+
if (data.content !== undefined && data.content !== null) {
|
|
51
|
+
if (typeof data.content !== 'string') {
|
|
52
|
+
return { valid: false, error: 'content must be a string when provided' };
|
|
53
|
+
}
|
|
54
|
+
if (data.content.length > 50000) {
|
|
55
|
+
return { valid: false, error: 'content exceeds 50KB inline limit' };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (data.contentHash !== undefined && data.contentHash !== null) {
|
|
59
|
+
if (typeof data.contentHash !== 'string' || !/^0x[a-fA-F0-9]{64}$/.test(data.contentHash)) {
|
|
60
|
+
return { valid: false, error: 'contentHash must be a 32-byte hex string (0x + 64 hex chars) when provided' };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (data.contentType !== undefined && data.contentType !== null) {
|
|
64
|
+
if (typeof data.contentType !== 'string' || data.contentType.length > 100) {
|
|
65
|
+
return { valid: false, error: 'contentType must be a string (max 100 chars) when provided' };
|
|
66
|
+
}
|
|
67
|
+
const base = String(data.contentType).split(';')[0].trim().toLowerCase();
|
|
68
|
+
// Minimal MIME sanity check (server validates more precisely).
|
|
69
|
+
if (!base || base.includes(' ') || !base.includes('/')) {
|
|
70
|
+
return { valid: false, error: 'contentType must be a valid MIME type when provided' };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (data.provenance !== undefined && data.provenance !== null) {
|
|
74
|
+
if (!data.provenance || typeof data.provenance !== 'object' || Array.isArray(data.provenance)) {
|
|
75
|
+
return { valid: false, error: 'provenance must be an object when provided' };
|
|
76
|
+
}
|
|
77
|
+
const dk = data.provenance.declaredKind;
|
|
78
|
+
if (dk !== undefined && dk !== null) {
|
|
79
|
+
const allowed = ['human', 'ai', 'mixed', 'unknown'];
|
|
80
|
+
if (typeof dk !== 'string' || !allowed.includes(dk)) {
|
|
81
|
+
return { valid: false, error: `provenance.declaredKind must be one of: ${allowed.join(', ')}` };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const ai = data.provenance.aiContext;
|
|
85
|
+
if (ai !== undefined && ai !== null) {
|
|
86
|
+
if (typeof ai !== 'object' || Array.isArray(ai)) {
|
|
87
|
+
return { valid: false, error: 'provenance.aiContext must be an object when provided' };
|
|
88
|
+
}
|
|
89
|
+
if (ai.generatorType !== undefined && ai.generatorType !== null) {
|
|
90
|
+
const allowed = ['local', 'saas', 'agent'];
|
|
91
|
+
if (typeof ai.generatorType !== 'string' || !allowed.includes(ai.generatorType)) {
|
|
92
|
+
return { valid: false, error: `provenance.aiContext.generatorType must be one of: ${allowed.join(', ')}` };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (ai.provider !== undefined && ai.provider !== null) {
|
|
96
|
+
if (typeof ai.provider !== 'string' || ai.provider.length > 64) {
|
|
97
|
+
return { valid: false, error: 'provenance.aiContext.provider must be a string (max 64 chars) when provided' };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (ai.model !== undefined && ai.model !== null) {
|
|
101
|
+
if (typeof ai.model !== 'string' || ai.model.length > 128) {
|
|
102
|
+
return { valid: false, error: 'provenance.aiContext.model must be a string (max 128 chars) when provided' };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (ai.runId !== undefined && ai.runId !== null) {
|
|
106
|
+
if (typeof ai.runId !== 'string' || ai.runId.length > 128) {
|
|
107
|
+
return { valid: false, error: 'provenance.aiContext.runId must be a string (max 128 chars) when provided' };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (data.reference !== undefined) {
|
|
113
|
+
if (!data.reference || typeof data.reference !== 'object') {
|
|
114
|
+
return { valid: false, error: 'reference must be an object when provided' };
|
|
115
|
+
}
|
|
116
|
+
if (!data.reference.type || typeof data.reference.type !== 'string') {
|
|
117
|
+
// Only required when reference object is present (or when doing reference-only proofs).
|
|
118
|
+
// Server requires reference.type when reference is used for traceability.
|
|
119
|
+
return { valid: false, error: 'reference.type is required when reference is provided' };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (!data.content && !data.contentHash) {
|
|
123
|
+
if (!data.reference || typeof data.reference !== 'object') {
|
|
124
|
+
return { valid: false, error: 'reference is required when neither content nor contentHash is provided' };
|
|
125
|
+
}
|
|
126
|
+
if (!data.reference.id || typeof data.reference.id !== 'string') {
|
|
127
|
+
return { valid: false, error: 'reference.id is required when neither content nor contentHash is provided' };
|
|
128
|
+
}
|
|
129
|
+
if (!data.reference.type || typeof data.reference.type !== 'string') {
|
|
130
|
+
return { valid: false, error: 'reference.type is required when neither content nor contentHash is provided' };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
case 'nft-ownership':
|
|
135
|
+
// ownerAddress is optional; server injects from request walletAddress when omitted.
|
|
136
|
+
if (
|
|
137
|
+
!data.contractAddress ||
|
|
138
|
+
data.tokenId === null ||
|
|
139
|
+
data.tokenId === undefined ||
|
|
140
|
+
typeof data.chainId !== 'number'
|
|
141
|
+
) {
|
|
142
|
+
return { valid: false, error: 'contractAddress, tokenId, and chainId are required' };
|
|
143
|
+
}
|
|
144
|
+
if (data.tokenType !== undefined && data.tokenType !== null) {
|
|
145
|
+
const tt = String(data.tokenType).toLowerCase();
|
|
146
|
+
if (tt !== 'erc721' && tt !== 'erc1155') {
|
|
147
|
+
return { valid: false, error: 'tokenType must be one of: erc721, erc1155' };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (data.blockNumber !== undefined && data.blockNumber !== null && !Number.isInteger(data.blockNumber)) {
|
|
151
|
+
return { valid: false, error: 'blockNumber must be an integer when provided' };
|
|
152
|
+
}
|
|
153
|
+
if (data.ownerAddress && !validateWalletAddress(data.ownerAddress)) {
|
|
154
|
+
return { valid: false, error: 'Invalid ownerAddress' };
|
|
155
|
+
}
|
|
156
|
+
if (!validateWalletAddress(data.contractAddress)) {
|
|
157
|
+
return { valid: false, error: 'Invalid contractAddress' };
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
case 'token-holding':
|
|
161
|
+
// ownerAddress is optional; server injects from request walletAddress when omitted.
|
|
162
|
+
if (
|
|
163
|
+
!data.contractAddress ||
|
|
164
|
+
data.minBalance === null ||
|
|
165
|
+
data.minBalance === undefined ||
|
|
166
|
+
typeof data.chainId !== 'number'
|
|
167
|
+
) {
|
|
168
|
+
return { valid: false, error: 'contractAddress, minBalance, and chainId are required' };
|
|
169
|
+
}
|
|
170
|
+
if (data.blockNumber !== undefined && data.blockNumber !== null && !Number.isInteger(data.blockNumber)) {
|
|
171
|
+
return { valid: false, error: 'blockNumber must be an integer when provided' };
|
|
172
|
+
}
|
|
173
|
+
if (data.ownerAddress && !validateWalletAddress(data.ownerAddress)) {
|
|
174
|
+
return { valid: false, error: 'Invalid ownerAddress' };
|
|
175
|
+
}
|
|
176
|
+
if (!validateWalletAddress(data.contractAddress)) {
|
|
177
|
+
return { valid: false, error: 'Invalid contractAddress' };
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
case 'ownership-dns-txt':
|
|
181
|
+
if (!data.domain || typeof data.domain !== 'string') {
|
|
182
|
+
return { valid: false, error: 'domain is required' };
|
|
183
|
+
}
|
|
184
|
+
if (data.walletAddress && !validateWalletAddress(data.walletAddress)) {
|
|
185
|
+
return { valid: false, error: 'Invalid walletAddress' };
|
|
186
|
+
}
|
|
187
|
+
break;
|
|
188
|
+
case 'wallet-link':
|
|
189
|
+
if (!data.primaryWalletAddress || !validateUniversalAddress(data.primaryWalletAddress, data.chain)) {
|
|
190
|
+
return { valid: false, error: 'primaryWalletAddress is required' };
|
|
191
|
+
}
|
|
192
|
+
if (!data.secondaryWalletAddress || !validateUniversalAddress(data.secondaryWalletAddress, data.chain)) {
|
|
193
|
+
return { valid: false, error: 'secondaryWalletAddress is required' };
|
|
194
|
+
}
|
|
195
|
+
if (!data.signature || typeof data.signature !== 'string') {
|
|
196
|
+
return { valid: false, error: 'signature is required (signed by secondary wallet)' };
|
|
197
|
+
}
|
|
198
|
+
if (typeof data.chain !== 'string' || !/^[a-z0-9]+:[^\s]+$/.test(data.chain)) {
|
|
199
|
+
return { valid: false, error: 'chain is required (namespace:reference)' };
|
|
200
|
+
}
|
|
201
|
+
if (typeof data.signatureMethod !== 'string' || !data.signatureMethod.trim()) {
|
|
202
|
+
return { valid: false, error: 'signatureMethod is required' };
|
|
203
|
+
}
|
|
204
|
+
if (typeof data.signedTimestamp !== 'number') {
|
|
205
|
+
return { valid: false, error: 'signedTimestamp is required' };
|
|
206
|
+
}
|
|
207
|
+
break;
|
|
208
|
+
case 'contract-ownership':
|
|
209
|
+
if (!data.contractAddress || !validateWalletAddress(data.contractAddress)) {
|
|
210
|
+
return { valid: false, error: 'contractAddress is required' };
|
|
211
|
+
}
|
|
212
|
+
if (data.walletAddress && !validateWalletAddress(data.walletAddress)) {
|
|
213
|
+
return { valid: false, error: 'Invalid walletAddress' };
|
|
214
|
+
}
|
|
215
|
+
if (typeof data.chainId !== 'number') {
|
|
216
|
+
return { valid: false, error: 'chainId is required' };
|
|
217
|
+
}
|
|
218
|
+
break;
|
|
219
|
+
case 'agent-identity':
|
|
220
|
+
if (!data.agentId || typeof data.agentId !== 'string' || data.agentId.length < 1 || data.agentId.length > 128) {
|
|
221
|
+
return { valid: false, error: 'agentId is required (1-128 chars)' };
|
|
222
|
+
}
|
|
223
|
+
if (!data.agentWallet || !validateWalletAddress(data.agentWallet)) {
|
|
224
|
+
return { valid: false, error: 'agentWallet is required' };
|
|
225
|
+
}
|
|
226
|
+
if (data.agentType && !['ai', 'bot', 'service', 'automation', 'agent'].includes(data.agentType)) {
|
|
227
|
+
return { valid: false, error: 'agentType must be one of: ai, bot, service, automation, agent' };
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
case 'agent-delegation':
|
|
231
|
+
if (!data.controllerWallet || !validateWalletAddress(data.controllerWallet)) {
|
|
232
|
+
return { valid: false, error: 'controllerWallet is required' };
|
|
233
|
+
}
|
|
234
|
+
if (!data.agentWallet || !validateWalletAddress(data.agentWallet)) {
|
|
235
|
+
return { valid: false, error: 'agentWallet is required' };
|
|
236
|
+
}
|
|
237
|
+
if (data.scope && (typeof data.scope !== 'string' || data.scope.length > 128)) {
|
|
238
|
+
return { valid: false, error: 'scope must be a string (max 128 chars)' };
|
|
239
|
+
}
|
|
240
|
+
if (data.expiresAt && (typeof data.expiresAt !== 'number' || data.expiresAt < Date.now())) {
|
|
241
|
+
return { valid: false, error: 'expiresAt must be a future timestamp' };
|
|
242
|
+
}
|
|
243
|
+
break;
|
|
244
|
+
case 'ai-content-moderation':
|
|
245
|
+
if (!data.content || typeof data.content !== 'string') {
|
|
246
|
+
return { valid: false, error: 'content is required' };
|
|
247
|
+
}
|
|
248
|
+
if (!data.contentType || typeof data.contentType !== 'string') {
|
|
249
|
+
return { valid: false, error: 'contentType (MIME type) is required' };
|
|
250
|
+
}
|
|
251
|
+
{
|
|
252
|
+
// Only allow content types that are actually moderated (no "verified but skipped" bypass).
|
|
253
|
+
const contentType = String(data.contentType).split(';')[0].trim().toLowerCase();
|
|
254
|
+
const validTypes = [
|
|
255
|
+
'image/jpeg',
|
|
256
|
+
'image/png',
|
|
257
|
+
'image/webp',
|
|
258
|
+
'image/gif',
|
|
259
|
+
'text/plain',
|
|
260
|
+
'text/markdown',
|
|
261
|
+
'text/x-markdown',
|
|
262
|
+
'application/json',
|
|
263
|
+
'application/xml'
|
|
264
|
+
];
|
|
265
|
+
if (!validTypes.includes(contentType)) {
|
|
266
|
+
return { valid: false, error: `contentType must be one of: ${validTypes.join(', ')}` };
|
|
267
|
+
}
|
|
268
|
+
const isTextual = contentType.startsWith('text/') || contentType.includes('markdown');
|
|
269
|
+
if (isTextual) {
|
|
270
|
+
// Backend enforces 50KB UTF-8 limit for textual moderation payloads.
|
|
271
|
+
try {
|
|
272
|
+
const maxBytes = 50 * 1024;
|
|
273
|
+
const bytes = (typeof TextEncoder !== 'undefined')
|
|
274
|
+
? new TextEncoder().encode(data.content).length
|
|
275
|
+
: String(data.content).length;
|
|
276
|
+
if (bytes > maxBytes) {
|
|
277
|
+
return { valid: false, error: `content exceeds ${maxBytes} bytes limit for ai-content-moderation verifier (text)` };
|
|
278
|
+
}
|
|
279
|
+
} catch {
|
|
280
|
+
// If encoding fails, defer to server.
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (data.content.length > 13653334) {
|
|
285
|
+
return { valid: false, error: 'content exceeds 10MB limit' };
|
|
286
|
+
}
|
|
287
|
+
break;
|
|
288
|
+
case 'ownership-pseudonym':
|
|
289
|
+
if (!data.pseudonymId || typeof data.pseudonymId !== 'string') {
|
|
290
|
+
return { valid: false, error: 'pseudonymId is required' };
|
|
291
|
+
}
|
|
292
|
+
// Validate handle format (3-64 chars, lowercase alphanumeric with ._-)
|
|
293
|
+
if (!/^[a-z0-9._-]{3,64}$/.test(data.pseudonymId.trim().toLowerCase())) {
|
|
294
|
+
return { valid: false, error: 'pseudonymId must be 3-64 characters, lowercase alphanumeric with dots, underscores, or hyphens' };
|
|
295
|
+
}
|
|
296
|
+
// Validate namespace if provided (1-64 chars)
|
|
297
|
+
if (data.namespace && typeof data.namespace === 'string') {
|
|
298
|
+
if (!/^[a-z0-9._-]{1,64}$/.test(data.namespace.trim().toLowerCase())) {
|
|
299
|
+
return { valid: false, error: 'namespace must be 1-64 characters, lowercase alphanumeric with dots, underscores, or hyphens' };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// Note: signature is not required - envelope signature provides authentication
|
|
303
|
+
break;
|
|
304
|
+
case 'wallet-risk':
|
|
305
|
+
if (data.walletAddress && !validateUniversalAddress(data.walletAddress, data.chain)) {
|
|
306
|
+
return { valid: false, error: 'Invalid walletAddress' };
|
|
307
|
+
}
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return { valid: true };
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
export class NeusClient {
|
|
315
|
+
constructor(config = {}) {
|
|
316
|
+
this.config = {
|
|
317
|
+
timeout: 30000,
|
|
318
|
+
enableLogging: false,
|
|
319
|
+
...config
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// NEUS Network API
|
|
323
|
+
this.baseUrl = this.config.apiUrl || 'https://api.neus.network';
|
|
324
|
+
// Enforce HTTPS for neus.network domains to satisfy CSP and normalize URLs
|
|
325
|
+
try {
|
|
326
|
+
const url = new URL(this.baseUrl);
|
|
327
|
+
if (url.hostname.endsWith('neus.network') && url.protocol === 'http:') {
|
|
328
|
+
url.protocol = 'https:';
|
|
329
|
+
}
|
|
330
|
+
// Always remove trailing slash for consistency
|
|
331
|
+
this.baseUrl = url.toString().replace(/\/$/, '');
|
|
332
|
+
} catch {
|
|
333
|
+
// If invalid URL string, leave as-is
|
|
334
|
+
}
|
|
335
|
+
// Normalize apiUrl on config
|
|
336
|
+
this.config.apiUrl = this.baseUrl;
|
|
337
|
+
// Default headers for API requests
|
|
338
|
+
this.defaultHeaders = {
|
|
339
|
+
'Content-Type': 'application/json',
|
|
340
|
+
'Accept': 'application/json',
|
|
341
|
+
'X-Neus-Sdk': 'js'
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// Optional API key (server-side only; do not embed in browser apps)
|
|
345
|
+
if (typeof this.config.apiKey === 'string' && this.config.apiKey.trim().length > 0) {
|
|
346
|
+
this.defaultHeaders['Authorization'] = `Bearer ${this.config.apiKey.trim()}`;
|
|
347
|
+
}
|
|
348
|
+
// Public app attribution header (non-secret)
|
|
349
|
+
if (typeof this.config.appId === 'string' && this.config.appId.trim().length > 0) {
|
|
350
|
+
this.defaultHeaders['X-Neus-App'] = this.config.appId.trim();
|
|
351
|
+
}
|
|
352
|
+
// Ephemeral sponsor capability token
|
|
353
|
+
if (typeof this.config.sponsorGrant === 'string' && this.config.sponsorGrant.trim().length > 0) {
|
|
354
|
+
this.defaultHeaders['X-Sponsor-Grant'] = this.config.sponsorGrant.trim();
|
|
355
|
+
}
|
|
356
|
+
// x402 retry receipt header
|
|
357
|
+
if (typeof this.config.paymentSignature === 'string' && this.config.paymentSignature.trim().length > 0) {
|
|
358
|
+
this.defaultHeaders['PAYMENT-SIGNATURE'] = this.config.paymentSignature.trim();
|
|
359
|
+
}
|
|
360
|
+
// Optional caller-supplied passthrough headers.
|
|
361
|
+
if (this.config.extraHeaders && typeof this.config.extraHeaders === 'object') {
|
|
362
|
+
for (const [k, v] of Object.entries(this.config.extraHeaders)) {
|
|
363
|
+
if (!k || v === undefined || v === null) continue;
|
|
364
|
+
const key = String(k).trim();
|
|
365
|
+
const value = String(v).trim();
|
|
366
|
+
if (!key || !value) continue;
|
|
367
|
+
this.defaultHeaders[key] = value;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
try {
|
|
371
|
+
// Attach origin in browser environments
|
|
372
|
+
if (typeof window !== 'undefined' && window.location && window.location.origin) {
|
|
373
|
+
this.defaultHeaders['X-Client-Origin'] = window.location.origin;
|
|
374
|
+
}
|
|
375
|
+
} catch {
|
|
376
|
+
// ignore: optional browser metadata header
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
_getHubChainId() {
|
|
381
|
+
const configured = Number(this.config?.hubChainId);
|
|
382
|
+
if (Number.isFinite(configured) && configured > 0) return Math.floor(configured);
|
|
383
|
+
return NEUS_CONSTANTS.HUB_CHAIN_ID;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
_normalizeIdentity(value) {
|
|
387
|
+
let raw = String(value || '').trim();
|
|
388
|
+
if (!raw) return '';
|
|
389
|
+
const didMatch = raw.match(/^did:pkh:([^:]+):([^:]+):(.+)$/i);
|
|
390
|
+
if (didMatch && didMatch[3]) {
|
|
391
|
+
raw = String(didMatch[3]).trim();
|
|
392
|
+
}
|
|
393
|
+
return EVM_ADDRESS_RE.test(raw) ? raw.toLowerCase() : raw;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
_inferChainForAddress(address, explicitChain) {
|
|
397
|
+
if (typeof explicitChain === 'string' && explicitChain.includes(':')) return explicitChain.trim();
|
|
398
|
+
const raw = String(address || '').trim();
|
|
399
|
+
const didMatch = raw.match(/^did:pkh:([^:]+):([^:]+):(.+)$/i);
|
|
400
|
+
if (didMatch && didMatch[1] && didMatch[2]) {
|
|
401
|
+
return `${didMatch[1]}:${didMatch[2]}`;
|
|
402
|
+
}
|
|
403
|
+
if (EVM_ADDRESS_RE.test(raw)) {
|
|
404
|
+
return `eip155:${this._getHubChainId()}`;
|
|
405
|
+
}
|
|
406
|
+
// Default non-EVM chain for universal wallet paths when caller omits chain.
|
|
407
|
+
return 'solana:mainnet';
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async _resolveWalletSigner(wallet) {
|
|
411
|
+
if (!wallet) {
|
|
412
|
+
throw new ConfigurationError('No wallet provider available');
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (wallet.address) {
|
|
416
|
+
return { signerWalletAddress: wallet.address, provider: wallet };
|
|
417
|
+
}
|
|
418
|
+
if (wallet.publicKey && typeof wallet.publicKey.toBase58 === 'function') {
|
|
419
|
+
return { signerWalletAddress: wallet.publicKey.toBase58(), provider: wallet };
|
|
420
|
+
}
|
|
421
|
+
if (typeof wallet.getAddress === 'function') {
|
|
422
|
+
const signerWalletAddress = await wallet.getAddress().catch(() => null);
|
|
423
|
+
return { signerWalletAddress, provider: wallet };
|
|
424
|
+
}
|
|
425
|
+
if (wallet.selectedAddress || wallet.request) {
|
|
426
|
+
const provider = wallet;
|
|
427
|
+
if (wallet.selectedAddress) {
|
|
428
|
+
return { signerWalletAddress: wallet.selectedAddress, provider };
|
|
429
|
+
}
|
|
430
|
+
const accounts = await provider.request({ method: 'eth_accounts' });
|
|
431
|
+
if (!accounts || accounts.length === 0) {
|
|
432
|
+
throw new ConfigurationError('No wallet accounts available');
|
|
433
|
+
}
|
|
434
|
+
return { signerWalletAddress: accounts[0], provider };
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
throw new ConfigurationError('Invalid wallet provider');
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
_getDefaultBrowserWallet() {
|
|
441
|
+
if (typeof window === 'undefined') return null;
|
|
442
|
+
return window.ethereum || window.solana || (window.phantom && window.phantom.solana) || null;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async _buildPrivateGateAuth({ address, wallet, chain, signatureMethod } = {}) {
|
|
446
|
+
const providerWallet = wallet || this._getDefaultBrowserWallet();
|
|
447
|
+
const { signerWalletAddress, provider } = await this._resolveWalletSigner(providerWallet);
|
|
448
|
+
if (!signerWalletAddress || typeof signerWalletAddress !== 'string') {
|
|
449
|
+
throw new ConfigurationError('No wallet accounts available');
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const normalizedSigner = this._normalizeIdentity(signerWalletAddress);
|
|
453
|
+
const normalizedAddress = this._normalizeIdentity(address);
|
|
454
|
+
if (!normalizedSigner || normalizedSigner !== normalizedAddress) {
|
|
455
|
+
throw new ValidationError('wallet must match address when includePrivate=true');
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const signerIsEvm = EVM_ADDRESS_RE.test(normalizedSigner);
|
|
459
|
+
const resolvedChain = this._inferChainForAddress(normalizedSigner, chain);
|
|
460
|
+
const resolvedSignatureMethod = (typeof signatureMethod === 'string' && signatureMethod.trim())
|
|
461
|
+
? signatureMethod.trim()
|
|
462
|
+
: (signerIsEvm ? 'eip191' : 'ed25519');
|
|
463
|
+
|
|
464
|
+
const signedTimestamp = Date.now();
|
|
465
|
+
const message = constructVerificationMessage({
|
|
466
|
+
walletAddress: signerWalletAddress,
|
|
467
|
+
signedTimestamp,
|
|
468
|
+
data: { action: 'gate_check_private_proofs', walletAddress: normalizedAddress },
|
|
469
|
+
verifierIds: ['ownership-basic'],
|
|
470
|
+
...(signerIsEvm ? { chainId: this._getHubChainId() } : { chain: resolvedChain })
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
let signature;
|
|
474
|
+
try {
|
|
475
|
+
signature = await signMessage({
|
|
476
|
+
provider,
|
|
477
|
+
message,
|
|
478
|
+
walletAddress: signerWalletAddress,
|
|
479
|
+
...(signerIsEvm ? {} : { chain: resolvedChain })
|
|
480
|
+
});
|
|
481
|
+
} catch (error) {
|
|
482
|
+
if (error.code === 4001) {
|
|
483
|
+
throw new ValidationError('User rejected signature request');
|
|
484
|
+
}
|
|
485
|
+
throw new ValidationError(`Failed to sign message: ${error.message}`);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return {
|
|
489
|
+
walletAddress: signerWalletAddress,
|
|
490
|
+
signature,
|
|
491
|
+
signedTimestamp,
|
|
492
|
+
...(signerIsEvm ? {} : { chain: resolvedChain, signatureMethod: resolvedSignatureMethod })
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
async createGatePrivateAuth(params = {}) {
|
|
497
|
+
const address = (params.address || '').toString();
|
|
498
|
+
if (!validateUniversalAddress(address, params.chain)) {
|
|
499
|
+
throw new ValidationError('Valid address is required');
|
|
500
|
+
}
|
|
501
|
+
return this._buildPrivateGateAuth({
|
|
502
|
+
address,
|
|
503
|
+
wallet: params.wallet,
|
|
504
|
+
chain: params.chain,
|
|
505
|
+
signatureMethod: params.signatureMethod
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ============================================================================
|
|
510
|
+
// CORE VERIFICATION METHODS
|
|
511
|
+
// ============================================================================
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* VERIFY - Standard verification (auto or manual)
|
|
515
|
+
*
|
|
516
|
+
* Create proofs with complete control over the verification process.
|
|
517
|
+
* If signature and walletAddress are omitted but verifier/content are provided,
|
|
518
|
+
* this method performs the wallet flow inline (no aliases, no secondary methods).
|
|
519
|
+
*
|
|
520
|
+
* @param {Object} params - Verification parameters
|
|
521
|
+
* @param {Array<string>} [params.verifierIds] - Array of verifier IDs (manual path)
|
|
522
|
+
* @param {Object} [params.data] - Verification data object (manual path)
|
|
523
|
+
* @param {string} [params.walletAddress] - Wallet address that signed the request (manual path)
|
|
524
|
+
* @param {string} [params.signature] - EIP-191 signature (manual path)
|
|
525
|
+
* @param {number} [params.signedTimestamp] - Unix timestamp when signature was created (manual path)
|
|
526
|
+
* @param {number} [params.chainId] - Chain ID for verification context (optional, managed by protocol)
|
|
527
|
+
* @param {Object} [params.options] - Additional options
|
|
528
|
+
* @param {string} [params.verifier] - Verifier ID (auto path)
|
|
529
|
+
* @param {string} [params.content] - Content/description (auto path)
|
|
530
|
+
* @param {Object} [params.wallet] - Optional injected wallet/provider (auto path)
|
|
531
|
+
* @returns {Promise<Object>} Verification result with proofId (qHash is a deprecated alias)
|
|
532
|
+
*
|
|
533
|
+
* @example
|
|
534
|
+
* const proof = await client.verify({
|
|
535
|
+
* verifierIds: ['ownership-basic'],
|
|
536
|
+
* data: {
|
|
537
|
+
* content: "My content",
|
|
538
|
+
* owner: walletAddress, // or ownerAddress for nft-ownership/token-holding
|
|
539
|
+
* reference: { type: 'other', id: 'my-unique-identifier' }
|
|
540
|
+
* },
|
|
541
|
+
* walletAddress: '0x...',
|
|
542
|
+
* signature: '0x...',
|
|
543
|
+
* signedTimestamp: Date.now(),
|
|
544
|
+
* options: { targetChains: [421614, 11155111] }
|
|
545
|
+
* });
|
|
546
|
+
*/
|
|
547
|
+
/**
|
|
548
|
+
* Create a verification proof
|
|
549
|
+
*
|
|
550
|
+
* @param {Object} params - Verification parameters
|
|
551
|
+
* @param {string} [params.verifier] - Verifier ID (e.g., 'ownership-basic')
|
|
552
|
+
* @param {string} [params.content] - Content to verify
|
|
553
|
+
* @param {Object} [params.data] - Structured verification data
|
|
554
|
+
* @param {Object} [params.wallet] - Wallet provider
|
|
555
|
+
* @param {Object} [params.options] - Additional options
|
|
556
|
+
* @returns {Promise<Object>} Verification result with proofId (qHash is a deprecated alias)
|
|
557
|
+
*
|
|
558
|
+
* @example
|
|
559
|
+
* // Simple ownership proof
|
|
560
|
+
* const proof = await client.verify({
|
|
561
|
+
* verifier: 'ownership-basic',
|
|
562
|
+
* content: 'Hello World',
|
|
563
|
+
* wallet: window.ethereum
|
|
564
|
+
* });
|
|
565
|
+
*/
|
|
566
|
+
async verify(params) {
|
|
567
|
+
// Auto path: if no manual signature fields but auto fields are provided, perform inline wallet flow
|
|
568
|
+
if ((!params?.signature || !params?.walletAddress) && (params?.verifier || params?.content || params?.data)) {
|
|
569
|
+
const { content, verifier = 'ownership-basic', data = null, wallet = null, options = {} } = params;
|
|
570
|
+
|
|
571
|
+
// ownership-basic: content required for simple mode, but data param mode allows contentHash or reference
|
|
572
|
+
if (verifier === 'ownership-basic' && !data && (!content || typeof content !== 'string')) {
|
|
573
|
+
throw new ValidationError('content is required and must be a string (or use data param with owner + reference)');
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
let validVerifiers = FALLBACK_PUBLIC_VERIFIERS;
|
|
577
|
+
try {
|
|
578
|
+
const discovered = await this.getVerifiers();
|
|
579
|
+
if (Array.isArray(discovered) && discovered.length > 0) {
|
|
580
|
+
validVerifiers = discovered;
|
|
581
|
+
}
|
|
582
|
+
} catch {
|
|
583
|
+
// Fallback keeps SDK usable if verifier catalog endpoint is temporarily unavailable.
|
|
584
|
+
}
|
|
585
|
+
if (!validVerifiers.includes(verifier)) {
|
|
586
|
+
throw new ValidationError(`Invalid verifier '${verifier}'. Must be one of: ${validVerifiers.join(', ')}.`);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (INTERACTIVE_VERIFIERS.has(verifier)) {
|
|
590
|
+
const hostedCheckoutUrl = options?.hostedCheckoutUrl || 'https://neus.network/verify';
|
|
591
|
+
throw new ValidationError(
|
|
592
|
+
`${verifier} requires hosted interactive checkout. Use VerifyGate or redirect to ${hostedCheckoutUrl}.`
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// These verifiers require explicit data parameter (no auto-path)
|
|
597
|
+
const requiresDataParam = [
|
|
598
|
+
'ownership-dns-txt',
|
|
599
|
+
'wallet-link',
|
|
600
|
+
'contract-ownership',
|
|
601
|
+
'ownership-pseudonym',
|
|
602
|
+
'wallet-risk',
|
|
603
|
+
'agent-identity',
|
|
604
|
+
'agent-delegation',
|
|
605
|
+
'ai-content-moderation'
|
|
606
|
+
];
|
|
607
|
+
if (requiresDataParam.includes(verifier)) {
|
|
608
|
+
if (!data || typeof data !== 'object') {
|
|
609
|
+
throw new ValidationError(`${verifier} requires explicit data parameter. Cannot use auto-path.`);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// Auto-detect wallet and get address
|
|
614
|
+
let walletAddress, provider;
|
|
615
|
+
if (wallet) {
|
|
616
|
+
walletAddress =
|
|
617
|
+
wallet.address ||
|
|
618
|
+
wallet.selectedAddress ||
|
|
619
|
+
wallet.walletAddress ||
|
|
620
|
+
(typeof wallet.getAddress === 'function' ? await wallet.getAddress() : null);
|
|
621
|
+
provider = wallet.provider || wallet;
|
|
622
|
+
if (!walletAddress && provider && typeof provider.request === 'function') {
|
|
623
|
+
const accounts = await provider.request({ method: 'eth_accounts' });
|
|
624
|
+
walletAddress = Array.isArray(accounts) ? accounts[0] : null;
|
|
625
|
+
}
|
|
626
|
+
} else {
|
|
627
|
+
if (typeof window === 'undefined' || !window.ethereum) {
|
|
628
|
+
throw new ConfigurationError('No Web3 wallet detected. Please install MetaMask or provide wallet parameter.');
|
|
629
|
+
}
|
|
630
|
+
await window.ethereum.request({ method: 'eth_requestAccounts' });
|
|
631
|
+
provider = window.ethereum;
|
|
632
|
+
const accounts = await provider.request({ method: 'eth_accounts' });
|
|
633
|
+
walletAddress = accounts[0];
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// Prepare verification data based on verifier type
|
|
637
|
+
let verificationData;
|
|
638
|
+
if (verifier === 'ownership-basic') {
|
|
639
|
+
if (data && typeof data === 'object') {
|
|
640
|
+
// Data param mode: use provided data, inject owner if missing
|
|
641
|
+
verificationData = {
|
|
642
|
+
owner: data.owner || walletAddress,
|
|
643
|
+
reference: data.reference,
|
|
644
|
+
...(data.content && { content: data.content }),
|
|
645
|
+
...(data.contentHash && { contentHash: data.contentHash }),
|
|
646
|
+
...(data.contentType && { contentType: data.contentType }),
|
|
647
|
+
...(data.provenance && { provenance: data.provenance })
|
|
648
|
+
};
|
|
649
|
+
} else {
|
|
650
|
+
// Simple content mode: derive reference from content
|
|
651
|
+
verificationData = {
|
|
652
|
+
content: content,
|
|
653
|
+
owner: walletAddress,
|
|
654
|
+
reference: { type: 'other' }
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
} else if (verifier === 'token-holding') {
|
|
658
|
+
if (!data?.contractAddress) {
|
|
659
|
+
throw new ValidationError('token-holding requires contractAddress in data parameter');
|
|
660
|
+
}
|
|
661
|
+
if (data?.minBalance === null || data?.minBalance === undefined) {
|
|
662
|
+
throw new ValidationError('token-holding requires minBalance in data parameter');
|
|
663
|
+
}
|
|
664
|
+
if (typeof data?.chainId !== 'number') {
|
|
665
|
+
throw new ValidationError('token-holding requires chainId (number) in data parameter');
|
|
666
|
+
}
|
|
667
|
+
verificationData = {
|
|
668
|
+
ownerAddress: walletAddress,
|
|
669
|
+
contractAddress: data.contractAddress,
|
|
670
|
+
minBalance: data.minBalance,
|
|
671
|
+
chainId: data.chainId
|
|
672
|
+
};
|
|
673
|
+
} else if (verifier === 'nft-ownership') {
|
|
674
|
+
if (!data?.contractAddress) {
|
|
675
|
+
throw new ValidationError('nft-ownership requires contractAddress in data parameter');
|
|
676
|
+
}
|
|
677
|
+
if (data?.tokenId === null || data?.tokenId === undefined) {
|
|
678
|
+
throw new ValidationError('nft-ownership requires tokenId in data parameter');
|
|
679
|
+
}
|
|
680
|
+
if (typeof data?.chainId !== 'number') {
|
|
681
|
+
throw new ValidationError('nft-ownership requires chainId (number) in data parameter');
|
|
682
|
+
}
|
|
683
|
+
verificationData = {
|
|
684
|
+
ownerAddress: walletAddress,
|
|
685
|
+
contractAddress: data.contractAddress,
|
|
686
|
+
tokenId: data.tokenId,
|
|
687
|
+
chainId: data.chainId,
|
|
688
|
+
tokenType: data?.tokenType || 'erc721'
|
|
689
|
+
};
|
|
690
|
+
} else if (verifier === 'ownership-dns-txt') {
|
|
691
|
+
if (!data?.domain) {
|
|
692
|
+
throw new ValidationError('ownership-dns-txt requires domain in data parameter');
|
|
693
|
+
}
|
|
694
|
+
verificationData = {
|
|
695
|
+
domain: data.domain,
|
|
696
|
+
walletAddress: walletAddress
|
|
697
|
+
};
|
|
698
|
+
} else if (verifier === 'wallet-link') {
|
|
699
|
+
if (!data?.secondaryWalletAddress) {
|
|
700
|
+
throw new ValidationError('wallet-link requires secondaryWalletAddress in data parameter');
|
|
701
|
+
}
|
|
702
|
+
if (!data?.signature) {
|
|
703
|
+
throw new ValidationError('wallet-link requires signature in data parameter (signed by secondary wallet)');
|
|
704
|
+
}
|
|
705
|
+
if (typeof data?.chain !== 'string' || !/^[a-z0-9]+:[^\s]+$/.test(data.chain)) {
|
|
706
|
+
throw new ValidationError('wallet-link requires chain (namespace:reference) in data parameter');
|
|
707
|
+
}
|
|
708
|
+
if (typeof data?.signatureMethod !== 'string' || !data.signatureMethod.trim()) {
|
|
709
|
+
throw new ValidationError('wallet-link requires signatureMethod in data parameter');
|
|
710
|
+
}
|
|
711
|
+
verificationData = {
|
|
712
|
+
primaryWalletAddress: walletAddress,
|
|
713
|
+
secondaryWalletAddress: data.secondaryWalletAddress,
|
|
714
|
+
signature: data.signature,
|
|
715
|
+
chain: data.chain,
|
|
716
|
+
signatureMethod: data.signatureMethod,
|
|
717
|
+
signedTimestamp: data?.signedTimestamp || Date.now()
|
|
718
|
+
};
|
|
719
|
+
} else if (verifier === 'contract-ownership') {
|
|
720
|
+
if (!data?.contractAddress) {
|
|
721
|
+
throw new ValidationError('contract-ownership requires contractAddress in data parameter');
|
|
722
|
+
}
|
|
723
|
+
if (typeof data?.chainId !== 'number') {
|
|
724
|
+
throw new ValidationError('contract-ownership requires chainId (number) in data parameter');
|
|
725
|
+
}
|
|
726
|
+
verificationData = {
|
|
727
|
+
contractAddress: data.contractAddress,
|
|
728
|
+
walletAddress: walletAddress,
|
|
729
|
+
chainId: data.chainId,
|
|
730
|
+
...(data?.method && { method: data.method })
|
|
731
|
+
};
|
|
732
|
+
} else if (verifier === 'agent-identity') {
|
|
733
|
+
if (!data?.agentId) {
|
|
734
|
+
throw new ValidationError('agent-identity requires agentId in data parameter');
|
|
735
|
+
}
|
|
736
|
+
verificationData = {
|
|
737
|
+
agentId: data.agentId,
|
|
738
|
+
agentWallet: data?.agentWallet || walletAddress,
|
|
739
|
+
...(data?.agentLabel && { agentLabel: data.agentLabel }),
|
|
740
|
+
...(data?.agentType && { agentType: data.agentType }),
|
|
741
|
+
...(data?.description && { description: data.description }),
|
|
742
|
+
...(data?.capabilities && { capabilities: data.capabilities })
|
|
743
|
+
};
|
|
744
|
+
} else if (verifier === 'agent-delegation') {
|
|
745
|
+
if (!data?.agentWallet) {
|
|
746
|
+
throw new ValidationError('agent-delegation requires agentWallet in data parameter');
|
|
747
|
+
}
|
|
748
|
+
verificationData = {
|
|
749
|
+
controllerWallet: data?.controllerWallet || walletAddress,
|
|
750
|
+
agentWallet: data.agentWallet,
|
|
751
|
+
...(data?.agentId && { agentId: data.agentId }),
|
|
752
|
+
...(data?.scope && { scope: data.scope }),
|
|
753
|
+
...(data?.permissions && { permissions: data.permissions }),
|
|
754
|
+
...(data?.maxSpend && { maxSpend: data.maxSpend }),
|
|
755
|
+
...(data?.expiresAt && { expiresAt: data.expiresAt })
|
|
756
|
+
};
|
|
757
|
+
} else if (verifier === 'ai-content-moderation') {
|
|
758
|
+
if (!data?.content) {
|
|
759
|
+
throw new ValidationError('ai-content-moderation requires content (base64) in data parameter');
|
|
760
|
+
}
|
|
761
|
+
if (!data?.contentType) {
|
|
762
|
+
throw new ValidationError('ai-content-moderation requires contentType (MIME type) in data parameter');
|
|
763
|
+
}
|
|
764
|
+
verificationData = {
|
|
765
|
+
content: data.content,
|
|
766
|
+
contentType: data.contentType,
|
|
767
|
+
...(data?.provider && { provider: data.provider })
|
|
768
|
+
};
|
|
769
|
+
} else if (verifier === 'ownership-pseudonym') {
|
|
770
|
+
if (!data?.pseudonymId) {
|
|
771
|
+
throw new ValidationError('ownership-pseudonym requires pseudonymId in data parameter');
|
|
772
|
+
}
|
|
773
|
+
verificationData = {
|
|
774
|
+
pseudonymId: data.pseudonymId,
|
|
775
|
+
...(data?.namespace && { namespace: data.namespace }),
|
|
776
|
+
...(data?.displayName && { displayName: data.displayName }),
|
|
777
|
+
...(data?.metadata && { metadata: data.metadata })
|
|
778
|
+
};
|
|
779
|
+
} else if (verifier === 'wallet-risk') {
|
|
780
|
+
// wallet-risk defaults to verifying the connected wallet
|
|
781
|
+
verificationData = {
|
|
782
|
+
walletAddress: data?.walletAddress || walletAddress,
|
|
783
|
+
...(data?.provider && { provider: data.provider }),
|
|
784
|
+
// Mainnet-first semantics: if caller doesn't provide chainId, default to Ethereum mainnet (1).
|
|
785
|
+
// This avoids accidental testnet semantics for risk providers.
|
|
786
|
+
chainId: (typeof data?.chainId === 'number' && Number.isFinite(data.chainId)) ? data.chainId : 1,
|
|
787
|
+
...(data?.includeDetails !== undefined && { includeDetails: data.includeDetails })
|
|
788
|
+
};
|
|
789
|
+
} else {
|
|
790
|
+
// Default structure for unknown verifiers (should not reach here with validVerifiers check)
|
|
791
|
+
verificationData = data ? {
|
|
792
|
+
content,
|
|
793
|
+
owner: walletAddress,
|
|
794
|
+
...data
|
|
795
|
+
} : {
|
|
796
|
+
content,
|
|
797
|
+
owner: walletAddress
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
const signedTimestamp = Date.now();
|
|
802
|
+
const verifierIds = [verifier];
|
|
803
|
+
const message = constructVerificationMessage({
|
|
804
|
+
walletAddress,
|
|
805
|
+
signedTimestamp,
|
|
806
|
+
data: verificationData,
|
|
807
|
+
verifierIds,
|
|
808
|
+
chainId: this._getHubChainId() // Protocol-managed chain
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
let signature;
|
|
812
|
+
try {
|
|
813
|
+
// UNIFIED SIGNING: Matches utils/core.ts personalSignUniversal exactly
|
|
814
|
+
const toHexUtf8 = (s) => {
|
|
815
|
+
try {
|
|
816
|
+
const enc = new TextEncoder();
|
|
817
|
+
const bytes = enc.encode(s);
|
|
818
|
+
let hex = '0x';
|
|
819
|
+
for (let i = 0; i < bytes.length; i++) hex += bytes[i].toString(16).padStart(2, '0');
|
|
820
|
+
return hex;
|
|
821
|
+
} catch {
|
|
822
|
+
let hex = '0x';
|
|
823
|
+
for (let i = 0; i < s.length; i++) hex += s.charCodeAt(i).toString(16).padStart(2, '0');
|
|
824
|
+
return hex;
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
// Detect Farcaster wallet - requires hex-encoded messages FIRST
|
|
829
|
+
const isFarcasterWallet = (() => {
|
|
830
|
+
if (typeof window === 'undefined') return false;
|
|
831
|
+
try {
|
|
832
|
+
const w = window;
|
|
833
|
+
const fc = w.farcaster;
|
|
834
|
+
if (!fc || !fc.context) return false;
|
|
835
|
+
const fcProvider = fc.provider || fc.walletProvider || (fc.context && fc.context.walletProvider);
|
|
836
|
+
if (fcProvider === provider) return true;
|
|
837
|
+
if (w.mini && w.mini.wallet === provider && fc && fc.context) return true;
|
|
838
|
+
if (w.ethereum === provider && fc && fc.context) return true;
|
|
839
|
+
} catch {
|
|
840
|
+
// ignore: optional Farcaster detection
|
|
841
|
+
}
|
|
842
|
+
return false;
|
|
843
|
+
})();
|
|
844
|
+
|
|
845
|
+
if (isFarcasterWallet) {
|
|
846
|
+
try {
|
|
847
|
+
const hexMsg = toHexUtf8(message);
|
|
848
|
+
signature = await provider.request({ method: 'personal_sign', params: [hexMsg, walletAddress] });
|
|
849
|
+
} catch (e) {
|
|
850
|
+
// Fall through
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
if (!signature) {
|
|
855
|
+
try {
|
|
856
|
+
signature = await provider.request({ method: 'personal_sign', params: [message, walletAddress] });
|
|
857
|
+
} catch (e) {
|
|
858
|
+
const msg = String(e && (e.message || e.reason) || e || '').toLowerCase();
|
|
859
|
+
const errCode = (e && (e.code || (e.error && e.error.code))) || null;
|
|
860
|
+
const needsHex = /byte|bytes|invalid byte sequence|encoding|non-hex/i.test(msg);
|
|
861
|
+
|
|
862
|
+
const methodUnsupported = (
|
|
863
|
+
/method.*not.*supported|unsupported|not implemented|method not found|unknown method|does not support/i.test(msg) ||
|
|
864
|
+
errCode === -32601 ||
|
|
865
|
+
errCode === 4200 ||
|
|
866
|
+
(msg.includes('personal_sign') && msg.includes('not')) ||
|
|
867
|
+
(msg.includes('request method') && msg.includes('not supported'))
|
|
868
|
+
);
|
|
869
|
+
|
|
870
|
+
if (methodUnsupported) {
|
|
871
|
+
this._log('personal_sign not supported; attempting eth_sign fallback');
|
|
872
|
+
try {
|
|
873
|
+
const enc = new TextEncoder();
|
|
874
|
+
const bytes = enc.encode(message);
|
|
875
|
+
const prefix = `\x19Ethereum Signed Message:\n${bytes.length}`;
|
|
876
|
+
const full = new Uint8Array(prefix.length + bytes.length);
|
|
877
|
+
for (let i = 0; i < prefix.length; i++) full[i] = prefix.charCodeAt(i);
|
|
878
|
+
full.set(bytes, prefix.length);
|
|
879
|
+
let payloadHex = '0x';
|
|
880
|
+
for (let i = 0; i < full.length; i++) payloadHex += full[i].toString(16).padStart(2, '0');
|
|
881
|
+
try {
|
|
882
|
+
if (typeof window !== 'undefined') window.__NEUS_ALLOW_ETH_SIGN__ = true;
|
|
883
|
+
} catch {
|
|
884
|
+
// ignore
|
|
885
|
+
}
|
|
886
|
+
signature = await provider.request({ method: 'eth_sign', params: [walletAddress, payloadHex], neusAllowEthSign: true });
|
|
887
|
+
try {
|
|
888
|
+
if (typeof window !== 'undefined') delete window.__NEUS_ALLOW_ETH_SIGN__;
|
|
889
|
+
} catch {
|
|
890
|
+
// ignore
|
|
891
|
+
}
|
|
892
|
+
} catch (fallbackErr) {
|
|
893
|
+
this._log('eth_sign fallback failed', { message: fallbackErr?.message || String(fallbackErr) });
|
|
894
|
+
try {
|
|
895
|
+
if (typeof window !== 'undefined') delete window.__NEUS_ALLOW_ETH_SIGN__;
|
|
896
|
+
} catch {
|
|
897
|
+
// ignore
|
|
898
|
+
}
|
|
899
|
+
throw e;
|
|
900
|
+
}
|
|
901
|
+
} else if (needsHex) {
|
|
902
|
+
this._log('Retrying personal_sign with hex-encoded message');
|
|
903
|
+
const hexMsg = toHexUtf8(message);
|
|
904
|
+
signature = await provider.request({ method: 'personal_sign', params: [hexMsg, walletAddress] });
|
|
905
|
+
} else {
|
|
906
|
+
throw e;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
} catch (error) {
|
|
911
|
+
if (error.code === 4001) {
|
|
912
|
+
throw new ValidationError('User rejected the signature request. Signature is required to create proofs.');
|
|
913
|
+
}
|
|
914
|
+
throw new ValidationError(`Failed to sign verification message: ${error.message}`);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
return this.verify({
|
|
918
|
+
verifierIds,
|
|
919
|
+
data: verificationData,
|
|
920
|
+
walletAddress,
|
|
921
|
+
signature,
|
|
922
|
+
signedTimestamp,
|
|
923
|
+
options
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
const {
|
|
928
|
+
verifierIds,
|
|
929
|
+
data,
|
|
930
|
+
walletAddress,
|
|
931
|
+
signature,
|
|
932
|
+
signedTimestamp,
|
|
933
|
+
chainId,
|
|
934
|
+
chain,
|
|
935
|
+
signatureMethod,
|
|
936
|
+
options = {}
|
|
937
|
+
} = params;
|
|
938
|
+
|
|
939
|
+
const resolvedChainId = chainId || (chain ? null : NEUS_CONSTANTS.HUB_CHAIN_ID);
|
|
940
|
+
|
|
941
|
+
// Normalize verifier IDs
|
|
942
|
+
const normalizeVerifierId = (id) => {
|
|
943
|
+
if (typeof id !== 'string') return id;
|
|
944
|
+
const match = id.match(/^(.*)@\d+$/);
|
|
945
|
+
return match ? match[1] : id;
|
|
946
|
+
};
|
|
947
|
+
const normalizedVerifierIds = Array.isArray(verifierIds) ? verifierIds.map(normalizeVerifierId) : [];
|
|
948
|
+
|
|
949
|
+
// Validate required parameters
|
|
950
|
+
if (!normalizedVerifierIds || normalizedVerifierIds.length === 0) {
|
|
951
|
+
throw new ValidationError('verifierIds array is required');
|
|
952
|
+
}
|
|
953
|
+
if (!data || typeof data !== 'object') {
|
|
954
|
+
throw new ValidationError('data object is required');
|
|
955
|
+
}
|
|
956
|
+
if (!walletAddress || typeof walletAddress !== 'string') {
|
|
957
|
+
throw new ValidationError('walletAddress is required');
|
|
958
|
+
}
|
|
959
|
+
if (!signature) {
|
|
960
|
+
throw new ValidationError('signature is required');
|
|
961
|
+
}
|
|
962
|
+
if (!signedTimestamp || typeof signedTimestamp !== 'number') {
|
|
963
|
+
throw new ValidationError('signedTimestamp is required');
|
|
964
|
+
}
|
|
965
|
+
if (resolvedChainId !== null && typeof resolvedChainId !== 'number') {
|
|
966
|
+
throw new ValidationError('chainId must be a number');
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
// Validate verifier data
|
|
970
|
+
for (const verifierId of normalizedVerifierIds) {
|
|
971
|
+
const validation = validateVerifierData(verifierId, data);
|
|
972
|
+
if (!validation.valid) {
|
|
973
|
+
throw new ValidationError(`Validation failed for verifier '${verifierId}': ${validation.error}`);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// Build options payload (public surface)
|
|
978
|
+
const optionsPayload = {
|
|
979
|
+
...(options && typeof options === 'object' ? options : {}),
|
|
980
|
+
targetChains: Array.isArray(options?.targetChains) ? options.targetChains : [],
|
|
981
|
+
// Privacy and storage options (defaults)
|
|
982
|
+
privacyLevel: options?.privacyLevel || 'private',
|
|
983
|
+
publicDisplay: options?.publicDisplay || false,
|
|
984
|
+
storeOriginalContent: options?.storeOriginalContent || false
|
|
985
|
+
};
|
|
986
|
+
if (typeof options?.enableIpfs === 'boolean') optionsPayload.enableIpfs = options.enableIpfs;
|
|
987
|
+
|
|
988
|
+
const requestData = {
|
|
989
|
+
verifierIds: normalizedVerifierIds,
|
|
990
|
+
data,
|
|
991
|
+
walletAddress,
|
|
992
|
+
signature,
|
|
993
|
+
signedTimestamp,
|
|
994
|
+
...(resolvedChainId !== null && { chainId: resolvedChainId }),
|
|
995
|
+
...(chain && { chain }),
|
|
996
|
+
...(signatureMethod && { signatureMethod }),
|
|
997
|
+
options: optionsPayload
|
|
998
|
+
};
|
|
999
|
+
|
|
1000
|
+
// SECURITY: Do not send proof signatures in Authorization headers.
|
|
1001
|
+
// Signatures belong in the request body only (they are not bearer tokens).
|
|
1002
|
+
const response = await this._makeRequest('POST', '/api/v1/verification', requestData);
|
|
1003
|
+
|
|
1004
|
+
if (!response.success) {
|
|
1005
|
+
throw new ApiError(`Verification failed: ${response.error?.message || 'Unknown error'}`, response.error);
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
return this._formatResponse(response);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// ============================================================================
|
|
1012
|
+
// STATUS AND UTILITY METHODS
|
|
1013
|
+
// ============================================================================
|
|
1014
|
+
|
|
1015
|
+
/**
|
|
1016
|
+
* Get verification status
|
|
1017
|
+
*
|
|
1018
|
+
* @param {string} proofId - Proof ID (standard). `qHash` is a deprecated alias (same value).
|
|
1019
|
+
* @returns {Promise<Object>} Verification status and data
|
|
1020
|
+
*
|
|
1021
|
+
* @example
|
|
1022
|
+
* const result = await client.getStatus('0x...');
|
|
1023
|
+
* console.log('Status:', result.status);
|
|
1024
|
+
*/
|
|
1025
|
+
async getStatus(proofId) {
|
|
1026
|
+
if (!proofId || typeof proofId !== 'string') {
|
|
1027
|
+
throw new ValidationError('proofId is required');
|
|
1028
|
+
}
|
|
1029
|
+
const response = await this._makeRequest('GET', `/api/v1/verification/status/${proofId}`);
|
|
1030
|
+
|
|
1031
|
+
if (!response.success) {
|
|
1032
|
+
throw new ApiError(`Failed to get status: ${response.error?.message || 'Unknown error'}`, response.error);
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
return this._formatResponse(response);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* Get private proof status with wallet signature
|
|
1040
|
+
*
|
|
1041
|
+
* @param {string} proofId - Proof ID (standard). `qHash` is a deprecated alias (same value).
|
|
1042
|
+
* @param {Object} wallet - Wallet provider (window.ethereum or ethers Wallet)
|
|
1043
|
+
* @returns {Promise<Object>} Private verification status and data
|
|
1044
|
+
*
|
|
1045
|
+
* @example
|
|
1046
|
+
* // Access private proof
|
|
1047
|
+
* const privateData = await client.getPrivateStatus(proofId, window.ethereum);
|
|
1048
|
+
*/
|
|
1049
|
+
async getPrivateStatus(proofId, wallet = null) {
|
|
1050
|
+
if (!proofId || typeof proofId !== 'string') {
|
|
1051
|
+
throw new ValidationError('proofId is required');
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// Allow pre-signed universal owner auth (e.g. Solana) to avoid wallet-provider assumptions.
|
|
1055
|
+
const isPreSignedAuth = wallet &&
|
|
1056
|
+
typeof wallet === 'object' &&
|
|
1057
|
+
typeof wallet.walletAddress === 'string' &&
|
|
1058
|
+
typeof wallet.signature === 'string' &&
|
|
1059
|
+
typeof wallet.signedTimestamp === 'number';
|
|
1060
|
+
if (isPreSignedAuth) {
|
|
1061
|
+
const auth = wallet;
|
|
1062
|
+
const headers = {
|
|
1063
|
+
'x-wallet-address': String(auth.walletAddress),
|
|
1064
|
+
'x-signature': String(auth.signature),
|
|
1065
|
+
'x-signed-timestamp': String(auth.signedTimestamp),
|
|
1066
|
+
...(typeof auth.chain === 'string' && auth.chain.trim() ? { 'x-chain': auth.chain.trim() } : {}),
|
|
1067
|
+
...(typeof auth.signatureMethod === 'string' && auth.signatureMethod.trim() ? { 'x-signature-method': auth.signatureMethod.trim() } : {})
|
|
1068
|
+
};
|
|
1069
|
+
const response = await this._makeRequest('GET', `/api/v1/verification/status/${proofId}`, null, headers);
|
|
1070
|
+
if (!response.success) {
|
|
1071
|
+
throw new ApiError(
|
|
1072
|
+
`Failed to access private proof: ${response.error?.message || 'Unauthorized'}`,
|
|
1073
|
+
response.error
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
return this._formatResponse(response);
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
const providerWallet = wallet || this._getDefaultBrowserWallet();
|
|
1080
|
+
const { signerWalletAddress: walletAddress, provider } = await this._resolveWalletSigner(providerWallet);
|
|
1081
|
+
if (!walletAddress || typeof walletAddress !== 'string') {
|
|
1082
|
+
throw new ConfigurationError('No wallet accounts available');
|
|
1083
|
+
}
|
|
1084
|
+
const signerIsEvm = EVM_ADDRESS_RE.test(this._normalizeIdentity(walletAddress));
|
|
1085
|
+
const chain = this._inferChainForAddress(walletAddress);
|
|
1086
|
+
const signatureMethod = signerIsEvm ? 'eip191' : 'ed25519';
|
|
1087
|
+
|
|
1088
|
+
const signedTimestamp = Date.now();
|
|
1089
|
+
|
|
1090
|
+
// IMPORTANT: This must match the server's Standard Signing String owner-access check.
|
|
1091
|
+
// Keep wire payload key `qHash` for backwards compatibility.
|
|
1092
|
+
const message = constructVerificationMessage({
|
|
1093
|
+
walletAddress,
|
|
1094
|
+
signedTimestamp,
|
|
1095
|
+
data: { action: 'access_private_proof', qHash: proofId },
|
|
1096
|
+
verifierIds: ['ownership-basic'],
|
|
1097
|
+
...(signerIsEvm ? { chainId: this._getHubChainId() } : { chain })
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
let signature;
|
|
1101
|
+
try {
|
|
1102
|
+
signature = await signMessage({
|
|
1103
|
+
provider,
|
|
1104
|
+
message,
|
|
1105
|
+
walletAddress,
|
|
1106
|
+
...(signerIsEvm ? {} : { chain })
|
|
1107
|
+
});
|
|
1108
|
+
} catch (error) {
|
|
1109
|
+
if (error.code === 4001) {
|
|
1110
|
+
throw new ValidationError('User rejected signature request');
|
|
1111
|
+
}
|
|
1112
|
+
throw new ValidationError(`Failed to sign message: ${error.message}`);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
// Make request with signature headers (server reads x-wallet-address/x-signature/x-signed-timestamp)
|
|
1116
|
+
const response = await this._makeRequest('GET', `/api/v1/verification/status/${proofId}`, null, {
|
|
1117
|
+
'x-wallet-address': walletAddress,
|
|
1118
|
+
'x-signature': signature,
|
|
1119
|
+
'x-signed-timestamp': signedTimestamp.toString(),
|
|
1120
|
+
...(signerIsEvm ? {} : { 'x-chain': chain, 'x-signature-method': signatureMethod })
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
if (!response.success) {
|
|
1124
|
+
throw new ApiError(
|
|
1125
|
+
`Failed to access private proof: ${response.error?.message || 'Unauthorized'}`,
|
|
1126
|
+
response.error
|
|
1127
|
+
);
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
return this._formatResponse(response);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
/**
|
|
1134
|
+
* Check API health
|
|
1135
|
+
*
|
|
1136
|
+
* @returns {Promise<boolean>} True if API is healthy
|
|
1137
|
+
*/
|
|
1138
|
+
async isHealthy() {
|
|
1139
|
+
try {
|
|
1140
|
+
const response = await this._makeRequest('GET', '/api/v1/health');
|
|
1141
|
+
return response.success === true;
|
|
1142
|
+
} catch {
|
|
1143
|
+
return false;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/**
|
|
1148
|
+
* List available verifiers
|
|
1149
|
+
*
|
|
1150
|
+
* @returns {Promise<string[]>} Array of verifier IDs
|
|
1151
|
+
*/
|
|
1152
|
+
async getVerifiers() {
|
|
1153
|
+
const response = await this._makeRequest('GET', '/api/v1/verification/verifiers');
|
|
1154
|
+
if (!response.success) {
|
|
1155
|
+
throw new ApiError(`Failed to get verifiers: ${response.error?.message || 'Unknown error'}`, response.error);
|
|
1156
|
+
}
|
|
1157
|
+
return Array.isArray(response.data) ? response.data : [];
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
/**
|
|
1161
|
+
* POLL PROOF STATUS - Wait for verification completion
|
|
1162
|
+
*
|
|
1163
|
+
* Polls the verification status until it reaches a terminal state (completed or failed).
|
|
1164
|
+
* Useful for providing real-time feedback to users during verification.
|
|
1165
|
+
*
|
|
1166
|
+
* @param {string} proofId - Proof ID to poll (standard). `qHash` is a deprecated alias (same value).
|
|
1167
|
+
* @param {Object} [options] - Polling options
|
|
1168
|
+
* @param {number} [options.interval=5000] - Polling interval in ms
|
|
1169
|
+
* @param {number} [options.timeout=120000] - Total timeout in ms
|
|
1170
|
+
* @param {Function} [options.onProgress] - Progress callback function
|
|
1171
|
+
* @returns {Promise<Object>} Final verification status
|
|
1172
|
+
*
|
|
1173
|
+
* @example
|
|
1174
|
+
* const finalStatus = await client.pollProofStatus(proofId, {
|
|
1175
|
+
* interval: 3000,
|
|
1176
|
+
* timeout: 60000,
|
|
1177
|
+
* onProgress: (status) => {
|
|
1178
|
+
* console.log('Current status:', status.status);
|
|
1179
|
+
* if (status.crosschain) {
|
|
1180
|
+
* console.log(`Cross-chain: ${status.crosschain.finalized}/${status.crosschain.totalChains}`);
|
|
1181
|
+
* }
|
|
1182
|
+
* }
|
|
1183
|
+
* });
|
|
1184
|
+
*/
|
|
1185
|
+
async pollProofStatus(proofId, options = {}) {
|
|
1186
|
+
const {
|
|
1187
|
+
interval = 5000,
|
|
1188
|
+
timeout = 120000,
|
|
1189
|
+
onProgress
|
|
1190
|
+
} = options;
|
|
1191
|
+
|
|
1192
|
+
if (!proofId || typeof proofId !== 'string') {
|
|
1193
|
+
throw new ValidationError('proofId is required');
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
const startTime = Date.now();
|
|
1197
|
+
let consecutiveRateLimits = 0;
|
|
1198
|
+
|
|
1199
|
+
while (Date.now() - startTime < timeout) {
|
|
1200
|
+
try {
|
|
1201
|
+
const status = await this.getStatus(proofId);
|
|
1202
|
+
consecutiveRateLimits = 0;
|
|
1203
|
+
|
|
1204
|
+
// Call progress callback if provided
|
|
1205
|
+
if (onProgress && typeof onProgress === 'function') {
|
|
1206
|
+
onProgress(status.data || status);
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
// Check for terminal states
|
|
1210
|
+
const currentStatus = status.data?.status || status.status;
|
|
1211
|
+
if (this._isTerminalStatus(currentStatus)) {
|
|
1212
|
+
this._log('Verification completed', { status: currentStatus, duration: Date.now() - startTime });
|
|
1213
|
+
return status;
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
// Wait before next poll
|
|
1217
|
+
await new Promise(resolve => setTimeout(resolve, interval));
|
|
1218
|
+
|
|
1219
|
+
} catch (error) {
|
|
1220
|
+
this._log('Status poll error', error.message);
|
|
1221
|
+
// Continue polling unless it's a validation error
|
|
1222
|
+
if (error instanceof ValidationError) {
|
|
1223
|
+
throw error;
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
let nextDelay = interval;
|
|
1227
|
+
if (error instanceof ApiError && Number(error.statusCode) === 429) {
|
|
1228
|
+
consecutiveRateLimits += 1;
|
|
1229
|
+
const exp = Math.min(6, consecutiveRateLimits); // cap growth
|
|
1230
|
+
const base = Math.max(500, Number(interval) || 0);
|
|
1231
|
+
const max = 30000; // 30s cap to keep UX responsive
|
|
1232
|
+
const backoff = Math.min(max, base * Math.pow(2, exp));
|
|
1233
|
+
const jitter = Math.floor(backoff * (0.5 + Math.random() * 0.5)); // 50-100%
|
|
1234
|
+
nextDelay = jitter;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
await new Promise(resolve => setTimeout(resolve, nextDelay));
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
throw new NetworkError(`Polling timeout after ${timeout}ms`, 'POLLING_TIMEOUT');
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/**
|
|
1245
|
+
* DETECT CHAIN ID - Get current wallet chain
|
|
1246
|
+
*
|
|
1247
|
+
* @returns {Promise<number>} Current chain ID
|
|
1248
|
+
*/
|
|
1249
|
+
async detectChainId() {
|
|
1250
|
+
if (typeof window === 'undefined' || !window.ethereum) {
|
|
1251
|
+
throw new ConfigurationError('No Web3 wallet detected');
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
try {
|
|
1255
|
+
const chainId = await window.ethereum.request({ method: 'eth_chainId' });
|
|
1256
|
+
return parseInt(chainId, 16);
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
throw new NetworkError(`Failed to detect chain ID: ${error.message}`);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/** Revoke your own proof (owner-signed) */
|
|
1263
|
+
async revokeOwnProof(proofId, wallet) {
|
|
1264
|
+
if (!proofId || typeof proofId !== 'string') {
|
|
1265
|
+
throw new ValidationError('proofId is required');
|
|
1266
|
+
}
|
|
1267
|
+
const providerWallet = wallet || this._getDefaultBrowserWallet();
|
|
1268
|
+
const { signerWalletAddress: address, provider } = await this._resolveWalletSigner(providerWallet);
|
|
1269
|
+
if (!address || typeof address !== 'string') {
|
|
1270
|
+
throw new ConfigurationError('No wallet accounts available');
|
|
1271
|
+
}
|
|
1272
|
+
const signerIsEvm = EVM_ADDRESS_RE.test(this._normalizeIdentity(address));
|
|
1273
|
+
const chain = this._inferChainForAddress(address);
|
|
1274
|
+
const signatureMethod = signerIsEvm ? 'eip191' : 'ed25519';
|
|
1275
|
+
const signedTimestamp = Date.now();
|
|
1276
|
+
|
|
1277
|
+
const message = constructVerificationMessage({
|
|
1278
|
+
walletAddress: address,
|
|
1279
|
+
signedTimestamp,
|
|
1280
|
+
// Keep wire payload key `qHash` for backwards compatibility.
|
|
1281
|
+
data: { action: 'revoke_proof', qHash: proofId },
|
|
1282
|
+
verifierIds: ['ownership-basic'],
|
|
1283
|
+
...(signerIsEvm ? { chainId: this._getHubChainId() } : { chain })
|
|
1284
|
+
});
|
|
1285
|
+
|
|
1286
|
+
let signature;
|
|
1287
|
+
try {
|
|
1288
|
+
signature = await signMessage({
|
|
1289
|
+
provider,
|
|
1290
|
+
message,
|
|
1291
|
+
walletAddress: address,
|
|
1292
|
+
...(signerIsEvm ? {} : { chain })
|
|
1293
|
+
});
|
|
1294
|
+
} catch (error) {
|
|
1295
|
+
if (error.code === 4001) {
|
|
1296
|
+
throw new ValidationError('User rejected revocation signature');
|
|
1297
|
+
}
|
|
1298
|
+
throw new ValidationError(`Failed to sign revocation: ${error.message}`);
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
const res = await fetch(`${this.config.apiUrl}/api/v1/proofs/${proofId}/revoke-self`, {
|
|
1302
|
+
method: 'POST',
|
|
1303
|
+
// SECURITY: Do not put proof signatures into Authorization headers.
|
|
1304
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1305
|
+
body: JSON.stringify({
|
|
1306
|
+
walletAddress: address,
|
|
1307
|
+
signature,
|
|
1308
|
+
signedTimestamp,
|
|
1309
|
+
...(signerIsEvm ? {} : { chain, signatureMethod })
|
|
1310
|
+
})
|
|
1311
|
+
});
|
|
1312
|
+
const json = await res.json();
|
|
1313
|
+
if (!json.success) {
|
|
1314
|
+
throw new ApiError(json.error?.message || 'Failed to revoke proof', json.error);
|
|
1315
|
+
}
|
|
1316
|
+
return true;
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
// ============================================================================
|
|
1320
|
+
// PROOFS & GATING METHODS
|
|
1321
|
+
// ============================================================================
|
|
1322
|
+
|
|
1323
|
+
/**
|
|
1324
|
+
* GET PROOFS BY WALLET - Fetch proofs for a wallet address
|
|
1325
|
+
*
|
|
1326
|
+
* @param {string} walletAddress - Wallet identity (EVM/Solana/DID)
|
|
1327
|
+
* @param {Object} [options] - Filter options
|
|
1328
|
+
* @param {number} [options.limit] - Max results (default: 50; higher limits require owner access)
|
|
1329
|
+
* @param {number} [options.offset] - Pagination offset (default: 0)
|
|
1330
|
+
* @returns {Promise<Object>} Proofs result
|
|
1331
|
+
*
|
|
1332
|
+
* @example
|
|
1333
|
+
* const result = await client.getProofsByWallet('0x...', {
|
|
1334
|
+
* limit: 50,
|
|
1335
|
+
* offset: 0
|
|
1336
|
+
* });
|
|
1337
|
+
*/
|
|
1338
|
+
async getProofsByWallet(walletAddress, options = {}) {
|
|
1339
|
+
if (!walletAddress || typeof walletAddress !== 'string') {
|
|
1340
|
+
throw new ValidationError('walletAddress is required');
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
const id = walletAddress.trim();
|
|
1344
|
+
const pathId = /^0x[a-fA-F0-9]{40}$/i.test(id) ? id.toLowerCase() : id;
|
|
1345
|
+
|
|
1346
|
+
const qs = [];
|
|
1347
|
+
if (options.limit) qs.push(`limit=${encodeURIComponent(String(options.limit))}`);
|
|
1348
|
+
if (options.offset) qs.push(`offset=${encodeURIComponent(String(options.offset))}`);
|
|
1349
|
+
|
|
1350
|
+
const query = qs.length ? `?${qs.join('&')}` : '';
|
|
1351
|
+
const response = await this._makeRequest(
|
|
1352
|
+
'GET',
|
|
1353
|
+
`/api/v1/proofs/byWallet/${encodeURIComponent(pathId)}${query}`
|
|
1354
|
+
);
|
|
1355
|
+
|
|
1356
|
+
if (!response.success) {
|
|
1357
|
+
throw new ApiError(`Failed to get proofs: ${response.error?.message || 'Unknown error'}`, response.error);
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
// Normalize response structure
|
|
1361
|
+
const proofs = response.data?.proofs || response.data || response.proofs || [];
|
|
1362
|
+
return {
|
|
1363
|
+
success: true,
|
|
1364
|
+
proofs: Array.isArray(proofs) ? proofs : [],
|
|
1365
|
+
totalCount: response.data?.totalCount ?? proofs.length,
|
|
1366
|
+
hasMore: Boolean(response.data?.hasMore),
|
|
1367
|
+
nextOffset: response.data?.nextOffset ?? null
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
/**
|
|
1372
|
+
* Get proofs by wallet (owner access)
|
|
1373
|
+
*
|
|
1374
|
+
* Signs an owner-access intent and requests private proofs via signature headers.
|
|
1375
|
+
*
|
|
1376
|
+
* @param {string} walletAddress - Wallet identity (EVM/Solana/DID)
|
|
1377
|
+
* @param {Object} [options]
|
|
1378
|
+
* @param {number} [options.limit] - Max results (server enforces caps)
|
|
1379
|
+
* @param {number} [options.offset] - Pagination offset
|
|
1380
|
+
* @param {Object} [wallet] - Optional injected wallet/provider (MetaMask/ethers Wallet)
|
|
1381
|
+
*/
|
|
1382
|
+
async getPrivateProofsByWallet(walletAddress, options = {}, wallet = null) {
|
|
1383
|
+
if (!walletAddress || typeof walletAddress !== 'string') {
|
|
1384
|
+
throw new ValidationError('walletAddress is required');
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
const id = walletAddress.trim();
|
|
1388
|
+
const pathId = /^0x[a-fA-F0-9]{40}$/i.test(id) ? id.toLowerCase() : id;
|
|
1389
|
+
|
|
1390
|
+
const requestedIdentity = this._normalizeIdentity(id);
|
|
1391
|
+
|
|
1392
|
+
// Auto-detect wallet if not provided
|
|
1393
|
+
if (!wallet) {
|
|
1394
|
+
const defaultWallet = this._getDefaultBrowserWallet();
|
|
1395
|
+
if (!defaultWallet) {
|
|
1396
|
+
throw new ConfigurationError('No wallet provider available');
|
|
1397
|
+
}
|
|
1398
|
+
wallet = defaultWallet;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
const { signerWalletAddress, provider } = await this._resolveWalletSigner(wallet);
|
|
1402
|
+
|
|
1403
|
+
if (!signerWalletAddress || typeof signerWalletAddress !== 'string') {
|
|
1404
|
+
throw new ConfigurationError('No wallet accounts available');
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
const normalizedSigner = this._normalizeIdentity(signerWalletAddress);
|
|
1408
|
+
if (!normalizedSigner || normalizedSigner !== requestedIdentity) {
|
|
1409
|
+
throw new ValidationError('wallet must match walletAddress for private proof access');
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
const signerIsEvm = EVM_ADDRESS_RE.test(normalizedSigner);
|
|
1413
|
+
const chain = this._inferChainForAddress(normalizedSigner, options?.chain);
|
|
1414
|
+
const signatureMethod = (typeof options?.signatureMethod === 'string' && options.signatureMethod.trim())
|
|
1415
|
+
? options.signatureMethod.trim()
|
|
1416
|
+
: (signerIsEvm ? 'eip191' : 'ed25519');
|
|
1417
|
+
|
|
1418
|
+
const signedTimestamp = Date.now();
|
|
1419
|
+
const message = constructVerificationMessage({
|
|
1420
|
+
walletAddress: signerWalletAddress,
|
|
1421
|
+
signedTimestamp,
|
|
1422
|
+
data: { action: 'access_private_proofs_by_wallet', walletAddress: normalizedSigner },
|
|
1423
|
+
verifierIds: ['ownership-basic'],
|
|
1424
|
+
...(signerIsEvm ? { chainId: this._getHubChainId() } : { chain })
|
|
1425
|
+
});
|
|
1426
|
+
|
|
1427
|
+
let signature;
|
|
1428
|
+
try {
|
|
1429
|
+
signature = await signMessage({
|
|
1430
|
+
provider,
|
|
1431
|
+
message,
|
|
1432
|
+
walletAddress: signerWalletAddress,
|
|
1433
|
+
...(signerIsEvm ? {} : { chain })
|
|
1434
|
+
});
|
|
1435
|
+
} catch (error) {
|
|
1436
|
+
if (error.code === 4001) {
|
|
1437
|
+
throw new ValidationError('User rejected signature request');
|
|
1438
|
+
}
|
|
1439
|
+
throw new ValidationError(`Failed to sign message: ${error.message}`);
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
const qs = [];
|
|
1443
|
+
if (options.limit) qs.push(`limit=${encodeURIComponent(String(options.limit))}`);
|
|
1444
|
+
if (options.offset) qs.push(`offset=${encodeURIComponent(String(options.offset))}`);
|
|
1445
|
+
const query = qs.length ? `?${qs.join('&')}` : '';
|
|
1446
|
+
|
|
1447
|
+
const response = await this._makeRequest('GET', `/api/v1/proofs/byWallet/${encodeURIComponent(pathId)}${query}`, null, {
|
|
1448
|
+
'x-wallet-address': signerWalletAddress,
|
|
1449
|
+
'x-signature': signature,
|
|
1450
|
+
'x-signed-timestamp': signedTimestamp.toString(),
|
|
1451
|
+
...(signerIsEvm ? {} : { 'x-chain': chain, 'x-signature-method': signatureMethod })
|
|
1452
|
+
});
|
|
1453
|
+
|
|
1454
|
+
if (!response.success) {
|
|
1455
|
+
throw new ApiError(`Failed to get proofs: ${response.error?.message || 'Unauthorized'}`, response.error);
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
const proofs = response.data?.proofs || response.data || response.proofs || [];
|
|
1459
|
+
return {
|
|
1460
|
+
success: true,
|
|
1461
|
+
proofs: Array.isArray(proofs) ? proofs : [],
|
|
1462
|
+
totalCount: response.data?.totalCount ?? proofs.length,
|
|
1463
|
+
hasMore: Boolean(response.data?.hasMore),
|
|
1464
|
+
nextOffset: response.data?.nextOffset ?? null
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
/**
|
|
1469
|
+
* GATE CHECK (API) - Minimal eligibility check
|
|
1470
|
+
*
|
|
1471
|
+
* Calls the gate endpoint and returns a **minimal** yes/no response.
|
|
1472
|
+
* By default this checks **public + discoverable** proofs only.
|
|
1473
|
+
*
|
|
1474
|
+
* When `includePrivate=true`, this can perform owner-signed private checks
|
|
1475
|
+
* (no full proof payloads returned) by providing a wallet/provider.
|
|
1476
|
+
*
|
|
1477
|
+
* Prefer this over `checkGate()` for integrations that want the
|
|
1478
|
+
* smallest, most stable surface area (and do NOT need full proof payloads).
|
|
1479
|
+
*
|
|
1480
|
+
* @param {Object} params - Gate check query params
|
|
1481
|
+
* @param {string} params.address - Wallet identity to check (EVM/Solana/DID)
|
|
1482
|
+
* @param {Array<string>|string} [params.verifierIds] - Verifier IDs to match (array or comma-separated)
|
|
1483
|
+
* @param {boolean} [params.requireAll] - Require all verifierIds on a single proof
|
|
1484
|
+
* @param {number} [params.minCount] - Minimum number of matching proofs
|
|
1485
|
+
* @param {number} [params.sinceDays] - Optional time window in days
|
|
1486
|
+
* @param {number} [params.since] - Optional unix timestamp in ms (lower bound)
|
|
1487
|
+
* @param {number} [params.limit] - Max rows to scan (server may clamp)
|
|
1488
|
+
* @param {boolean} [params.includePrivate] - Include private proofs for owner-authenticated requests
|
|
1489
|
+
* @param {boolean} [params.includeQHashes] - Include matched qHashes in response (minimal IDs only)
|
|
1490
|
+
* @param {Object} [params.wallet] - Optional wallet/provider used to sign includePrivate owner checks
|
|
1491
|
+
* @returns {Promise<Object>} API response ({ success, data })
|
|
1492
|
+
*/
|
|
1493
|
+
async gateCheck(params = {}) {
|
|
1494
|
+
const address = (params.address || '').toString();
|
|
1495
|
+
if (!validateUniversalAddress(address, params.chain)) {
|
|
1496
|
+
throw new ValidationError('Valid address is required');
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
// Build query string safely (stringify all values; allow arrays for common fields)
|
|
1500
|
+
const qs = new URLSearchParams();
|
|
1501
|
+
qs.set('address', address);
|
|
1502
|
+
|
|
1503
|
+
const setIfPresent = (key, value) => {
|
|
1504
|
+
if (value === undefined || value === null) return;
|
|
1505
|
+
const str = typeof value === 'string' ? value : String(value);
|
|
1506
|
+
if (str.length === 0) return;
|
|
1507
|
+
qs.set(key, str);
|
|
1508
|
+
};
|
|
1509
|
+
|
|
1510
|
+
const setBoolIfPresent = (key, value) => {
|
|
1511
|
+
if (value === undefined || value === null) return;
|
|
1512
|
+
qs.set(key, value ? 'true' : 'false');
|
|
1513
|
+
};
|
|
1514
|
+
|
|
1515
|
+
const setCsvIfPresent = (key, value) => {
|
|
1516
|
+
if (value === undefined || value === null) return;
|
|
1517
|
+
if (Array.isArray(value)) {
|
|
1518
|
+
const items = value.map(v => String(v).trim()).filter(Boolean);
|
|
1519
|
+
if (items.length) qs.set(key, items.join(','));
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
setIfPresent(key, value);
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
// Common filters
|
|
1526
|
+
setCsvIfPresent('verifierIds', params.verifierIds);
|
|
1527
|
+
setBoolIfPresent('requireAll', params.requireAll);
|
|
1528
|
+
setIfPresent('minCount', params.minCount);
|
|
1529
|
+
setIfPresent('sinceDays', params.sinceDays);
|
|
1530
|
+
setIfPresent('since', params.since);
|
|
1531
|
+
setIfPresent('limit', params.limit);
|
|
1532
|
+
setBoolIfPresent('includePrivate', params.includePrivate);
|
|
1533
|
+
setBoolIfPresent('includeQHashes', params.includeQHashes);
|
|
1534
|
+
|
|
1535
|
+
// Common match filters (optional)
|
|
1536
|
+
setIfPresent('referenceType', params.referenceType);
|
|
1537
|
+
setIfPresent('referenceId', params.referenceId);
|
|
1538
|
+
setIfPresent('tag', params.tag);
|
|
1539
|
+
setCsvIfPresent('tags', params.tags);
|
|
1540
|
+
setIfPresent('contentType', params.contentType);
|
|
1541
|
+
setIfPresent('content', params.content);
|
|
1542
|
+
setIfPresent('contentHash', params.contentHash);
|
|
1543
|
+
|
|
1544
|
+
// Asset/ownership filters
|
|
1545
|
+
setIfPresent('contractAddress', params.contractAddress);
|
|
1546
|
+
setIfPresent('tokenId', params.tokenId);
|
|
1547
|
+
setIfPresent('chainId', params.chainId);
|
|
1548
|
+
setIfPresent('domain', params.domain);
|
|
1549
|
+
setIfPresent('minBalance', params.minBalance);
|
|
1550
|
+
|
|
1551
|
+
// Wallet filters
|
|
1552
|
+
setIfPresent('provider', params.provider);
|
|
1553
|
+
setIfPresent('ownerAddress', params.ownerAddress);
|
|
1554
|
+
setIfPresent('riskLevel', params.riskLevel);
|
|
1555
|
+
setBoolIfPresent('sanctioned', params.sanctioned);
|
|
1556
|
+
setBoolIfPresent('poisoned', params.poisoned);
|
|
1557
|
+
setIfPresent('primaryWalletAddress', params.primaryWalletAddress);
|
|
1558
|
+
setIfPresent('secondaryWalletAddress', params.secondaryWalletAddress);
|
|
1559
|
+
setIfPresent('verificationMethod', params.verificationMethod);
|
|
1560
|
+
|
|
1561
|
+
let headersOverride = null;
|
|
1562
|
+
if (params.includePrivate === true) {
|
|
1563
|
+
const provided = params.privateAuth && typeof params.privateAuth === 'object' ? params.privateAuth : null;
|
|
1564
|
+
let auth = provided;
|
|
1565
|
+
if (!auth) {
|
|
1566
|
+
const walletCandidate = params.wallet || this._getDefaultBrowserWallet();
|
|
1567
|
+
if (walletCandidate) {
|
|
1568
|
+
auth = await this._buildPrivateGateAuth({
|
|
1569
|
+
address,
|
|
1570
|
+
wallet: walletCandidate,
|
|
1571
|
+
chain: params.chain,
|
|
1572
|
+
signatureMethod: params.signatureMethod
|
|
1573
|
+
});
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
if (!auth) {
|
|
1577
|
+
// No signer context available - proceed as public/discoverable gate check.
|
|
1578
|
+
} else {
|
|
1579
|
+
const normalizedAuthWallet = this._normalizeIdentity(auth.walletAddress);
|
|
1580
|
+
const normalizedAddress = this._normalizeIdentity(address);
|
|
1581
|
+
if (!normalizedAuthWallet || normalizedAuthWallet !== normalizedAddress) {
|
|
1582
|
+
throw new ValidationError('privateAuth.walletAddress must match address when includePrivate=true');
|
|
1583
|
+
}
|
|
1584
|
+
const authChain = (typeof auth.chain === 'string' && auth.chain.includes(':')) ? auth.chain.trim() : null;
|
|
1585
|
+
const authSignatureMethod = (typeof auth.signatureMethod === 'string' && auth.signatureMethod.trim())
|
|
1586
|
+
? auth.signatureMethod.trim()
|
|
1587
|
+
: null;
|
|
1588
|
+
headersOverride = {
|
|
1589
|
+
'x-wallet-address': String(auth.walletAddress),
|
|
1590
|
+
'x-signature': String(auth.signature),
|
|
1591
|
+
'x-signed-timestamp': String(auth.signedTimestamp),
|
|
1592
|
+
...(authChain ? { 'x-chain': authChain } : {}),
|
|
1593
|
+
...(authSignatureMethod ? { 'x-signature-method': authSignatureMethod } : {})
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
const response = await this._makeRequest('GET', `/api/v1/proofs/check?${qs.toString()}`, null, headersOverride);
|
|
1599
|
+
if (!response.success) {
|
|
1600
|
+
throw new ApiError(`Gate check failed: ${response.error?.message || 'Unknown error'}`, response.error);
|
|
1601
|
+
}
|
|
1602
|
+
return response;
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
/**
|
|
1606
|
+
* CHECK GATE - Evaluate requirements against existing proofs
|
|
1607
|
+
*
|
|
1608
|
+
* Gate-first verification: checks if wallet has valid proofs satisfying requirements.
|
|
1609
|
+
* Returns which requirements are missing/expired.
|
|
1610
|
+
*
|
|
1611
|
+
* @param {Object} params - Gate check parameters
|
|
1612
|
+
* @param {string} params.walletAddress - Target wallet
|
|
1613
|
+
* @param {Array<Object>} params.requirements - Array of gate requirements
|
|
1614
|
+
* @param {string} params.requirements[].verifierId - Required verifier ID
|
|
1615
|
+
* @param {number} [params.requirements[].maxAgeMs] - Max proof age in ms (TTL)
|
|
1616
|
+
* @param {boolean} [params.requirements[].optional] - If true, not required for gate satisfaction
|
|
1617
|
+
* @param {number} [params.requirements[].minCount] - Minimum proofs needed (default: 1)
|
|
1618
|
+
* @param {Object} [params.requirements[].match] - Verifier data match criteria
|
|
1619
|
+
* Supports nested fields: 'reference.type', 'reference.id', 'content', 'contentHash', 'input.*', 'license.*'
|
|
1620
|
+
* Supports verifier-specific:
|
|
1621
|
+
* - NFT/Token: 'contractAddress', 'tokenId', 'chainId', 'ownerAddress', 'minBalance'
|
|
1622
|
+
* - DNS: 'domain', 'walletAddress'
|
|
1623
|
+
* - Wallet-link: 'primaryWalletAddress', 'secondaryWalletAddress', 'chain', 'signatureMethod'
|
|
1624
|
+
* - Contract-ownership: 'contractAddress', 'chainId', 'owner', 'verificationMethod'
|
|
1625
|
+
* Note: contentHash matching uses approximation in SDK; for exact SHA-256 matching, use backend API
|
|
1626
|
+
* @param {Array} [params.proofs] - Pre-fetched proofs (skip API call)
|
|
1627
|
+
* @returns {Promise<Object>} Gate result with satisfied, missing, existing
|
|
1628
|
+
*
|
|
1629
|
+
* @example
|
|
1630
|
+
* // Basic gate check
|
|
1631
|
+
* const result = await client.checkGate({
|
|
1632
|
+
* walletAddress: '0x...',
|
|
1633
|
+
* requirements: [
|
|
1634
|
+
* { verifierId: 'nft-ownership', match: { contractAddress: '0x...' } }
|
|
1635
|
+
* ]
|
|
1636
|
+
* });
|
|
1637
|
+
*/
|
|
1638
|
+
async checkGate(params) {
|
|
1639
|
+
const { walletAddress, requirements, proofs: preloadedProofs } = params;
|
|
1640
|
+
|
|
1641
|
+
if (!validateUniversalAddress(walletAddress)) {
|
|
1642
|
+
throw new ValidationError('Valid walletAddress is required');
|
|
1643
|
+
}
|
|
1644
|
+
if (!Array.isArray(requirements) || requirements.length === 0) {
|
|
1645
|
+
throw new ValidationError('requirements array is required and must not be empty');
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
// Use preloaded proofs or fetch from API
|
|
1649
|
+
let proofs = preloadedProofs;
|
|
1650
|
+
if (!proofs) {
|
|
1651
|
+
const result = await this.getProofsByWallet(walletAddress, { limit: 100 });
|
|
1652
|
+
proofs = result.proofs;
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
const now = Date.now();
|
|
1656
|
+
const existing = {};
|
|
1657
|
+
const missing = [];
|
|
1658
|
+
|
|
1659
|
+
for (const req of requirements) {
|
|
1660
|
+
const { verifierId, maxAgeMs, optional = false, minCount = 1, match } = req;
|
|
1661
|
+
|
|
1662
|
+
// Find matching proofs for this verifier
|
|
1663
|
+
const matchingProofs = (proofs || []).filter(proof => {
|
|
1664
|
+
// Must have this verifier and be verified
|
|
1665
|
+
const verifiedVerifiers = proof.verifiedVerifiers || [];
|
|
1666
|
+
const verifier = verifiedVerifiers.find(
|
|
1667
|
+
v => v.verifierId === verifierId && v.verified === true
|
|
1668
|
+
);
|
|
1669
|
+
if (!verifier) return false;
|
|
1670
|
+
|
|
1671
|
+
// Check proof is not revoked
|
|
1672
|
+
if (proof.revokedAt) return false;
|
|
1673
|
+
|
|
1674
|
+
// Check TTL if specified
|
|
1675
|
+
if (maxAgeMs) {
|
|
1676
|
+
const proofTimestamp = proof.createdAt || proof.signedTimestamp || 0;
|
|
1677
|
+
const proofAge = now - proofTimestamp;
|
|
1678
|
+
if (proofAge > maxAgeMs) return false;
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
// Check custom match criteria if specified
|
|
1682
|
+
if (match && typeof match === 'object') {
|
|
1683
|
+
const data = verifier.data || {};
|
|
1684
|
+
const input = data.input || {}; // NFT/token verifiers store fields in input
|
|
1685
|
+
|
|
1686
|
+
for (const [key, expected] of Object.entries(match)) {
|
|
1687
|
+
let actualValue = null;
|
|
1688
|
+
|
|
1689
|
+
// Handle nested field access
|
|
1690
|
+
if (key.includes('.')) {
|
|
1691
|
+
const parts = key.split('.');
|
|
1692
|
+
let current = data;
|
|
1693
|
+
|
|
1694
|
+
if (parts[0] === 'input' && input) {
|
|
1695
|
+
current = input;
|
|
1696
|
+
parts.shift();
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
for (const part of parts) {
|
|
1700
|
+
if (current && typeof current === 'object' && part in current) {
|
|
1701
|
+
current = current[part];
|
|
1702
|
+
} else {
|
|
1703
|
+
current = undefined;
|
|
1704
|
+
break;
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
actualValue = current;
|
|
1708
|
+
} else {
|
|
1709
|
+
actualValue = input[key] || data[key];
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
if (key === 'content' && actualValue === undefined) {
|
|
1713
|
+
actualValue = data.reference?.id || data.content;
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
// Special handling for verifier-specific fields
|
|
1717
|
+
if (actualValue === undefined) {
|
|
1718
|
+
if (key === 'contractAddress') {
|
|
1719
|
+
actualValue = input.contractAddress || data.contractAddress;
|
|
1720
|
+
} else if (key === 'tokenId') {
|
|
1721
|
+
actualValue = input.tokenId || data.tokenId;
|
|
1722
|
+
} else if (key === 'chainId') {
|
|
1723
|
+
actualValue = input.chainId || data.chainId;
|
|
1724
|
+
} else if (key === 'ownerAddress') {
|
|
1725
|
+
actualValue = input.ownerAddress || data.owner || data.walletAddress;
|
|
1726
|
+
} else if (key === 'minBalance') {
|
|
1727
|
+
actualValue = input.minBalance || data.onChainData?.requiredMinBalance || data.minBalance;
|
|
1728
|
+
} else if (key === 'primaryWalletAddress') {
|
|
1729
|
+
actualValue = data.primaryWalletAddress;
|
|
1730
|
+
} else if (key === 'secondaryWalletAddress') {
|
|
1731
|
+
actualValue = data.secondaryWalletAddress;
|
|
1732
|
+
} else if (key === 'verificationMethod') {
|
|
1733
|
+
actualValue = data.verificationMethod;
|
|
1734
|
+
} else if (key === 'domain') {
|
|
1735
|
+
actualValue = data.domain;
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
// Content hash check (approximation)
|
|
1740
|
+
if (key === 'contentHash' && actualValue === undefined && data.content) {
|
|
1741
|
+
try {
|
|
1742
|
+
// Simple hash approximation for non-crypto envs
|
|
1743
|
+
let hash = 0;
|
|
1744
|
+
const str = String(data.content);
|
|
1745
|
+
for (let i = 0; i < str.length; i++) {
|
|
1746
|
+
const char = str.charCodeAt(i);
|
|
1747
|
+
hash = ((hash << 5) - hash) + char;
|
|
1748
|
+
hash = hash & hash;
|
|
1749
|
+
}
|
|
1750
|
+
actualValue = '0x' + Math.abs(hash).toString(16).padStart(64, '0').substring(0, 66);
|
|
1751
|
+
} catch {
|
|
1752
|
+
actualValue = String(data.content);
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
let normalizedActual = actualValue;
|
|
1757
|
+
let normalizedExpected = expected;
|
|
1758
|
+
|
|
1759
|
+
if (actualValue === undefined || actualValue === null) {
|
|
1760
|
+
return false;
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
if (key === 'chainId' || (key === 'tokenId' && (typeof actualValue === 'number' || !isNaN(Number(actualValue))))) {
|
|
1764
|
+
normalizedActual = Number(actualValue);
|
|
1765
|
+
normalizedExpected = Number(expected);
|
|
1766
|
+
if (isNaN(normalizedActual) || isNaN(normalizedExpected)) return false;
|
|
1767
|
+
}
|
|
1768
|
+
else if (typeof actualValue === 'string' && (actualValue.startsWith('0x') || actualValue.length > 20)) {
|
|
1769
|
+
normalizedActual = actualValue.toLowerCase();
|
|
1770
|
+
normalizedExpected = typeof expected === 'string' ? String(expected).toLowerCase() : expected;
|
|
1771
|
+
}
|
|
1772
|
+
else {
|
|
1773
|
+
normalizedActual = actualValue;
|
|
1774
|
+
normalizedExpected = expected;
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
if (normalizedActual !== normalizedExpected) {
|
|
1778
|
+
return false;
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
return true;
|
|
1784
|
+
});
|
|
1785
|
+
|
|
1786
|
+
if (matchingProofs.length >= minCount) {
|
|
1787
|
+
const sorted = matchingProofs.sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0));
|
|
1788
|
+
existing[verifierId] = sorted[0];
|
|
1789
|
+
} else if (!optional) {
|
|
1790
|
+
missing.push(req);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
return {
|
|
1795
|
+
satisfied: missing.length === 0,
|
|
1796
|
+
missing,
|
|
1797
|
+
existing,
|
|
1798
|
+
allProofs: proofs
|
|
1799
|
+
};
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
// ============================================================================
|
|
1803
|
+
// PRIVATE UTILITY METHODS
|
|
1804
|
+
// ============================================================================
|
|
1805
|
+
|
|
1806
|
+
/**
|
|
1807
|
+
* Get connected wallet address
|
|
1808
|
+
* @private
|
|
1809
|
+
*/
|
|
1810
|
+
async _getWalletAddress() {
|
|
1811
|
+
if (typeof window === 'undefined' || !window.ethereum) {
|
|
1812
|
+
throw new ConfigurationError('No Web3 wallet detected');
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
const accounts = await window.ethereum.request({ method: 'eth_accounts' });
|
|
1816
|
+
if (!accounts || accounts.length === 0) {
|
|
1817
|
+
throw new ConfigurationError('No wallet accounts available');
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
return accounts[0];
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
/**
|
|
1824
|
+
* Make HTTP request to API
|
|
1825
|
+
* @private
|
|
1826
|
+
*/
|
|
1827
|
+
async _makeRequest(method, endpoint, data = null, headersOverride = null) {
|
|
1828
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
1829
|
+
|
|
1830
|
+
const controller = new AbortController();
|
|
1831
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
1832
|
+
|
|
1833
|
+
const options = {
|
|
1834
|
+
method,
|
|
1835
|
+
headers: { ...this.defaultHeaders, ...(headersOverride || {}) },
|
|
1836
|
+
signal: controller.signal
|
|
1837
|
+
};
|
|
1838
|
+
|
|
1839
|
+
if (data && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
|
|
1840
|
+
options.body = JSON.stringify(data);
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
this._log(`${method} ${endpoint}`, data ? { requestBodyKeys: Object.keys(data) } : {});
|
|
1844
|
+
|
|
1845
|
+
try {
|
|
1846
|
+
const response = await fetch(url, options);
|
|
1847
|
+
clearTimeout(timeoutId);
|
|
1848
|
+
|
|
1849
|
+
let responseData;
|
|
1850
|
+
try {
|
|
1851
|
+
responseData = await response.json();
|
|
1852
|
+
} catch {
|
|
1853
|
+
responseData = { error: { message: 'Invalid JSON response' } };
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
if (!response.ok) {
|
|
1857
|
+
throw ApiError.fromResponse(response, responseData);
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
return responseData;
|
|
1861
|
+
|
|
1862
|
+
} catch (error) {
|
|
1863
|
+
clearTimeout(timeoutId);
|
|
1864
|
+
|
|
1865
|
+
if (error.name === 'AbortError') {
|
|
1866
|
+
throw new NetworkError(`Request timeout after ${this.config.timeout}ms`);
|
|
1867
|
+
}
|
|
1868
|
+
|
|
1869
|
+
if (error instanceof ApiError) {
|
|
1870
|
+
throw error;
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
throw new NetworkError(`Network error: ${error.message}`);
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
/**
|
|
1878
|
+
* Format API response for consistent structure
|
|
1879
|
+
* @private
|
|
1880
|
+
*/
|
|
1881
|
+
_formatResponse(response) {
|
|
1882
|
+
const proofId = response?.data?.proofId ||
|
|
1883
|
+
response?.proofId ||
|
|
1884
|
+
response?.data?.resource?.proofId ||
|
|
1885
|
+
response?.data?.qHash ||
|
|
1886
|
+
response?.qHash ||
|
|
1887
|
+
response?.data?.resource?.qHash ||
|
|
1888
|
+
response?.data?.id;
|
|
1889
|
+
const qHash = response?.data?.qHash ||
|
|
1890
|
+
response?.qHash ||
|
|
1891
|
+
response?.data?.resource?.qHash ||
|
|
1892
|
+
proofId ||
|
|
1893
|
+
response?.data?.id;
|
|
1894
|
+
const finalProofId = proofId || qHash || null;
|
|
1895
|
+
const finalQHash = qHash || proofId || finalProofId;
|
|
1896
|
+
|
|
1897
|
+
const status = response?.data?.status ||
|
|
1898
|
+
response?.status ||
|
|
1899
|
+
response?.data?.resource?.status ||
|
|
1900
|
+
(response?.success ? 'completed' : 'unknown');
|
|
1901
|
+
|
|
1902
|
+
return {
|
|
1903
|
+
success: response.success,
|
|
1904
|
+
proofId: finalProofId,
|
|
1905
|
+
qHash: finalQHash,
|
|
1906
|
+
status,
|
|
1907
|
+
data: response.data,
|
|
1908
|
+
message: response.message,
|
|
1909
|
+
timestamp: Date.now(),
|
|
1910
|
+
statusUrl: finalProofId ? `${this.baseUrl}/api/v1/verification/status/${finalProofId}` : null
|
|
1911
|
+
};
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
/**
|
|
1915
|
+
* Check if status is terminal (completed or failed)
|
|
1916
|
+
* @private
|
|
1917
|
+
*/
|
|
1918
|
+
_isTerminalStatus(status) {
|
|
1919
|
+
const terminalStates = [
|
|
1920
|
+
'verified',
|
|
1921
|
+
'verified_crosschain_propagated',
|
|
1922
|
+
'completed_all_successful',
|
|
1923
|
+
'failed',
|
|
1924
|
+
'error',
|
|
1925
|
+
'rejected',
|
|
1926
|
+
'cancelled'
|
|
1927
|
+
];
|
|
1928
|
+
return typeof status === 'string' && terminalStates.some(state => status.includes(state));
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
/** SDK logging (opt-in via config.enableLogging) */
|
|
1932
|
+
_log(message, data = {}) {
|
|
1933
|
+
if (this.config.enableLogging) {
|
|
1934
|
+
try {
|
|
1935
|
+
const prefix = '[neus/sdk]';
|
|
1936
|
+
if (data && Object.keys(data).length > 0) {
|
|
1937
|
+
// eslint-disable-next-line no-console
|
|
1938
|
+
console.log(prefix, message, data);
|
|
1939
|
+
} else {
|
|
1940
|
+
// eslint-disable-next-line no-console
|
|
1941
|
+
console.log(prefix, message);
|
|
1942
|
+
}
|
|
1943
|
+
} catch {
|
|
1944
|
+
// ignore logging failures
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
// Export the constructVerificationMessage function for advanced use
|
|
1951
|
+
export { constructVerificationMessage };
|