x402z-shared 0.0.15 → 0.0.19

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/dist/index.js CHANGED
@@ -29,9 +29,14 @@ __export(index_exports, {
29
29
  createNonce: () => createNonce,
30
30
  createRelayer: () => createRelayer,
31
31
  decryptEuint64: () => decryptEuint64,
32
+ ensureUnderlyingAllowance: () => ensureUnderlyingAllowance,
33
+ getTokenEip712Domain: () => getTokenEip712Domain,
32
34
  getTransferAmounts: () => getTransferAmounts,
35
+ getUnderlyingTokenAddress: () => getUnderlyingTokenAddress,
36
+ getUnwrapHandleFromLogs: () => getUnwrapHandleFromLogs,
33
37
  hashEncryptedAmountInput: () => hashEncryptedAmountInput,
34
38
  normalizeAmount: () => normalizeAmount,
39
+ prepareUnwrapAuthorization: () => prepareUnwrapAuthorization,
35
40
  publicDecrypt: () => publicDecrypt
36
41
  });
37
42
  module.exports = __toCommonJS(index_exports);
@@ -39,187 +44,1420 @@ module.exports = __toCommonJS(index_exports);
39
44
  // src/core/abi.ts
40
45
  var confidentialTokenAbi = [
41
46
  {
42
- inputs: [
43
- { internalType: "address", name: "to", type: "address" },
44
- { internalType: "bytes32", name: "encryptedAmountInput", type: "bytes32" },
45
- { internalType: "bytes", name: "inputProof", type: "bytes" }
47
+ "inputs": [
48
+ {
49
+ "internalType": "string",
50
+ "name": "name_",
51
+ "type": "string"
52
+ },
53
+ {
54
+ "internalType": "string",
55
+ "name": "symbol_",
56
+ "type": "string"
57
+ },
58
+ {
59
+ "internalType": "string",
60
+ "name": "contractURI_",
61
+ "type": "string"
62
+ },
63
+ {
64
+ "internalType": "contract IERC20",
65
+ "name": "underlying_",
66
+ "type": "address"
67
+ }
68
+ ],
69
+ "stateMutability": "nonpayable",
70
+ "type": "constructor"
71
+ },
72
+ {
73
+ "inputs": [],
74
+ "name": "AuthorizationExpired",
75
+ "type": "error"
76
+ },
77
+ {
78
+ "inputs": [],
79
+ "name": "ECDSAInvalidSignature",
80
+ "type": "error"
81
+ },
82
+ {
83
+ "inputs": [
84
+ {
85
+ "internalType": "uint256",
86
+ "name": "length",
87
+ "type": "uint256"
88
+ }
89
+ ],
90
+ "name": "ECDSAInvalidSignatureLength",
91
+ "type": "error"
92
+ },
93
+ {
94
+ "inputs": [
95
+ {
96
+ "internalType": "bytes32",
97
+ "name": "s",
98
+ "type": "bytes32"
99
+ }
100
+ ],
101
+ "name": "ECDSAInvalidSignatureS",
102
+ "type": "error"
103
+ },
104
+ {
105
+ "inputs": [
106
+ {
107
+ "internalType": "uint256",
108
+ "name": "requestId",
109
+ "type": "uint256"
110
+ }
111
+ ],
112
+ "name": "ERC7984InvalidGatewayRequest",
113
+ "type": "error"
114
+ },
115
+ {
116
+ "inputs": [
117
+ {
118
+ "internalType": "address",
119
+ "name": "receiver",
120
+ "type": "address"
121
+ }
122
+ ],
123
+ "name": "ERC7984InvalidReceiver",
124
+ "type": "error"
125
+ },
126
+ {
127
+ "inputs": [
128
+ {
129
+ "internalType": "address",
130
+ "name": "sender",
131
+ "type": "address"
132
+ }
133
+ ],
134
+ "name": "ERC7984InvalidSender",
135
+ "type": "error"
136
+ },
137
+ {
138
+ "inputs": [],
139
+ "name": "ERC7984TotalSupplyOverflow",
140
+ "type": "error"
141
+ },
142
+ {
143
+ "inputs": [
144
+ {
145
+ "internalType": "address",
146
+ "name": "caller",
147
+ "type": "address"
148
+ }
149
+ ],
150
+ "name": "ERC7984UnauthorizedCaller",
151
+ "type": "error"
152
+ },
153
+ {
154
+ "inputs": [
155
+ {
156
+ "internalType": "address",
157
+ "name": "holder",
158
+ "type": "address"
159
+ },
160
+ {
161
+ "internalType": "address",
162
+ "name": "spender",
163
+ "type": "address"
164
+ }
165
+ ],
166
+ "name": "ERC7984UnauthorizedSpender",
167
+ "type": "error"
168
+ },
169
+ {
170
+ "inputs": [
171
+ {
172
+ "internalType": "euint64",
173
+ "name": "amount",
174
+ "type": "bytes32"
175
+ },
176
+ {
177
+ "internalType": "address",
178
+ "name": "user",
179
+ "type": "address"
180
+ }
181
+ ],
182
+ "name": "ERC7984UnauthorizedUseOfEncryptedAmount",
183
+ "type": "error"
184
+ },
185
+ {
186
+ "inputs": [
187
+ {
188
+ "internalType": "address",
189
+ "name": "holder",
190
+ "type": "address"
191
+ }
192
+ ],
193
+ "name": "ERC7984ZeroBalance",
194
+ "type": "error"
195
+ },
196
+ {
197
+ "inputs": [],
198
+ "name": "EncryptedAmountMismatch",
199
+ "type": "error"
200
+ },
201
+ {
202
+ "inputs": [],
203
+ "name": "InvalidHolder",
204
+ "type": "error"
205
+ },
206
+ {
207
+ "inputs": [],
208
+ "name": "InvalidKMSSignatures",
209
+ "type": "error"
210
+ },
211
+ {
212
+ "inputs": [],
213
+ "name": "InvalidPayee",
214
+ "type": "error"
215
+ },
216
+ {
217
+ "inputs": [],
218
+ "name": "InvalidRecipient",
219
+ "type": "error"
220
+ },
221
+ {
222
+ "inputs": [],
223
+ "name": "InvalidShortString",
224
+ "type": "error"
225
+ },
226
+ {
227
+ "inputs": [],
228
+ "name": "InvalidSignature",
229
+ "type": "error"
230
+ },
231
+ {
232
+ "inputs": [
233
+ {
234
+ "internalType": "euint64",
235
+ "name": "amount",
236
+ "type": "bytes32"
237
+ }
238
+ ],
239
+ "name": "InvalidUnwrapRequest",
240
+ "type": "error"
241
+ },
242
+ {
243
+ "inputs": [],
244
+ "name": "NonceAlreadyUsed",
245
+ "type": "error"
246
+ },
247
+ {
248
+ "inputs": [],
249
+ "name": "NotYetValid",
250
+ "type": "error"
251
+ },
252
+ {
253
+ "inputs": [
254
+ {
255
+ "internalType": "uint8",
256
+ "name": "bits",
257
+ "type": "uint8"
258
+ },
259
+ {
260
+ "internalType": "uint256",
261
+ "name": "value",
262
+ "type": "uint256"
263
+ }
264
+ ],
265
+ "name": "SafeCastOverflowedUintDowncast",
266
+ "type": "error"
267
+ },
268
+ {
269
+ "inputs": [
270
+ {
271
+ "internalType": "address",
272
+ "name": "token",
273
+ "type": "address"
274
+ }
275
+ ],
276
+ "name": "SafeERC20FailedOperation",
277
+ "type": "error"
278
+ },
279
+ {
280
+ "inputs": [
281
+ {
282
+ "internalType": "bytes32",
283
+ "name": "handle",
284
+ "type": "bytes32"
285
+ },
286
+ {
287
+ "internalType": "address",
288
+ "name": "sender",
289
+ "type": "address"
290
+ }
291
+ ],
292
+ "name": "SenderNotAllowedToUseHandle",
293
+ "type": "error"
294
+ },
295
+ {
296
+ "inputs": [
297
+ {
298
+ "internalType": "string",
299
+ "name": "str",
300
+ "type": "string"
301
+ }
302
+ ],
303
+ "name": "StringTooLong",
304
+ "type": "error"
305
+ },
306
+ {
307
+ "inputs": [],
308
+ "name": "Unauthorized",
309
+ "type": "error"
310
+ },
311
+ {
312
+ "inputs": [],
313
+ "name": "ZamaProtocolUnsupported",
314
+ "type": "error"
315
+ },
316
+ {
317
+ "anonymous": false,
318
+ "inputs": [
319
+ {
320
+ "indexed": true,
321
+ "internalType": "euint64",
322
+ "name": "encryptedAmount",
323
+ "type": "bytes32"
324
+ },
325
+ {
326
+ "indexed": true,
327
+ "internalType": "address",
328
+ "name": "requester",
329
+ "type": "address"
330
+ }
331
+ ],
332
+ "name": "AmountDiscloseRequested",
333
+ "type": "event"
334
+ },
335
+ {
336
+ "anonymous": false,
337
+ "inputs": [
338
+ {
339
+ "indexed": true,
340
+ "internalType": "euint64",
341
+ "name": "encryptedAmount",
342
+ "type": "bytes32"
343
+ },
344
+ {
345
+ "indexed": false,
346
+ "internalType": "uint64",
347
+ "name": "amount",
348
+ "type": "uint64"
349
+ }
350
+ ],
351
+ "name": "AmountDisclosed",
352
+ "type": "event"
353
+ },
354
+ {
355
+ "anonymous": false,
356
+ "inputs": [
357
+ {
358
+ "indexed": true,
359
+ "internalType": "address",
360
+ "name": "holder",
361
+ "type": "address"
362
+ },
363
+ {
364
+ "indexed": true,
365
+ "internalType": "address",
366
+ "name": "payee",
367
+ "type": "address"
368
+ },
369
+ {
370
+ "indexed": false,
371
+ "internalType": "uint256",
372
+ "name": "maxClearAmount",
373
+ "type": "uint256"
374
+ },
375
+ {
376
+ "indexed": true,
377
+ "internalType": "bytes32",
378
+ "name": "resourceHash",
379
+ "type": "bytes32"
380
+ },
381
+ {
382
+ "indexed": false,
383
+ "internalType": "bytes32",
384
+ "name": "nonce",
385
+ "type": "bytes32"
386
+ },
387
+ {
388
+ "indexed": false,
389
+ "internalType": "euint64",
390
+ "name": "transferredAmount",
391
+ "type": "bytes32"
392
+ }
393
+ ],
394
+ "name": "ConfidentialPaymentExecuted",
395
+ "type": "event"
396
+ },
397
+ {
398
+ "anonymous": false,
399
+ "inputs": [
400
+ {
401
+ "indexed": true,
402
+ "internalType": "address",
403
+ "name": "from",
404
+ "type": "address"
405
+ },
406
+ {
407
+ "indexed": true,
408
+ "internalType": "address",
409
+ "name": "to",
410
+ "type": "address"
411
+ },
412
+ {
413
+ "indexed": true,
414
+ "internalType": "euint64",
415
+ "name": "amount",
416
+ "type": "bytes32"
417
+ }
418
+ ],
419
+ "name": "ConfidentialTransfer",
420
+ "type": "event"
421
+ },
422
+ {
423
+ "anonymous": false,
424
+ "inputs": [],
425
+ "name": "EIP712DomainChanged",
426
+ "type": "event"
427
+ },
428
+ {
429
+ "anonymous": false,
430
+ "inputs": [
431
+ {
432
+ "indexed": false,
433
+ "internalType": "address",
434
+ "name": "account",
435
+ "type": "address"
436
+ },
437
+ {
438
+ "indexed": false,
439
+ "internalType": "address",
440
+ "name": "oldObserver",
441
+ "type": "address"
442
+ },
443
+ {
444
+ "indexed": false,
445
+ "internalType": "address",
446
+ "name": "newObserver",
447
+ "type": "address"
448
+ }
449
+ ],
450
+ "name": "ERC7984ObserverAccessObserverSet",
451
+ "type": "event"
452
+ },
453
+ {
454
+ "anonymous": false,
455
+ "inputs": [
456
+ {
457
+ "indexed": true,
458
+ "internalType": "address",
459
+ "name": "holder",
460
+ "type": "address"
461
+ },
462
+ {
463
+ "indexed": true,
464
+ "internalType": "address",
465
+ "name": "operator",
466
+ "type": "address"
467
+ },
468
+ {
469
+ "indexed": false,
470
+ "internalType": "uint48",
471
+ "name": "until",
472
+ "type": "uint48"
473
+ }
474
+ ],
475
+ "name": "OperatorSet",
476
+ "type": "event"
477
+ },
478
+ {
479
+ "anonymous": false,
480
+ "inputs": [
481
+ {
482
+ "indexed": false,
483
+ "internalType": "bytes32[]",
484
+ "name": "handlesList",
485
+ "type": "bytes32[]"
486
+ },
487
+ {
488
+ "indexed": false,
489
+ "internalType": "bytes",
490
+ "name": "abiEncodedCleartexts",
491
+ "type": "bytes"
492
+ }
493
+ ],
494
+ "name": "PublicDecryptionVerified",
495
+ "type": "event"
496
+ },
497
+ {
498
+ "anonymous": false,
499
+ "inputs": [
500
+ {
501
+ "indexed": true,
502
+ "internalType": "address",
503
+ "name": "receiver",
504
+ "type": "address"
505
+ },
506
+ {
507
+ "indexed": false,
508
+ "internalType": "euint64",
509
+ "name": "encryptedAmount",
510
+ "type": "bytes32"
511
+ },
512
+ {
513
+ "indexed": false,
514
+ "internalType": "uint64",
515
+ "name": "cleartextAmount",
516
+ "type": "uint64"
517
+ }
518
+ ],
519
+ "name": "UnwrapFinalized",
520
+ "type": "event"
521
+ },
522
+ {
523
+ "anonymous": false,
524
+ "inputs": [
525
+ {
526
+ "indexed": true,
527
+ "internalType": "address",
528
+ "name": "receiver",
529
+ "type": "address"
530
+ },
531
+ {
532
+ "indexed": false,
533
+ "internalType": "euint64",
534
+ "name": "amount",
535
+ "type": "bytes32"
536
+ }
537
+ ],
538
+ "name": "UnwrapRequested",
539
+ "type": "event"
540
+ },
541
+ {
542
+ "anonymous": false,
543
+ "inputs": [
544
+ {
545
+ "indexed": true,
546
+ "internalType": "address",
547
+ "name": "holder",
548
+ "type": "address"
549
+ },
550
+ {
551
+ "indexed": true,
552
+ "internalType": "address",
553
+ "name": "to",
554
+ "type": "address"
555
+ },
556
+ {
557
+ "indexed": false,
558
+ "internalType": "bytes32",
559
+ "name": "nonce",
560
+ "type": "bytes32"
561
+ },
562
+ {
563
+ "indexed": false,
564
+ "internalType": "bytes32",
565
+ "name": "encryptedAmountHandle",
566
+ "type": "bytes32"
567
+ }
568
+ ],
569
+ "name": "UnwrapWithAuthorizationExecuted",
570
+ "type": "event"
571
+ },
572
+ {
573
+ "inputs": [],
574
+ "name": "CONFIDENTIAL_PAYMENT_TYPEHASH",
575
+ "outputs": [
576
+ {
577
+ "internalType": "bytes32",
578
+ "name": "",
579
+ "type": "bytes32"
580
+ }
581
+ ],
582
+ "stateMutability": "view",
583
+ "type": "function"
584
+ },
585
+ {
586
+ "inputs": [],
587
+ "name": "UNWRAP_AUTHORIZATION_TYPEHASH",
588
+ "outputs": [
589
+ {
590
+ "internalType": "bytes32",
591
+ "name": "",
592
+ "type": "bytes32"
593
+ }
594
+ ],
595
+ "stateMutability": "view",
596
+ "type": "function"
597
+ },
598
+ {
599
+ "inputs": [
600
+ {
601
+ "internalType": "address",
602
+ "name": "account",
603
+ "type": "address"
604
+ }
605
+ ],
606
+ "name": "confidentialBalanceOf",
607
+ "outputs": [
608
+ {
609
+ "internalType": "euint64",
610
+ "name": "",
611
+ "type": "bytes32"
612
+ }
613
+ ],
614
+ "stateMutability": "view",
615
+ "type": "function"
616
+ },
617
+ {
618
+ "inputs": [],
619
+ "name": "confidentialProtocolId",
620
+ "outputs": [
621
+ {
622
+ "internalType": "uint256",
623
+ "name": "",
624
+ "type": "uint256"
625
+ }
626
+ ],
627
+ "stateMutability": "view",
628
+ "type": "function"
629
+ },
630
+ {
631
+ "inputs": [],
632
+ "name": "confidentialTotalSupply",
633
+ "outputs": [
634
+ {
635
+ "internalType": "euint64",
636
+ "name": "",
637
+ "type": "bytes32"
638
+ }
639
+ ],
640
+ "stateMutability": "view",
641
+ "type": "function"
642
+ },
643
+ {
644
+ "inputs": [
645
+ {
646
+ "internalType": "address",
647
+ "name": "to",
648
+ "type": "address"
649
+ },
650
+ {
651
+ "internalType": "externalEuint64",
652
+ "name": "encryptedAmount",
653
+ "type": "bytes32"
654
+ },
655
+ {
656
+ "internalType": "bytes",
657
+ "name": "inputProof",
658
+ "type": "bytes"
659
+ }
660
+ ],
661
+ "name": "confidentialTransfer",
662
+ "outputs": [
663
+ {
664
+ "internalType": "euint64",
665
+ "name": "",
666
+ "type": "bytes32"
667
+ }
668
+ ],
669
+ "stateMutability": "nonpayable",
670
+ "type": "function"
671
+ },
672
+ {
673
+ "inputs": [
674
+ {
675
+ "internalType": "address",
676
+ "name": "to",
677
+ "type": "address"
678
+ },
679
+ {
680
+ "internalType": "euint64",
681
+ "name": "amount",
682
+ "type": "bytes32"
683
+ }
684
+ ],
685
+ "name": "confidentialTransfer",
686
+ "outputs": [
687
+ {
688
+ "internalType": "euint64",
689
+ "name": "",
690
+ "type": "bytes32"
691
+ }
692
+ ],
693
+ "stateMutability": "nonpayable",
694
+ "type": "function"
695
+ },
696
+ {
697
+ "inputs": [
698
+ {
699
+ "internalType": "address",
700
+ "name": "to",
701
+ "type": "address"
702
+ },
703
+ {
704
+ "internalType": "euint64",
705
+ "name": "amount",
706
+ "type": "bytes32"
707
+ },
708
+ {
709
+ "internalType": "bytes",
710
+ "name": "data",
711
+ "type": "bytes"
712
+ }
713
+ ],
714
+ "name": "confidentialTransferAndCall",
715
+ "outputs": [
716
+ {
717
+ "internalType": "euint64",
718
+ "name": "transferred",
719
+ "type": "bytes32"
720
+ }
721
+ ],
722
+ "stateMutability": "nonpayable",
723
+ "type": "function"
724
+ },
725
+ {
726
+ "inputs": [
727
+ {
728
+ "internalType": "address",
729
+ "name": "to",
730
+ "type": "address"
731
+ },
732
+ {
733
+ "internalType": "externalEuint64",
734
+ "name": "encryptedAmount",
735
+ "type": "bytes32"
736
+ },
737
+ {
738
+ "internalType": "bytes",
739
+ "name": "inputProof",
740
+ "type": "bytes"
741
+ },
742
+ {
743
+ "internalType": "bytes",
744
+ "name": "data",
745
+ "type": "bytes"
746
+ }
747
+ ],
748
+ "name": "confidentialTransferAndCall",
749
+ "outputs": [
750
+ {
751
+ "internalType": "euint64",
752
+ "name": "transferred",
753
+ "type": "bytes32"
754
+ }
755
+ ],
756
+ "stateMutability": "nonpayable",
757
+ "type": "function"
758
+ },
759
+ {
760
+ "inputs": [
761
+ {
762
+ "internalType": "address",
763
+ "name": "from",
764
+ "type": "address"
765
+ },
766
+ {
767
+ "internalType": "address",
768
+ "name": "to",
769
+ "type": "address"
770
+ },
771
+ {
772
+ "internalType": "externalEuint64",
773
+ "name": "encryptedAmount",
774
+ "type": "bytes32"
775
+ },
776
+ {
777
+ "internalType": "bytes",
778
+ "name": "inputProof",
779
+ "type": "bytes"
780
+ }
781
+ ],
782
+ "name": "confidentialTransferFrom",
783
+ "outputs": [
784
+ {
785
+ "internalType": "euint64",
786
+ "name": "transferred",
787
+ "type": "bytes32"
788
+ }
789
+ ],
790
+ "stateMutability": "nonpayable",
791
+ "type": "function"
792
+ },
793
+ {
794
+ "inputs": [
795
+ {
796
+ "internalType": "address",
797
+ "name": "from",
798
+ "type": "address"
799
+ },
800
+ {
801
+ "internalType": "address",
802
+ "name": "to",
803
+ "type": "address"
804
+ },
805
+ {
806
+ "internalType": "euint64",
807
+ "name": "amount",
808
+ "type": "bytes32"
809
+ }
810
+ ],
811
+ "name": "confidentialTransferFrom",
812
+ "outputs": [
813
+ {
814
+ "internalType": "euint64",
815
+ "name": "transferred",
816
+ "type": "bytes32"
817
+ }
818
+ ],
819
+ "stateMutability": "nonpayable",
820
+ "type": "function"
821
+ },
822
+ {
823
+ "inputs": [
824
+ {
825
+ "internalType": "address",
826
+ "name": "from",
827
+ "type": "address"
828
+ },
829
+ {
830
+ "internalType": "address",
831
+ "name": "to",
832
+ "type": "address"
833
+ },
834
+ {
835
+ "internalType": "externalEuint64",
836
+ "name": "encryptedAmount",
837
+ "type": "bytes32"
838
+ },
839
+ {
840
+ "internalType": "bytes",
841
+ "name": "inputProof",
842
+ "type": "bytes"
843
+ },
844
+ {
845
+ "internalType": "bytes",
846
+ "name": "data",
847
+ "type": "bytes"
848
+ }
849
+ ],
850
+ "name": "confidentialTransferFromAndCall",
851
+ "outputs": [
852
+ {
853
+ "internalType": "euint64",
854
+ "name": "transferred",
855
+ "type": "bytes32"
856
+ }
857
+ ],
858
+ "stateMutability": "nonpayable",
859
+ "type": "function"
860
+ },
861
+ {
862
+ "inputs": [
863
+ {
864
+ "internalType": "address",
865
+ "name": "from",
866
+ "type": "address"
867
+ },
868
+ {
869
+ "internalType": "address",
870
+ "name": "to",
871
+ "type": "address"
872
+ },
873
+ {
874
+ "internalType": "euint64",
875
+ "name": "amount",
876
+ "type": "bytes32"
877
+ },
878
+ {
879
+ "internalType": "bytes",
880
+ "name": "data",
881
+ "type": "bytes"
882
+ }
883
+ ],
884
+ "name": "confidentialTransferFromAndCall",
885
+ "outputs": [
886
+ {
887
+ "internalType": "euint64",
888
+ "name": "transferred",
889
+ "type": "bytes32"
890
+ }
891
+ ],
892
+ "stateMutability": "nonpayable",
893
+ "type": "function"
894
+ },
895
+ {
896
+ "inputs": [
897
+ {
898
+ "components": [
899
+ {
900
+ "internalType": "address",
901
+ "name": "holder",
902
+ "type": "address"
903
+ },
904
+ {
905
+ "internalType": "address",
906
+ "name": "payee",
907
+ "type": "address"
908
+ },
909
+ {
910
+ "internalType": "uint256",
911
+ "name": "maxClearAmount",
912
+ "type": "uint256"
913
+ },
914
+ {
915
+ "internalType": "bytes32",
916
+ "name": "resourceHash",
917
+ "type": "bytes32"
918
+ },
919
+ {
920
+ "internalType": "uint48",
921
+ "name": "validAfter",
922
+ "type": "uint48"
923
+ },
924
+ {
925
+ "internalType": "uint48",
926
+ "name": "validBefore",
927
+ "type": "uint48"
928
+ },
929
+ {
930
+ "internalType": "bytes32",
931
+ "name": "nonce",
932
+ "type": "bytes32"
933
+ },
934
+ {
935
+ "internalType": "bytes32",
936
+ "name": "encryptedAmountHash",
937
+ "type": "bytes32"
938
+ }
939
+ ],
940
+ "internalType": "struct FHEToken.ConfidentialPayment",
941
+ "name": "p",
942
+ "type": "tuple"
943
+ },
944
+ {
945
+ "internalType": "externalEuint64",
946
+ "name": "encryptedAmountInput",
947
+ "type": "bytes32"
948
+ },
949
+ {
950
+ "internalType": "bytes",
951
+ "name": "inputProof",
952
+ "type": "bytes"
953
+ },
954
+ {
955
+ "internalType": "bytes",
956
+ "name": "sig",
957
+ "type": "bytes"
958
+ }
959
+ ],
960
+ "name": "confidentialTransferWithAuthorization",
961
+ "outputs": [
962
+ {
963
+ "internalType": "euint64",
964
+ "name": "transferred",
965
+ "type": "bytes32"
966
+ }
967
+ ],
968
+ "stateMutability": "nonpayable",
969
+ "type": "function"
970
+ },
971
+ {
972
+ "inputs": [],
973
+ "name": "contractURI",
974
+ "outputs": [
975
+ {
976
+ "internalType": "string",
977
+ "name": "",
978
+ "type": "string"
979
+ }
980
+ ],
981
+ "stateMutability": "view",
982
+ "type": "function"
983
+ },
984
+ {
985
+ "inputs": [],
986
+ "name": "decimals",
987
+ "outputs": [
988
+ {
989
+ "internalType": "uint8",
990
+ "name": "",
991
+ "type": "uint8"
992
+ }
993
+ ],
994
+ "stateMutability": "view",
995
+ "type": "function"
996
+ },
997
+ {
998
+ "inputs": [
999
+ {
1000
+ "internalType": "euint64",
1001
+ "name": "encryptedAmount",
1002
+ "type": "bytes32"
1003
+ },
1004
+ {
1005
+ "internalType": "uint64",
1006
+ "name": "cleartextAmount",
1007
+ "type": "uint64"
1008
+ },
1009
+ {
1010
+ "internalType": "bytes",
1011
+ "name": "decryptionProof",
1012
+ "type": "bytes"
1013
+ }
46
1014
  ],
47
- name: "confidentialTransfer",
48
- outputs: [{ internalType: "euint64", name: "transferred", type: "bytes32" }],
49
- stateMutability: "nonpayable",
50
- type: "function"
1015
+ "name": "discloseEncryptedAmount",
1016
+ "outputs": [],
1017
+ "stateMutability": "nonpayable",
1018
+ "type": "function"
51
1019
  },
52
1020
  {
53
- inputs: [
1021
+ "inputs": [],
1022
+ "name": "eip712Domain",
1023
+ "outputs": [
54
1024
  {
55
- components: [
56
- { internalType: "address", name: "holder", type: "address" },
57
- { internalType: "address", name: "payee", type: "address" },
58
- { internalType: "uint256", name: "maxClearAmount", type: "uint256" },
59
- { internalType: "bytes32", name: "resourceHash", type: "bytes32" },
60
- { internalType: "uint48", name: "validAfter", type: "uint48" },
61
- { internalType: "uint48", name: "validBefore", type: "uint48" },
62
- { internalType: "bytes32", name: "nonce", type: "bytes32" },
63
- { internalType: "bytes32", name: "encryptedAmountHash", type: "bytes32" }
64
- ],
65
- internalType: "struct FHEToken.ConfidentialPayment",
66
- name: "p",
67
- type: "tuple"
1025
+ "internalType": "bytes1",
1026
+ "name": "fields",
1027
+ "type": "bytes1"
1028
+ },
1029
+ {
1030
+ "internalType": "string",
1031
+ "name": "name",
1032
+ "type": "string"
1033
+ },
1034
+ {
1035
+ "internalType": "string",
1036
+ "name": "version",
1037
+ "type": "string"
68
1038
  },
69
- { internalType: "externalEuint64", name: "encryptedAmountInput", type: "bytes32" },
70
- { internalType: "bytes", name: "inputProof", type: "bytes" },
71
- { internalType: "bytes", name: "sig", type: "bytes" }
1039
+ {
1040
+ "internalType": "uint256",
1041
+ "name": "chainId",
1042
+ "type": "uint256"
1043
+ },
1044
+ {
1045
+ "internalType": "address",
1046
+ "name": "verifyingContract",
1047
+ "type": "address"
1048
+ },
1049
+ {
1050
+ "internalType": "bytes32",
1051
+ "name": "salt",
1052
+ "type": "bytes32"
1053
+ },
1054
+ {
1055
+ "internalType": "uint256[]",
1056
+ "name": "extensions",
1057
+ "type": "uint256[]"
1058
+ }
72
1059
  ],
73
- name: "confidentialTransferWithAuthorization",
74
- outputs: [{ internalType: "euint64", name: "transferred", type: "bytes32" }],
75
- stateMutability: "nonpayable",
76
- type: "function"
1060
+ "stateMutability": "view",
1061
+ "type": "function"
77
1062
  },
78
1063
  {
79
- inputs: [
80
- { internalType: "address", name: "to", type: "address" },
81
- { internalType: "uint256", name: "amount", type: "uint256" }
1064
+ "inputs": [
1065
+ {
1066
+ "internalType": "euint64",
1067
+ "name": "burntAmount",
1068
+ "type": "bytes32"
1069
+ },
1070
+ {
1071
+ "internalType": "uint64",
1072
+ "name": "burntAmountCleartext",
1073
+ "type": "uint64"
1074
+ },
1075
+ {
1076
+ "internalType": "bytes",
1077
+ "name": "decryptionProof",
1078
+ "type": "bytes"
1079
+ }
82
1080
  ],
83
- name: "wrap",
84
- outputs: [],
85
- stateMutability: "nonpayable",
86
- type: "function"
1081
+ "name": "finalizeUnwrap",
1082
+ "outputs": [],
1083
+ "stateMutability": "nonpayable",
1084
+ "type": "function"
87
1085
  },
88
1086
  {
89
- inputs: [
90
- { internalType: "address", name: "from", type: "address" },
91
- { internalType: "address", name: "to", type: "address" },
92
- { internalType: "bytes32", name: "encryptedAmountInput", type: "bytes32" },
93
- { internalType: "bytes", name: "inputProof", type: "bytes" }
1087
+ "inputs": [],
1088
+ "name": "inferredTotalSupply",
1089
+ "outputs": [
1090
+ {
1091
+ "internalType": "uint256",
1092
+ "name": "",
1093
+ "type": "uint256"
1094
+ }
94
1095
  ],
95
- name: "unwrap",
96
- outputs: [],
97
- stateMutability: "nonpayable",
98
- type: "function"
1096
+ "stateMutability": "view",
1097
+ "type": "function"
99
1098
  },
100
1099
  {
101
- inputs: [
102
- { internalType: "address", name: "from", type: "address" },
103
- { internalType: "address", name: "to", type: "address" },
104
- { internalType: "euint64", name: "amount", type: "bytes32" }
1100
+ "inputs": [
1101
+ {
1102
+ "internalType": "address",
1103
+ "name": "holder",
1104
+ "type": "address"
1105
+ },
1106
+ {
1107
+ "internalType": "address",
1108
+ "name": "spender",
1109
+ "type": "address"
1110
+ }
105
1111
  ],
106
- name: "unwrap",
107
- outputs: [],
108
- stateMutability: "nonpayable",
109
- type: "function"
1112
+ "name": "isOperator",
1113
+ "outputs": [
1114
+ {
1115
+ "internalType": "bool",
1116
+ "name": "",
1117
+ "type": "bool"
1118
+ }
1119
+ ],
1120
+ "stateMutability": "view",
1121
+ "type": "function"
110
1122
  },
111
1123
  {
112
- inputs: [
1124
+ "inputs": [],
1125
+ "name": "maxTotalSupply",
1126
+ "outputs": [
113
1127
  {
114
- components: [
115
- { internalType: "address", name: "holder", type: "address" },
116
- { internalType: "address", name: "to", type: "address" },
117
- { internalType: "uint48", name: "validAfter", type: "uint48" },
118
- { internalType: "uint48", name: "validBefore", type: "uint48" },
119
- { internalType: "bytes32", name: "nonce", type: "bytes32" },
120
- { internalType: "bytes32", name: "encryptedAmountHash", type: "bytes32" }
121
- ],
122
- internalType: "struct FHEToken.UnwrapAuthorization",
123
- name: "p",
124
- type: "tuple"
1128
+ "internalType": "uint256",
1129
+ "name": "",
1130
+ "type": "uint256"
1131
+ }
1132
+ ],
1133
+ "stateMutability": "view",
1134
+ "type": "function"
1135
+ },
1136
+ {
1137
+ "inputs": [],
1138
+ "name": "name",
1139
+ "outputs": [
1140
+ {
1141
+ "internalType": "string",
1142
+ "name": "",
1143
+ "type": "string"
1144
+ }
1145
+ ],
1146
+ "stateMutability": "view",
1147
+ "type": "function"
1148
+ },
1149
+ {
1150
+ "inputs": [
1151
+ {
1152
+ "internalType": "address",
1153
+ "name": "account",
1154
+ "type": "address"
1155
+ }
1156
+ ],
1157
+ "name": "observer",
1158
+ "outputs": [
1159
+ {
1160
+ "internalType": "address",
1161
+ "name": "",
1162
+ "type": "address"
1163
+ }
1164
+ ],
1165
+ "stateMutability": "view",
1166
+ "type": "function"
1167
+ },
1168
+ {
1169
+ "inputs": [
1170
+ {
1171
+ "internalType": "address",
1172
+ "name": "",
1173
+ "type": "address"
1174
+ },
1175
+ {
1176
+ "internalType": "address",
1177
+ "name": "from",
1178
+ "type": "address"
125
1179
  },
126
1180
  {
127
- internalType: "externalEuint64",
128
- name: "encryptedAmountInput",
129
- type: "bytes32"
1181
+ "internalType": "uint256",
1182
+ "name": "amount",
1183
+ "type": "uint256"
130
1184
  },
131
- { internalType: "bytes", name: "inputProof", type: "bytes" },
132
- { internalType: "bytes", name: "sig", type: "bytes" }
1185
+ {
1186
+ "internalType": "bytes",
1187
+ "name": "data",
1188
+ "type": "bytes"
1189
+ }
133
1190
  ],
134
- name: "unwrapWithAuthorization",
135
- outputs: [],
136
- stateMutability: "nonpayable",
137
- type: "function"
1191
+ "name": "onTransferReceived",
1192
+ "outputs": [
1193
+ {
1194
+ "internalType": "bytes4",
1195
+ "name": "",
1196
+ "type": "bytes4"
1197
+ }
1198
+ ],
1199
+ "stateMutability": "nonpayable",
1200
+ "type": "function"
138
1201
  },
139
1202
  {
140
- inputs: [
141
- { internalType: "euint64", name: "burntAmount", type: "bytes32" },
142
- { internalType: "uint64", name: "burntAmountCleartext", type: "uint64" },
143
- { internalType: "bytes", name: "decryptionProof", type: "bytes" }
1203
+ "inputs": [],
1204
+ "name": "rate",
1205
+ "outputs": [
1206
+ {
1207
+ "internalType": "uint256",
1208
+ "name": "",
1209
+ "type": "uint256"
1210
+ }
144
1211
  ],
145
- name: "finalizeUnwrap",
146
- outputs: [],
147
- stateMutability: "nonpayable",
148
- type: "function"
1212
+ "stateMutability": "view",
1213
+ "type": "function"
149
1214
  },
150
1215
  {
151
- inputs: [{ internalType: "address", name: "account", type: "address" }],
152
- name: "confidentialBalanceOf",
153
- outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }],
154
- stateMutability: "view",
155
- type: "function"
1216
+ "inputs": [
1217
+ {
1218
+ "internalType": "euint64",
1219
+ "name": "encryptedAmount",
1220
+ "type": "bytes32"
1221
+ }
1222
+ ],
1223
+ "name": "requestDiscloseEncryptedAmount",
1224
+ "outputs": [],
1225
+ "stateMutability": "nonpayable",
1226
+ "type": "function"
156
1227
  },
157
1228
  {
158
- anonymous: false,
159
- inputs: [
160
- { indexed: true, internalType: "address", name: "holder", type: "address" },
161
- { indexed: true, internalType: "address", name: "payee", type: "address" },
162
- { indexed: false, internalType: "uint256", name: "maxClearAmount", type: "uint256" },
163
- { indexed: true, internalType: "bytes32", name: "resourceHash", type: "bytes32" },
164
- { indexed: false, internalType: "bytes32", name: "nonce", type: "bytes32" },
165
- { indexed: false, internalType: "bytes32", name: "transferredAmount", type: "bytes32" }
1229
+ "inputs": [
1230
+ {
1231
+ "internalType": "address",
1232
+ "name": "account",
1233
+ "type": "address"
1234
+ },
1235
+ {
1236
+ "internalType": "address",
1237
+ "name": "newObserver",
1238
+ "type": "address"
1239
+ }
166
1240
  ],
167
- name: "ConfidentialPaymentExecuted",
168
- type: "event"
1241
+ "name": "setObserver",
1242
+ "outputs": [],
1243
+ "stateMutability": "nonpayable",
1244
+ "type": "function"
169
1245
  },
170
1246
  {
171
- inputs: [
172
- { indexed: true, internalType: "address", name: "receiver", type: "address" },
173
- { indexed: false, internalType: "bytes32", name: "encryptedAmount", type: "bytes32" },
174
- { indexed: false, internalType: "uint64", name: "cleartextAmount", type: "uint64" }
1247
+ "inputs": [
1248
+ {
1249
+ "internalType": "address",
1250
+ "name": "operator",
1251
+ "type": "address"
1252
+ },
1253
+ {
1254
+ "internalType": "uint48",
1255
+ "name": "until",
1256
+ "type": "uint48"
1257
+ }
175
1258
  ],
176
- name: "UnwrapFinalized",
177
- type: "event"
1259
+ "name": "setOperator",
1260
+ "outputs": [],
1261
+ "stateMutability": "nonpayable",
1262
+ "type": "function"
178
1263
  },
179
1264
  {
180
- inputs: [
181
- { indexed: true, internalType: "address", name: "receiver", type: "address" },
182
- { indexed: false, internalType: "bytes32", name: "amount", type: "bytes32" }
1265
+ "inputs": [
1266
+ {
1267
+ "internalType": "bytes4",
1268
+ "name": "interfaceId",
1269
+ "type": "bytes4"
1270
+ }
1271
+ ],
1272
+ "name": "supportsInterface",
1273
+ "outputs": [
1274
+ {
1275
+ "internalType": "bool",
1276
+ "name": "",
1277
+ "type": "bool"
1278
+ }
183
1279
  ],
184
- name: "UnwrapRequested",
185
- type: "event"
1280
+ "stateMutability": "view",
1281
+ "type": "function"
186
1282
  },
187
1283
  {
188
- inputs: [
189
- { indexed: true, internalType: "address", name: "holder", type: "address" },
190
- { indexed: true, internalType: "address", name: "to", type: "address" },
191
- { indexed: false, internalType: "bytes32", name: "nonce", type: "bytes32" },
192
- { indexed: false, internalType: "bytes32", name: "encryptedAmountHandle", type: "bytes32" }
1284
+ "inputs": [],
1285
+ "name": "symbol",
1286
+ "outputs": [
1287
+ {
1288
+ "internalType": "string",
1289
+ "name": "",
1290
+ "type": "string"
1291
+ }
193
1292
  ],
194
- name: "UnwrapWithAuthorizationExecuted",
195
- type: "event"
1293
+ "stateMutability": "view",
1294
+ "type": "function"
196
1295
  },
197
1296
  {
198
- inputs: [
199
- { internalType: "address", name: "", type: "address" },
200
- { internalType: "bytes32", name: "", type: "bytes32" }
1297
+ "inputs": [],
1298
+ "name": "underlying",
1299
+ "outputs": [
1300
+ {
1301
+ "internalType": "contract IERC20",
1302
+ "name": "",
1303
+ "type": "address"
1304
+ }
201
1305
  ],
202
- name: "usedNonces",
203
- outputs: [{ internalType: "bool", name: "", type: "bool" }],
204
- stateMutability: "view",
205
- type: "function"
1306
+ "stateMutability": "view",
1307
+ "type": "function"
206
1308
  },
207
1309
  {
208
- inputs: [{ internalType: "address", name: "account", type: "address" }],
209
- name: "observer",
210
- outputs: [{ internalType: "address", name: "", type: "address" }],
211
- stateMutability: "view",
212
- type: "function"
1310
+ "inputs": [
1311
+ {
1312
+ "internalType": "address",
1313
+ "name": "from",
1314
+ "type": "address"
1315
+ },
1316
+ {
1317
+ "internalType": "address",
1318
+ "name": "to",
1319
+ "type": "address"
1320
+ },
1321
+ {
1322
+ "internalType": "externalEuint64",
1323
+ "name": "encryptedAmount",
1324
+ "type": "bytes32"
1325
+ },
1326
+ {
1327
+ "internalType": "bytes",
1328
+ "name": "inputProof",
1329
+ "type": "bytes"
1330
+ }
1331
+ ],
1332
+ "name": "unwrap",
1333
+ "outputs": [],
1334
+ "stateMutability": "nonpayable",
1335
+ "type": "function"
213
1336
  },
214
1337
  {
215
- inputs: [
216
- { internalType: "address", name: "account", type: "address" },
217
- { internalType: "address", name: "newObserver", type: "address" }
1338
+ "inputs": [
1339
+ {
1340
+ "internalType": "address",
1341
+ "name": "from",
1342
+ "type": "address"
1343
+ },
1344
+ {
1345
+ "internalType": "address",
1346
+ "name": "to",
1347
+ "type": "address"
1348
+ },
1349
+ {
1350
+ "internalType": "euint64",
1351
+ "name": "amount",
1352
+ "type": "bytes32"
1353
+ }
218
1354
  ],
219
- name: "setObserver",
220
- outputs: [],
221
- stateMutability: "nonpayable",
222
- type: "function"
1355
+ "name": "unwrap",
1356
+ "outputs": [],
1357
+ "stateMutability": "nonpayable",
1358
+ "type": "function"
1359
+ },
1360
+ {
1361
+ "inputs": [
1362
+ {
1363
+ "components": [
1364
+ {
1365
+ "internalType": "address",
1366
+ "name": "holder",
1367
+ "type": "address"
1368
+ },
1369
+ {
1370
+ "internalType": "address",
1371
+ "name": "to",
1372
+ "type": "address"
1373
+ },
1374
+ {
1375
+ "internalType": "uint48",
1376
+ "name": "validAfter",
1377
+ "type": "uint48"
1378
+ },
1379
+ {
1380
+ "internalType": "uint48",
1381
+ "name": "validBefore",
1382
+ "type": "uint48"
1383
+ },
1384
+ {
1385
+ "internalType": "bytes32",
1386
+ "name": "nonce",
1387
+ "type": "bytes32"
1388
+ },
1389
+ {
1390
+ "internalType": "bytes32",
1391
+ "name": "encryptedAmountHash",
1392
+ "type": "bytes32"
1393
+ }
1394
+ ],
1395
+ "internalType": "struct FHEToken.UnwrapAuthorization",
1396
+ "name": "p",
1397
+ "type": "tuple"
1398
+ },
1399
+ {
1400
+ "internalType": "externalEuint64",
1401
+ "name": "encryptedAmountInput",
1402
+ "type": "bytes32"
1403
+ },
1404
+ {
1405
+ "internalType": "bytes",
1406
+ "name": "inputProof",
1407
+ "type": "bytes"
1408
+ },
1409
+ {
1410
+ "internalType": "bytes",
1411
+ "name": "sig",
1412
+ "type": "bytes"
1413
+ }
1414
+ ],
1415
+ "name": "unwrapWithAuthorization",
1416
+ "outputs": [],
1417
+ "stateMutability": "nonpayable",
1418
+ "type": "function"
1419
+ },
1420
+ {
1421
+ "inputs": [
1422
+ {
1423
+ "internalType": "address",
1424
+ "name": "",
1425
+ "type": "address"
1426
+ },
1427
+ {
1428
+ "internalType": "bytes32",
1429
+ "name": "",
1430
+ "type": "bytes32"
1431
+ }
1432
+ ],
1433
+ "name": "usedNonces",
1434
+ "outputs": [
1435
+ {
1436
+ "internalType": "bool",
1437
+ "name": "",
1438
+ "type": "bool"
1439
+ }
1440
+ ],
1441
+ "stateMutability": "view",
1442
+ "type": "function"
1443
+ },
1444
+ {
1445
+ "inputs": [
1446
+ {
1447
+ "internalType": "address",
1448
+ "name": "to",
1449
+ "type": "address"
1450
+ },
1451
+ {
1452
+ "internalType": "uint256",
1453
+ "name": "amount",
1454
+ "type": "uint256"
1455
+ }
1456
+ ],
1457
+ "name": "wrap",
1458
+ "outputs": [],
1459
+ "stateMutability": "nonpayable",
1460
+ "type": "function"
223
1461
  }
224
1462
  ];
225
1463
 
@@ -322,33 +1560,277 @@ async function publicDecrypt(relayer, handles) {
322
1560
  };
323
1561
  }
324
1562
 
325
- // src/token/transfer.ts
1563
+ // src/unwrap.ts
326
1564
  var import_viem3 = require("viem");
1565
+ async function getTokenEip712Domain(params) {
1566
+ const tokenAddress = (0, import_viem3.getAddress)(params.tokenAddress);
1567
+ const publicClient = (0, import_viem3.createPublicClient)({ transport: (0, import_viem3.http)(params.rpcUrl) });
1568
+ let name = params.fallbackName;
1569
+ let version = params.fallbackVersion;
1570
+ let chainId;
1571
+ let verifyingContract = tokenAddress;
1572
+ try {
1573
+ const domain = await publicClient.readContract({
1574
+ address: tokenAddress,
1575
+ abi: [
1576
+ {
1577
+ name: "eip712Domain",
1578
+ type: "function",
1579
+ stateMutability: "view",
1580
+ inputs: [],
1581
+ outputs: [
1582
+ { type: "bytes1" },
1583
+ { type: "string" },
1584
+ { type: "string" },
1585
+ { type: "uint256" },
1586
+ { type: "address" },
1587
+ { type: "bytes32" },
1588
+ { type: "uint256[]" }
1589
+ ]
1590
+ }
1591
+ ],
1592
+ functionName: "eip712Domain"
1593
+ });
1594
+ const [, domainName, domainVersion, domainChainId, domainVerifying] = domain;
1595
+ name = domainName || name;
1596
+ version = domainVersion || version;
1597
+ chainId = Number(domainChainId);
1598
+ verifyingContract = domainVerifying || verifyingContract;
1599
+ } catch {
1600
+ }
1601
+ if (!name || !version) {
1602
+ try {
1603
+ name = await publicClient.readContract({
1604
+ address: tokenAddress,
1605
+ abi: [{ name: "name", type: "function", stateMutability: "view", inputs: [], outputs: [{ type: "string" }] }],
1606
+ functionName: "name"
1607
+ });
1608
+ } catch {
1609
+ }
1610
+ try {
1611
+ version = await publicClient.readContract({
1612
+ address: tokenAddress,
1613
+ abi: [{ name: "version", type: "function", stateMutability: "view", inputs: [], outputs: [{ type: "string" }] }],
1614
+ functionName: "version"
1615
+ });
1616
+ } catch {
1617
+ }
1618
+ }
1619
+ if (!chainId) {
1620
+ chainId = await publicClient.getChainId();
1621
+ }
1622
+ if (!name || !version) {
1623
+ throw new Error("Missing EIP-712 name/version for unwrap authorization");
1624
+ }
1625
+ return {
1626
+ name,
1627
+ version,
1628
+ chainId,
1629
+ verifyingContract
1630
+ };
1631
+ }
1632
+ async function prepareUnwrapAuthorization(params) {
1633
+ const tokenAddress = (0, import_viem3.getAddress)(params.tokenAddress);
1634
+ const holderAddress = (0, import_viem3.getAddress)(params.signer.address);
1635
+ const callerAddress = (0, import_viem3.getAddress)(params.callerAddress);
1636
+ const amount = Number(params.amount);
1637
+ if (!Number.isSafeInteger(amount)) {
1638
+ throw new Error("Unwrap amount must be a safe integer");
1639
+ }
1640
+ const now = Math.floor(Date.now() / 1e3);
1641
+ const validAfter = params.validAfter ?? now - 60;
1642
+ const validBefore = params.validBefore ?? now + 300;
1643
+ const nonce = params.nonce ?? createNonce();
1644
+ const { handle, inputProof } = await createEncryptedAmountInput(
1645
+ params.relayer,
1646
+ tokenAddress,
1647
+ callerAddress,
1648
+ amount
1649
+ );
1650
+ const authorization = {
1651
+ holder: holderAddress,
1652
+ to: callerAddress,
1653
+ validAfter,
1654
+ validBefore,
1655
+ nonce,
1656
+ encryptedAmountHash: hashEncryptedAmountInput(handle)
1657
+ };
1658
+ const types = {
1659
+ UnwrapAuthorization: [
1660
+ { name: "holder", type: "address" },
1661
+ { name: "to", type: "address" },
1662
+ { name: "validAfter", type: "uint48" },
1663
+ { name: "validBefore", type: "uint48" },
1664
+ { name: "nonce", type: "bytes32" },
1665
+ { name: "encryptedAmountHash", type: "bytes32" }
1666
+ ]
1667
+ };
1668
+ const message = {
1669
+ holder: authorization.holder,
1670
+ to: authorization.to,
1671
+ validAfter: BigInt(validAfter),
1672
+ validBefore: BigInt(validBefore),
1673
+ nonce: authorization.nonce,
1674
+ encryptedAmountHash: authorization.encryptedAmountHash
1675
+ };
1676
+ const sign = params.signer.signTypedData;
1677
+ const signature = sign.length >= 3 ? await sign(
1678
+ {
1679
+ name: params.domain.name,
1680
+ version: params.domain.version,
1681
+ chainId: params.domain.chainId,
1682
+ verifyingContract: params.domain.verifyingContract
1683
+ },
1684
+ types,
1685
+ message
1686
+ ) : await sign({
1687
+ domain: {
1688
+ name: params.domain.name,
1689
+ version: params.domain.version,
1690
+ chainId: params.domain.chainId,
1691
+ verifyingContract: params.domain.verifyingContract
1692
+ },
1693
+ types,
1694
+ primaryType: "UnwrapAuthorization",
1695
+ message
1696
+ });
1697
+ return {
1698
+ authorization,
1699
+ handle,
1700
+ inputProof,
1701
+ signature
1702
+ };
1703
+ }
1704
+ function getUnwrapHandleFromLogs(params) {
1705
+ const tokenAddress = (0, import_viem3.getAddress)(params.tokenAddress).toLowerCase();
1706
+ for (const logEntry of params.logs) {
1707
+ if (!logEntry.address || logEntry.address.toLowerCase() !== tokenAddress) {
1708
+ continue;
1709
+ }
1710
+ try {
1711
+ if (logEntry.topics.length === 0) {
1712
+ continue;
1713
+ }
1714
+ const topics = [
1715
+ logEntry.topics[0],
1716
+ ...logEntry.topics.slice(1)
1717
+ ];
1718
+ const decoded = (0, import_viem3.decodeEventLog)({
1719
+ abi: confidentialTokenAbi,
1720
+ eventName: "UnwrapRequested",
1721
+ data: logEntry.data,
1722
+ topics
1723
+ });
1724
+ const args = decoded.args;
1725
+ if (args.amount) {
1726
+ return args.amount;
1727
+ }
1728
+ } catch {
1729
+ }
1730
+ }
1731
+ return params.fallbackHandle ?? null;
1732
+ }
1733
+
1734
+ // src/wrap.ts
1735
+ var import_viem4 = require("viem");
1736
+ var fhetokenViewAbi = [
1737
+ {
1738
+ inputs: [],
1739
+ name: "underlying",
1740
+ outputs: [{ internalType: "address", name: "", type: "address" }],
1741
+ stateMutability: "view",
1742
+ type: "function"
1743
+ }
1744
+ ];
1745
+ var erc20Abi = [
1746
+ {
1747
+ inputs: [
1748
+ { internalType: "address", name: "owner", type: "address" },
1749
+ { internalType: "address", name: "spender", type: "address" }
1750
+ ],
1751
+ name: "allowance",
1752
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
1753
+ stateMutability: "view",
1754
+ type: "function"
1755
+ },
1756
+ {
1757
+ inputs: [
1758
+ { internalType: "address", name: "spender", type: "address" },
1759
+ { internalType: "uint256", name: "amount", type: "uint256" }
1760
+ ],
1761
+ name: "approve",
1762
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
1763
+ stateMutability: "nonpayable",
1764
+ type: "function"
1765
+ }
1766
+ ];
1767
+ async function getUnderlyingTokenAddress(params) {
1768
+ const tokenAddress = (0, import_viem4.getAddress)(params.tokenAddress);
1769
+ const publicClient = (0, import_viem4.createPublicClient)({ transport: (0, import_viem4.http)(params.rpcUrl) });
1770
+ return publicClient.readContract({
1771
+ address: tokenAddress,
1772
+ abi: fhetokenViewAbi,
1773
+ functionName: "underlying"
1774
+ });
1775
+ }
1776
+ async function ensureUnderlyingAllowance(params) {
1777
+ const tokenAddress = (0, import_viem4.getAddress)(params.tokenAddress);
1778
+ const owner = (0, import_viem4.getAddress)(params.owner);
1779
+ const spender = (0, import_viem4.getAddress)(params.spender ?? tokenAddress);
1780
+ const publicClient = (0, import_viem4.createPublicClient)({ transport: (0, import_viem4.http)(params.rpcUrl) });
1781
+ const underlying = await publicClient.readContract({
1782
+ address: tokenAddress,
1783
+ abi: fhetokenViewAbi,
1784
+ functionName: "underlying"
1785
+ });
1786
+ const allowance = await publicClient.readContract({
1787
+ address: underlying,
1788
+ abi: erc20Abi,
1789
+ functionName: "allowance",
1790
+ args: [owner, spender]
1791
+ });
1792
+ if (allowance >= params.amount) {
1793
+ return { underlying, allowance, approved: false };
1794
+ }
1795
+ const txHash = await params.writeContract({
1796
+ address: underlying,
1797
+ abi: erc20Abi,
1798
+ functionName: "approve",
1799
+ args: [spender, params.amount]
1800
+ });
1801
+ if (params.waitForReceipt) {
1802
+ await params.waitForReceipt(txHash);
1803
+ }
1804
+ return { underlying, allowance, approved: true, txHash };
1805
+ }
1806
+
1807
+ // src/token/transfer.ts
1808
+ var import_viem5 = require("viem");
327
1809
  async function getTransferAmounts(options) {
328
- if (!(0, import_viem3.isAddress)(options.tokenAddress)) {
1810
+ if (!(0, import_viem5.isAddress)(options.tokenAddress)) {
329
1811
  throw new Error(`Invalid token address: ${options.tokenAddress}`);
330
1812
  }
331
- if (!(0, import_viem3.isHex)(options.txHash, { strict: true })) {
1813
+ if (!(0, import_viem5.isHex)(options.txHash, { strict: true })) {
332
1814
  throw new Error(`Invalid transaction hash: ${options.txHash}`);
333
1815
  }
334
- if (options.from && !(0, import_viem3.isAddress)(options.from)) {
1816
+ if (options.from && !(0, import_viem5.isAddress)(options.from)) {
335
1817
  throw new Error(`Invalid from address: ${options.from}`);
336
1818
  }
337
- if (options.to && !(0, import_viem3.isAddress)(options.to)) {
1819
+ if (options.to && !(0, import_viem5.isAddress)(options.to)) {
338
1820
  throw new Error(`Invalid to address: ${options.to}`);
339
1821
  }
340
- const publicClient = (0, import_viem3.createPublicClient)({ transport: (0, import_viem3.http)(options.rpcUrl) });
1822
+ const publicClient = (0, import_viem5.createPublicClient)({ transport: (0, import_viem5.http)(options.rpcUrl) });
341
1823
  const receipt = await publicClient.getTransactionReceipt({
342
1824
  hash: options.txHash
343
1825
  });
344
- const tokenAddress = (0, import_viem3.getAddress)(options.tokenAddress);
345
- const fromFilter = options.from ? (0, import_viem3.getAddress)(options.from) : void 0;
346
- const toFilter = options.to ? (0, import_viem3.getAddress)(options.to) : void 0;
347
- const logs = receipt.logs.filter((log) => (0, import_viem3.getAddress)(log.address) === tokenAddress);
1826
+ const tokenAddress = (0, import_viem5.getAddress)(options.tokenAddress);
1827
+ const fromFilter = options.from ? (0, import_viem5.getAddress)(options.from) : void 0;
1828
+ const toFilter = options.to ? (0, import_viem5.getAddress)(options.to) : void 0;
1829
+ const logs = receipt.logs.filter((log) => (0, import_viem5.getAddress)(log.address) === tokenAddress);
348
1830
  const transfers = [];
349
1831
  for (const log of logs) {
350
1832
  try {
351
- const decoded = (0, import_viem3.decodeEventLog)({
1833
+ const decoded = (0, import_viem5.decodeEventLog)({
352
1834
  abi: confidentialTokenAbi,
353
1835
  eventName: "ConfidentialPaymentExecuted",
354
1836
  data: log.data,
@@ -356,8 +1838,8 @@ async function getTransferAmounts(options) {
356
1838
  });
357
1839
  const args = decoded.args;
358
1840
  const entry = {
359
- holder: (0, import_viem3.getAddress)(args.holder),
360
- payee: (0, import_viem3.getAddress)(args.payee),
1841
+ holder: (0, import_viem5.getAddress)(args.holder),
1842
+ payee: (0, import_viem5.getAddress)(args.payee),
361
1843
  maxClearAmount: BigInt(args.maxClearAmount),
362
1844
  resourceHash: args.resourceHash,
363
1845
  nonce: args.nonce,
@@ -374,10 +1856,10 @@ async function getTransferAmounts(options) {
374
1856
  if (!options.signer) {
375
1857
  throw new Error("Missing signer for decryption");
376
1858
  }
377
- const signerAddress = (0, import_viem3.getAddress)(options.signer.address);
378
- const holderAddress = (0, import_viem3.getAddress)(entry.holder);
379
- const payeeAddress = (0, import_viem3.getAddress)(entry.payee);
380
- if (!(0, import_viem3.isAddressEqual)(signerAddress, holderAddress) && !(0, import_viem3.isAddressEqual)(signerAddress, payeeAddress)) {
1859
+ const signerAddress = (0, import_viem5.getAddress)(options.signer.address);
1860
+ const holderAddress = (0, import_viem5.getAddress)(entry.holder);
1861
+ const payeeAddress = (0, import_viem5.getAddress)(entry.payee);
1862
+ if (!(0, import_viem5.isAddressEqual)(signerAddress, holderAddress) && !(0, import_viem5.isAddressEqual)(signerAddress, payeeAddress)) {
381
1863
  const [holderObserver, payeeObserver] = await Promise.all([
382
1864
  publicClient.readContract({
383
1865
  address: tokenAddress,
@@ -392,7 +1874,7 @@ async function getTransferAmounts(options) {
392
1874
  args: [payeeAddress]
393
1875
  })
394
1876
  ]);
395
- const allowed = holderObserver && (0, import_viem3.isAddressEqual)(holderObserver, signerAddress) || payeeObserver && (0, import_viem3.isAddressEqual)(payeeObserver, signerAddress);
1877
+ const allowed = holderObserver && (0, import_viem5.isAddressEqual)(holderObserver, signerAddress) || payeeObserver && (0, import_viem5.isAddressEqual)(payeeObserver, signerAddress);
396
1878
  if (!allowed) {
397
1879
  throw new Error(confidentialErrorCodes.observerNotAuthorized);
398
1880
  }
@@ -415,20 +1897,20 @@ async function getTransferAmounts(options) {
415
1897
  }
416
1898
 
417
1899
  // src/token/token.ts
418
- var import_viem6 = require("viem");
1900
+ var import_viem8 = require("viem");
419
1901
 
420
1902
  // src/token/balance.ts
421
- var import_viem4 = require("viem");
1903
+ var import_viem6 = require("viem");
422
1904
  var ZERO_HANDLE = "0x" + "00".repeat(32);
423
1905
  async function readBalance(options) {
424
- if (!(0, import_viem4.isAddress)(options.tokenAddress)) {
1906
+ if (!(0, import_viem6.isAddress)(options.tokenAddress)) {
425
1907
  throw new Error(`Invalid token address: ${options.tokenAddress}`);
426
1908
  }
427
1909
  const account = options.account ?? options.signer?.address;
428
- if (!account || !(0, import_viem4.isAddress)(account)) {
1910
+ if (!account || !(0, import_viem6.isAddress)(account)) {
429
1911
  throw new Error(`Invalid account address: ${account ?? "undefined"}`);
430
1912
  }
431
- const publicClient = (0, import_viem4.createPublicClient)({ transport: (0, import_viem4.http)(options.rpcUrl) });
1913
+ const publicClient = (0, import_viem6.createPublicClient)({ transport: (0, import_viem6.http)(options.rpcUrl) });
432
1914
  const confidentialBalanceAbi = [
433
1915
  {
434
1916
  inputs: [{ internalType: "address", name: "account", type: "address" }],
@@ -451,15 +1933,15 @@ async function readBalance(options) {
451
1933
  if (!options.signer) {
452
1934
  throw new Error("Missing signer for decryption");
453
1935
  }
454
- const signerAddress = (0, import_viem4.getAddress)(options.signer.address);
455
- if (!(0, import_viem4.isAddressEqual)(signerAddress, (0, import_viem4.getAddress)(account))) {
1936
+ const signerAddress = (0, import_viem6.getAddress)(options.signer.address);
1937
+ if (!(0, import_viem6.isAddressEqual)(signerAddress, (0, import_viem6.getAddress)(account))) {
456
1938
  const observer = await publicClient.readContract({
457
1939
  address: options.tokenAddress,
458
1940
  abi: confidentialTokenAbi,
459
1941
  functionName: "observer",
460
1942
  args: [account]
461
1943
  });
462
- if (!observer || !(0, import_viem4.isAddressEqual)(observer, signerAddress)) {
1944
+ if (!observer || !(0, import_viem6.isAddressEqual)(observer, signerAddress)) {
463
1945
  throw new Error(confidentialErrorCodes.observerNotAuthorized);
464
1946
  }
465
1947
  }
@@ -476,28 +1958,28 @@ async function readBalance(options) {
476
1958
  }
477
1959
 
478
1960
  // src/token/observer.ts
479
- var import_viem5 = require("viem");
1961
+ var import_viem7 = require("viem");
480
1962
  async function setObserver(options) {
481
- if (!(0, import_viem5.isAddress)(options.tokenAddress)) {
1963
+ if (!(0, import_viem7.isAddress)(options.tokenAddress)) {
482
1964
  throw new Error(`Invalid token address: ${options.tokenAddress}`);
483
1965
  }
484
- if (!(0, import_viem5.isAddress)(options.account)) {
1966
+ if (!(0, import_viem7.isAddress)(options.account)) {
485
1967
  throw new Error(`Invalid account address: ${options.account}`);
486
1968
  }
487
- if (!(0, import_viem5.isAddress)(options.observer)) {
1969
+ if (!(0, import_viem7.isAddress)(options.observer)) {
488
1970
  throw new Error(`Invalid observer address: ${options.observer}`);
489
1971
  }
490
- const tokenAddress = (0, import_viem5.getAddress)(options.tokenAddress);
491
- const account = (0, import_viem5.getAddress)(options.account);
492
- const observer = (0, import_viem5.getAddress)(options.observer);
493
- const publicClient = (0, import_viem5.createPublicClient)({ transport: (0, import_viem5.http)(options.rpcUrl) });
1972
+ const tokenAddress = (0, import_viem7.getAddress)(options.tokenAddress);
1973
+ const account = (0, import_viem7.getAddress)(options.account);
1974
+ const observer = (0, import_viem7.getAddress)(options.observer);
1975
+ const publicClient = (0, import_viem7.createPublicClient)({ transport: (0, import_viem7.http)(options.rpcUrl) });
494
1976
  const existing = await publicClient.readContract({
495
1977
  address: tokenAddress,
496
1978
  abi: confidentialTokenAbi,
497
1979
  functionName: "observer",
498
1980
  args: [account]
499
1981
  });
500
- if (existing && (0, import_viem5.isAddress)(existing) && (0, import_viem5.getAddress)(existing) === observer) {
1982
+ if (existing && (0, import_viem7.isAddress)(existing) && (0, import_viem7.getAddress)(existing) === observer) {
501
1983
  return "0x" + "00".repeat(32);
502
1984
  }
503
1985
  return options.signer.writeContract({
@@ -510,10 +1992,10 @@ async function setObserver(options) {
510
1992
 
511
1993
  // src/token/token.ts
512
1994
  function assertAddress(value, label) {
513
- if (!(0, import_viem6.isAddress)(value)) {
1995
+ if (!(0, import_viem8.isAddress)(value)) {
514
1996
  throw new Error(`Invalid ${label}: ${value}`);
515
1997
  }
516
- return (0, import_viem6.getAddress)(value);
1998
+ return (0, import_viem8.getAddress)(value);
517
1999
  }
518
2000
  function requireWriteContract(signer) {
519
2001
  if (!signer.writeContract) {
@@ -651,8 +2133,13 @@ var ConfidentialToken = class {
651
2133
  createNonce,
652
2134
  createRelayer,
653
2135
  decryptEuint64,
2136
+ ensureUnderlyingAllowance,
2137
+ getTokenEip712Domain,
654
2138
  getTransferAmounts,
2139
+ getUnderlyingTokenAddress,
2140
+ getUnwrapHandleFromLogs,
655
2141
  hashEncryptedAmountInput,
656
2142
  normalizeAmount,
2143
+ prepareUnwrapAuthorization,
657
2144
  publicDecrypt
658
2145
  });