@lightprotocol/compressed-token 0.17.0 → 0.17.2-alpha.0

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.
@@ -1,2828 +1,2 @@
1
- 'use strict';
2
-
3
- var stateless_js = require('@lightprotocol/stateless.js');
4
- var web3_js = require('@solana/web3.js');
5
- var anchor = require('@coral-xyz/anchor');
6
- var splToken = require('@solana/spl-token');
7
-
8
- const IDL = {
9
- version: '1.2.0',
10
- name: 'light_compressed_token',
11
- instructions: [
12
- {
13
- name: 'createTokenPool',
14
- docs: [
15
- 'This instruction creates a token pool for a given mint. Every spl mint',
16
- 'can have one token pool. When a token is compressed the tokens are',
17
- 'transferrred to the token pool, and their compressed equivalent is',
18
- 'minted into a Merkle tree.',
19
- ],
20
- accounts: [
21
- {
22
- name: 'feePayer',
23
- isMut: true,
24
- isSigner: true,
25
- docs: ['UNCHECKED: only pays fees.'],
26
- },
27
- {
28
- name: 'tokenPoolPda',
29
- isMut: true,
30
- isSigner: false,
31
- },
32
- {
33
- name: 'systemProgram',
34
- isMut: false,
35
- isSigner: false,
36
- },
37
- {
38
- name: 'mint',
39
- isMut: true,
40
- isSigner: false,
41
- },
42
- {
43
- name: 'tokenProgram',
44
- isMut: false,
45
- isSigner: false,
46
- },
47
- {
48
- name: 'cpiAuthorityPda',
49
- isMut: false,
50
- isSigner: false,
51
- },
52
- ],
53
- args: [],
54
- },
55
- {
56
- name: 'mintTo',
57
- docs: [
58
- 'Mints tokens from an spl token mint to a list of compressed accounts.',
59
- 'Minted tokens are transferred to a pool account owned by the compressed',
60
- 'token program. The instruction creates one compressed output account for',
61
- 'every amount and pubkey input pair. A constant amount of lamports can be',
62
- 'transferred to each output account to enable. A use case to add lamports',
63
- 'to a compressed token account is to prevent spam. This is the only way',
64
- 'to add lamports to a compressed token account.',
65
- ],
66
- accounts: [
67
- {
68
- name: 'feePayer',
69
- isMut: true,
70
- isSigner: true,
71
- docs: ['UNCHECKED: only pays fees.'],
72
- },
73
- {
74
- name: 'authority',
75
- isMut: false,
76
- isSigner: true,
77
- },
78
- {
79
- name: 'cpiAuthorityPda',
80
- isMut: false,
81
- isSigner: false,
82
- },
83
- {
84
- name: 'mint',
85
- isMut: true,
86
- isSigner: false,
87
- },
88
- {
89
- name: 'tokenPoolPda',
90
- isMut: true,
91
- isSigner: false,
92
- },
93
- {
94
- name: 'tokenProgram',
95
- isMut: false,
96
- isSigner: false,
97
- },
98
- {
99
- name: 'lightSystemProgram',
100
- isMut: false,
101
- isSigner: false,
102
- },
103
- {
104
- name: 'registeredProgramPda',
105
- isMut: false,
106
- isSigner: false,
107
- },
108
- {
109
- name: 'noopProgram',
110
- isMut: false,
111
- isSigner: false,
112
- docs: ['programs'],
113
- },
114
- {
115
- name: 'accountCompressionAuthority',
116
- isMut: false,
117
- isSigner: false,
118
- },
119
- {
120
- name: 'accountCompressionProgram',
121
- isMut: false,
122
- isSigner: false,
123
- },
124
- {
125
- name: 'merkleTree',
126
- isMut: true,
127
- isSigner: false,
128
- },
129
- {
130
- name: 'selfProgram',
131
- isMut: false,
132
- isSigner: false,
133
- },
134
- {
135
- name: 'systemProgram',
136
- isMut: false,
137
- isSigner: false,
138
- },
139
- {
140
- name: 'solPoolPda',
141
- isMut: true,
142
- isSigner: false,
143
- isOptional: true,
144
- },
145
- ],
146
- args: [
147
- {
148
- name: 'publicKeys',
149
- type: {
150
- vec: 'publicKey',
151
- },
152
- },
153
- {
154
- name: 'amounts',
155
- type: {
156
- vec: 'u64',
157
- },
158
- },
159
- {
160
- name: 'lamports',
161
- type: {
162
- option: 'u64',
163
- },
164
- },
165
- ],
166
- },
167
- {
168
- name: 'compressSplTokenAccount',
169
- docs: [
170
- 'Compresses the balance of an spl token account sub an optional remaining',
171
- 'amount. This instruction does not close the spl token account. To close',
172
- 'the account bundle a close spl account instruction in your transaction.',
173
- ],
174
- accounts: [
175
- {
176
- name: 'feePayer',
177
- isMut: true,
178
- isSigner: true,
179
- docs: ['UNCHECKED: only pays fees.'],
180
- },
181
- {
182
- name: 'authority',
183
- isMut: false,
184
- isSigner: true,
185
- docs: [
186
- 'Authority is verified through proof since both owner and delegate',
187
- 'are included in the token data hash, which is a public input to the',
188
- 'validity proof.',
189
- ],
190
- },
191
- {
192
- name: 'cpiAuthorityPda',
193
- isMut: false,
194
- isSigner: false,
195
- },
196
- {
197
- name: 'lightSystemProgram',
198
- isMut: false,
199
- isSigner: false,
200
- },
201
- {
202
- name: 'registeredProgramPda',
203
- isMut: false,
204
- isSigner: false,
205
- },
206
- {
207
- name: 'noopProgram',
208
- isMut: false,
209
- isSigner: false,
210
- },
211
- {
212
- name: 'accountCompressionAuthority',
213
- isMut: false,
214
- isSigner: false,
215
- },
216
- {
217
- name: 'accountCompressionProgram',
218
- isMut: false,
219
- isSigner: false,
220
- },
221
- {
222
- name: 'selfProgram',
223
- isMut: false,
224
- isSigner: false,
225
- docs: ['this program is the signer of the cpi.'],
226
- },
227
- {
228
- name: 'tokenPoolPda',
229
- isMut: true,
230
- isSigner: false,
231
- isOptional: true,
232
- },
233
- {
234
- name: 'compressOrDecompressTokenAccount',
235
- isMut: true,
236
- isSigner: false,
237
- isOptional: true,
238
- },
239
- {
240
- name: 'tokenProgram',
241
- isMut: false,
242
- isSigner: false,
243
- isOptional: true,
244
- },
245
- {
246
- name: 'systemProgram',
247
- isMut: false,
248
- isSigner: false,
249
- },
250
- ],
251
- args: [
252
- {
253
- name: 'owner',
254
- type: 'publicKey',
255
- },
256
- {
257
- name: 'remainingAmount',
258
- type: {
259
- option: 'u64',
260
- },
261
- },
262
- {
263
- name: 'cpiContext',
264
- type: {
265
- option: {
266
- defined: 'CompressedCpiContext',
267
- },
268
- },
269
- },
270
- ],
271
- },
272
- {
273
- name: 'transfer',
274
- docs: [
275
- 'Transfers compressed tokens from one account to another. All accounts',
276
- 'must be of the same mint. Additional spl tokens can be compressed or',
277
- 'decompressed. In one transaction only compression or decompression is',
278
- 'possible. Lamports can be transferred alongside tokens. If output token',
279
- 'accounts specify less lamports than inputs the remaining lamports are',
280
- 'transferred to an output compressed account. Signer must be owner or',
281
- 'delegate. If a delegated token account is transferred the delegate is',
282
- 'not preserved.',
283
- ],
284
- accounts: [
285
- {
286
- name: 'feePayer',
287
- isMut: true,
288
- isSigner: true,
289
- docs: ['UNCHECKED: only pays fees.'],
290
- },
291
- {
292
- name: 'authority',
293
- isMut: false,
294
- isSigner: true,
295
- docs: [
296
- 'Authority is verified through proof since both owner and delegate',
297
- 'are included in the token data hash, which is a public input to the',
298
- 'validity proof.',
299
- ],
300
- },
301
- {
302
- name: 'cpiAuthorityPda',
303
- isMut: false,
304
- isSigner: false,
305
- },
306
- {
307
- name: 'lightSystemProgram',
308
- isMut: false,
309
- isSigner: false,
310
- },
311
- {
312
- name: 'registeredProgramPda',
313
- isMut: false,
314
- isSigner: false,
315
- },
316
- {
317
- name: 'noopProgram',
318
- isMut: false,
319
- isSigner: false,
320
- },
321
- {
322
- name: 'accountCompressionAuthority',
323
- isMut: false,
324
- isSigner: false,
325
- },
326
- {
327
- name: 'accountCompressionProgram',
328
- isMut: false,
329
- isSigner: false,
330
- },
331
- {
332
- name: 'selfProgram',
333
- isMut: false,
334
- isSigner: false,
335
- docs: ['this program is the signer of the cpi.'],
336
- },
337
- {
338
- name: 'tokenPoolPda',
339
- isMut: true,
340
- isSigner: false,
341
- isOptional: true,
342
- },
343
- {
344
- name: 'compressOrDecompressTokenAccount',
345
- isMut: true,
346
- isSigner: false,
347
- isOptional: true,
348
- },
349
- {
350
- name: 'tokenProgram',
351
- isMut: false,
352
- isSigner: false,
353
- isOptional: true,
354
- },
355
- {
356
- name: 'systemProgram',
357
- isMut: false,
358
- isSigner: false,
359
- },
360
- ],
361
- args: [
362
- {
363
- name: 'inputs',
364
- type: 'bytes',
365
- },
366
- ],
367
- },
368
- {
369
- name: 'approve',
370
- docs: [
371
- 'Delegates an amount to a delegate. A compressed token account is either',
372
- 'completely delegated or not. Prior delegates are not preserved. Cannot',
373
- 'be called by a delegate.',
374
- 'The instruction creates two output accounts:',
375
- '1. one account with delegated amount',
376
- '2. one account with remaining(change) amount',
377
- ],
378
- accounts: [
379
- {
380
- name: 'feePayer',
381
- isMut: true,
382
- isSigner: true,
383
- docs: ['UNCHECKED: only pays fees.'],
384
- },
385
- {
386
- name: 'authority',
387
- isMut: false,
388
- isSigner: true,
389
- docs: [
390
- 'Authority is verified through proof since both owner and delegate',
391
- 'are included in the token data hash, which is a public input to the',
392
- 'validity proof.',
393
- ],
394
- },
395
- {
396
- name: 'cpiAuthorityPda',
397
- isMut: false,
398
- isSigner: false,
399
- },
400
- {
401
- name: 'lightSystemProgram',
402
- isMut: false,
403
- isSigner: false,
404
- },
405
- {
406
- name: 'registeredProgramPda',
407
- isMut: false,
408
- isSigner: false,
409
- },
410
- {
411
- name: 'noopProgram',
412
- isMut: false,
413
- isSigner: false,
414
- },
415
- {
416
- name: 'accountCompressionAuthority',
417
- isMut: false,
418
- isSigner: false,
419
- },
420
- {
421
- name: 'accountCompressionProgram',
422
- isMut: false,
423
- isSigner: false,
424
- },
425
- {
426
- name: 'selfProgram',
427
- isMut: false,
428
- isSigner: false,
429
- docs: ['this program is the signer of the cpi.'],
430
- },
431
- {
432
- name: 'systemProgram',
433
- isMut: false,
434
- isSigner: false,
435
- },
436
- ],
437
- args: [
438
- {
439
- name: 'inputs',
440
- type: 'bytes',
441
- },
442
- ],
443
- },
444
- {
445
- name: 'revoke',
446
- docs: [
447
- 'Revokes a delegation. The instruction merges all inputs into one output',
448
- 'account. Cannot be called by a delegate. Delegates are not preserved.',
449
- ],
450
- accounts: [
451
- {
452
- name: 'feePayer',
453
- isMut: true,
454
- isSigner: true,
455
- docs: ['UNCHECKED: only pays fees.'],
456
- },
457
- {
458
- name: 'authority',
459
- isMut: false,
460
- isSigner: true,
461
- docs: [
462
- 'Authority is verified through proof since both owner and delegate',
463
- 'are included in the token data hash, which is a public input to the',
464
- 'validity proof.',
465
- ],
466
- },
467
- {
468
- name: 'cpiAuthorityPda',
469
- isMut: false,
470
- isSigner: false,
471
- },
472
- {
473
- name: 'lightSystemProgram',
474
- isMut: false,
475
- isSigner: false,
476
- },
477
- {
478
- name: 'registeredProgramPda',
479
- isMut: false,
480
- isSigner: false,
481
- },
482
- {
483
- name: 'noopProgram',
484
- isMut: false,
485
- isSigner: false,
486
- },
487
- {
488
- name: 'accountCompressionAuthority',
489
- isMut: false,
490
- isSigner: false,
491
- },
492
- {
493
- name: 'accountCompressionProgram',
494
- isMut: false,
495
- isSigner: false,
496
- },
497
- {
498
- name: 'selfProgram',
499
- isMut: false,
500
- isSigner: false,
501
- docs: ['this program is the signer of the cpi.'],
502
- },
503
- {
504
- name: 'systemProgram',
505
- isMut: false,
506
- isSigner: false,
507
- },
508
- ],
509
- args: [
510
- {
511
- name: 'inputs',
512
- type: 'bytes',
513
- },
514
- ],
515
- },
516
- {
517
- name: 'freeze',
518
- docs: [
519
- 'Freezes compressed token accounts. Inputs must not be frozen. Creates as',
520
- 'many outputs as inputs. Balances and delegates are preserved.',
521
- ],
522
- accounts: [
523
- {
524
- name: 'feePayer',
525
- isMut: true,
526
- isSigner: true,
527
- docs: ['UNCHECKED: only pays fees.'],
528
- },
529
- {
530
- name: 'authority',
531
- isMut: false,
532
- isSigner: true,
533
- },
534
- {
535
- name: 'cpiAuthorityPda',
536
- isMut: false,
537
- isSigner: false,
538
- },
539
- {
540
- name: 'lightSystemProgram',
541
- isMut: false,
542
- isSigner: false,
543
- },
544
- {
545
- name: 'registeredProgramPda',
546
- isMut: false,
547
- isSigner: false,
548
- },
549
- {
550
- name: 'noopProgram',
551
- isMut: false,
552
- isSigner: false,
553
- },
554
- {
555
- name: 'accountCompressionAuthority',
556
- isMut: false,
557
- isSigner: false,
558
- },
559
- {
560
- name: 'accountCompressionProgram',
561
- isMut: false,
562
- isSigner: false,
563
- },
564
- {
565
- name: 'selfProgram',
566
- isMut: false,
567
- isSigner: false,
568
- docs: ['that this program is the signer of the cpi.'],
569
- },
570
- {
571
- name: 'systemProgram',
572
- isMut: false,
573
- isSigner: false,
574
- },
575
- {
576
- name: 'mint',
577
- isMut: false,
578
- isSigner: false,
579
- },
580
- ],
581
- args: [
582
- {
583
- name: 'inputs',
584
- type: 'bytes',
585
- },
586
- ],
587
- },
588
- {
589
- name: 'thaw',
590
- docs: [
591
- 'Thaws frozen compressed token accounts. Inputs must be frozen. Creates',
592
- 'as many outputs as inputs. Balances and delegates are preserved.',
593
- ],
594
- accounts: [
595
- {
596
- name: 'feePayer',
597
- isMut: true,
598
- isSigner: true,
599
- docs: ['UNCHECKED: only pays fees.'],
600
- },
601
- {
602
- name: 'authority',
603
- isMut: false,
604
- isSigner: true,
605
- },
606
- {
607
- name: 'cpiAuthorityPda',
608
- isMut: false,
609
- isSigner: false,
610
- },
611
- {
612
- name: 'lightSystemProgram',
613
- isMut: false,
614
- isSigner: false,
615
- },
616
- {
617
- name: 'registeredProgramPda',
618
- isMut: false,
619
- isSigner: false,
620
- },
621
- {
622
- name: 'noopProgram',
623
- isMut: false,
624
- isSigner: false,
625
- },
626
- {
627
- name: 'accountCompressionAuthority',
628
- isMut: false,
629
- isSigner: false,
630
- },
631
- {
632
- name: 'accountCompressionProgram',
633
- isMut: false,
634
- isSigner: false,
635
- },
636
- {
637
- name: 'selfProgram',
638
- isMut: false,
639
- isSigner: false,
640
- docs: ['that this program is the signer of the cpi.'],
641
- },
642
- {
643
- name: 'systemProgram',
644
- isMut: false,
645
- isSigner: false,
646
- },
647
- {
648
- name: 'mint',
649
- isMut: false,
650
- isSigner: false,
651
- },
652
- ],
653
- args: [
654
- {
655
- name: 'inputs',
656
- type: 'bytes',
657
- },
658
- ],
659
- },
660
- {
661
- name: 'burn',
662
- docs: [
663
- 'Burns compressed tokens and spl tokens from the pool account. Delegates',
664
- 'can burn tokens. The output compressed token account remains delegated.',
665
- 'Creates one output compressed token account.',
666
- ],
667
- accounts: [
668
- {
669
- name: 'feePayer',
670
- isMut: true,
671
- isSigner: true,
672
- docs: ['UNCHECKED: only pays fees.'],
673
- },
674
- {
675
- name: 'authority',
676
- isMut: false,
677
- isSigner: true,
678
- docs: [
679
- 'Authority is verified through proof since both owner and delegate',
680
- 'are included in the token data hash, which is a public input to the',
681
- 'validity proof.',
682
- ],
683
- },
684
- {
685
- name: 'cpiAuthorityPda',
686
- isMut: false,
687
- isSigner: false,
688
- },
689
- {
690
- name: 'mint',
691
- isMut: true,
692
- isSigner: false,
693
- },
694
- {
695
- name: 'tokenPoolPda',
696
- isMut: true,
697
- isSigner: false,
698
- },
699
- {
700
- name: 'tokenProgram',
701
- isMut: false,
702
- isSigner: false,
703
- },
704
- {
705
- name: 'lightSystemProgram',
706
- isMut: false,
707
- isSigner: false,
708
- },
709
- {
710
- name: 'registeredProgramPda',
711
- isMut: false,
712
- isSigner: false,
713
- },
714
- {
715
- name: 'noopProgram',
716
- isMut: false,
717
- isSigner: false,
718
- },
719
- {
720
- name: 'accountCompressionAuthority',
721
- isMut: false,
722
- isSigner: false,
723
- },
724
- {
725
- name: 'accountCompressionProgram',
726
- isMut: false,
727
- isSigner: false,
728
- },
729
- {
730
- name: 'selfProgram',
731
- isMut: false,
732
- isSigner: false,
733
- },
734
- {
735
- name: 'systemProgram',
736
- isMut: false,
737
- isSigner: false,
738
- },
739
- ],
740
- args: [
741
- {
742
- name: 'inputs',
743
- type: 'bytes',
744
- },
745
- ],
746
- },
747
- {
748
- name: 'stubIdlBuild',
749
- docs: [
750
- 'This function is a stub to allow Anchor to include the input types in',
751
- 'the IDL. It should not be included in production builds nor be called in',
752
- 'practice.',
753
- ],
754
- accounts: [
755
- {
756
- name: 'feePayer',
757
- isMut: true,
758
- isSigner: true,
759
- docs: ['UNCHECKED: only pays fees.'],
760
- },
761
- {
762
- name: 'authority',
763
- isMut: false,
764
- isSigner: true,
765
- docs: [
766
- 'Authority is verified through proof since both owner and delegate',
767
- 'are included in the token data hash, which is a public input to the',
768
- 'validity proof.',
769
- ],
770
- },
771
- {
772
- name: 'cpiAuthorityPda',
773
- isMut: false,
774
- isSigner: false,
775
- },
776
- {
777
- name: 'lightSystemProgram',
778
- isMut: false,
779
- isSigner: false,
780
- },
781
- {
782
- name: 'registeredProgramPda',
783
- isMut: false,
784
- isSigner: false,
785
- },
786
- {
787
- name: 'noopProgram',
788
- isMut: false,
789
- isSigner: false,
790
- },
791
- {
792
- name: 'accountCompressionAuthority',
793
- isMut: false,
794
- isSigner: false,
795
- },
796
- {
797
- name: 'accountCompressionProgram',
798
- isMut: false,
799
- isSigner: false,
800
- },
801
- {
802
- name: 'selfProgram',
803
- isMut: false,
804
- isSigner: false,
805
- docs: ['this program is the signer of the cpi.'],
806
- },
807
- {
808
- name: 'tokenPoolPda',
809
- isMut: true,
810
- isSigner: false,
811
- isOptional: true,
812
- },
813
- {
814
- name: 'compressOrDecompressTokenAccount',
815
- isMut: true,
816
- isSigner: false,
817
- isOptional: true,
818
- },
819
- {
820
- name: 'tokenProgram',
821
- isMut: false,
822
- isSigner: false,
823
- isOptional: true,
824
- },
825
- {
826
- name: 'systemProgram',
827
- isMut: false,
828
- isSigner: false,
829
- },
830
- ],
831
- args: [
832
- {
833
- name: 'inputs1',
834
- type: {
835
- defined: 'CompressedTokenInstructionDataTransfer',
836
- },
837
- },
838
- {
839
- name: 'inputs2',
840
- type: {
841
- defined: 'TokenData',
842
- },
843
- },
844
- ],
845
- },
846
- ],
847
- types: [
848
- {
849
- name: 'AccessMetadata',
850
- type: {
851
- kind: 'struct',
852
- fields: [
853
- {
854
- name: 'owner',
855
- docs: ['Owner of the Merkle tree.'],
856
- type: 'publicKey',
857
- },
858
- {
859
- name: 'programOwner',
860
- docs: [
861
- 'Program owner of the Merkle tree. This will be used for program owned Merkle trees.',
862
- ],
863
- type: 'publicKey',
864
- },
865
- {
866
- name: 'forester',
867
- docs: [
868
- 'Optional privileged forester pubkey, can be set for custom Merkle trees',
869
- 'without a network fee. Merkle trees without network fees are not',
870
- 'forested by light foresters. The variable is not used in the account',
871
- 'compression program but the registry program. The registry program',
872
- 'implements access control to prevent contention during forester. The',
873
- 'forester pubkey specified in this struct can bypass contention checks.',
874
- ],
875
- type: 'publicKey',
876
- },
877
- ],
878
- },
879
- },
880
- {
881
- name: 'AccountState',
882
- type: {
883
- kind: 'enum',
884
- variants: [
885
- {
886
- name: 'Initialized',
887
- },
888
- {
889
- name: 'Frozen',
890
- },
891
- ],
892
- },
893
- },
894
- {
895
- name: 'CompressedAccount',
896
- type: {
897
- kind: 'struct',
898
- fields: [
899
- {
900
- name: 'owner',
901
- type: 'publicKey',
902
- },
903
- {
904
- name: 'lamports',
905
- type: 'u64',
906
- },
907
- {
908
- name: 'address',
909
- type: {
910
- option: {
911
- array: ['u8', 32],
912
- },
913
- },
914
- },
915
- {
916
- name: 'data',
917
- type: {
918
- option: {
919
- defined: 'CompressedAccountData',
920
- },
921
- },
922
- },
923
- ],
924
- },
925
- },
926
- {
927
- name: 'CompressedAccountData',
928
- type: {
929
- kind: 'struct',
930
- fields: [
931
- {
932
- name: 'discriminator',
933
- type: {
934
- array: ['u8', 8],
935
- },
936
- },
937
- {
938
- name: 'data',
939
- type: 'bytes',
940
- },
941
- {
942
- name: 'dataHash',
943
- type: {
944
- array: ['u8', 32],
945
- },
946
- },
947
- ],
948
- },
949
- },
950
- {
951
- name: 'CompressedCpiContext',
952
- type: {
953
- kind: 'struct',
954
- fields: [
955
- {
956
- name: 'setContext',
957
- docs: [
958
- 'Is set by the program that is invoking the CPI to signal that is should',
959
- 'set the cpi context.',
960
- ],
961
- type: 'bool',
962
- },
963
- {
964
- name: 'firstSetContext',
965
- docs: [
966
- 'Is set to wipe the cpi context since someone could have set it before',
967
- 'with unrelated data.',
968
- ],
969
- type: 'bool',
970
- },
971
- {
972
- name: 'cpiContextAccountIndex',
973
- docs: [
974
- 'Index of cpi context account in remaining accounts.',
975
- ],
976
- type: 'u8',
977
- },
978
- ],
979
- },
980
- },
981
- {
982
- name: 'CompressedProof',
983
- type: {
984
- kind: 'struct',
985
- fields: [
986
- {
987
- name: 'a',
988
- type: {
989
- array: ['u8', 32],
990
- },
991
- },
992
- {
993
- name: 'b',
994
- type: {
995
- array: ['u8', 64],
996
- },
997
- },
998
- {
999
- name: 'c',
1000
- type: {
1001
- array: ['u8', 32],
1002
- },
1003
- },
1004
- ],
1005
- },
1006
- },
1007
- {
1008
- name: 'CompressedTokenInstructionDataTransfer',
1009
- type: {
1010
- kind: 'struct',
1011
- fields: [
1012
- {
1013
- name: 'proof',
1014
- type: {
1015
- option: {
1016
- defined: 'CompressedProof',
1017
- },
1018
- },
1019
- },
1020
- {
1021
- name: 'mint',
1022
- type: 'publicKey',
1023
- },
1024
- {
1025
- name: 'delegatedTransfer',
1026
- docs: [
1027
- 'Is required if the signer is delegate,',
1028
- '-> delegate is authority account,',
1029
- 'owner = Some(owner) is the owner of the token account.',
1030
- ],
1031
- type: {
1032
- option: {
1033
- defined: 'DelegatedTransfer',
1034
- },
1035
- },
1036
- },
1037
- {
1038
- name: 'inputTokenDataWithContext',
1039
- type: {
1040
- vec: {
1041
- defined: 'InputTokenDataWithContext',
1042
- },
1043
- },
1044
- },
1045
- {
1046
- name: 'outputCompressedAccounts',
1047
- type: {
1048
- vec: {
1049
- defined: 'PackedTokenTransferOutputData',
1050
- },
1051
- },
1052
- },
1053
- {
1054
- name: 'isCompress',
1055
- type: 'bool',
1056
- },
1057
- {
1058
- name: 'compressOrDecompressAmount',
1059
- type: {
1060
- option: 'u64',
1061
- },
1062
- },
1063
- {
1064
- name: 'cpiContext',
1065
- type: {
1066
- option: {
1067
- defined: 'CompressedCpiContext',
1068
- },
1069
- },
1070
- },
1071
- {
1072
- name: 'lamportsChangeAccountMerkleTreeIndex',
1073
- type: {
1074
- option: 'u8',
1075
- },
1076
- },
1077
- ],
1078
- },
1079
- },
1080
- {
1081
- name: 'DelegatedTransfer',
1082
- docs: [
1083
- 'Struct to provide the owner when the delegate is signer of the transaction.',
1084
- ],
1085
- type: {
1086
- kind: 'struct',
1087
- fields: [
1088
- {
1089
- name: 'owner',
1090
- type: 'publicKey',
1091
- },
1092
- {
1093
- name: 'delegateChangeAccountIndex',
1094
- docs: [
1095
- 'Index of change compressed account in output compressed accounts. In',
1096
- "case that the delegate didn't spend the complete delegated compressed",
1097
- 'account balance the change compressed account will be delegated to her',
1098
- 'as well.',
1099
- ],
1100
- type: {
1101
- option: 'u8',
1102
- },
1103
- },
1104
- ],
1105
- },
1106
- },
1107
- {
1108
- name: 'InputTokenDataWithContext',
1109
- type: {
1110
- kind: 'struct',
1111
- fields: [
1112
- {
1113
- name: 'amount',
1114
- type: 'u64',
1115
- },
1116
- {
1117
- name: 'delegateIndex',
1118
- type: {
1119
- option: 'u8',
1120
- },
1121
- },
1122
- {
1123
- name: 'merkleContext',
1124
- type: {
1125
- defined: 'PackedMerkleContext',
1126
- },
1127
- },
1128
- {
1129
- name: 'rootIndex',
1130
- type: 'u16',
1131
- },
1132
- {
1133
- name: 'lamports',
1134
- type: {
1135
- option: 'u64',
1136
- },
1137
- },
1138
- {
1139
- name: 'tlv',
1140
- docs: [
1141
- 'Placeholder for TokenExtension tlv data (unimplemented)',
1142
- ],
1143
- type: {
1144
- option: 'bytes',
1145
- },
1146
- },
1147
- ],
1148
- },
1149
- },
1150
- {
1151
- name: 'InstructionDataInvoke',
1152
- type: {
1153
- kind: 'struct',
1154
- fields: [
1155
- {
1156
- name: 'proof',
1157
- type: {
1158
- option: {
1159
- defined: 'CompressedProof',
1160
- },
1161
- },
1162
- },
1163
- {
1164
- name: 'inputCompressedAccountsWithMerkleContext',
1165
- type: {
1166
- vec: {
1167
- defined: 'PackedCompressedAccountWithMerkleContext',
1168
- },
1169
- },
1170
- },
1171
- {
1172
- name: 'outputCompressedAccounts',
1173
- type: {
1174
- vec: {
1175
- defined: 'OutputCompressedAccountWithPackedContext',
1176
- },
1177
- },
1178
- },
1179
- {
1180
- name: 'relayFee',
1181
- type: {
1182
- option: 'u64',
1183
- },
1184
- },
1185
- {
1186
- name: 'newAddressParams',
1187
- type: {
1188
- vec: {
1189
- defined: 'NewAddressParamsPacked',
1190
- },
1191
- },
1192
- },
1193
- {
1194
- name: 'compressOrDecompressLamports',
1195
- type: {
1196
- option: 'u64',
1197
- },
1198
- },
1199
- {
1200
- name: 'isCompress',
1201
- type: 'bool',
1202
- },
1203
- ],
1204
- },
1205
- },
1206
- {
1207
- name: 'InstructionDataInvokeCpi',
1208
- type: {
1209
- kind: 'struct',
1210
- fields: [
1211
- {
1212
- name: 'proof',
1213
- type: {
1214
- option: {
1215
- defined: 'CompressedProof',
1216
- },
1217
- },
1218
- },
1219
- {
1220
- name: 'newAddressParams',
1221
- type: {
1222
- vec: {
1223
- defined: 'NewAddressParamsPacked',
1224
- },
1225
- },
1226
- },
1227
- {
1228
- name: 'inputCompressedAccountsWithMerkleContext',
1229
- type: {
1230
- vec: {
1231
- defined: 'PackedCompressedAccountWithMerkleContext',
1232
- },
1233
- },
1234
- },
1235
- {
1236
- name: 'outputCompressedAccounts',
1237
- type: {
1238
- vec: {
1239
- defined: 'OutputCompressedAccountWithPackedContext',
1240
- },
1241
- },
1242
- },
1243
- {
1244
- name: 'relayFee',
1245
- type: {
1246
- option: 'u64',
1247
- },
1248
- },
1249
- {
1250
- name: 'compressOrDecompressLamports',
1251
- type: {
1252
- option: 'u64',
1253
- },
1254
- },
1255
- {
1256
- name: 'isCompress',
1257
- type: 'bool',
1258
- },
1259
- {
1260
- name: 'cpiContext',
1261
- type: {
1262
- option: {
1263
- defined: 'CompressedCpiContext',
1264
- },
1265
- },
1266
- },
1267
- ],
1268
- },
1269
- },
1270
- {
1271
- name: 'MerkleTreeMetadata',
1272
- type: {
1273
- kind: 'struct',
1274
- fields: [
1275
- {
1276
- name: 'accessMetadata',
1277
- type: {
1278
- defined: 'AccessMetadata',
1279
- },
1280
- },
1281
- {
1282
- name: 'rolloverMetadata',
1283
- type: {
1284
- defined: 'RolloverMetadata',
1285
- },
1286
- },
1287
- {
1288
- name: 'associatedQueue',
1289
- type: 'publicKey',
1290
- },
1291
- {
1292
- name: 'nextMerkleTree',
1293
- type: 'publicKey',
1294
- },
1295
- ],
1296
- },
1297
- },
1298
- {
1299
- name: 'MerkleTreeSequenceNumber',
1300
- type: {
1301
- kind: 'struct',
1302
- fields: [
1303
- {
1304
- name: 'pubkey',
1305
- type: 'publicKey',
1306
- },
1307
- {
1308
- name: 'seq',
1309
- type: 'u64',
1310
- },
1311
- ],
1312
- },
1313
- },
1314
- {
1315
- name: 'NewAddressParamsPacked',
1316
- type: {
1317
- kind: 'struct',
1318
- fields: [
1319
- {
1320
- name: 'seed',
1321
- type: {
1322
- array: ['u8', 32],
1323
- },
1324
- },
1325
- {
1326
- name: 'addressQueueAccountIndex',
1327
- type: 'u8',
1328
- },
1329
- {
1330
- name: 'addressMerkleTreeAccountIndex',
1331
- type: 'u8',
1332
- },
1333
- {
1334
- name: 'addressMerkleTreeRootIndex',
1335
- type: 'u16',
1336
- },
1337
- ],
1338
- },
1339
- },
1340
- {
1341
- name: 'OutputCompressedAccountWithPackedContext',
1342
- type: {
1343
- kind: 'struct',
1344
- fields: [
1345
- {
1346
- name: 'compressedAccount',
1347
- type: {
1348
- defined: 'CompressedAccount',
1349
- },
1350
- },
1351
- {
1352
- name: 'merkleTreeIndex',
1353
- type: 'u8',
1354
- },
1355
- ],
1356
- },
1357
- },
1358
- {
1359
- name: 'PackedCompressedAccountWithMerkleContext',
1360
- type: {
1361
- kind: 'struct',
1362
- fields: [
1363
- {
1364
- name: 'compressedAccount',
1365
- type: {
1366
- defined: 'CompressedAccount',
1367
- },
1368
- },
1369
- {
1370
- name: 'merkleContext',
1371
- type: {
1372
- defined: 'PackedMerkleContext',
1373
- },
1374
- },
1375
- {
1376
- name: 'rootIndex',
1377
- docs: [
1378
- 'Index of root used in inclusion validity proof.',
1379
- ],
1380
- type: 'u16',
1381
- },
1382
- {
1383
- name: 'readOnly',
1384
- docs: [
1385
- 'Placeholder to mark accounts read-only unimplemented set to false.',
1386
- ],
1387
- type: 'bool',
1388
- },
1389
- ],
1390
- },
1391
- },
1392
- {
1393
- name: 'PackedMerkleContext',
1394
- type: {
1395
- kind: 'struct',
1396
- fields: [
1397
- {
1398
- name: 'merkleTreePubkeyIndex',
1399
- type: 'u8',
1400
- },
1401
- {
1402
- name: 'nullifierQueuePubkeyIndex',
1403
- type: 'u8',
1404
- },
1405
- {
1406
- name: 'leafIndex',
1407
- type: 'u32',
1408
- },
1409
- {
1410
- name: 'queueIndex',
1411
- docs: [
1412
- 'Index of leaf in queue. Placeholder of batched Merkle tree updates',
1413
- 'currently unimplemented.',
1414
- ],
1415
- type: {
1416
- option: {
1417
- defined: 'QueueIndex',
1418
- },
1419
- },
1420
- },
1421
- ],
1422
- },
1423
- },
1424
- {
1425
- name: 'PackedTokenTransferOutputData',
1426
- type: {
1427
- kind: 'struct',
1428
- fields: [
1429
- {
1430
- name: 'owner',
1431
- type: 'publicKey',
1432
- },
1433
- {
1434
- name: 'amount',
1435
- type: 'u64',
1436
- },
1437
- {
1438
- name: 'lamports',
1439
- type: {
1440
- option: 'u64',
1441
- },
1442
- },
1443
- {
1444
- name: 'merkleTreeIndex',
1445
- type: 'u8',
1446
- },
1447
- {
1448
- name: 'tlv',
1449
- docs: [
1450
- 'Placeholder for TokenExtension tlv data (unimplemented)',
1451
- ],
1452
- type: {
1453
- option: 'bytes',
1454
- },
1455
- },
1456
- ],
1457
- },
1458
- },
1459
- {
1460
- name: 'PublicTransactionEvent',
1461
- type: {
1462
- kind: 'struct',
1463
- fields: [
1464
- {
1465
- name: 'inputCompressedAccountHashes',
1466
- type: {
1467
- vec: {
1468
- array: ['u8', 32],
1469
- },
1470
- },
1471
- },
1472
- {
1473
- name: 'outputCompressedAccountHashes',
1474
- type: {
1475
- vec: {
1476
- array: ['u8', 32],
1477
- },
1478
- },
1479
- },
1480
- {
1481
- name: 'outputCompressedAccounts',
1482
- type: {
1483
- vec: {
1484
- defined: 'OutputCompressedAccountWithPackedContext',
1485
- },
1486
- },
1487
- },
1488
- {
1489
- name: 'outputLeafIndices',
1490
- type: {
1491
- vec: 'u32',
1492
- },
1493
- },
1494
- {
1495
- name: 'sequenceNumbers',
1496
- type: {
1497
- vec: {
1498
- defined: 'MerkleTreeSequenceNumber',
1499
- },
1500
- },
1501
- },
1502
- {
1503
- name: 'relayFee',
1504
- type: {
1505
- option: 'u64',
1506
- },
1507
- },
1508
- {
1509
- name: 'isCompress',
1510
- type: 'bool',
1511
- },
1512
- {
1513
- name: 'compressOrDecompressLamports',
1514
- type: {
1515
- option: 'u64',
1516
- },
1517
- },
1518
- {
1519
- name: 'pubkeyArray',
1520
- type: {
1521
- vec: 'publicKey',
1522
- },
1523
- },
1524
- {
1525
- name: 'message',
1526
- type: {
1527
- option: 'bytes',
1528
- },
1529
- },
1530
- ],
1531
- },
1532
- },
1533
- {
1534
- name: 'QueueIndex',
1535
- type: {
1536
- kind: 'struct',
1537
- fields: [
1538
- {
1539
- name: 'queueId',
1540
- docs: ['Id of queue in queue account.'],
1541
- type: 'u8',
1542
- },
1543
- {
1544
- name: 'index',
1545
- docs: ['Index of compressed account hash in queue.'],
1546
- type: 'u16',
1547
- },
1548
- ],
1549
- },
1550
- },
1551
- {
1552
- name: 'RolloverMetadata',
1553
- type: {
1554
- kind: 'struct',
1555
- fields: [
1556
- {
1557
- name: 'index',
1558
- docs: ['Unique index.'],
1559
- type: 'u64',
1560
- },
1561
- {
1562
- name: 'rolloverFee',
1563
- docs: [
1564
- 'This fee is used for rent for the next account.',
1565
- 'It accumulates in the account so that once the corresponding Merkle tree account is full it can be rolled over',
1566
- ],
1567
- type: 'u64',
1568
- },
1569
- {
1570
- name: 'rolloverThreshold',
1571
- docs: [
1572
- 'The threshold in percentage points when the account should be rolled over (95 corresponds to 95% filled).',
1573
- ],
1574
- type: 'u64',
1575
- },
1576
- {
1577
- name: 'networkFee',
1578
- docs: ['Tip for maintaining the account.'],
1579
- type: 'u64',
1580
- },
1581
- {
1582
- name: 'rolledoverSlot',
1583
- docs: [
1584
- 'The slot when the account was rolled over, a rolled over account should not be written to.',
1585
- ],
1586
- type: 'u64',
1587
- },
1588
- {
1589
- name: 'closeThreshold',
1590
- docs: [
1591
- 'If current slot is greater than rolledover_slot + close_threshold and',
1592
- "the account is empty it can be closed. No 'close' functionality has been",
1593
- 'implemented yet.',
1594
- ],
1595
- type: 'u64',
1596
- },
1597
- {
1598
- name: 'additionalBytes',
1599
- docs: [
1600
- 'Placeholder for bytes of additional accounts which are tied to the',
1601
- 'Merkle trees operation and need to be rolled over as well.',
1602
- ],
1603
- type: 'u64',
1604
- },
1605
- ],
1606
- },
1607
- },
1608
- {
1609
- name: 'TokenData',
1610
- type: {
1611
- kind: 'struct',
1612
- fields: [
1613
- {
1614
- name: 'mint',
1615
- docs: ['The mint associated with this account'],
1616
- type: 'publicKey',
1617
- },
1618
- {
1619
- name: 'owner',
1620
- docs: ['The owner of this account.'],
1621
- type: 'publicKey',
1622
- },
1623
- {
1624
- name: 'amount',
1625
- docs: ['The amount of tokens this account holds.'],
1626
- type: 'u64',
1627
- },
1628
- {
1629
- name: 'delegate',
1630
- docs: [
1631
- 'If `delegate` is `Some` then `delegated_amount` represents',
1632
- 'the amount authorized by the delegate',
1633
- ],
1634
- type: {
1635
- option: 'publicKey',
1636
- },
1637
- },
1638
- {
1639
- name: 'state',
1640
- docs: ["The account's state"],
1641
- type: {
1642
- defined: 'AccountState',
1643
- },
1644
- },
1645
- {
1646
- name: 'tlv',
1647
- docs: [
1648
- 'Placeholder for TokenExtension tlv data (unimplemented)',
1649
- ],
1650
- type: {
1651
- option: 'bytes',
1652
- },
1653
- },
1654
- ],
1655
- },
1656
- },
1657
- ],
1658
- errors: [
1659
- {
1660
- code: 6000,
1661
- name: 'PublicKeyAmountMissmatch',
1662
- msg: 'public keys and amounts must be of same length',
1663
- },
1664
- {
1665
- code: 6001,
1666
- name: 'ComputeInputSumFailed',
1667
- msg: 'ComputeInputSumFailed',
1668
- },
1669
- {
1670
- code: 6002,
1671
- name: 'ComputeOutputSumFailed',
1672
- msg: 'ComputeOutputSumFailed',
1673
- },
1674
- {
1675
- code: 6003,
1676
- name: 'ComputeCompressSumFailed',
1677
- msg: 'ComputeCompressSumFailed',
1678
- },
1679
- {
1680
- code: 6004,
1681
- name: 'ComputeDecompressSumFailed',
1682
- msg: 'ComputeDecompressSumFailed',
1683
- },
1684
- {
1685
- code: 6005,
1686
- name: 'SumCheckFailed',
1687
- msg: 'SumCheckFailed',
1688
- },
1689
- {
1690
- code: 6006,
1691
- name: 'DecompressRecipientUndefinedForDecompress',
1692
- msg: 'DecompressRecipientUndefinedForDecompress',
1693
- },
1694
- {
1695
- code: 6007,
1696
- name: 'CompressedPdaUndefinedForDecompress',
1697
- msg: 'CompressedPdaUndefinedForDecompress',
1698
- },
1699
- {
1700
- code: 6008,
1701
- name: 'DeCompressAmountUndefinedForDecompress',
1702
- msg: 'DeCompressAmountUndefinedForDecompress',
1703
- },
1704
- {
1705
- code: 6009,
1706
- name: 'CompressedPdaUndefinedForCompress',
1707
- msg: 'CompressedPdaUndefinedForCompress',
1708
- },
1709
- {
1710
- code: 6010,
1711
- name: 'DeCompressAmountUndefinedForCompress',
1712
- msg: 'DeCompressAmountUndefinedForCompress',
1713
- },
1714
- {
1715
- code: 6011,
1716
- name: 'DelegateSignerCheckFailed',
1717
- msg: 'DelegateSignerCheckFailed',
1718
- },
1719
- {
1720
- code: 6012,
1721
- name: 'MintTooLarge',
1722
- msg: 'Minted amount greater than u64::MAX',
1723
- },
1724
- {
1725
- code: 6013,
1726
- name: 'SplTokenSupplyMismatch',
1727
- msg: 'SplTokenSupplyMismatch',
1728
- },
1729
- {
1730
- code: 6014,
1731
- name: 'HeapMemoryCheckFailed',
1732
- msg: 'HeapMemoryCheckFailed',
1733
- },
1734
- {
1735
- code: 6015,
1736
- name: 'InstructionNotCallable',
1737
- msg: 'The instruction is not callable',
1738
- },
1739
- {
1740
- code: 6016,
1741
- name: 'ArithmeticUnderflow',
1742
- msg: 'ArithmeticUnderflow',
1743
- },
1744
- {
1745
- code: 6017,
1746
- name: 'HashToFieldError',
1747
- msg: 'HashToFieldError',
1748
- },
1749
- {
1750
- code: 6018,
1751
- name: 'InvalidAuthorityMint',
1752
- msg: 'Expected the authority to be also a mint authority',
1753
- },
1754
- {
1755
- code: 6019,
1756
- name: 'InvalidFreezeAuthority',
1757
- msg: 'Provided authority is not the freeze authority',
1758
- },
1759
- {
1760
- code: 6020,
1761
- name: 'InvalidDelegateIndex',
1762
- },
1763
- {
1764
- code: 6021,
1765
- name: 'TokenPoolPdaUndefined',
1766
- },
1767
- {
1768
- code: 6022,
1769
- name: 'IsTokenPoolPda',
1770
- msg: 'Compress or decompress recipient is the same account as the token pool pda.',
1771
- },
1772
- {
1773
- code: 6023,
1774
- name: 'InvalidTokenPoolPda',
1775
- },
1776
- {
1777
- code: 6024,
1778
- name: 'NoInputTokenAccountsProvided',
1779
- },
1780
- {
1781
- code: 6025,
1782
- name: 'NoInputsProvided',
1783
- },
1784
- {
1785
- code: 6026,
1786
- name: 'MintHasNoFreezeAuthority',
1787
- },
1788
- {
1789
- code: 6027,
1790
- name: 'MintWithInvalidExtension',
1791
- },
1792
- {
1793
- code: 6028,
1794
- name: 'InsufficientTokenAccountBalance',
1795
- msg: 'The token account balance is less than the remaining amount.',
1796
- },
1797
- ],
1798
- };
1799
-
1800
- // TODO: include owner and lamports in packing.
1801
- /**
1802
- * Packs Compressed Token Accounts.
1803
- */
1804
- function packCompressedTokenAccounts(params) {
1805
- const { inputCompressedTokenAccounts, outputStateTrees, remainingAccounts = [], rootIndices, tokenTransferOutputs, } = params;
1806
- const _remainingAccounts = remainingAccounts.slice();
1807
- let delegateIndex = null;
1808
- if (inputCompressedTokenAccounts.length > 0 &&
1809
- inputCompressedTokenAccounts[0].parsed.delegate) {
1810
- delegateIndex = stateless_js.getIndexOrAdd(_remainingAccounts, inputCompressedTokenAccounts[0].parsed.delegate);
1811
- }
1812
- /// TODO: move pubkeyArray to remainingAccounts
1813
- /// Currently just packs 'delegate' to pubkeyArray
1814
- const packedInputTokenData = [];
1815
- /// pack inputs
1816
- inputCompressedTokenAccounts.forEach((account, index) => {
1817
- const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account.compressedAccount.merkleTree);
1818
- const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account.compressedAccount.nullifierQueue);
1819
- packedInputTokenData.push({
1820
- amount: account.parsed.amount,
1821
- delegateIndex,
1822
- merkleContext: {
1823
- merkleTreePubkeyIndex,
1824
- nullifierQueuePubkeyIndex,
1825
- leafIndex: account.compressedAccount.leafIndex,
1826
- queueIndex: null,
1827
- },
1828
- rootIndex: rootIndices[index],
1829
- lamports: account.compressedAccount.lamports.eq(stateless_js.bn(0))
1830
- ? null
1831
- : account.compressedAccount.lamports,
1832
- tlv: null,
1833
- });
1834
- });
1835
- /// pack output state trees
1836
- const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(outputStateTrees, tokenTransferOutputs.length, inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1837
- const packedOutputTokenData = [];
1838
- paddedOutputStateMerkleTrees.forEach((account, index) => {
1839
- var _a;
1840
- const merkleTreeIndex = stateless_js.getIndexOrAdd(_remainingAccounts, account);
1841
- packedOutputTokenData.push({
1842
- owner: tokenTransferOutputs[index].owner,
1843
- amount: tokenTransferOutputs[index].amount,
1844
- lamports: ((_a = tokenTransferOutputs[index].lamports) === null || _a === void 0 ? void 0 : _a.eq(stateless_js.bn(0)))
1845
- ? null
1846
- : tokenTransferOutputs[index].lamports,
1847
- merkleTreeIndex,
1848
- tlv: null,
1849
- });
1850
- });
1851
- // to meta
1852
- const remainingAccountMetas = _remainingAccounts.map((account) => ({
1853
- pubkey: account,
1854
- isWritable: true,
1855
- isSigner: false,
1856
- }));
1857
- return {
1858
- inputTokenDataWithContext: packedInputTokenData,
1859
- remainingAccountMetas,
1860
- packedOutputTokenData,
1861
- };
1862
- }
1863
-
1864
- const POOL_SEED = Buffer.from('pool');
1865
- const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
1866
- const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
1867
-
1868
- /**
1869
- * Sum up the token amounts of the compressed token accounts
1870
- */
1871
- const sumUpTokenAmount = (accounts) => {
1872
- return accounts.reduce((acc, account) => acc.add(account.parsed.amount), stateless_js.bn(0));
1873
- };
1874
- /**
1875
- * Validate that all the compressed token accounts are owned by the same owner.
1876
- */
1877
- const validateSameTokenOwner = (accounts) => {
1878
- const owner = accounts[0].parsed.owner;
1879
- accounts.forEach(acc => {
1880
- if (!acc.parsed.owner.equals(owner)) {
1881
- throw new Error('Token accounts must be owned by the same owner');
1882
- }
1883
- });
1884
- };
1885
- /**
1886
- * Parse compressed token accounts to get the mint, current owner and delegate.
1887
- */
1888
- const parseTokenData = (compressedTokenAccounts) => {
1889
- const mint = compressedTokenAccounts[0].parsed.mint;
1890
- const currentOwner = compressedTokenAccounts[0].parsed.owner;
1891
- const delegate = compressedTokenAccounts[0].parsed.delegate;
1892
- return { mint, currentOwner, delegate };
1893
- };
1894
- /**
1895
- * Create the output state for a transfer transaction.
1896
- * @param inputCompressedTokenAccounts Input state
1897
- * @param toAddress Recipient address
1898
- * @param amount Amount of tokens to transfer
1899
- * @returns Output token data for the transfer
1900
- * instruction
1901
- */
1902
- function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount) {
1903
- amount = stateless_js.bn(amount);
1904
- const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
1905
- const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1906
- const changeAmount = inputAmount.sub(amount);
1907
- stateless_js.validateSufficientBalance(changeAmount);
1908
- if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
1909
- return [
1910
- {
1911
- owner: toAddress,
1912
- amount,
1913
- lamports: inputLamports,
1914
- tlv: null,
1915
- },
1916
- ];
1917
- }
1918
- /// validates token program
1919
- stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1920
- validateSameTokenOwner(inputCompressedTokenAccounts);
1921
- const outputCompressedAccounts = [
1922
- {
1923
- owner: inputCompressedTokenAccounts[0].parsed.owner,
1924
- amount: changeAmount,
1925
- lamports: inputLamports,
1926
- tlv: null,
1927
- },
1928
- {
1929
- owner: toAddress,
1930
- amount,
1931
- lamports: stateless_js.bn(0),
1932
- tlv: null,
1933
- },
1934
- ];
1935
- return outputCompressedAccounts;
1936
- }
1937
- /**
1938
- * Create the output state for a compress transaction.
1939
- * @param inputCompressedTokenAccounts Input state
1940
- * @param amount Amount of tokens to compress
1941
- * @returns Output token data for the compress
1942
- * instruction
1943
- */
1944
- function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
1945
- amount = stateless_js.bn(amount);
1946
- const inputLamports = stateless_js.sumUpLamports(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1947
- const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
1948
- const changeAmount = inputAmount.sub(amount);
1949
- stateless_js.validateSufficientBalance(changeAmount);
1950
- /// lamports gets decompressed
1951
- if (changeAmount.eq(stateless_js.bn(0)) && inputLamports.eq(stateless_js.bn(0))) {
1952
- return [];
1953
- }
1954
- stateless_js.validateSameOwner(inputCompressedTokenAccounts.map(acc => acc.compressedAccount));
1955
- validateSameTokenOwner(inputCompressedTokenAccounts);
1956
- const tokenTransferOutputs = [
1957
- {
1958
- owner: inputCompressedTokenAccounts[0].parsed.owner,
1959
- amount: changeAmount,
1960
- lamports: inputLamports,
1961
- tlv: null,
1962
- },
1963
- ];
1964
- return tokenTransferOutputs;
1965
- }
1966
- class CompressedTokenProgram {
1967
- /**
1968
- * @internal
1969
- */
1970
- constructor() { }
1971
- /**
1972
- * Set a custom programId via PublicKey or base58 encoded string.
1973
- * This method is not required for regular usage.
1974
- *
1975
- * Use this only if you know what you are doing.
1976
- */
1977
- static setProgramId(programId) {
1978
- this.programId =
1979
- typeof programId === 'string'
1980
- ? new web3_js.PublicKey(programId)
1981
- : programId;
1982
- // Reset program when programId changes
1983
- this._program = null;
1984
- }
1985
- /** @internal */
1986
- static get program() {
1987
- if (!this._program) {
1988
- this.initializeProgram();
1989
- }
1990
- return this._program;
1991
- }
1992
- /**
1993
- * @internal
1994
- * Initializes the program statically if not already initialized.
1995
- */
1996
- static initializeProgram() {
1997
- if (!this._program) {
1998
- /// Note: We can use a mock connection because we're using the
1999
- /// program only for serde and building instructions, not for
2000
- /// interacting with the network.
2001
- const mockKeypair = web3_js.Keypair.generate();
2002
- const mockConnection = new web3_js.Connection('http://127.0.0.1:8899', 'confirmed');
2003
- const mockProvider = new anchor.AnchorProvider(mockConnection, stateless_js.useWallet(mockKeypair), stateless_js.confirmConfig);
2004
- anchor.setProvider(mockProvider);
2005
- this._program = new anchor.Program(IDL, this.programId, mockProvider);
2006
- }
2007
- }
2008
- /** @internal */
2009
- static deriveTokenPoolPda(mint) {
2010
- const seeds = [POOL_SEED, mint.toBuffer()];
2011
- const [address, _] = web3_js.PublicKey.findProgramAddressSync(seeds, this.programId);
2012
- return address;
2013
- }
2014
- /** @internal */
2015
- static get deriveCpiAuthorityPda() {
2016
- const [address, _] = web3_js.PublicKey.findProgramAddressSync([CPI_AUTHORITY_SEED], this.programId);
2017
- return address;
2018
- }
2019
- /**
2020
- * Construct createMint instruction for compressed tokens
2021
- */
2022
- static async createMint(params) {
2023
- const { mint, authority, feePayer, rentExemptBalance, tokenProgramId, freezeAuthority, mintSize, } = params;
2024
- const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
2025
- /// Create and initialize SPL Mint account
2026
- const createMintAccountInstruction = web3_js.SystemProgram.createAccount({
2027
- fromPubkey: feePayer,
2028
- lamports: rentExemptBalance,
2029
- newAccountPubkey: mint,
2030
- programId: tokenProgram,
2031
- space: mintSize !== null && mintSize !== void 0 ? mintSize : splToken.MINT_SIZE,
2032
- });
2033
- const initializeMintInstruction = splToken.createInitializeMint2Instruction(mint, params.decimals, authority, freezeAuthority, tokenProgram);
2034
- const ix = await this.createTokenPool({
2035
- feePayer,
2036
- mint,
2037
- tokenProgramId: tokenProgram,
2038
- });
2039
- return [createMintAccountInstruction, initializeMintInstruction, ix];
2040
- }
2041
- /**
2042
- * Enable compression for an existing SPL mint, creating an omnibus account.
2043
- * For new mints, use `CompressedTokenProgram.createMint`.
2044
- */
2045
- static async createTokenPool(params) {
2046
- const { mint, feePayer, tokenProgramId } = params;
2047
- const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
2048
- const tokenPoolPda = this.deriveTokenPoolPda(mint);
2049
- const ix = await this.program.methods
2050
- .createTokenPool()
2051
- .accounts({
2052
- mint,
2053
- feePayer,
2054
- tokenPoolPda,
2055
- systemProgram: web3_js.SystemProgram.programId,
2056
- tokenProgram,
2057
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
2058
- })
2059
- .instruction();
2060
- return ix;
2061
- }
2062
- /**
2063
- * Construct mintTo instruction for compressed tokens
2064
- */
2065
- static async mintTo(params) {
2066
- const systemKeys = stateless_js.defaultStaticAccountsStruct();
2067
- const { mint, feePayer, authority, merkleTree, toPubkey, amount, tokenProgramId, } = params;
2068
- const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
2069
- const tokenPoolPda = this.deriveTokenPoolPda(mint);
2070
- const amounts = stateless_js.toArray(amount).map(amount => stateless_js.bn(amount));
2071
- const toPubkeys = stateless_js.toArray(toPubkey);
2072
- if (amounts.length !== toPubkeys.length) {
2073
- throw new Error('Amount and toPubkey arrays must have the same length');
2074
- }
2075
- const instruction = await this.program.methods
2076
- .mintTo(toPubkeys, amounts, null)
2077
- .accounts({
2078
- feePayer,
2079
- authority,
2080
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
2081
- mint,
2082
- tokenPoolPda,
2083
- tokenProgram,
2084
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
2085
- registeredProgramPda: systemKeys.registeredProgramPda,
2086
- noopProgram: systemKeys.noopProgram,
2087
- accountCompressionAuthority: systemKeys.accountCompressionAuthority,
2088
- accountCompressionProgram: systemKeys.accountCompressionProgram,
2089
- merkleTree: merkleTree !== null && merkleTree !== void 0 ? merkleTree : stateless_js.defaultTestStateTreeAccounts().merkleTree,
2090
- selfProgram: this.programId,
2091
- solPoolPda: null,
2092
- })
2093
- .instruction();
2094
- return instruction;
2095
- }
2096
- /**
2097
- * Mint tokens from registered SPL mint account to a compressed account
2098
- */
2099
- static async approveAndMintTo(params) {
2100
- const { mint, feePayer, authorityTokenAccount, authority, merkleTree, toPubkey, tokenProgramId, } = params;
2101
- const amount = BigInt(params.amount.toString());
2102
- /// 1. Mint to existing ATA of mintAuthority.
2103
- const splMintToInstruction = splToken.createMintToInstruction(mint, authorityTokenAccount, authority, amount, [], tokenProgramId);
2104
- /// 2. Compress from mint authority ATA to recipient compressed account
2105
- const compressInstruction = await this.compress({
2106
- payer: feePayer,
2107
- owner: authority,
2108
- source: authorityTokenAccount,
2109
- toAddress: toPubkey,
2110
- mint,
2111
- amount: params.amount,
2112
- outputStateTree: merkleTree,
2113
- tokenProgramId,
2114
- });
2115
- return [splMintToInstruction, compressInstruction];
2116
- }
2117
- /**
2118
- * Construct transfer instruction for compressed tokens
2119
- */
2120
- static async transfer(params) {
2121
- const { payer, inputCompressedTokenAccounts, recentInputStateRootIndices, recentValidityProof, amount, outputStateTrees, toAddress, } = params;
2122
- const tokenTransferOutputs = createTransferOutputState(inputCompressedTokenAccounts, toAddress, amount);
2123
- const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
2124
- inputCompressedTokenAccounts,
2125
- outputStateTrees,
2126
- rootIndices: recentInputStateRootIndices,
2127
- tokenTransferOutputs,
2128
- });
2129
- const { mint, currentOwner } = parseTokenData(inputCompressedTokenAccounts);
2130
- const data = {
2131
- proof: recentValidityProof,
2132
- mint,
2133
- delegatedTransfer: null, // TODO: implement
2134
- inputTokenDataWithContext,
2135
- outputCompressedAccounts: packedOutputTokenData,
2136
- compressOrDecompressAmount: null,
2137
- isCompress: false,
2138
- cpiContext: null,
2139
- lamportsChangeAccountMerkleTreeIndex: null,
2140
- };
2141
- const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
2142
- const { accountCompressionAuthority, noopProgram, registeredProgramPda, accountCompressionProgram, } = stateless_js.defaultStaticAccountsStruct();
2143
- const instruction = await this.program.methods
2144
- .transfer(encodedData)
2145
- .accounts({
2146
- feePayer: payer,
2147
- authority: currentOwner,
2148
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
2149
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
2150
- registeredProgramPda: registeredProgramPda,
2151
- noopProgram: noopProgram,
2152
- accountCompressionAuthority: accountCompressionAuthority,
2153
- accountCompressionProgram: accountCompressionProgram,
2154
- selfProgram: this.programId,
2155
- tokenPoolPda: null,
2156
- compressOrDecompressTokenAccount: null,
2157
- tokenProgram: null,
2158
- })
2159
- .remainingAccounts(remainingAccountMetas)
2160
- .instruction();
2161
- return instruction;
2162
- }
2163
- /**
2164
- * Create lookup table instructions for the token program's default accounts.
2165
- */
2166
- static async createTokenProgramLookupTable(params) {
2167
- const { authority, mints, recentSlot, payer, remainingAccounts } = params;
2168
- const [createInstruction, lookupTableAddress] = web3_js.AddressLookupTableProgram.createLookupTable({
2169
- authority,
2170
- payer: authority,
2171
- recentSlot,
2172
- });
2173
- let optionalMintKeys = [];
2174
- if (mints) {
2175
- optionalMintKeys = [
2176
- ...mints,
2177
- ...mints.map(mint => this.deriveTokenPoolPda(mint)),
2178
- ];
2179
- }
2180
- const extendInstruction = web3_js.AddressLookupTableProgram.extendLookupTable({
2181
- payer,
2182
- authority,
2183
- lookupTable: lookupTableAddress,
2184
- addresses: [
2185
- this.deriveCpiAuthorityPda,
2186
- stateless_js.LightSystemProgram.programId,
2187
- stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
2188
- stateless_js.defaultStaticAccountsStruct().noopProgram,
2189
- stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
2190
- stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
2191
- stateless_js.defaultTestStateTreeAccounts().merkleTree,
2192
- stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
2193
- stateless_js.defaultTestStateTreeAccounts().addressTree,
2194
- stateless_js.defaultTestStateTreeAccounts().addressQueue,
2195
- this.programId,
2196
- splToken.TOKEN_PROGRAM_ID,
2197
- splToken.TOKEN_2022_PROGRAM_ID,
2198
- authority,
2199
- ...optionalMintKeys,
2200
- ...(remainingAccounts !== null && remainingAccounts !== void 0 ? remainingAccounts : []),
2201
- ],
2202
- });
2203
- return {
2204
- instructions: [createInstruction, extendInstruction],
2205
- address: lookupTableAddress,
2206
- };
2207
- }
2208
- /**
2209
- * Create compress instruction
2210
- * @returns compressInstruction
2211
- */
2212
- static async compress(params) {
2213
- const { payer, owner, source, toAddress, mint, outputStateTree, tokenProgramId, } = params;
2214
- if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
2215
- throw new Error('Both amount and toAddress must be arrays or both must be single values');
2216
- }
2217
- let tokenTransferOutputs;
2218
- if (Array.isArray(params.amount) && Array.isArray(params.toAddress)) {
2219
- if (params.amount.length !== params.toAddress.length) {
2220
- throw new Error('Amount and toAddress arrays must have the same length');
2221
- }
2222
- tokenTransferOutputs = params.amount.map((amt, index) => {
2223
- const amount = stateless_js.bn(amt);
2224
- return {
2225
- owner: params.toAddress[index],
2226
- amount,
2227
- lamports: stateless_js.bn(0),
2228
- tlv: null,
2229
- };
2230
- });
2231
- }
2232
- else {
2233
- tokenTransferOutputs = [
2234
- {
2235
- owner: toAddress,
2236
- amount: stateless_js.bn(params.amount),
2237
- lamports: stateless_js.bn(0),
2238
- tlv: null,
2239
- },
2240
- ];
2241
- }
2242
- const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
2243
- inputCompressedTokenAccounts: [],
2244
- outputStateTrees: outputStateTree,
2245
- rootIndices: [],
2246
- tokenTransferOutputs,
2247
- });
2248
- const data = {
2249
- proof: null,
2250
- mint,
2251
- delegatedTransfer: null, // TODO: implement
2252
- inputTokenDataWithContext,
2253
- outputCompressedAccounts: packedOutputTokenData,
2254
- compressOrDecompressAmount: Array.isArray(params.amount)
2255
- ? params.amount
2256
- .map(amt => new anchor.BN(amt))
2257
- .reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
2258
- : new anchor.BN(params.amount),
2259
- isCompress: true,
2260
- cpiContext: null,
2261
- lamportsChangeAccountMerkleTreeIndex: null,
2262
- };
2263
- const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
2264
- const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
2265
- const instruction = await this.program.methods
2266
- .transfer(encodedData)
2267
- .accounts({
2268
- feePayer: payer,
2269
- authority: owner,
2270
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
2271
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
2272
- registeredProgramPda: stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
2273
- noopProgram: stateless_js.defaultStaticAccountsStruct().noopProgram,
2274
- accountCompressionAuthority: stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
2275
- accountCompressionProgram: stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
2276
- selfProgram: this.programId,
2277
- tokenPoolPda: this.deriveTokenPoolPda(mint),
2278
- compressOrDecompressTokenAccount: source, // token
2279
- tokenProgram,
2280
- })
2281
- .remainingAccounts(remainingAccountMetas)
2282
- .instruction();
2283
- return instruction;
2284
- }
2285
- /**
2286
- * Construct decompress instruction
2287
- */
2288
- static async decompress(params) {
2289
- const { payer, inputCompressedTokenAccounts, toAddress, outputStateTree, recentValidityProof, recentInputStateRootIndices, tokenProgramId, } = params;
2290
- const amount = stateless_js.bn(params.amount);
2291
- const tokenTransferOutputs = createDecompressOutputState(inputCompressedTokenAccounts, amount);
2292
- /// Pack
2293
- const { inputTokenDataWithContext, packedOutputTokenData, remainingAccountMetas, } = packCompressedTokenAccounts({
2294
- inputCompressedTokenAccounts,
2295
- outputStateTrees: outputStateTree,
2296
- rootIndices: recentInputStateRootIndices,
2297
- tokenTransferOutputs: tokenTransferOutputs,
2298
- });
2299
- const { mint, currentOwner } = parseTokenData(inputCompressedTokenAccounts);
2300
- const data = {
2301
- proof: recentValidityProof,
2302
- mint,
2303
- delegatedTransfer: null, // TODO: implement
2304
- inputTokenDataWithContext,
2305
- outputCompressedAccounts: packedOutputTokenData,
2306
- compressOrDecompressAmount: amount,
2307
- isCompress: false,
2308
- cpiContext: null,
2309
- lamportsChangeAccountMerkleTreeIndex: null,
2310
- };
2311
- const encodedData = this.program.coder.types.encode('CompressedTokenInstructionDataTransfer', data);
2312
- const { accountCompressionAuthority, noopProgram, registeredProgramPda, accountCompressionProgram, } = stateless_js.defaultStaticAccountsStruct();
2313
- const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
2314
- const instruction = await this.program.methods
2315
- .transfer(encodedData)
2316
- .accounts({
2317
- feePayer: payer,
2318
- authority: currentOwner,
2319
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
2320
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
2321
- registeredProgramPda: registeredProgramPda,
2322
- noopProgram: noopProgram,
2323
- accountCompressionAuthority: accountCompressionAuthority,
2324
- accountCompressionProgram: accountCompressionProgram,
2325
- selfProgram: this.programId,
2326
- tokenPoolPda: this.deriveTokenPoolPda(mint),
2327
- compressOrDecompressTokenAccount: toAddress,
2328
- tokenProgram,
2329
- })
2330
- .remainingAccounts(remainingAccountMetas)
2331
- .instruction();
2332
- return instruction;
2333
- }
2334
- static async mergeTokenAccounts(params) {
2335
- const { payer, owner, inputCompressedTokenAccounts, outputStateTree, recentValidityProof, recentInputStateRootIndices, } = params;
2336
- if (inputCompressedTokenAccounts.length > 3) {
2337
- throw new Error('Cannot merge more than 3 token accounts at once');
2338
- }
2339
- const ix = await this.transfer({
2340
- payer,
2341
- inputCompressedTokenAccounts,
2342
- toAddress: owner,
2343
- amount: inputCompressedTokenAccounts.reduce((sum, account) => sum.add(account.parsed.amount), new anchor.BN(0)),
2344
- outputStateTrees: outputStateTree,
2345
- recentInputStateRootIndices,
2346
- recentValidityProof,
2347
- });
2348
- return [ix];
2349
- }
2350
- static async compressSplTokenAccount(params) {
2351
- const { feePayer, authority, tokenAccount, mint, remainingAmount, outputStateTree, tokenProgramId, } = params;
2352
- const tokenProgram = tokenProgramId !== null && tokenProgramId !== void 0 ? tokenProgramId : splToken.TOKEN_PROGRAM_ID;
2353
- const remainingAccountMetas = [
2354
- {
2355
- pubkey: outputStateTree,
2356
- isSigner: false,
2357
- isWritable: true,
2358
- },
2359
- ];
2360
- const instruction = await this.program.methods
2361
- .compressSplTokenAccount(authority, remainingAmount !== null && remainingAmount !== void 0 ? remainingAmount : null, null)
2362
- .accounts({
2363
- feePayer,
2364
- authority,
2365
- cpiAuthorityPda: this.deriveCpiAuthorityPda,
2366
- lightSystemProgram: stateless_js.LightSystemProgram.programId,
2367
- registeredProgramPda: stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
2368
- noopProgram: stateless_js.defaultStaticAccountsStruct().noopProgram,
2369
- accountCompressionAuthority: stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
2370
- accountCompressionProgram: stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
2371
- selfProgram: this.programId,
2372
- tokenPoolPda: this.deriveTokenPoolPda(mint),
2373
- compressOrDecompressTokenAccount: tokenAccount,
2374
- tokenProgram,
2375
- systemProgram: web3_js.SystemProgram.programId,
2376
- })
2377
- .remainingAccounts(remainingAccountMetas)
2378
- .instruction();
2379
- return instruction;
2380
- }
2381
- static async get_mint_program_id(mint, connection) {
2382
- var _a;
2383
- return (_a = (await connection.getAccountInfo(mint))) === null || _a === void 0 ? void 0 : _a.owner;
2384
- }
2385
- }
2386
- /**
2387
- * Public key that identifies the CompressedPda program
2388
- */
2389
- CompressedTokenProgram.programId = new web3_js.PublicKey('cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m');
2390
- CompressedTokenProgram._program = null;
2391
-
2392
- /**
2393
- * Mint compressed tokens to a solana address from an external mint authority
2394
- *
2395
- * @param rpc Rpc to use
2396
- * @param payer Payer of the transaction fees
2397
- * @param mint Mint for the account
2398
- * @param destination Address of the account to mint to
2399
- * @param authority Minting authority
2400
- * @param amount Amount to mint
2401
- * @param merkleTree State tree account that the compressed tokens should be
2402
- * part of. Defaults to the default state tree account.
2403
- * @param confirmOptions Options for confirming the transaction
2404
- *
2405
- * @return Signature of the confirmed transaction
2406
- */
2407
- async function approveAndMintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions, tokenProgramId) {
2408
- tokenProgramId = tokenProgramId
2409
- ? tokenProgramId
2410
- : await CompressedTokenProgram.get_mint_program_id(mint, rpc);
2411
- const authorityTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(rpc, payer, mint, authority.publicKey, undefined, undefined, confirmOptions, tokenProgramId);
2412
- const ixs = await CompressedTokenProgram.approveAndMintTo({
2413
- feePayer: payer.publicKey,
2414
- mint,
2415
- authority: authority.publicKey,
2416
- authorityTokenAccount: authorityTokenAccount.address,
2417
- amount,
2418
- toPubkey: destination,
2419
- merkleTree,
2420
- tokenProgramId,
2421
- });
2422
- const { blockhash } = await rpc.getLatestBlockhash();
2423
- const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
2424
- const tx = stateless_js.buildAndSignTx([
2425
- web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
2426
- ...ixs,
2427
- ], payer, blockhash, additionalSigners);
2428
- const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
2429
- return txId;
2430
- }
2431
-
2432
- /**
2433
- * Compress SPL tokens
2434
- *
2435
- * @param rpc Rpc connection to use
2436
- * @param payer Payer of the transaction fees
2437
- * @param mint Mint of the compressed token
2438
- * @param amount Number of tokens to transfer
2439
- * @param owner Owner of the compressed tokens.
2440
- * @param sourceTokenAccount Source (associated) token account
2441
- * @param toAddress Destination address of the recipient
2442
- * @param merkleTree State tree account that the compressed tokens
2443
- * should be inserted into. Defaults to a default
2444
- * state tree account.
2445
- * @param confirmOptions Options for confirming the transaction
2446
- *
2447
- *
2448
- * @return Signature of the confirmed transaction
2449
- */
2450
- async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toAddress, merkleTree, confirmOptions, tokenProgramId) {
2451
- tokenProgramId = tokenProgramId
2452
- ? tokenProgramId
2453
- : await CompressedTokenProgram.get_mint_program_id(mint, rpc);
2454
- const compressIx = await CompressedTokenProgram.compress({
2455
- payer: payer.publicKey,
2456
- owner: owner.publicKey,
2457
- source: sourceTokenAccount,
2458
- toAddress,
2459
- amount,
2460
- mint,
2461
- outputStateTree: merkleTree,
2462
- tokenProgramId,
2463
- });
2464
- const blockhashCtx = await rpc.getLatestBlockhash();
2465
- const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
2466
- const signedTx = stateless_js.buildAndSignTx([
2467
- web3_js.ComputeBudgetProgram.setComputeUnitLimit({
2468
- units: 1000000,
2469
- }),
2470
- compressIx,
2471
- ], payer, blockhashCtx.blockhash, additionalSigners);
2472
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions, blockhashCtx);
2473
- return txId;
2474
- }
2475
-
2476
- /**
2477
- * Transfer compressed tokens from one owner to another
2478
- *
2479
- * @param rpc Rpc to use
2480
- * @param payer Payer of the transaction fees
2481
- * @param mint Mint of the compressed token
2482
- * @param amount Number of tokens to transfer
2483
- * @param owner Owner of the compressed tokens
2484
- * @param toAddress Destination address of the recipient
2485
- * @param merkleTree State tree account that the compressed tokens should be
2486
- * inserted into. Defaults to the default state tree
2487
- * account.
2488
- * @param confirmOptions Options for confirming the transaction
2489
- *
2490
- *
2491
- * @return Signature of the confirmed transaction
2492
- */
2493
- async function transfer(rpc, payer, mint, amount, owner, toAddress,
2494
- /// TODO: allow multiple
2495
- merkleTree, confirmOptions) {
2496
- amount = stateless_js.bn(amount);
2497
- const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, {
2498
- mint,
2499
- });
2500
- const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(compressedTokenAccounts.items, amount);
2501
- const proof = await rpc.getValidityProof(inputAccounts.map(account => stateless_js.bn(account.compressedAccount.hash)));
2502
- const ix = await CompressedTokenProgram.transfer({
2503
- payer: payer.publicKey,
2504
- inputCompressedTokenAccounts: inputAccounts,
2505
- toAddress,
2506
- amount,
2507
- recentInputStateRootIndices: proof.rootIndices,
2508
- recentValidityProof: proof.compressedProof,
2509
- outputStateTrees: merkleTree,
2510
- });
2511
- const { blockhash } = await rpc.getLatestBlockhash();
2512
- const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
2513
- const signedTx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
2514
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
2515
- return txId;
2516
- }
2517
- /**
2518
- * Selects the minimal number of compressed token accounts for a transfer.
2519
- *
2520
- * 1. Sorts the accounts by amount in descending order
2521
- * 2. Accumulates the amount until it is greater than or equal to the transfer
2522
- * amount
2523
- */
2524
- function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
2525
- let accumulatedAmount = stateless_js.bn(0);
2526
- let accumulatedLamports = stateless_js.bn(0);
2527
- const selectedAccounts = [];
2528
- accounts.sort((a, b) => b.parsed.amount.cmp(a.parsed.amount));
2529
- for (const account of accounts) {
2530
- if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
2531
- break;
2532
- accumulatedAmount = accumulatedAmount.add(account.parsed.amount);
2533
- accumulatedLamports = accumulatedLamports.add(account.compressedAccount.lamports);
2534
- selectedAccounts.push(account);
2535
- }
2536
- if (accumulatedAmount.lt(stateless_js.bn(transferAmount))) {
2537
- throw new Error(`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`);
2538
- }
2539
- return [
2540
- selectedAccounts,
2541
- accumulatedAmount,
2542
- accumulatedLamports.lt(stateless_js.bn(0)) ? accumulatedLamports : null,
2543
- ];
2544
- }
2545
-
2546
- /**
2547
- * Decompress compressed tokens
2548
- *
2549
- * @param rpc Rpc to use
2550
- * @param payer Payer of the transaction fees
2551
- * @param mint Mint of the compressed token
2552
- * @param amount Number of tokens to transfer
2553
- * @param owner Owner of the compressed tokens
2554
- * @param toAddress Destination **uncompressed** (associated) token account
2555
- * address.
2556
- * @param merkleTree State tree account that any change compressed tokens should be
2557
- * inserted into. Defaults to a default state tree
2558
- * account.
2559
- * @param confirmOptions Options for confirming the transaction
2560
- *
2561
- *
2562
- * @return Signature of the confirmed transaction
2563
- */
2564
- async function decompress(rpc, payer, mint, amount, owner, toAddress,
2565
- /// TODO: allow multiple
2566
- merkleTree, confirmOptions, tokenProgramId) {
2567
- tokenProgramId = tokenProgramId
2568
- ? tokenProgramId
2569
- : await CompressedTokenProgram.get_mint_program_id(mint, rpc);
2570
- amount = stateless_js.bn(amount);
2571
- const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, {
2572
- mint,
2573
- });
2574
- /// TODO: consider using a different selection algorithm
2575
- const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(compressedTokenAccounts.items, amount);
2576
- const proof = await rpc.getValidityProof(inputAccounts.map(account => stateless_js.bn(account.compressedAccount.hash)));
2577
- const ix = await CompressedTokenProgram.decompress({
2578
- payer: payer.publicKey,
2579
- inputCompressedTokenAccounts: inputAccounts,
2580
- toAddress, // TODO: add explicit check that it is a token account
2581
- amount,
2582
- outputStateTree: merkleTree,
2583
- recentInputStateRootIndices: proof.rootIndices,
2584
- recentValidityProof: proof.compressedProof,
2585
- tokenProgramId,
2586
- });
2587
- const { blockhash } = await rpc.getLatestBlockhash();
2588
- const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
2589
- const signedTx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
2590
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
2591
- return txId;
2592
- }
2593
-
2594
- /**
2595
- * Create and initialize a new compressed token mint
2596
- *
2597
- * @param rpc RPC to use
2598
- * @param payer Payer of the transaction and initialization fees
2599
- * @param mintAuthority Account or multisig that will control minting
2600
- * @param decimals Location of the decimal place
2601
- * @param keypair Optional keypair, defaulting to a new random one
2602
- * @param confirmOptions Options for confirming the transaction
2603
- * @param isToken22 Whether to create a Token 2022 mint. Defaults to false.
2604
- *
2605
- * @return Address of the new mint and the transaction signature
2606
- */
2607
- async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js.Keypair.generate(), confirmOptions, isToken22 = false) {
2608
- const rentExemptBalance = await rpc.getMinimumBalanceForRentExemption(splToken.MINT_SIZE);
2609
- const tokenProgramId = isToken22 ? splToken.TOKEN_2022_PROGRAM_ID : splToken.TOKEN_PROGRAM_ID;
2610
- const ixs = await CompressedTokenProgram.createMint({
2611
- feePayer: payer.publicKey,
2612
- mint: keypair.publicKey,
2613
- decimals,
2614
- authority: mintAuthority,
2615
- freezeAuthority: null, // TODO: add feature
2616
- rentExemptBalance,
2617
- tokenProgramId,
2618
- });
2619
- const { blockhash } = await rpc.getLatestBlockhash();
2620
- const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
2621
- const tx = stateless_js.buildAndSignTx(ixs, payer, blockhash, additionalSigners);
2622
- const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
2623
- return { mint: keypair.publicKey, transactionSignature: txId };
2624
- }
2625
-
2626
- /**
2627
- * Mint compressed tokens to a solana address
2628
- *
2629
- * @param rpc Rpc to use
2630
- * @param payer Payer of the transaction fees
2631
- * @param mint Mint for the account
2632
- * @param destination Address of the account to mint to. Can be an array of
2633
- * addresses if the amount is an array of amounts.
2634
- * @param authority Minting authority
2635
- * @param amount Amount to mint. Can be an array of amounts if the
2636
- * destination is an array of addresses.
2637
- * @param merkleTree State tree account that the compressed tokens should be
2638
- * part of. Defaults to the default state tree account.
2639
- * @param confirmOptions Options for confirming the transaction
2640
- *
2641
- * @return Signature of the confirmed transaction
2642
- */
2643
- async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTree, confirmOptions, tokenProgramId) {
2644
- tokenProgramId = tokenProgramId
2645
- ? tokenProgramId
2646
- : await CompressedTokenProgram.get_mint_program_id(mint, rpc);
2647
- const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
2648
- const ix = await CompressedTokenProgram.mintTo({
2649
- feePayer: payer.publicKey,
2650
- mint,
2651
- authority: authority.publicKey,
2652
- amount: amount,
2653
- toPubkey: destination,
2654
- merkleTree,
2655
- tokenProgramId,
2656
- });
2657
- const { blockhash } = await rpc.getLatestBlockhash();
2658
- const tx = stateless_js.buildAndSignTx([web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }), ix], payer, blockhash, additionalSigners);
2659
- const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
2660
- return txId;
2661
- }
2662
-
2663
- /**
2664
- * Merge multiple compressed token accounts for a given mint into a single
2665
- * account
2666
- *
2667
- * @param rpc RPC to use
2668
- * @param payer Payer of the transaction fees
2669
- * @param mint Public key of the token's mint
2670
- * @param owner Owner of the token accounts to be merged
2671
- * @param merkleTree Optional merkle tree for compressed tokens
2672
- * @param confirmOptions Options for confirming the transaction
2673
- *
2674
- * @return Array of transaction signatures
2675
- */
2676
- async function mergeTokenAccounts(rpc, payer, mint, owner, merkleTree, confirmOptions) {
2677
- const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(owner.publicKey, { mint });
2678
- if (compressedTokenAccounts.items.length === 0) {
2679
- throw new Error(`No compressed token accounts found for mint ${mint.toBase58()}`);
2680
- }
2681
- if (compressedTokenAccounts.items.length >= 6) {
2682
- throw new Error(`Too many compressed token accounts used for mint ${mint.toBase58()}`);
2683
- }
2684
- const instructions = [
2685
- web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
2686
- ];
2687
- for (let i = 0; i < compressedTokenAccounts.items.slice(0, 6).length; i += 3) {
2688
- const batch = compressedTokenAccounts.items.slice(i, i + 3);
2689
- const proof = await rpc.getValidityProof(batch.map(account => stateless_js.bn(account.compressedAccount.hash)));
2690
- const batchInstructions = await CompressedTokenProgram.mergeTokenAccounts({
2691
- payer: payer.publicKey,
2692
- owner: owner.publicKey,
2693
- mint,
2694
- inputCompressedTokenAccounts: batch,
2695
- outputStateTree: merkleTree,
2696
- recentValidityProof: proof.compressedProof,
2697
- recentInputStateRootIndices: proof.rootIndices,
2698
- });
2699
- instructions.push(...batchInstructions);
2700
- }
2701
- const { blockhash } = await rpc.getLatestBlockhash();
2702
- const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
2703
- const signedTx = stateless_js.buildAndSignTx(instructions, payer, blockhash, additionalSigners);
2704
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions);
2705
- return txId;
2706
- }
2707
-
2708
- /**
2709
- * Register an existing mint with the CompressedToken program
2710
- *
2711
- * @param rpc RPC to use
2712
- * @param payer Payer of the transaction and initialization fees
2713
- * @param mintAuthority Account or multisig that will control minting. Is signer.
2714
- * @param mintAddress Address of the existing mint
2715
- * @param confirmOptions Options for confirming the transaction
2716
- *
2717
- * @return transaction signature
2718
- */
2719
- async function createTokenPool(rpc, payer, mint, confirmOptions, tokenProgramId) {
2720
- tokenProgramId = tokenProgramId
2721
- ? tokenProgramId
2722
- : await CompressedTokenProgram.get_mint_program_id(mint, rpc);
2723
- const ix = await CompressedTokenProgram.createTokenPool({
2724
- feePayer: payer.publicKey,
2725
- mint,
2726
- tokenProgramId,
2727
- });
2728
- const { blockhash } = await rpc.getLatestBlockhash();
2729
- const tx = stateless_js.buildAndSignTx([ix], payer, blockhash);
2730
- const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
2731
- return txId;
2732
- }
2733
-
2734
- /**
2735
- * Create a lookup table for the token program's default accounts
2736
- *
2737
- * @param rpc Rpc connection to use
2738
- * @param payer Payer of the transaction fees
2739
- * @param authority Authority of the lookup table
2740
- * @param mints Optional array of mint public keys to include in
2741
- * the lookup table
2742
- * @param additionalAccounts Optional array of additional account public keys
2743
- * to include in the lookup table
2744
- *
2745
- * @return Transaction signatures and the address of the created lookup table
2746
- */
2747
- async function createTokenProgramLookupTable(rpc, payer, authority, mints, additionalAccounts) {
2748
- const recentSlot = await rpc.getSlot('finalized');
2749
- const { instructions, address } = await CompressedTokenProgram.createTokenProgramLookupTable({
2750
- payer: payer.publicKey,
2751
- authority: authority.publicKey,
2752
- mints,
2753
- remainingAccounts: additionalAccounts,
2754
- recentSlot,
2755
- });
2756
- const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
2757
- const blockhashCtx = await rpc.getLatestBlockhash();
2758
- const signedTx = stateless_js.buildAndSignTx([instructions[0]], payer, blockhashCtx.blockhash, additionalSigners);
2759
- /// Must wait for the first instruction to be finalized.
2760
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, { commitment: 'finalized' }, blockhashCtx);
2761
- const blockhashCtx2 = await rpc.getLatestBlockhash();
2762
- const signedTx2 = stateless_js.buildAndSignTx([instructions[1]], payer, blockhashCtx2.blockhash, additionalSigners);
2763
- const txId2 = await stateless_js.sendAndConfirmTx(rpc, signedTx2, { commitment: 'finalized' }, blockhashCtx2);
2764
- return { txIds: [txId, txId2], address };
2765
- }
2766
-
2767
- /**
2768
- * Compress SPL tokens into compressed token format
2769
- *
2770
- * @param rpc Rpc connection to use
2771
- * @param payer Payer of the transaction fees
2772
- * @param mint Mint of the token to compress
2773
- * @param owner Owner of the token account
2774
- * @param tokenAccount Token account to compress
2775
- * @param outputStateTree State tree to insert the compressed token account into
2776
- * @param remainingAmount Optional: amount to leave in token account. Default: 0
2777
- * @param confirmOptions Options for confirming the transaction
2778
- *
2779
- * @return Signature of the confirmed transaction
2780
- */
2781
- async function compressSplTokenAccount(rpc, payer, mint, owner, tokenAccount, outputStateTree, remainingAmount, confirmOptions, tokenProgramId) {
2782
- tokenProgramId = tokenProgramId
2783
- ? tokenProgramId
2784
- : await CompressedTokenProgram.get_mint_program_id(mint, rpc);
2785
- const compressIx = await CompressedTokenProgram.compressSplTokenAccount({
2786
- feePayer: payer.publicKey,
2787
- authority: owner.publicKey,
2788
- tokenAccount,
2789
- mint,
2790
- remainingAmount,
2791
- outputStateTree,
2792
- tokenProgramId,
2793
- });
2794
- const blockhashCtx = await rpc.getLatestBlockhash();
2795
- const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
2796
- const signedTx = stateless_js.buildAndSignTx([
2797
- web3_js.ComputeBudgetProgram.setComputeUnitLimit({
2798
- units: 1000000,
2799
- }),
2800
- compressIx,
2801
- ], payer, blockhashCtx.blockhash, additionalSigners);
2802
- const txId = await stateless_js.sendAndConfirmTx(rpc, signedTx, confirmOptions, blockhashCtx);
2803
- return txId;
2804
- }
2805
-
2806
- exports.CPI_AUTHORITY_SEED = CPI_AUTHORITY_SEED;
2807
- exports.CompressedTokenProgram = CompressedTokenProgram;
2808
- exports.IDL = IDL;
2809
- exports.POOL_SEED = POOL_SEED;
2810
- exports.SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE;
2811
- exports.approveAndMintTo = approveAndMintTo;
2812
- exports.compress = compress;
2813
- exports.compressSplTokenAccount = compressSplTokenAccount;
2814
- exports.createDecompressOutputState = createDecompressOutputState;
2815
- exports.createMint = createMint;
2816
- exports.createTokenPool = createTokenPool;
2817
- exports.createTokenProgramLookupTable = createTokenProgramLookupTable;
2818
- exports.createTransferOutputState = createTransferOutputState;
2819
- exports.decompress = decompress;
2820
- exports.mergeTokenAccounts = mergeTokenAccounts;
2821
- exports.mintTo = mintTo;
2822
- exports.packCompressedTokenAccounts = packCompressedTokenAccounts;
2823
- exports.parseTokenData = parseTokenData;
2824
- exports.selectMinCompressedTokenAccountsForTransfer = selectMinCompressedTokenAccountsForTransfer;
2825
- exports.sumUpTokenAmount = sumUpTokenAmount;
2826
- exports.transfer = transfer;
2827
- exports.validateSameTokenOwner = validateSameTokenOwner;
1
+ "use strict";var t=require("@lightprotocol/stateless.js"),e=require("@solana/web3.js"),r=require("@solana/spl-token"),n=require("buffer");function i(e){const{inputCompressedTokenAccounts:r,outputStateTrees:n,remainingAccounts:i=[],rootIndices:o,tokenTransferOutputs:s}=e,a=i.slice();let u=null;r.length>0&&r[0].parsed.delegate&&(u=t.getIndexOrAdd(a,r[0].parsed.delegate));const h=[];r.forEach(((e,r)=>{const n=t.getIndexOrAdd(a,e.compressedAccount.merkleTree),i=t.getIndexOrAdd(a,e.compressedAccount.nullifierQueue);h.push({amount:e.parsed.amount,delegateIndex:u,merkleContext:{merkleTreePubkeyIndex:n,nullifierQueuePubkeyIndex:i,leafIndex:e.compressedAccount.leafIndex,queueIndex:null},rootIndex:o[r],lamports:e.compressedAccount.lamports.eq(t.bn(0))?null:e.compressedAccount.lamports,tlv:null})}));const l=t.padOutputStateMerkleTrees(n,s.length,r.map((t=>t.compressedAccount))),d=[];l.forEach(((e,r)=>{var n;const i=t.getIndexOrAdd(a,e);d.push({owner:s[r].owner,amount:s[r].amount,lamports:(null===(n=s[r].lamports)||void 0===n?void 0:n.eq(t.bn(0)))?null:s[r].lamports,merkleTreeIndex:i,tlv:null})}));const c=a.map((t=>({pubkey:t,isWritable:1,isSigner:0})));return{inputTokenDataWithContext:h,remainingAccountMetas:c,packedOutputTokenData:d}}const o=Buffer.from("pool"),s=Buffer.from("cpi_authority");var a="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function u(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function h(t){if(t.__esModule)return t;var e=t.default;if("function"==typeof e){var r=function t(){return this instanceof t?Reflect.construct(e,arguments,this.constructor):e.apply(this,arguments)};r.prototype=e.prototype}else r={};return Object.defineProperty(r,"__esModule",{value:1}),Object.keys(t).forEach((function(e){var n=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(r,e,n.get?n:{enumerable:1,get:function(){return t[e]}})})),r}var l={exports:{}},d=h(Object.freeze({__proto__:null,default:{}}));!function(t,e){function r(t,e){if(!t)throw new Error(e||"Assertion failed")}function n(t,e){t.super_=e;var r=function(){};r.prototype=e.prototype,t.prototype=new r,t.prototype.constructor=t}function i(t,e,r){if(i.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(r=e,e=10),this._init(t||0,e||10,r||"be"))}var o;"object"==typeof l?l.exports=i:e.BN=i,i.BN=i,i.wordSize=26;try{o="undefined"!=typeof window&&void 0!==window.Buffer?window.Buffer:d.Buffer}catch(t){}function s(t,e){var n=t.charCodeAt(e);return n>=48&&n<=57?n-48:n>=65&&n<=70?n-55:n>=97&&n<=102?n-87:void r(0,"Invalid character in "+t)}function a(t,e,r){var n=s(t,r);return r-1>=e&&(n|=s(t,r-1)<<4),n}function u(t,e,n,i){for(var o=0,s=0,a=Math.min(t.length,n),u=e;u<a;u++){var h=t.charCodeAt(u)-48;o*=i,s=h>=49?h-49+10:h>=17?h-17+10:h,r(h>=0&&s<i,"Invalid character"),o+=s}return o}function h(t,e){t.words=e.words,t.length=e.length,t.negative=e.negative,t.red=e.red}if(i.isBN=function(t){return t instanceof i?1:null!==t&&"object"==typeof t&&t.constructor.wordSize===i.wordSize&&Array.isArray(t.words)},i.max=function(t,e){return t.cmp(e)>0?t:e},i.min=function(t,e){return t.cmp(e)<0?t:e},i.prototype._init=function(t,e,n){if("number"==typeof t)return this._initNumber(t,e,n);if("object"==typeof t)return this._initArray(t,e,n);"hex"===e&&(e=16),r(e===(0|e)&&e>=2&&e<=36);var i=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&(i++,this.negative=1),i<t.length&&(16===e?this._parseHex(t,i,n):(this._parseBase(t,e,i),"le"===n&&this._initArray(this.toArray(),e,n)))},i.prototype._initNumber=function(t,e,n){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(r(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===n&&this._initArray(this.toArray(),e,n)},i.prototype._initArray=function(t,e,n){if(r("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var o,s,a=0;if("be"===n)for(i=t.length-1,o=0;i>=0;i-=3)s=t[i]|t[i-1]<<8|t[i-2]<<16,this.words[o]|=s<<a&67108863,this.words[o+1]=s>>>26-a&67108863,(a+=24)>=26&&(a-=26,o++);else if("le"===n)for(i=0,o=0;i<t.length;i+=3)s=t[i]|t[i+1]<<8|t[i+2]<<16,this.words[o]|=s<<a&67108863,this.words[o+1]=s>>>26-a&67108863,(a+=24)>=26&&(a-=26,o++);return this._strip()},i.prototype._parseHex=function(t,e,r){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var i,o=0,s=0;if("be"===r)for(n=t.length-1;n>=e;n-=2)i=a(t,e,n)<<o,this.words[s]|=67108863&i,o>=18?(o-=18,s+=1,this.words[s]|=i>>>26):o+=8;else for(n=(t.length-e)%2==0?e+1:e;n<t.length;n+=2)i=a(t,e,n)<<o,this.words[s]|=67108863&i,o>=18?(o-=18,s+=1,this.words[s]|=i>>>26):o+=8;this._strip()},i.prototype._parseBase=function(t,e,r){this.words=[0],this.length=1;for(var n=0,i=1;i<=67108863;i*=e)n++;n--,i=i/e|0;for(var o=t.length-r,s=o%n,a=Math.min(o,o-s)+r,h=0,l=r;l<a;l+=n)h=u(t,l,l+n,e),this.imuln(i),this.words[0]+h<67108864?this.words[0]+=h:this._iaddn(h);if(0!==s){var d=1;for(h=u(t,l,t.length,e),l=0;l<s;l++)d*=e;this.imuln(d),this.words[0]+h<67108864?this.words[0]+=h:this._iaddn(h)}this._strip()},i.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},i.prototype._move=function(t){h(t,this)},i.prototype.clone=function(){var t=new i(null);return this.copy(t),t},i.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},i.prototype._strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},i.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},"undefined"!=typeof Symbol&&"function"==typeof Symbol.for)try{i.prototype[Symbol.for("nodejs.util.inspect.custom")]=c}catch(t){i.prototype.inspect=c}else i.prototype.inspect=c;function c(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var m=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],p=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],f=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function g(t,e,r){r.negative=e.negative^t.negative;var n=t.length+e.length|0;r.length=n,n=n-1|0;var i=0|t.words[0],o=0|e.words[0],s=i*o,a=67108863&s,u=s/67108864|0;r.words[0]=a;for(var h=1;h<n;h++){for(var l=u>>>26,d=67108863&u,c=Math.min(h,e.length-1),m=Math.max(0,h-t.length+1);m<=c;m++){var p=h-m|0;l+=(s=(i=0|t.words[p])*(o=0|e.words[m])+d)/67108864|0,d=67108863&s}r.words[h]=0|d,u=0|l}return 0!==u?r.words[h]=0|u:r.length--,r._strip()}i.prototype.toString=function(t,e){var n;if(e=0|e||1,16===(t=t||10)||"hex"===t){n="";for(var i=0,o=0,s=0;s<this.length;s++){var a=this.words[s],u=(16777215&(a<<i|o)).toString(16);o=a>>>24-i&16777215,(i+=2)>=26&&(i-=26,s--),n=0!==o||s!==this.length-1?m[6-u.length]+u+n:u+n}for(0!==o&&(n=o.toString(16)+n);n.length%e!=0;)n="0"+n;return 0!==this.negative&&(n="-"+n),n}if(t===(0|t)&&t>=2&&t<=36){var h=p[t],l=f[t];n="";var d=this.clone();for(d.negative=0;!d.isZero();){var c=d.modrn(l).toString(t);n=(d=d.idivn(l)).isZero()?c+n:m[h-c.length]+c+n}for(this.isZero()&&(n="0"+n);n.length%e!=0;)n="0"+n;return 0!==this.negative&&(n="-"+n),n}r(0,"Base should be between 2 and 36")},i.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&r(0,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},i.prototype.toJSON=function(){return this.toString(16,2)},o&&(i.prototype.toBuffer=function(t,e){return this.toArrayLike(o,t,e)}),i.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},i.prototype.toArrayLike=function(t,e,n){this._strip();var i=this.byteLength(),o=n||Math.max(1,i);r(i<=o,"byte array longer than desired length"),r(o>0,"Requested array length <= 0");var s=function(t,e){return t.allocUnsafe?t.allocUnsafe(e):new t(e)}(t,o);return this["_toArrayLike"+("le"===e?"LE":"BE")](s,i),s},i.prototype._toArrayLikeLE=function(t){for(var e=0,r=0,n=0,i=0;n<this.length;n++){var o=this.words[n]<<i|r;t[e++]=255&o,e<t.length&&(t[e++]=o>>8&255),e<t.length&&(t[e++]=o>>16&255),6===i?(e<t.length&&(t[e++]=o>>24&255),r=0,i=0):(r=o>>>24,i+=2)}if(e<t.length)for(t[e++]=r;e<t.length;)t[e++]=0},i.prototype._toArrayLikeBE=function(t){for(var e=t.length-1,r=0,n=0,i=0;n<this.length;n++){var o=this.words[n]<<i|r;t[e--]=255&o,e>=0&&(t[e--]=o>>8&255),e>=0&&(t[e--]=o>>16&255),6===i?(e>=0&&(t[e--]=o>>24&255),r=0,i=0):(r=o>>>24,i+=2)}if(e>=0)for(t[e--]=r;e>=0;)t[e--]=0},Math.clz32?i.prototype._countBits=function(t){return 32-Math.clz32(t)}:i.prototype._countBits=function(t){var e=t,r=0;return e>=4096&&(r+=13,e>>>=13),e>=64&&(r+=7,e>>>=7),e>=8&&(r+=4,e>>>=4),e>=2&&(r+=2,e>>>=2),r+e},i.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,r=0;return 0==(8191&e)&&(r+=13,e>>>=13),0==(127&e)&&(r+=7,e>>>=7),0==(15&e)&&(r+=4,e>>>=4),0==(3&e)&&(r+=2,e>>>=2),0==(1&e)&&r++,r},i.prototype.bitLength=function(){var t=this.words[this.length-1],e=this._countBits(t);return 26*(this.length-1)+e},i.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var r=this._zeroBits(this.words[e]);if(t+=r,26!==r)break}return t},i.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},i.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},i.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},i.prototype.isNeg=function(){return 0!==this.negative},i.prototype.neg=function(){return this.clone().ineg()},i.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},i.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this._strip()},i.prototype.ior=function(t){return r(0==(this.negative|t.negative)),this.iuor(t)},i.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},i.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},i.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var r=0;r<e.length;r++)this.words[r]=this.words[r]&t.words[r];return this.length=e.length,this._strip()},i.prototype.iand=function(t){return r(0==(this.negative|t.negative)),this.iuand(t)},i.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},i.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},i.prototype.iuxor=function(t){var e,r;this.length>t.length?(e=this,r=t):(e=t,r=this);for(var n=0;n<r.length;n++)this.words[n]=e.words[n]^r.words[n];if(this!==e)for(;n<e.length;n++)this.words[n]=e.words[n];return this.length=e.length,this._strip()},i.prototype.ixor=function(t){return r(0==(this.negative|t.negative)),this.iuxor(t)},i.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},i.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},i.prototype.inotn=function(t){r("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),n=t%26;this._expand(e),n>0&&e--;for(var i=0;i<e;i++)this.words[i]=67108863&~this.words[i];return n>0&&(this.words[i]=~this.words[i]&67108863>>26-n),this._strip()},i.prototype.notn=function(t){return this.clone().inotn(t)},i.prototype.setn=function(t,e){r("number"==typeof t&&t>=0);var n=t/26|0,i=t%26;return this._expand(n+1),this.words[n]=e?this.words[n]|1<<i:this.words[n]&~(1<<i),this._strip()},i.prototype.iadd=function(t){var e,r,n;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(r=this,n=t):(r=t,n=this);for(var i=0,o=0;o<n.length;o++)e=(0|r.words[o])+(0|n.words[o])+i,this.words[o]=67108863&e,i=e>>>26;for(;0!==i&&o<r.length;o++)e=(0|r.words[o])+i,this.words[o]=67108863&e,i=e>>>26;if(this.length=r.length,0!==i)this.words[this.length]=i,this.length++;else if(r!==this)for(;o<r.length;o++)this.words[o]=r.words[o];return this},i.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},i.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var r,n,i=this.cmp(t);if(0===i)return this.negative=0,this.length=1,this.words[0]=0,this;i>0?(r=this,n=t):(r=t,n=this);for(var o=0,s=0;s<n.length;s++)o=(e=(0|r.words[s])-(0|n.words[s])+o)>>26,this.words[s]=67108863&e;for(;0!==o&&s<r.length;s++)o=(e=(0|r.words[s])+o)>>26,this.words[s]=67108863&e;if(0===o&&s<r.length&&r!==this)for(;s<r.length;s++)this.words[s]=r.words[s];return this.length=Math.max(this.length,s),r!==this&&(this.negative=1),this._strip()},i.prototype.sub=function(t){return this.clone().isub(t)};var y=function(t,e,r){var n,i,o,s=t.words,a=e.words,u=r.words,h=0,l=0|s[0],d=8191&l,c=l>>>13,m=0|s[1],p=8191&m,f=m>>>13,g=0|s[2],y=8191&g,w=g>>>13,v=0|s[3],M=8191&v,b=v>>>13,k=0|s[4],P=8191&k,S=k>>>13,A=0|s[5],T=8191&A,x=A>>>13,_=0|s[6],I=8191&_,E=_>>>13,C=0|s[7],L=8191&C,B=C>>>13,O=0|s[8],R=8191&O,D=O>>>13,K=0|s[9],U=8191&K,W=K>>>13,q=0|a[0],N=8191&q,j=q>>>13,V=0|a[1],Z=8191&V,z=V>>>13,F=0|a[2],G=8191&F,Q=F>>>13,$=0|a[3],H=8191&$,Y=$>>>13,J=0|a[4],X=8191&J,tt=J>>>13,et=0|a[5],rt=8191&et,nt=et>>>13,it=0|a[6],ot=8191&it,st=it>>>13,at=0|a[7],ut=8191&at,ht=at>>>13,lt=0|a[8],dt=8191&lt,ct=lt>>>13,mt=0|a[9],pt=8191&mt,ft=mt>>>13;r.negative=t.negative^e.negative,r.length=19;var gt=(h+(n=Math.imul(d,N))|0)+((8191&(i=(i=Math.imul(d,j))+Math.imul(c,N)|0))<<13)|0;h=((o=Math.imul(c,j))+(i>>>13)|0)+(gt>>>26)|0,gt&=67108863,n=Math.imul(p,N),i=(i=Math.imul(p,j))+Math.imul(f,N)|0,o=Math.imul(f,j);var yt=(h+(n=n+Math.imul(d,Z)|0)|0)+((8191&(i=(i=i+Math.imul(d,z)|0)+Math.imul(c,Z)|0))<<13)|0;h=((o=o+Math.imul(c,z)|0)+(i>>>13)|0)+(yt>>>26)|0,yt&=67108863,n=Math.imul(y,N),i=(i=Math.imul(y,j))+Math.imul(w,N)|0,o=Math.imul(w,j),n=n+Math.imul(p,Z)|0,i=(i=i+Math.imul(p,z)|0)+Math.imul(f,Z)|0,o=o+Math.imul(f,z)|0;var wt=(h+(n=n+Math.imul(d,G)|0)|0)+((8191&(i=(i=i+Math.imul(d,Q)|0)+Math.imul(c,G)|0))<<13)|0;h=((o=o+Math.imul(c,Q)|0)+(i>>>13)|0)+(wt>>>26)|0,wt&=67108863,n=Math.imul(M,N),i=(i=Math.imul(M,j))+Math.imul(b,N)|0,o=Math.imul(b,j),n=n+Math.imul(y,Z)|0,i=(i=i+Math.imul(y,z)|0)+Math.imul(w,Z)|0,o=o+Math.imul(w,z)|0,n=n+Math.imul(p,G)|0,i=(i=i+Math.imul(p,Q)|0)+Math.imul(f,G)|0,o=o+Math.imul(f,Q)|0;var vt=(h+(n=n+Math.imul(d,H)|0)|0)+((8191&(i=(i=i+Math.imul(d,Y)|0)+Math.imul(c,H)|0))<<13)|0;h=((o=o+Math.imul(c,Y)|0)+(i>>>13)|0)+(vt>>>26)|0,vt&=67108863,n=Math.imul(P,N),i=(i=Math.imul(P,j))+Math.imul(S,N)|0,o=Math.imul(S,j),n=n+Math.imul(M,Z)|0,i=(i=i+Math.imul(M,z)|0)+Math.imul(b,Z)|0,o=o+Math.imul(b,z)|0,n=n+Math.imul(y,G)|0,i=(i=i+Math.imul(y,Q)|0)+Math.imul(w,G)|0,o=o+Math.imul(w,Q)|0,n=n+Math.imul(p,H)|0,i=(i=i+Math.imul(p,Y)|0)+Math.imul(f,H)|0,o=o+Math.imul(f,Y)|0;var Mt=(h+(n=n+Math.imul(d,X)|0)|0)+((8191&(i=(i=i+Math.imul(d,tt)|0)+Math.imul(c,X)|0))<<13)|0;h=((o=o+Math.imul(c,tt)|0)+(i>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,n=Math.imul(T,N),i=(i=Math.imul(T,j))+Math.imul(x,N)|0,o=Math.imul(x,j),n=n+Math.imul(P,Z)|0,i=(i=i+Math.imul(P,z)|0)+Math.imul(S,Z)|0,o=o+Math.imul(S,z)|0,n=n+Math.imul(M,G)|0,i=(i=i+Math.imul(M,Q)|0)+Math.imul(b,G)|0,o=o+Math.imul(b,Q)|0,n=n+Math.imul(y,H)|0,i=(i=i+Math.imul(y,Y)|0)+Math.imul(w,H)|0,o=o+Math.imul(w,Y)|0,n=n+Math.imul(p,X)|0,i=(i=i+Math.imul(p,tt)|0)+Math.imul(f,X)|0,o=o+Math.imul(f,tt)|0;var bt=(h+(n=n+Math.imul(d,rt)|0)|0)+((8191&(i=(i=i+Math.imul(d,nt)|0)+Math.imul(c,rt)|0))<<13)|0;h=((o=o+Math.imul(c,nt)|0)+(i>>>13)|0)+(bt>>>26)|0,bt&=67108863,n=Math.imul(I,N),i=(i=Math.imul(I,j))+Math.imul(E,N)|0,o=Math.imul(E,j),n=n+Math.imul(T,Z)|0,i=(i=i+Math.imul(T,z)|0)+Math.imul(x,Z)|0,o=o+Math.imul(x,z)|0,n=n+Math.imul(P,G)|0,i=(i=i+Math.imul(P,Q)|0)+Math.imul(S,G)|0,o=o+Math.imul(S,Q)|0,n=n+Math.imul(M,H)|0,i=(i=i+Math.imul(M,Y)|0)+Math.imul(b,H)|0,o=o+Math.imul(b,Y)|0,n=n+Math.imul(y,X)|0,i=(i=i+Math.imul(y,tt)|0)+Math.imul(w,X)|0,o=o+Math.imul(w,tt)|0,n=n+Math.imul(p,rt)|0,i=(i=i+Math.imul(p,nt)|0)+Math.imul(f,rt)|0,o=o+Math.imul(f,nt)|0;var kt=(h+(n=n+Math.imul(d,ot)|0)|0)+((8191&(i=(i=i+Math.imul(d,st)|0)+Math.imul(c,ot)|0))<<13)|0;h=((o=o+Math.imul(c,st)|0)+(i>>>13)|0)+(kt>>>26)|0,kt&=67108863,n=Math.imul(L,N),i=(i=Math.imul(L,j))+Math.imul(B,N)|0,o=Math.imul(B,j),n=n+Math.imul(I,Z)|0,i=(i=i+Math.imul(I,z)|0)+Math.imul(E,Z)|0,o=o+Math.imul(E,z)|0,n=n+Math.imul(T,G)|0,i=(i=i+Math.imul(T,Q)|0)+Math.imul(x,G)|0,o=o+Math.imul(x,Q)|0,n=n+Math.imul(P,H)|0,i=(i=i+Math.imul(P,Y)|0)+Math.imul(S,H)|0,o=o+Math.imul(S,Y)|0,n=n+Math.imul(M,X)|0,i=(i=i+Math.imul(M,tt)|0)+Math.imul(b,X)|0,o=o+Math.imul(b,tt)|0,n=n+Math.imul(y,rt)|0,i=(i=i+Math.imul(y,nt)|0)+Math.imul(w,rt)|0,o=o+Math.imul(w,nt)|0,n=n+Math.imul(p,ot)|0,i=(i=i+Math.imul(p,st)|0)+Math.imul(f,ot)|0,o=o+Math.imul(f,st)|0;var Pt=(h+(n=n+Math.imul(d,ut)|0)|0)+((8191&(i=(i=i+Math.imul(d,ht)|0)+Math.imul(c,ut)|0))<<13)|0;h=((o=o+Math.imul(c,ht)|0)+(i>>>13)|0)+(Pt>>>26)|0,Pt&=67108863,n=Math.imul(R,N),i=(i=Math.imul(R,j))+Math.imul(D,N)|0,o=Math.imul(D,j),n=n+Math.imul(L,Z)|0,i=(i=i+Math.imul(L,z)|0)+Math.imul(B,Z)|0,o=o+Math.imul(B,z)|0,n=n+Math.imul(I,G)|0,i=(i=i+Math.imul(I,Q)|0)+Math.imul(E,G)|0,o=o+Math.imul(E,Q)|0,n=n+Math.imul(T,H)|0,i=(i=i+Math.imul(T,Y)|0)+Math.imul(x,H)|0,o=o+Math.imul(x,Y)|0,n=n+Math.imul(P,X)|0,i=(i=i+Math.imul(P,tt)|0)+Math.imul(S,X)|0,o=o+Math.imul(S,tt)|0,n=n+Math.imul(M,rt)|0,i=(i=i+Math.imul(M,nt)|0)+Math.imul(b,rt)|0,o=o+Math.imul(b,nt)|0,n=n+Math.imul(y,ot)|0,i=(i=i+Math.imul(y,st)|0)+Math.imul(w,ot)|0,o=o+Math.imul(w,st)|0,n=n+Math.imul(p,ut)|0,i=(i=i+Math.imul(p,ht)|0)+Math.imul(f,ut)|0,o=o+Math.imul(f,ht)|0;var St=(h+(n=n+Math.imul(d,dt)|0)|0)+((8191&(i=(i=i+Math.imul(d,ct)|0)+Math.imul(c,dt)|0))<<13)|0;h=((o=o+Math.imul(c,ct)|0)+(i>>>13)|0)+(St>>>26)|0,St&=67108863,n=Math.imul(U,N),i=(i=Math.imul(U,j))+Math.imul(W,N)|0,o=Math.imul(W,j),n=n+Math.imul(R,Z)|0,i=(i=i+Math.imul(R,z)|0)+Math.imul(D,Z)|0,o=o+Math.imul(D,z)|0,n=n+Math.imul(L,G)|0,i=(i=i+Math.imul(L,Q)|0)+Math.imul(B,G)|0,o=o+Math.imul(B,Q)|0,n=n+Math.imul(I,H)|0,i=(i=i+Math.imul(I,Y)|0)+Math.imul(E,H)|0,o=o+Math.imul(E,Y)|0,n=n+Math.imul(T,X)|0,i=(i=i+Math.imul(T,tt)|0)+Math.imul(x,X)|0,o=o+Math.imul(x,tt)|0,n=n+Math.imul(P,rt)|0,i=(i=i+Math.imul(P,nt)|0)+Math.imul(S,rt)|0,o=o+Math.imul(S,nt)|0,n=n+Math.imul(M,ot)|0,i=(i=i+Math.imul(M,st)|0)+Math.imul(b,ot)|0,o=o+Math.imul(b,st)|0,n=n+Math.imul(y,ut)|0,i=(i=i+Math.imul(y,ht)|0)+Math.imul(w,ut)|0,o=o+Math.imul(w,ht)|0,n=n+Math.imul(p,dt)|0,i=(i=i+Math.imul(p,ct)|0)+Math.imul(f,dt)|0,o=o+Math.imul(f,ct)|0;var At=(h+(n=n+Math.imul(d,pt)|0)|0)+((8191&(i=(i=i+Math.imul(d,ft)|0)+Math.imul(c,pt)|0))<<13)|0;h=((o=o+Math.imul(c,ft)|0)+(i>>>13)|0)+(At>>>26)|0,At&=67108863,n=Math.imul(U,Z),i=(i=Math.imul(U,z))+Math.imul(W,Z)|0,o=Math.imul(W,z),n=n+Math.imul(R,G)|0,i=(i=i+Math.imul(R,Q)|0)+Math.imul(D,G)|0,o=o+Math.imul(D,Q)|0,n=n+Math.imul(L,H)|0,i=(i=i+Math.imul(L,Y)|0)+Math.imul(B,H)|0,o=o+Math.imul(B,Y)|0,n=n+Math.imul(I,X)|0,i=(i=i+Math.imul(I,tt)|0)+Math.imul(E,X)|0,o=o+Math.imul(E,tt)|0,n=n+Math.imul(T,rt)|0,i=(i=i+Math.imul(T,nt)|0)+Math.imul(x,rt)|0,o=o+Math.imul(x,nt)|0,n=n+Math.imul(P,ot)|0,i=(i=i+Math.imul(P,st)|0)+Math.imul(S,ot)|0,o=o+Math.imul(S,st)|0,n=n+Math.imul(M,ut)|0,i=(i=i+Math.imul(M,ht)|0)+Math.imul(b,ut)|0,o=o+Math.imul(b,ht)|0,n=n+Math.imul(y,dt)|0,i=(i=i+Math.imul(y,ct)|0)+Math.imul(w,dt)|0,o=o+Math.imul(w,ct)|0;var Tt=(h+(n=n+Math.imul(p,pt)|0)|0)+((8191&(i=(i=i+Math.imul(p,ft)|0)+Math.imul(f,pt)|0))<<13)|0;h=((o=o+Math.imul(f,ft)|0)+(i>>>13)|0)+(Tt>>>26)|0,Tt&=67108863,n=Math.imul(U,G),i=(i=Math.imul(U,Q))+Math.imul(W,G)|0,o=Math.imul(W,Q),n=n+Math.imul(R,H)|0,i=(i=i+Math.imul(R,Y)|0)+Math.imul(D,H)|0,o=o+Math.imul(D,Y)|0,n=n+Math.imul(L,X)|0,i=(i=i+Math.imul(L,tt)|0)+Math.imul(B,X)|0,o=o+Math.imul(B,tt)|0,n=n+Math.imul(I,rt)|0,i=(i=i+Math.imul(I,nt)|0)+Math.imul(E,rt)|0,o=o+Math.imul(E,nt)|0,n=n+Math.imul(T,ot)|0,i=(i=i+Math.imul(T,st)|0)+Math.imul(x,ot)|0,o=o+Math.imul(x,st)|0,n=n+Math.imul(P,ut)|0,i=(i=i+Math.imul(P,ht)|0)+Math.imul(S,ut)|0,o=o+Math.imul(S,ht)|0,n=n+Math.imul(M,dt)|0,i=(i=i+Math.imul(M,ct)|0)+Math.imul(b,dt)|0,o=o+Math.imul(b,ct)|0;var xt=(h+(n=n+Math.imul(y,pt)|0)|0)+((8191&(i=(i=i+Math.imul(y,ft)|0)+Math.imul(w,pt)|0))<<13)|0;h=((o=o+Math.imul(w,ft)|0)+(i>>>13)|0)+(xt>>>26)|0,xt&=67108863,n=Math.imul(U,H),i=(i=Math.imul(U,Y))+Math.imul(W,H)|0,o=Math.imul(W,Y),n=n+Math.imul(R,X)|0,i=(i=i+Math.imul(R,tt)|0)+Math.imul(D,X)|0,o=o+Math.imul(D,tt)|0,n=n+Math.imul(L,rt)|0,i=(i=i+Math.imul(L,nt)|0)+Math.imul(B,rt)|0,o=o+Math.imul(B,nt)|0,n=n+Math.imul(I,ot)|0,i=(i=i+Math.imul(I,st)|0)+Math.imul(E,ot)|0,o=o+Math.imul(E,st)|0,n=n+Math.imul(T,ut)|0,i=(i=i+Math.imul(T,ht)|0)+Math.imul(x,ut)|0,o=o+Math.imul(x,ht)|0,n=n+Math.imul(P,dt)|0,i=(i=i+Math.imul(P,ct)|0)+Math.imul(S,dt)|0,o=o+Math.imul(S,ct)|0;var _t=(h+(n=n+Math.imul(M,pt)|0)|0)+((8191&(i=(i=i+Math.imul(M,ft)|0)+Math.imul(b,pt)|0))<<13)|0;h=((o=o+Math.imul(b,ft)|0)+(i>>>13)|0)+(_t>>>26)|0,_t&=67108863,n=Math.imul(U,X),i=(i=Math.imul(U,tt))+Math.imul(W,X)|0,o=Math.imul(W,tt),n=n+Math.imul(R,rt)|0,i=(i=i+Math.imul(R,nt)|0)+Math.imul(D,rt)|0,o=o+Math.imul(D,nt)|0,n=n+Math.imul(L,ot)|0,i=(i=i+Math.imul(L,st)|0)+Math.imul(B,ot)|0,o=o+Math.imul(B,st)|0,n=n+Math.imul(I,ut)|0,i=(i=i+Math.imul(I,ht)|0)+Math.imul(E,ut)|0,o=o+Math.imul(E,ht)|0,n=n+Math.imul(T,dt)|0,i=(i=i+Math.imul(T,ct)|0)+Math.imul(x,dt)|0,o=o+Math.imul(x,ct)|0;var It=(h+(n=n+Math.imul(P,pt)|0)|0)+((8191&(i=(i=i+Math.imul(P,ft)|0)+Math.imul(S,pt)|0))<<13)|0;h=((o=o+Math.imul(S,ft)|0)+(i>>>13)|0)+(It>>>26)|0,It&=67108863,n=Math.imul(U,rt),i=(i=Math.imul(U,nt))+Math.imul(W,rt)|0,o=Math.imul(W,nt),n=n+Math.imul(R,ot)|0,i=(i=i+Math.imul(R,st)|0)+Math.imul(D,ot)|0,o=o+Math.imul(D,st)|0,n=n+Math.imul(L,ut)|0,i=(i=i+Math.imul(L,ht)|0)+Math.imul(B,ut)|0,o=o+Math.imul(B,ht)|0,n=n+Math.imul(I,dt)|0,i=(i=i+Math.imul(I,ct)|0)+Math.imul(E,dt)|0,o=o+Math.imul(E,ct)|0;var Et=(h+(n=n+Math.imul(T,pt)|0)|0)+((8191&(i=(i=i+Math.imul(T,ft)|0)+Math.imul(x,pt)|0))<<13)|0;h=((o=o+Math.imul(x,ft)|0)+(i>>>13)|0)+(Et>>>26)|0,Et&=67108863,n=Math.imul(U,ot),i=(i=Math.imul(U,st))+Math.imul(W,ot)|0,o=Math.imul(W,st),n=n+Math.imul(R,ut)|0,i=(i=i+Math.imul(R,ht)|0)+Math.imul(D,ut)|0,o=o+Math.imul(D,ht)|0,n=n+Math.imul(L,dt)|0,i=(i=i+Math.imul(L,ct)|0)+Math.imul(B,dt)|0,o=o+Math.imul(B,ct)|0;var Ct=(h+(n=n+Math.imul(I,pt)|0)|0)+((8191&(i=(i=i+Math.imul(I,ft)|0)+Math.imul(E,pt)|0))<<13)|0;h=((o=o+Math.imul(E,ft)|0)+(i>>>13)|0)+(Ct>>>26)|0,Ct&=67108863,n=Math.imul(U,ut),i=(i=Math.imul(U,ht))+Math.imul(W,ut)|0,o=Math.imul(W,ht),n=n+Math.imul(R,dt)|0,i=(i=i+Math.imul(R,ct)|0)+Math.imul(D,dt)|0,o=o+Math.imul(D,ct)|0;var Lt=(h+(n=n+Math.imul(L,pt)|0)|0)+((8191&(i=(i=i+Math.imul(L,ft)|0)+Math.imul(B,pt)|0))<<13)|0;h=((o=o+Math.imul(B,ft)|0)+(i>>>13)|0)+(Lt>>>26)|0,Lt&=67108863,n=Math.imul(U,dt),i=(i=Math.imul(U,ct))+Math.imul(W,dt)|0,o=Math.imul(W,ct);var Bt=(h+(n=n+Math.imul(R,pt)|0)|0)+((8191&(i=(i=i+Math.imul(R,ft)|0)+Math.imul(D,pt)|0))<<13)|0;h=((o=o+Math.imul(D,ft)|0)+(i>>>13)|0)+(Bt>>>26)|0,Bt&=67108863;var Ot=(h+(n=Math.imul(U,pt))|0)+((8191&(i=(i=Math.imul(U,ft))+Math.imul(W,pt)|0))<<13)|0;return h=((o=Math.imul(W,ft))+(i>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,u[0]=gt,u[1]=yt,u[2]=wt,u[3]=vt,u[4]=Mt,u[5]=bt,u[6]=kt,u[7]=Pt,u[8]=St,u[9]=At,u[10]=Tt,u[11]=xt,u[12]=_t,u[13]=It,u[14]=Et,u[15]=Ct,u[16]=Lt,u[17]=Bt,u[18]=Ot,0!==h&&(u[19]=h,r.length++),r};function w(t,e,r){r.negative=e.negative^t.negative,r.length=t.length+e.length;for(var n=0,i=0,o=0;o<r.length-1;o++){var s=i;i=0;for(var a=67108863&n,u=Math.min(o,e.length-1),h=Math.max(0,o-t.length+1);h<=u;h++){var l=o-h,d=(0|t.words[l])*(0|e.words[h]),c=67108863&d;a=67108863&(c=c+a|0),i+=(s=(s=s+(d/67108864|0)|0)+(c>>>26)|0)>>>26,s&=67108863}r.words[o]=a,n=s,s=i}return 0!==n?r.words[o]=n:r.length--,r._strip()}function v(t,e,r){return w(t,e,r)}Math.imul||(y=g),i.prototype.mulTo=function(t,e){var r=this.length+t.length;return 10===this.length&&10===t.length?y(this,t,e):r<63?g(this,t,e):r<1024?w(this,t,e):v(this,t,e)},i.prototype.mul=function(t){var e=new i(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},i.prototype.mulf=function(t){var e=new i(null);return e.words=new Array(this.length+t.length),v(this,t,e)},i.prototype.imul=function(t){return this.clone().mulTo(t,this)},i.prototype.imuln=function(t){var e=t<0;e&&(t=-t),r("number"==typeof t),r(t<67108864);for(var n=0,i=0;i<this.length;i++){var o=(0|this.words[i])*t,s=(67108863&o)+(67108863&n);n>>=26,n+=o/67108864|0,n+=s>>>26,this.words[i]=67108863&s}return 0!==n&&(this.words[i]=n,this.length++),e?this.ineg():this},i.prototype.muln=function(t){return this.clone().imuln(t)},i.prototype.sqr=function(){return this.mul(this)},i.prototype.isqr=function(){return this.imul(this.clone())},i.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),r=0;r<e.length;r++){var n=r/26|0,i=r%26;e[r]=t.words[n]>>>i&1}return e}(t);if(0===e.length)return new i(1);for(var r=this,n=0;n<e.length&&0===e[n];n++,r=r.sqr());if(++n<e.length)for(var o=r.sqr();n<e.length;n++,o=o.sqr())0!==e[n]&&(r=r.mul(o));return r},i.prototype.iushln=function(t){r("number"==typeof t&&t>=0);var e,n=t%26,i=(t-n)/26,o=67108863>>>26-n<<26-n;if(0!==n){var s=0;for(e=0;e<this.length;e++){var a=this.words[e]&o,u=(0|this.words[e])-a<<n;this.words[e]=u|s,s=a>>>26-n}s&&(this.words[e]=s,this.length++)}if(0!==i){for(e=this.length-1;e>=0;e--)this.words[e+i]=this.words[e];for(e=0;e<i;e++)this.words[e]=0;this.length+=i}return this._strip()},i.prototype.ishln=function(t){return r(0===this.negative),this.iushln(t)},i.prototype.iushrn=function(t,e,n){var i;r("number"==typeof t&&t>=0),i=e?(e-e%26)/26:0;var o=t%26,s=Math.min((t-o)/26,this.length),a=67108863^67108863>>>o<<o,u=n;if(i-=s,i=Math.max(0,i),u){for(var h=0;h<s;h++)u.words[h]=this.words[h];u.length=s}if(0===s);else if(this.length>s)for(this.length-=s,h=0;h<this.length;h++)this.words[h]=this.words[h+s];else this.words[0]=0,this.length=1;var l=0;for(h=this.length-1;h>=0&&(0!==l||h>=i);h--){var d=0|this.words[h];this.words[h]=l<<26-o|d>>>o,l=d&a}return u&&0!==l&&(u.words[u.length++]=l),0===this.length&&(this.words[0]=0,this.length=1),this._strip()},i.prototype.ishrn=function(t,e,n){return r(0===this.negative),this.iushrn(t,e,n)},i.prototype.shln=function(t){return this.clone().ishln(t)},i.prototype.ushln=function(t){return this.clone().iushln(t)},i.prototype.shrn=function(t){return this.clone().ishrn(t)},i.prototype.ushrn=function(t){return this.clone().iushrn(t)},i.prototype.testn=function(t){r("number"==typeof t&&t>=0);var e=t%26,n=(t-e)/26,i=1<<e;return this.length<=n?0:!!(this.words[n]&i)},i.prototype.imaskn=function(t){r("number"==typeof t&&t>=0);var e=t%26,n=(t-e)/26;if(r(0===this.negative,"imaskn works only with positive numbers"),this.length<=n)return this;if(0!==e&&n++,this.length=Math.min(n,this.length),0!==e){var i=67108863^67108863>>>e<<e;this.words[this.length-1]&=i}return this._strip()},i.prototype.maskn=function(t){return this.clone().imaskn(t)},i.prototype.iaddn=function(t){return r("number"==typeof t),r(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<=t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},i.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},i.prototype.isubn=function(t){if(r("number"==typeof t),r(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this._strip()},i.prototype.addn=function(t){return this.clone().iaddn(t)},i.prototype.subn=function(t){return this.clone().isubn(t)},i.prototype.iabs=function(){return this.negative=0,this},i.prototype.abs=function(){return this.clone().iabs()},i.prototype._ishlnsubmul=function(t,e,n){var i,o,s=t.length+n;this._expand(s);var a=0;for(i=0;i<t.length;i++){o=(0|this.words[i+n])+a;var u=(0|t.words[i])*e;a=((o-=67108863&u)>>26)-(u/67108864|0),this.words[i+n]=67108863&o}for(;i<this.length-n;i++)a=(o=(0|this.words[i+n])+a)>>26,this.words[i+n]=67108863&o;if(0===a)return this._strip();for(r(-1===a),a=0,i=0;i<this.length;i++)a=(o=-(0|this.words[i])+a)>>26,this.words[i]=67108863&o;return this.negative=1,this._strip()},i.prototype._wordDiv=function(t,e){var r=(this.length,t.length),n=this.clone(),o=t,s=0|o.words[o.length-1];0!=(r=26-this._countBits(s))&&(o=o.ushln(r),n.iushln(r),s=0|o.words[o.length-1]);var a,u=n.length-o.length;if("mod"!==e){(a=new i(null)).length=u+1,a.words=new Array(a.length);for(var h=0;h<a.length;h++)a.words[h]=0}var l=n.clone()._ishlnsubmul(o,1,u);0===l.negative&&(n=l,a&&(a.words[u]=1));for(var d=u-1;d>=0;d--){var c=67108864*(0|n.words[o.length+d])+(0|n.words[o.length+d-1]);for(c=Math.min(c/s|0,67108863),n._ishlnsubmul(o,c,d);0!==n.negative;)c--,n.negative=0,n._ishlnsubmul(o,1,d),n.isZero()||(n.negative^=1);a&&(a.words[d]=c)}return a&&a._strip(),n._strip(),"div"!==e&&0!==r&&n.iushrn(r),{div:a||null,mod:n}},i.prototype.divmod=function(t,e,n){return r(!t.isZero()),this.isZero()?{div:new i(0),mod:new i(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(o=a.div.neg()),"div"!==e&&(s=a.mod.neg(),n&&0!==s.negative&&s.iadd(t)),{div:o,mod:s}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(o=a.div.neg()),{div:o,mod:a.mod}):0!=(this.negative&t.negative)?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(s=a.mod.neg(),n&&0!==s.negative&&s.isub(t)),{div:a.div,mod:s}):t.length>this.length||this.cmp(t)<0?{div:new i(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new i(this.modrn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new i(this.modrn(t.words[0]))}:this._wordDiv(t,e);var o,s,a},i.prototype.div=function(t){return this.divmod(t,"div",0).div},i.prototype.mod=function(t){return this.divmod(t,"mod",0).mod},i.prototype.umod=function(t){return this.divmod(t,"mod",1).mod},i.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var r=0!==e.div.negative?e.mod.isub(t):e.mod,n=t.ushrn(1),i=t.andln(1),o=r.cmp(n);return o<0||1===i&&0===o?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},i.prototype.modrn=function(t){var e=t<0;e&&(t=-t),r(t<=67108863);for(var n=(1<<26)%t,i=0,o=this.length-1;o>=0;o--)i=(n*i+(0|this.words[o]))%t;return e?-i:i},i.prototype.modn=function(t){return this.modrn(t)},i.prototype.idivn=function(t){var e=t<0;e&&(t=-t),r(t<=67108863);for(var n=0,i=this.length-1;i>=0;i--){var o=(0|this.words[i])+67108864*n;this.words[i]=o/t|0,n=o%t}return this._strip(),e?this.ineg():this},i.prototype.divn=function(t){return this.clone().idivn(t)},i.prototype.egcd=function(t){r(0===t.negative),r(!t.isZero());var e=this,n=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var o=new i(1),s=new i(0),a=new i(0),u=new i(1),h=0;e.isEven()&&n.isEven();)e.iushrn(1),n.iushrn(1),++h;for(var l=n.clone(),d=e.clone();!e.isZero();){for(var c=0,m=1;0==(e.words[0]&m)&&c<26;++c,m<<=1);if(c>0)for(e.iushrn(c);c-- >0;)(o.isOdd()||s.isOdd())&&(o.iadd(l),s.isub(d)),o.iushrn(1),s.iushrn(1);for(var p=0,f=1;0==(n.words[0]&f)&&p<26;++p,f<<=1);if(p>0)for(n.iushrn(p);p-- >0;)(a.isOdd()||u.isOdd())&&(a.iadd(l),u.isub(d)),a.iushrn(1),u.iushrn(1);e.cmp(n)>=0?(e.isub(n),o.isub(a),s.isub(u)):(n.isub(e),a.isub(o),u.isub(s))}return{a:a,b:u,gcd:n.iushln(h)}},i.prototype._invmp=function(t){r(0===t.negative),r(!t.isZero());var e=this,n=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var o,s=new i(1),a=new i(0),u=n.clone();e.cmpn(1)>0&&n.cmpn(1)>0;){for(var h=0,l=1;0==(e.words[0]&l)&&h<26;++h,l<<=1);if(h>0)for(e.iushrn(h);h-- >0;)s.isOdd()&&s.iadd(u),s.iushrn(1);for(var d=0,c=1;0==(n.words[0]&c)&&d<26;++d,c<<=1);if(d>0)for(n.iushrn(d);d-- >0;)a.isOdd()&&a.iadd(u),a.iushrn(1);e.cmp(n)>=0?(e.isub(n),s.isub(a)):(n.isub(e),a.isub(s))}return(o=0===e.cmpn(1)?s:a).cmpn(0)<0&&o.iadd(t),o},i.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),r=t.clone();e.negative=0,r.negative=0;for(var n=0;e.isEven()&&r.isEven();n++)e.iushrn(1),r.iushrn(1);for(;;){for(;e.isEven();)e.iushrn(1);for(;r.isEven();)r.iushrn(1);var i=e.cmp(r);if(i<0){var o=e;e=r,r=o}else if(0===i||0===r.cmpn(1))break;e.isub(r)}return r.iushln(n)},i.prototype.invm=function(t){return this.egcd(t).a.umod(t)},i.prototype.isEven=function(){return 0==(1&this.words[0])},i.prototype.isOdd=function(){return 1==(1&this.words[0])},i.prototype.andln=function(t){return this.words[0]&t},i.prototype.bincn=function(t){r("number"==typeof t);var e=t%26,n=(t-e)/26,i=1<<e;if(this.length<=n)return this._expand(n+1),this.words[n]|=i,this;for(var o=i,s=n;0!==o&&s<this.length;s++){var a=0|this.words[s];o=(a+=o)>>>26,a&=67108863,this.words[s]=a}return 0!==o&&(this.words[s]=o,this.length++),this},i.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},i.prototype.cmpn=function(t){var e,n=t<0;if(0!==this.negative&&!n)return-1;if(0===this.negative&&n)return 1;if(this._strip(),this.length>1)e=1;else{n&&(t=-t),r(t<=67108863,"Number is too big");var i=0|this.words[0];e=i===t?0:i<t?-1:1}return 0!==this.negative?0|-e:e},i.prototype.cmp=function(t){if(0!==this.negative&&0===t.negative)return-1;if(0===this.negative&&0!==t.negative)return 1;var e=this.ucmp(t);return 0!==this.negative?0|-e:e},i.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,r=this.length-1;r>=0;r--){var n=0|this.words[r],i=0|t.words[r];if(n!==i){n<i?e=-1:n>i&&(e=1);break}}return e},i.prototype.gtn=function(t){return 1===this.cmpn(t)},i.prototype.gt=function(t){return 1===this.cmp(t)},i.prototype.gten=function(t){return this.cmpn(t)>=0},i.prototype.gte=function(t){return this.cmp(t)>=0},i.prototype.ltn=function(t){return-1===this.cmpn(t)},i.prototype.lt=function(t){return-1===this.cmp(t)},i.prototype.lten=function(t){return this.cmpn(t)<=0},i.prototype.lte=function(t){return this.cmp(t)<=0},i.prototype.eqn=function(t){return 0===this.cmpn(t)},i.prototype.eq=function(t){return 0===this.cmp(t)},i.red=function(t){return new T(t)},i.prototype.toRed=function(t){return r(!this.red,"Already a number in reduction context"),r(0===this.negative,"red works only with positives"),t.convertTo(this)._forceRed(t)},i.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},i.prototype._forceRed=function(t){return this.red=t,this},i.prototype.forceRed=function(t){return r(!this.red,"Already a number in reduction context"),this._forceRed(t)},i.prototype.redAdd=function(t){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},i.prototype.redIAdd=function(t){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},i.prototype.redSub=function(t){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},i.prototype.redISub=function(t){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},i.prototype.redShl=function(t){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},i.prototype.redMul=function(t){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},i.prototype.redIMul=function(t){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},i.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},i.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},i.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},i.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},i.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},i.prototype.redPow=function(t){return r(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var M={k256:null,p224:null,p192:null,p25519:null};function b(t,e){this.name=t,this.p=new i(e,16),this.n=this.p.bitLength(),this.k=new i(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function k(){b.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function P(){b.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function S(){b.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function A(){b.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function T(t){if("string"==typeof t){var e=i._prime(t);this.m=e.p,this.prime=e}else r(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}function x(t){T.call(this,t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new i(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}b.prototype._tmp=function(){var t=new i(null);return t.words=new Array(Math.ceil(this.n/13)),t},b.prototype.ireduce=function(t){var e,r=t;do{this.split(r,this.tmp),e=(r=(r=this.imulK(r)).iadd(this.tmp)).bitLength()}while(e>this.n);var n=e<this.n?-1:r.ucmp(this.p);return 0===n?(r.words[0]=0,r.length=1):n>0?r.isub(this.p):void 0!==r.strip?r.strip():r._strip(),r},b.prototype.split=function(t,e){t.iushrn(this.n,0,e)},b.prototype.imulK=function(t){return t.imul(this.k)},n(k,b),k.prototype.split=function(t,e){for(var r=4194303,n=Math.min(t.length,9),i=0;i<n;i++)e.words[i]=t.words[i];if(e.length=n,t.length<=9)return t.words[0]=0,void(t.length=1);var o=t.words[9];for(e.words[e.length++]=o&r,i=10;i<t.length;i++){var s=0|t.words[i];t.words[i-10]=(s&r)<<4|o>>>22,o=s}o>>>=22,t.words[i-10]=o,0===o&&t.length>10?t.length-=10:t.length-=9},k.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,r=0;r<t.length;r++){var n=0|t.words[r];e+=977*n,t.words[r]=67108863&e,e=64*n+(e/67108864|0)}return 0===t.words[t.length-1]&&(t.length--,0===t.words[t.length-1]&&t.length--),t},n(P,b),n(S,b),n(A,b),A.prototype.imulK=function(t){for(var e=0,r=0;r<t.length;r++){var n=19*(0|t.words[r])+e,i=67108863&n;n>>>=26,t.words[r]=i,e=n}return 0!==e&&(t.words[t.length++]=e),t},i._prime=function(t){if(M[t])return M[t];var e;if("k256"===t)e=new k;else if("p224"===t)e=new P;else if("p192"===t)e=new S;else{if("p25519"!==t)throw new Error("Unknown prime "+t);e=new A}return M[t]=e,e},T.prototype._verify1=function(t){r(0===t.negative,"red works only with positives"),r(t.red,"red works only with red numbers")},T.prototype._verify2=function(t,e){r(0==(t.negative|e.negative),"red works only with positives"),r(t.red&&t.red===e.red,"red works only with red numbers")},T.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):(h(t,t.umod(this.m)._forceRed(this)),t)},T.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},T.prototype.add=function(t,e){this._verify2(t,e);var r=t.add(e);return r.cmp(this.m)>=0&&r.isub(this.m),r._forceRed(this)},T.prototype.iadd=function(t,e){this._verify2(t,e);var r=t.iadd(e);return r.cmp(this.m)>=0&&r.isub(this.m),r},T.prototype.sub=function(t,e){this._verify2(t,e);var r=t.sub(e);return r.cmpn(0)<0&&r.iadd(this.m),r._forceRed(this)},T.prototype.isub=function(t,e){this._verify2(t,e);var r=t.isub(e);return r.cmpn(0)<0&&r.iadd(this.m),r},T.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},T.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},T.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},T.prototype.isqr=function(t){return this.imul(t,t.clone())},T.prototype.sqr=function(t){return this.mul(t,t)},T.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(r(e%2==1),3===e){var n=this.m.add(new i(1)).iushrn(2);return this.pow(t,n)}for(var o=this.m.subn(1),s=0;!o.isZero()&&0===o.andln(1);)s++,o.iushrn(1);r(!o.isZero());var a=new i(1).toRed(this),u=a.redNeg(),h=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new i(2*l*l).toRed(this);0!==this.pow(l,h).cmp(u);)l.redIAdd(u);for(var d=this.pow(l,o),c=this.pow(t,o.addn(1).iushrn(1)),m=this.pow(t,o),p=s;0!==m.cmp(a);){for(var f=m,g=0;0!==f.cmp(a);g++)f=f.redSqr();r(g<p);var y=this.pow(d,new i(1).iushln(p-g-1));c=c.redMul(y),d=y.redSqr(),m=m.redMul(d),p=g}return c},T.prototype.invm=function(t){var e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},T.prototype.pow=function(t,e){if(e.isZero())return new i(1).toRed(this);if(0===e.cmpn(1))return t.clone();var r=new Array(16);r[0]=new i(1).toRed(this),r[1]=t;for(var n=2;n<r.length;n++)r[n]=this.mul(r[n-1],t);var o=r[0],s=0,a=0,u=e.bitLength()%26;for(0===u&&(u=26),n=e.length-1;n>=0;n--){for(var h=e.words[n],l=u-1;l>=0;l--){var d=h>>l&1;o!==r[0]&&(o=this.sqr(o)),0!==d||0!==s?(s<<=1,s|=d,(4==++a||0===n&&0===l)&&(o=this.mul(o,r[s]),a=0,s=0)):a=0}u=26}return o},T.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},T.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},i.mont=function(t){return new x(t)},n(x,T),x.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},x.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},x.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var r=t.imul(e),n=r.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),i=r.isub(n).iushrn(this.shift),o=i;return i.cmp(this.m)>=0?o=i.isub(this.m):i.cmpn(0)<0&&(o=i.iadd(this.m)),o._forceRed(this)},x.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new i(0)._forceRed(this);var r=t.mul(e),n=r.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),o=r.isub(n).iushrn(this.shift),s=o;return o.cmp(this.m)>=0?s=o.isub(this.m):o.cmpn(0)<0&&(s=o.iadd(this.m)),s._forceRed(this)},x.prototype.invm=function(t){return this.imod(t._invmp(this.m).mul(this.r2))._forceRed(this)}}(0,a);var c=l.exports,m=u(c),p={},f={};class g{constructor(t,e){if(!Number.isInteger(t))throw new TypeError("span must be an integer");this.span=t,this.property=e}makeDestinationObject(){return{}}decode(t,e){throw new Error("Layout is abstract")}encode(t,e,r){throw new Error("Layout is abstract")}getSpan(t,e){if(0>this.span)throw new RangeError("indeterminate span");return this.span}replicate(t){const e=Object.create(this.constructor.prototype);return Object.assign(e,this),e.property=t,e}fromArray(t){}}function y(t,e){return e.property?t+"["+e.property+"]":t}f.Layout=g,f.nameWithProperty=y,f.bindConstructorLayout=function(t,e){if("function"!=typeof t)throw new TypeError("Class must be constructor");if(t.hasOwnProperty("layout_"))throw new Error("Class is already bound to a layout");if(!(e&&e instanceof g))throw new TypeError("layout must be a Layout");if(e.hasOwnProperty("boundConstructor_"))throw new Error("layout is already bound to a constructor");t.layout_=e,e.boundConstructor_=t,e.makeDestinationObject=()=>new t,Object.defineProperty(t.prototype,"encode",{value:function(t,r){return e.encode(this,t,r)},writable:1}),Object.defineProperty(t,"decode",{value:function(t,r){return e.decode(t,r)},writable:1})};class w extends g{isCount(){throw new Error("ExternalLayout is abstract")}}class v extends w{constructor(t,e){if(void 0===t&&(t=1),!Number.isInteger(t)||0>=t)throw new TypeError("elementSpan must be a (positive) integer");super(-1,e),this.elementSpan=t}isCount(){return 1}decode(t,e){void 0===e&&(e=0);const r=t.length-e;return Math.floor(r/this.elementSpan)}encode(t,e,r){return 0}}class M extends w{constructor(t,e,r){if(!(t instanceof g))throw new TypeError("layout must be a Layout");if(void 0===e)e=0;else if(!Number.isInteger(e))throw new TypeError("offset must be integer or undefined");super(t.span,r||t.property),this.layout=t,this.offset=e}isCount(){return this.layout instanceof b||this.layout instanceof k}decode(t,e){return void 0===e&&(e=0),this.layout.decode(t,e+this.offset)}encode(t,e,r){return void 0===r&&(r=0),this.layout.encode(t,e,r+this.offset)}}class b extends g{constructor(t,e){if(super(t,e),6<this.span)throw new RangeError("span must not exceed 6 bytes")}decode(t,e){return void 0===e&&(e=0),t.readUIntLE(e,this.span)}encode(t,e,r){return void 0===r&&(r=0),e.writeUIntLE(t,r,this.span),this.span}}class k extends g{constructor(t,e){if(super(t,e),6<this.span)throw new RangeError("span must not exceed 6 bytes")}decode(t,e){return void 0===e&&(e=0),t.readUIntBE(e,this.span)}encode(t,e,r){return void 0===r&&(r=0),e.writeUIntBE(t,r,this.span),this.span}}class P extends g{constructor(t,e){if(super(t,e),6<this.span)throw new RangeError("span must not exceed 6 bytes")}decode(t,e){return void 0===e&&(e=0),t.readIntLE(e,this.span)}encode(t,e,r){return void 0===r&&(r=0),e.writeIntLE(t,r,this.span),this.span}}class S extends g{constructor(t,e){if(super(t,e),6<this.span)throw new RangeError("span must not exceed 6 bytes")}decode(t,e){return void 0===e&&(e=0),t.readIntBE(e,this.span)}encode(t,e,r){return void 0===r&&(r=0),e.writeIntBE(t,r,this.span),this.span}}const A=Math.pow(2,32);function T(t){const e=Math.floor(t/A);return{hi32:e,lo32:t-e*A}}function x(t,e){return t*A+e}class _ extends g{constructor(t){super(8,t)}decode(t,e){void 0===e&&(e=0);const r=t.readUInt32LE(e);return x(t.readUInt32LE(e+4),r)}encode(t,e,r){void 0===r&&(r=0);const n=T(t);return e.writeUInt32LE(n.lo32,r),e.writeUInt32LE(n.hi32,r+4),8}}class I extends g{constructor(t){super(8,t)}decode(t,e){return void 0===e&&(e=0),x(t.readUInt32BE(e),t.readUInt32BE(e+4))}encode(t,e,r){void 0===r&&(r=0);const n=T(t);return e.writeUInt32BE(n.hi32,r),e.writeUInt32BE(n.lo32,r+4),8}}class E extends g{constructor(t){super(8,t)}decode(t,e){void 0===e&&(e=0);const r=t.readUInt32LE(e);return x(t.readInt32LE(e+4),r)}encode(t,e,r){void 0===r&&(r=0);const n=T(t);return e.writeUInt32LE(n.lo32,r),e.writeInt32LE(n.hi32,r+4),8}}class C extends g{constructor(t){super(8,t)}decode(t,e){return void 0===e&&(e=0),x(t.readInt32BE(e),t.readUInt32BE(e+4))}encode(t,e,r){void 0===r&&(r=0);const n=T(t);return e.writeInt32BE(n.hi32,r),e.writeUInt32BE(n.lo32,r+4),8}}class L extends g{constructor(t){super(4,t)}decode(t,e){return void 0===e&&(e=0),t.readFloatLE(e)}encode(t,e,r){return void 0===r&&(r=0),e.writeFloatLE(t,r),4}}class B extends g{constructor(t){super(4,t)}decode(t,e){return void 0===e&&(e=0),t.readFloatBE(e)}encode(t,e,r){return void 0===r&&(r=0),e.writeFloatBE(t,r),4}}class O extends g{constructor(t){super(8,t)}decode(t,e){return void 0===e&&(e=0),t.readDoubleLE(e)}encode(t,e,r){return void 0===r&&(r=0),e.writeDoubleLE(t,r),8}}class R extends g{constructor(t){super(8,t)}decode(t,e){return void 0===e&&(e=0),t.readDoubleBE(e)}encode(t,e,r){return void 0===r&&(r=0),e.writeDoubleBE(t,r),8}}class D extends g{constructor(t,e,r){if(!(t instanceof g))throw new TypeError("elementLayout must be a Layout");if(!(e instanceof w&&e.isCount()||Number.isInteger(e)&&0<=e))throw new TypeError("count must be non-negative integer or an unsigned integer ExternalLayout");let n=-1;!(e instanceof w)&&0<t.span&&(n=e*t.span),super(n,r),this.elementLayout=t,this.count=e}getSpan(t,e){if(0<=this.span)return this.span;void 0===e&&(e=0);let r=0,n=this.count;if(n instanceof w&&(n=n.decode(t,e)),0<this.elementLayout.span)r=n*this.elementLayout.span;else{let i=0;for(;i<n;)r+=this.elementLayout.getSpan(t,e+r),++i}return r}decode(t,e){void 0===e&&(e=0);const r=[];let n=0,i=this.count;for(i instanceof w&&(i=i.decode(t,e));n<i;)r.push(this.elementLayout.decode(t,e)),e+=this.elementLayout.getSpan(t,e),n+=1;return r}encode(t,e,r){void 0===r&&(r=0);const n=this.elementLayout,i=t.reduce(((t,i)=>t+n.encode(i,e,r+t)),0);return this.count instanceof w&&this.count.encode(t.length,e,r),i}}class K extends g{constructor(t,e,r){if(!Array.isArray(t)||!t.reduce(((t,e)=>t&&e instanceof g),1))throw new TypeError("fields must be array of Layout instances");"boolean"==typeof e&&void 0===r&&(r=e,e=void 0);for(const e of t)if(0>e.span&&void 0===e.property)throw new Error("fields cannot contain unnamed variable-length layout");let n=-1;try{n=t.reduce(((t,e)=>t+e.getSpan()),0)}catch(t){}super(n,e),this.fields=t,this.decodePrefixes=!!r}getSpan(t,e){if(0<=this.span)return this.span;void 0===e&&(e=0);let r=0;try{r=this.fields.reduce(((r,n)=>{const i=n.getSpan(t,e);return e+=i,r+i}),0)}catch(t){throw new RangeError("indeterminate span")}return r}decode(t,e){void 0===e&&(e=0);const r=this.makeDestinationObject();for(const n of this.fields)if(void 0!==n.property&&(r[n.property]=n.decode(t,e)),e+=n.getSpan(t,e),this.decodePrefixes&&t.length===e)break;return r}encode(t,e,r){void 0===r&&(r=0);const n=r;let i=0,o=0;for(const n of this.fields){let s=n.span;if(o=0<s?s:0,void 0!==n.property){const i=t[n.property];void 0!==i&&(o=n.encode(i,e,r),0>s&&(s=n.getSpan(e,r)))}i=r,r+=s}return i+o-n}fromArray(t){const e=this.makeDestinationObject();for(const r of this.fields)void 0!==r.property&&0<t.length&&(e[r.property]=t.shift());return e}layoutFor(t){if("string"!=typeof t)throw new TypeError("property must be string");for(const e of this.fields)if(e.property===t)return e}offsetOf(t){if("string"!=typeof t)throw new TypeError("property must be string");let e=0;for(const r of this.fields){if(r.property===t)return e;0>r.span?e=-1:0<=e&&(e+=r.span)}}}class U{constructor(t){this.property=t}decode(){throw new Error("UnionDiscriminator is abstract")}encode(){throw new Error("UnionDiscriminator is abstract")}}class W extends U{constructor(t,e){if(!(t instanceof w&&t.isCount()))throw new TypeError("layout must be an unsigned integer ExternalLayout");super(e||t.property||"variant"),this.layout=t}decode(t,e){return this.layout.decode(t,e)}encode(t,e,r){return this.layout.encode(t,e,r)}}class q extends g{constructor(t,e,r){const n=t instanceof b||t instanceof k;if(n)t=new W(new M(t));else if(t instanceof w&&t.isCount())t=new W(t);else if(!(t instanceof U))throw new TypeError("discr must be a UnionDiscriminator or an unsigned integer layout");if(void 0===e&&(e=null),!(null===e||e instanceof g))throw new TypeError("defaultLayout must be null or a Layout");if(null!==e){if(0>e.span)throw new Error("defaultLayout must have constant span");void 0===e.property&&(e=e.replicate("content"))}let i=-1;e&&(i=e.span,0<=i&&n&&(i+=t.layout.span)),super(i,r),this.discriminator=t,this.usesPrefixDiscriminator=n,this.defaultLayout=e,this.registry={};let o=this.defaultGetSourceVariant.bind(this);this.getSourceVariant=function(t){return o(t)},this.configGetSourceVariant=function(t){o=t.bind(this)}}getSpan(t,e){if(0<=this.span)return this.span;void 0===e&&(e=0);const r=this.getVariant(t,e);if(!r)throw new Error("unable to determine span for unrecognized variant");return r.getSpan(t,e)}defaultGetSourceVariant(t){if(t.hasOwnProperty(this.discriminator.property)){if(this.defaultLayout&&t.hasOwnProperty(this.defaultLayout.property))return;const e=this.registry[t[this.discriminator.property]];if(e&&(!e.layout||t.hasOwnProperty(e.property)))return e}else for(const e in this.registry){const r=this.registry[e];if(t.hasOwnProperty(r.property))return r}throw new Error("unable to infer src variant")}decode(t,e){let r;void 0===e&&(e=0);const n=this.discriminator,i=n.decode(t,e);let o=this.registry[i];if(void 0===o){let s=0;o=this.defaultLayout,this.usesPrefixDiscriminator&&(s=n.layout.span),r=this.makeDestinationObject(),r[n.property]=i,r[o.property]=this.defaultLayout.decode(t,e+s)}else r=o.decode(t,e);return r}encode(t,e,r){void 0===r&&(r=0);const n=this.getSourceVariant(t);if(void 0===n){const n=this.discriminator,i=this.defaultLayout;let o=0;return this.usesPrefixDiscriminator&&(o=n.layout.span),n.encode(t[n.property],e,r),o+i.encode(t[i.property],e,r+o)}return n.encode(t,e,r)}addVariant(t,e,r){const n=new N(this,t,e,r);return this.registry[t]=n,n}getVariant(t,e){let r=t;return Buffer.isBuffer(t)&&(void 0===e&&(e=0),r=this.discriminator.decode(t,e)),this.registry[r]}}class N extends g{constructor(t,e,r,n){if(!(t instanceof q))throw new TypeError("union must be a Union");if(!Number.isInteger(e)||0>e)throw new TypeError("variant must be a (non-negative) integer");if("string"==typeof r&&void 0===n&&(n=r,r=null),r){if(!(r instanceof g))throw new TypeError("layout must be a Layout");if(null!==t.defaultLayout&&0<=r.span&&r.span>t.defaultLayout.span)throw new Error("variant span exceeds span of containing union");if("string"!=typeof n)throw new TypeError("variant must have a String property")}let i=t.span;0>t.span&&(i=r?r.span:0,0<=i&&t.usesPrefixDiscriminator&&(i+=t.discriminator.layout.span)),super(i,n),this.union=t,this.variant=e,this.layout=r||null}getSpan(t,e){if(0<=this.span)return this.span;void 0===e&&(e=0);let r=0;return this.union.usesPrefixDiscriminator&&(r=this.union.discriminator.layout.span),r+this.layout.getSpan(t,e+r)}decode(t,e){const r=this.makeDestinationObject();if(void 0===e&&(e=0),this!==this.union.getVariant(t,e))throw new Error("variant mismatch");let n=0;return this.union.usesPrefixDiscriminator&&(n=this.union.discriminator.layout.span),this.layout?r[this.property]=this.layout.decode(t,e+n):this.property?r[this.property]=1:this.union.usesPrefixDiscriminator&&(r[this.union.discriminator.property]=this.variant),r}encode(t,e,r){void 0===r&&(r=0);let n=0;if(this.union.usesPrefixDiscriminator&&(n=this.union.discriminator.layout.span),this.layout&&!t.hasOwnProperty(this.property))throw new TypeError("variant lacks property "+this.property);this.union.discriminator.encode(this.variant,e,r);let i=n;if(this.layout&&(this.layout.encode(t[this.property],e,r+n),i+=this.layout.getSpan(e,r+n),0<=this.union.span&&i>this.union.span))throw new Error("encoded variant overruns containing union");return i}fromArray(t){if(this.layout)return this.layout.fromArray(t)}}function j(t){return 0>t&&(t+=4294967296),t}class V extends g{constructor(t,e,r){if(!(t instanceof b||t instanceof k))throw new TypeError("word must be a UInt or UIntBE layout");if("string"==typeof e&&void 0===r&&(r=e,e=void 0),4<t.span)throw new RangeError("word cannot exceed 32 bits");super(t.span,r),this.word=t,this.msb=!!e,this.fields=[];let n=0;this._packedSetValue=function(t){return n=j(t),this},this._packedGetValue=function(){return n}}decode(t,e){const r=this.makeDestinationObject();void 0===e&&(e=0);const n=this.word.decode(t,e);this._packedSetValue(n);for(const t of this.fields)void 0!==t.property&&(r[t.property]=t.decode(n));return r}encode(t,e,r){void 0===r&&(r=0);const n=this.word.decode(e,r);this._packedSetValue(n);for(const e of this.fields)if(void 0!==e.property){const r=t[e.property];void 0!==r&&e.encode(r)}return this.word.encode(this._packedGetValue(),e,r)}addField(t,e){const r=new Z(this,t,e);return this.fields.push(r),r}addBoolean(t){const e=new z(this,t);return this.fields.push(e),e}fieldFor(t){if("string"!=typeof t)throw new TypeError("property must be string");for(const e of this.fields)if(e.property===t)return e}}class Z{constructor(t,e,r){if(!(t instanceof V))throw new TypeError("container must be a BitStructure");if(!Number.isInteger(e)||0>=e)throw new TypeError("bits must be positive integer");const n=8*t.span,i=t.fields.reduce(((t,e)=>t+e.bits),0);if(e+i>n)throw new Error("bits too long for span remainder ("+(n-i)+" of "+n+" remain)");this.container=t,this.bits=e,this.valueMask=(1<<e)-1,32===e&&(this.valueMask=4294967295),this.start=i,this.container.msb&&(this.start=n-i-e),this.wordMask=j(this.valueMask<<this.start),this.property=r}decode(){return j(this.container._packedGetValue()&this.wordMask)>>>this.start}encode(t){if(!Number.isInteger(t)||t!==j(t&this.valueMask))throw new TypeError(y("BitField.encode",this)+" value must be integer not exceeding "+this.valueMask);const e=this.container._packedGetValue(),r=j(t<<this.start);this.container._packedSetValue(j(e&~this.wordMask)|r)}}class z extends Z{constructor(t,e){super(t,1,e)}decode(t,e){return!!Z.prototype.decode.call(this,t,e)}encode(t){return"boolean"==typeof t&&(t=+t),Z.prototype.encode.call(this,t)}}class F extends g{constructor(t,e){if(!(t instanceof w&&t.isCount()||Number.isInteger(t)&&0<=t))throw new TypeError("length must be positive integer or an unsigned integer ExternalLayout");let r=-1;t instanceof w||(r=t),super(r,e),this.length=t}getSpan(t,e){let r=this.span;return 0>r&&(r=this.length.decode(t,e)),r}decode(t,e){void 0===e&&(e=0);let r=this.span;return 0>r&&(r=this.length.decode(t,e)),t.slice(e,e+r)}encode(t,e,r){let n=this.length;if(this.length instanceof w&&(n=t.length),!Buffer.isBuffer(t)||n!==t.length)throw new TypeError(y("Blob.encode",this)+" requires (length "+n+") Buffer as src");if(r+n>e.length)throw new RangeError("encoding overruns Buffer");return e.write(t.toString("hex"),r,n,"hex"),this.length instanceof w&&this.length.encode(n,e,r),n}}class G extends g{constructor(t){super(-1,t)}getSpan(t,e){if(!Buffer.isBuffer(t))throw new TypeError("b must be a Buffer");void 0===e&&(e=0);let r=e;for(;r<t.length&&0!==t[r];)r+=1;return 1+r-e}decode(t,e,r){void 0===e&&(e=0);let n=this.getSpan(t,e);return t.slice(e,e+n-1).toString("utf-8")}encode(t,e,r){void 0===r&&(r=0),"string"!=typeof t&&(t=t.toString());const n=new Buffer(t,"utf8"),i=n.length;if(r+i>e.length)throw new RangeError("encoding overruns Buffer");return n.copy(e,r),e[r+i]=0,i+1}}class Q extends g{constructor(t,e){if("string"==typeof t&&void 0===e&&(e=t,t=void 0),void 0===t)t=-1;else if(!Number.isInteger(t))throw new TypeError("maxSpan must be an integer");super(-1,e),this.maxSpan=t}getSpan(t,e){if(!Buffer.isBuffer(t))throw new TypeError("b must be a Buffer");return void 0===e&&(e=0),t.length-e}decode(t,e,r){void 0===e&&(e=0);let n=this.getSpan(t,e);if(0<=this.maxSpan&&this.maxSpan<n)throw new RangeError("text length exceeds maxSpan");return t.slice(e,e+n).toString("utf-8")}encode(t,e,r){void 0===r&&(r=0),"string"!=typeof t&&(t=t.toString());const n=new Buffer(t,"utf8"),i=n.length;if(0<=this.maxSpan&&this.maxSpan<i)throw new RangeError("text length exceeds maxSpan");if(r+i>e.length)throw new RangeError("encoding overruns Buffer");return n.copy(e,r),i}}class $ extends g{constructor(t,e){super(0,e),this.value=t}decode(t,e,r){return this.value}encode(t,e,r){return 0}}f.ExternalLayout=w,f.GreedyCount=v,f.OffsetLayout=M,f.UInt=b,f.UIntBE=k,f.Int=P,f.IntBE=S,f.Float=L,f.FloatBE=B,f.Double=O,f.DoubleBE=R,f.Sequence=D,f.Structure=K,f.UnionDiscriminator=U,f.UnionLayoutDiscriminator=W,f.Union=q,f.VariantLayout=N,f.BitStructure=V,f.BitField=Z,f.Boolean=z,f.Blob=F,f.CString=G,f.UTF8=Q,f.Constant=$,f.greedy=(t,e)=>new v(t,e),f.offset=(t,e,r)=>new M(t,e,r),f.u8=t=>new b(1,t),f.u16=t=>new b(2,t),f.u24=t=>new b(3,t),f.u32=t=>new b(4,t),f.u40=t=>new b(5,t),f.u48=t=>new b(6,t),f.nu64=t=>new _(t),f.u16be=t=>new k(2,t),f.u24be=t=>new k(3,t),f.u32be=t=>new k(4,t),f.u40be=t=>new k(5,t),f.u48be=t=>new k(6,t),f.nu64be=t=>new I(t),f.s8=t=>new P(1,t),f.s16=t=>new P(2,t),f.s24=t=>new P(3,t),f.s32=t=>new P(4,t),f.s40=t=>new P(5,t),f.s48=t=>new P(6,t),f.ns64=t=>new E(t),f.s16be=t=>new S(2,t),f.s24be=t=>new S(3,t),f.s32be=t=>new S(4,t),f.s40be=t=>new S(5,t),f.s48be=t=>new S(6,t),f.ns64be=t=>new C(t),f.f32=t=>new L(t),f.f32be=t=>new B(t),f.f64=t=>new O(t),f.f64be=t=>new R(t),f.struct=(t,e,r)=>new K(t,e,r),f.bits=(t,e,r)=>new V(t,e,r),f.seq=(t,e,r)=>new D(t,e,r),f.union=(t,e,r)=>new q(t,e,r),f.unionLayoutDiscriminator=(t,e)=>new W(t,e),f.blob=(t,e)=>new F(t,e),f.cstr=t=>new G(t),f.utf8=(t,e)=>new Q(t,e),f.const=(t,e)=>new $(t,e),function(t){var r=a&&a.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(t,"__esModule",{value:1}),t.map=t.array=t.rustEnum=t.str=t.vecU8=t.tagged=t.vec=t.bool=t.option=t.publicKey=t.i256=t.u256=t.i128=t.u128=t.i64=t.u64=t.struct=t.f64=t.f32=t.i32=t.u32=t.i16=t.u16=t.i8=t.u8=void 0;const n=f,i=e,o=r(c);var s=f;Object.defineProperty(t,"u8",{enumerable:1,get:function(){return s.u8}}),Object.defineProperty(t,"i8",{enumerable:1,get:function(){return s.s8}}),Object.defineProperty(t,"u16",{enumerable:1,get:function(){return s.u16}}),Object.defineProperty(t,"i16",{enumerable:1,get:function(){return s.s16}}),Object.defineProperty(t,"u32",{enumerable:1,get:function(){return s.u32}}),Object.defineProperty(t,"i32",{enumerable:1,get:function(){return s.s32}}),Object.defineProperty(t,"f32",{enumerable:1,get:function(){return s.f32}}),Object.defineProperty(t,"f64",{enumerable:1,get:function(){return s.f64}}),Object.defineProperty(t,"struct",{enumerable:1,get:function(){return s.struct}});class u extends n.Layout{constructor(t,e,r){super(t,r),this.blob=(0,n.blob)(t),this.signed=e}decode(t,e=0){const r=new o.default(this.blob.decode(t,e),10,"le");return this.signed?r.fromTwos(8*this.span).clone():r}encode(t,e,r=0){return this.signed&&(t=t.toTwos(8*this.span)),this.blob.encode(t.toArrayLike(Buffer,"le",this.span),e,r)}}function h(t){return new u(8,0,t)}t.u64=h,t.i64=function(t){return new u(8,1,t)},t.u128=function(t){return new u(16,0,t)},t.i128=function(t){return new u(16,1,t)},t.u256=function(t){return new u(32,0,t)},t.i256=function(t){return new u(32,1,t)};class l extends n.Layout{constructor(t,e,r,n){super(t.span,n),this.layout=t,this.decoder=e,this.encoder=r}decode(t,e){return this.decoder(this.layout.decode(t,e))}encode(t,e,r){return this.layout.encode(this.encoder(t),e,r)}getSpan(t,e){return this.layout.getSpan(t,e)}}t.publicKey=function(t){return new l((0,n.blob)(32),(t=>new i.PublicKey(t)),(t=>t.toBuffer()),t)};class d extends n.Layout{constructor(t,e){super(-1,e),this.layout=t,this.discriminator=(0,n.u8)()}encode(t,e,r=0){return null==t?this.discriminator.encode(0,e,r):(this.discriminator.encode(1,e,r),this.layout.encode(t,e,r+1)+1)}decode(t,e=0){const r=this.discriminator.decode(t,e);if(0===r)return null;if(1===r)return this.layout.decode(t,e+1);throw new Error("Invalid option "+this.property)}getSpan(t,e=0){const r=this.discriminator.decode(t,e);if(0===r)return 1;if(1===r)return this.layout.getSpan(t,e+1)+1;throw new Error("Invalid option "+this.property)}}function m(t){if(0===t)return 0;if(1===t)return 1;throw new Error("Invalid bool: "+t)}function p(t){return t?1:0}function g(t){const e=(0,n.u32)("length"),r=(0,n.struct)([e,(0,n.blob)((0,n.offset)(e,-e.span),"data")]);return new l(r,(({data:t})=>t),(t=>({data:t})),t)}t.option=function(t,e){return new d(t,e)},t.bool=function(t){return new l((0,n.u8)(),m,p,t)},t.vec=function(t,e){const r=(0,n.u32)("length"),i=(0,n.struct)([r,(0,n.seq)(t,(0,n.offset)(r,-r.span),"values")]);return new l(i,(({values:t})=>t),(t=>({values:t})),e)},t.tagged=function(t,e,r){const i=(0,n.struct)([h("tag"),e.replicate("data")]);return new l(i,(function({tag:e,data:r}){if(!e.eq(t))throw new Error("Invalid tag, expected: "+t.toString("hex")+", got: "+e.toString("hex"));return r}),(e=>({tag:t,data:e})),r)},t.vecU8=g,t.str=function(t){return new l(g(),(t=>t.toString("utf-8")),(t=>Buffer.from(t,"utf-8")),t)},t.rustEnum=function(t,e,r){const i=(0,n.union)(null!=r?r:(0,n.u8)(),e);return t.forEach(((t,e)=>i.addVariant(e,t,t.property))),i},t.array=function(t,e,r){const i=(0,n.struct)([(0,n.seq)(t,e,"values")]);return new l(i,(({values:t})=>t),(t=>({values:t})),r)};class y extends n.Layout{constructor(t,e,r){super(t.span+e.span,r),this.keyLayout=t,this.valueLayout=e}decode(t,e){return e=e||0,[this.keyLayout.decode(t,e),this.valueLayout.decode(t,e+this.keyLayout.getSpan(t,e))]}encode(t,e,r){r=r||0;const n=this.keyLayout.encode(t[0],e,r);return n+this.valueLayout.encode(t[1],e,r+n)}getSpan(t,e){return this.keyLayout.getSpan(t,e)+this.valueLayout.getSpan(t,e)}}t.map=function(t,e,r){const i=(0,n.u32)("length"),o=(0,n.struct)([i,(0,n.seq)(new y(t,e),(0,n.offset)(i,-i.span),"values")]);return new l(o,(({values:t})=>new Map(t)),(t=>({values:Array.from(t.entries())})),r)}}(p);const H=n.Buffer.from([23,169,27,122,147,169,209,152]),Y=p.struct([p.array(p.u8(),32,"a"),p.array(p.u8(),64,"b"),p.array(p.u8(),32,"c")]),J=p.struct([p.publicKey("owner"),p.u64("amount"),p.option(p.u64(),"lamports"),p.u8("merkleTreeIndex"),p.option(p.vecU8(),"tlv")]),X=p.struct([p.u8("queueId"),p.u16("index")]),tt=p.struct([p.u64("amount"),p.option(p.u8(),"delegateIndex"),p.struct([p.u8("merkleTreePubkeyIndex"),p.u8("nullifierQueuePubkeyIndex"),p.u32("leafIndex"),p.option(X,"QueueIndex")],"merkleContext"),p.u16("rootIndex"),p.option(p.u64(),"lamports"),p.option(p.vecU8(),"tlv")]),et=p.struct([p.publicKey("owner"),p.option(p.u8(),"delegateChangeAccountIndex")]),rt=p.struct([p.bool("setContext"),p.bool("firstSetContext"),p.u8("cpiContextAccountIndex")]),nt=p.struct([p.option(Y,"proof"),p.publicKey("mint"),p.option(et,"delegatedTransfer"),p.vec(tt,"inputTokenDataWithContext"),p.vec(J,"outputCompressedAccounts"),p.bool("isCompress"),p.option(p.u64(),"compressOrDecompressAmount"),p.option(rt,"cpiContext"),p.option(p.u8(),"lamportsChangeAccountMerkleTreeIndex")]),it=p.struct([p.vec(p.publicKey(),"recipients"),p.vec(p.u64(),"amounts"),p.option(p.u64(),"lamports")]),ot=p.struct([p.publicKey("owner"),p.option(p.u64(),"remainingAmount"),p.option(rt,"cpiContext")]),st=n.Buffer.from([241,34,48,186,37,179,123,192]),at=n.Buffer.from([163,52,200,231,140,3,69,186]),ut=n.Buffer.from([112,230,105,101,145,202,157,97]);function ht(t){const e=n.Buffer.alloc(1e3),r=nt.encode(t,e),i=n.Buffer.alloc(4);return i.writeUInt32LE(r,0),n.Buffer.concat([at,i,e.slice(0,r)])}p.struct([p.publicKey("owner"),p.option(p.u64(),"remainingAmount"),p.option(p.struct([],"CompressedCpiContext"),"cpiContext")],"compressSplTokenAccountArgs");const lt=t=>{const e=gt.programId,{feePayer:r,authority:n,cpiAuthorityPda:i,lightSystemProgram:o,registeredProgramPda:s,noopProgram:a,accountCompressionAuthority:u,accountCompressionProgram:h,selfProgram:l,tokenPoolPda:d,compressOrDecompressTokenAccount:c,tokenProgram:m,systemProgram:p}=t;return[{pubkey:r,isSigner:1,isWritable:1},{pubkey:n,isSigner:1,isWritable:0},{pubkey:i,isSigner:0,isWritable:0},{pubkey:o,isSigner:0,isWritable:0},{pubkey:s,isSigner:0,isWritable:0},{pubkey:a,isSigner:0,isWritable:0},{pubkey:u,isSigner:0,isWritable:0},{pubkey:h,isSigner:0,isWritable:0},{pubkey:l,isSigner:0,isWritable:0},{pubkey:null!=d?d:e,isSigner:0,isWritable:1},{pubkey:null!=c?c:e,isSigner:0,isWritable:1},{pubkey:null!=m?m:e,isSigner:0,isWritable:0},{pubkey:p,isSigner:0,isWritable:0}]},dt=e=>e.reduce(((t,e)=>t.add(e.parsed.amount)),t.bn(0)),ct=t=>{const e=t[0].parsed.owner;t.forEach((t=>{if(!t.parsed.owner.equals(e))throw new Error("Token accounts must be owned by the same owner")}))},mt=t=>({mint:t[0].parsed.mint,currentOwner:t[0].parsed.owner,delegate:t[0].parsed.delegate});function pt(e,r,n){n=t.bn(n);const i=dt(e),o=t.sumUpLamports(e.map((t=>t.compressedAccount))),s=i.sub(n);return t.validateSufficientBalance(s),s.eq(t.bn(0))&&o.eq(t.bn(0))?[{owner:r,amount:n,lamports:o,tlv:null}]:(t.validateSameOwner(e.map((t=>t.compressedAccount))),ct(e),[{owner:e[0].parsed.owner,amount:s,lamports:o,tlv:null},{owner:r,amount:n,lamports:t.bn(0),tlv:null}])}function ft(e,r){r=t.bn(r);const n=t.sumUpLamports(e.map((t=>t.compressedAccount))),i=dt(e).sub(r);return t.validateSufficientBalance(i),i.eq(t.bn(0))&&n.eq(t.bn(0))?[]:(t.validateSameOwner(e.map((t=>t.compressedAccount))),ct(e),[{owner:e[0].parsed.owner,amount:i,lamports:n,tlv:null}])}class gt{constructor(){}static setProgramId(t){this.programId="string"==typeof t?new e.PublicKey(t):t}static deriveTokenPoolPda(t){const r=[o,t.toBuffer()],[n,i]=e.PublicKey.findProgramAddressSync(r,this.programId);return n}static get deriveCpiAuthorityPda(){const[t,r]=e.PublicKey.findProgramAddressSync([s],this.programId);return t}static async createMint(t){const{mint:n,authority:i,feePayer:o,rentExemptBalance:s,tokenProgramId:a,freezeAuthority:u,mintSize:h}=t,l=null!=a?a:r.TOKEN_PROGRAM_ID;return[e.SystemProgram.createAccount({fromPubkey:o,lamports:s,newAccountPubkey:n,programId:l,space:null!=h?h:r.MINT_SIZE}),r.createInitializeMint2Instruction(n,t.decimals,i,u,l),await this.createTokenPool({feePayer:o,mint:n,tokenProgramId:l})]}static async createTokenPool(t){const{mint:n,feePayer:i,tokenProgramId:o}=t,s=null!=o?o:r.TOKEN_PROGRAM_ID,a=(t=>{const{feePayer:e,tokenPoolPda:r,systemProgram:n,mint:i,tokenProgram:o,cpiAuthorityPda:s}=t;return[{pubkey:e,isSigner:1,isWritable:1},{pubkey:r,isSigner:0,isWritable:1},{pubkey:n,isSigner:0,isWritable:0},{pubkey:i,isSigner:0,isWritable:1},{pubkey:o,isSigner:0,isWritable:0},{pubkey:s,isSigner:0,isWritable:0}]})({mint:n,feePayer:i,tokenPoolPda:this.deriveTokenPoolPda(n),tokenProgram:s,cpiAuthorityPda:this.deriveCpiAuthorityPda,systemProgram:e.SystemProgram.programId});return new e.TransactionInstruction({programId:this.programId,keys:a,data:H})}static async mintTo(i){const o=t.defaultStaticAccountsStruct(),{mint:s,feePayer:a,authority:u,merkleTree:h,toPubkey:l,amount:d,tokenProgramId:c}=i,m=null!=c?c:r.TOKEN_PROGRAM_ID,p=this.deriveTokenPoolPda(s),f=t.toArray(d).map((e=>t.bn(e))),g=t.toArray(l);if(f.length!==g.length)throw new Error("Amount and toPubkey arrays must have the same length");const y=(t=>{const e=gt.programId,{feePayer:r,authority:n,cpiAuthorityPda:i,mint:o,tokenPoolPda:s,tokenProgram:a,lightSystemProgram:u,registeredProgramPda:h,noopProgram:l,accountCompressionAuthority:d,accountCompressionProgram:c,merkleTree:m,selfProgram:p,systemProgram:f,solPoolPda:g}=t;return[{pubkey:r,isSigner:1,isWritable:1},{pubkey:n,isSigner:1,isWritable:0},{pubkey:i,isSigner:0,isWritable:0},{pubkey:o,isSigner:0,isWritable:1},{pubkey:s,isSigner:0,isWritable:1},{pubkey:a,isSigner:0,isWritable:0},{pubkey:u,isSigner:0,isWritable:0},{pubkey:h,isSigner:0,isWritable:0},{pubkey:l,isSigner:0,isWritable:0},{pubkey:d,isSigner:0,isWritable:0},{pubkey:c,isSigner:0,isWritable:0},{pubkey:m,isSigner:0,isWritable:1},{pubkey:p,isSigner:0,isWritable:0},{pubkey:f,isSigner:0,isWritable:0},{pubkey:null!=g?g:e,isSigner:0,isWritable:1}]})({mint:s,feePayer:a,authority:u,cpiAuthorityPda:this.deriveCpiAuthorityPda,tokenProgram:m,tokenPoolPda:p,lightSystemProgram:t.LightSystemProgram.programId,registeredProgramPda:o.registeredProgramPda,noopProgram:o.noopProgram,accountCompressionAuthority:o.accountCompressionAuthority,accountCompressionProgram:o.accountCompressionProgram,merkleTree:null!=h?h:t.defaultTestStateTreeAccounts().merkleTree,selfProgram:this.programId,systemProgram:e.SystemProgram.programId,solPoolPda:null}),w=function(t,e){const r=n.Buffer.alloc(1e3),i=it.encode({recipients:t,amounts:e,lamports:null},r);return n.Buffer.concat([st,r.slice(0,i)])}(g,f);return new e.TransactionInstruction({programId:this.programId,keys:y,data:w})}static async approveAndMintTo(t){const{mint:e,feePayer:n,authorityTokenAccount:i,authority:o,merkleTree:s,toPubkey:a,tokenProgramId:u}=t,h=BigInt(t.amount.toString());return[r.createMintToInstruction(e,i,o,h,[],u),await this.compress({payer:n,owner:o,source:i,toAddress:a,mint:e,amount:t.amount,outputStateTree:s,tokenProgramId:u})]}static async transfer(r){const{payer:n,inputCompressedTokenAccounts:o,recentInputStateRootIndices:s,recentValidityProof:a,amount:u,outputStateTrees:h,toAddress:l}=r,d=pt(o,l,u),{inputTokenDataWithContext:c,packedOutputTokenData:m,remainingAccountMetas:p}=i({inputCompressedTokenAccounts:o,outputStateTrees:h,rootIndices:s,tokenTransferOutputs:d}),{mint:f,currentOwner:g}=mt(o),y=ht({proof:a,mint:f,delegatedTransfer:null,inputTokenDataWithContext:c,outputCompressedAccounts:m,compressOrDecompressAmount:null,isCompress:0,cpiContext:null,lamportsChangeAccountMerkleTreeIndex:null}),{accountCompressionAuthority:w,noopProgram:v,registeredProgramPda:M,accountCompressionProgram:b}=t.defaultStaticAccountsStruct(),k=lt({feePayer:n,authority:g,cpiAuthorityPda:this.deriveCpiAuthorityPda,lightSystemProgram:t.LightSystemProgram.programId,registeredProgramPda:M,noopProgram:v,accountCompressionAuthority:w,accountCompressionProgram:b,selfProgram:this.programId,tokenPoolPda:void 0,compressOrDecompressTokenAccount:void 0,tokenProgram:void 0,systemProgram:e.SystemProgram.programId});return k.push(...p),new e.TransactionInstruction({programId:this.programId,keys:k,data:y})}static async createTokenProgramLookupTable(n){const{authority:i,mints:o,recentSlot:s,payer:a,remainingAccounts:u}=n,[h,l]=e.AddressLookupTableProgram.createLookupTable({authority:i,payer:i,recentSlot:s});let d=[];return o&&(d=[...o,...o.map((t=>this.deriveTokenPoolPda(t)))]),{instructions:[h,e.AddressLookupTableProgram.extendLookupTable({payer:a,authority:i,lookupTable:l,addresses:[this.deriveCpiAuthorityPda,t.LightSystemProgram.programId,t.defaultStaticAccountsStruct().registeredProgramPda,t.defaultStaticAccountsStruct().noopProgram,t.defaultStaticAccountsStruct().accountCompressionAuthority,t.defaultStaticAccountsStruct().accountCompressionProgram,t.defaultTestStateTreeAccounts().merkleTree,t.defaultTestStateTreeAccounts().nullifierQueue,t.defaultTestStateTreeAccounts().addressTree,t.defaultTestStateTreeAccounts().addressQueue,this.programId,r.TOKEN_PROGRAM_ID,r.TOKEN_2022_PROGRAM_ID,i,...d,...null!=u?u:[]]})],address:l}}static async compress(n){const{payer:o,owner:s,source:a,toAddress:u,mint:h,outputStateTree:l,tokenProgramId:d}=n;if(Array.isArray(n.amount)!==Array.isArray(n.toAddress))throw new Error("Both amount and toAddress must be arrays or both must be single values");let c;if(Array.isArray(n.amount)&&Array.isArray(n.toAddress)){if(n.amount.length!==n.toAddress.length)throw new Error("Amount and toAddress arrays must have the same length");c=n.amount.map(((e,r)=>{const i=t.bn(e);return{owner:n.toAddress[r],amount:i,lamports:t.bn(0),tlv:null}}))}else c=[{owner:u,amount:t.bn(n.amount),lamports:t.bn(0),tlv:null}];const{inputTokenDataWithContext:p,packedOutputTokenData:f,remainingAccountMetas:g}=i({inputCompressedTokenAccounts:[],outputStateTrees:l,rootIndices:[],tokenTransferOutputs:c}),y=ht({proof:null,mint:h,delegatedTransfer:null,inputTokenDataWithContext:p,outputCompressedAccounts:f,compressOrDecompressAmount:Array.isArray(n.amount)?n.amount.map((t=>new m(t))).reduce(((t,e)=>t.add(e)),new m(0)):new m(n.amount),isCompress:1,cpiContext:null,lamportsChangeAccountMerkleTreeIndex:null}),w=null!=d?d:r.TOKEN_PROGRAM_ID,v=lt(Object.assign(Object.assign({},t.defaultStaticAccountsStruct()),{feePayer:o,authority:s,cpiAuthorityPda:this.deriveCpiAuthorityPda,lightSystemProgram:t.LightSystemProgram.programId,selfProgram:this.programId,systemProgram:e.SystemProgram.programId,tokenPoolPda:this.deriveTokenPoolPda(h),compressOrDecompressTokenAccount:a,tokenProgram:w}));return v.push(...g),new e.TransactionInstruction({programId:this.programId,keys:v,data:y})}static async decompress(n){const{payer:o,inputCompressedTokenAccounts:s,toAddress:a,outputStateTree:u,recentValidityProof:h,recentInputStateRootIndices:l,tokenProgramId:d}=n,c=t.bn(n.amount),m=ft(s,c),{inputTokenDataWithContext:p,packedOutputTokenData:f,remainingAccountMetas:g}=i({inputCompressedTokenAccounts:s,outputStateTrees:u,rootIndices:l,tokenTransferOutputs:m}),{mint:y,currentOwner:w}=mt(s),v=ht({proof:h,mint:y,delegatedTransfer:null,inputTokenDataWithContext:p,outputCompressedAccounts:f,compressOrDecompressAmount:c,isCompress:0,cpiContext:null,lamportsChangeAccountMerkleTreeIndex:null}),M=null!=d?d:r.TOKEN_PROGRAM_ID,{accountCompressionAuthority:b,noopProgram:k,registeredProgramPda:P,accountCompressionProgram:S}=t.defaultStaticAccountsStruct(),A=lt({feePayer:o,authority:w,cpiAuthorityPda:this.deriveCpiAuthorityPda,lightSystemProgram:t.LightSystemProgram.programId,registeredProgramPda:P,noopProgram:k,accountCompressionAuthority:b,accountCompressionProgram:S,selfProgram:this.programId,tokenPoolPda:this.deriveTokenPoolPda(y),compressOrDecompressTokenAccount:a,tokenProgram:M,systemProgram:e.SystemProgram.programId});return A.push(...g),new e.TransactionInstruction({programId:this.programId,keys:A,data:v})}static async mergeTokenAccounts(t){const{payer:e,owner:r,inputCompressedTokenAccounts:n,outputStateTree:i,recentValidityProof:o,recentInputStateRootIndices:s}=t;if(n.length>3)throw new Error("Cannot merge more than 3 token accounts at once");return[await this.transfer({payer:e,inputCompressedTokenAccounts:n,toAddress:r,amount:n.reduce(((t,e)=>t.add(e.parsed.amount)),new m(0)),outputStateTrees:i,recentInputStateRootIndices:s,recentValidityProof:o})]}static async compressSplTokenAccount(i){const{feePayer:o,authority:s,tokenAccount:a,mint:u,remainingAmount:h,outputStateTree:l,tokenProgramId:d}=i,c=null!=d?d:r.TOKEN_PROGRAM_ID,m=[{pubkey:l,isSigner:0,isWritable:1}],p=function(t,e){const r=n.Buffer.alloc(1e3),i=ot.encode({owner:t,remainingAmount:e,cpiContext:null},r);return n.Buffer.concat([ut,r.slice(0,i)])}(s,null!=h?h:null),{accountCompressionAuthority:f,noopProgram:g,registeredProgramPda:y,accountCompressionProgram:w}=t.defaultStaticAccountsStruct(),v=lt({feePayer:o,authority:s,cpiAuthorityPda:this.deriveCpiAuthorityPda,lightSystemProgram:t.LightSystemProgram.programId,registeredProgramPda:y,noopProgram:g,accountCompressionAuthority:f,accountCompressionProgram:w,selfProgram:this.programId,tokenPoolPda:this.deriveTokenPoolPda(u),compressOrDecompressTokenAccount:a,tokenProgram:c,systemProgram:e.SystemProgram.programId});return v.push(...m),new e.TransactionInstruction({programId:this.programId,keys:v,data:p})}static async get_mint_program_id(t,e){var r;return null===(r=await e.getAccountInfo(t))||void 0===r?void 0:r.owner}}function yt(e,r){let n=t.bn(0),i=t.bn(0);const o=[];e.sort(((t,e)=>e.parsed.amount.cmp(t.parsed.amount)));for(const s of e){if(n.gte(t.bn(r)))break;n=n.add(s.parsed.amount),i=i.add(s.compressedAccount.lamports),o.push(s)}if(n.lt(t.bn(r)))throw new Error(`Not enough balance for transfer. Required: ${r.toString()}, available: ${n.toString()}`);return[o,n,i.lt(t.bn(0))?i:null]}gt.programId=new e.PublicKey("cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m"),exports.CPI_AUTHORITY_SEED=s,exports.CompressedTokenProgram=gt,exports.POOL_SEED=o,exports.SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE=1461600,exports.approveAndMintTo=async function(n,i,o,s,a,u,h,l,d){d=d||await gt.get_mint_program_id(o,n);const c=await r.getOrCreateAssociatedTokenAccount(n,i,o,a.publicKey,void 0,void 0,l,d),m=await gt.approveAndMintTo({feePayer:i.publicKey,mint:o,authority:a.publicKey,authorityTokenAccount:c.address,amount:u,toPubkey:s,merkleTree:h,tokenProgramId:d}),{blockhash:p}=await n.getLatestBlockhash(),f=t.dedupeSigner(i,[a]),g=t.buildAndSignTx([e.ComputeBudgetProgram.setComputeUnitLimit({units:1e6}),...m],i,p,f);return await t.sendAndConfirmTx(n,g,l)},exports.compress=async function(r,n,i,o,s,a,u,h,l,d){d=d||await gt.get_mint_program_id(i,r);const c=await gt.compress({payer:n.publicKey,owner:s.publicKey,source:a,toAddress:u,amount:o,mint:i,outputStateTree:h,tokenProgramId:d}),m=await r.getLatestBlockhash(),p=t.dedupeSigner(n,[s]),f=t.buildAndSignTx([e.ComputeBudgetProgram.setComputeUnitLimit({units:1e6}),c],n,m.blockhash,p);return await t.sendAndConfirmTx(r,f,l,m)},exports.compressSplTokenAccount=async function(r,n,i,o,s,a,u,h,l){l=l||await gt.get_mint_program_id(i,r);const d=await gt.compressSplTokenAccount({feePayer:n.publicKey,authority:o.publicKey,tokenAccount:s,mint:i,remainingAmount:u,outputStateTree:a,tokenProgramId:l}),c=await r.getLatestBlockhash(),m=t.dedupeSigner(n,[o]),p=t.buildAndSignTx([e.ComputeBudgetProgram.setComputeUnitLimit({units:1e6}),d],n,c.blockhash,m);return await t.sendAndConfirmTx(r,p,h,c)},exports.createDecompressOutputState=ft,exports.createMint=async function(n,i,o,s,a=e.Keypair.generate(),u,h=0){const l=await n.getMinimumBalanceForRentExemption(r.MINT_SIZE),d=h?r.TOKEN_2022_PROGRAM_ID:r.TOKEN_PROGRAM_ID,c=await gt.createMint({feePayer:i.publicKey,mint:a.publicKey,decimals:s,authority:o,freezeAuthority:null,rentExemptBalance:l,tokenProgramId:d}),{blockhash:m}=await n.getLatestBlockhash(),p=t.dedupeSigner(i,[a]),f=t.buildAndSignTx(c,i,m,p),g=await t.sendAndConfirmTx(n,f,u);return{mint:a.publicKey,transactionSignature:g}},exports.createTokenPool=async function(e,r,n,i,o){o=o||await gt.get_mint_program_id(n,e);const s=await gt.createTokenPool({feePayer:r.publicKey,mint:n,tokenProgramId:o}),{blockhash:a}=await e.getLatestBlockhash(),u=t.buildAndSignTx([s],r,a);return await t.sendAndConfirmTx(e,u,i)},exports.createTokenProgramLookupTable=async function(e,r,n,i,o){const s=await e.getSlot("finalized"),{instructions:a,address:u}=await gt.createTokenProgramLookupTable({payer:r.publicKey,authority:n.publicKey,mints:i,remainingAccounts:o,recentSlot:s}),h=t.dedupeSigner(r,[n]),l=await e.getLatestBlockhash(),d=t.buildAndSignTx([a[0]],r,l.blockhash,h),c=await t.sendAndConfirmTx(e,d,{commitment:"finalized"},l),m=await e.getLatestBlockhash(),p=t.buildAndSignTx([a[1]],r,m.blockhash,h);return{txIds:[c,await t.sendAndConfirmTx(e,p,{commitment:"finalized"},m)],address:u}},exports.createTransferOutputState=pt,exports.decompress=async function(r,n,i,o,s,a,u,h,l){l=l||await gt.get_mint_program_id(i,r),o=t.bn(o);const d=await r.getCompressedTokenAccountsByOwner(s.publicKey,{mint:i}),[c]=yt(d.items,o),m=await r.getValidityProof(c.map((e=>t.bn(e.compressedAccount.hash)))),p=await gt.decompress({payer:n.publicKey,inputCompressedTokenAccounts:c,toAddress:a,amount:o,outputStateTree:u,recentInputStateRootIndices:m.rootIndices,recentValidityProof:m.compressedProof,tokenProgramId:l}),{blockhash:f}=await r.getLatestBlockhash(),g=t.dedupeSigner(n,[s]),y=t.buildAndSignTx([e.ComputeBudgetProgram.setComputeUnitLimit({units:1e6}),p],n,f,g);return await t.sendAndConfirmTx(r,y,h)},exports.mergeTokenAccounts=async function(r,n,i,o,s,a){const u=await r.getCompressedTokenAccountsByOwner(o.publicKey,{mint:i});if(0===u.items.length)throw new Error(`No compressed token accounts found for mint ${i.toBase58()}`);if(u.items.length>=6)throw new Error(`Too many compressed token accounts used for mint ${i.toBase58()}`);const h=[e.ComputeBudgetProgram.setComputeUnitLimit({units:1e6})];for(let e=0;e<u.items.slice(0,6).length;e+=3){const a=u.items.slice(e,e+3),l=await r.getValidityProof(a.map((e=>t.bn(e.compressedAccount.hash)))),d=await gt.mergeTokenAccounts({payer:n.publicKey,owner:o.publicKey,mint:i,inputCompressedTokenAccounts:a,outputStateTree:s,recentValidityProof:l.compressedProof,recentInputStateRootIndices:l.rootIndices});h.push(...d)}const{blockhash:l}=await r.getLatestBlockhash(),d=t.dedupeSigner(n,[o]),c=t.buildAndSignTx(h,n,l,d);return await t.sendAndConfirmTx(r,c,a)},exports.mintTo=async function(r,n,i,o,s,a,u,h,l){l=l||await gt.get_mint_program_id(i,r);const d=t.dedupeSigner(n,[s]),c=await gt.mintTo({feePayer:n.publicKey,mint:i,authority:s.publicKey,amount:a,toPubkey:o,merkleTree:u,tokenProgramId:l}),{blockhash:m}=await r.getLatestBlockhash(),p=t.buildAndSignTx([e.ComputeBudgetProgram.setComputeUnitLimit({units:1e6}),c],n,m,d);return await t.sendAndConfirmTx(r,p,h)},exports.packCompressedTokenAccounts=i,exports.parseTokenData=mt,exports.selectMinCompressedTokenAccountsForTransfer=yt,exports.sumUpTokenAmount=dt,exports.transfer=async function(r,n,i,o,s,a,u,h){o=t.bn(o);const l=await r.getCompressedTokenAccountsByOwner(s.publicKey,{mint:i}),[d]=yt(l.items,o),c=await r.getValidityProof(d.map((e=>t.bn(e.compressedAccount.hash)))),m=await gt.transfer({payer:n.publicKey,inputCompressedTokenAccounts:d,toAddress:a,amount:o,recentInputStateRootIndices:c.rootIndices,recentValidityProof:c.compressedProof,outputStateTrees:u}),{blockhash:p}=await r.getLatestBlockhash(),f=t.dedupeSigner(n,[s]),g=t.buildAndSignTx([e.ComputeBudgetProgram.setComputeUnitLimit({units:1e6}),m],n,p,f);return await t.sendAndConfirmTx(r,g,h)},exports.validateSameTokenOwner=ct;
2828
2
  //# sourceMappingURL=index.cjs.map