summaryrefslogtreecommitdiff
path: root/lib/taler-wallet-android.js
blob: 2731ce8f62b698532990c4a478a71ebf8833c83d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
22310
22311
22312
22313
22314
22315
22316
22317
22318
22319
22320
22321
22322
22323
22324
22325
22326
22327
22328
22329
22330
22331
22332
22333
22334
22335
22336
22337
22338
22339
22340
22341
22342
22343
22344
22345
22346
22347
22348
22349
22350
22351
22352
22353
22354
22355
22356
22357
22358
22359
22360
22361
22362
22363
22364
22365
22366
22367
22368
22369
22370
22371
22372
22373
22374
22375
22376
22377
22378
22379
22380
22381
22382
22383
22384
22385
22386
22387
22388
22389
22390
22391
22392
22393
22394
22395
22396
22397
22398
22399
22400
22401
22402
22403
22404
22405
22406
22407
22408
22409
22410
22411
22412
22413
22414
22415
22416
22417
22418
22419
22420
22421
22422
22423
22424
22425
22426
22427
22428
22429
22430
22431
22432
22433
22434
22435
22436
22437
22438
22439
22440
22441
22442
22443
22444
22445
22446
22447
22448
22449
22450
22451
22452
22453
22454
22455
22456
22457
22458
22459
22460
22461
22462
22463
22464
22465
22466
22467
22468
22469
22470
22471
22472
22473
22474
22475
22476
22477
22478
22479
22480
22481
22482
22483
22484
22485
22486
22487
22488
22489
22490
22491
22492
22493
22494
22495
22496
22497
22498
22499
22500
22501
22502
22503
22504
22505
22506
22507
22508
22509
22510
22511
22512
22513
22514
22515
22516
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529
22530
22531
22532
22533
22534
22535
22536
22537
22538
22539
22540
22541
22542
22543
22544
22545
22546
22547
22548
22549
22550
22551
22552
22553
22554
22555
22556
22557
22558
22559
22560
22561
22562
22563
22564
22565
22566
22567
22568
22569
22570
22571
22572
22573
22574
22575
22576
22577
22578
22579
22580
22581
22582
22583
22584
22585
22586
22587
22588
22589
22590
22591
22592
22593
22594
22595
22596
22597
22598
22599
22600
22601
22602
22603
22604
22605
22606
22607
22608
22609
22610
22611
22612
22613
22614
22615
22616
22617
22618
22619
22620
22621
22622
22623
22624
22625
22626
22627
22628
22629
22630
22631
22632
22633
22634
22635
22636
22637
22638
22639
22640
22641
22642
22643
22644
22645
22646
22647
22648
22649
22650
22651
22652
22653
22654
22655
22656
22657
22658
22659
22660
22661
22662
22663
22664
22665
22666
22667
22668
22669
22670
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var crypto = _interopDefault(require('crypto'));
var http = _interopDefault(require('http'));
var https = _interopDefault(require('https'));
var url = _interopDefault(require('url'));
var assert = _interopDefault(require('assert'));
var stream = _interopDefault(require('stream'));
var tty = _interopDefault(require('tty'));
var util = _interopDefault(require('util'));
var os = _interopDefault(require('os'));
var zlib = _interopDefault(require('zlib'));
var querystring = _interopDefault(require('querystring'));
var fs = _interopDefault(require('fs'));

var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

function unwrapExports (x) {
	return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}

function createCommonjsModule(fn, module) {
	return module = { exports: {} }, fn(module, module.exports), module.exports;
}

function getCjsExportFromNamespace (n) {
	return n && n['default'] || n;
}

var timer = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2017 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
class IntervalHandle {
    constructor(h) {
        this.h = h;
    }
    clear() {
        clearInterval(this.h);
    }
}
class TimeoutHandle {
    constructor(h) {
        this.h = h;
    }
    clear() {
        clearTimeout(this.h);
    }
}
/**
 * Get a performance counter in milliseconds.
 */
exports.performanceNow = (() => {
    if (typeof process !== "undefined" && process.hrtime) {
        return () => {
            const t = process.hrtime();
            return t[0] * 1e9 + t[1];
        };
    }
    else {
        return () => performance.now();
    }
})();
/**
 * Call a function every time the delay given in milliseconds passes.
 */
function every(delayMs, callback) {
    return new IntervalHandle(setInterval(callback, delayMs));
}
exports.every = every;
/**
 * Call a function after the delay given in milliseconds passes.
 */
function after(delayMs, callback) {
    return new TimeoutHandle(setTimeout(callback, delayMs));
}
exports.after = after;
const nullTimerHandle = {
    clear() {
        // do nothing
        return;
    },
};
/**
 * Group of timers that can be destroyed at once.
 */
class TimerGroup {
    constructor() {
        this.stopped = false;
        this.timerMap = {};
        this.idGen = 1;
    }
    stopCurrentAndFutureTimers() {
        this.stopped = true;
        for (const x in this.timerMap) {
            if (!this.timerMap.hasOwnProperty(x)) {
                continue;
            }
            this.timerMap[x].clear();
            delete this.timerMap[x];
        }
    }
    resolveAfter(delayMs) {
        return new Promise((resolve, reject) => {
            this.after(delayMs, () => {
                resolve();
            });
        });
    }
    after(delayMs, callback) {
        if (this.stopped) {
            console.warn("dropping timer since timer group is stopped");
            return nullTimerHandle;
        }
        const h = after(delayMs, callback);
        const myId = this.idGen++;
        this.timerMap[myId] = h;
        const tm = this.timerMap;
        return {
            clear() {
                h.clear();
                delete tm[myId];
            },
        };
    }
    every(delayMs, callback) {
        if (this.stopped) {
            console.warn("dropping timer since timer group is stopped");
            return nullTimerHandle;
        }
        const h = every(delayMs, callback);
        const myId = this.idGen++;
        this.timerMap[myId] = h;
        const tm = this.timerMap;
        return {
            clear() {
                h.clear();
                delete tm[myId];
            },
        };
    }
}
exports.TimerGroup = TimerGroup;

});

unwrapExports(timer);
var timer_1 = timer.performanceNow;
var timer_2 = timer.every;
var timer_3 = timer.after;
var timer_4 = timer.TimerGroup;

var cryptoApi = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2016 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const timer$1 = __importStar(timer);
/**
 * Number of different priorities. Each priority p
 * must be 0 <= p < NUM_PRIO.
 */
const NUM_PRIO = 5;
class BrowserCryptoWorkerFactory {
    startWorker() {
        const workerCtor = Worker;
        const workerPath = "/dist/cryptoWorker-bundle.js";
        return new workerCtor(workerPath);
    }
    getConcurrency() {
        let concurrency = 2;
        try {
            // only works in the browser
            // tslint:disable-next-line:no-string-literal
            concurrency = navigator["hardwareConcurrency"];
            concurrency = Math.max(1, Math.ceil(concurrency / 2));
        }
        catch (e) {
            concurrency = 2;
        }
        return concurrency;
    }
}
exports.BrowserCryptoWorkerFactory = BrowserCryptoWorkerFactory;
/**
 * Crypto API that interfaces manages a background crypto thread
 * for the execution of expensive operations.
 */
class CryptoApi {
    constructor(workerFactory) {
        this.nextRpcId = 1;
        /**
         * Number of busy workers.
         */
        this.numBusy = 0;
        /**
         * Did we stop accepting new requests?
         */
        this.stopped = false;
        this.workerFactory = workerFactory;
        this.workers = new Array(workerFactory.getConcurrency());
        for (let i = 0; i < this.workers.length; i++) {
            this.workers[i] = {
                currentWorkItem: null,
                terminationTimerHandle: null,
                w: null,
            };
        }
        this.workQueues = [];
        for (let i = 0; i < NUM_PRIO; i++) {
            this.workQueues.push([]);
        }
    }
    /**
     * Terminate all worker threads.
     */
    terminateWorkers() {
        for (let worker of this.workers) {
            if (worker.w) {
                CryptoApi.enableTracing && console.log("terminating worker");
                worker.w.terminate();
                if (worker.terminationTimerHandle) {
                    worker.terminationTimerHandle.clear();
                    worker.terminationTimerHandle = null;
                }
                if (worker.currentWorkItem) {
                    worker.currentWorkItem.reject(Error("explicitly terminated"));
                    worker.currentWorkItem = null;
                }
                worker.w = null;
            }
        }
    }
    stop() {
        this.terminateWorkers();
        this.stopped = true;
    }
    /**
     * Start a worker (if not started) and set as busy.
     */
    wake(ws, work) {
        if (this.stopped) {
            console.log("cryptoApi is stopped");
            CryptoApi.enableTracing && console.log("not waking, as cryptoApi is stopped");
            return;
        }
        if (ws.currentWorkItem !== null) {
            throw Error("assertion failed");
        }
        ws.currentWorkItem = work;
        this.numBusy++;
        if (!ws.w) {
            const w = this.workerFactory.startWorker();
            w.onmessage = (m) => this.handleWorkerMessage(ws, m);
            w.onerror = (e) => this.handleWorkerError(ws, e);
            ws.w = w;
        }
        const msg = {
            args: work.args,
            id: work.rpcId,
            operation: work.operation,
        };
        this.resetWorkerTimeout(ws);
        work.startTime = timer$1.performanceNow();
        setImmediate(() => ws.w.postMessage(msg));
    }
    resetWorkerTimeout(ws) {
        if (ws.terminationTimerHandle !== null) {
            ws.terminationTimerHandle.clear();
            ws.terminationTimerHandle = null;
        }
        const destroy = () => {
            // terminate worker if it's idle
            if (ws.w && ws.currentWorkItem === null) {
                ws.w.terminate();
                ws.w = null;
            }
        };
        ws.terminationTimerHandle = timer$1.after(15 * 1000, destroy);
    }
    handleWorkerError(ws, e) {
        if (ws.currentWorkItem) {
            console.error(`error in worker during ${ws.currentWorkItem.operation}`, e);
        }
        else {
            console.error("error in worker", e);
        }
        console.error(e.message);
        try {
            ws.w.terminate();
            ws.w = null;
        }
        catch (e) {
            console.error(e);
        }
        if (ws.currentWorkItem !== null) {
            ws.currentWorkItem.reject(e);
            ws.currentWorkItem = null;
            this.numBusy--;
        }
        this.findWork(ws);
    }
    findWork(ws) {
        // try to find more work for this worker
        for (let i = 0; i < NUM_PRIO; i++) {
            const q = this.workQueues[NUM_PRIO - i - 1];
            if (q.length !== 0) {
                const work = q.shift();
                this.wake(ws, work);
                return;
            }
        }
    }
    handleWorkerMessage(ws, msg) {
        const id = msg.data.id;
        if (typeof id !== "number") {
            console.error("rpc id must be number");
            return;
        }
        const currentWorkItem = ws.currentWorkItem;
        ws.currentWorkItem = null;
        this.numBusy--;
        this.findWork(ws);
        if (!currentWorkItem) {
            console.error("unsolicited response from worker");
            return;
        }
        if (id !== currentWorkItem.rpcId) {
            console.error(`RPC with id ${id} has no registry entry`);
            return;
        }
        CryptoApi.enableTracing &&
            console.log(`rpc ${currentWorkItem.operation} took ${timer$1.performanceNow() -
                currentWorkItem.startTime}ms`);
        currentWorkItem.resolve(msg.data.result);
    }
    doRpc(operation, priority, ...args) {
        const p = new Promise((resolve, reject) => {
            const rpcId = this.nextRpcId++;
            const workItem = {
                operation,
                args,
                resolve,
                reject,
                rpcId,
                startTime: 0,
            };
            if (this.numBusy === this.workers.length) {
                const q = this.workQueues[priority];
                if (!q) {
                    throw Error("assertion failed");
                }
                this.workQueues[priority].push(workItem);
                return;
            }
            for (const ws of this.workers) {
                if (ws.currentWorkItem !== null) {
                    continue;
                }
                this.wake(ws, workItem);
                return;
            }
            throw Error("assertion failed");
        });
        return p;
    }
    createPlanchet(req) {
        return this.doRpc("createPlanchet", 1, req);
    }
    createTipPlanchet(denom) {
        return this.doRpc("createTipPlanchet", 1, denom);
    }
    hashString(str) {
        return this.doRpc("hashString", 1, str);
    }
    hashDenomPub(denomPub) {
        return this.doRpc("hashDenomPub", 1, denomPub);
    }
    isValidDenom(denom, masterPub) {
        return this.doRpc("isValidDenom", 2, denom, masterPub);
    }
    isValidWireFee(type, wf, masterPub) {
        return this.doRpc("isValidWireFee", 2, type, wf, masterPub);
    }
    isValidPaymentSignature(sig, contractHash, merchantPub) {
        return this.doRpc("isValidPaymentSignature", 1, sig, contractHash, merchantPub);
    }
    signDeposit(contractTerms, cds, totalAmount) {
        return this.doRpc("signDeposit", 3, contractTerms, cds, totalAmount);
    }
    createEddsaKeypair() {
        return this.doRpc("createEddsaKeypair", 1);
    }
    rsaUnblind(sig, bk, pk) {
        return this.doRpc("rsaUnblind", 4, sig, bk, pk);
    }
    createPaybackRequest(coin) {
        return this.doRpc("createPaybackRequest", 1, coin);
    }
    createRefreshSession(exchangeBaseUrl, kappa, meltCoin, newCoinDenoms, meltFee) {
        return this.doRpc("createRefreshSession", 4, exchangeBaseUrl, kappa, meltCoin, newCoinDenoms, meltFee);
    }
    signCoinLink(oldCoinPriv, newDenomHash, oldCoinPub, transferPub, coinEv) {
        return this.doRpc("signCoinLink", 4, oldCoinPriv, newDenomHash, oldCoinPub, transferPub, coinEv);
    }
    benchmark(repetitions) {
        return this.doRpc("benchmark", 1, repetitions);
    }
}
exports.CryptoApi = CryptoApi;
CryptoApi.enableTracing = false;

});

unwrapExports(cryptoApi);
var cryptoApi_1 = cryptoApi.BrowserCryptoWorkerFactory;
var cryptoApi_2 = cryptoApi.CryptoApi;

var promiseUtils = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Get an unresolved promise together with its extracted resolve / reject
 * function.
 */
function openPromise() {
    let resolve = null;
    let reject = null;
    const promise = new Promise((res, rej) => {
        resolve = res;
        reject = rej;
    });
    if (!(resolve && reject)) {
        // Never happens, unless JS implementation is broken
        throw Error();
    }
    return { resolve, reject, promise };
}
exports.openPromise = openPromise;
class AsyncCondition {
    constructor() {
        const op = openPromise();
        this._waitPromise = op.promise;
        this._resolveWaitPromise = op.resolve;
    }
    wait() {
        return this._waitPromise;
    }
    trigger() {
        this._resolveWaitPromise();
        const op = openPromise();
        this._waitPromise = op.promise;
        this._resolveWaitPromise = op.resolve;
    }
}
exports.AsyncCondition = AsyncCondition;

});

unwrapExports(promiseUtils);
var promiseUtils_1 = promiseUtils.openPromise;
var promiseUtils_2 = promiseUtils.AsyncCondition;

var query = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2016 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Database query abstractions.
 * @module Query
 * @author Florian Dold
 */
/**
 * Imports.
 */

/**
 * Definition of an object store.
 */
class Store {
    constructor(name, storeParams, validator) {
        this.name = name;
        this.storeParams = storeParams;
        this.validator = validator;
    }
}
exports.Store = Store;
function requestToPromise(req) {
    const stack = Error("Failed request was started here.");
    return new Promise((resolve, reject) => {
        req.onsuccess = () => {
            resolve(req.result);
        };
        req.onerror = () => {
            console.log("error in DB request", req.error);
            reject(req.error);
            console.log("Request failed:", stack);
        };
    });
}
function transactionToPromise(tx) {
    const stack = Error("Failed transaction was started here.");
    return new Promise((resolve, reject) => {
        tx.onabort = () => {
            reject(exports.TransactionAbort);
        };
        tx.oncomplete = () => {
            resolve();
        };
        tx.onerror = () => {
            console.error("Transaction failed:", stack);
            reject(tx.error);
        };
    });
}
function oneShotGet(db, store, key) {
    return __awaiter(this, void 0, void 0, function* () {
        const tx = db.transaction([store.name], "readonly");
        const req = tx.objectStore(store.name).get(key);
        const v = yield requestToPromise(req);
        yield transactionToPromise(tx);
        return v;
    });
}
exports.oneShotGet = oneShotGet;
function oneShotGetIndexed(db, index, key) {
    return __awaiter(this, void 0, void 0, function* () {
        const tx = db.transaction([index.storeName], "readonly");
        const req = tx
            .objectStore(index.storeName)
            .index(index.indexName)
            .get(key);
        const v = yield requestToPromise(req);
        yield transactionToPromise(tx);
        return v;
    });
}
exports.oneShotGetIndexed = oneShotGetIndexed;
function oneShotPut(db, store, value, key) {
    return __awaiter(this, void 0, void 0, function* () {
        const tx = db.transaction([store.name], "readwrite");
        const req = tx.objectStore(store.name).put(value, key);
        const v = yield requestToPromise(req);
        yield transactionToPromise(tx);
        return v;
    });
}
exports.oneShotPut = oneShotPut;
function applyMutation(req, f) {
    return new Promise((resolve, reject) => {
        req.onsuccess = () => {
            const cursor = req.result;
            if (cursor) {
                const val = cursor.value;
                const modVal = f(val);
                if (modVal !== undefined && modVal !== null) {
                    const req2 = cursor.update(modVal);
                    req2.onerror = () => {
                        reject(req2.error);
                    };
                    req2.onsuccess = () => {
                        cursor.continue();
                    };
                }
                else {
                    cursor.continue();
                }
            }
            else {
                resolve();
            }
        };
        req.onerror = () => {
            reject(req.error);
        };
    });
}
function oneShotMutate(db, store, key, f) {
    return __awaiter(this, void 0, void 0, function* () {
        const tx = db.transaction([store.name], "readwrite");
        const req = tx.objectStore(store.name).openCursor(key);
        yield applyMutation(req, f);
        yield transactionToPromise(tx);
    });
}
exports.oneShotMutate = oneShotMutate;
class ResultStream {
    constructor(req) {
        this.req = req;
        this.gotCursorEnd = false;
        this.awaitingResult = false;
        this.awaitingResult = true;
        let p = promiseUtils.openPromise();
        this.currentPromise = p.promise;
        req.onsuccess = () => {
            if (!this.awaitingResult) {
                throw Error("BUG: invariant violated");
            }
            const cursor = req.result;
            if (cursor) {
                this.awaitingResult = false;
                p.resolve();
                p = promiseUtils.openPromise();
                this.currentPromise = p.promise;
            }
            else {
                this.gotCursorEnd = true;
                p.resolve();
            }
        };
        req.onerror = () => {
            p.reject(req.error);
        };
    }
    toArray() {
        return __awaiter(this, void 0, void 0, function* () {
            const arr = [];
            while (true) {
                const x = yield this.next();
                if (x.hasValue) {
                    arr.push(x.value);
                }
                else {
                    break;
                }
            }
            return arr;
        });
    }
    map(f) {
        return __awaiter(this, void 0, void 0, function* () {
            const arr = [];
            while (true) {
                const x = yield this.next();
                if (x.hasValue) {
                    arr.push(f(x.value));
                }
                else {
                    break;
                }
            }
            return arr;
        });
    }
    forEach(f) {
        return __awaiter(this, void 0, void 0, function* () {
            while (true) {
                const x = yield this.next();
                if (x.hasValue) {
                    f(x.value);
                }
                else {
                    break;
                }
            }
        });
    }
    filter(f) {
        return __awaiter(this, void 0, void 0, function* () {
            const arr = [];
            while (true) {
                const x = yield this.next();
                if (x.hasValue) {
                    if (f(x.value)) {
                        arr.push(x.value);
                    }
                }
                else {
                    break;
                }
            }
            return arr;
        });
    }
    next() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.gotCursorEnd) {
                return { hasValue: false };
            }
            if (!this.awaitingResult) {
                const cursor = this.req.result;
                if (!cursor) {
                    throw Error("assertion failed");
                }
                this.awaitingResult = true;
                cursor.continue();
            }
            yield this.currentPromise;
            if (this.gotCursorEnd) {
                return { hasValue: false };
            }
            const cursor = this.req.result;
            if (!cursor) {
                throw Error("assertion failed");
            }
            return { hasValue: true, value: cursor.value };
        });
    }
}
function oneShotIter(db, store) {
    const tx = db.transaction([store.name], "readonly");
    const req = tx.objectStore(store.name).openCursor();
    return new ResultStream(req);
}
exports.oneShotIter = oneShotIter;
function oneShotIterIndex(db, index, query) {
    const tx = db.transaction([index.storeName], "readonly");
    const req = tx
        .objectStore(index.storeName)
        .index(index.indexName)
        .openCursor(query);
    return new ResultStream(req);
}
exports.oneShotIterIndex = oneShotIterIndex;
class TransactionHandle {
    constructor(tx) {
        this.tx = tx;
    }
    put(store, value, key) {
        const req = this.tx.objectStore(store.name).put(value, key);
        return requestToPromise(req);
    }
    add(store, value, key) {
        const req = this.tx.objectStore(store.name).add(value, key);
        return requestToPromise(req);
    }
    get(store, key) {
        const req = this.tx.objectStore(store.name).get(key);
        return requestToPromise(req);
    }
    getIndexed(index, key) {
        const req = this.tx
            .objectStore(index.storeName)
            .index(index.indexName)
            .get(key);
        return requestToPromise(req);
    }
    iter(store, key) {
        const req = this.tx.objectStore(store.name).openCursor(key);
        return new ResultStream(req);
    }
    delete(store, key) {
        const req = this.tx.objectStore(store.name).delete(key);
        return requestToPromise(req);
    }
    mutate(store, key, f) {
        const req = this.tx.objectStore(store.name).openCursor(key);
        return applyMutation(req, f);
    }
}
function runWithReadTransaction(db, stores, f) {
    return runWithTransaction(db, stores, f, "readonly");
}
exports.runWithReadTransaction = runWithReadTransaction;
function runWithWriteTransaction(db, stores, f) {
    return runWithTransaction(db, stores, f, "readwrite");
}
exports.runWithWriteTransaction = runWithWriteTransaction;
function runWithTransaction(db, stores, f, mode) {
    const stack = Error("Failed transaction was started here.");
    return new Promise((resolve, reject) => {
        const storeName = stores.map(x => x.name);
        const tx = db.transaction(storeName, mode);
        let funResult = undefined;
        let gotFunResult = false;
        tx.oncomplete = () => {
            // This is a fatal error: The transaction completed *before*
            // the transaction function returned.  Likely, the transaction
            // function waited on a promise that is *not* resolved in the
            // microtask queue, thus triggering the auto-commit behavior.
            // Unfortunately, the auto-commit behavior of IDB can't be switched
            // of.  There are some proposals to add this functionality in the future.
            if (!gotFunResult) {
                const msg = "BUG: transaction closed before transaction function returned";
                console.error(msg);
                reject(Error(msg));
            }
            resolve(funResult);
        };
        tx.onerror = () => {
            console.error("error in transaction");
        };
        tx.onabort = () => {
            if (tx.error) {
                console.error("Transaction aborted with error:", tx.error);
            }
            else {
                console.log("Trasaction aborted (no error)");
            }
            reject(exports.TransactionAbort);
        };
        const th = new TransactionHandle(tx);
        const resP = f(th);
        resP
            .then(result => {
            gotFunResult = true;
            funResult = result;
        })
            .catch(e => {
            if (e == exports.TransactionAbort) {
                console.info("aborting transaction");
            }
            else {
                tx.abort();
                console.error("Transaction failed:", e);
                console.error(stack);
            }
        });
    });
}
/**
 * Definition of an index.
 */
class Index {
    constructor(s, indexName, keyPath, options) {
        this.indexName = indexName;
        this.keyPath = keyPath;
        const defaultOptions = {
            multiEntry: false,
        };
        this.options = Object.assign(Object.assign({}, defaultOptions), (options || {}));
        this.storeName = s.name;
    }
}
exports.Index = Index;
/**
 * Exception that should be thrown by client code to abort a transaction.
 */
exports.TransactionAbort = Symbol("transaction_abort");

});

unwrapExports(query);
var query_1 = query.Store;
var query_2 = query.oneShotGet;
var query_3 = query.oneShotGetIndexed;
var query_4 = query.oneShotPut;
var query_5 = query.oneShotMutate;
var query_6 = query.oneShotIter;
var query_7 = query.oneShotIterIndex;
var query_8 = query.runWithReadTransaction;
var query_9 = query.runWithWriteTransaction;
var query_10 = query.Index;
var query_11 = query.TransactionAbort;

var checkable = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2016 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Decorators for validating JSON objects and converting them to a typed
 * object.
 *
 * The decorators are put onto classes, and the validation is done
 * via a static method that is filled in by the annotation.
 *
 * Example:
 * ```
 *  @Checkable.Class
 *  class Person {
 *    @Checkable.String
 *    name: string;
 *    @Checkable.Number
 *    age: number;
 *
 *    // Method will be implemented automatically
 *    static checked(obj: any): Person;
 *  }
 * ```
 */
var Checkable;
(function (Checkable) {
    // tslint:disable-next-line:no-shadowed-variable
    Checkable.SchemaError = (function SchemaError(message) {
        const that = this;
        that.name = "SchemaError";
        that.message = message;
        that.stack = new Error().stack;
    });
    Checkable.SchemaError.prototype = new Error();
    /**
     * Classes that are checkable are annotated with this
     * checkable info symbol, which contains the information necessary
     * to check if they're valid.
     */
    const checkableInfoSym = Symbol("checkableInfo");
    /**
     * Get the current property list for a checkable type.
     */
    function getCheckableInfo(target) {
        let chk = target[checkableInfoSym];
        if (!chk) {
            chk = { props: [], extraAllowed: false };
            target[checkableInfoSym] = chk;
        }
        return chk;
    }
    function checkNumber(target, prop, path) {
        if ((typeof target) !== "number") {
            throw new Checkable.SchemaError(`expected number for ${path}`);
        }
        return target;
    }
    function checkString(target, prop, path) {
        if (typeof target !== "string") {
            throw new Checkable.SchemaError(`expected string for ${path}, got ${typeof target} instead`);
        }
        if (prop.stringChecker && !prop.stringChecker(target)) {
            throw new Checkable.SchemaError(`string property ${path} malformed`);
        }
        return target;
    }
    function checkBoolean(target, prop, path) {
        if (typeof target !== "boolean") {
            throw new Checkable.SchemaError(`expected boolean for ${path}, got ${typeof target} instead`);
        }
        return target;
    }
    function checkAnyObject(target, prop, path) {
        if (typeof target !== "object") {
            throw new Checkable.SchemaError(`expected (any) object for ${path}, got ${typeof target} instead`);
        }
        return target;
    }
    function checkAny(target, prop, path) {
        return target;
    }
    function checkList(target, prop, path) {
        if (!Array.isArray(target)) {
            throw new Checkable.SchemaError(`array expected for ${path}, got ${typeof target} instead`);
        }
        for (let i = 0; i < target.length; i++) {
            const v = target[i];
            prop.elementChecker(v, prop.elementProp, path.concat([i]));
        }
        return target;
    }
    function checkMap(target, prop, path) {
        if (typeof target !== "object") {
            throw new Checkable.SchemaError(`expected object for ${path}, got ${typeof target} instead`);
        }
        for (const key in target) {
            prop.keyProp.checker(key, prop.keyProp, path.concat([key]));
            const value = target[key];
            prop.valueProp.checker(value, prop.valueProp, path.concat([key]));
        }
        return target;
    }
    function checkOptional(target, prop, path) {
        console.assert(prop.propertyKey);
        prop.elementChecker(target, prop.elementProp, path.concat([prop.propertyKey]));
        return target;
    }
    function checkValue(target, prop, path) {
        let type;
        if (prop.type) {
            type = prop.type;
        }
        else if (prop.typeThunk) {
            type = prop.typeThunk();
            if (!type) {
                throw Error(`assertion failed: typeThunk returned null (prop is ${JSON.stringify(prop)})`);
            }
        }
        else {
            throw Error(`assertion failed: type/typeThunk missing (prop is ${JSON.stringify(prop)})`);
        }
        const typeName = type.name || "??";
        const v = target;
        if (!v || typeof v !== "object") {
            throw new Checkable.SchemaError(`expected object for ${path.join(".")}, got ${typeof v} instead`);
        }
        const chk = type.prototype[checkableInfoSym];
        const props = chk.props;
        const remainingPropNames = new Set(Object.getOwnPropertyNames(v));
        const obj = new type();
        for (const innerProp of props) {
            if (!remainingPropNames.has(innerProp.propertyKey)) {
                if (innerProp.optional) {
                    continue;
                }
                throw new Checkable.SchemaError(`Property '${innerProp.propertyKey}' missing on '${path}' of '${typeName}'`);
            }
            if (!remainingPropNames.delete(innerProp.propertyKey)) {
                throw new Checkable.SchemaError("assertion failed");
            }
            const propVal = v[innerProp.propertyKey];
            obj[innerProp.propertyKey] = innerProp.checker(propVal, innerProp, path.concat([innerProp.propertyKey]));
        }
        if (!chk.extraAllowed && remainingPropNames.size !== 0) {
            const err = `superfluous properties ${JSON.stringify(Array.from(remainingPropNames.values()))} of ${typeName}`;
            throw new Checkable.SchemaError(err);
        }
        return obj;
    }
    /**
     * Class with checkable annotations on fields.
     * This annotation adds the implementation of the `checked`
     * static method.
     */
    function Class(opts = {}) {
        return (target) => {
            const chk = getCheckableInfo(target.prototype);
            chk.extraAllowed = !!opts.extra;
            target.checked = (v) => {
                const cv = checkValue(v, {
                    checker: checkValue,
                    propertyKey: "(root)",
                    type: target,
                }, ["(root)"]);
                if (opts.validate) {
                    if (typeof target.validate !== "function") {
                        throw Error("invalid Checkable annotion: validate method required");
                    }
                    // May throw exception
                    target.validate(cv);
                }
                return cv;
            };
            return target;
        };
    }
    Checkable.Class = Class;
    /**
     * Target property must be a Checkable object of the given type.
     */
    function Value(typeThunk) {
        function deco(target, propertyKey) {
            const chk = getCheckableInfo(target);
            chk.props.push({
                checker: checkValue,
                propertyKey,
                typeThunk,
            });
        }
        return deco;
    }
    Checkable.Value = Value;
    /**
     * List of values that match the given annotation.  For example, `@Checkable.List(Checkable.String)` is
     * an annotation for a list of strings.
     */
    function List(type) {
        const stub = {};
        type(stub, "(list-element)");
        const elementProp = getCheckableInfo(stub).props[0];
        const elementChecker = elementProp.checker;
        if (!elementChecker) {
            throw Error("assertion failed");
        }
        function deco(target, propertyKey) {
            const chk = getCheckableInfo(target);
            chk.props.push({
                checker: checkList,
                elementChecker,
                elementProp,
                propertyKey,
            });
        }
        return deco;
    }
    Checkable.List = List;
    /**
     * Map from the key type to value type.  Takes two annotations,
     * one for the key type and one for the value type.
     */
    function Map(keyType, valueType) {
        const keyStub = {};
        keyType(keyStub, "(map-key)");
        const keyProp = getCheckableInfo(keyStub).props[0];
        if (!keyProp) {
            throw Error("assertion failed");
        }
        const valueStub = {};
        valueType(valueStub, "(map-value)");
        const valueProp = getCheckableInfo(valueStub).props[0];
        if (!valueProp) {
            throw Error("assertion failed");
        }
        function deco(target, propertyKey) {
            const chk = getCheckableInfo(target);
            chk.props.push({
                checker: checkMap,
                keyProp,
                propertyKey,
                valueProp,
            });
        }
        return deco;
    }
    Checkable.Map = Map;
    /**
     * Makes another annotation optional, for example `@Checkable.Optional(Checkable.Number)`.
     */
    function Optional(type) {
        const stub = {};
        type(stub, "(optional-element)");
        const elementProp = getCheckableInfo(stub).props[0];
        const elementChecker = elementProp.checker;
        if (!elementChecker) {
            throw Error("assertion failed");
        }
        function deco(target, propertyKey) {
            const chk = getCheckableInfo(target);
            chk.props.push({
                checker: checkOptional,
                elementChecker,
                elementProp,
                optional: true,
                propertyKey,
            });
        }
        return deco;
    }
    Checkable.Optional = Optional;
    /**
     * Target property must be a number.
     */
    function Number() {
        const deco = (target, propertyKey) => {
            const chk = getCheckableInfo(target);
            chk.props.push({ checker: checkNumber, propertyKey });
        };
        return deco;
    }
    Checkable.Number = Number;
    /**
     * Target property must be an arbitary object.
     */
    function AnyObject() {
        const deco = (target, propertyKey) => {
            const chk = getCheckableInfo(target);
            chk.props.push({
                checker: checkAnyObject,
                propertyKey,
            });
        };
        return deco;
    }
    Checkable.AnyObject = AnyObject;
    /**
     * Target property can be anything.
     *
     * Not useful by itself, but in combination with higher-order annotations
     * such as List or Map.
     */
    function Any() {
        const deco = (target, propertyKey) => {
            const chk = getCheckableInfo(target);
            chk.props.push({
                checker: checkAny,
                optional: true,
                propertyKey,
            });
        };
        return deco;
    }
    Checkable.Any = Any;
    /**
     * Target property must be a string.
     */
    function String(stringChecker) {
        const deco = (target, propertyKey) => {
            const chk = getCheckableInfo(target);
            chk.props.push({ checker: checkString, propertyKey, stringChecker });
        };
        return deco;
    }
    Checkable.String = String;
    /**
     * Target property must be a boolean value.
     */
    function Boolean() {
        const deco = (target, propertyKey) => {
            const chk = getCheckableInfo(target);
            chk.props.push({ checker: checkBoolean, propertyKey });
        };
        return deco;
    }
    Checkable.Boolean = Boolean;
})(Checkable = exports.Checkable || (exports.Checkable = {}));

});

unwrapExports(checkable);
var checkable_1 = checkable.Checkable;

var amounts = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2018 GNUnet e.V. and INRIA

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __decorate = (commonjsGlobal && commonjsGlobal.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Types and helper functions for dealing with Taler amounts.
 */
/**
 * Imports.
 */

/**
 * Number of fractional units that one value unit represents.
 */
exports.fractionalBase = 1e8;
/**
 * How many digits behind the comma are required to represent the
 * fractional value in human readable decimal format?  Must match
 * lg(fractionalBase)
 */
exports.fractionalLength = 8;
/**
 * Maximum allowed value field of an amount.
 */
exports.maxAmountValue = Math.pow(2, 52);
/**
 * Non-negative financial amount.  Fractional values are expressed as multiples
 * of 1e-8.
 */
let AmountJson = class AmountJson {
};
__decorate([
    checkable.Checkable.Number()
], AmountJson.prototype, "value", void 0);
__decorate([
    checkable.Checkable.Number()
], AmountJson.prototype, "fraction", void 0);
__decorate([
    checkable.Checkable.String()
], AmountJson.prototype, "currency", void 0);
AmountJson = __decorate([
    checkable.Checkable.Class()
], AmountJson);
exports.AmountJson = AmountJson;
/**
 * Get an amount that represents zero units of a currency.
 */
function getZero(currency) {
    return {
        currency,
        fraction: 0,
        value: 0,
    };
}
exports.getZero = getZero;
function sum(amounts) {
    if (amounts.length <= 0) {
        throw Error("can't sum zero amounts");
    }
    return add(amounts[0], ...amounts.slice(1));
}
exports.sum = sum;
/**
 * Add two amounts.  Return the result and whether
 * the addition overflowed.  The overflow is always handled
 * by saturating and never by wrapping.
 *
 * Throws when currencies don't match.
 */
function add(first, ...rest) {
    const currency = first.currency;
    let value = first.value + Math.floor(first.fraction / exports.fractionalBase);
    if (value > exports.maxAmountValue) {
        return {
            amount: { currency, value: exports.maxAmountValue, fraction: exports.fractionalBase - 1 },
            saturated: true
        };
    }
    let fraction = first.fraction % exports.fractionalBase;
    for (const x of rest) {
        if (x.currency !== currency) {
            throw Error(`Mismatched currency: ${x.currency} and ${currency}`);
        }
        value = value + x.value + Math.floor((fraction + x.fraction) / exports.fractionalBase);
        fraction = Math.floor((fraction + x.fraction) % exports.fractionalBase);
        if (value > exports.maxAmountValue) {
            return {
                amount: { currency, value: exports.maxAmountValue, fraction: exports.fractionalBase - 1 },
                saturated: true
            };
        }
    }
    return { amount: { currency, value, fraction }, saturated: false };
}
exports.add = add;
/**
 * Subtract two amounts.  Return the result and whether
 * the subtraction overflowed.  The overflow is always handled
 * by saturating and never by wrapping.
 *
 * Throws when currencies don't match.
 */
function sub(a, ...rest) {
    const currency = a.currency;
    let value = a.value;
    let fraction = a.fraction;
    for (const b of rest) {
        if (b.currency !== currency) {
            throw Error(`Mismatched currency: ${b.currency} and ${currency}`);
        }
        if (fraction < b.fraction) {
            if (value < 1) {
                return { amount: { currency, value: 0, fraction: 0 }, saturated: true };
            }
            value--;
            fraction += exports.fractionalBase;
        }
        console.assert(fraction >= b.fraction);
        fraction -= b.fraction;
        if (value < b.value) {
            return { amount: { currency, value: 0, fraction: 0 }, saturated: true };
        }
        value -= b.value;
    }
    return { amount: { currency, value, fraction }, saturated: false };
}
exports.sub = sub;
/**
 * Compare two amounts.  Returns 0 when equal, -1 when a < b
 * and +1 when a > b.  Throws when currencies don't match.
 */
function cmp(a, b) {
    if (a.currency !== b.currency) {
        throw Error(`Mismatched currency: ${a.currency} and ${b.currency}`);
    }
    const av = a.value + Math.floor(a.fraction / exports.fractionalBase);
    const af = a.fraction % exports.fractionalBase;
    const bv = b.value + Math.floor(b.fraction / exports.fractionalBase);
    const bf = b.fraction % exports.fractionalBase;
    switch (true) {
        case av < bv:
            return -1;
        case av > bv:
            return 1;
        case af < bf:
            return -1;
        case af > bf:
            return 1;
        case af === bf:
            return 0;
        default:
            throw Error("assertion failed");
    }
}
exports.cmp = cmp;
/**
 * Create a copy of an amount.
 */
function copy(a) {
    return {
        currency: a.currency,
        fraction: a.fraction,
        value: a.value,
    };
}
exports.copy = copy;
/**
 * Divide an amount.  Throws on division by zero.
 */
function divide(a, n) {
    if (n === 0) {
        throw Error(`Division by 0`);
    }
    if (n === 1) {
        return { value: a.value, fraction: a.fraction, currency: a.currency };
    }
    const r = a.value % n;
    return {
        currency: a.currency,
        fraction: Math.floor(((r * exports.fractionalBase) + a.fraction) / n),
        value: Math.floor(a.value / n),
    };
}
exports.divide = divide;
/**
 * Check if an amount is non-zero.
 */
function isNonZero(a) {
    return a.value > 0 || a.fraction > 0;
}
exports.isNonZero = isNonZero;
/**
 * Parse an amount like 'EUR:20.5' for 20 Euros and 50 ct.
 */
function parse(s) {
    const res = s.match(/^([a-zA-Z0-9_*-]+):([0-9]+)([.][0-9]+)?$/);
    if (!res) {
        return undefined;
    }
    const tail = res[3] || ".0";
    if (tail.length > exports.fractionalLength + 1) {
        return undefined;
    }
    let value = Number.parseInt(res[2]);
    if (value > exports.maxAmountValue) {
        return undefined;
    }
    return {
        currency: res[1],
        fraction: Math.round(exports.fractionalBase * Number.parseFloat(tail)),
        value,
    };
}
exports.parse = parse;
/**
 * Parse amount in standard string form (like 'EUR:20.5'),
 * throw if the input is not a valid amount.
 */
function parseOrThrow(s) {
    const res = parse(s);
    if (!res) {
        throw Error(`Can't parse amount: "${s}"`);
    }
    return res;
}
exports.parseOrThrow = parseOrThrow;
/**
 * Convert a float to a Taler amount.
 * Loss of precision possible.
 */
function fromFloat(floatVal, currency) {
    return {
        currency,
        fraction: Math.floor((floatVal - Math.floor(floatVal)) * exports.fractionalBase),
        value: Math.floor(floatVal),
    };
}
exports.fromFloat = fromFloat;
/**
 * Convert to standard human-readable string representation that's
 * also used in JSON formats.
 */
function toString(a) {
    const av = a.value + Math.floor(a.fraction / exports.fractionalBase);
    const af = a.fraction % exports.fractionalBase;
    let s = av.toString();
    if (af) {
        s = s + ".";
        let n = af;
        for (let i = 0; i < exports.fractionalLength; i++) {
            if (!n) {
                break;
            }
            s = s + Math.floor(n / exports.fractionalBase * 10).toString();
            n = (n * 10) % exports.fractionalBase;
        }
    }
    return `${a.currency}:${s}`;
}
exports.toString = toString;
/**
 * Check if the argument is a valid amount in string form.
 */
function check(a) {
    if (typeof a !== "string") {
        return false;
    }
    try {
        const parsedAmount = parse(a);
        return !!parsedAmount;
    }
    catch (_a) {
        return false;
    }
}
exports.check = check;

});

unwrapExports(amounts);
var amounts_1 = amounts.fractionalBase;
var amounts_2 = amounts.fractionalLength;
var amounts_3 = amounts.maxAmountValue;
var amounts_4 = amounts.AmountJson;
var amounts_5 = amounts.getZero;
var amounts_6 = amounts.sum;
var amounts_7 = amounts.add;
var amounts_8 = amounts.sub;
var amounts_9 = amounts.cmp;
var amounts_10 = amounts.copy;
var amounts_11 = amounts.divide;
var amounts_12 = amounts.isNonZero;
var amounts_13 = amounts.parse;
var amounts_14 = amounts.parseOrThrow;
var amounts_15 = amounts.fromFloat;
var amounts_16 = amounts.check;

var helpers = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2016 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Amounts = __importStar(amounts);
/**
 * Show an amount in a form suitable for the user.
 * FIXME:  In the future, this should consider currency-specific
 * settings such as significant digits or currency symbols.
 */
function amountToPretty(amount) {
    const x = amount.value + amount.fraction / Amounts.fractionalBase;
    return `${x} ${amount.currency}`;
}
exports.amountToPretty = amountToPretty;
/**
 * Canonicalize a base url, typically for the exchange.
 *
 * See http://api.taler.net/wallet.html#general
 */
function canonicalizeBaseUrl(url) {
    if (!url.startsWith("http") && !url.startsWith("https")) {
        url = "https://" + url;
    }
    const x = new URL(url);
    if (!x.pathname.endsWith("/")) {
        x.pathname = x.pathname + "/";
    }
    x.search = "";
    x.hash = "";
    return x.href;
}
exports.canonicalizeBaseUrl = canonicalizeBaseUrl;
/**
 * Convert object to JSON with canonical ordering of keys
 * and whitespace omitted.
 */
function canonicalJson(obj) {
    // Check for cycles, etc.
    JSON.stringify(obj);
    if (typeof obj === "string" || typeof obj === "number" || obj === null) {
        return JSON.stringify(obj);
    }
    if (Array.isArray(obj)) {
        const objs = obj.map((e) => canonicalJson(e));
        return `[${objs.join(",")}]`;
    }
    const keys = [];
    for (const key in obj) {
        keys.push(key);
    }
    keys.sort();
    let s = "{";
    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        s += JSON.stringify(key) + ":" + canonicalJson(obj[key]);
        if (i !== keys.length - 1) {
            s += ",";
        }
    }
    return s + "}";
}
exports.canonicalJson = canonicalJson;
/**
 * Check for deep equality of two objects.
 * Only arrays, objects and primitives are supported.
 */
function deepEquals(x, y) {
    if (x === y) {
        return true;
    }
    if (Array.isArray(x) && x.length !== y.length) {
        return false;
    }
    const p = Object.keys(x);
    return Object.keys(y).every((i) => p.indexOf(i) !== -1) &&
        p.every((i) => deepEquals(x[i], y[i]));
}
exports.deepEquals = deepEquals;
/**
 * Map from a collection to a list or results and then
 * concatenate the results.
 */
function flatMap(xs, f) {
    return xs.reduce((acc, next) => [...f(next), ...acc], []);
}
exports.flatMap = flatMap;
/**
 * Extract a numeric timstamp (in seconds) from the Taler date format
 * ("/Date([n])/").  Returns null if input is not in the right format.
 */
function getTalerStampSec(stamp) {
    const m = stamp.match(/\/?Date\(([0-9]*)\)\/?/);
    if (!m || !m[1]) {
        return null;
    }
    return parseInt(m[1], 10);
}
exports.getTalerStampSec = getTalerStampSec;
/**
 * Extract a timestamp from a Taler timestamp string.
 */
function extractTalerStamp(stamp) {
    const m = stamp.match(/\/?Date\(([0-9]*)\)\/?/);
    if (!m || !m[1]) {
        return undefined;
    }
    return {
        t_ms: parseInt(m[1], 10) * 1000,
    };
}
exports.extractTalerStamp = extractTalerStamp;
/**
 * Extract a timestamp from a Taler timestamp string.
 */
function extractTalerStampOrThrow(stamp) {
    const r = extractTalerStamp(stamp);
    if (!r) {
        throw Error("invalid time stamp");
    }
    return r;
}
exports.extractTalerStampOrThrow = extractTalerStampOrThrow;
/**
 * Check if a timestamp is in the right format.
 */
function timestampCheck(stamp) {
    return getTalerStampSec(stamp) !== null;
}
exports.timestampCheck = timestampCheck;
/**
 * Get a JavaScript Date object from a Taler date string.
 * Returns null if input is not in the right format.
 */
function getTalerStampDate(stamp) {
    const sec = getTalerStampSec(stamp);
    if (sec == null) {
        return null;
    }
    return new Date(sec * 1000);
}
exports.getTalerStampDate = getTalerStampDate;
/**
 * Compute the hash function of a JSON object.
 */
function hash(val) {
    const str = canonicalJson(val);
    // https://github.com/darkskyapp/string-hash
    let h = 5381;
    let i = str.length;
    while (i) {
        h = (h * 33) ^ str.charCodeAt(--i);
    }
    /* JavaScript does bitwise operations (like XOR, above) on 32-bit signed
    * integers. Since we want the results to be always positive, convert the
    * signed int to an unsigned by doing an unsigned bitshift. */
    return h >>> 0;
}
exports.hash = hash;
/**
 * Lexically compare two strings.
 */
function strcmp(s1, s2) {
    if (s1 < s2) {
        return -1;
    }
    if (s1 > s2) {
        return 1;
    }
    return 0;
}
exports.strcmp = strcmp;

});

unwrapExports(helpers);
var helpers_1 = helpers.amountToPretty;
var helpers_2 = helpers.canonicalizeBaseUrl;
var helpers_3 = helpers.canonicalJson;
var helpers_4 = helpers.deepEquals;
var helpers_5 = helpers.flatMap;
var helpers_6 = helpers.getTalerStampSec;
var helpers_7 = helpers.extractTalerStamp;
var helpers_8 = helpers.extractTalerStampOrThrow;
var helpers_9 = helpers.timestampCheck;
var helpers_10 = helpers.getTalerStampDate;
var helpers_11 = helpers.hash;
var helpers_12 = helpers.strcmp;

var talerTypes = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2018 GNUnet e.V. and INRIA

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __decorate = (commonjsGlobal && commonjsGlobal.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Type and schema definitions for the base taler protocol.
 *
 * All types here should be "@Checkable".
 *
 * Even though the rest of the wallet uses camelCase for fields, use snake_case
 * here, since that's the convention for the Taler JSON+HTTP API.
 */
/**
 * Imports.
 */

const Amounts = __importStar(amounts);

/**
 * Denomination as found in the /keys response from the exchange.
 */
let Denomination = class Denomination {
};
__decorate([
    checkable.Checkable.String(Amounts.check)
], Denomination.prototype, "value", void 0);
__decorate([
    checkable.Checkable.String()
], Denomination.prototype, "denom_pub", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], Denomination.prototype, "fee_withdraw", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], Denomination.prototype, "fee_deposit", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], Denomination.prototype, "fee_refresh", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], Denomination.prototype, "fee_refund", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], Denomination.prototype, "stamp_start", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], Denomination.prototype, "stamp_expire_withdraw", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], Denomination.prototype, "stamp_expire_legal", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], Denomination.prototype, "stamp_expire_deposit", void 0);
__decorate([
    checkable.Checkable.String()
], Denomination.prototype, "master_sig", void 0);
Denomination = __decorate([
    checkable.Checkable.Class()
], Denomination);
exports.Denomination = Denomination;
/**
 * Signature by the auditor that a particular denomination key is audited.
 */
let AuditorDenomSig = class AuditorDenomSig {
};
__decorate([
    checkable.Checkable.String()
], AuditorDenomSig.prototype, "denom_pub_h", void 0);
__decorate([
    checkable.Checkable.String()
], AuditorDenomSig.prototype, "auditor_sig", void 0);
AuditorDenomSig = __decorate([
    checkable.Checkable.Class()
], AuditorDenomSig);
exports.AuditorDenomSig = AuditorDenomSig;
/**
 * Auditor information as given by the exchange in /keys.
 */
let Auditor = class Auditor {
};
__decorate([
    checkable.Checkable.String()
], Auditor.prototype, "auditor_pub", void 0);
__decorate([
    checkable.Checkable.String()
], Auditor.prototype, "auditor_url", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.Value(() => AuditorDenomSig))
], Auditor.prototype, "denomination_keys", void 0);
Auditor = __decorate([
    checkable.Checkable.Class()
], Auditor);
exports.Auditor = Auditor;
/**
 * Response that we get from the exchange for a payback request.
 */
let PaybackConfirmation = class PaybackConfirmation {
};
__decorate([
    checkable.Checkable.String()
], PaybackConfirmation.prototype, "reserve_pub", void 0);
__decorate([
    checkable.Checkable.String()
], PaybackConfirmation.prototype, "amount", void 0);
__decorate([
    checkable.Checkable.String()
], PaybackConfirmation.prototype, "timestamp", void 0);
__decorate([
    checkable.Checkable.String()
], PaybackConfirmation.prototype, "exchange_sig", void 0);
__decorate([
    checkable.Checkable.String()
], PaybackConfirmation.prototype, "exchange_pub", void 0);
PaybackConfirmation = __decorate([
    checkable.Checkable.Class()
], PaybackConfirmation);
exports.PaybackConfirmation = PaybackConfirmation;
/**
 * Information about an exchange as stored inside a
 * merchant's contract terms.
 */
let ExchangeHandle = class ExchangeHandle {
};
__decorate([
    checkable.Checkable.String()
], ExchangeHandle.prototype, "master_pub", void 0);
__decorate([
    checkable.Checkable.String()
], ExchangeHandle.prototype, "url", void 0);
ExchangeHandle = __decorate([
    checkable.Checkable.Class()
], ExchangeHandle);
exports.ExchangeHandle = ExchangeHandle;
/**
 * Contract terms from a merchant.
 */
let ContractTerms = class ContractTerms {
    static validate(x) {
        if (x.exchanges.length === 0) {
            throw Error("no exchanges in contract terms");
        }
    }
};
__decorate([
    checkable.Checkable.String()
], ContractTerms.prototype, "H_wire", void 0);
__decorate([
    checkable.Checkable.String()
], ContractTerms.prototype, "wire_method", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], ContractTerms.prototype, "summary", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], ContractTerms.prototype, "nonce", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], ContractTerms.prototype, "amount", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.AnyObject())
], ContractTerms.prototype, "auditors", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], ContractTerms.prototype, "pay_deadline", void 0);
__decorate([
    checkable.Checkable.Any()
], ContractTerms.prototype, "locations", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], ContractTerms.prototype, "max_fee", void 0);
__decorate([
    checkable.Checkable.Any()
], ContractTerms.prototype, "merchant", void 0);
__decorate([
    checkable.Checkable.String()
], ContractTerms.prototype, "merchant_pub", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.Value(() => ExchangeHandle))
], ContractTerms.prototype, "exchanges", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.AnyObject())
], ContractTerms.prototype, "products", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], ContractTerms.prototype, "refund_deadline", void 0);
__decorate([
    checkable.Checkable.String()
], ContractTerms.prototype, "wire_transfer_deadline", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], ContractTerms.prototype, "timestamp", void 0);
__decorate([
    checkable.Checkable.String()
], ContractTerms.prototype, "order_id", void 0);
__decorate([
    checkable.Checkable.String()
], ContractTerms.prototype, "merchant_base_url", void 0);
__decorate([
    checkable.Checkable.String()
], ContractTerms.prototype, "fulfillment_url", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.Number())
], ContractTerms.prototype, "wire_fee_amortization", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], ContractTerms.prototype, "max_wire_fee", void 0);
__decorate([
    checkable.Checkable.Any()
], ContractTerms.prototype, "extra", void 0);
ContractTerms = __decorate([
    checkable.Checkable.Class({ validate: true })
], ContractTerms);
exports.ContractTerms = ContractTerms;
/**
 * Refund permission in the format that the merchant gives it to us.
 */
let MerchantRefundPermission = class MerchantRefundPermission {
};
__decorate([
    checkable.Checkable.String(Amounts.check)
], MerchantRefundPermission.prototype, "refund_amount", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], MerchantRefundPermission.prototype, "refund_fee", void 0);
__decorate([
    checkable.Checkable.String()
], MerchantRefundPermission.prototype, "coin_pub", void 0);
__decorate([
    checkable.Checkable.Number()
], MerchantRefundPermission.prototype, "rtransaction_id", void 0);
__decorate([
    checkable.Checkable.String()
], MerchantRefundPermission.prototype, "merchant_sig", void 0);
MerchantRefundPermission = __decorate([
    checkable.Checkable.Class()
], MerchantRefundPermission);
exports.MerchantRefundPermission = MerchantRefundPermission;
/**
 * Response for a refund pickup or a /pay in abort mode.
 */
let MerchantRefundResponse = class MerchantRefundResponse {
};
__decorate([
    checkable.Checkable.String()
], MerchantRefundResponse.prototype, "merchant_pub", void 0);
__decorate([
    checkable.Checkable.String()
], MerchantRefundResponse.prototype, "h_contract_terms", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.Value(() => MerchantRefundPermission))
], MerchantRefundResponse.prototype, "refund_permissions", void 0);
MerchantRefundResponse = __decorate([
    checkable.Checkable.Class()
], MerchantRefundResponse);
exports.MerchantRefundResponse = MerchantRefundResponse;
/**
 * Reserve signature, defined as separate class to facilitate
 * schema validation with "@Checkable".
 */
let ReserveSigSingleton = class ReserveSigSingleton {
};
__decorate([
    checkable.Checkable.String()
], ReserveSigSingleton.prototype, "reserve_sig", void 0);
ReserveSigSingleton = __decorate([
    checkable.Checkable.Class()
], ReserveSigSingleton);
exports.ReserveSigSingleton = ReserveSigSingleton;
/**
 * Response to /reserve/status
 */
let ReserveStatus = class ReserveStatus {
};
__decorate([
    checkable.Checkable.String()
], ReserveStatus.prototype, "balance", void 0);
__decorate([
    checkable.Checkable.Any()
], ReserveStatus.prototype, "history", void 0);
ReserveStatus = __decorate([
    checkable.Checkable.Class()
], ReserveStatus);
exports.ReserveStatus = ReserveStatus;
/**
 * Response of the merchant
 * to the TipPickupRequest.
 */
let TipResponse = class TipResponse {
};
__decorate([
    checkable.Checkable.String()
], TipResponse.prototype, "reserve_pub", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.Value(() => ReserveSigSingleton))
], TipResponse.prototype, "reserve_sigs", void 0);
TipResponse = __decorate([
    checkable.Checkable.Class()
], TipResponse);
exports.TipResponse = TipResponse;
/**
 * Element of the payback list that the
 * exchange gives us in /keys.
 */
let Payback = class Payback {
};
__decorate([
    checkable.Checkable.String()
], Payback.prototype, "h_denom_pub", void 0);
Payback = __decorate([
    checkable.Checkable.Class()
], Payback);
exports.Payback = Payback;
/**
 * Structure that the exchange gives us in /keys.
 */
let KeysJson = class KeysJson {
};
__decorate([
    checkable.Checkable.List(checkable.Checkable.Value(() => Denomination))
], KeysJson.prototype, "denoms", void 0);
__decorate([
    checkable.Checkable.String()
], KeysJson.prototype, "master_public_key", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.Value(() => Auditor))
], KeysJson.prototype, "auditors", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], KeysJson.prototype, "list_issue_date", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.List(checkable.Checkable.Value(() => Payback)))
], KeysJson.prototype, "payback", void 0);
__decorate([
    checkable.Checkable.Any()
], KeysJson.prototype, "signkeys", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], KeysJson.prototype, "version", void 0);
KeysJson = __decorate([
    checkable.Checkable.Class({ extra: true })
], KeysJson);
exports.KeysJson = KeysJson;
/**
 * Wire fees as anounced by the exchange.
 */
let WireFeesJson = class WireFeesJson {
};
__decorate([
    checkable.Checkable.String(Amounts.check)
], WireFeesJson.prototype, "wire_fee", void 0);
__decorate([
    checkable.Checkable.String(Amounts.check)
], WireFeesJson.prototype, "closing_fee", void 0);
__decorate([
    checkable.Checkable.String()
], WireFeesJson.prototype, "sig", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], WireFeesJson.prototype, "start_date", void 0);
__decorate([
    checkable.Checkable.String(helpers.timestampCheck)
], WireFeesJson.prototype, "end_date", void 0);
WireFeesJson = __decorate([
    checkable.Checkable.Class()
], WireFeesJson);
exports.WireFeesJson = WireFeesJson;
let AccountInfo = class AccountInfo {
};
__decorate([
    checkable.Checkable.String()
], AccountInfo.prototype, "url", void 0);
__decorate([
    checkable.Checkable.String()
], AccountInfo.prototype, "master_sig", void 0);
AccountInfo = __decorate([
    checkable.Checkable.Class({ extra: true })
], AccountInfo);
exports.AccountInfo = AccountInfo;
let ExchangeWireJson = class ExchangeWireJson {
};
__decorate([
    checkable.Checkable.Map(checkable.Checkable.String(), checkable.Checkable.List(checkable.Checkable.Value(() => WireFeesJson)))
], ExchangeWireJson.prototype, "fees", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.Value(() => AccountInfo))
], ExchangeWireJson.prototype, "accounts", void 0);
ExchangeWireJson = __decorate([
    checkable.Checkable.Class({ extra: true })
], ExchangeWireJson);
exports.ExchangeWireJson = ExchangeWireJson;
/**
 * Proposal returned from the contract URL.
 */
let Proposal = class Proposal {
};
__decorate([
    checkable.Checkable.Value(() => ContractTerms)
], Proposal.prototype, "contract_terms", void 0);
__decorate([
    checkable.Checkable.String()
], Proposal.prototype, "sig", void 0);
Proposal = __decorate([
    checkable.Checkable.Class({ extra: true })
], Proposal);
exports.Proposal = Proposal;
/**
 * Response from the internal merchant API.
 */
let CheckPaymentResponse = class CheckPaymentResponse {
};
__decorate([
    checkable.Checkable.Boolean()
], CheckPaymentResponse.prototype, "paid", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.Boolean())
], CheckPaymentResponse.prototype, "refunded", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], CheckPaymentResponse.prototype, "refunded_amount", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.Value(() => ContractTerms))
], CheckPaymentResponse.prototype, "contract_terms", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], CheckPaymentResponse.prototype, "taler_pay_uri", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], CheckPaymentResponse.prototype, "contract_url", void 0);
CheckPaymentResponse = __decorate([
    checkable.Checkable.Class({ extra: true })
], CheckPaymentResponse);
exports.CheckPaymentResponse = CheckPaymentResponse;
/**
 * Response from the bank.
 */
let WithdrawOperationStatusResponse = class WithdrawOperationStatusResponse {
};
__decorate([
    checkable.Checkable.Boolean()
], WithdrawOperationStatusResponse.prototype, "selection_done", void 0);
__decorate([
    checkable.Checkable.Boolean()
], WithdrawOperationStatusResponse.prototype, "transfer_done", void 0);
__decorate([
    checkable.Checkable.String()
], WithdrawOperationStatusResponse.prototype, "amount", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], WithdrawOperationStatusResponse.prototype, "sender_wire", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], WithdrawOperationStatusResponse.prototype, "suggested_exchange", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], WithdrawOperationStatusResponse.prototype, "confirm_transfer_url", void 0);
__decorate([
    checkable.Checkable.List(checkable.Checkable.String())
], WithdrawOperationStatusResponse.prototype, "wire_types", void 0);
WithdrawOperationStatusResponse = __decorate([
    checkable.Checkable.Class({ extra: true })
], WithdrawOperationStatusResponse);
exports.WithdrawOperationStatusResponse = WithdrawOperationStatusResponse;
/**
 * Response from the merchant.
 */
let TipPickupGetResponse = class TipPickupGetResponse {
};
__decorate([
    checkable.Checkable.AnyObject()
], TipPickupGetResponse.prototype, "extra", void 0);
__decorate([
    checkable.Checkable.String()
], TipPickupGetResponse.prototype, "amount", void 0);
__decorate([
    checkable.Checkable.String()
], TipPickupGetResponse.prototype, "amount_left", void 0);
__decorate([
    checkable.Checkable.String()
], TipPickupGetResponse.prototype, "exchange_url", void 0);
__decorate([
    checkable.Checkable.String()
], TipPickupGetResponse.prototype, "stamp_expire", void 0);
__decorate([
    checkable.Checkable.String()
], TipPickupGetResponse.prototype, "stamp_created", void 0);
TipPickupGetResponse = __decorate([
    checkable.Checkable.Class({ extra: true })
], TipPickupGetResponse);
exports.TipPickupGetResponse = TipPickupGetResponse;

});

unwrapExports(talerTypes);
var talerTypes_1 = talerTypes.Denomination;
var talerTypes_2 = talerTypes.AuditorDenomSig;
var talerTypes_3 = talerTypes.Auditor;
var talerTypes_4 = talerTypes.PaybackConfirmation;
var talerTypes_5 = talerTypes.ExchangeHandle;
var talerTypes_6 = talerTypes.ContractTerms;
var talerTypes_7 = talerTypes.MerchantRefundPermission;
var talerTypes_8 = talerTypes.MerchantRefundResponse;
var talerTypes_9 = talerTypes.ReserveSigSingleton;
var talerTypes_10 = talerTypes.ReserveStatus;
var talerTypes_11 = talerTypes.TipResponse;
var talerTypes_12 = talerTypes.Payback;
var talerTypes_13 = talerTypes.KeysJson;
var talerTypes_14 = talerTypes.WireFeesJson;
var talerTypes_15 = talerTypes.AccountInfo;
var talerTypes_16 = talerTypes.ExchangeWireJson;
var talerTypes_17 = talerTypes.Proposal;
var talerTypes_18 = talerTypes.CheckPaymentResponse;
var talerTypes_19 = talerTypes.WithdrawOperationStatusResponse;
var talerTypes_20 = talerTypes.TipPickupGetResponse;

var walletTypes = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2015-2017 GNUnet e.V. and INRIA

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __decorate = (commonjsGlobal && commonjsGlobal.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Types used by clients of the wallet.
 *
 * These types are defined in a separate file make tree shaking easier, since
 * some components use these types (via RPC) but do not depend on the wallet
 * code directly.
 */
/**
 * Imports.
 */


/**
 * Response for the create reserve request to the wallet.
 */
let CreateReserveResponse = class CreateReserveResponse {
};
__decorate([
    checkable.Checkable.String()
], CreateReserveResponse.prototype, "exchange", void 0);
__decorate([
    checkable.Checkable.String()
], CreateReserveResponse.prototype, "reservePub", void 0);
CreateReserveResponse = __decorate([
    checkable.Checkable.Class()
], CreateReserveResponse);
exports.CreateReserveResponse = CreateReserveResponse;
/**
 * For terseness.
 */
function mkAmount(value, fraction, currency) {
    return { value, fraction, currency };
}
exports.mkAmount = mkAmount;
/**
 * Request to mark a reserve as confirmed.
 */
let CreateReserveRequest = class CreateReserveRequest {
};
__decorate([
    checkable.Checkable.Value(() => amounts.AmountJson)
], CreateReserveRequest.prototype, "amount", void 0);
__decorate([
    checkable.Checkable.String()
], CreateReserveRequest.prototype, "exchange", void 0);
__decorate([
    checkable.Checkable.String()
], CreateReserveRequest.prototype, "exchangeWire", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], CreateReserveRequest.prototype, "senderWire", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], CreateReserveRequest.prototype, "bankWithdrawStatusUrl", void 0);
CreateReserveRequest = __decorate([
    checkable.Checkable.Class()
], CreateReserveRequest);
exports.CreateReserveRequest = CreateReserveRequest;
/**
 * Request to mark a reserve as confirmed.
 */
let ConfirmReserveRequest = class ConfirmReserveRequest {
};
__decorate([
    checkable.Checkable.String()
], ConfirmReserveRequest.prototype, "reservePub", void 0);
ConfirmReserveRequest = __decorate([
    checkable.Checkable.Class()
], ConfirmReserveRequest);
exports.ConfirmReserveRequest = ConfirmReserveRequest;
/**
 * Wire coins to the user's own bank account.
 */
let ReturnCoinsRequest = class ReturnCoinsRequest {
};
__decorate([
    checkable.Checkable.Value(() => amounts.AmountJson)
], ReturnCoinsRequest.prototype, "amount", void 0);
__decorate([
    checkable.Checkable.String()
], ReturnCoinsRequest.prototype, "exchange", void 0);
__decorate([
    checkable.Checkable.Any()
], ReturnCoinsRequest.prototype, "senderWire", void 0);
ReturnCoinsRequest = __decorate([
    checkable.Checkable.Class()
], ReturnCoinsRequest);
exports.ReturnCoinsRequest = ReturnCoinsRequest;
let Timestamp = class Timestamp {
};
__decorate([
    checkable.Checkable.Number()
], Timestamp.prototype, "t_ms", void 0);
Timestamp = __decorate([
    checkable.Checkable.Class()
], Timestamp);
exports.Timestamp = Timestamp;
function getTimestampNow() {
    return {
        t_ms: new Date().getTime(),
    };
}
exports.getTimestampNow = getTimestampNow;

});

unwrapExports(walletTypes);
var walletTypes_1 = walletTypes.CreateReserveResponse;
var walletTypes_2 = walletTypes.mkAmount;
var walletTypes_3 = walletTypes.CreateReserveRequest;
var walletTypes_4 = walletTypes.ConfirmReserveRequest;
var walletTypes_5 = walletTypes.ReturnCoinsRequest;
var walletTypes_6 = walletTypes.Timestamp;
var walletTypes_7 = walletTypes.getTimestampNow;

var dbTypes = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2018 GNUnet e.V. and INRIA

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __decorate = (commonjsGlobal && commonjsGlobal.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Types for records stored in the wallet's database.
 *
 * Types for the objects in the database should end in "-Record".
 */
/**
 * Imports.
 */





/**
 * Current database version, should be incremented
 * each time we do incompatible schema changes on the database.
 * In the future we might consider adding migration functions for
 * each version increment.
 */
exports.WALLET_DB_VERSION = 28;
var ReserveRecordStatus;
(function (ReserveRecordStatus) {
    /**
     * Waiting for manual confirmation.
     */
    ReserveRecordStatus["UNCONFIRMED"] = "unconfirmed";
    /**
     * Reserve must be registered with the bank.
     */
    ReserveRecordStatus["REGISTERING_BANK"] = "registering-bank";
    /**
     * We've registered reserve's information with the bank
     * and are now waiting for the user to confirm the withdraw
     * with the bank (typically 2nd factor auth).
     */
    ReserveRecordStatus["WAIT_CONFIRM_BANK"] = "wait-confirm-bank";
    /**
     * Querying reserve status with the exchange.
     */
    ReserveRecordStatus["QUERYING_STATUS"] = "querying-status";
    /**
     * Status is queried, the wallet must now select coins
     * and start withdrawing.
     */
    ReserveRecordStatus["WITHDRAWING"] = "withdrawing";
    /**
     * The corresponding withdraw record has been created.
     * No further processing is done, unless explicitly requested
     * by the user.
     */
    ReserveRecordStatus["DORMANT"] = "dormant";
})(ReserveRecordStatus = exports.ReserveRecordStatus || (exports.ReserveRecordStatus = {}));
/**
 * Status of a denomination.
 */
var DenominationStatus;
(function (DenominationStatus) {
    /**
     * Verification was delayed.
     */
    DenominationStatus[DenominationStatus["Unverified"] = 0] = "Unverified";
    /**
     * Verified as valid.
     */
    DenominationStatus[DenominationStatus["VerifiedGood"] = 1] = "VerifiedGood";
    /**
     * Verified as invalid.
     */
    DenominationStatus[DenominationStatus["VerifiedBad"] = 2] = "VerifiedBad";
})(DenominationStatus = exports.DenominationStatus || (exports.DenominationStatus = {}));
/**
 * Denomination record as stored in the wallet's database.
 */
let DenominationRecord = class DenominationRecord {
};
__decorate([
    checkable.Checkable.Value(() => amounts.AmountJson)
], DenominationRecord.prototype, "value", void 0);
__decorate([
    checkable.Checkable.String()
], DenominationRecord.prototype, "denomPub", void 0);
__decorate([
    checkable.Checkable.String()
], DenominationRecord.prototype, "denomPubHash", void 0);
__decorate([
    checkable.Checkable.Value(() => amounts.AmountJson)
], DenominationRecord.prototype, "feeWithdraw", void 0);
__decorate([
    checkable.Checkable.Value(() => amounts.AmountJson)
], DenominationRecord.prototype, "feeDeposit", void 0);
__decorate([
    checkable.Checkable.Value(() => amounts.AmountJson)
], DenominationRecord.prototype, "feeRefresh", void 0);
__decorate([
    checkable.Checkable.Value(() => amounts.AmountJson)
], DenominationRecord.prototype, "feeRefund", void 0);
__decorate([
    checkable.Checkable.Value(() => walletTypes.Timestamp)
], DenominationRecord.prototype, "stampStart", void 0);
__decorate([
    checkable.Checkable.Value(() => walletTypes.Timestamp)
], DenominationRecord.prototype, "stampExpireWithdraw", void 0);
__decorate([
    checkable.Checkable.Value(() => walletTypes.Timestamp)
], DenominationRecord.prototype, "stampExpireLegal", void 0);
__decorate([
    checkable.Checkable.Value(() => walletTypes.Timestamp)
], DenominationRecord.prototype, "stampExpireDeposit", void 0);
__decorate([
    checkable.Checkable.String()
], DenominationRecord.prototype, "masterSig", void 0);
__decorate([
    checkable.Checkable.Number()
], DenominationRecord.prototype, "status", void 0);
__decorate([
    checkable.Checkable.Boolean()
], DenominationRecord.prototype, "isOffered", void 0);
__decorate([
    checkable.Checkable.String()
], DenominationRecord.prototype, "exchangeBaseUrl", void 0);
DenominationRecord = __decorate([
    checkable.Checkable.Class()
], DenominationRecord);
exports.DenominationRecord = DenominationRecord;
var ExchangeUpdateStatus;
(function (ExchangeUpdateStatus) {
    ExchangeUpdateStatus["FETCH_KEYS"] = "fetch_keys";
    ExchangeUpdateStatus["FETCH_WIRE"] = "fetch_wire";
    ExchangeUpdateStatus["FINISHED"] = "finished";
})(ExchangeUpdateStatus = exports.ExchangeUpdateStatus || (exports.ExchangeUpdateStatus = {}));
/**
 * Status of a coin.
 */
var CoinStatus;
(function (CoinStatus) {
    /**
     * Withdrawn and never shown to anybody.
     */
    CoinStatus["Fresh"] = "fresh";
    /**
     * Used for a completed transaction and now dirty.
     */
    CoinStatus["Dirty"] = "dirty";
    /**
     * A coin that has been spent and refreshed.
     */
    CoinStatus["Dormant"] = "dormant";
})(CoinStatus = exports.CoinStatus || (exports.CoinStatus = {}));
var CoinSource;
(function (CoinSource) {
    CoinSource["Withdraw"] = "withdraw";
    CoinSource["Refresh"] = "refresh";
    CoinSource["Tip"] = "tip";
})(CoinSource = exports.CoinSource || (exports.CoinSource = {}));
var ProposalStatus;
(function (ProposalStatus) {
    /**
     * Not downloaded yet.
     */
    ProposalStatus["DOWNLOADING"] = "downloading";
    /**
     * Proposal downloaded, but the user needs to accept/reject it.
     */
    ProposalStatus["PROPOSED"] = "proposed";
    /**
     * The user has accepted the proposal.
     */
    ProposalStatus["ACCEPTED"] = "accepted";
    /**
     * The user has rejected the proposal.
     */
    ProposalStatus["REJECTED"] = "rejected";
    /**
     * Downloaded proposal was detected as a re-purchase.
     */
    ProposalStatus["REPURCHASE"] = "repurchase";
})(ProposalStatus = exports.ProposalStatus || (exports.ProposalStatus = {}));
let ProposalDownload = class ProposalDownload {
};
__decorate([
    checkable.Checkable.Value(() => talerTypes.ContractTerms)
], ProposalDownload.prototype, "contractTerms", void 0);
__decorate([
    checkable.Checkable.String()
], ProposalDownload.prototype, "merchantSig", void 0);
__decorate([
    checkable.Checkable.String()
], ProposalDownload.prototype, "contractTermsHash", void 0);
ProposalDownload = __decorate([
    checkable.Checkable.Class()
], ProposalDownload);
exports.ProposalDownload = ProposalDownload;
/**
 * Record for a downloaded order, stored in the wallet's database.
 */
let ProposalRecord = class ProposalRecord {
};
__decorate([
    checkable.Checkable.String()
], ProposalRecord.prototype, "url", void 0);
__decorate([
    checkable.Checkable.String()
], ProposalRecord.prototype, "proposalId", void 0);
__decorate([
    checkable.Checkable.Number()
], ProposalRecord.prototype, "timestamp", void 0);
__decorate([
    checkable.Checkable.String()
], ProposalRecord.prototype, "noncePriv", void 0);
__decorate([
    checkable.Checkable.String()
], ProposalRecord.prototype, "noncePub", void 0);
__decorate([
    checkable.Checkable.String()
], ProposalRecord.prototype, "proposalStatus", void 0);
__decorate([
    checkable.Checkable.String()
], ProposalRecord.prototype, "repurchaseProposalId", void 0);
__decorate([
    checkable.Checkable.Optional(checkable.Checkable.String())
], ProposalRecord.prototype, "downloadSessionId", void 0);
ProposalRecord = __decorate([
    checkable.Checkable.Class()
], ProposalRecord);
exports.ProposalRecord = ProposalRecord;
/* tslint:disable:completed-docs */
/**
 * The stores and indices for the wallet database.
 */
var Stores;
(function (Stores) {
    class ExchangesStore extends query.Store {
        constructor() {
            super("exchanges", { keyPath: "baseUrl" });
        }
    }
    class CoinsStore extends query.Store {
        constructor() {
            super("coins", { keyPath: "coinPub" });
            this.exchangeBaseUrlIndex = new query.Index(this, "exchangeBaseUrl", "exchangeBaseUrl");
            this.denomPubIndex = new query.Index(this, "denomPubIndex", "denomPub");
            this.byWithdrawalWithIdx = new query.Index(this, "planchetsByWithdrawalWithIdxIndex", ["withdrawSessionId", "coinIndex"]);
        }
    }
    class ProposalsStore extends query.Store {
        constructor() {
            super("proposals", { keyPath: "proposalId" });
            this.urlIndex = new query.Index(this, "urlIndex", "url");
        }
    }
    class PurchasesStore extends query.Store {
        constructor() {
            super("purchases", { keyPath: "proposalId" });
            this.fulfillmentUrlIndex = new query.Index(this, "fulfillmentUrlIndex", "contractTerms.fulfillment_url");
            this.orderIdIndex = new query.Index(this, "orderIdIndex", "contractTerms.order_id");
        }
    }
    class DenominationsStore extends query.Store {
        constructor() {
            // cast needed because of bug in type annotations
            super("denominations", {
                keyPath: ["exchangeBaseUrl", "denomPub"],
            });
            this.denomPubHashIndex = new query.Index(this, "denomPubHashIndex", "denomPubHash");
            this.exchangeBaseUrlIndex = new query.Index(this, "exchangeBaseUrlIndex", "exchangeBaseUrl");
            this.denomPubIndex = new query.Index(this, "denomPubIndex", "denomPub");
        }
    }
    class CurrenciesStore extends query.Store {
        constructor() {
            super("currencies", { keyPath: "name" });
        }
    }
    class ConfigStore extends query.Store {
        constructor() {
            super("config", { keyPath: "key" });
        }
    }
    class ReservesStore extends query.Store {
        constructor() {
            super("reserves", { keyPath: "reservePub" });
        }
    }
    class TipsStore extends query.Store {
        constructor() {
            super("tips", { keyPath: "tipId" });
        }
    }
    class SenderWiresStore extends query.Store {
        constructor() {
            super("senderWires", { keyPath: "paytoUri" });
        }
    }
    class WithdrawalSessionsStore extends query.Store {
        constructor() {
            super("withdrawals", { keyPath: "withdrawSessionId" });
        }
    }
    class BankWithdrawUrisStore extends query.Store {
        constructor() {
            super("bankWithdrawUris", { keyPath: "talerWithdrawUri" });
        }
    }
    Stores.coins = new CoinsStore();
    Stores.coinsReturns = new query.Store("coinsReturns", {
        keyPath: "contractTermsHash",
    });
    Stores.config = new ConfigStore();
    Stores.currencies = new CurrenciesStore();
    Stores.denominations = new DenominationsStore();
    Stores.exchanges = new ExchangesStore();
    Stores.proposals = new ProposalsStore();
    Stores.refresh = new query.Store("refresh", {
        keyPath: "refreshSessionId",
    });
    Stores.reserves = new ReservesStore();
    Stores.purchases = new PurchasesStore();
    Stores.tips = new TipsStore();
    Stores.senderWires = new SenderWiresStore();
    Stores.withdrawalSession = new WithdrawalSessionsStore();
    Stores.bankWithdrawUris = new BankWithdrawUrisStore();
})(Stores = exports.Stores || (exports.Stores = {}));
/* tslint:enable:completed-docs */

});

unwrapExports(dbTypes);
var dbTypes_1 = dbTypes.WALLET_DB_VERSION;
var dbTypes_2 = dbTypes.ReserveRecordStatus;
var dbTypes_3 = dbTypes.DenominationStatus;
var dbTypes_4 = dbTypes.DenominationRecord;
var dbTypes_5 = dbTypes.ExchangeUpdateStatus;
var dbTypes_6 = dbTypes.CoinStatus;
var dbTypes_7 = dbTypes.CoinSource;
var dbTypes_8 = dbTypes.ProposalStatus;
var dbTypes_9 = dbTypes.ProposalDownload;
var dbTypes_10 = dbTypes.ProposalRecord;
var dbTypes_11 = dbTypes.Stores;

var taleruri = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
function parseWithdrawUri(s) {
    const pfx = "taler://withdraw/";
    if (!s.startsWith(pfx)) {
        return undefined;
    }
    const rest = s.substring(pfx.length);
    let [host, path, withdrawId] = rest.split("/");
    if (path === "-") {
        path = "api/withdraw-operation";
    }
    return {
        statusUrl: `https://${host}/${path}/${withdrawId}`,
    };
}
exports.parseWithdrawUri = parseWithdrawUri;
function parsePayUri(s) {
    if (s.startsWith("https://") || s.startsWith("http://")) {
        return {
            downloadUrl: s,
            sessionId: undefined,
        };
    }
    const pfx = "taler://pay/";
    if (!s.startsWith(pfx)) {
        return undefined;
    }
    const [path, search] = s.slice(pfx.length).split("?");
    let [host, maybePath, maybeInstance, orderId, maybeSessionid] = path.split("/");
    if (!host) {
        return undefined;
    }
    if (!maybePath) {
        return undefined;
    }
    if (!orderId) {
        return undefined;
    }
    if (maybePath === "-") {
        maybePath = "public/";
    }
    else {
        maybePath = decodeURIComponent(maybePath) + "/";
    }
    let maybeInstancePath = "";
    if (maybeInstance !== "-") {
        maybeInstancePath = `instances/${maybeInstance}/`;
    }
    let protocol = "https";
    const searchParams = new URLSearchParams(search);
    if (searchParams.get("insecure") === "1") {
        protocol = "http";
    }
    const downloadUrl = `${protocol}://${host}/` +
        decodeURIComponent(maybePath) +
        maybeInstancePath +
        `proposal?order_id=${orderId}`;
    return {
        downloadUrl,
        sessionId: maybeSessionid,
    };
}
exports.parsePayUri = parsePayUri;
function parseTipUri(s) {
    const pfx = "taler://tip/";
    if (!s.startsWith(pfx)) {
        return undefined;
    }
    const path = s.slice(pfx.length);
    let [host, maybePath, maybeInstance, tipId] = path.split("/");
    if (!host) {
        return undefined;
    }
    if (!maybePath) {
        return undefined;
    }
    if (!tipId) {
        return undefined;
    }
    if (maybePath === "-") {
        maybePath = "public/";
    }
    else {
        maybePath = decodeURIComponent(maybePath) + "/";
    }
    let maybeInstancePath = "";
    if (maybeInstance !== "-") {
        maybeInstancePath = `instances/${maybeInstance}/`;
    }
    const merchantBaseUrl = `https://${host}/${maybePath}${maybeInstancePath}`;
    return {
        merchantTipId: tipId,
        merchantOrigin: new URL(merchantBaseUrl).origin,
        merchantBaseUrl,
    };
}
exports.parseTipUri = parseTipUri;
function parseRefundUri(s) {
    const pfx = "taler://refund/";
    if (!s.startsWith(pfx)) {
        return undefined;
    }
    const path = s.slice(pfx.length);
    let [host, maybePath, maybeInstance, orderId] = path.split("/");
    if (!host) {
        return undefined;
    }
    if (!maybePath) {
        return undefined;
    }
    if (!orderId) {
        return undefined;
    }
    if (maybePath === "-") {
        maybePath = "public/";
    }
    else {
        maybePath = decodeURIComponent(maybePath) + "/";
    }
    let maybeInstancePath = "";
    if (maybeInstance !== "-") {
        maybeInstancePath = `instances/${maybeInstance}/`;
    }
    const refundUrl = "https://" +
        host +
        "/" +
        maybePath +
        maybeInstancePath +
        "refund" +
        "?order_id=" +
        orderId;
    return {
        refundUrl,
    };
}
exports.parseRefundUri = parseRefundUri;

});

unwrapExports(taleruri);
var taleruri_1 = taleruri.parseWithdrawUri;
var taleruri_2 = taleruri.parsePayUri;
var taleruri_3 = taleruri.parseTipUri;
var taleruri_4 = taleruri.parseRefundUri;

var logging = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2019 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
class Logger {
    constructor(tag) {
        this.tag = tag;
    }
    info(message, ...args) {
        console.log(`${new Date().toISOString()} ${this.tag} INFO ` + message, ...args);
    }
    trace(message, ...args) {
        console.log(`${new Date().toISOString()} ${this.tag} TRACE ` + message, ...args);
    }
}
exports.Logger = Logger;

});

unwrapExports(logging);
var logging_1 = logging.Logger;

var payto = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
function parsePaytoUri(s) {
    const pfx = "payto://";
    if (!s.startsWith(pfx)) {
        return undefined;
    }
    const [acct, search] = s.slice(pfx.length).split("?");
    const firstSlashPos = acct.indexOf("/");
    if (firstSlashPos === -1) {
        return undefined;
    }
    const targetType = acct.slice(0, firstSlashPos);
    const targetPath = acct.slice(firstSlashPos + 1);
    const params = {};
    const searchParams = new URLSearchParams(search || "");
    searchParams.forEach((v, k) => {
        params[v] = k;
    });
    return {
        targetPath,
        targetType,
        params,
    };
}
exports.parsePaytoUri = parsePaytoUri;

});

unwrapExports(payto);
var payto_1 = payto.parsePaytoUri;

var exchanges = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });






const Amounts = __importStar(amounts);

function denominationRecordFromKeys(ws, exchangeBaseUrl, denomIn) {
    return __awaiter(this, void 0, void 0, function* () {
        const denomPubHash = yield ws.cryptoApi.hashDenomPub(denomIn.denom_pub);
        const d = {
            denomPub: denomIn.denom_pub,
            denomPubHash,
            exchangeBaseUrl,
            feeDeposit: Amounts.parseOrThrow(denomIn.fee_deposit),
            feeRefresh: Amounts.parseOrThrow(denomIn.fee_refresh),
            feeRefund: Amounts.parseOrThrow(denomIn.fee_refund),
            feeWithdraw: Amounts.parseOrThrow(denomIn.fee_withdraw),
            isOffered: true,
            masterSig: denomIn.master_sig,
            stampExpireDeposit: helpers.extractTalerStampOrThrow(denomIn.stamp_expire_deposit),
            stampExpireLegal: helpers.extractTalerStampOrThrow(denomIn.stamp_expire_legal),
            stampExpireWithdraw: helpers.extractTalerStampOrThrow(denomIn.stamp_expire_withdraw),
            stampStart: helpers.extractTalerStampOrThrow(denomIn.stamp_start),
            status: dbTypes.DenominationStatus.Unverified,
            value: Amounts.parseOrThrow(denomIn.value),
        };
        return d;
    });
}
function setExchangeError(ws, baseUrl, err) {
    return __awaiter(this, void 0, void 0, function* () {
        const mut = (exchange) => {
            exchange.lastError = err;
            return exchange;
        };
        yield query.oneShotMutate(ws.db, dbTypes.Stores.exchanges, baseUrl, mut);
    });
}
/**
 * Fetch the exchange's /keys and update our database accordingly.
 *
 * Exceptions thrown in this method must be caught and reported
 * in the pending operations.
 */
function updateExchangeWithKeys(ws, baseUrl) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function* () {
        const existingExchangeRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, baseUrl);
        if (((_a = existingExchangeRecord) === null || _a === void 0 ? void 0 : _a.updateStatus) != dbTypes.ExchangeUpdateStatus.FETCH_KEYS) {
            return;
        }
        const keysUrl = new URL("keys", baseUrl);
        keysUrl.searchParams.set("cacheBreaker", wallet.WALLET_CACHE_BREAKER_CLIENT_VERSION);
        let keysResp;
        try {
            keysResp = yield ws.http.get(keysUrl.href);
        }
        catch (e) {
            const m = `Fetching keys failed: ${e.message}`;
            yield setExchangeError(ws, baseUrl, {
                type: "network",
                details: {
                    requestUrl: (_b = e.config) === null || _b === void 0 ? void 0 : _b.url,
                },
                message: m,
            });
            throw new wallet.OperationFailedAndReportedError(m);
        }
        let exchangeKeysJson;
        try {
            exchangeKeysJson = talerTypes.KeysJson.checked(keysResp.responseJson);
        }
        catch (e) {
            const m = `Parsing /keys response failed: ${e.message}`;
            yield setExchangeError(ws, baseUrl, {
                type: "protocol-violation",
                details: {},
                message: m,
            });
            throw new wallet.OperationFailedAndReportedError(m);
        }
        const lastUpdateTimestamp = helpers.extractTalerStamp(exchangeKeysJson.list_issue_date);
        if (!lastUpdateTimestamp) {
            const m = `Parsing /keys response failed: invalid list_issue_date.`;
            yield setExchangeError(ws, baseUrl, {
                type: "protocol-violation",
                details: {},
                message: m,
            });
            throw new wallet.OperationFailedAndReportedError(m);
        }
        if (exchangeKeysJson.denoms.length === 0) {
            const m = "exchange doesn't offer any denominations";
            yield setExchangeError(ws, baseUrl, {
                type: "protocol-violation",
                details: {},
                message: m,
            });
            throw new wallet.OperationFailedAndReportedError(m);
        }
        const protocolVersion = exchangeKeysJson.version;
        if (!protocolVersion) {
            const m = "outdate exchange, no version in /keys response";
            yield setExchangeError(ws, baseUrl, {
                type: "protocol-violation",
                details: {},
                message: m,
            });
            throw new wallet.OperationFailedAndReportedError(m);
        }
        const currency = Amounts.parseOrThrow(exchangeKeysJson.denoms[0].value)
            .currency;
        const newDenominations = yield Promise.all(exchangeKeysJson.denoms.map(d => denominationRecordFromKeys(ws, baseUrl, d)));
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.exchanges, dbTypes.Stores.denominations], (tx) => __awaiter(this, void 0, void 0, function* () {
            const r = yield tx.get(dbTypes.Stores.exchanges, baseUrl);
            if (!r) {
                console.warn(`exchange ${baseUrl} no longer present`);
                return;
            }
            if (r.details) ;
            r.details = {
                auditors: exchangeKeysJson.auditors,
                currency: currency,
                lastUpdateTime: lastUpdateTimestamp,
                masterPublicKey: exchangeKeysJson.master_public_key,
                protocolVersion: protocolVersion,
            };
            r.updateStatus = dbTypes.ExchangeUpdateStatus.FETCH_WIRE;
            r.lastError = undefined;
            yield tx.put(dbTypes.Stores.exchanges, r);
            for (const newDenom of newDenominations) {
                const oldDenom = yield tx.get(dbTypes.Stores.denominations, [
                    baseUrl,
                    newDenom.denomPub,
                ]);
                if (oldDenom) ;
                else {
                    yield tx.put(dbTypes.Stores.denominations, newDenom);
                }
            }
        }));
    });
}
/**
 * Fetch wire information for an exchange and store it in the database.
 *
 * @param exchangeBaseUrl Exchange base URL, assumed to be already normalized.
 */
function updateExchangeWithWireInfo(ws, exchangeBaseUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        const exchange = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, exchangeBaseUrl);
        if (!exchange) {
            return;
        }
        if (exchange.updateStatus != dbTypes.ExchangeUpdateStatus.FETCH_WIRE) {
            return;
        }
        const reqUrl = new URL("wire", exchangeBaseUrl);
        reqUrl.searchParams.set("cacheBreaker", wallet.WALLET_CACHE_BREAKER_CLIENT_VERSION);
        const resp = yield ws.http.get(reqUrl.href);
        const wiJson = resp.responseJson;
        if (!wiJson) {
            throw Error("/wire response malformed");
        }
        const wireInfo = talerTypes.ExchangeWireJson.checked(wiJson);
        const feesForType = {};
        for (const wireMethod of Object.keys(wireInfo.fees)) {
            const feeList = [];
            for (const x of wireInfo.fees[wireMethod]) {
                const startStamp = helpers.extractTalerStamp(x.start_date);
                if (!startStamp) {
                    throw Error("wrong date format");
                }
                const endStamp = helpers.extractTalerStamp(x.end_date);
                if (!endStamp) {
                    throw Error("wrong date format");
                }
                feeList.push({
                    closingFee: Amounts.parseOrThrow(x.closing_fee),
                    endStamp,
                    sig: x.sig,
                    startStamp,
                    wireFee: Amounts.parseOrThrow(x.wire_fee),
                });
            }
            feesForType[wireMethod] = feeList;
        }
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.exchanges], (tx) => __awaiter(this, void 0, void 0, function* () {
            const r = yield tx.get(dbTypes.Stores.exchanges, exchangeBaseUrl);
            if (!r) {
                return;
            }
            if (r.updateStatus != dbTypes.ExchangeUpdateStatus.FETCH_WIRE) {
                return;
            }
            r.wireInfo = {
                accounts: wireInfo.accounts,
                feesForType: feesForType,
            };
            r.updateStatus = dbTypes.ExchangeUpdateStatus.FINISHED;
            r.lastError = undefined;
            yield tx.put(dbTypes.Stores.exchanges, r);
        }));
    });
}
/**
 * Update or add exchange DB entry by fetching the /keys and /wire information.
 * Optionally link the reserve entry to the new or existing
 * exchange entry in then DB.
 */
function updateExchangeFromUrl(ws, baseUrl, force = false) {
    return __awaiter(this, void 0, void 0, function* () {
        const now = walletTypes.getTimestampNow();
        baseUrl = helpers.canonicalizeBaseUrl(baseUrl);
        const r = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, baseUrl);
        if (!r) {
            const newExchangeRecord = {
                baseUrl: baseUrl,
                details: undefined,
                wireInfo: undefined,
                updateStatus: dbTypes.ExchangeUpdateStatus.FETCH_KEYS,
                updateStarted: now,
                updateReason: "initial",
                timestampAdded: walletTypes.getTimestampNow(),
            };
            yield query.oneShotPut(ws.db, dbTypes.Stores.exchanges, newExchangeRecord);
        }
        else {
            yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.exchanges], (t) => __awaiter(this, void 0, void 0, function* () {
                const rec = yield t.get(dbTypes.Stores.exchanges, baseUrl);
                if (!rec) {
                    return;
                }
                if (rec.updateStatus != dbTypes.ExchangeUpdateStatus.FETCH_KEYS && !force) {
                    return;
                }
                if (rec.updateStatus != dbTypes.ExchangeUpdateStatus.FETCH_KEYS && force) {
                    rec.updateReason = "forced";
                }
                rec.updateStarted = now;
                rec.updateStatus = dbTypes.ExchangeUpdateStatus.FETCH_KEYS;
                rec.lastError = undefined;
                t.put(dbTypes.Stores.exchanges, rec);
            }));
        }
        yield updateExchangeWithKeys(ws, baseUrl);
        yield updateExchangeWithWireInfo(ws, baseUrl);
        const updatedExchange = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, baseUrl);
        if (!updatedExchange) {
            // This should practically never happen
            throw Error("exchange not found");
        }
        return updatedExchange;
    });
}
exports.updateExchangeFromUrl = updateExchangeFromUrl;
/**
 * Check if and how an exchange is trusted and/or audited.
 */
function getExchangeTrust(ws, exchangeInfo) {
    return __awaiter(this, void 0, void 0, function* () {
        let isTrusted = false;
        let isAudited = false;
        const exchangeDetails = exchangeInfo.details;
        if (!exchangeDetails) {
            throw Error(`exchange ${exchangeInfo.baseUrl} details not available`);
        }
        const currencyRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.currencies, exchangeDetails.currency);
        if (currencyRecord) {
            for (const trustedExchange of currencyRecord.exchanges) {
                if (trustedExchange.exchangePub === exchangeDetails.masterPublicKey) {
                    isTrusted = true;
                    break;
                }
            }
            for (const trustedAuditor of currencyRecord.auditors) {
                for (const exchangeAuditor of exchangeDetails.auditors) {
                    if (trustedAuditor.auditorPub === exchangeAuditor.auditor_pub) {
                        isAudited = true;
                        break;
                    }
                }
            }
        }
        return { isTrusted, isAudited };
    });
}
exports.getExchangeTrust = getExchangeTrust;
function getExchangePaytoUri(ws, exchangeBaseUrl, supportedTargetTypes) {
    return __awaiter(this, void 0, void 0, function* () {
        // We do the update here, since the exchange might not even exist
        // yet in our database.
        const exchangeRecord = yield updateExchangeFromUrl(ws, exchangeBaseUrl);
        if (!exchangeRecord) {
            throw Error(`Exchange '${exchangeBaseUrl}' not found.`);
        }
        const exchangeWireInfo = exchangeRecord.wireInfo;
        if (!exchangeWireInfo) {
            throw Error(`Exchange wire info for '${exchangeBaseUrl}' not found.`);
        }
        for (let account of exchangeWireInfo.accounts) {
            const res = payto.parsePaytoUri(account.url);
            if (!res) {
                continue;
            }
            if (supportedTargetTypes.includes(res.targetType)) {
                return account.url;
            }
        }
        throw Error("no matching exchange account found");
    });
}
exports.getExchangePaytoUri = getExchangePaytoUri;

});

unwrapExports(exchanges);
var exchanges_1 = exchanges.updateExchangeFromUrl;
var exchanges_2 = exchanges.getExchangeTrust;
var exchanges_3 = exchanges.getExchangePaytoUri;

var assertUnreachable_1 = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
function assertUnreachable(x) {
    throw new Error("Didn't expect to get here");
}
exports.assertUnreachable = assertUnreachable;

});

unwrapExports(assertUnreachable_1);
var assertUnreachable_2 = assertUnreachable_1.assertUnreachable;

var naclFast = createCommonjsModule(function (module, exports) {
// Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.
// TypeScript port in 2019 by Florian Dold.
// Public domain.
//
// Implementation derived from TweetNaCl version 20140427.
// See for details: http://tweetnacl.cr.yp.to/
Object.defineProperty(exports, "__esModule", { value: true });
const gf = function (init = []) {
    const r = new Float64Array(16);
    if (init)
        for (let i = 0; i < init.length; i++)
            r[i] = init[i];
    return r;
};
//  Pluggable, initialized in high-level API below.
let randombytes = function (x, n) {
    throw new Error("no PRNG");
};
const _0 = new Uint8Array(16);
const _9 = new Uint8Array(32);
_9[0] = 9;
// prettier-ignore
const gf0 = gf();
const gf1 = gf([1]);
const _121665 = gf([0xdb41, 1]);
const D = gf([
    0x78a3,
    0x1359,
    0x4dca,
    0x75eb,
    0xd8ab,
    0x4141,
    0x0a4d,
    0x0070,
    0xe898,
    0x7779,
    0x4079,
    0x8cc7,
    0xfe73,
    0x2b6f,
    0x6cee,
    0x5203,
]);
const D2 = gf([
    0xf159,
    0x26b2,
    0x9b94,
    0xebd6,
    0xb156,
    0x8283,
    0x149a,
    0x00e0,
    0xd130,
    0xeef3,
    0x80f2,
    0x198e,
    0xfce7,
    0x56df,
    0xd9dc,
    0x2406,
]);
const X = gf([
    0xd51a,
    0x8f25,
    0x2d60,
    0xc956,
    0xa7b2,
    0x9525,
    0xc760,
    0x692c,
    0xdc5c,
    0xfdd6,
    0xe231,
    0xc0a4,
    0x53fe,
    0xcd6e,
    0x36d3,
    0x2169,
]);
const Y = gf([
    0x6658,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
    0x6666,
]);
const I = gf([
    0xa0b0,
    0x4a0e,
    0x1b27,
    0xc4ee,
    0xe478,
    0xad2f,
    0x1806,
    0x2f43,
    0xd7a7,
    0x3dfb,
    0x0099,
    0x2b4d,
    0xdf0b,
    0x4fc1,
    0x2480,
    0x2b83,
]);
function ts64(x, i, h, l) {
    x[i] = (h >> 24) & 0xff;
    x[i + 1] = (h >> 16) & 0xff;
    x[i + 2] = (h >> 8) & 0xff;
    x[i + 3] = h & 0xff;
    x[i + 4] = (l >> 24) & 0xff;
    x[i + 5] = (l >> 16) & 0xff;
    x[i + 6] = (l >> 8) & 0xff;
    x[i + 7] = l & 0xff;
}
function vn(x, xi, y, yi, n) {
    var i, d = 0;
    for (i = 0; i < n; i++)
        d |= x[xi + i] ^ y[yi + i];
    return (1 & ((d - 1) >>> 8)) - 1;
}
function crypto_verify_16(x, xi, y, yi) {
    return vn(x, xi, y, yi, 16);
}
function crypto_verify_32(x, xi, y, yi) {
    return vn(x, xi, y, yi, 32);
}
// prettier-ignore
function core_salsa20(o, p, k, c) {
    var j0 = c[0] & 0xff | (c[1] & 0xff) << 8 | (c[2] & 0xff) << 16 | (c[3] & 0xff) << 24, j1 = k[0] & 0xff | (k[1] & 0xff) << 8 | (k[2] & 0xff) << 16 | (k[3] & 0xff) << 24, j2 = k[4] & 0xff | (k[5] & 0xff) << 8 | (k[6] & 0xff) << 16 | (k[7] & 0xff) << 24, j3 = k[8] & 0xff | (k[9] & 0xff) << 8 | (k[10] & 0xff) << 16 | (k[11] & 0xff) << 24, j4 = k[12] & 0xff | (k[13] & 0xff) << 8 | (k[14] & 0xff) << 16 | (k[15] & 0xff) << 24, j5 = c[4] & 0xff | (c[5] & 0xff) << 8 | (c[6] & 0xff) << 16 | (c[7] & 0xff) << 24, j6 = p[0] & 0xff | (p[1] & 0xff) << 8 | (p[2] & 0xff) << 16 | (p[3] & 0xff) << 24, j7 = p[4] & 0xff | (p[5] & 0xff) << 8 | (p[6] & 0xff) << 16 | (p[7] & 0xff) << 24, j8 = p[8] & 0xff | (p[9] & 0xff) << 8 | (p[10] & 0xff) << 16 | (p[11] & 0xff) << 24, j9 = p[12] & 0xff | (p[13] & 0xff) << 8 | (p[14] & 0xff) << 16 | (p[15] & 0xff) << 24, j10 = c[8] & 0xff | (c[9] & 0xff) << 8 | (c[10] & 0xff) << 16 | (c[11] & 0xff) << 24, j11 = k[16] & 0xff | (k[17] & 0xff) << 8 | (k[18] & 0xff) << 16 | (k[19] & 0xff) << 24, j12 = k[20] & 0xff | (k[21] & 0xff) << 8 | (k[22] & 0xff) << 16 | (k[23] & 0xff) << 24, j13 = k[24] & 0xff | (k[25] & 0xff) << 8 | (k[26] & 0xff) << 16 | (k[27] & 0xff) << 24, j14 = k[28] & 0xff | (k[29] & 0xff) << 8 | (k[30] & 0xff) << 16 | (k[31] & 0xff) << 24, j15 = c[12] & 0xff | (c[13] & 0xff) << 8 | (c[14] & 0xff) << 16 | (c[15] & 0xff) << 24;
    var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
    for (var i = 0; i < 20; i += 2) {
        u = x0 + x12 | 0;
        x4 ^= u << 7 | u >>> (32 - 7);
        u = x4 + x0 | 0;
        x8 ^= u << 9 | u >>> (32 - 9);
        u = x8 + x4 | 0;
        x12 ^= u << 13 | u >>> (32 - 13);
        u = x12 + x8 | 0;
        x0 ^= u << 18 | u >>> (32 - 18);
        u = x5 + x1 | 0;
        x9 ^= u << 7 | u >>> (32 - 7);
        u = x9 + x5 | 0;
        x13 ^= u << 9 | u >>> (32 - 9);
        u = x13 + x9 | 0;
        x1 ^= u << 13 | u >>> (32 - 13);
        u = x1 + x13 | 0;
        x5 ^= u << 18 | u >>> (32 - 18);
        u = x10 + x6 | 0;
        x14 ^= u << 7 | u >>> (32 - 7);
        u = x14 + x10 | 0;
        x2 ^= u << 9 | u >>> (32 - 9);
        u = x2 + x14 | 0;
        x6 ^= u << 13 | u >>> (32 - 13);
        u = x6 + x2 | 0;
        x10 ^= u << 18 | u >>> (32 - 18);
        u = x15 + x11 | 0;
        x3 ^= u << 7 | u >>> (32 - 7);
        u = x3 + x15 | 0;
        x7 ^= u << 9 | u >>> (32 - 9);
        u = x7 + x3 | 0;
        x11 ^= u << 13 | u >>> (32 - 13);
        u = x11 + x7 | 0;
        x15 ^= u << 18 | u >>> (32 - 18);
        u = x0 + x3 | 0;
        x1 ^= u << 7 | u >>> (32 - 7);
        u = x1 + x0 | 0;
        x2 ^= u << 9 | u >>> (32 - 9);
        u = x2 + x1 | 0;
        x3 ^= u << 13 | u >>> (32 - 13);
        u = x3 + x2 | 0;
        x0 ^= u << 18 | u >>> (32 - 18);
        u = x5 + x4 | 0;
        x6 ^= u << 7 | u >>> (32 - 7);
        u = x6 + x5 | 0;
        x7 ^= u << 9 | u >>> (32 - 9);
        u = x7 + x6 | 0;
        x4 ^= u << 13 | u >>> (32 - 13);
        u = x4 + x7 | 0;
        x5 ^= u << 18 | u >>> (32 - 18);
        u = x10 + x9 | 0;
        x11 ^= u << 7 | u >>> (32 - 7);
        u = x11 + x10 | 0;
        x8 ^= u << 9 | u >>> (32 - 9);
        u = x8 + x11 | 0;
        x9 ^= u << 13 | u >>> (32 - 13);
        u = x9 + x8 | 0;
        x10 ^= u << 18 | u >>> (32 - 18);
        u = x15 + x14 | 0;
        x12 ^= u << 7 | u >>> (32 - 7);
        u = x12 + x15 | 0;
        x13 ^= u << 9 | u >>> (32 - 9);
        u = x13 + x12 | 0;
        x14 ^= u << 13 | u >>> (32 - 13);
        u = x14 + x13 | 0;
        x15 ^= u << 18 | u >>> (32 - 18);
    }
    x0 = x0 + j0 | 0;
    x1 = x1 + j1 | 0;
    x2 = x2 + j2 | 0;
    x3 = x3 + j3 | 0;
    x4 = x4 + j4 | 0;
    x5 = x5 + j5 | 0;
    x6 = x6 + j6 | 0;
    x7 = x7 + j7 | 0;
    x8 = x8 + j8 | 0;
    x9 = x9 + j9 | 0;
    x10 = x10 + j10 | 0;
    x11 = x11 + j11 | 0;
    x12 = x12 + j12 | 0;
    x13 = x13 + j13 | 0;
    x14 = x14 + j14 | 0;
    x15 = x15 + j15 | 0;
    o[0] = x0 >>> 0 & 0xff;
    o[1] = x0 >>> 8 & 0xff;
    o[2] = x0 >>> 16 & 0xff;
    o[3] = x0 >>> 24 & 0xff;
    o[4] = x1 >>> 0 & 0xff;
    o[5] = x1 >>> 8 & 0xff;
    o[6] = x1 >>> 16 & 0xff;
    o[7] = x1 >>> 24 & 0xff;
    o[8] = x2 >>> 0 & 0xff;
    o[9] = x2 >>> 8 & 0xff;
    o[10] = x2 >>> 16 & 0xff;
    o[11] = x2 >>> 24 & 0xff;
    o[12] = x3 >>> 0 & 0xff;
    o[13] = x3 >>> 8 & 0xff;
    o[14] = x3 >>> 16 & 0xff;
    o[15] = x3 >>> 24 & 0xff;
    o[16] = x4 >>> 0 & 0xff;
    o[17] = x4 >>> 8 & 0xff;
    o[18] = x4 >>> 16 & 0xff;
    o[19] = x4 >>> 24 & 0xff;
    o[20] = x5 >>> 0 & 0xff;
    o[21] = x5 >>> 8 & 0xff;
    o[22] = x5 >>> 16 & 0xff;
    o[23] = x5 >>> 24 & 0xff;
    o[24] = x6 >>> 0 & 0xff;
    o[25] = x6 >>> 8 & 0xff;
    o[26] = x6 >>> 16 & 0xff;
    o[27] = x6 >>> 24 & 0xff;
    o[28] = x7 >>> 0 & 0xff;
    o[29] = x7 >>> 8 & 0xff;
    o[30] = x7 >>> 16 & 0xff;
    o[31] = x7 >>> 24 & 0xff;
    o[32] = x8 >>> 0 & 0xff;
    o[33] = x8 >>> 8 & 0xff;
    o[34] = x8 >>> 16 & 0xff;
    o[35] = x8 >>> 24 & 0xff;
    o[36] = x9 >>> 0 & 0xff;
    o[37] = x9 >>> 8 & 0xff;
    o[38] = x9 >>> 16 & 0xff;
    o[39] = x9 >>> 24 & 0xff;
    o[40] = x10 >>> 0 & 0xff;
    o[41] = x10 >>> 8 & 0xff;
    o[42] = x10 >>> 16 & 0xff;
    o[43] = x10 >>> 24 & 0xff;
    o[44] = x11 >>> 0 & 0xff;
    o[45] = x11 >>> 8 & 0xff;
    o[46] = x11 >>> 16 & 0xff;
    o[47] = x11 >>> 24 & 0xff;
    o[48] = x12 >>> 0 & 0xff;
    o[49] = x12 >>> 8 & 0xff;
    o[50] = x12 >>> 16 & 0xff;
    o[51] = x12 >>> 24 & 0xff;
    o[52] = x13 >>> 0 & 0xff;
    o[53] = x13 >>> 8 & 0xff;
    o[54] = x13 >>> 16 & 0xff;
    o[55] = x13 >>> 24 & 0xff;
    o[56] = x14 >>> 0 & 0xff;
    o[57] = x14 >>> 8 & 0xff;
    o[58] = x14 >>> 16 & 0xff;
    o[59] = x14 >>> 24 & 0xff;
    o[60] = x15 >>> 0 & 0xff;
    o[61] = x15 >>> 8 & 0xff;
    o[62] = x15 >>> 16 & 0xff;
    o[63] = x15 >>> 24 & 0xff;
}
function core_hsalsa20(o, p, k, c) {
    var j0 = (c[0] & 0xff) |
        ((c[1] & 0xff) << 8) |
        ((c[2] & 0xff) << 16) |
        ((c[3] & 0xff) << 24), j1 = (k[0] & 0xff) |
        ((k[1] & 0xff) << 8) |
        ((k[2] & 0xff) << 16) |
        ((k[3] & 0xff) << 24), j2 = (k[4] & 0xff) |
        ((k[5] & 0xff) << 8) |
        ((k[6] & 0xff) << 16) |
        ((k[7] & 0xff) << 24), j3 = (k[8] & 0xff) |
        ((k[9] & 0xff) << 8) |
        ((k[10] & 0xff) << 16) |
        ((k[11] & 0xff) << 24), j4 = (k[12] & 0xff) |
        ((k[13] & 0xff) << 8) |
        ((k[14] & 0xff) << 16) |
        ((k[15] & 0xff) << 24), j5 = (c[4] & 0xff) |
        ((c[5] & 0xff) << 8) |
        ((c[6] & 0xff) << 16) |
        ((c[7] & 0xff) << 24), j6 = (p[0] & 0xff) |
        ((p[1] & 0xff) << 8) |
        ((p[2] & 0xff) << 16) |
        ((p[3] & 0xff) << 24), j7 = (p[4] & 0xff) |
        ((p[5] & 0xff) << 8) |
        ((p[6] & 0xff) << 16) |
        ((p[7] & 0xff) << 24), j8 = (p[8] & 0xff) |
        ((p[9] & 0xff) << 8) |
        ((p[10] & 0xff) << 16) |
        ((p[11] & 0xff) << 24), j9 = (p[12] & 0xff) |
        ((p[13] & 0xff) << 8) |
        ((p[14] & 0xff) << 16) |
        ((p[15] & 0xff) << 24), j10 = (c[8] & 0xff) |
        ((c[9] & 0xff) << 8) |
        ((c[10] & 0xff) << 16) |
        ((c[11] & 0xff) << 24), j11 = (k[16] & 0xff) |
        ((k[17] & 0xff) << 8) |
        ((k[18] & 0xff) << 16) |
        ((k[19] & 0xff) << 24), j12 = (k[20] & 0xff) |
        ((k[21] & 0xff) << 8) |
        ((k[22] & 0xff) << 16) |
        ((k[23] & 0xff) << 24), j13 = (k[24] & 0xff) |
        ((k[25] & 0xff) << 8) |
        ((k[26] & 0xff) << 16) |
        ((k[27] & 0xff) << 24), j14 = (k[28] & 0xff) |
        ((k[29] & 0xff) << 8) |
        ((k[30] & 0xff) << 16) |
        ((k[31] & 0xff) << 24), j15 = (c[12] & 0xff) |
        ((c[13] & 0xff) << 8) |
        ((c[14] & 0xff) << 16) |
        ((c[15] & 0xff) << 24);
    var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
    for (var i = 0; i < 20; i += 2) {
        u = (x0 + x12) | 0;
        x4 ^= (u << 7) | (u >>> (32 - 7));
        u = (x4 + x0) | 0;
        x8 ^= (u << 9) | (u >>> (32 - 9));
        u = (x8 + x4) | 0;
        x12 ^= (u << 13) | (u >>> (32 - 13));
        u = (x12 + x8) | 0;
        x0 ^= (u << 18) | (u >>> (32 - 18));
        u = (x5 + x1) | 0;
        x9 ^= (u << 7) | (u >>> (32 - 7));
        u = (x9 + x5) | 0;
        x13 ^= (u << 9) | (u >>> (32 - 9));
        u = (x13 + x9) | 0;
        x1 ^= (u << 13) | (u >>> (32 - 13));
        u = (x1 + x13) | 0;
        x5 ^= (u << 18) | (u >>> (32 - 18));
        u = (x10 + x6) | 0;
        x14 ^= (u << 7) | (u >>> (32 - 7));
        u = (x14 + x10) | 0;
        x2 ^= (u << 9) | (u >>> (32 - 9));
        u = (x2 + x14) | 0;
        x6 ^= (u << 13) | (u >>> (32 - 13));
        u = (x6 + x2) | 0;
        x10 ^= (u << 18) | (u >>> (32 - 18));
        u = (x15 + x11) | 0;
        x3 ^= (u << 7) | (u >>> (32 - 7));
        u = (x3 + x15) | 0;
        x7 ^= (u << 9) | (u >>> (32 - 9));
        u = (x7 + x3) | 0;
        x11 ^= (u << 13) | (u >>> (32 - 13));
        u = (x11 + x7) | 0;
        x15 ^= (u << 18) | (u >>> (32 - 18));
        u = (x0 + x3) | 0;
        x1 ^= (u << 7) | (u >>> (32 - 7));
        u = (x1 + x0) | 0;
        x2 ^= (u << 9) | (u >>> (32 - 9));
        u = (x2 + x1) | 0;
        x3 ^= (u << 13) | (u >>> (32 - 13));
        u = (x3 + x2) | 0;
        x0 ^= (u << 18) | (u >>> (32 - 18));
        u = (x5 + x4) | 0;
        x6 ^= (u << 7) | (u >>> (32 - 7));
        u = (x6 + x5) | 0;
        x7 ^= (u << 9) | (u >>> (32 - 9));
        u = (x7 + x6) | 0;
        x4 ^= (u << 13) | (u >>> (32 - 13));
        u = (x4 + x7) | 0;
        x5 ^= (u << 18) | (u >>> (32 - 18));
        u = (x10 + x9) | 0;
        x11 ^= (u << 7) | (u >>> (32 - 7));
        u = (x11 + x10) | 0;
        x8 ^= (u << 9) | (u >>> (32 - 9));
        u = (x8 + x11) | 0;
        x9 ^= (u << 13) | (u >>> (32 - 13));
        u = (x9 + x8) | 0;
        x10 ^= (u << 18) | (u >>> (32 - 18));
        u = (x15 + x14) | 0;
        x12 ^= (u << 7) | (u >>> (32 - 7));
        u = (x12 + x15) | 0;
        x13 ^= (u << 9) | (u >>> (32 - 9));
        u = (x13 + x12) | 0;
        x14 ^= (u << 13) | (u >>> (32 - 13));
        u = (x14 + x13) | 0;
        x15 ^= (u << 18) | (u >>> (32 - 18));
    }
    o[0] = (x0 >>> 0) & 0xff;
    o[1] = (x0 >>> 8) & 0xff;
    o[2] = (x0 >>> 16) & 0xff;
    o[3] = (x0 >>> 24) & 0xff;
    o[4] = (x5 >>> 0) & 0xff;
    o[5] = (x5 >>> 8) & 0xff;
    o[6] = (x5 >>> 16) & 0xff;
    o[7] = (x5 >>> 24) & 0xff;
    o[8] = (x10 >>> 0) & 0xff;
    o[9] = (x10 >>> 8) & 0xff;
    o[10] = (x10 >>> 16) & 0xff;
    o[11] = (x10 >>> 24) & 0xff;
    o[12] = (x15 >>> 0) & 0xff;
    o[13] = (x15 >>> 8) & 0xff;
    o[14] = (x15 >>> 16) & 0xff;
    o[15] = (x15 >>> 24) & 0xff;
    o[16] = (x6 >>> 0) & 0xff;
    o[17] = (x6 >>> 8) & 0xff;
    o[18] = (x6 >>> 16) & 0xff;
    o[19] = (x6 >>> 24) & 0xff;
    o[20] = (x7 >>> 0) & 0xff;
    o[21] = (x7 >>> 8) & 0xff;
    o[22] = (x7 >>> 16) & 0xff;
    o[23] = (x7 >>> 24) & 0xff;
    o[24] = (x8 >>> 0) & 0xff;
    o[25] = (x8 >>> 8) & 0xff;
    o[26] = (x8 >>> 16) & 0xff;
    o[27] = (x8 >>> 24) & 0xff;
    o[28] = (x9 >>> 0) & 0xff;
    o[29] = (x9 >>> 8) & 0xff;
    o[30] = (x9 >>> 16) & 0xff;
    o[31] = (x9 >>> 24) & 0xff;
}
function crypto_core_salsa20(out, inp, k, c) {
    core_salsa20(out, inp, k, c);
}
function crypto_core_hsalsa20(out, inp, k, c) {
    core_hsalsa20(out, inp, k, c);
}
var sigma = new Uint8Array([
    101,
    120,
    112,
    97,
    110,
    100,
    32,
    51,
    50,
    45,
    98,
    121,
    116,
    101,
    32,
    107,
]);
// "expand 32-byte k"
function crypto_stream_salsa20_xor(c, cpos, m, mpos, b, n, k) {
    var z = new Uint8Array(16), x = new Uint8Array(64);
    var u, i;
    for (i = 0; i < 16; i++)
        z[i] = 0;
    for (i = 0; i < 8; i++)
        z[i] = n[i];
    while (b >= 64) {
        crypto_core_salsa20(x, z, k, sigma);
        for (i = 0; i < 64; i++)
            c[cpos + i] = m[mpos + i] ^ x[i];
        u = 1;
        for (i = 8; i < 16; i++) {
            u = (u + (z[i] & 0xff)) | 0;
            z[i] = u & 0xff;
            u >>>= 8;
        }
        b -= 64;
        cpos += 64;
        mpos += 64;
    }
    if (b > 0) {
        crypto_core_salsa20(x, z, k, sigma);
        for (i = 0; i < b; i++)
            c[cpos + i] = m[mpos + i] ^ x[i];
    }
    return 0;
}
function crypto_stream_salsa20(c, cpos, b, n, k) {
    var z = new Uint8Array(16), x = new Uint8Array(64);
    var u, i;
    for (i = 0; i < 16; i++)
        z[i] = 0;
    for (i = 0; i < 8; i++)
        z[i] = n[i];
    while (b >= 64) {
        crypto_core_salsa20(x, z, k, sigma);
        for (i = 0; i < 64; i++)
            c[cpos + i] = x[i];
        u = 1;
        for (i = 8; i < 16; i++) {
            u = (u + (z[i] & 0xff)) | 0;
            z[i] = u & 0xff;
            u >>>= 8;
        }
        b -= 64;
        cpos += 64;
    }
    if (b > 0) {
        crypto_core_salsa20(x, z, k, sigma);
        for (i = 0; i < b; i++)
            c[cpos + i] = x[i];
    }
    return 0;
}
function crypto_stream(c, cpos, d, n, k) {
    var s = new Uint8Array(32);
    crypto_core_hsalsa20(s, n, k, sigma);
    var sn = new Uint8Array(8);
    for (var i = 0; i < 8; i++)
        sn[i] = n[i + 16];
    return crypto_stream_salsa20(c, cpos, d, sn, s);
}
function crypto_stream_xor(c, cpos, m, mpos, d, n, k) {
    var s = new Uint8Array(32);
    crypto_core_hsalsa20(s, n, k, sigma);
    var sn = new Uint8Array(8);
    for (var i = 0; i < 8; i++)
        sn[i] = n[i + 16];
    return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, sn, s);
}
/*
 * Port of Andrew Moon's Poly1305-donna-16. Public domain.
 * https://github.com/floodyberry/poly1305-donna
 */
class poly1305 {
    constructor(key) {
        this.buffer = new Uint8Array(16);
        this.r = new Uint16Array(10);
        this.h = new Uint16Array(10);
        this.pad = new Uint16Array(8);
        this.leftover = 0;
        this.fin = 0;
        var t0, t1, t2, t3, t4, t5, t6, t7;
        t0 = (key[0] & 0xff) | ((key[1] & 0xff) << 8);
        this.r[0] = t0 & 0x1fff;
        t1 = (key[2] & 0xff) | ((key[3] & 0xff) << 8);
        this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
        t2 = (key[4] & 0xff) | ((key[5] & 0xff) << 8);
        this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
        t3 = (key[6] & 0xff) | ((key[7] & 0xff) << 8);
        this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
        t4 = (key[8] & 0xff) | ((key[9] & 0xff) << 8);
        this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
        this.r[5] = (t4 >>> 1) & 0x1ffe;
        t5 = (key[10] & 0xff) | ((key[11] & 0xff) << 8);
        this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
        t6 = (key[12] & 0xff) | ((key[13] & 0xff) << 8);
        this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
        t7 = (key[14] & 0xff) | ((key[15] & 0xff) << 8);
        this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
        this.r[9] = (t7 >>> 5) & 0x007f;
        this.pad[0] = (key[16] & 0xff) | ((key[17] & 0xff) << 8);
        this.pad[1] = (key[18] & 0xff) | ((key[19] & 0xff) << 8);
        this.pad[2] = (key[20] & 0xff) | ((key[21] & 0xff) << 8);
        this.pad[3] = (key[22] & 0xff) | ((key[23] & 0xff) << 8);
        this.pad[4] = (key[24] & 0xff) | ((key[25] & 0xff) << 8);
        this.pad[5] = (key[26] & 0xff) | ((key[27] & 0xff) << 8);
        this.pad[6] = (key[28] & 0xff) | ((key[29] & 0xff) << 8);
        this.pad[7] = (key[30] & 0xff) | ((key[31] & 0xff) << 8);
    }
    blocks(m, mpos, bytes) {
        var hibit = this.fin ? 0 : 1 << 11;
        var t0, t1, t2, t3, t4, t5, t6, t7, c;
        var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
        var h0 = this.h[0], h1 = this.h[1], h2 = this.h[2], h3 = this.h[3], h4 = this.h[4], h5 = this.h[5], h6 = this.h[6], h7 = this.h[7], h8 = this.h[8], h9 = this.h[9];
        var r0 = this.r[0], r1 = this.r[1], r2 = this.r[2], r3 = this.r[3], r4 = this.r[4], r5 = this.r[5], r6 = this.r[6], r7 = this.r[7], r8 = this.r[8], r9 = this.r[9];
        while (bytes >= 16) {
            t0 = (m[mpos + 0] & 0xff) | ((m[mpos + 1] & 0xff) << 8);
            h0 += t0 & 0x1fff;
            t1 = (m[mpos + 2] & 0xff) | ((m[mpos + 3] & 0xff) << 8);
            h1 += ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
            t2 = (m[mpos + 4] & 0xff) | ((m[mpos + 5] & 0xff) << 8);
            h2 += ((t1 >>> 10) | (t2 << 6)) & 0x1fff;
            t3 = (m[mpos + 6] & 0xff) | ((m[mpos + 7] & 0xff) << 8);
            h3 += ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
            t4 = (m[mpos + 8] & 0xff) | ((m[mpos + 9] & 0xff) << 8);
            h4 += ((t3 >>> 4) | (t4 << 12)) & 0x1fff;
            h5 += (t4 >>> 1) & 0x1fff;
            t5 = (m[mpos + 10] & 0xff) | ((m[mpos + 11] & 0xff) << 8);
            h6 += ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
            t6 = (m[mpos + 12] & 0xff) | ((m[mpos + 13] & 0xff) << 8);
            h7 += ((t5 >>> 11) | (t6 << 5)) & 0x1fff;
            t7 = (m[mpos + 14] & 0xff) | ((m[mpos + 15] & 0xff) << 8);
            h8 += ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
            h9 += (t7 >>> 5) | hibit;
            c = 0;
            d0 = c;
            d0 += h0 * r0;
            d0 += h1 * (5 * r9);
            d0 += h2 * (5 * r8);
            d0 += h3 * (5 * r7);
            d0 += h4 * (5 * r6);
            c = d0 >>> 13;
            d0 &= 0x1fff;
            d0 += h5 * (5 * r5);
            d0 += h6 * (5 * r4);
            d0 += h7 * (5 * r3);
            d0 += h8 * (5 * r2);
            d0 += h9 * (5 * r1);
            c += d0 >>> 13;
            d0 &= 0x1fff;
            d1 = c;
            d1 += h0 * r1;
            d1 += h1 * r0;
            d1 += h2 * (5 * r9);
            d1 += h3 * (5 * r8);
            d1 += h4 * (5 * r7);
            c = d1 >>> 13;
            d1 &= 0x1fff;
            d1 += h5 * (5 * r6);
            d1 += h6 * (5 * r5);
            d1 += h7 * (5 * r4);
            d1 += h8 * (5 * r3);
            d1 += h9 * (5 * r2);
            c += d1 >>> 13;
            d1 &= 0x1fff;
            d2 = c;
            d2 += h0 * r2;
            d2 += h1 * r1;
            d2 += h2 * r0;
            d2 += h3 * (5 * r9);
            d2 += h4 * (5 * r8);
            c = d2 >>> 13;
            d2 &= 0x1fff;
            d2 += h5 * (5 * r7);
            d2 += h6 * (5 * r6);
            d2 += h7 * (5 * r5);
            d2 += h8 * (5 * r4);
            d2 += h9 * (5 * r3);
            c += d2 >>> 13;
            d2 &= 0x1fff;
            d3 = c;
            d3 += h0 * r3;
            d3 += h1 * r2;
            d3 += h2 * r1;
            d3 += h3 * r0;
            d3 += h4 * (5 * r9);
            c = d3 >>> 13;
            d3 &= 0x1fff;
            d3 += h5 * (5 * r8);
            d3 += h6 * (5 * r7);
            d3 += h7 * (5 * r6);
            d3 += h8 * (5 * r5);
            d3 += h9 * (5 * r4);
            c += d3 >>> 13;
            d3 &= 0x1fff;
            d4 = c;
            d4 += h0 * r4;
            d4 += h1 * r3;
            d4 += h2 * r2;
            d4 += h3 * r1;
            d4 += h4 * r0;
            c = d4 >>> 13;
            d4 &= 0x1fff;
            d4 += h5 * (5 * r9);
            d4 += h6 * (5 * r8);
            d4 += h7 * (5 * r7);
            d4 += h8 * (5 * r6);
            d4 += h9 * (5 * r5);
            c += d4 >>> 13;
            d4 &= 0x1fff;
            d5 = c;
            d5 += h0 * r5;
            d5 += h1 * r4;
            d5 += h2 * r3;
            d5 += h3 * r2;
            d5 += h4 * r1;
            c = d5 >>> 13;
            d5 &= 0x1fff;
            d5 += h5 * r0;
            d5 += h6 * (5 * r9);
            d5 += h7 * (5 * r8);
            d5 += h8 * (5 * r7);
            d5 += h9 * (5 * r6);
            c += d5 >>> 13;
            d5 &= 0x1fff;
            d6 = c;
            d6 += h0 * r6;
            d6 += h1 * r5;
            d6 += h2 * r4;
            d6 += h3 * r3;
            d6 += h4 * r2;
            c = d6 >>> 13;
            d6 &= 0x1fff;
            d6 += h5 * r1;
            d6 += h6 * r0;
            d6 += h7 * (5 * r9);
            d6 += h8 * (5 * r8);
            d6 += h9 * (5 * r7);
            c += d6 >>> 13;
            d6 &= 0x1fff;
            d7 = c;
            d7 += h0 * r7;
            d7 += h1 * r6;
            d7 += h2 * r5;
            d7 += h3 * r4;
            d7 += h4 * r3;
            c = d7 >>> 13;
            d7 &= 0x1fff;
            d7 += h5 * r2;
            d7 += h6 * r1;
            d7 += h7 * r0;
            d7 += h8 * (5 * r9);
            d7 += h9 * (5 * r8);
            c += d7 >>> 13;
            d7 &= 0x1fff;
            d8 = c;
            d8 += h0 * r8;
            d8 += h1 * r7;
            d8 += h2 * r6;
            d8 += h3 * r5;
            d8 += h4 * r4;
            c = d8 >>> 13;
            d8 &= 0x1fff;
            d8 += h5 * r3;
            d8 += h6 * r2;
            d8 += h7 * r1;
            d8 += h8 * r0;
            d8 += h9 * (5 * r9);
            c += d8 >>> 13;
            d8 &= 0x1fff;
            d9 = c;
            d9 += h0 * r9;
            d9 += h1 * r8;
            d9 += h2 * r7;
            d9 += h3 * r6;
            d9 += h4 * r5;
            c = d9 >>> 13;
            d9 &= 0x1fff;
            d9 += h5 * r4;
            d9 += h6 * r3;
            d9 += h7 * r2;
            d9 += h8 * r1;
            d9 += h9 * r0;
            c += d9 >>> 13;
            d9 &= 0x1fff;
            c = ((c << 2) + c) | 0;
            c = (c + d0) | 0;
            d0 = c & 0x1fff;
            c = c >>> 13;
            d1 += c;
            h0 = d0;
            h1 = d1;
            h2 = d2;
            h3 = d3;
            h4 = d4;
            h5 = d5;
            h6 = d6;
            h7 = d7;
            h8 = d8;
            h9 = d9;
            mpos += 16;
            bytes -= 16;
        }
        this.h[0] = h0;
        this.h[1] = h1;
        this.h[2] = h2;
        this.h[3] = h3;
        this.h[4] = h4;
        this.h[5] = h5;
        this.h[6] = h6;
        this.h[7] = h7;
        this.h[8] = h8;
        this.h[9] = h9;
    }
    finish(mac, macpos) {
        var g = new Uint16Array(10);
        var c, mask, f, i;
        if (this.leftover) {
            i = this.leftover;
            this.buffer[i++] = 1;
            for (; i < 16; i++)
                this.buffer[i] = 0;
            this.fin = 1;
            this.blocks(this.buffer, 0, 16);
        }
        c = this.h[1] >>> 13;
        this.h[1] &= 0x1fff;
        for (i = 2; i < 10; i++) {
            this.h[i] += c;
            c = this.h[i] >>> 13;
            this.h[i] &= 0x1fff;
        }
        this.h[0] += c * 5;
        c = this.h[0] >>> 13;
        this.h[0] &= 0x1fff;
        this.h[1] += c;
        c = this.h[1] >>> 13;
        this.h[1] &= 0x1fff;
        this.h[2] += c;
        g[0] = this.h[0] + 5;
        c = g[0] >>> 13;
        g[0] &= 0x1fff;
        for (i = 1; i < 10; i++) {
            g[i] = this.h[i] + c;
            c = g[i] >>> 13;
            g[i] &= 0x1fff;
        }
        g[9] -= 1 << 13;
        mask = (c ^ 1) - 1;
        for (i = 0; i < 10; i++)
            g[i] &= mask;
        mask = ~mask;
        for (i = 0; i < 10; i++)
            this.h[i] = (this.h[i] & mask) | g[i];
        this.h[0] = (this.h[0] | (this.h[1] << 13)) & 0xffff;
        this.h[1] = ((this.h[1] >>> 3) | (this.h[2] << 10)) & 0xffff;
        this.h[2] = ((this.h[2] >>> 6) | (this.h[3] << 7)) & 0xffff;
        this.h[3] = ((this.h[3] >>> 9) | (this.h[4] << 4)) & 0xffff;
        this.h[4] =
            ((this.h[4] >>> 12) | (this.h[5] << 1) | (this.h[6] << 14)) & 0xffff;
        this.h[5] = ((this.h[6] >>> 2) | (this.h[7] << 11)) & 0xffff;
        this.h[6] = ((this.h[7] >>> 5) | (this.h[8] << 8)) & 0xffff;
        this.h[7] = ((this.h[8] >>> 8) | (this.h[9] << 5)) & 0xffff;
        f = this.h[0] + this.pad[0];
        this.h[0] = f & 0xffff;
        for (i = 1; i < 8; i++) {
            f = (((this.h[i] + this.pad[i]) | 0) + (f >>> 16)) | 0;
            this.h[i] = f & 0xffff;
        }
        mac[macpos + 0] = (this.h[0] >>> 0) & 0xff;
        mac[macpos + 1] = (this.h[0] >>> 8) & 0xff;
        mac[macpos + 2] = (this.h[1] >>> 0) & 0xff;
        mac[macpos + 3] = (this.h[1] >>> 8) & 0xff;
        mac[macpos + 4] = (this.h[2] >>> 0) & 0xff;
        mac[macpos + 5] = (this.h[2] >>> 8) & 0xff;
        mac[macpos + 6] = (this.h[3] >>> 0) & 0xff;
        mac[macpos + 7] = (this.h[3] >>> 8) & 0xff;
        mac[macpos + 8] = (this.h[4] >>> 0) & 0xff;
        mac[macpos + 9] = (this.h[4] >>> 8) & 0xff;
        mac[macpos + 10] = (this.h[5] >>> 0) & 0xff;
        mac[macpos + 11] = (this.h[5] >>> 8) & 0xff;
        mac[macpos + 12] = (this.h[6] >>> 0) & 0xff;
        mac[macpos + 13] = (this.h[6] >>> 8) & 0xff;
        mac[macpos + 14] = (this.h[7] >>> 0) & 0xff;
        mac[macpos + 15] = (this.h[7] >>> 8) & 0xff;
    }
    update(m, mpos, bytes) {
        var i, want;
        if (this.leftover) {
            want = 16 - this.leftover;
            if (want > bytes)
                want = bytes;
            for (i = 0; i < want; i++)
                this.buffer[this.leftover + i] = m[mpos + i];
            bytes -= want;
            mpos += want;
            this.leftover += want;
            if (this.leftover < 16)
                return;
            this.blocks(this.buffer, 0, 16);
            this.leftover = 0;
        }
        if (bytes >= 16) {
            want = bytes - (bytes % 16);
            this.blocks(m, mpos, want);
            mpos += want;
            bytes -= want;
        }
        if (bytes) {
            for (i = 0; i < bytes; i++)
                this.buffer[this.leftover + i] = m[mpos + i];
            this.leftover += bytes;
        }
    }
}
function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
    var s = new poly1305(k);
    s.update(m, mpos, n);
    s.finish(out, outpos);
    return 0;
}
function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
    var x = new Uint8Array(16);
    crypto_onetimeauth(x, 0, m, mpos, n, k);
    return crypto_verify_16(h, hpos, x, 0);
}
function crypto_secretbox(c, m, d, n, k) {
    var i;
    if (d < 32)
        return -1;
    crypto_stream_xor(c, 0, m, 0, d, n, k);
    crypto_onetimeauth(c, 16, c, 32, d - 32, c);
    for (i = 0; i < 16; i++)
        c[i] = 0;
    return 0;
}
function crypto_secretbox_open(m, c, d, n, k) {
    var i;
    var x = new Uint8Array(32);
    if (d < 32)
        return -1;
    crypto_stream(x, 0, 32, n, k);
    if (crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) !== 0)
        return -1;
    crypto_stream_xor(m, 0, c, 0, d, n, k);
    for (i = 0; i < 32; i++)
        m[i] = 0;
    return 0;
}
function set25519(r, a) {
    var i;
    for (i = 0; i < 16; i++)
        r[i] = a[i] | 0;
}
function car25519(o) {
    var i, v, c = 1;
    for (i = 0; i < 16; i++) {
        v = o[i] + c + 65535;
        c = Math.floor(v / 65536);
        o[i] = v - c * 65536;
    }
    o[0] += c - 1 + 37 * (c - 1);
}
function sel25519(p, q, b) {
    var t, c = ~(b - 1);
    for (var i = 0; i < 16; i++) {
        t = c & (p[i] ^ q[i]);
        p[i] ^= t;
        q[i] ^= t;
    }
}
function pack25519(o, n) {
    var i, j, b;
    var m = gf(), t = gf();
    for (i = 0; i < 16; i++)
        t[i] = n[i];
    car25519(t);
    car25519(t);
    car25519(t);
    for (j = 0; j < 2; j++) {
        m[0] = t[0] - 0xffed;
        for (i = 1; i < 15; i++) {
            m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
            m[i - 1] &= 0xffff;
        }
        m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
        b = (m[15] >> 16) & 1;
        m[14] &= 0xffff;
        sel25519(t, m, 1 - b);
    }
    for (i = 0; i < 16; i++) {
        o[2 * i] = t[i] & 0xff;
        o[2 * i + 1] = t[i] >> 8;
    }
}
function neq25519(a, b) {
    var c = new Uint8Array(32), d = new Uint8Array(32);
    pack25519(c, a);
    pack25519(d, b);
    return crypto_verify_32(c, 0, d, 0);
}
function par25519(a) {
    var d = new Uint8Array(32);
    pack25519(d, a);
    return d[0] & 1;
}
function unpack25519(o, n) {
    var i;
    for (i = 0; i < 16; i++)
        o[i] = n[2 * i] + (n[2 * i + 1] << 8);
    o[15] &= 0x7fff;
}
function A(o, a, b) {
    for (var i = 0; i < 16; i++)
        o[i] = a[i] + b[i];
}
function Z(o, a, b) {
    for (var i = 0; i < 16; i++)
        o[i] = a[i] - b[i];
}
function M(o, a, b) {
    var v, c, t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0, t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0, t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0, b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], b8 = b[8], b9 = b[9], b10 = b[10], b11 = b[11], b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15];
    v = a[0];
    t0 += v * b0;
    t1 += v * b1;
    t2 += v * b2;
    t3 += v * b3;
    t4 += v * b4;
    t5 += v * b5;
    t6 += v * b6;
    t7 += v * b7;
    t8 += v * b8;
    t9 += v * b9;
    t10 += v * b10;
    t11 += v * b11;
    t12 += v * b12;
    t13 += v * b13;
    t14 += v * b14;
    t15 += v * b15;
    v = a[1];
    t1 += v * b0;
    t2 += v * b1;
    t3 += v * b2;
    t4 += v * b3;
    t5 += v * b4;
    t6 += v * b5;
    t7 += v * b6;
    t8 += v * b7;
    t9 += v * b8;
    t10 += v * b9;
    t11 += v * b10;
    t12 += v * b11;
    t13 += v * b12;
    t14 += v * b13;
    t15 += v * b14;
    t16 += v * b15;
    v = a[2];
    t2 += v * b0;
    t3 += v * b1;
    t4 += v * b2;
    t5 += v * b3;
    t6 += v * b4;
    t7 += v * b5;
    t8 += v * b6;
    t9 += v * b7;
    t10 += v * b8;
    t11 += v * b9;
    t12 += v * b10;
    t13 += v * b11;
    t14 += v * b12;
    t15 += v * b13;
    t16 += v * b14;
    t17 += v * b15;
    v = a[3];
    t3 += v * b0;
    t4 += v * b1;
    t5 += v * b2;
    t6 += v * b3;
    t7 += v * b4;
    t8 += v * b5;
    t9 += v * b6;
    t10 += v * b7;
    t11 += v * b8;
    t12 += v * b9;
    t13 += v * b10;
    t14 += v * b11;
    t15 += v * b12;
    t16 += v * b13;
    t17 += v * b14;
    t18 += v * b15;
    v = a[4];
    t4 += v * b0;
    t5 += v * b1;
    t6 += v * b2;
    t7 += v * b3;
    t8 += v * b4;
    t9 += v * b5;
    t10 += v * b6;
    t11 += v * b7;
    t12 += v * b8;
    t13 += v * b9;
    t14 += v * b10;
    t15 += v * b11;
    t16 += v * b12;
    t17 += v * b13;
    t18 += v * b14;
    t19 += v * b15;
    v = a[5];
    t5 += v * b0;
    t6 += v * b1;
    t7 += v * b2;
    t8 += v * b3;
    t9 += v * b4;
    t10 += v * b5;
    t11 += v * b6;
    t12 += v * b7;
    t13 += v * b8;
    t14 += v * b9;
    t15 += v * b10;
    t16 += v * b11;
    t17 += v * b12;
    t18 += v * b13;
    t19 += v * b14;
    t20 += v * b15;
    v = a[6];
    t6 += v * b0;
    t7 += v * b1;
    t8 += v * b2;
    t9 += v * b3;
    t10 += v * b4;
    t11 += v * b5;
    t12 += v * b6;
    t13 += v * b7;
    t14 += v * b8;
    t15 += v * b9;
    t16 += v * b10;
    t17 += v * b11;
    t18 += v * b12;
    t19 += v * b13;
    t20 += v * b14;
    t21 += v * b15;
    v = a[7];
    t7 += v * b0;
    t8 += v * b1;
    t9 += v * b2;
    t10 += v * b3;
    t11 += v * b4;
    t12 += v * b5;
    t13 += v * b6;
    t14 += v * b7;
    t15 += v * b8;
    t16 += v * b9;
    t17 += v * b10;
    t18 += v * b11;
    t19 += v * b12;
    t20 += v * b13;
    t21 += v * b14;
    t22 += v * b15;
    v = a[8];
    t8 += v * b0;
    t9 += v * b1;
    t10 += v * b2;
    t11 += v * b3;
    t12 += v * b4;
    t13 += v * b5;
    t14 += v * b6;
    t15 += v * b7;
    t16 += v * b8;
    t17 += v * b9;
    t18 += v * b10;
    t19 += v * b11;
    t20 += v * b12;
    t21 += v * b13;
    t22 += v * b14;
    t23 += v * b15;
    v = a[9];
    t9 += v * b0;
    t10 += v * b1;
    t11 += v * b2;
    t12 += v * b3;
    t13 += v * b4;
    t14 += v * b5;
    t15 += v * b6;
    t16 += v * b7;
    t17 += v * b8;
    t18 += v * b9;
    t19 += v * b10;
    t20 += v * b11;
    t21 += v * b12;
    t22 += v * b13;
    t23 += v * b14;
    t24 += v * b15;
    v = a[10];
    t10 += v * b0;
    t11 += v * b1;
    t12 += v * b2;
    t13 += v * b3;
    t14 += v * b4;
    t15 += v * b5;
    t16 += v * b6;
    t17 += v * b7;
    t18 += v * b8;
    t19 += v * b9;
    t20 += v * b10;
    t21 += v * b11;
    t22 += v * b12;
    t23 += v * b13;
    t24 += v * b14;
    t25 += v * b15;
    v = a[11];
    t11 += v * b0;
    t12 += v * b1;
    t13 += v * b2;
    t14 += v * b3;
    t15 += v * b4;
    t16 += v * b5;
    t17 += v * b6;
    t18 += v * b7;
    t19 += v * b8;
    t20 += v * b9;
    t21 += v * b10;
    t22 += v * b11;
    t23 += v * b12;
    t24 += v * b13;
    t25 += v * b14;
    t26 += v * b15;
    v = a[12];
    t12 += v * b0;
    t13 += v * b1;
    t14 += v * b2;
    t15 += v * b3;
    t16 += v * b4;
    t17 += v * b5;
    t18 += v * b6;
    t19 += v * b7;
    t20 += v * b8;
    t21 += v * b9;
    t22 += v * b10;
    t23 += v * b11;
    t24 += v * b12;
    t25 += v * b13;
    t26 += v * b14;
    t27 += v * b15;
    v = a[13];
    t13 += v * b0;
    t14 += v * b1;
    t15 += v * b2;
    t16 += v * b3;
    t17 += v * b4;
    t18 += v * b5;
    t19 += v * b6;
    t20 += v * b7;
    t21 += v * b8;
    t22 += v * b9;
    t23 += v * b10;
    t24 += v * b11;
    t25 += v * b12;
    t26 += v * b13;
    t27 += v * b14;
    t28 += v * b15;
    v = a[14];
    t14 += v * b0;
    t15 += v * b1;
    t16 += v * b2;
    t17 += v * b3;
    t18 += v * b4;
    t19 += v * b5;
    t20 += v * b6;
    t21 += v * b7;
    t22 += v * b8;
    t23 += v * b9;
    t24 += v * b10;
    t25 += v * b11;
    t26 += v * b12;
    t27 += v * b13;
    t28 += v * b14;
    t29 += v * b15;
    v = a[15];
    t15 += v * b0;
    t16 += v * b1;
    t17 += v * b2;
    t18 += v * b3;
    t19 += v * b4;
    t20 += v * b5;
    t21 += v * b6;
    t22 += v * b7;
    t23 += v * b8;
    t24 += v * b9;
    t25 += v * b10;
    t26 += v * b11;
    t27 += v * b12;
    t28 += v * b13;
    t29 += v * b14;
    t30 += v * b15;
    t0 += 38 * t16;
    t1 += 38 * t17;
    t2 += 38 * t18;
    t3 += 38 * t19;
    t4 += 38 * t20;
    t5 += 38 * t21;
    t6 += 38 * t22;
    t7 += 38 * t23;
    t8 += 38 * t24;
    t9 += 38 * t25;
    t10 += 38 * t26;
    t11 += 38 * t27;
    t12 += 38 * t28;
    t13 += 38 * t29;
    t14 += 38 * t30;
    // t15 left as is
    // first car
    c = 1;
    v = t0 + c + 65535;
    c = Math.floor(v / 65536);
    t0 = v - c * 65536;
    v = t1 + c + 65535;
    c = Math.floor(v / 65536);
    t1 = v - c * 65536;
    v = t2 + c + 65535;
    c = Math.floor(v / 65536);
    t2 = v - c * 65536;
    v = t3 + c + 65535;
    c = Math.floor(v / 65536);
    t3 = v - c * 65536;
    v = t4 + c + 65535;
    c = Math.floor(v / 65536);
    t4 = v - c * 65536;
    v = t5 + c + 65535;
    c = Math.floor(v / 65536);
    t5 = v - c * 65536;
    v = t6 + c + 65535;
    c = Math.floor(v / 65536);
    t6 = v - c * 65536;
    v = t7 + c + 65535;
    c = Math.floor(v / 65536);
    t7 = v - c * 65536;
    v = t8 + c + 65535;
    c = Math.floor(v / 65536);
    t8 = v - c * 65536;
    v = t9 + c + 65535;
    c = Math.floor(v / 65536);
    t9 = v - c * 65536;
    v = t10 + c + 65535;
    c = Math.floor(v / 65536);
    t10 = v - c * 65536;
    v = t11 + c + 65535;
    c = Math.floor(v / 65536);
    t11 = v - c * 65536;
    v = t12 + c + 65535;
    c = Math.floor(v / 65536);
    t12 = v - c * 65536;
    v = t13 + c + 65535;
    c = Math.floor(v / 65536);
    t13 = v - c * 65536;
    v = t14 + c + 65535;
    c = Math.floor(v / 65536);
    t14 = v - c * 65536;
    v = t15 + c + 65535;
    c = Math.floor(v / 65536);
    t15 = v - c * 65536;
    t0 += c - 1 + 37 * (c - 1);
    // second car
    c = 1;
    v = t0 + c + 65535;
    c = Math.floor(v / 65536);
    t0 = v - c * 65536;
    v = t1 + c + 65535;
    c = Math.floor(v / 65536);
    t1 = v - c * 65536;
    v = t2 + c + 65535;
    c = Math.floor(v / 65536);
    t2 = v - c * 65536;
    v = t3 + c + 65535;
    c = Math.floor(v / 65536);
    t3 = v - c * 65536;
    v = t4 + c + 65535;
    c = Math.floor(v / 65536);
    t4 = v - c * 65536;
    v = t5 + c + 65535;
    c = Math.floor(v / 65536);
    t5 = v - c * 65536;
    v = t6 + c + 65535;
    c = Math.floor(v / 65536);
    t6 = v - c * 65536;
    v = t7 + c + 65535;
    c = Math.floor(v / 65536);
    t7 = v - c * 65536;
    v = t8 + c + 65535;
    c = Math.floor(v / 65536);
    t8 = v - c * 65536;
    v = t9 + c + 65535;
    c = Math.floor(v / 65536);
    t9 = v - c * 65536;
    v = t10 + c + 65535;
    c = Math.floor(v / 65536);
    t10 = v - c * 65536;
    v = t11 + c + 65535;
    c = Math.floor(v / 65536);
    t11 = v - c * 65536;
    v = t12 + c + 65535;
    c = Math.floor(v / 65536);
    t12 = v - c * 65536;
    v = t13 + c + 65535;
    c = Math.floor(v / 65536);
    t13 = v - c * 65536;
    v = t14 + c + 65535;
    c = Math.floor(v / 65536);
    t14 = v - c * 65536;
    v = t15 + c + 65535;
    c = Math.floor(v / 65536);
    t15 = v - c * 65536;
    t0 += c - 1 + 37 * (c - 1);
    o[0] = t0;
    o[1] = t1;
    o[2] = t2;
    o[3] = t3;
    o[4] = t4;
    o[5] = t5;
    o[6] = t6;
    o[7] = t7;
    o[8] = t8;
    o[9] = t9;
    o[10] = t10;
    o[11] = t11;
    o[12] = t12;
    o[13] = t13;
    o[14] = t14;
    o[15] = t15;
}
function S(o, a) {
    M(o, a, a);
}
function inv25519(o, i) {
    var c = gf();
    var a;
    for (a = 0; a < 16; a++)
        c[a] = i[a];
    for (a = 253; a >= 0; a--) {
        S(c, c);
        if (a !== 2 && a !== 4)
            M(c, c, i);
    }
    for (a = 0; a < 16; a++)
        o[a] = c[a];
}
function pow2523(o, i) {
    var c = gf();
    var a;
    for (a = 0; a < 16; a++)
        c[a] = i[a];
    for (a = 250; a >= 0; a--) {
        S(c, c);
        if (a !== 1)
            M(c, c, i);
    }
    for (a = 0; a < 16; a++)
        o[a] = c[a];
}
function crypto_scalarmult(q, n, p) {
    var z = new Uint8Array(32);
    var x = new Float64Array(80), r, i;
    var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf();
    for (i = 0; i < 31; i++)
        z[i] = n[i];
    z[31] = (n[31] & 127) | 64;
    z[0] &= 248;
    unpack25519(x, p);
    for (i = 0; i < 16; i++) {
        b[i] = x[i];
        d[i] = a[i] = c[i] = 0;
    }
    a[0] = d[0] = 1;
    for (i = 254; i >= 0; --i) {
        r = (z[i >>> 3] >>> (i & 7)) & 1;
        sel25519(a, b, r);
        sel25519(c, d, r);
        A(e, a, c);
        Z(a, a, c);
        A(c, b, d);
        Z(b, b, d);
        S(d, e);
        S(f, a);
        M(a, c, a);
        M(c, b, e);
        A(e, a, c);
        Z(a, a, c);
        S(b, a);
        Z(c, d, f);
        M(a, c, _121665);
        A(a, a, d);
        M(c, c, a);
        M(a, d, f);
        M(d, b, x);
        S(b, e);
        sel25519(a, b, r);
        sel25519(c, d, r);
    }
    for (i = 0; i < 16; i++) {
        x[i + 16] = a[i];
        x[i + 32] = c[i];
        x[i + 48] = b[i];
        x[i + 64] = d[i];
    }
    var x32 = x.subarray(32);
    var x16 = x.subarray(16);
    inv25519(x32, x32);
    M(x16, x16, x32);
    pack25519(q, x16);
    return 0;
}
function crypto_scalarmult_base(q, n) {
    return crypto_scalarmult(q, n, _9);
}
function crypto_box_keypair(y, x) {
    randombytes(x, 32);
    return crypto_scalarmult_base(y, x);
}
function crypto_box_beforenm(k, y, x) {
    var s = new Uint8Array(32);
    crypto_scalarmult(s, x, y);
    return crypto_core_hsalsa20(k, _0, s, sigma);
}
// prettier-ignore
var K = [
    0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
    0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
    0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
    0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
    0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
    0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
    0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
    0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
    0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
    0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
    0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
    0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
    0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
    0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
    0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
    0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
    0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
    0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
    0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
    0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
    0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
    0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
    0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
    0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
    0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
    0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
    0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
    0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
    0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
    0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
    0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
    0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
    0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
    0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
    0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
    0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
    0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
    0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
    0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
    0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
];
function crypto_hashblocks_hl(hh, hl, m, n) {
    var wh = new Int32Array(16), wl = new Int32Array(16), bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7, bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7, th, tl, i, j, h, l, a, b, c, d;
    var ah0 = hh[0], ah1 = hh[1], ah2 = hh[2], ah3 = hh[3], ah4 = hh[4], ah5 = hh[5], ah6 = hh[6], ah7 = hh[7], al0 = hl[0], al1 = hl[1], al2 = hl[2], al3 = hl[3], al4 = hl[4], al5 = hl[5], al6 = hl[6], al7 = hl[7];
    var pos = 0;
    while (n >= 128) {
        for (i = 0; i < 16; i++) {
            j = 8 * i + pos;
            wh[i] = (m[j + 0] << 24) | (m[j + 1] << 16) | (m[j + 2] << 8) | m[j + 3];
            wl[i] = (m[j + 4] << 24) | (m[j + 5] << 16) | (m[j + 6] << 8) | m[j + 7];
        }
        for (i = 0; i < 80; i++) {
            bh0 = ah0;
            bh1 = ah1;
            bh2 = ah2;
            bh3 = ah3;
            bh4 = ah4;
            bh5 = ah5;
            bh6 = ah6;
            bh7 = ah7;
            bl0 = al0;
            bl1 = al1;
            bl2 = al2;
            bl3 = al3;
            bl4 = al4;
            bl5 = al5;
            bl6 = al6;
            bl7 = al7;
            // add
            h = ah7;
            l = al7;
            a = l & 0xffff;
            b = l >>> 16;
            c = h & 0xffff;
            d = h >>> 16;
            // Sigma1
            h =
                ((ah4 >>> 14) | (al4 << (32 - 14))) ^
                    ((ah4 >>> 18) | (al4 << (32 - 18))) ^
                    ((al4 >>> (41 - 32)) | (ah4 << (32 - (41 - 32))));
            l =
                ((al4 >>> 14) | (ah4 << (32 - 14))) ^
                    ((al4 >>> 18) | (ah4 << (32 - 18))) ^
                    ((ah4 >>> (41 - 32)) | (al4 << (32 - (41 - 32))));
            a += l & 0xffff;
            b += l >>> 16;
            c += h & 0xffff;
            d += h >>> 16;
            // Ch
            h = (ah4 & ah5) ^ (~ah4 & ah6);
            l = (al4 & al5) ^ (~al4 & al6);
            a += l & 0xffff;
            b += l >>> 16;
            c += h & 0xffff;
            d += h >>> 16;
            // K
            h = K[i * 2];
            l = K[i * 2 + 1];
            a += l & 0xffff;
            b += l >>> 16;
            c += h & 0xffff;
            d += h >>> 16;
            // w
            h = wh[i % 16];
            l = wl[i % 16];
            a += l & 0xffff;
            b += l >>> 16;
            c += h & 0xffff;
            d += h >>> 16;
            b += a >>> 16;
            c += b >>> 16;
            d += c >>> 16;
            th = (c & 0xffff) | (d << 16);
            tl = (a & 0xffff) | (b << 16);
            // add
            h = th;
            l = tl;
            a = l & 0xffff;
            b = l >>> 16;
            c = h & 0xffff;
            d = h >>> 16;
            // Sigma0
            h =
                ((ah0 >>> 28) | (al0 << (32 - 28))) ^
                    ((al0 >>> (34 - 32)) | (ah0 << (32 - (34 - 32)))) ^
                    ((al0 >>> (39 - 32)) | (ah0 << (32 - (39 - 32))));
            l =
                ((al0 >>> 28) | (ah0 << (32 - 28))) ^
                    ((ah0 >>> (34 - 32)) | (al0 << (32 - (34 - 32)))) ^
                    ((ah0 >>> (39 - 32)) | (al0 << (32 - (39 - 32))));
            a += l & 0xffff;
            b += l >>> 16;
            c += h & 0xffff;
            d += h >>> 16;
            // Maj
            h = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2);
            l = (al0 & al1) ^ (al0 & al2) ^ (al1 & al2);
            a += l & 0xffff;
            b += l >>> 16;
            c += h & 0xffff;
            d += h >>> 16;
            b += a >>> 16;
            c += b >>> 16;
            d += c >>> 16;
            bh7 = (c & 0xffff) | (d << 16);
            bl7 = (a & 0xffff) | (b << 16);
            // add
            h = bh3;
            l = bl3;
            a = l & 0xffff;
            b = l >>> 16;
            c = h & 0xffff;
            d = h >>> 16;
            h = th;
            l = tl;
            a += l & 0xffff;
            b += l >>> 16;
            c += h & 0xffff;
            d += h >>> 16;
            b += a >>> 16;
            c += b >>> 16;
            d += c >>> 16;
            bh3 = (c & 0xffff) | (d << 16);
            bl3 = (a & 0xffff) | (b << 16);
            ah1 = bh0;
            ah2 = bh1;
            ah3 = bh2;
            ah4 = bh3;
            ah5 = bh4;
            ah6 = bh5;
            ah7 = bh6;
            ah0 = bh7;
            al1 = bl0;
            al2 = bl1;
            al3 = bl2;
            al4 = bl3;
            al5 = bl4;
            al6 = bl5;
            al7 = bl6;
            al0 = bl7;
            if (i % 16 === 15) {
                for (j = 0; j < 16; j++) {
                    // add
                    h = wh[j];
                    l = wl[j];
                    a = l & 0xffff;
                    b = l >>> 16;
                    c = h & 0xffff;
                    d = h >>> 16;
                    h = wh[(j + 9) % 16];
                    l = wl[(j + 9) % 16];
                    a += l & 0xffff;
                    b += l >>> 16;
                    c += h & 0xffff;
                    d += h >>> 16;
                    // sigma0
                    th = wh[(j + 1) % 16];
                    tl = wl[(j + 1) % 16];
                    h =
                        ((th >>> 1) | (tl << (32 - 1))) ^
                            ((th >>> 8) | (tl << (32 - 8))) ^
                            (th >>> 7);
                    l =
                        ((tl >>> 1) | (th << (32 - 1))) ^
                            ((tl >>> 8) | (th << (32 - 8))) ^
                            ((tl >>> 7) | (th << (32 - 7)));
                    a += l & 0xffff;
                    b += l >>> 16;
                    c += h & 0xffff;
                    d += h >>> 16;
                    // sigma1
                    th = wh[(j + 14) % 16];
                    tl = wl[(j + 14) % 16];
                    h =
                        ((th >>> 19) | (tl << (32 - 19))) ^
                            ((tl >>> (61 - 32)) | (th << (32 - (61 - 32)))) ^
                            (th >>> 6);
                    l =
                        ((tl >>> 19) | (th << (32 - 19))) ^
                            ((th >>> (61 - 32)) | (tl << (32 - (61 - 32)))) ^
                            ((tl >>> 6) | (th << (32 - 6)));
                    a += l & 0xffff;
                    b += l >>> 16;
                    c += h & 0xffff;
                    d += h >>> 16;
                    b += a >>> 16;
                    c += b >>> 16;
                    d += c >>> 16;
                    wh[j] = (c & 0xffff) | (d << 16);
                    wl[j] = (a & 0xffff) | (b << 16);
                }
            }
        }
        // add
        h = ah0;
        l = al0;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[0];
        l = hl[0];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[0] = ah0 = (c & 0xffff) | (d << 16);
        hl[0] = al0 = (a & 0xffff) | (b << 16);
        h = ah1;
        l = al1;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[1];
        l = hl[1];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[1] = ah1 = (c & 0xffff) | (d << 16);
        hl[1] = al1 = (a & 0xffff) | (b << 16);
        h = ah2;
        l = al2;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[2];
        l = hl[2];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[2] = ah2 = (c & 0xffff) | (d << 16);
        hl[2] = al2 = (a & 0xffff) | (b << 16);
        h = ah3;
        l = al3;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[3];
        l = hl[3];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[3] = ah3 = (c & 0xffff) | (d << 16);
        hl[3] = al3 = (a & 0xffff) | (b << 16);
        h = ah4;
        l = al4;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[4];
        l = hl[4];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[4] = ah4 = (c & 0xffff) | (d << 16);
        hl[4] = al4 = (a & 0xffff) | (b << 16);
        h = ah5;
        l = al5;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[5];
        l = hl[5];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[5] = ah5 = (c & 0xffff) | (d << 16);
        hl[5] = al5 = (a & 0xffff) | (b << 16);
        h = ah6;
        l = al6;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[6];
        l = hl[6];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[6] = ah6 = (c & 0xffff) | (d << 16);
        hl[6] = al6 = (a & 0xffff) | (b << 16);
        h = ah7;
        l = al7;
        a = l & 0xffff;
        b = l >>> 16;
        c = h & 0xffff;
        d = h >>> 16;
        h = hh[7];
        l = hl[7];
        a += l & 0xffff;
        b += l >>> 16;
        c += h & 0xffff;
        d += h >>> 16;
        b += a >>> 16;
        c += b >>> 16;
        d += c >>> 16;
        hh[7] = ah7 = (c & 0xffff) | (d << 16);
        hl[7] = al7 = (a & 0xffff) | (b << 16);
        pos += 128;
        n -= 128;
    }
    return n;
}
function crypto_hash(out, m, n) {
    const hh = new Int32Array(8);
    const hl = new Int32Array(8);
    const x = new Uint8Array(256);
    let b = n;
    hh[0] = 0x6a09e667;
    hh[1] = 0xbb67ae85;
    hh[2] = 0x3c6ef372;
    hh[3] = 0xa54ff53a;
    hh[4] = 0x510e527f;
    hh[5] = 0x9b05688c;
    hh[6] = 0x1f83d9ab;
    hh[7] = 0x5be0cd19;
    hl[0] = 0xf3bcc908;
    hl[1] = 0x84caa73b;
    hl[2] = 0xfe94f82b;
    hl[3] = 0x5f1d36f1;
    hl[4] = 0xade682d1;
    hl[5] = 0x2b3e6c1f;
    hl[6] = 0xfb41bd6b;
    hl[7] = 0x137e2179;
    crypto_hashblocks_hl(hh, hl, m, n);
    n %= 128;
    for (let i = 0; i < n; i++)
        x[i] = m[b - n + i];
    x[n] = 128;
    n = 256 - 128 * (n < 112 ? 1 : 0);
    x[n - 9] = 0;
    ts64(x, n - 8, (b / 0x20000000) | 0, b << 3);
    crypto_hashblocks_hl(hh, hl, x, n);
    for (let i = 0; i < 8; i++)
        ts64(out, 8 * i, hh[i], hl[i]);
    return 0;
}
/**
 * Incremental version of crypto_hash.
 */
class HashState {
    constructor() {
        this.hh = new Int32Array(8);
        this.hl = new Int32Array(8);
        this.next = new Uint8Array(128);
        this.p = 0;
        this.total = 0;
        this.hh[0] = 0x6a09e667;
        this.hh[1] = 0xbb67ae85;
        this.hh[2] = 0x3c6ef372;
        this.hh[3] = 0xa54ff53a;
        this.hh[4] = 0x510e527f;
        this.hh[5] = 0x9b05688c;
        this.hh[6] = 0x1f83d9ab;
        this.hh[7] = 0x5be0cd19;
        this.hl[0] = 0xf3bcc908;
        this.hl[1] = 0x84caa73b;
        this.hl[2] = 0xfe94f82b;
        this.hl[3] = 0x5f1d36f1;
        this.hl[4] = 0xade682d1;
        this.hl[5] = 0x2b3e6c1f;
        this.hl[6] = 0xfb41bd6b;
        this.hl[7] = 0x137e2179;
    }
    update(data) {
        this.total += data.length;
        let i = 0;
        while (i < data.length) {
            const r = 128 - this.p;
            if (r > (data.length - i)) {
                for (let j = 0; i + j < data.length; j++) {
                    this.next[this.p + j] = data[i + j];
                }
                this.p += data.length - i;
                break;
            }
            else {
                for (let j = 0; this.p + j < 128; j++) {
                    this.next[this.p + j] = data[i + j];
                }
                crypto_hashblocks_hl(this.hh, this.hl, this.next, 128);
                i += 128 - this.p;
                this.p = 0;
            }
        }
        return this;
    }
    finish() {
        const out = new Uint8Array(64);
        let n = this.p;
        const x = new Uint8Array(256);
        let b = this.total;
        for (let i = 0; i < n; i++)
            x[i] = this.next[i];
        x[n] = 128;
        n = 256 - 128 * (n < 112 ? 1 : 0);
        x[n - 9] = 0;
        ts64(x, n - 8, (b / 0x20000000) | 0, b << 3);
        crypto_hashblocks_hl(this.hh, this.hl, x, n);
        for (let i = 0; i < 8; i++)
            ts64(out, 8 * i, this.hh[i], this.hl[i]);
        return out;
    }
}
exports.HashState = HashState;
function add(p, q) {
    var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
    Z(a, p[1], p[0]);
    Z(t, q[1], q[0]);
    M(a, a, t);
    A(b, p[0], p[1]);
    A(t, q[0], q[1]);
    M(b, b, t);
    M(c, p[3], q[3]);
    M(c, c, D2);
    M(d, p[2], q[2]);
    A(d, d, d);
    Z(e, b, a);
    Z(f, d, c);
    A(g, d, c);
    A(h, b, a);
    M(p[0], e, f);
    M(p[1], h, g);
    M(p[2], g, f);
    M(p[3], e, h);
}
function cswap(p, q, b) {
    var i;
    for (i = 0; i < 4; i++) {
        sel25519(p[i], q[i], b);
    }
}
function pack(r, p) {
    var tx = gf(), ty = gf(), zi = gf();
    inv25519(zi, p[2]);
    M(tx, p[0], zi);
    M(ty, p[1], zi);
    pack25519(r, ty);
    r[31] ^= par25519(tx) << 7;
}
function scalarmult(p, q, s) {
    var b, i;
    set25519(p[0], gf0);
    set25519(p[1], gf1);
    set25519(p[2], gf1);
    set25519(p[3], gf0);
    for (i = 255; i >= 0; --i) {
        b = (s[(i / 8) | 0] >> (i & 7)) & 1;
        cswap(p, q, b);
        add(q, p);
        add(p, p);
        cswap(p, q, b);
    }
}
function scalarbase(p, s) {
    const q = [gf(), gf(), gf(), gf()];
    set25519(q[0], X);
    set25519(q[1], Y);
    set25519(q[2], gf1);
    M(q[3], X, Y);
    scalarmult(p, q, s);
}
function crypto_sign_keypair(pk, sk, seeded) {
    const d = new Uint8Array(64);
    const p = [gf(), gf(), gf(), gf()];
    if (!seeded)
        randombytes(sk, 32);
    crypto_hash(d, sk, 32);
    d[0] &= 248;
    d[31] &= 127;
    d[31] |= 64;
    scalarbase(p, d);
    pack(pk, p);
    for (let i = 0; i < 32; i++)
        sk[i + 32] = pk[i];
    return 0;
}
var L = new Float64Array([
    0xed,
    0xd3,
    0xf5,
    0x5c,
    0x1a,
    0x63,
    0x12,
    0x58,
    0xd6,
    0x9c,
    0xf7,
    0xa2,
    0xde,
    0xf9,
    0xde,
    0x14,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0x10,
]);
function modL(r, x) {
    var carry, i, j, k;
    for (i = 63; i >= 32; --i) {
        carry = 0;
        for (j = i - 32, k = i - 12; j < k; ++j) {
            x[j] += carry - 16 * x[i] * L[j - (i - 32)];
            carry = (x[j] + 128) >> 8;
            x[j] -= carry * 256;
        }
        x[j] += carry;
        x[i] = 0;
    }
    carry = 0;
    for (j = 0; j < 32; j++) {
        x[j] += carry - (x[31] >> 4) * L[j];
        carry = x[j] >> 8;
        x[j] &= 255;
    }
    for (j = 0; j < 32; j++)
        x[j] -= carry * L[j];
    for (i = 0; i < 32; i++) {
        x[i + 1] += x[i] >> 8;
        r[i] = x[i] & 255;
    }
}
function reduce(r) {
    const x = new Float64Array(64);
    for (let i = 0; i < 64; i++)
        x[i] = r[i];
    for (let i = 0; i < 64; i++)
        r[i] = 0;
    modL(r, x);
}
// Note: difference from C - smlen returned, not passed as argument.
function crypto_sign(sm, m, n, sk) {
    var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
    var i, j, x = new Float64Array(64);
    var p = [gf(), gf(), gf(), gf()];
    crypto_hash(d, sk, 32);
    d[0] &= 248;
    d[31] &= 127;
    d[31] |= 64;
    var smlen = n + 64;
    for (i = 0; i < n; i++)
        sm[64 + i] = m[i];
    for (i = 0; i < 32; i++)
        sm[32 + i] = d[32 + i];
    crypto_hash(r, sm.subarray(32), n + 32);
    reduce(r);
    scalarbase(p, r);
    pack(sm, p);
    for (i = 32; i < 64; i++)
        sm[i] = sk[i];
    crypto_hash(h, sm, n + 64);
    reduce(h);
    for (i = 0; i < 64; i++)
        x[i] = 0;
    for (i = 0; i < 32; i++)
        x[i] = r[i];
    for (i = 0; i < 32; i++) {
        for (j = 0; j < 32; j++) {
            x[i + j] += h[i] * d[j];
        }
    }
    modL(sm.subarray(32), x);
    return smlen;
}
function unpackneg(r, p) {
    const t = gf();
    const chk = gf();
    const num = gf();
    const den = gf();
    const den2 = gf();
    const den4 = gf();
    const den6 = gf();
    set25519(r[2], gf1);
    unpack25519(r[1], p);
    S(num, r[1]);
    M(den, num, D);
    Z(num, num, r[2]);
    A(den, r[2], den);
    S(den2, den);
    S(den4, den2);
    M(den6, den4, den2);
    M(t, den6, num);
    M(t, t, den);
    pow2523(t, t);
    M(t, t, num);
    M(t, t, den);
    M(t, t, den);
    M(r[0], t, den);
    S(chk, r[0]);
    M(chk, chk, den);
    if (neq25519(chk, num))
        M(r[0], r[0], I);
    S(chk, r[0]);
    M(chk, chk, den);
    if (neq25519(chk, num))
        return -1;
    if (par25519(r[0]) === p[31] >> 7)
        Z(r[0], gf0, r[0]);
    M(r[3], r[0], r[1]);
    return 0;
}
function crypto_sign_open(m, sm, n, pk) {
    var i, mlen;
    var t = new Uint8Array(32), h = new Uint8Array(64);
    var p = [gf(), gf(), gf(), gf()], q = [gf(), gf(), gf(), gf()];
    mlen = -1;
    if (n < 64)
        return -1;
    if (unpackneg(q, pk))
        return -1;
    for (i = 0; i < n; i++)
        m[i] = sm[i];
    for (i = 0; i < 32; i++)
        m[i + 32] = pk[i];
    crypto_hash(h, m, n);
    reduce(h);
    scalarmult(p, q, h);
    scalarbase(q, sm.subarray(32));
    add(p, q);
    pack(t, p);
    n -= 64;
    if (crypto_verify_32(sm, 0, t, 0)) {
        for (i = 0; i < n; i++)
            m[i] = 0;
        return -1;
    }
    for (i = 0; i < n; i++)
        m[i] = sm[i + 64];
    mlen = n;
    return mlen;
}
var crypto_secretbox_KEYBYTES = 32, crypto_secretbox_NONCEBYTES = 24, crypto_secretbox_ZEROBYTES = 32, crypto_secretbox_BOXZEROBYTES = 16, crypto_scalarmult_BYTES = 32, crypto_scalarmult_SCALARBYTES = 32, crypto_box_PUBLICKEYBYTES = 32, crypto_box_SECRETKEYBYTES = 32, crypto_box_BEFORENMBYTES = 32, crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES, crypto_sign_BYTES = 64, crypto_sign_PUBLICKEYBYTES = 32, crypto_sign_SECRETKEYBYTES = 64, crypto_sign_SEEDBYTES = 32, crypto_hash_BYTES = 64;
/* High-level API */
function checkLengths(k, n) {
    if (k.length !== crypto_secretbox_KEYBYTES)
        throw new Error("bad key size");
    if (n.length !== crypto_secretbox_NONCEBYTES)
        throw new Error("bad nonce size");
}
function checkBoxLengths(pk, sk) {
    if (pk.length !== crypto_box_PUBLICKEYBYTES)
        throw new Error("bad public key size");
    if (sk.length !== crypto_box_SECRETKEYBYTES)
        throw new Error("bad secret key size");
}
function checkArrayTypes(...args) {
    for (var i = 0; i < args.length; i++) {
        if (!(args[i] instanceof Uint8Array))
            throw new TypeError("unexpected type, use Uint8Array");
    }
}
function cleanup(arr) {
    for (var i = 0; i < arr.length; i++)
        arr[i] = 0;
}
function randomBytes(n) {
    var b = new Uint8Array(n);
    randombytes(b, n);
    return b;
}
exports.randomBytes = randomBytes;
function secretbox(msg, nonce, key) {
    checkArrayTypes(msg, nonce, key);
    checkLengths(key, nonce);
    var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
    var c = new Uint8Array(m.length);
    for (var i = 0; i < msg.length; i++)
        m[i + crypto_secretbox_ZEROBYTES] = msg[i];
    crypto_secretbox(c, m, m.length, nonce, key);
    return c.subarray(crypto_secretbox_BOXZEROBYTES);
}
exports.secretbox = secretbox;
function secretbox_open(box, nonce, key) {
    checkArrayTypes(box, nonce, key);
    checkLengths(key, nonce);
    var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
    var m = new Uint8Array(c.length);
    for (var i = 0; i < box.length; i++)
        c[i + crypto_secretbox_BOXZEROBYTES] = box[i];
    if (c.length < 32)
        return null;
    if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0)
        return null;
    return m.subarray(crypto_secretbox_ZEROBYTES);
}
exports.secretbox_open = secretbox_open;
exports.secretbox_keyLength = crypto_secretbox_KEYBYTES;
exports.secretbox_nonceLength = crypto_secretbox_NONCEBYTES;
exports.secretbox_overheadLength = crypto_secretbox_BOXZEROBYTES;
function scalarMult(n, p) {
    checkArrayTypes(n, p);
    if (n.length !== crypto_scalarmult_SCALARBYTES)
        throw new Error("bad n size");
    if (p.length !== crypto_scalarmult_BYTES)
        throw new Error("bad p size");
    var q = new Uint8Array(crypto_scalarmult_BYTES);
    crypto_scalarmult(q, n, p);
    return q;
}
exports.scalarMult = scalarMult;
function scalarMult_base(n) {
    checkArrayTypes(n);
    if (n.length !== crypto_scalarmult_SCALARBYTES)
        throw new Error("bad n size");
    var q = new Uint8Array(crypto_scalarmult_BYTES);
    crypto_scalarmult_base(q, n);
    return q;
}
exports.scalarMult_base = scalarMult_base;
exports.scalarMult_scalarLength = crypto_scalarmult_SCALARBYTES;
exports.scalarMult_groupElementLength = crypto_scalarmult_BYTES;
function box(msg, nonce, publicKey, secretKey) {
    var k = box_before(publicKey, secretKey);
    return secretbox(msg, nonce, k);
}
exports.box = box;
function box_before(publicKey, secretKey) {
    checkArrayTypes(publicKey, secretKey);
    checkBoxLengths(publicKey, secretKey);
    var k = new Uint8Array(crypto_box_BEFORENMBYTES);
    crypto_box_beforenm(k, publicKey, secretKey);
    return k;
}
exports.box_before = box_before;
exports.box_after = secretbox;
function box_open(msg, nonce, publicKey, secretKey) {
    var k = box_before(publicKey, secretKey);
    return secretbox_open(msg, nonce, k);
}
exports.box_open = box_open;
exports.box_open_after = secretbox_open;
function box_keyPair() {
    var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
    var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
    crypto_box_keypair(pk, sk);
    return { publicKey: pk, secretKey: sk };
}
exports.box_keyPair = box_keyPair;
function box_keyPair_fromSecretKey(secretKey) {
    checkArrayTypes(secretKey);
    if (secretKey.length !== crypto_box_SECRETKEYBYTES)
        throw new Error("bad secret key size");
    var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
    crypto_scalarmult_base(pk, secretKey);
    return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
}
exports.box_keyPair_fromSecretKey = box_keyPair_fromSecretKey;
exports.box_publicKeyLength = crypto_box_PUBLICKEYBYTES;
exports.box_secretKeyLength = crypto_box_SECRETKEYBYTES;
exports.box_sharedKeyLength = crypto_box_BEFORENMBYTES;
exports.box_nonceLength = crypto_box_NONCEBYTES;
exports.box_overheadLength = exports.secretbox_overheadLength;
function sign(msg, secretKey) {
    checkArrayTypes(msg, secretKey);
    if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
        throw new Error("bad secret key size");
    var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length);
    crypto_sign(signedMsg, msg, msg.length, secretKey);
    return signedMsg;
}
exports.sign = sign;
function sign_open(signedMsg, publicKey) {
    checkArrayTypes(signedMsg, publicKey);
    if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
        throw new Error("bad public key size");
    var tmp = new Uint8Array(signedMsg.length);
    var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
    if (mlen < 0)
        return null;
    var m = new Uint8Array(mlen);
    for (var i = 0; i < m.length; i++)
        m[i] = tmp[i];
    return m;
}
exports.sign_open = sign_open;
function sign_detached(msg, secretKey) {
    var signedMsg = sign(msg, secretKey);
    var sig = new Uint8Array(crypto_sign_BYTES);
    for (var i = 0; i < sig.length; i++)
        sig[i] = signedMsg[i];
    return sig;
}
exports.sign_detached = sign_detached;
function sign_detached_verify(msg, sig, publicKey) {
    checkArrayTypes(msg, sig, publicKey);
    if (sig.length !== crypto_sign_BYTES)
        throw new Error("bad signature size");
    if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
        throw new Error("bad public key size");
    var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
    var m = new Uint8Array(crypto_sign_BYTES + msg.length);
    var i;
    for (i = 0; i < crypto_sign_BYTES; i++)
        sm[i] = sig[i];
    for (i = 0; i < msg.length; i++)
        sm[i + crypto_sign_BYTES] = msg[i];
    return crypto_sign_open(m, sm, sm.length, publicKey) >= 0;
}
exports.sign_detached_verify = sign_detached_verify;
function sign_keyPair() {
    var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
    var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
    crypto_sign_keypair(pk, sk, false);
    return { publicKey: pk, secretKey: sk };
}
exports.sign_keyPair = sign_keyPair;
function x25519_edwards_keyPair_fromSecretKey(secretKey) {
    const p = [gf(), gf(), gf(), gf()];
    const pk = new Uint8Array(32);
    const d = new Uint8Array(64);
    if (secretKey.length != 32) {
        throw new Error("bad secret key size");
    }
    d.set(secretKey, 0);
    //crypto_hash(d, secretKey, 32);
    d[0] &= 248;
    d[31] &= 127;
    d[31] |= 64;
    scalarbase(p, d);
    pack(pk, p);
    return pk;
}
exports.x25519_edwards_keyPair_fromSecretKey = x25519_edwards_keyPair_fromSecretKey;
function sign_keyPair_fromSecretKey(secretKey) {
    checkArrayTypes(secretKey);
    if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
        throw new Error("bad secret key size");
    var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
    for (var i = 0; i < pk.length; i++)
        pk[i] = secretKey[32 + i];
    return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
}
exports.sign_keyPair_fromSecretKey = sign_keyPair_fromSecretKey;
function sign_keyPair_fromSeed(seed) {
    checkArrayTypes(seed);
    if (seed.length !== crypto_sign_SEEDBYTES)
        throw new Error("bad seed size");
    var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
    var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
    for (var i = 0; i < 32; i++)
        sk[i] = seed[i];
    crypto_sign_keypair(pk, sk, true);
    return { publicKey: pk, secretKey: sk };
}
exports.sign_keyPair_fromSeed = sign_keyPair_fromSeed;
exports.sign_publicKeyLength = crypto_sign_PUBLICKEYBYTES;
exports.sign_secretKeyLength = crypto_sign_SECRETKEYBYTES;
exports.sign_seedLength = crypto_sign_SEEDBYTES;
exports.sign_signatureLength = crypto_sign_BYTES;
function hash(msg) {
    checkArrayTypes(msg);
    var h = new Uint8Array(crypto_hash_BYTES);
    crypto_hash(h, msg, msg.length);
    return h;
}
exports.hash = hash;
exports.hash_hashLength = crypto_hash_BYTES;
function verify(x, y) {
    checkArrayTypes(x, y);
    // Zero length arguments are considered not equal.
    if (x.length === 0 || y.length === 0)
        return false;
    if (x.length !== y.length)
        return false;
    return vn(x, 0, y, 0, x.length) === 0 ? true : false;
}
exports.verify = verify;
function setPRNG(fn) {
    randombytes = fn;
}
exports.setPRNG = setPRNG;
function sign_ed25519_pk_to_curve25519(ed25519_pk) {
    const ge_a = [gf(), gf(), gf(), gf()];
    const x = gf();
    const one_minus_y = gf();
    const x25519_pk = new Uint8Array(32);
    if (unpackneg(ge_a, ed25519_pk)) {
        throw Error("invalid public key");
    }
    set25519(one_minus_y, gf1);
    Z(one_minus_y, one_minus_y, ge_a[1]);
    set25519(x, gf1);
    A(x, x, ge_a[1]);
    inv25519(one_minus_y, one_minus_y);
    M(x, x, one_minus_y);
    pack25519(x25519_pk, x);
    return x25519_pk;
}
exports.sign_ed25519_pk_to_curve25519 = sign_ed25519_pk_to_curve25519;
(function () {
    // Initialize PRNG if environment provides CSPRNG.
    // If not, methods calling randombytes will throw.
    const crypto$1 = typeof self !== "undefined" ? self.crypto || self.msCrypto : null;
    if (crypto$1 && crypto$1.getRandomValues) {
        // Browsers.
        var QUOTA = 65536;
        setPRNG(function (x, n) {
            var i, v = new Uint8Array(n);
            for (i = 0; i < n; i += QUOTA) {
                crypto$1.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
            }
            for (i = 0; i < n; i++)
                x[i] = v[i];
            cleanup(v);
        });
    }
    else if (typeof require !== "undefined") {
        // Node.js.
        const cr = crypto;
        if (cr && cr.randomBytes) {
            setPRNG(function (x, n) {
                var i, v = cr.randomBytes(n);
                for (i = 0; i < n; i++)
                    x[i] = v[i];
                cleanup(v);
            });
        }
    }
})();

});

unwrapExports(naclFast);
var naclFast_1 = naclFast.HashState;
var naclFast_2 = naclFast.randomBytes;
var naclFast_3 = naclFast.secretbox;
var naclFast_4 = naclFast.secretbox_open;
var naclFast_5 = naclFast.secretbox_keyLength;
var naclFast_6 = naclFast.secretbox_nonceLength;
var naclFast_7 = naclFast.secretbox_overheadLength;
var naclFast_8 = naclFast.scalarMult;
var naclFast_9 = naclFast.scalarMult_base;
var naclFast_10 = naclFast.scalarMult_scalarLength;
var naclFast_11 = naclFast.scalarMult_groupElementLength;
var naclFast_12 = naclFast.box;
var naclFast_13 = naclFast.box_before;
var naclFast_14 = naclFast.box_after;
var naclFast_15 = naclFast.box_open;
var naclFast_16 = naclFast.box_open_after;
var naclFast_17 = naclFast.box_keyPair;
var naclFast_18 = naclFast.box_keyPair_fromSecretKey;
var naclFast_19 = naclFast.box_publicKeyLength;
var naclFast_20 = naclFast.box_secretKeyLength;
var naclFast_21 = naclFast.box_sharedKeyLength;
var naclFast_22 = naclFast.box_nonceLength;
var naclFast_23 = naclFast.box_overheadLength;
var naclFast_24 = naclFast.sign;
var naclFast_25 = naclFast.sign_open;
var naclFast_26 = naclFast.sign_detached;
var naclFast_27 = naclFast.sign_detached_verify;
var naclFast_28 = naclFast.sign_keyPair;
var naclFast_29 = naclFast.x25519_edwards_keyPair_fromSecretKey;
var naclFast_30 = naclFast.sign_keyPair_fromSecretKey;
var naclFast_31 = naclFast.sign_keyPair_fromSeed;
var naclFast_32 = naclFast.sign_publicKeyLength;
var naclFast_33 = naclFast.sign_secretKeyLength;
var naclFast_34 = naclFast.sign_seedLength;
var naclFast_35 = naclFast.sign_signatureLength;
var naclFast_36 = naclFast.hash;
var naclFast_37 = naclFast.hash_hashLength;
var naclFast_38 = naclFast.verify;
var naclFast_39 = naclFast.setPRNG;
var naclFast_40 = naclFast.sign_ed25519_pk_to_curve25519;

var BigInteger = createCommonjsModule(function (module) {
var bigInt = (function (undefined$1) {

    var BASE = 1e7,
        LOG_BASE = 7,
        MAX_INT = 9007199254740992,
        MAX_INT_ARR = smallToArray(MAX_INT),
        DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz";

    var supportsNativeBigInt = typeof BigInt === "function";

    function Integer(v, radix, alphabet, caseSensitive) {
        if (typeof v === "undefined") return Integer[0];
        if (typeof radix !== "undefined") return +radix === 10 && !alphabet ? parseValue(v) : parseBase(v, radix, alphabet, caseSensitive);
        return parseValue(v);
    }

    function BigInteger(value, sign) {
        this.value = value;
        this.sign = sign;
        this.isSmall = false;
    }
    BigInteger.prototype = Object.create(Integer.prototype);

    function SmallInteger(value) {
        this.value = value;
        this.sign = value < 0;
        this.isSmall = true;
    }
    SmallInteger.prototype = Object.create(Integer.prototype);

    function NativeBigInt(value) {
        this.value = value;
    }
    NativeBigInt.prototype = Object.create(Integer.prototype);

    function isPrecise(n) {
        return -MAX_INT < n && n < MAX_INT;
    }

    function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
        if (n < 1e7)
            return [n];
        if (n < 1e14)
            return [n % 1e7, Math.floor(n / 1e7)];
        return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
    }

    function arrayToSmall(arr) { // If BASE changes this function may need to change
        trim(arr);
        var length = arr.length;
        if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
            switch (length) {
                case 0: return 0;
                case 1: return arr[0];
                case 2: return arr[0] + arr[1] * BASE;
                default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
            }
        }
        return arr;
    }

    function trim(v) {
        var i = v.length;
        while (v[--i] === 0);
        v.length = i + 1;
    }

    function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
        var x = new Array(length);
        var i = -1;
        while (++i < length) {
            x[i] = 0;
        }
        return x;
    }

    function truncate(n) {
        if (n > 0) return Math.floor(n);
        return Math.ceil(n);
    }

    function add(a, b) { // assumes a and b are arrays with a.length >= b.length
        var l_a = a.length,
            l_b = b.length,
            r = new Array(l_a),
            carry = 0,
            base = BASE,
            sum, i;
        for (i = 0; i < l_b; i++) {
            sum = a[i] + b[i] + carry;
            carry = sum >= base ? 1 : 0;
            r[i] = sum - carry * base;
        }
        while (i < l_a) {
            sum = a[i] + carry;
            carry = sum === base ? 1 : 0;
            r[i++] = sum - carry * base;
        }
        if (carry > 0) r.push(carry);
        return r;
    }

    function addAny(a, b) {
        if (a.length >= b.length) return add(a, b);
        return add(b, a);
    }

    function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
        var l = a.length,
            r = new Array(l),
            base = BASE,
            sum, i;
        for (i = 0; i < l; i++) {
            sum = a[i] - base + carry;
            carry = Math.floor(sum / base);
            r[i] = sum - carry * base;
            carry += 1;
        }
        while (carry > 0) {
            r[i++] = carry % base;
            carry = Math.floor(carry / base);
        }
        return r;
    }

    BigInteger.prototype.add = function (v) {
        var n = parseValue(v);
        if (this.sign !== n.sign) {
            return this.subtract(n.negate());
        }
        var a = this.value, b = n.value;
        if (n.isSmall) {
            return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
        }
        return new BigInteger(addAny(a, b), this.sign);
    };
    BigInteger.prototype.plus = BigInteger.prototype.add;

    SmallInteger.prototype.add = function (v) {
        var n = parseValue(v);
        var a = this.value;
        if (a < 0 !== n.sign) {
            return this.subtract(n.negate());
        }
        var b = n.value;
        if (n.isSmall) {
            if (isPrecise(a + b)) return new SmallInteger(a + b);
            b = smallToArray(Math.abs(b));
        }
        return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
    };
    SmallInteger.prototype.plus = SmallInteger.prototype.add;

    NativeBigInt.prototype.add = function (v) {
        return new NativeBigInt(this.value + parseValue(v).value);
    };
    NativeBigInt.prototype.plus = NativeBigInt.prototype.add;

    function subtract(a, b) { // assumes a and b are arrays with a >= b
        var a_l = a.length,
            b_l = b.length,
            r = new Array(a_l),
            borrow = 0,
            base = BASE,
            i, difference;
        for (i = 0; i < b_l; i++) {
            difference = a[i] - borrow - b[i];
            if (difference < 0) {
                difference += base;
                borrow = 1;
            } else borrow = 0;
            r[i] = difference;
        }
        for (i = b_l; i < a_l; i++) {
            difference = a[i] - borrow;
            if (difference < 0) difference += base;
            else {
                r[i++] = difference;
                break;
            }
            r[i] = difference;
        }
        for (; i < a_l; i++) {
            r[i] = a[i];
        }
        trim(r);
        return r;
    }

    function subtractAny(a, b, sign) {
        var value;
        if (compareAbs(a, b) >= 0) {
            value = subtract(a, b);
        } else {
            value = subtract(b, a);
            sign = !sign;
        }
        value = arrayToSmall(value);
        if (typeof value === "number") {
            if (sign) value = -value;
            return new SmallInteger(value);
        }
        return new BigInteger(value, sign);
    }

    function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT
        var l = a.length,
            r = new Array(l),
            carry = -b,
            base = BASE,
            i, difference;
        for (i = 0; i < l; i++) {
            difference = a[i] + carry;
            carry = Math.floor(difference / base);
            difference %= base;
            r[i] = difference < 0 ? difference + base : difference;
        }
        r = arrayToSmall(r);
        if (typeof r === "number") {
            if (sign) r = -r;
            return new SmallInteger(r);
        } return new BigInteger(r, sign);
    }

    BigInteger.prototype.subtract = function (v) {
        var n = parseValue(v);
        if (this.sign !== n.sign) {
            return this.add(n.negate());
        }
        var a = this.value, b = n.value;
        if (n.isSmall)
            return subtractSmall(a, Math.abs(b), this.sign);
        return subtractAny(a, b, this.sign);
    };
    BigInteger.prototype.minus = BigInteger.prototype.subtract;

    SmallInteger.prototype.subtract = function (v) {
        var n = parseValue(v);
        var a = this.value;
        if (a < 0 !== n.sign) {
            return this.add(n.negate());
        }
        var b = n.value;
        if (n.isSmall) {
            return new SmallInteger(a - b);
        }
        return subtractSmall(b, Math.abs(a), a >= 0);
    };
    SmallInteger.prototype.minus = SmallInteger.prototype.subtract;

    NativeBigInt.prototype.subtract = function (v) {
        return new NativeBigInt(this.value - parseValue(v).value);
    };
    NativeBigInt.prototype.minus = NativeBigInt.prototype.subtract;

    BigInteger.prototype.negate = function () {
        return new BigInteger(this.value, !this.sign);
    };
    SmallInteger.prototype.negate = function () {
        var sign = this.sign;
        var small = new SmallInteger(-this.value);
        small.sign = !sign;
        return small;
    };
    NativeBigInt.prototype.negate = function () {
        return new NativeBigInt(-this.value);
    };

    BigInteger.prototype.abs = function () {
        return new BigInteger(this.value, false);
    };
    SmallInteger.prototype.abs = function () {
        return new SmallInteger(Math.abs(this.value));
    };
    NativeBigInt.prototype.abs = function () {
        return new NativeBigInt(this.value >= 0 ? this.value : -this.value);
    };


    function multiplyLong(a, b) {
        var a_l = a.length,
            b_l = b.length,
            l = a_l + b_l,
            r = createArray(l),
            base = BASE,
            product, carry, i, a_i, b_j;
        for (i = 0; i < a_l; ++i) {
            a_i = a[i];
            for (var j = 0; j < b_l; ++j) {
                b_j = b[j];
                product = a_i * b_j + r[i + j];
                carry = Math.floor(product / base);
                r[i + j] = product - carry * base;
                r[i + j + 1] += carry;
            }
        }
        trim(r);
        return r;
    }

    function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE
        var l = a.length,
            r = new Array(l),
            base = BASE,
            carry = 0,
            product, i;
        for (i = 0; i < l; i++) {
            product = a[i] * b + carry;
            carry = Math.floor(product / base);
            r[i] = product - carry * base;
        }
        while (carry > 0) {
            r[i++] = carry % base;
            carry = Math.floor(carry / base);
        }
        return r;
    }

    function shiftLeft(x, n) {
        var r = [];
        while (n-- > 0) r.push(0);
        return r.concat(x);
    }

    function multiplyKaratsuba(x, y) {
        var n = Math.max(x.length, y.length);

        if (n <= 30) return multiplyLong(x, y);
        n = Math.ceil(n / 2);

        var b = x.slice(n),
            a = x.slice(0, n),
            d = y.slice(n),
            c = y.slice(0, n);

        var ac = multiplyKaratsuba(a, c),
            bd = multiplyKaratsuba(b, d),
            abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));

        var product = addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n));
        trim(product);
        return product;
    }

    // The following function is derived from a surface fit of a graph plotting the performance difference
    // between long multiplication and karatsuba multiplication versus the lengths of the two arrays.
    function useKaratsuba(l1, l2) {
        return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0;
    }

    BigInteger.prototype.multiply = function (v) {
        var n = parseValue(v),
            a = this.value, b = n.value,
            sign = this.sign !== n.sign,
            abs;
        if (n.isSmall) {
            if (b === 0) return Integer[0];
            if (b === 1) return this;
            if (b === -1) return this.negate();
            abs = Math.abs(b);
            if (abs < BASE) {
                return new BigInteger(multiplySmall(a, abs), sign);
            }
            b = smallToArray(abs);
        }
        if (useKaratsuba(a.length, b.length)) // Karatsuba is only faster for certain array sizes
            return new BigInteger(multiplyKaratsuba(a, b), sign);
        return new BigInteger(multiplyLong(a, b), sign);
    };

    BigInteger.prototype.times = BigInteger.prototype.multiply;

    function multiplySmallAndArray(a, b, sign) { // a >= 0
        if (a < BASE) {
            return new BigInteger(multiplySmall(b, a), sign);
        }
        return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
    }
    SmallInteger.prototype._multiplyBySmall = function (a) {
        if (isPrecise(a.value * this.value)) {
            return new SmallInteger(a.value * this.value);
        }
        return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign);
    };
    BigInteger.prototype._multiplyBySmall = function (a) {
        if (a.value === 0) return Integer[0];
        if (a.value === 1) return this;
        if (a.value === -1) return this.negate();
        return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign);
    };
    SmallInteger.prototype.multiply = function (v) {
        return parseValue(v)._multiplyBySmall(this);
    };
    SmallInteger.prototype.times = SmallInteger.prototype.multiply;

    NativeBigInt.prototype.multiply = function (v) {
        return new NativeBigInt(this.value * parseValue(v).value);
    };
    NativeBigInt.prototype.times = NativeBigInt.prototype.multiply;

    function square(a) {
        //console.assert(2 * BASE * BASE < MAX_INT);
        var l = a.length,
            r = createArray(l + l),
            base = BASE,
            product, carry, i, a_i, a_j;
        for (i = 0; i < l; i++) {
            a_i = a[i];
            carry = 0 - a_i * a_i;
            for (var j = i; j < l; j++) {
                a_j = a[j];
                product = 2 * (a_i * a_j) + r[i + j] + carry;
                carry = Math.floor(product / base);
                r[i + j] = product - carry * base;
            }
            r[i + l] = carry;
        }
        trim(r);
        return r;
    }

    BigInteger.prototype.square = function () {
        return new BigInteger(square(this.value), false);
    };

    SmallInteger.prototype.square = function () {
        var value = this.value * this.value;
        if (isPrecise(value)) return new SmallInteger(value);
        return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
    };

    NativeBigInt.prototype.square = function (v) {
        return new NativeBigInt(this.value * this.value);
    };

    function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
        var a_l = a.length,
            b_l = b.length,
            base = BASE,
            result = createArray(b.length),
            divisorMostSignificantDigit = b[b_l - 1],
            // normalization
            lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
            remainder = multiplySmall(a, lambda),
            divisor = multiplySmall(b, lambda),
            quotientDigit, shift, carry, borrow, i, l, q;
        if (remainder.length <= a_l) remainder.push(0);
        divisor.push(0);
        divisorMostSignificantDigit = divisor[b_l - 1];
        for (shift = a_l - b_l; shift >= 0; shift--) {
            quotientDigit = base - 1;
            if (remainder[shift + b_l] !== divisorMostSignificantDigit) {
                quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit);
            }
            // quotientDigit <= base - 1
            carry = 0;
            borrow = 0;
            l = divisor.length;
            for (i = 0; i < l; i++) {
                carry += quotientDigit * divisor[i];
                q = Math.floor(carry / base);
                borrow += remainder[shift + i] - (carry - q * base);
                carry = q;
                if (borrow < 0) {
                    remainder[shift + i] = borrow + base;
                    borrow = -1;
                } else {
                    remainder[shift + i] = borrow;
                    borrow = 0;
                }
            }
            while (borrow !== 0) {
                quotientDigit -= 1;
                carry = 0;
                for (i = 0; i < l; i++) {
                    carry += remainder[shift + i] - base + divisor[i];
                    if (carry < 0) {
                        remainder[shift + i] = carry + base;
                        carry = 0;
                    } else {
                        remainder[shift + i] = carry;
                        carry = 1;
                    }
                }
                borrow += carry;
            }
            result[shift] = quotientDigit;
        }
        // denormalization
        remainder = divModSmall(remainder, lambda)[0];
        return [arrayToSmall(result), arrayToSmall(remainder)];
    }

    function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
        // Performs faster than divMod1 on larger input sizes.
        var a_l = a.length,
            b_l = b.length,
            result = [],
            part = [],
            base = BASE,
            guess, xlen, highx, highy, check;
        while (a_l) {
            part.unshift(a[--a_l]);
            trim(part);
            if (compareAbs(part, b) < 0) {
                result.push(0);
                continue;
            }
            xlen = part.length;
            highx = part[xlen - 1] * base + part[xlen - 2];
            highy = b[b_l - 1] * base + b[b_l - 2];
            if (xlen > b_l) {
                highx = (highx + 1) * base;
            }
            guess = Math.ceil(highx / highy);
            do {
                check = multiplySmall(b, guess);
                if (compareAbs(check, part) <= 0) break;
                guess--;
            } while (guess);
            result.push(guess);
            part = subtract(part, check);
        }
        result.reverse();
        return [arrayToSmall(result), arrayToSmall(part)];
    }

    function divModSmall(value, lambda) {
        var length = value.length,
            quotient = createArray(length),
            base = BASE,
            i, q, remainder, divisor;
        remainder = 0;
        for (i = length - 1; i >= 0; --i) {
            divisor = remainder * base + value[i];
            q = truncate(divisor / lambda);
            remainder = divisor - q * lambda;
            quotient[i] = q | 0;
        }
        return [quotient, remainder | 0];
    }

    function divModAny(self, v) {
        var value, n = parseValue(v);
        if (supportsNativeBigInt) {
            return [new NativeBigInt(self.value / n.value), new NativeBigInt(self.value % n.value)];
        }
        var a = self.value, b = n.value;
        var quotient;
        if (b === 0) throw new Error("Cannot divide by zero");
        if (self.isSmall) {
            if (n.isSmall) {
                return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
            }
            return [Integer[0], self];
        }
        if (n.isSmall) {
            if (b === 1) return [self, Integer[0]];
            if (b == -1) return [self.negate(), Integer[0]];
            var abs = Math.abs(b);
            if (abs < BASE) {
                value = divModSmall(a, abs);
                quotient = arrayToSmall(value[0]);
                var remainder = value[1];
                if (self.sign) remainder = -remainder;
                if (typeof quotient === "number") {
                    if (self.sign !== n.sign) quotient = -quotient;
                    return [new SmallInteger(quotient), new SmallInteger(remainder)];
                }
                return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)];
            }
            b = smallToArray(abs);
        }
        var comparison = compareAbs(a, b);
        if (comparison === -1) return [Integer[0], self];
        if (comparison === 0) return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]];

        // divMod1 is faster on smaller input sizes
        if (a.length + b.length <= 200)
            value = divMod1(a, b);
        else value = divMod2(a, b);

        quotient = value[0];
        var qSign = self.sign !== n.sign,
            mod = value[1],
            mSign = self.sign;
        if (typeof quotient === "number") {
            if (qSign) quotient = -quotient;
            quotient = new SmallInteger(quotient);
        } else quotient = new BigInteger(quotient, qSign);
        if (typeof mod === "number") {
            if (mSign) mod = -mod;
            mod = new SmallInteger(mod);
        } else mod = new BigInteger(mod, mSign);
        return [quotient, mod];
    }

    BigInteger.prototype.divmod = function (v) {
        var result = divModAny(this, v);
        return {
            quotient: result[0],
            remainder: result[1]
        };
    };
    NativeBigInt.prototype.divmod = SmallInteger.prototype.divmod = BigInteger.prototype.divmod;


    BigInteger.prototype.divide = function (v) {
        return divModAny(this, v)[0];
    };
    NativeBigInt.prototype.over = NativeBigInt.prototype.divide = function (v) {
        return new NativeBigInt(this.value / parseValue(v).value);
    };
    SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide;

    BigInteger.prototype.mod = function (v) {
        return divModAny(this, v)[1];
    };
    NativeBigInt.prototype.mod = NativeBigInt.prototype.remainder = function (v) {
        return new NativeBigInt(this.value % parseValue(v).value);
    };
    SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod;

    BigInteger.prototype.pow = function (v) {
        var n = parseValue(v),
            a = this.value,
            b = n.value,
            value, x, y;
        if (b === 0) return Integer[1];
        if (a === 0) return Integer[0];
        if (a === 1) return Integer[1];
        if (a === -1) return n.isEven() ? Integer[1] : Integer[-1];
        if (n.sign) {
            return Integer[0];
        }
        if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large.");
        if (this.isSmall) {
            if (isPrecise(value = Math.pow(a, b)))
                return new SmallInteger(truncate(value));
        }
        x = this;
        y = Integer[1];
        while (true) {
            if (b & 1 === 1) {
                y = y.times(x);
                --b;
            }
            if (b === 0) break;
            b /= 2;
            x = x.square();
        }
        return y;
    };
    SmallInteger.prototype.pow = BigInteger.prototype.pow;

    NativeBigInt.prototype.pow = function (v) {
        var n = parseValue(v);
        var a = this.value, b = n.value;
        var _0 = BigInt(0), _1 = BigInt(1), _2 = BigInt(2);
        if (b === _0) return Integer[1];
        if (a === _0) return Integer[0];
        if (a === _1) return Integer[1];
        if (a === BigInt(-1)) return n.isEven() ? Integer[1] : Integer[-1];
        if (n.isNegative()) return new NativeBigInt(_0);
        var x = this;
        var y = Integer[1];
        while (true) {
            if ((b & _1) === _1) {
                y = y.times(x);
                --b;
            }
            if (b === _0) break;
            b /= _2;
            x = x.square();
        }
        return y;
    };

    BigInteger.prototype.modPow = function (exp, mod) {
        exp = parseValue(exp);
        mod = parseValue(mod);
        if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0");
        var r = Integer[1],
            base = this.mod(mod);
        if (exp.isNegative()) {
            exp = exp.multiply(Integer[-1]);
            base = base.modInv(mod);
        }
        while (exp.isPositive()) {
            if (base.isZero()) return Integer[0];
            if (exp.isOdd()) r = r.multiply(base).mod(mod);
            exp = exp.divide(2);
            base = base.square().mod(mod);
        }
        return r;
    };
    NativeBigInt.prototype.modPow = SmallInteger.prototype.modPow = BigInteger.prototype.modPow;

    function compareAbs(a, b) {
        if (a.length !== b.length) {
            return a.length > b.length ? 1 : -1;
        }
        for (var i = a.length - 1; i >= 0; i--) {
            if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
        }
        return 0;
    }

    BigInteger.prototype.compareAbs = function (v) {
        var n = parseValue(v),
            a = this.value,
            b = n.value;
        if (n.isSmall) return 1;
        return compareAbs(a, b);
    };
    SmallInteger.prototype.compareAbs = function (v) {
        var n = parseValue(v),
            a = Math.abs(this.value),
            b = n.value;
        if (n.isSmall) {
            b = Math.abs(b);
            return a === b ? 0 : a > b ? 1 : -1;
        }
        return -1;
    };
    NativeBigInt.prototype.compareAbs = function (v) {
        var a = this.value;
        var b = parseValue(v).value;
        a = a >= 0 ? a : -a;
        b = b >= 0 ? b : -b;
        return a === b ? 0 : a > b ? 1 : -1;
    };

    BigInteger.prototype.compare = function (v) {
        // See discussion about comparison with Infinity:
        // https://github.com/peterolson/BigInteger.js/issues/61
        if (v === Infinity) {
            return -1;
        }
        if (v === -Infinity) {
            return 1;
        }

        var n = parseValue(v),
            a = this.value,
            b = n.value;
        if (this.sign !== n.sign) {
            return n.sign ? 1 : -1;
        }
        if (n.isSmall) {
            return this.sign ? -1 : 1;
        }
        return compareAbs(a, b) * (this.sign ? -1 : 1);
    };
    BigInteger.prototype.compareTo = BigInteger.prototype.compare;

    SmallInteger.prototype.compare = function (v) {
        if (v === Infinity) {
            return -1;
        }
        if (v === -Infinity) {
            return 1;
        }

        var n = parseValue(v),
            a = this.value,
            b = n.value;
        if (n.isSmall) {
            return a == b ? 0 : a > b ? 1 : -1;
        }
        if (a < 0 !== n.sign) {
            return a < 0 ? -1 : 1;
        }
        return a < 0 ? 1 : -1;
    };
    SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;

    NativeBigInt.prototype.compare = function (v) {
        if (v === Infinity) {
            return -1;
        }
        if (v === -Infinity) {
            return 1;
        }
        var a = this.value;
        var b = parseValue(v).value;
        return a === b ? 0 : a > b ? 1 : -1;
    };
    NativeBigInt.prototype.compareTo = NativeBigInt.prototype.compare;

    BigInteger.prototype.equals = function (v) {
        return this.compare(v) === 0;
    };
    NativeBigInt.prototype.eq = NativeBigInt.prototype.equals = SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals;

    BigInteger.prototype.notEquals = function (v) {
        return this.compare(v) !== 0;
    };
    NativeBigInt.prototype.neq = NativeBigInt.prototype.notEquals = SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals;

    BigInteger.prototype.greater = function (v) {
        return this.compare(v) > 0;
    };
    NativeBigInt.prototype.gt = NativeBigInt.prototype.greater = SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater;

    BigInteger.prototype.lesser = function (v) {
        return this.compare(v) < 0;
    };
    NativeBigInt.prototype.lt = NativeBigInt.prototype.lesser = SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser;

    BigInteger.prototype.greaterOrEquals = function (v) {
        return this.compare(v) >= 0;
    };
    NativeBigInt.prototype.geq = NativeBigInt.prototype.greaterOrEquals = SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;

    BigInteger.prototype.lesserOrEquals = function (v) {
        return this.compare(v) <= 0;
    };
    NativeBigInt.prototype.leq = NativeBigInt.prototype.lesserOrEquals = SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;

    BigInteger.prototype.isEven = function () {
        return (this.value[0] & 1) === 0;
    };
    SmallInteger.prototype.isEven = function () {
        return (this.value & 1) === 0;
    };
    NativeBigInt.prototype.isEven = function () {
        return (this.value & BigInt(1)) === BigInt(0);
    };

    BigInteger.prototype.isOdd = function () {
        return (this.value[0] & 1) === 1;
    };
    SmallInteger.prototype.isOdd = function () {
        return (this.value & 1) === 1;
    };
    NativeBigInt.prototype.isOdd = function () {
        return (this.value & BigInt(1)) === BigInt(1);
    };

    BigInteger.prototype.isPositive = function () {
        return !this.sign;
    };
    SmallInteger.prototype.isPositive = function () {
        return this.value > 0;
    };
    NativeBigInt.prototype.isPositive = SmallInteger.prototype.isPositive;

    BigInteger.prototype.isNegative = function () {
        return this.sign;
    };
    SmallInteger.prototype.isNegative = function () {
        return this.value < 0;
    };
    NativeBigInt.prototype.isNegative = SmallInteger.prototype.isNegative;

    BigInteger.prototype.isUnit = function () {
        return false;
    };
    SmallInteger.prototype.isUnit = function () {
        return Math.abs(this.value) === 1;
    };
    NativeBigInt.prototype.isUnit = function () {
        return this.abs().value === BigInt(1);
    };

    BigInteger.prototype.isZero = function () {
        return false;
    };
    SmallInteger.prototype.isZero = function () {
        return this.value === 0;
    };
    NativeBigInt.prototype.isZero = function () {
        return this.value === BigInt(0);
    };

    BigInteger.prototype.isDivisibleBy = function (v) {
        var n = parseValue(v);
        if (n.isZero()) return false;
        if (n.isUnit()) return true;
        if (n.compareAbs(2) === 0) return this.isEven();
        return this.mod(n).isZero();
    };
    NativeBigInt.prototype.isDivisibleBy = SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy;

    function isBasicPrime(v) {
        var n = v.abs();
        if (n.isUnit()) return false;
        if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
        if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
        if (n.lesser(49)) return true;
        // we don't know if it's prime: let the other functions figure it out
    }

    function millerRabinTest(n, a) {
        var nPrev = n.prev(),
            b = nPrev,
            r = 0,
            d, i, x;
        while (b.isEven()) b = b.divide(2), r++;
        next: for (i = 0; i < a.length; i++) {
            if (n.lesser(a[i])) continue;
            x = bigInt(a[i]).modPow(b, n);
            if (x.isUnit() || x.equals(nPrev)) continue;
            for (d = r - 1; d != 0; d--) {
                x = x.square().mod(n);
                if (x.isUnit()) return false;
                if (x.equals(nPrev)) continue next;
            }
            return false;
        }
        return true;
    }

    // Set "strict" to true to force GRH-supported lower bound of 2*log(N)^2
    BigInteger.prototype.isPrime = function (strict) {
        var isPrime = isBasicPrime(this);
        if (isPrime !== undefined$1) return isPrime;
        var n = this.abs();
        var bits = n.bitLength();
        if (bits <= 64)
            return millerRabinTest(n, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]);
        var logN = Math.log(2) * bits.toJSNumber();
        var t = Math.ceil((strict === true) ? (2 * Math.pow(logN, 2)) : logN);
        for (var a = [], i = 0; i < t; i++) {
            a.push(bigInt(i + 2));
        }
        return millerRabinTest(n, a);
    };
    NativeBigInt.prototype.isPrime = SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;

    BigInteger.prototype.isProbablePrime = function (iterations, rng) {
        var isPrime = isBasicPrime(this);
        if (isPrime !== undefined$1) return isPrime;
        var n = this.abs();
        var t = iterations === undefined$1 ? 5 : iterations;
        for (var a = [], i = 0; i < t; i++) {
            a.push(bigInt.randBetween(2, n.minus(2), rng));
        }
        return millerRabinTest(n, a);
    };
    NativeBigInt.prototype.isProbablePrime = SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;

    BigInteger.prototype.modInv = function (n) {
        var t = bigInt.zero, newT = bigInt.one, r = parseValue(n), newR = this.abs(), q, lastT, lastR;
        while (!newR.isZero()) {
            q = r.divide(newR);
            lastT = t;
            lastR = r;
            t = newT;
            r = newR;
            newT = lastT.subtract(q.multiply(newT));
            newR = lastR.subtract(q.multiply(newR));
        }
        if (!r.isUnit()) throw new Error(this.toString() + " and " + n.toString() + " are not co-prime");
        if (t.compare(0) === -1) {
            t = t.add(n);
        }
        if (this.isNegative()) {
            return t.negate();
        }
        return t;
    };

    NativeBigInt.prototype.modInv = SmallInteger.prototype.modInv = BigInteger.prototype.modInv;

    BigInteger.prototype.next = function () {
        var value = this.value;
        if (this.sign) {
            return subtractSmall(value, 1, this.sign);
        }
        return new BigInteger(addSmall(value, 1), this.sign);
    };
    SmallInteger.prototype.next = function () {
        var value = this.value;
        if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
        return new BigInteger(MAX_INT_ARR, false);
    };
    NativeBigInt.prototype.next = function () {
        return new NativeBigInt(this.value + BigInt(1));
    };

    BigInteger.prototype.prev = function () {
        var value = this.value;
        if (this.sign) {
            return new BigInteger(addSmall(value, 1), true);
        }
        return subtractSmall(value, 1, this.sign);
    };
    SmallInteger.prototype.prev = function () {
        var value = this.value;
        if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
        return new BigInteger(MAX_INT_ARR, true);
    };
    NativeBigInt.prototype.prev = function () {
        return new NativeBigInt(this.value - BigInt(1));
    };

    var powersOfTwo = [1];
    while (2 * powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
    var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];

    function shift_isSmall(n) {
        return Math.abs(n) <= BASE;
    }

    BigInteger.prototype.shiftLeft = function (v) {
        var n = parseValue(v).toJSNumber();
        if (!shift_isSmall(n)) {
            throw new Error(String(n) + " is too large for shifting.");
        }
        if (n < 0) return this.shiftRight(-n);
        var result = this;
        if (result.isZero()) return result;
        while (n >= powers2Length) {
            result = result.multiply(highestPower2);
            n -= powers2Length - 1;
        }
        return result.multiply(powersOfTwo[n]);
    };
    NativeBigInt.prototype.shiftLeft = SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft;

    BigInteger.prototype.shiftRight = function (v) {
        var remQuo;
        var n = parseValue(v).toJSNumber();
        if (!shift_isSmall(n)) {
            throw new Error(String(n) + " is too large for shifting.");
        }
        if (n < 0) return this.shiftLeft(-n);
        var result = this;
        while (n >= powers2Length) {
            if (result.isZero() || (result.isNegative() && result.isUnit())) return result;
            remQuo = divModAny(result, highestPower2);
            result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
            n -= powers2Length - 1;
        }
        remQuo = divModAny(result, powersOfTwo[n]);
        return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
    };
    NativeBigInt.prototype.shiftRight = SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight;

    function bitwise(x, y, fn) {
        y = parseValue(y);
        var xSign = x.isNegative(), ySign = y.isNegative();
        var xRem = xSign ? x.not() : x,
            yRem = ySign ? y.not() : y;
        var xDigit = 0, yDigit = 0;
        var xDivMod = null, yDivMod = null;
        var result = [];
        while (!xRem.isZero() || !yRem.isZero()) {
            xDivMod = divModAny(xRem, highestPower2);
            xDigit = xDivMod[1].toJSNumber();
            if (xSign) {
                xDigit = highestPower2 - 1 - xDigit; // two's complement for negative numbers
            }

            yDivMod = divModAny(yRem, highestPower2);
            yDigit = yDivMod[1].toJSNumber();
            if (ySign) {
                yDigit = highestPower2 - 1 - yDigit; // two's complement for negative numbers
            }

            xRem = xDivMod[0];
            yRem = yDivMod[0];
            result.push(fn(xDigit, yDigit));
        }
        var sum = fn(xSign ? 1 : 0, ySign ? 1 : 0) !== 0 ? bigInt(-1) : bigInt(0);
        for (var i = result.length - 1; i >= 0; i -= 1) {
            sum = sum.multiply(highestPower2).add(bigInt(result[i]));
        }
        return sum;
    }

    BigInteger.prototype.not = function () {
        return this.negate().prev();
    };
    NativeBigInt.prototype.not = SmallInteger.prototype.not = BigInteger.prototype.not;

    BigInteger.prototype.and = function (n) {
        return bitwise(this, n, function (a, b) { return a & b; });
    };
    NativeBigInt.prototype.and = SmallInteger.prototype.and = BigInteger.prototype.and;

    BigInteger.prototype.or = function (n) {
        return bitwise(this, n, function (a, b) { return a | b; });
    };
    NativeBigInt.prototype.or = SmallInteger.prototype.or = BigInteger.prototype.or;

    BigInteger.prototype.xor = function (n) {
        return bitwise(this, n, function (a, b) { return a ^ b; });
    };
    NativeBigInt.prototype.xor = SmallInteger.prototype.xor = BigInteger.prototype.xor;

    var LOBMASK_I = 1 << 30, LOBMASK_BI = (BASE & -BASE) * (BASE & -BASE) | LOBMASK_I;
    function roughLOB(n) { // get lowestOneBit (rough)
        // SmallInteger: return Min(lowestOneBit(n), 1 << 30)
        // BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7]
        var v = n.value,
            x = typeof v === "number" ? v | LOBMASK_I :
                typeof v === "bigint" ? v | BigInt(LOBMASK_I) :
                    v[0] + v[1] * BASE | LOBMASK_BI;
        return x & -x;
    }

    function integerLogarithm(value, base) {
        if (base.compareTo(value) <= 0) {
            var tmp = integerLogarithm(value, base.square(base));
            var p = tmp.p;
            var e = tmp.e;
            var t = p.multiply(base);
            return t.compareTo(value) <= 0 ? { p: t, e: e * 2 + 1 } : { p: p, e: e * 2 };
        }
        return { p: bigInt(1), e: 0 };
    }

    BigInteger.prototype.bitLength = function () {
        var n = this;
        if (n.compareTo(bigInt(0)) < 0) {
            n = n.negate().subtract(bigInt(1));
        }
        if (n.compareTo(bigInt(0)) === 0) {
            return bigInt(0);
        }
        return bigInt(integerLogarithm(n, bigInt(2)).e).add(bigInt(1));
    };
    NativeBigInt.prototype.bitLength = SmallInteger.prototype.bitLength = BigInteger.prototype.bitLength;

    function max(a, b) {
        a = parseValue(a);
        b = parseValue(b);
        return a.greater(b) ? a : b;
    }
    function min(a, b) {
        a = parseValue(a);
        b = parseValue(b);
        return a.lesser(b) ? a : b;
    }
    function gcd(a, b) {
        a = parseValue(a).abs();
        b = parseValue(b).abs();
        if (a.equals(b)) return a;
        if (a.isZero()) return b;
        if (b.isZero()) return a;
        var c = Integer[1], d, t;
        while (a.isEven() && b.isEven()) {
            d = min(roughLOB(a), roughLOB(b));
            a = a.divide(d);
            b = b.divide(d);
            c = c.multiply(d);
        }
        while (a.isEven()) {
            a = a.divide(roughLOB(a));
        }
        do {
            while (b.isEven()) {
                b = b.divide(roughLOB(b));
            }
            if (a.greater(b)) {
                t = b; b = a; a = t;
            }
            b = b.subtract(a);
        } while (!b.isZero());
        return c.isUnit() ? a : a.multiply(c);
    }
    function lcm(a, b) {
        a = parseValue(a).abs();
        b = parseValue(b).abs();
        return a.divide(gcd(a, b)).multiply(b);
    }
    function randBetween(a, b, rng) {
        a = parseValue(a);
        b = parseValue(b);
        var usedRNG = rng || Math.random;
        var low = min(a, b), high = max(a, b);
        var range = high.subtract(low).add(1);
        if (range.isSmall) return low.add(Math.floor(usedRNG() * range));
        var digits = toBase(range, BASE).value;
        var result = [], restricted = true;
        for (var i = 0; i < digits.length; i++) {
            var top = restricted ? digits[i] : BASE;
            var digit = truncate(usedRNG() * top);
            result.push(digit);
            if (digit < top) restricted = false;
        }
        return low.add(Integer.fromArray(result, BASE, false));
    }

    var parseBase = function (text, base, alphabet, caseSensitive) {
        alphabet = alphabet || DEFAULT_ALPHABET;
        text = String(text);
        if (!caseSensitive) {
            text = text.toLowerCase();
            alphabet = alphabet.toLowerCase();
        }
        var length = text.length;
        var i;
        var absBase = Math.abs(base);
        var alphabetValues = {};
        for (i = 0; i < alphabet.length; i++) {
            alphabetValues[alphabet[i]] = i;
        }
        for (i = 0; i < length; i++) {
            var c = text[i];
            if (c === "-") continue;
            if (c in alphabetValues) {
                if (alphabetValues[c] >= absBase) {
                    if (c === "1" && absBase === 1) continue;
                    throw new Error(c + " is not a valid digit in base " + base + ".");
                }
            }
        }
        base = parseValue(base);
        var digits = [];
        var isNegative = text[0] === "-";
        for (i = isNegative ? 1 : 0; i < text.length; i++) {
            var c = text[i];
            if (c in alphabetValues) digits.push(parseValue(alphabetValues[c]));
            else if (c === "<") {
                var start = i;
                do { i++; } while (text[i] !== ">" && i < text.length);
                digits.push(parseValue(text.slice(start + 1, i)));
            }
            else throw new Error(c + " is not a valid character");
        }
        return parseBaseFromArray(digits, base, isNegative);
    };

    function parseBaseFromArray(digits, base, isNegative) {
        var val = Integer[0], pow = Integer[1], i;
        for (i = digits.length - 1; i >= 0; i--) {
            val = val.add(digits[i].times(pow));
            pow = pow.times(base);
        }
        return isNegative ? val.negate() : val;
    }

    function stringify(digit, alphabet) {
        alphabet = alphabet || DEFAULT_ALPHABET;
        if (digit < alphabet.length) {
            return alphabet[digit];
        }
        return "<" + digit + ">";
    }

    function toBase(n, base) {
        base = bigInt(base);
        if (base.isZero()) {
            if (n.isZero()) return { value: [0], isNegative: false };
            throw new Error("Cannot convert nonzero numbers to base 0.");
        }
        if (base.equals(-1)) {
            if (n.isZero()) return { value: [0], isNegative: false };
            if (n.isNegative())
                return {
                    value: [].concat.apply([], Array.apply(null, Array(-n.toJSNumber()))
                        .map(Array.prototype.valueOf, [1, 0])
                    ),
                    isNegative: false
                };

            var arr = Array.apply(null, Array(n.toJSNumber() - 1))
                .map(Array.prototype.valueOf, [0, 1]);
            arr.unshift([1]);
            return {
                value: [].concat.apply([], arr),
                isNegative: false
            };
        }

        var neg = false;
        if (n.isNegative() && base.isPositive()) {
            neg = true;
            n = n.abs();
        }
        if (base.isUnit()) {
            if (n.isZero()) return { value: [0], isNegative: false };

            return {
                value: Array.apply(null, Array(n.toJSNumber()))
                    .map(Number.prototype.valueOf, 1),
                isNegative: neg
            };
        }
        var out = [];
        var left = n, divmod;
        while (left.isNegative() || left.compareAbs(base) >= 0) {
            divmod = left.divmod(base);
            left = divmod.quotient;
            var digit = divmod.remainder;
            if (digit.isNegative()) {
                digit = base.minus(digit).abs();
                left = left.next();
            }
            out.push(digit.toJSNumber());
        }
        out.push(left.toJSNumber());
        return { value: out.reverse(), isNegative: neg };
    }

    function toBaseString(n, base, alphabet) {
        var arr = toBase(n, base);
        return (arr.isNegative ? "-" : "") + arr.value.map(function (x) {
            return stringify(x, alphabet);
        }).join('');
    }

    BigInteger.prototype.toArray = function (radix) {
        return toBase(this, radix);
    };

    SmallInteger.prototype.toArray = function (radix) {
        return toBase(this, radix);
    };

    NativeBigInt.prototype.toArray = function (radix) {
        return toBase(this, radix);
    };

    BigInteger.prototype.toString = function (radix, alphabet) {
        if (radix === undefined$1) radix = 10;
        if (radix !== 10) return toBaseString(this, radix, alphabet);
        var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit;
        while (--l >= 0) {
            digit = String(v[l]);
            str += zeros.slice(digit.length) + digit;
        }
        var sign = this.sign ? "-" : "";
        return sign + str;
    };

    SmallInteger.prototype.toString = function (radix, alphabet) {
        if (radix === undefined$1) radix = 10;
        if (radix != 10) return toBaseString(this, radix, alphabet);
        return String(this.value);
    };

    NativeBigInt.prototype.toString = SmallInteger.prototype.toString;

    NativeBigInt.prototype.toJSON = BigInteger.prototype.toJSON = SmallInteger.prototype.toJSON = function () { return this.toString(); };

    BigInteger.prototype.valueOf = function () {
        return parseInt(this.toString(), 10);
    };
    BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;

    SmallInteger.prototype.valueOf = function () {
        return this.value;
    };
    SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
    NativeBigInt.prototype.valueOf = NativeBigInt.prototype.toJSNumber = function () {
        return parseInt(this.toString(), 10);
    };

    function parseStringValue(v) {
        if (isPrecise(+v)) {
            var x = +v;
            if (x === truncate(x))
                return supportsNativeBigInt ? new NativeBigInt(BigInt(x)) : new SmallInteger(x);
            throw new Error("Invalid integer: " + v);
        }
        var sign = v[0] === "-";
        if (sign) v = v.slice(1);
        var split = v.split(/e/i);
        if (split.length > 2) throw new Error("Invalid integer: " + split.join("e"));
        if (split.length === 2) {
            var exp = split[1];
            if (exp[0] === "+") exp = exp.slice(1);
            exp = +exp;
            if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent.");
            var text = split[0];
            var decimalPlace = text.indexOf(".");
            if (decimalPlace >= 0) {
                exp -= text.length - decimalPlace - 1;
                text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
            }
            if (exp < 0) throw new Error("Cannot include negative exponent part for integers");
            text += (new Array(exp + 1)).join("0");
            v = text;
        }
        var isValid = /^([0-9][0-9]*)$/.test(v);
        if (!isValid) throw new Error("Invalid integer: " + v);
        if (supportsNativeBigInt) {
            return new NativeBigInt(BigInt(sign ? "-" + v : v));
        }
        var r = [], max = v.length, l = LOG_BASE, min = max - l;
        while (max > 0) {
            r.push(+v.slice(min, max));
            min -= l;
            if (min < 0) min = 0;
            max -= l;
        }
        trim(r);
        return new BigInteger(r, sign);
    }

    function parseNumberValue(v) {
        if (supportsNativeBigInt) {
            return new NativeBigInt(BigInt(v));
        }
        if (isPrecise(v)) {
            if (v !== truncate(v)) throw new Error(v + " is not an integer.");
            return new SmallInteger(v);
        }
        return parseStringValue(v.toString());
    }

    function parseValue(v) {
        if (typeof v === "number") {
            return parseNumberValue(v);
        }
        if (typeof v === "string") {
            return parseStringValue(v);
        }
        if (typeof v === "bigint") {
            return new NativeBigInt(v);
        }
        return v;
    }
    // Pre-define numbers in range [-999,999]
    for (var i = 0; i < 1000; i++) {
        Integer[i] = parseValue(i);
        if (i > 0) Integer[-i] = parseValue(-i);
    }
    // Backwards compatibility
    Integer.one = Integer[1];
    Integer.zero = Integer[0];
    Integer.minusOne = Integer[-1];
    Integer.max = max;
    Integer.min = min;
    Integer.gcd = gcd;
    Integer.lcm = lcm;
    Integer.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger || x instanceof NativeBigInt; };
    Integer.randBetween = randBetween;

    Integer.fromArray = function (digits, base, isNegative) {
        return parseBaseFromArray(digits.map(parseValue), parseValue(base || 10), isNegative);
    };

    return Integer;
})();

// Node.js check
if ( module.hasOwnProperty("exports")) {
    module.exports = bigInt;
}
});

var sha256_1 = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });
// SHA-256 for JavaScript.
//
// Written in 2014-2016 by Dmitry Chestnykh.
// Public domain, no warranty.
//
// Functions (accept and return Uint8Arrays):
//
//   sha256(message) -> hash
//   sha256.hmac(key, message) -> mac
//
//  Classes:
//
//   new sha256.Hash()
exports.digestLength = 32;
exports.blockSize = 64;
// SHA-256 constants
const K = new Uint32Array([
    0x428a2f98,
    0x71374491,
    0xb5c0fbcf,
    0xe9b5dba5,
    0x3956c25b,
    0x59f111f1,
    0x923f82a4,
    0xab1c5ed5,
    0xd807aa98,
    0x12835b01,
    0x243185be,
    0x550c7dc3,
    0x72be5d74,
    0x80deb1fe,
    0x9bdc06a7,
    0xc19bf174,
    0xe49b69c1,
    0xefbe4786,
    0x0fc19dc6,
    0x240ca1cc,
    0x2de92c6f,
    0x4a7484aa,
    0x5cb0a9dc,
    0x76f988da,
    0x983e5152,
    0xa831c66d,
    0xb00327c8,
    0xbf597fc7,
    0xc6e00bf3,
    0xd5a79147,
    0x06ca6351,
    0x14292967,
    0x27b70a85,
    0x2e1b2138,
    0x4d2c6dfc,
    0x53380d13,
    0x650a7354,
    0x766a0abb,
    0x81c2c92e,
    0x92722c85,
    0xa2bfe8a1,
    0xa81a664b,
    0xc24b8b70,
    0xc76c51a3,
    0xd192e819,
    0xd6990624,
    0xf40e3585,
    0x106aa070,
    0x19a4c116,
    0x1e376c08,
    0x2748774c,
    0x34b0bcb5,
    0x391c0cb3,
    0x4ed8aa4a,
    0x5b9cca4f,
    0x682e6ff3,
    0x748f82ee,
    0x78a5636f,
    0x84c87814,
    0x8cc70208,
    0x90befffa,
    0xa4506ceb,
    0xbef9a3f7,
    0xc67178f2,
]);
function hashBlocks(w, v, p, pos, len) {
    let a, b, c, d, e, f, g, h, u, i, j, t1, t2;
    while (len >= 64) {
        a = v[0];
        b = v[1];
        c = v[2];
        d = v[3];
        e = v[4];
        f = v[5];
        g = v[6];
        h = v[7];
        for (i = 0; i < 16; i++) {
            j = pos + i * 4;
            w[i] =
                ((p[j] & 0xff) << 24) |
                    ((p[j + 1] & 0xff) << 16) |
                    ((p[j + 2] & 0xff) << 8) |
                    (p[j + 3] & 0xff);
        }
        for (i = 16; i < 64; i++) {
            u = w[i - 2];
            t1 =
                ((u >>> 17) | (u << (32 - 17))) ^
                    ((u >>> 19) | (u << (32 - 19))) ^
                    (u >>> 10);
            u = w[i - 15];
            t2 =
                ((u >>> 7) | (u << (32 - 7))) ^
                    ((u >>> 18) | (u << (32 - 18))) ^
                    (u >>> 3);
            w[i] = ((t1 + w[i - 7]) | 0) + ((t2 + w[i - 16]) | 0);
        }
        for (i = 0; i < 64; i++) {
            t1 =
                ((((((e >>> 6) | (e << (32 - 6))) ^
                    ((e >>> 11) | (e << (32 - 11))) ^
                    ((e >>> 25) | (e << (32 - 25)))) +
                    ((e & f) ^ (~e & g))) |
                    0) +
                    ((h + ((K[i] + w[i]) | 0)) | 0)) |
                    0;
            t2 =
                ((((a >>> 2) | (a << (32 - 2))) ^
                    ((a >>> 13) | (a << (32 - 13))) ^
                    ((a >>> 22) | (a << (32 - 22)))) +
                    ((a & b) ^ (a & c) ^ (b & c))) |
                    0;
            h = g;
            g = f;
            f = e;
            e = (d + t1) | 0;
            d = c;
            c = b;
            b = a;
            a = (t1 + t2) | 0;
        }
        v[0] += a;
        v[1] += b;
        v[2] += c;
        v[3] += d;
        v[4] += e;
        v[5] += f;
        v[6] += g;
        v[7] += h;
        pos += 64;
        len -= 64;
    }
    return pos;
}
// Hash implements SHA256 hash algorithm.
class HashSha256 {
    constructor() {
        this.digestLength = exports.digestLength;
        this.blockSize = exports.blockSize;
        // Note: Int32Array is used instead of Uint32Array for performance reasons.
        this.state = new Int32Array(8); // hash state
        this.temp = new Int32Array(64); // temporary state
        this.buffer = new Uint8Array(128); // buffer for data to hash
        this.bufferLength = 0; // number of bytes in buffer
        this.bytesHashed = 0; // number of total bytes hashed
        this.finished = false; // indicates whether the hash was finalized
        this.reset();
    }
    // Resets hash state making it possible
    // to re-use this instance to hash other data.
    reset() {
        this.state[0] = 0x6a09e667;
        this.state[1] = 0xbb67ae85;
        this.state[2] = 0x3c6ef372;
        this.state[3] = 0xa54ff53a;
        this.state[4] = 0x510e527f;
        this.state[5] = 0x9b05688c;
        this.state[6] = 0x1f83d9ab;
        this.state[7] = 0x5be0cd19;
        this.bufferLength = 0;
        this.bytesHashed = 0;
        this.finished = false;
        return this;
    }
    // Cleans internal buffers and re-initializes hash state.
    clean() {
        for (let i = 0; i < this.buffer.length; i++) {
            this.buffer[i] = 0;
        }
        for (let i = 0; i < this.temp.length; i++) {
            this.temp[i] = 0;
        }
        this.reset();
    }
    // Updates hash state with the given data.
    //
    // Optionally, length of the data can be specified to hash
    // fewer bytes than data.length.
    //
    // Throws error when trying to update already finalized hash:
    // instance must be reset to use it again.
    update(data, dataLength = data.length) {
        if (this.finished) {
            throw new Error("SHA256: can't update because hash was finished.");
        }
        let dataPos = 0;
        this.bytesHashed += dataLength;
        if (this.bufferLength > 0) {
            while (this.bufferLength < 64 && dataLength > 0) {
                this.buffer[this.bufferLength++] = data[dataPos++];
                dataLength--;
            }
            if (this.bufferLength === 64) {
                hashBlocks(this.temp, this.state, this.buffer, 0, 64);
                this.bufferLength = 0;
            }
        }
        if (dataLength >= 64) {
            dataPos = hashBlocks(this.temp, this.state, data, dataPos, dataLength);
            dataLength %= 64;
        }
        while (dataLength > 0) {
            this.buffer[this.bufferLength++] = data[dataPos++];
            dataLength--;
        }
        return this;
    }
    // Finalizes hash state and puts hash into out.
    //
    // If hash was already finalized, puts the same value.
    finish(out) {
        if (!this.finished) {
            const bytesHashed = this.bytesHashed;
            const left = this.bufferLength;
            const bitLenHi = (bytesHashed / 0x20000000) | 0;
            const bitLenLo = bytesHashed << 3;
            const padLength = bytesHashed % 64 < 56 ? 64 : 128;
            this.buffer[left] = 0x80;
            for (let i = left + 1; i < padLength - 8; i++) {
                this.buffer[i] = 0;
            }
            this.buffer[padLength - 8] = (bitLenHi >>> 24) & 0xff;
            this.buffer[padLength - 7] = (bitLenHi >>> 16) & 0xff;
            this.buffer[padLength - 6] = (bitLenHi >>> 8) & 0xff;
            this.buffer[padLength - 5] = (bitLenHi >>> 0) & 0xff;
            this.buffer[padLength - 4] = (bitLenLo >>> 24) & 0xff;
            this.buffer[padLength - 3] = (bitLenLo >>> 16) & 0xff;
            this.buffer[padLength - 2] = (bitLenLo >>> 8) & 0xff;
            this.buffer[padLength - 1] = (bitLenLo >>> 0) & 0xff;
            hashBlocks(this.temp, this.state, this.buffer, 0, padLength);
            this.finished = true;
        }
        for (let i = 0; i < 8; i++) {
            out[i * 4 + 0] = (this.state[i] >>> 24) & 0xff;
            out[i * 4 + 1] = (this.state[i] >>> 16) & 0xff;
            out[i * 4 + 2] = (this.state[i] >>> 8) & 0xff;
            out[i * 4 + 3] = (this.state[i] >>> 0) & 0xff;
        }
        return this;
    }
    // Returns the final hash digest.
    digest() {
        const out = new Uint8Array(this.digestLength);
        this.finish(out);
        return out;
    }
    // Internal function for use in HMAC for optimization.
    _saveState(out) {
        for (let i = 0; i < this.state.length; i++) {
            out[i] = this.state[i];
        }
    }
    // Internal function for use in HMAC for optimization.
    _restoreState(from, bytesHashed) {
        for (let i = 0; i < this.state.length; i++) {
            this.state[i] = from[i];
        }
        this.bytesHashed = bytesHashed;
        this.finished = false;
        this.bufferLength = 0;
    }
}
exports.HashSha256 = HashSha256;
// HMAC implements HMAC-SHA256 message authentication algorithm.
class HMAC {
    constructor(key) {
        this.inner = new HashSha256();
        this.outer = new HashSha256();
        this.blockSize = this.inner.blockSize;
        this.digestLength = this.inner.digestLength;
        const pad = new Uint8Array(this.blockSize);
        if (key.length > this.blockSize) {
            new HashSha256()
                .update(key)
                .finish(pad)
                .clean();
        }
        else {
            for (let i = 0; i < key.length; i++) {
                pad[i] = key[i];
            }
        }
        for (let i = 0; i < pad.length; i++) {
            pad[i] ^= 0x36;
        }
        this.inner.update(pad);
        for (let i = 0; i < pad.length; i++) {
            pad[i] ^= 0x36 ^ 0x5c;
        }
        this.outer.update(pad);
        this.istate = new Uint32Array(8);
        this.ostate = new Uint32Array(8);
        this.inner._saveState(this.istate);
        this.outer._saveState(this.ostate);
        for (let i = 0; i < pad.length; i++) {
            pad[i] = 0;
        }
    }
    // Returns HMAC state to the state initialized with key
    // to make it possible to run HMAC over the other data with the same
    // key without creating a new instance.
    reset() {
        this.inner._restoreState(this.istate, this.inner.blockSize);
        this.outer._restoreState(this.ostate, this.outer.blockSize);
        return this;
    }
    // Cleans HMAC state.
    clean() {
        for (let i = 0; i < this.istate.length; i++) {
            this.ostate[i] = this.istate[i] = 0;
        }
        this.inner.clean();
        this.outer.clean();
    }
    // Updates state with provided data.
    update(data) {
        this.inner.update(data);
        return this;
    }
    // Finalizes HMAC and puts the result in out.
    finish(out) {
        if (this.outer.finished) {
            this.outer.finish(out);
        }
        else {
            this.inner.finish(out);
            this.outer.update(out, this.digestLength).finish(out);
        }
        return this;
    }
    // Returns message authentication code.
    digest() {
        const out = new Uint8Array(this.digestLength);
        this.finish(out);
        return out;
    }
}
exports.HMAC = HMAC;
// Returns SHA256 hash of data.
function sha256(data) {
    const h = new HashSha256().update(data);
    const digest = h.digest();
    h.clean();
    return digest;
}
exports.sha256 = sha256;
// Returns HMAC-SHA256 of data under the key.
function hmacSha256(key, data) {
    const h = new HMAC(key).update(data);
    const digest = h.digest();
    h.clean();
    return digest;
}
exports.hmacSha256 = hmacSha256;

});

unwrapExports(sha256_1);
var sha256_2 = sha256_1.digestLength;
var sha256_3 = sha256_1.blockSize;
var sha256_4 = sha256_1.HashSha256;
var sha256_5 = sha256_1.HMAC;
var sha256_6 = sha256_1.sha256;
var sha256_7 = sha256_1.hmacSha256;

var kdf_1 = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });


function sha512(data) {
    return naclFast.hash(data);
}
exports.sha512 = sha512;
function hmac(digest, blockSize, key, message) {
    if (key.byteLength > blockSize) {
        key = digest(key);
    }
    if (key.byteLength < blockSize) {
        const k = key;
        key = new Uint8Array(blockSize);
        key.set(k, 0);
    }
    const okp = new Uint8Array(blockSize);
    const ikp = new Uint8Array(blockSize);
    for (let i = 0; i < blockSize; i++) {
        ikp[i] = key[i] ^ 0x36;
        okp[i] = key[i] ^ 0x5c;
    }
    const b1 = new Uint8Array(blockSize + message.byteLength);
    b1.set(ikp, 0);
    b1.set(message, blockSize);
    const h0 = digest(b1);
    const b2 = new Uint8Array(blockSize + h0.length);
    b2.set(okp, 0);
    b2.set(h0, blockSize);
    return digest(b2);
}
exports.hmac = hmac;
function hmacSha512(key, message) {
    return hmac(sha512, 128, key, message);
}
exports.hmacSha512 = hmacSha512;
function hmacSha256(key, message) {
    return hmac(sha256_1.sha256, 64, key, message);
}
exports.hmacSha256 = hmacSha256;
function kdf(outputLength, ikm, salt, info) {
    // extract
    const prk = hmacSha512(salt, ikm);
    // expand
    const N = Math.ceil(outputLength / 32);
    const output = new Uint8Array(N * 32);
    for (let i = 0; i < N; i++) {
        let buf;
        if (i == 0) {
            buf = new Uint8Array(info.byteLength + 1);
            buf.set(info, 0);
        }
        else {
            buf = new Uint8Array(info.byteLength + 1 + 32);
            for (let j = 0; j < 32; j++) {
                buf[j] = output[(i - 1) * 32 + j];
            }
            buf.set(info, 32);
        }
        buf[buf.length - 1] = i + 1;
        const chunk = hmacSha256(prk, buf);
        output.set(chunk, i * 32);
    }
    return output.slice(0, outputLength);
}
exports.kdf = kdf;

});

unwrapExports(kdf_1);
var kdf_2 = kdf_1.sha512;
var kdf_3 = kdf_1.hmac;
var kdf_4 = kdf_1.hmacSha512;
var kdf_5 = kdf_1.hmacSha256;
var kdf_6 = kdf_1.kdf;

var talerCrypto = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Native implementation of GNU Taler crypto.
 */

const big_integer_1 = __importDefault(BigInteger);

function getRandomBytes(n) {
    return naclFast.randomBytes(n);
}
exports.getRandomBytes = getRandomBytes;
const encTable = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
class EncodingError extends Error {
    constructor() {
        super("Encoding error");
        Object.setPrototypeOf(this, EncodingError.prototype);
    }
}
function getValue(chr) {
    let a = chr;
    switch (chr) {
        case "O":
        case "o":
            a = "0;";
            break;
        case "i":
        case "I":
        case "l":
        case "L":
            a = "1";
            break;
        case "u":
        case "U":
            a = "V";
    }
    if (a >= "0" && a <= "9") {
        return a.charCodeAt(0) - "0".charCodeAt(0);
    }
    if (a >= "a" && a <= "z")
        a = a.toUpperCase();
    let dec = 0;
    if (a >= "A" && a <= "Z") {
        if ("I" < a)
            dec++;
        if ("L" < a)
            dec++;
        if ("O" < a)
            dec++;
        if ("U" < a)
            dec++;
        return a.charCodeAt(0) - "A".charCodeAt(0) + 10 - dec;
    }
    throw new EncodingError();
}
function encodeCrock(data) {
    const dataBytes = new Uint8Array(data);
    let sb = "";
    const size = data.byteLength;
    let bitBuf = 0;
    let numBits = 0;
    let pos = 0;
    while (pos < size || numBits > 0) {
        if (pos < size && numBits < 5) {
            const d = dataBytes[pos++];
            bitBuf = (bitBuf << 8) | d;
            numBits += 8;
        }
        if (numBits < 5) {
            // zero-padding
            bitBuf = bitBuf << (5 - numBits);
            numBits = 5;
        }
        const v = (bitBuf >>> (numBits - 5)) & 31;
        sb += encTable[v];
        numBits -= 5;
    }
    return sb;
}
exports.encodeCrock = encodeCrock;
function decodeCrock(encoded) {
    const size = encoded.length;
    let bitpos = 0;
    let bitbuf = 0;
    let readPosition = 0;
    const outLen = Math.floor((size * 5) / 8);
    const out = new Uint8Array(outLen);
    let outPos = 0;
    while (readPosition < size || bitpos > 0) {
        if (readPosition < size) {
            const v = getValue(encoded[readPosition++]);
            bitbuf = (bitbuf << 5) | v;
            bitpos += 5;
        }
        while (bitpos >= 8) {
            const d = (bitbuf >>> (bitpos - 8)) & 0xff;
            out[outPos++] = d;
            bitpos -= 8;
        }
        if (readPosition == size && bitpos > 0) {
            bitbuf = (bitbuf << (8 - bitpos)) & 0xff;
            bitpos = bitbuf == 0 ? 0 : 8;
        }
    }
    return out;
}
exports.decodeCrock = decodeCrock;
function eddsaGetPublic(eddsaPriv) {
    const pair = naclFast.sign_keyPair_fromSeed(eddsaPriv);
    return pair.publicKey;
}
exports.eddsaGetPublic = eddsaGetPublic;
function ecdheGetPublic(ecdhePriv) {
    return naclFast.scalarMult_base(ecdhePriv);
}
exports.ecdheGetPublic = ecdheGetPublic;
function keyExchangeEddsaEcdhe(eddsaPriv, ecdhePub) {
    const ph = naclFast.hash(eddsaPriv);
    const a = new Uint8Array(32);
    for (let i = 0; i < 32; i++) {
        a[i] = ph[i];
    }
    const x = naclFast.scalarMult(a, ecdhePub);
    return naclFast.hash(x);
}
exports.keyExchangeEddsaEcdhe = keyExchangeEddsaEcdhe;
function keyExchangeEcdheEddsa(ecdhePriv, eddsaPub) {
    const curve25519Pub = naclFast.sign_ed25519_pk_to_curve25519(eddsaPub);
    const x = naclFast.scalarMult(ecdhePriv, curve25519Pub);
    return naclFast.hash(x);
}
exports.keyExchangeEcdheEddsa = keyExchangeEcdheEddsa;
/**
 * KDF modulo a big integer.
 */
function kdfMod(n, ikm, salt, info) {
    const nbits = n.bitLength().toJSNumber();
    const buflen = Math.floor((nbits - 1) / 8 + 1);
    const mask = (1 << (8 - (buflen * 8 - nbits))) - 1;
    let counter = 0;
    while (true) {
        const ctx = new Uint8Array(info.byteLength + 2);
        ctx.set(info, 0);
        ctx[ctx.length - 2] = (counter >>> 8) & 0xff;
        ctx[ctx.length - 1] = counter & 0xff;
        const buf = kdf_1.kdf(buflen, ikm, salt, ctx);
        const arr = Array.from(buf);
        arr[0] = arr[0] & mask;
        const r = big_integer_1.default.fromArray(arr, 256, false);
        if (r.lt(n)) {
            return r;
        }
        counter++;
    }
}
function stringToBytes(s) {
    const te = new TextEncoder();
    return te.encode(s);
}
exports.stringToBytes = stringToBytes;
function loadBigInt(arr) {
    return big_integer_1.default.fromArray(Array.from(arr), 256, false);
}
function rsaBlindingKeyDerive(rsaPub, bks) {
    const salt = stringToBytes("Blinding KDF extrator HMAC key");
    const info = stringToBytes("Blinding KDF");
    return kdfMod(rsaPub.N, bks, salt, info);
}
/*
 * Test for malicious RSA key.
 *
 * Assuming n is an RSA modulous and r is generated using a call to
 * GNUNET_CRYPTO_kdf_mod_mpi, if gcd(r,n) != 1 then n must be a
 * malicious RSA key designed to deanomize the user.
 *
 * @param r KDF result
 * @param n RSA modulus of the public key
 */
function rsaGcdValidate(r, n) {
    const t = big_integer_1.default.gcd(r, n);
    if (!t.equals(big_integer_1.default.one)) {
        throw Error("malicious RSA public key");
    }
}
function rsaFullDomainHash(hm, rsaPub) {
    const info = stringToBytes("RSA-FDA FTpsW!");
    const salt = rsaPubEncode(rsaPub);
    const r = kdfMod(rsaPub.N, hm, salt, info);
    rsaGcdValidate(r, rsaPub.N);
    return r;
}
function rsaPubDecode(rsaPub) {
    const modulusLength = (rsaPub[0] << 8) | rsaPub[1];
    const exponentLength = (rsaPub[2] << 8) | rsaPub[3];
    if (4 + exponentLength + modulusLength != rsaPub.length) {
        throw Error("invalid RSA public key (format wrong)");
    }
    const modulus = rsaPub.slice(4, 4 + modulusLength);
    const exponent = rsaPub.slice(4 + modulusLength, 4 + modulusLength + exponentLength);
    const res = {
        N: loadBigInt(modulus),
        e: loadBigInt(exponent),
    };
    return res;
}
function rsaPubEncode(rsaPub) {
    const mb = rsaPub.N.toArray(256).value;
    const eb = rsaPub.e.toArray(256).value;
    const out = new Uint8Array(4 + mb.length + eb.length);
    out[0] = (mb.length >>> 8) & 0xff;
    out[1] = mb.length & 0xff;
    out[2] = (eb.length >>> 8) & 0xff;
    out[3] = eb.length & 0xff;
    out.set(mb, 4);
    out.set(eb, 4 + mb.length);
    return out;
}
function rsaBlind(hm, bks, rsaPubEnc) {
    const rsaPub = rsaPubDecode(rsaPubEnc);
    const data = rsaFullDomainHash(hm, rsaPub);
    const r = rsaBlindingKeyDerive(rsaPub, bks);
    const r_e = r.modPow(rsaPub.e, rsaPub.N);
    const bm = r_e.multiply(data).mod(rsaPub.N);
    return new Uint8Array(bm.toArray(256).value);
}
exports.rsaBlind = rsaBlind;
function rsaUnblind(sig, rsaPubEnc, bks) {
    const rsaPub = rsaPubDecode(rsaPubEnc);
    const blinded_s = loadBigInt(sig);
    const r = rsaBlindingKeyDerive(rsaPub, bks);
    const r_inv = r.modInv(rsaPub.N);
    const s = blinded_s.multiply(r_inv).mod(rsaPub.N);
    return new Uint8Array(s.toArray(256).value);
}
exports.rsaUnblind = rsaUnblind;
function rsaVerify(hm, rsaSig, rsaPubEnc) {
    const rsaPub = rsaPubDecode(rsaPubEnc);
    const d = rsaFullDomainHash(hm, rsaPub);
    const sig = loadBigInt(rsaSig);
    const sig_e = sig.modPow(rsaPub.e, rsaPub.N);
    return sig_e.equals(d);
}
exports.rsaVerify = rsaVerify;
function createEddsaKeyPair() {
    const eddsaPriv = naclFast.randomBytes(32);
    const eddsaPub = eddsaGetPublic(eddsaPriv);
    return { eddsaPriv, eddsaPub };
}
exports.createEddsaKeyPair = createEddsaKeyPair;
function createEcdheKeyPair() {
    const ecdhePriv = naclFast.randomBytes(32);
    const ecdhePub = ecdheGetPublic(ecdhePriv);
    return { ecdhePriv, ecdhePub };
}
exports.createEcdheKeyPair = createEcdheKeyPair;
function createBlindingKeySecret() {
    return naclFast.randomBytes(32);
}
exports.createBlindingKeySecret = createBlindingKeySecret;
function hash(d) {
    return naclFast.hash(d);
}
exports.hash = hash;
function eddsaSign(msg, eddsaPriv) {
    const pair = naclFast.sign_keyPair_fromSeed(eddsaPriv);
    return naclFast.sign_detached(msg, pair.secretKey);
}
exports.eddsaSign = eddsaSign;
function eddsaVerify(msg, sig, eddsaPub) {
    return naclFast.sign_detached_verify(msg, sig, eddsaPub);
}
exports.eddsaVerify = eddsaVerify;
function createHashContext() {
    return new naclFast.HashState();
}
exports.createHashContext = createHashContext;
function setupRefreshPlanchet(secretSeed, coinNumber) {
    const info = stringToBytes("taler-coin-derivation");
    const saltArrBuf = new ArrayBuffer(4);
    const salt = new Uint8Array(saltArrBuf);
    const saltDataView = new DataView(saltArrBuf);
    saltDataView.setUint32(0, coinNumber);
    const out = kdf_1.kdf(64, secretSeed, salt, info);
    const coinPriv = out.slice(0, 32);
    const bks = out.slice(32, 64);
    return {
        bks,
        coinPriv,
        coinPub: eddsaGetPublic(coinPriv),
    };
}
exports.setupRefreshPlanchet = setupRefreshPlanchet;

});

unwrapExports(talerCrypto);
var talerCrypto_1 = talerCrypto.getRandomBytes;
var talerCrypto_2 = talerCrypto.encodeCrock;
var talerCrypto_3 = talerCrypto.decodeCrock;
var talerCrypto_4 = talerCrypto.eddsaGetPublic;
var talerCrypto_5 = talerCrypto.ecdheGetPublic;
var talerCrypto_6 = talerCrypto.keyExchangeEddsaEcdhe;
var talerCrypto_7 = talerCrypto.keyExchangeEcdheEddsa;
var talerCrypto_8 = talerCrypto.stringToBytes;
var talerCrypto_9 = talerCrypto.rsaBlind;
var talerCrypto_10 = talerCrypto.rsaUnblind;
var talerCrypto_11 = talerCrypto.rsaVerify;
var talerCrypto_12 = talerCrypto.createEddsaKeyPair;
var talerCrypto_13 = talerCrypto.createEcdheKeyPair;
var talerCrypto_14 = talerCrypto.createBlindingKeySecret;
var talerCrypto_15 = talerCrypto.hash;
var talerCrypto_16 = talerCrypto.eddsaSign;
var talerCrypto_17 = talerCrypto.eddsaVerify;
var talerCrypto_18 = talerCrypto.createHashContext;
var talerCrypto_19 = talerCrypto.setupRefreshPlanchet;

var reserves = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });





const Amounts = __importStar(amounts);







const logger = new logging.Logger("reserves.ts");
/**
 * Create a reserve, but do not flag it as confirmed yet.
 *
 * Adds the corresponding exchange as a trusted exchange if it is neither
 * audited nor trusted already.
 */
function createReserve(ws, req) {
    return __awaiter(this, void 0, void 0, function* () {
        const keypair = yield ws.cryptoApi.createEddsaKeypair();
        const now = walletTypes.getTimestampNow();
        const canonExchange = helpers.canonicalizeBaseUrl(req.exchange);
        let reserveStatus;
        if (req.bankWithdrawStatusUrl) {
            reserveStatus = dbTypes.ReserveRecordStatus.REGISTERING_BANK;
        }
        else {
            reserveStatus = dbTypes.ReserveRecordStatus.UNCONFIRMED;
        }
        const currency = req.amount.currency;
        const reserveRecord = {
            created: now,
            withdrawAllocatedAmount: Amounts.getZero(currency),
            withdrawCompletedAmount: Amounts.getZero(currency),
            withdrawRemainingAmount: Amounts.getZero(currency),
            exchangeBaseUrl: canonExchange,
            hasPayback: false,
            initiallyRequestedAmount: req.amount,
            reservePriv: keypair.priv,
            reservePub: keypair.pub,
            senderWire: req.senderWire,
            timestampConfirmed: undefined,
            timestampReserveInfoPosted: undefined,
            bankWithdrawStatusUrl: req.bankWithdrawStatusUrl,
            exchangeWire: req.exchangeWire,
            reserveStatus,
            lastStatusQuery: undefined,
        };
        const senderWire = req.senderWire;
        if (senderWire) {
            const rec = {
                paytoUri: senderWire,
            };
            yield query.oneShotPut(ws.db, dbTypes.Stores.senderWires, rec);
        }
        const exchangeInfo = yield exchanges.updateExchangeFromUrl(ws, req.exchange);
        const exchangeDetails = exchangeInfo.details;
        if (!exchangeDetails) {
            console.log(exchangeDetails);
            throw Error("exchange not updated");
        }
        const { isAudited, isTrusted } = yield exchanges.getExchangeTrust(ws, exchangeInfo);
        let currencyRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.currencies, exchangeDetails.currency);
        if (!currencyRecord) {
            currencyRecord = {
                auditors: [],
                exchanges: [],
                fractionalDigits: 2,
                name: exchangeDetails.currency,
            };
        }
        if (!isAudited && !isTrusted) {
            currencyRecord.exchanges.push({
                baseUrl: req.exchange,
                exchangePub: exchangeDetails.masterPublicKey,
            });
        }
        const cr = currencyRecord;
        const resp = yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.currencies, dbTypes.Stores.reserves, dbTypes.Stores.bankWithdrawUris], (tx) => __awaiter(this, void 0, void 0, function* () {
            // Check if we have already created a reserve for that bankWithdrawStatusUrl
            if (reserveRecord.bankWithdrawStatusUrl) {
                const bwi = yield tx.get(dbTypes.Stores.bankWithdrawUris, reserveRecord.bankWithdrawStatusUrl);
                if (bwi) {
                    const otherReserve = yield tx.get(dbTypes.Stores.reserves, bwi.reservePub);
                    if (otherReserve) {
                        logger.trace("returning existing reserve for bankWithdrawStatusUri");
                        return {
                            exchange: otherReserve.exchangeBaseUrl,
                            reservePub: otherReserve.reservePub,
                        };
                    }
                }
                yield tx.put(dbTypes.Stores.bankWithdrawUris, {
                    reservePub: reserveRecord.reservePub,
                    talerWithdrawUri: reserveRecord.bankWithdrawStatusUrl,
                });
            }
            yield tx.put(dbTypes.Stores.currencies, cr);
            yield tx.put(dbTypes.Stores.reserves, reserveRecord);
            const r = {
                exchange: canonExchange,
                reservePub: keypair.pub,
            };
            return r;
        }));
        // Asynchronously process the reserve, but return
        // to the caller already.
        processReserve(ws, resp.reservePub).catch(e => {
            console.error("Processing reserve failed:", e);
        });
        return resp;
    });
}
exports.createReserve = createReserve;
/**
 * First fetch information requred to withdraw from the reserve,
 * then deplete the reserve, withdrawing coins until it is empty.
 *
 * The returned promise resolves once the reserve is set to the
 * state DORMANT.
 */
function processReserve(ws, reservePub) {
    return __awaiter(this, void 0, void 0, function* () {
        const p = ws.memoProcessReserve.find(reservePub);
        if (p) {
            return p;
        }
        else {
            return ws.memoProcessReserve.put(reservePub, processReserveImpl(ws, reservePub));
        }
    });
}
exports.processReserve = processReserve;
function registerReserveWithBank(ws, reservePub) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
        let reserve = yield query.oneShotGet(ws.db, dbTypes.Stores.reserves, reservePub);
        switch ((_a = reserve) === null || _a === void 0 ? void 0 : _a.reserveStatus) {
            case dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK:
            case dbTypes.ReserveRecordStatus.REGISTERING_BANK:
                break;
            default:
                return;
        }
        const bankStatusUrl = reserve.bankWithdrawStatusUrl;
        if (!bankStatusUrl) {
            return;
        }
        console.log("making selection");
        if (reserve.timestampReserveInfoPosted) {
            throw Error("bank claims that reserve info selection is not done");
        }
        const bankResp = yield ws.http.postJson(bankStatusUrl, {
            reserve_pub: reservePub,
            selected_exchange: reserve.exchangeWire,
        });
        console.log("got response", bankResp);
        yield query.oneShotMutate(ws.db, dbTypes.Stores.reserves, reservePub, r => {
            switch (r.reserveStatus) {
                case dbTypes.ReserveRecordStatus.REGISTERING_BANK:
                case dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK:
                    break;
                default:
                    return;
            }
            r.timestampReserveInfoPosted = walletTypes.getTimestampNow();
            r.reserveStatus = dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK;
            return r;
        });
        return processReserveBankStatus(ws, reservePub);
    });
}
function processReserveBankStatus(ws, reservePub) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
        let reserve = yield query.oneShotGet(ws.db, dbTypes.Stores.reserves, reservePub);
        switch ((_a = reserve) === null || _a === void 0 ? void 0 : _a.reserveStatus) {
            case dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK:
            case dbTypes.ReserveRecordStatus.REGISTERING_BANK:
                break;
            default:
                return;
        }
        const bankStatusUrl = reserve.bankWithdrawStatusUrl;
        if (!bankStatusUrl) {
            return;
        }
        let status;
        try {
            const statusResp = yield ws.http.get(bankStatusUrl);
            status = talerTypes.WithdrawOperationStatusResponse.checked(statusResp.responseJson);
        }
        catch (e) {
            throw e;
        }
        if (status.selection_done) {
            if (reserve.reserveStatus === dbTypes.ReserveRecordStatus.REGISTERING_BANK) {
                yield registerReserveWithBank(ws, reservePub);
                return yield processReserveBankStatus(ws, reservePub);
            }
        }
        else {
            yield registerReserveWithBank(ws, reservePub);
            return yield processReserveBankStatus(ws, reservePub);
        }
        if (status.transfer_done) {
            yield query.oneShotMutate(ws.db, dbTypes.Stores.reserves, reservePub, r => {
                switch (r.reserveStatus) {
                    case dbTypes.ReserveRecordStatus.REGISTERING_BANK:
                    case dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK:
                        break;
                    default:
                        return;
                }
                const now = walletTypes.getTimestampNow();
                r.timestampConfirmed = now;
                r.reserveStatus = dbTypes.ReserveRecordStatus.QUERYING_STATUS;
                return r;
            });
            yield processReserveImpl(ws, reservePub);
        }
        else {
            yield query.oneShotMutate(ws.db, dbTypes.Stores.reserves, reservePub, r => {
                switch (r.reserveStatus) {
                    case dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK:
                        break;
                    default:
                        return;
                }
                r.bankWithdrawConfirmUrl = status.confirm_transfer_url;
                return r;
            });
        }
    });
}
exports.processReserveBankStatus = processReserveBankStatus;
function setReserveError(ws, reservePub, err) {
    return __awaiter(this, void 0, void 0, function* () {
        const mut = (reserve) => {
            reserve.lastError = err;
            return reserve;
        };
        yield query.oneShotMutate(ws.db, dbTypes.Stores.reserves, reservePub, mut);
    });
}
/**
 * Update the information about a reserve that is stored in the wallet
 * by quering the reserve's exchange.
 */
function updateReserve(ws, reservePub) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
        const reserve = yield query.oneShotGet(ws.db, dbTypes.Stores.reserves, reservePub);
        if (!reserve) {
            throw Error("reserve not in db");
        }
        if (reserve.timestampConfirmed === undefined) {
            throw Error("reserve not confirmed yet");
        }
        if (reserve.reserveStatus !== dbTypes.ReserveRecordStatus.QUERYING_STATUS) {
            return;
        }
        const reqUrl = new URL("reserve/status", reserve.exchangeBaseUrl);
        reqUrl.searchParams.set("reserve_pub", reservePub);
        let resp;
        try {
            resp = yield ws.http.get(reqUrl.href);
        }
        catch (e) {
            if (((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
                const m = "The exchange does not know about this reserve (yet).";
                yield setReserveError(ws, reservePub, {
                    type: "waiting",
                    details: {},
                    message: "The exchange does not know about this reserve (yet).",
                });
                throw new wallet.OperationFailedAndReportedError(m);
            }
            else {
                const m = e.message;
                yield setReserveError(ws, reservePub, {
                    type: "network",
                    details: {},
                    message: m,
                });
                throw new wallet.OperationFailedAndReportedError(m);
            }
        }
        const reserveInfo = talerTypes.ReserveStatus.checked(resp.responseJson);
        const balance = Amounts.parseOrThrow(reserveInfo.balance);
        yield query.oneShotMutate(ws.db, dbTypes.Stores.reserves, reserve.reservePub, r => {
            if (r.reserveStatus !== dbTypes.ReserveRecordStatus.QUERYING_STATUS) {
                return;
            }
            // FIXME: check / compare history!
            if (!r.lastStatusQuery) {
                // FIXME: check if this matches initial expectations
                r.withdrawRemainingAmount = balance;
            }
            else {
                const expectedBalance = Amounts.sub(r.withdrawAllocatedAmount, r.withdrawCompletedAmount);
                const cmp = Amounts.cmp(balance, expectedBalance.amount);
                if (cmp == 0) {
                    // Nothing changed.
                    return;
                }
                if (cmp > 0) {
                    const extra = Amounts.sub(balance, expectedBalance.amount).amount;
                    r.withdrawRemainingAmount = Amounts.add(r.withdrawRemainingAmount, extra).amount;
                }
            }
            r.lastStatusQuery = walletTypes.getTimestampNow();
            r.reserveStatus = dbTypes.ReserveRecordStatus.WITHDRAWING;
            return r;
        });
        ws.notifier.notify();
    });
}
function processReserveImpl(ws, reservePub) {
    return __awaiter(this, void 0, void 0, function* () {
        const reserve = yield query.oneShotGet(ws.db, dbTypes.Stores.reserves, reservePub);
        if (!reserve) {
            console.log("not processing reserve: reserve does not exist");
            return;
        }
        logger.trace(`Processing reserve ${reservePub} with status ${reserve.reserveStatus}`);
        switch (reserve.reserveStatus) {
            case dbTypes.ReserveRecordStatus.UNCONFIRMED:
                // nothing to do
                break;
            case dbTypes.ReserveRecordStatus.REGISTERING_BANK:
                yield processReserveBankStatus(ws, reservePub);
                return processReserveImpl(ws, reservePub);
            case dbTypes.ReserveRecordStatus.QUERYING_STATUS:
                yield updateReserve(ws, reservePub);
                return processReserveImpl(ws, reservePub);
            case dbTypes.ReserveRecordStatus.WITHDRAWING:
                yield depleteReserve(ws, reservePub);
                break;
            case dbTypes.ReserveRecordStatus.DORMANT:
                // nothing to do
                break;
            case dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK:
                yield processReserveBankStatus(ws, reservePub);
                break;
            default:
                console.warn("unknown reserve record status:", reserve.reserveStatus);
                assertUnreachable_1.assertUnreachable(reserve.reserveStatus);
                break;
        }
    });
}
function confirmReserve(ws, req) {
    return __awaiter(this, void 0, void 0, function* () {
        const now = walletTypes.getTimestampNow();
        yield query.oneShotMutate(ws.db, dbTypes.Stores.reserves, req.reservePub, reserve => {
            if (reserve.reserveStatus !== dbTypes.ReserveRecordStatus.UNCONFIRMED) {
                return;
            }
            reserve.timestampConfirmed = now;
            reserve.reserveStatus = dbTypes.ReserveRecordStatus.QUERYING_STATUS;
            return reserve;
        });
        ws.notifier.notify();
        processReserve(ws, req.reservePub).catch(e => {
            console.log("processing reserve failed:", e);
        });
    });
}
exports.confirmReserve = confirmReserve;
/**
 * Withdraw coins from a reserve until it is empty.
 *
 * When finished, marks the reserve as depleted by setting
 * the depleted timestamp.
 */
function depleteReserve(ws, reservePub) {
    return __awaiter(this, void 0, void 0, function* () {
        const reserve = yield query.oneShotGet(ws.db, dbTypes.Stores.reserves, reservePub);
        if (!reserve) {
            return;
        }
        if (reserve.reserveStatus !== dbTypes.ReserveRecordStatus.WITHDRAWING) {
            return;
        }
        logger.trace(`depleting reserve ${reservePub}`);
        const withdrawAmount = reserve.withdrawRemainingAmount;
        logger.trace(`getting denom list`);
        const denomsForWithdraw = yield withdraw.getVerifiedWithdrawDenomList(ws, reserve.exchangeBaseUrl, withdrawAmount);
        logger.trace(`got denom list`);
        if (denomsForWithdraw.length === 0) {
            const m = `Unable to withdraw from reserve, no denominations are available to withdraw.`;
            yield setReserveError(ws, reserve.reservePub, {
                type: "internal",
                message: m,
                details: {},
            });
            console.log(m);
            throw new wallet.OperationFailedAndReportedError(m);
        }
        logger.trace("selected denominations");
        const withdrawalSessionId = talerCrypto.encodeCrock(naclFast.randomBytes(32));
        const totalCoinValue = Amounts.sum(denomsForWithdraw.map(x => x.value)).amount;
        const withdrawalRecord = {
            withdrawSessionId: withdrawalSessionId,
            exchangeBaseUrl: reserve.exchangeBaseUrl,
            source: {
                type: "reserve",
                reservePub: reserve.reservePub,
            },
            rawWithdrawalAmount: withdrawAmount,
            startTimestamp: walletTypes.getTimestampNow(),
            denoms: denomsForWithdraw.map(x => x.denomPub),
            withdrawn: denomsForWithdraw.map(x => false),
            planchets: denomsForWithdraw.map(x => undefined),
            totalCoinValue,
        };
        const totalCoinWithdrawFee = Amounts.sum(denomsForWithdraw.map(x => x.feeWithdraw)).amount;
        const totalWithdrawAmount = Amounts.add(totalCoinValue, totalCoinWithdrawFee)
            .amount;
        function mutateReserve(r) {
            const remaining = Amounts.sub(r.withdrawRemainingAmount, totalWithdrawAmount);
            if (remaining.saturated) {
                console.error("can't create planchets, saturated");
                throw query.TransactionAbort;
            }
            const allocated = Amounts.add(r.withdrawAllocatedAmount, totalWithdrawAmount);
            if (allocated.saturated) {
                console.error("can't create planchets, saturated");
                throw query.TransactionAbort;
            }
            r.withdrawRemainingAmount = remaining.amount;
            r.withdrawAllocatedAmount = allocated.amount;
            r.reserveStatus = dbTypes.ReserveRecordStatus.DORMANT;
            return r;
        }
        const success = yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.withdrawalSession, dbTypes.Stores.reserves], (tx) => __awaiter(this, void 0, void 0, function* () {
            const myReserve = yield tx.get(dbTypes.Stores.reserves, reservePub);
            if (!myReserve) {
                return false;
            }
            if (myReserve.reserveStatus !== dbTypes.ReserveRecordStatus.WITHDRAWING) {
                return false;
            }
            yield tx.mutate(dbTypes.Stores.reserves, reserve.reservePub, mutateReserve);
            yield tx.put(dbTypes.Stores.withdrawalSession, withdrawalRecord);
            return true;
        }));
        if (success) {
            console.log("processing new withdraw session");
            yield withdraw.processWithdrawSession(ws, withdrawalSessionId);
        }
        else {
            console.trace("withdraw session already existed");
        }
    });
}

});

unwrapExports(reserves);
var reserves_1 = reserves.createReserve;
var reserves_2 = reserves.processReserve;
var reserves_3 = reserves.processReserveBankStatus;
var reserves_4 = reserves.confirmReserve;

var libtoolVersion = createCommonjsModule(function (module, exports) {
/*
 This file is part of TALER
 (C) 2017 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Compare two libtool-style version strings.
 */
function compare(me, other) {
    const meVer = parseVersion(me);
    const otherVer = parseVersion(other);
    if (!(meVer && otherVer)) {
        return undefined;
    }
    const compatible = (meVer.current - meVer.age <= otherVer.current &&
        meVer.current >= (otherVer.current - otherVer.age));
    const currentCmp = Math.sign(meVer.current - otherVer.current);
    return { compatible, currentCmp };
}
exports.compare = compare;
function parseVersion(v) {
    const [currentStr, revisionStr, ageStr, ...rest] = v.split(":");
    if (rest.length !== 0) {
        return undefined;
    }
    const current = Number.parseInt(currentStr);
    const revision = Number.parseInt(revisionStr);
    const age = Number.parseInt(ageStr);
    if (Number.isNaN(current)) {
        return undefined;
    }
    if (Number.isNaN(revision)) {
        return undefined;
    }
    if (Number.isNaN(age)) {
        return undefined;
    }
    return { current, revision, age };
}

});

unwrapExports(libtoolVersion);
var libtoolVersion_1 = libtoolVersion.compare;

var withdraw = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });

const Amounts = __importStar(amounts);








const LibtoolVersion = __importStar(libtoolVersion);
const logger = new logging.Logger("withdraw.ts");
function isWithdrawableDenom(d) {
    const now = walletTypes.getTimestampNow();
    const started = now.t_ms >= d.stampStart.t_ms;
    const stillOkay = d.stampExpireWithdraw.t_ms + 60 * 1000 > now.t_ms;
    return started && stillOkay;
}
/**
 * Get a list of denominations (with repetitions possible)
 * whose total value is as close as possible to the available
 * amount, but never larger.
 */
function getWithdrawDenomList(amountAvailable, denoms) {
    let remaining = Amounts.copy(amountAvailable);
    const ds = [];
    denoms = denoms.filter(isWithdrawableDenom);
    denoms.sort((d1, d2) => Amounts.cmp(d2.value, d1.value));
    // This is an arbitrary number of coins
    // we can withdraw in one go.  It's not clear if this limit
    // is useful ...
    for (let i = 0; i < 1000; i++) {
        let found = false;
        for (const d of denoms) {
            const cost = Amounts.add(d.value, d.feeWithdraw).amount;
            if (Amounts.cmp(remaining, cost) < 0) {
                continue;
            }
            found = true;
            remaining = Amounts.sub(remaining, cost).amount;
            ds.push(d);
            break;
        }
        if (!found) {
            break;
        }
    }
    return ds;
}
exports.getWithdrawDenomList = getWithdrawDenomList;
/**
 * Get information about a withdrawal from
 * a taler://withdraw URI.
 */
function getWithdrawalInfo(ws, talerWithdrawUri) {
    return __awaiter(this, void 0, void 0, function* () {
        const uriResult = taleruri.parseWithdrawUri(talerWithdrawUri);
        if (!uriResult) {
            throw Error("can't parse URL");
        }
        const resp = yield ws.http.get(uriResult.statusUrl);
        console.log("resp:", resp.responseJson);
        const status = talerTypes.WithdrawOperationStatusResponse.checked(resp.responseJson);
        return {
            amount: Amounts.parseOrThrow(status.amount),
            confirmTransferUrl: status.confirm_transfer_url,
            extractedStatusUrl: uriResult.statusUrl,
            selectionDone: status.selection_done,
            senderWire: status.sender_wire,
            suggestedExchange: status.suggested_exchange,
            transferDone: status.transfer_done,
            wireTypes: status.wire_types,
        };
    });
}
exports.getWithdrawalInfo = getWithdrawalInfo;
function acceptWithdrawal(ws, talerWithdrawUri, selectedExchange) {
    return __awaiter(this, void 0, void 0, function* () {
        const withdrawInfo = yield getWithdrawalInfo(ws, talerWithdrawUri);
        const exchangeWire = yield exchanges.getExchangePaytoUri(ws, selectedExchange, withdrawInfo.wireTypes);
        const reserve = yield reserves.createReserve(ws, {
            amount: withdrawInfo.amount,
            bankWithdrawStatusUrl: withdrawInfo.extractedStatusUrl,
            exchange: selectedExchange,
            senderWire: withdrawInfo.senderWire,
            exchangeWire: exchangeWire,
        });
        ws.badge.showNotification();
        ws.notifier.notify();
        // We do this here, as the reserve should be registered before we return,
        // so that we can redirect the user to the bank's status page.
        yield reserves.processReserveBankStatus(ws, reserve.reservePub);
        ws.notifier.notify();
        console.log("acceptWithdrawal: returning");
        return {
            reservePub: reserve.reservePub,
            confirmTransferUrl: withdrawInfo.confirmTransferUrl,
        };
    });
}
exports.acceptWithdrawal = acceptWithdrawal;
function getPossibleDenoms(ws, exchangeBaseUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        return yield query.oneShotIterIndex(ws.db, dbTypes.Stores.denominations.exchangeBaseUrlIndex, exchangeBaseUrl).filter(d => {
            return (d.status === dbTypes.DenominationStatus.Unverified ||
                d.status === dbTypes.DenominationStatus.VerifiedGood);
        });
    });
}
/**
 * Given a planchet, withdraw a coin from the exchange.
 */
function processPlanchet(ws, withdrawalSessionId, coinIdx) {
    return __awaiter(this, void 0, void 0, function* () {
        const withdrawalSession = yield query.oneShotGet(ws.db, dbTypes.Stores.withdrawalSession, withdrawalSessionId);
        if (!withdrawalSession) {
            return;
        }
        if (withdrawalSession.withdrawn[coinIdx]) {
            return;
        }
        if (withdrawalSession.source.type === "reserve") ;
        const planchet = withdrawalSession.planchets[coinIdx];
        if (!planchet) {
            console.log("processPlanchet: planchet not found");
            return;
        }
        const exchange = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, withdrawalSession.exchangeBaseUrl);
        if (!exchange) {
            console.error("db inconsistent: exchange for planchet not found");
            return;
        }
        const denom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
            withdrawalSession.exchangeBaseUrl,
            planchet.denomPub,
        ]);
        if (!denom) {
            console.error("db inconsistent: denom for planchet not found");
            return;
        }
        const wd = {};
        wd.denom_pub_hash = planchet.denomPubHash;
        wd.reserve_pub = planchet.reservePub;
        wd.reserve_sig = planchet.withdrawSig;
        wd.coin_ev = planchet.coinEv;
        const reqUrl = new URL("reserve/withdraw", exchange.baseUrl).href;
        const resp = yield ws.http.postJson(reqUrl, wd);
        const r = resp.responseJson;
        const denomSig = yield ws.cryptoApi.rsaUnblind(r.ev_sig, planchet.blindingKey, planchet.denomPub);
        const coin = {
            blindingKey: planchet.blindingKey,
            coinPriv: planchet.coinPriv,
            coinPub: planchet.coinPub,
            currentAmount: planchet.coinValue,
            denomPub: planchet.denomPub,
            denomPubHash: planchet.denomPubHash,
            denomSig,
            exchangeBaseUrl: withdrawalSession.exchangeBaseUrl,
            reservePub: planchet.reservePub,
            status: dbTypes.CoinStatus.Fresh,
            coinIndex: coinIdx,
            withdrawSessionId: withdrawalSessionId,
        };
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.coins, dbTypes.Stores.withdrawalSession, dbTypes.Stores.reserves], (tx) => __awaiter(this, void 0, void 0, function* () {
            const ws = yield tx.get(dbTypes.Stores.withdrawalSession, withdrawalSessionId);
            if (!ws) {
                return;
            }
            if (ws.withdrawn[coinIdx]) {
                // Already withdrawn
                return;
            }
            ws.withdrawn[coinIdx] = true;
            yield tx.put(dbTypes.Stores.withdrawalSession, ws);
            if (!planchet.isFromTip) {
                const r = yield tx.get(dbTypes.Stores.reserves, planchet.reservePub);
                if (r) {
                    r.withdrawCompletedAmount = Amounts.add(r.withdrawCompletedAmount, Amounts.add(denom.value, denom.feeWithdraw).amount).amount;
                    yield tx.put(dbTypes.Stores.reserves, r);
                }
            }
            yield tx.add(dbTypes.Stores.coins, coin);
        }));
        ws.notifier.notify();
        logger.trace(`withdraw of one coin ${coin.coinPub} finished`);
    });
}
/**
 * Get a list of denominations to withdraw from the given exchange for the
 * given amount, making sure that all denominations' signatures are verified.
 *
 * Writes to the DB in order to record the result from verifying
 * denominations.
 */
function getVerifiedWithdrawDenomList(ws, exchangeBaseUrl, amount) {
    return __awaiter(this, void 0, void 0, function* () {
        const exchange = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, exchangeBaseUrl);
        if (!exchange) {
            console.log("exchange not found");
            throw Error(`exchange ${exchangeBaseUrl} not found`);
        }
        const exchangeDetails = exchange.details;
        if (!exchangeDetails) {
            console.log("exchange details not available");
            throw Error(`exchange ${exchangeBaseUrl} details not available`);
        }
        console.log("getting possible denoms");
        const possibleDenoms = yield getPossibleDenoms(ws, exchange.baseUrl);
        console.log("got possible denoms");
        let allValid = false;
        let selectedDenoms;
        do {
            allValid = true;
            selectedDenoms = getWithdrawDenomList(amount, possibleDenoms);
            console.log("got withdraw denom list");
            for (const denom of selectedDenoms || []) {
                if (denom.status === dbTypes.DenominationStatus.Unverified) {
                    console.log("checking validity", denom, exchangeDetails.masterPublicKey);
                    const valid = yield ws.cryptoApi.isValidDenom(denom, exchangeDetails.masterPublicKey);
                    console.log("done checking validity");
                    if (!valid) {
                        denom.status = dbTypes.DenominationStatus.VerifiedBad;
                        allValid = false;
                    }
                    else {
                        denom.status = dbTypes.DenominationStatus.VerifiedGood;
                    }
                    yield query.oneShotPut(ws.db, dbTypes.Stores.denominations, denom);
                }
            }
        } while (selectedDenoms.length > 0 && !allValid);
        console.log("returning denoms");
        return selectedDenoms;
    });
}
exports.getVerifiedWithdrawDenomList = getVerifiedWithdrawDenomList;
function makePlanchet(ws, withdrawalSessionId, coinIndex) {
    return __awaiter(this, void 0, void 0, function* () {
        const withdrawalSession = yield query.oneShotGet(ws.db, dbTypes.Stores.withdrawalSession, withdrawalSessionId);
        if (!withdrawalSession) {
            return;
        }
        const src = withdrawalSession.source;
        if (src.type !== "reserve") {
            throw Error("invalid state");
        }
        const reserve = yield query.oneShotGet(ws.db, dbTypes.Stores.reserves, src.reservePub);
        if (!reserve) {
            return;
        }
        const denom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
            withdrawalSession.exchangeBaseUrl,
            withdrawalSession.denoms[coinIndex],
        ]);
        if (!denom) {
            return;
        }
        const r = yield ws.cryptoApi.createPlanchet({
            denomPub: denom.denomPub,
            feeWithdraw: denom.feeWithdraw,
            reservePriv: reserve.reservePriv,
            reservePub: reserve.reservePub,
            value: denom.value,
        });
        const newPlanchet = {
            blindingKey: r.blindingKey,
            coinEv: r.coinEv,
            coinPriv: r.coinPriv,
            coinPub: r.coinPub,
            coinValue: r.coinValue,
            denomPub: r.denomPub,
            denomPubHash: r.denomPubHash,
            isFromTip: false,
            reservePub: r.reservePub,
            withdrawSig: r.withdrawSig,
        };
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.withdrawalSession], (tx) => __awaiter(this, void 0, void 0, function* () {
            const myWs = yield tx.get(dbTypes.Stores.withdrawalSession, withdrawalSessionId);
            if (!myWs) {
                return;
            }
            if (myWs.planchets[coinIndex]) {
                return;
            }
            myWs.planchets[coinIndex] = newPlanchet;
            yield tx.put(dbTypes.Stores.withdrawalSession, myWs);
        }));
    });
}
function processWithdrawCoin(ws, withdrawalSessionId, coinIndex) {
    return __awaiter(this, void 0, void 0, function* () {
        logger.trace("starting withdraw for coin", coinIndex);
        const withdrawalSession = yield query.oneShotGet(ws.db, dbTypes.Stores.withdrawalSession, withdrawalSessionId);
        if (!withdrawalSession) {
            console.log("ws doesn't exist");
            return;
        }
        const coin = yield query.oneShotGetIndexed(ws.db, dbTypes.Stores.coins.byWithdrawalWithIdx, [withdrawalSessionId, coinIndex]);
        if (coin) {
            console.log("coin already exists");
            return;
        }
        if (!withdrawalSession.planchets[coinIndex]) {
            logger.trace("creating planchet for coin", coinIndex);
            const key = `${withdrawalSessionId}-${coinIndex}`;
            const p = ws.memoMakePlanchet.find(key);
            if (p) {
                yield p;
            }
            else {
                ws.memoMakePlanchet.put(key, makePlanchet(ws, withdrawalSessionId, coinIndex));
            }
            yield makePlanchet(ws, withdrawalSessionId, coinIndex);
            logger.trace("done creating planchet for coin", coinIndex);
        }
        yield processPlanchet(ws, withdrawalSessionId, coinIndex);
        logger.trace("starting withdraw for coin", coinIndex);
    });
}
function processWithdrawSession(ws, withdrawalSessionId) {
    return __awaiter(this, void 0, void 0, function* () {
        logger.trace("processing withdraw session", withdrawalSessionId);
        const withdrawalSession = yield query.oneShotGet(ws.db, dbTypes.Stores.withdrawalSession, withdrawalSessionId);
        if (!withdrawalSession) {
            logger.trace("withdraw session doesn't exist");
            return;
        }
        const ps = withdrawalSession.denoms.map((d, i) => processWithdrawCoin(ws, withdrawalSessionId, i));
        yield Promise.all(ps);
        ws.badge.showNotification();
        return;
    });
}
exports.processWithdrawSession = processWithdrawSession;
function getWithdrawDetailsForAmount(ws, baseUrl, amount) {
    return __awaiter(this, void 0, void 0, function* () {
        const exchangeInfo = yield exchanges.updateExchangeFromUrl(ws, baseUrl);
        const exchangeDetails = exchangeInfo.details;
        if (!exchangeDetails) {
            throw Error(`exchange ${exchangeInfo.baseUrl} details not available`);
        }
        const exchangeWireInfo = exchangeInfo.wireInfo;
        if (!exchangeWireInfo) {
            throw Error(`exchange ${exchangeInfo.baseUrl} wire details not available`);
        }
        const selectedDenoms = yield getVerifiedWithdrawDenomList(ws, baseUrl, amount);
        let acc = Amounts.getZero(amount.currency);
        for (const d of selectedDenoms) {
            acc = Amounts.add(acc, d.feeWithdraw).amount;
        }
        const actualCoinCost = selectedDenoms
            .map((d) => Amounts.add(d.value, d.feeWithdraw).amount)
            .reduce((a, b) => Amounts.add(a, b).amount);
        const exchangeWireAccounts = [];
        for (let account of exchangeWireInfo.accounts) {
            exchangeWireAccounts.push(account.url);
        }
        const { isTrusted, isAudited } = yield exchanges.getExchangeTrust(ws, exchangeInfo);
        let earliestDepositExpiration = selectedDenoms[0].stampExpireDeposit;
        for (let i = 1; i < selectedDenoms.length; i++) {
            const expireDeposit = selectedDenoms[i].stampExpireDeposit;
            if (expireDeposit.t_ms < earliestDepositExpiration.t_ms) {
                earliestDepositExpiration = expireDeposit;
            }
        }
        const possibleDenoms = yield query.oneShotIterIndex(ws.db, dbTypes.Stores.denominations.exchangeBaseUrlIndex, baseUrl).filter(d => d.isOffered);
        const trustedAuditorPubs = [];
        const currencyRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.currencies, amount.currency);
        if (currencyRecord) {
            trustedAuditorPubs.push(...currencyRecord.auditors.map(a => a.auditorPub));
        }
        let versionMatch;
        if (exchangeDetails.protocolVersion) {
            versionMatch = LibtoolVersion.compare(wallet.WALLET_PROTOCOL_VERSION, exchangeDetails.protocolVersion);
            if (versionMatch &&
                !versionMatch.compatible &&
                versionMatch.currentCmp === -1) {
                console.warn(`wallet version ${wallet.WALLET_PROTOCOL_VERSION} might be outdated ` +
                    `(exchange has ${exchangeDetails.protocolVersion}), checking for updates`);
            }
        }
        const ret = {
            earliestDepositExpiration,
            exchangeInfo,
            exchangeWireAccounts,
            exchangeVersion: exchangeDetails.protocolVersion || "unknown",
            isAudited,
            isTrusted,
            numOfferedDenoms: possibleDenoms.length,
            overhead: Amounts.sub(amount, actualCoinCost).amount,
            selectedDenoms,
            trustedAuditorPubs,
            versionMatch,
            walletVersion: wallet.WALLET_PROTOCOL_VERSION,
            wireFees: exchangeWireInfo,
            withdrawFee: acc,
        };
        return ret;
    });
}
exports.getWithdrawDetailsForAmount = getWithdrawDetailsForAmount;
function getWithdrawDetailsForUri(ws, talerWithdrawUri, maybeSelectedExchange) {
    return __awaiter(this, void 0, void 0, function* () {
        const info = yield getWithdrawalInfo(ws, talerWithdrawUri);
        let rci = undefined;
        if (maybeSelectedExchange) {
            rci = yield getWithdrawDetailsForAmount(ws, maybeSelectedExchange, info.amount);
        }
        return {
            withdrawInfo: info,
            reserveCreationInfo: rci,
        };
    });
}
exports.getWithdrawDetailsForUri = getWithdrawDetailsForUri;

});

unwrapExports(withdraw);
var withdraw_1 = withdraw.getWithdrawDenomList;
var withdraw_2 = withdraw.getWithdrawalInfo;
var withdraw_3 = withdraw.acceptWithdrawal;
var withdraw_4 = withdraw.getVerifiedWithdrawDenomList;
var withdraw_5 = withdraw.processWithdrawSession;
var withdraw_6 = withdraw.getWithdrawDetailsForAmount;
var withdraw_7 = withdraw.getWithdrawDetailsForUri;

var refresh_1 = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Amounts = __importStar(amounts);






const logger = new logging.Logger("refresh.ts");
/**
 * Get the amount that we lose when refreshing a coin of the given denomination
 * with a certain amount left.
 *
 * If the amount left is zero, then the refresh cost
 * is also considered to be zero.  If a refresh isn't possible (e.g. due to lack of
 * the right denominations), then the cost is the full amount left.
 *
 * Considers refresh fees, withdrawal fees after refresh and amounts too small
 * to refresh.
 */
function getTotalRefreshCost(denoms, refreshedDenom, amountLeft) {
    const withdrawAmount = Amounts.sub(amountLeft, refreshedDenom.feeRefresh)
        .amount;
    const withdrawDenoms = withdraw.getWithdrawDenomList(withdrawAmount, denoms);
    const resultingAmount = Amounts.add(Amounts.getZero(withdrawAmount.currency), ...withdrawDenoms.map(d => d.value)).amount;
    const totalCost = Amounts.sub(amountLeft, resultingAmount).amount;
    logger.trace("total refresh cost for", helpers.amountToPretty(amountLeft), "is", helpers.amountToPretty(totalCost));
    return totalCost;
}
exports.getTotalRefreshCost = getTotalRefreshCost;
function refreshMelt(ws, refreshSessionId) {
    return __awaiter(this, void 0, void 0, function* () {
        const refreshSession = yield query.oneShotGet(ws.db, dbTypes.Stores.refresh, refreshSessionId);
        if (!refreshSession) {
            return;
        }
        if (refreshSession.norevealIndex !== undefined) {
            return;
        }
        const coin = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, refreshSession.meltCoinPub);
        if (!coin) {
            console.error("can't melt coin, it does not exist");
            return;
        }
        const reqUrl = new URL("refresh/melt", refreshSession.exchangeBaseUrl);
        const meltReq = {
            coin_pub: coin.coinPub,
            confirm_sig: refreshSession.confirmSig,
            denom_pub_hash: coin.denomPubHash,
            denom_sig: coin.denomSig,
            rc: refreshSession.hash,
            value_with_fee: refreshSession.valueWithFee,
        };
        logger.trace("melt request:", meltReq);
        const resp = yield ws.http.postJson(reqUrl.href, meltReq);
        logger.trace("melt response:", resp.responseJson);
        if (resp.status !== 200) {
            console.error(resp.responseJson);
            throw Error("refresh failed");
        }
        const respJson = resp.responseJson;
        const norevealIndex = respJson.noreveal_index;
        if (typeof norevealIndex !== "number") {
            throw Error("invalid response");
        }
        refreshSession.norevealIndex = norevealIndex;
        yield query.oneShotMutate(ws.db, dbTypes.Stores.refresh, refreshSessionId, rs => {
            if (rs.norevealIndex !== undefined) {
                return;
            }
            if (rs.finished) {
                return;
            }
            rs.norevealIndex = norevealIndex;
            return rs;
        });
        ws.notifier.notify();
    });
}
function refreshReveal(ws, refreshSessionId) {
    return __awaiter(this, void 0, void 0, function* () {
        const refreshSession = yield query.oneShotGet(ws.db, dbTypes.Stores.refresh, refreshSessionId);
        if (!refreshSession) {
            return;
        }
        const norevealIndex = refreshSession.norevealIndex;
        if (norevealIndex === undefined) {
            throw Error("can't reveal without melting first");
        }
        const privs = Array.from(refreshSession.transferPrivs);
        privs.splice(norevealIndex, 1);
        const planchets = refreshSession.planchetsForGammas[norevealIndex];
        if (!planchets) {
            throw Error("refresh index error");
        }
        const meltCoinRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, refreshSession.meltCoinPub);
        if (!meltCoinRecord) {
            throw Error("inconsistent database");
        }
        const evs = planchets.map((x) => x.coinEv);
        const linkSigs = [];
        for (let i = 0; i < refreshSession.newDenoms.length; i++) {
            const linkSig = yield ws.cryptoApi.signCoinLink(meltCoinRecord.coinPriv, refreshSession.newDenomHashes[i], refreshSession.meltCoinPub, refreshSession.transferPubs[norevealIndex], planchets[i].coinEv);
            linkSigs.push(linkSig);
        }
        const req = {
            coin_evs: evs,
            new_denoms_h: refreshSession.newDenomHashes,
            rc: refreshSession.hash,
            transfer_privs: privs,
            transfer_pub: refreshSession.transferPubs[norevealIndex],
            link_sigs: linkSigs,
        };
        const reqUrl = new URL("refresh/reveal", refreshSession.exchangeBaseUrl);
        logger.trace("reveal request:", req);
        let resp;
        try {
            resp = yield ws.http.postJson(reqUrl.href, req);
        }
        catch (e) {
            console.error("got error during /refresh/reveal request");
            console.error(e);
            return;
        }
        logger.trace("session:", refreshSession);
        logger.trace("reveal response:", resp);
        if (resp.status !== 200) {
            console.error("error: /refresh/reveal returned status " + resp.status);
            return;
        }
        const respJson = resp.responseJson;
        if (!respJson.ev_sigs || !Array.isArray(respJson.ev_sigs)) {
            console.error("/refresh/reveal did not contain ev_sigs");
            return;
        }
        const exchange = query.oneShotGet(ws.db, dbTypes.Stores.exchanges, refreshSession.exchangeBaseUrl);
        if (!exchange) {
            console.error(`exchange ${refreshSession.exchangeBaseUrl} not found`);
            return;
        }
        const coins = [];
        for (let i = 0; i < respJson.ev_sigs.length; i++) {
            const denom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
                refreshSession.exchangeBaseUrl,
                refreshSession.newDenoms[i],
            ]);
            if (!denom) {
                console.error("denom not found");
                continue;
            }
            const pc = refreshSession.planchetsForGammas[refreshSession.norevealIndex][i];
            const denomSig = yield ws.cryptoApi.rsaUnblind(respJson.ev_sigs[i].ev_sig, pc.blindingKey, denom.denomPub);
            const coin = {
                blindingKey: pc.blindingKey,
                coinPriv: pc.privateKey,
                coinPub: pc.publicKey,
                currentAmount: denom.value,
                denomPub: denom.denomPub,
                denomPubHash: denom.denomPubHash,
                denomSig,
                exchangeBaseUrl: refreshSession.exchangeBaseUrl,
                reservePub: undefined,
                status: dbTypes.CoinStatus.Fresh,
                coinIndex: -1,
                withdrawSessionId: "",
            };
            coins.push(coin);
        }
        refreshSession.finished = true;
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.coins, dbTypes.Stores.refresh], (tx) => __awaiter(this, void 0, void 0, function* () {
            const rs = yield tx.get(dbTypes.Stores.refresh, refreshSessionId);
            if (!rs) {
                return;
            }
            if (rs.finished) {
                return;
            }
            for (let coin of coins) {
                yield tx.put(dbTypes.Stores.coins, coin);
            }
            yield tx.put(dbTypes.Stores.refresh, refreshSession);
        }));
        ws.notifier.notify();
    });
}
function processRefreshSession(ws, refreshSessionId) {
    return __awaiter(this, void 0, void 0, function* () {
        const refreshSession = yield query.oneShotGet(ws.db, dbTypes.Stores.refresh, refreshSessionId);
        if (!refreshSession) {
            return;
        }
        if (refreshSession.finished) {
            return;
        }
        if (typeof refreshSession.norevealIndex !== "number") {
            yield refreshMelt(ws, refreshSession.refreshSessionId);
        }
        yield refreshReveal(ws, refreshSession.refreshSessionId);
        logger.trace("refresh finished");
    });
}
exports.processRefreshSession = processRefreshSession;
function refresh(ws, oldCoinPub, force = false) {
    return __awaiter(this, void 0, void 0, function* () {
        const coin = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, oldCoinPub);
        if (!coin) {
            console.warn("can't refresh, coin not in database");
            return;
        }
        switch (coin.status) {
            case dbTypes.CoinStatus.Dirty:
                break;
            case dbTypes.CoinStatus.Dormant:
                return;
            case dbTypes.CoinStatus.Fresh:
                if (!force) {
                    return;
                }
                break;
        }
        const exchange = yield exchanges.updateExchangeFromUrl(ws, coin.exchangeBaseUrl);
        if (!exchange) {
            throw Error("db inconsistent: exchange of coin not found");
        }
        const oldDenom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
            exchange.baseUrl,
            coin.denomPub,
        ]);
        if (!oldDenom) {
            throw Error("db inconsistent: denomination for coin not found");
        }
        const availableDenoms = yield query.oneShotIterIndex(ws.db, dbTypes.Stores.denominations.exchangeBaseUrlIndex, exchange.baseUrl).toArray();
        const availableAmount = Amounts.sub(coin.currentAmount, oldDenom.feeRefresh)
            .amount;
        const newCoinDenoms = withdraw.getWithdrawDenomList(availableAmount, availableDenoms);
        if (newCoinDenoms.length === 0) {
            logger.trace(`not refreshing, available amount ${helpers.amountToPretty(availableAmount)} too small`);
            yield query.oneShotMutate(ws.db, dbTypes.Stores.coins, oldCoinPub, x => {
                if (x.status != coin.status) {
                    // Concurrent modification?
                    return;
                }
                x.status = dbTypes.CoinStatus.Dormant;
                return x;
            });
            ws.notifier.notify();
            return;
        }
        const refreshSession = yield ws.cryptoApi.createRefreshSession(exchange.baseUrl, 3, coin, newCoinDenoms, oldDenom.feeRefresh);
        function mutateCoin(c) {
            const r = Amounts.sub(c.currentAmount, refreshSession.valueWithFee);
            if (r.saturated) {
                // Something else must have written the coin value
                throw query.TransactionAbort;
            }
            c.currentAmount = r.amount;
            c.status = dbTypes.CoinStatus.Dormant;
            return c;
        }
        // Store refresh session and subtract refreshed amount from
        // coin in the same transaction.
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.refresh, dbTypes.Stores.coins], (tx) => __awaiter(this, void 0, void 0, function* () {
            yield tx.put(dbTypes.Stores.refresh, refreshSession);
            yield tx.mutate(dbTypes.Stores.coins, coin.coinPub, mutateCoin);
        }));
        logger.info(`created refresh session ${refreshSession.refreshSessionId}`);
        ws.notifier.notify();
        yield processRefreshSession(ws, refreshSession.refreshSessionId);
    });
}
exports.refresh = refresh;

});

unwrapExports(refresh_1);
var refresh_2 = refresh_1.getTotalRefreshCost;
var refresh_3 = refresh_1.processRefreshSession;
var refresh_4 = refresh_1.refresh;

var pay = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });




const Amounts = __importStar(amounts);





const logger = new logging.Logger("pay.ts");
/**
 * Select coins for a payment under the merchant's constraints.
 *
 * @param denoms all available denoms, used to compute refresh fees
 */
function selectPayCoins(denoms, cds, paymentAmount, depositFeeLimit) {
    if (cds.length === 0) {
        return undefined;
    }
    // Sort by ascending deposit fee and denomPub if deposit fee is the same
    // (to guarantee deterministic results)
    cds.sort((o1, o2) => Amounts.cmp(o1.denom.feeDeposit, o2.denom.feeDeposit) ||
        helpers.strcmp(o1.denom.denomPub, o2.denom.denomPub));
    const currency = cds[0].denom.value.currency;
    const cdsResult = [];
    let accDepositFee = Amounts.getZero(currency);
    let accAmount = Amounts.getZero(currency);
    for (const { coin, denom } of cds) {
        if (coin.suspended) {
            continue;
        }
        if (coin.status !== dbTypes.CoinStatus.Fresh) {
            continue;
        }
        if (Amounts.cmp(denom.feeDeposit, coin.currentAmount) >= 0) {
            continue;
        }
        cdsResult.push({ coin, denom });
        accDepositFee = Amounts.add(denom.feeDeposit, accDepositFee).amount;
        let leftAmount = Amounts.sub(coin.currentAmount, Amounts.sub(paymentAmount, accAmount).amount).amount;
        accAmount = Amounts.add(coin.currentAmount, accAmount).amount;
        const coversAmount = Amounts.cmp(accAmount, paymentAmount) >= 0;
        const coversAmountWithFee = Amounts.cmp(accAmount, Amounts.add(paymentAmount, denom.feeDeposit).amount) >= 0;
        const isBelowFee = Amounts.cmp(accDepositFee, depositFeeLimit) <= 0;
        logger.trace("candidate coin selection", {
            coversAmount,
            isBelowFee,
            accDepositFee,
            accAmount,
            paymentAmount,
        });
        if ((coversAmount && isBelowFee) || coversAmountWithFee) {
            const depositFeeToCover = Amounts.sub(accDepositFee, depositFeeLimit)
                .amount;
            leftAmount = Amounts.sub(leftAmount, depositFeeToCover).amount;
            logger.trace("deposit fee to cover", helpers.amountToPretty(depositFeeToCover));
            let totalFees = Amounts.getZero(currency);
            if (coversAmountWithFee && !isBelowFee) {
                // these are the fees the customer has to pay
                // because the merchant doesn't cover them
                totalFees = Amounts.sub(depositFeeLimit, accDepositFee).amount;
            }
            totalFees = Amounts.add(totalFees, refresh_1.getTotalRefreshCost(denoms, denom, leftAmount)).amount;
            return { cds: cdsResult, totalFees };
        }
    }
    return undefined;
}
exports.selectPayCoins = selectPayCoins;
/**
 * Get exchanges and associated coins that are still spendable, but only
 * if the sum the coins' remaining value covers the payment amount and fees.
 */
function getCoinsForPayment(ws, args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { allowedAuditors, allowedExchanges, depositFeeLimit, paymentAmount, wireFeeAmortization, wireFeeLimit, wireFeeTime, wireMethod, } = args;
        let remainingAmount = paymentAmount;
        const exchanges = yield query.oneShotIter(ws.db, dbTypes.Stores.exchanges).toArray();
        for (const exchange of exchanges) {
            let isOkay = false;
            const exchangeDetails = exchange.details;
            if (!exchangeDetails) {
                continue;
            }
            const exchangeFees = exchange.wireInfo;
            if (!exchangeFees) {
                continue;
            }
            // is the exchange explicitly allowed?
            for (const allowedExchange of allowedExchanges) {
                if (allowedExchange.master_pub === exchangeDetails.masterPublicKey) {
                    isOkay = true;
                    break;
                }
            }
            // is the exchange allowed because of one of its auditors?
            if (!isOkay) {
                for (const allowedAuditor of allowedAuditors) {
                    for (const auditor of exchangeDetails.auditors) {
                        if (auditor.auditor_pub === allowedAuditor.auditor_pub) {
                            isOkay = true;
                            break;
                        }
                    }
                    if (isOkay) {
                        break;
                    }
                }
            }
            if (!isOkay) {
                continue;
            }
            const coins = yield query.oneShotIterIndex(ws.db, dbTypes.Stores.coins.exchangeBaseUrlIndex, exchange.baseUrl).toArray();
            const denoms = yield query.oneShotIterIndex(ws.db, dbTypes.Stores.denominations.exchangeBaseUrlIndex, exchange.baseUrl).toArray();
            if (!coins || coins.length === 0) {
                continue;
            }
            // Denomination of the first coin, we assume that all other
            // coins have the same currency
            const firstDenom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
                exchange.baseUrl,
                coins[0].denomPub,
            ]);
            if (!firstDenom) {
                throw Error("db inconsistent");
            }
            const currency = firstDenom.value.currency;
            const cds = [];
            for (const coin of coins) {
                const denom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
                    exchange.baseUrl,
                    coin.denomPub,
                ]);
                if (!denom) {
                    throw Error("db inconsistent");
                }
                if (denom.value.currency !== currency) {
                    console.warn(`same pubkey for different currencies at exchange ${exchange.baseUrl}`);
                    continue;
                }
                if (coin.suspended) {
                    continue;
                }
                if (coin.status !== dbTypes.CoinStatus.Fresh) {
                    continue;
                }
                cds.push({ coin, denom });
            }
            let totalFees = Amounts.getZero(currency);
            let wireFee;
            for (const fee of exchangeFees.feesForType[wireMethod] || []) {
                if (fee.startStamp <= wireFeeTime && fee.endStamp >= wireFeeTime) {
                    wireFee = fee.wireFee;
                    break;
                }
            }
            if (wireFee) {
                const amortizedWireFee = Amounts.divide(wireFee, wireFeeAmortization);
                if (Amounts.cmp(wireFeeLimit, amortizedWireFee) < 0) {
                    totalFees = Amounts.add(amortizedWireFee, totalFees).amount;
                    remainingAmount = Amounts.add(amortizedWireFee, remainingAmount).amount;
                }
            }
            const res = selectPayCoins(denoms, cds, remainingAmount, depositFeeLimit);
            if (res) {
                totalFees = Amounts.add(totalFees, res.totalFees).amount;
                return {
                    cds: res.cds,
                    exchangeUrl: exchange.baseUrl,
                    totalAmount: remainingAmount,
                    totalFees,
                };
            }
        }
        return undefined;
    });
}
/**
 * Record all information that is necessary to
 * pay for a proposal in the wallet's database.
 */
function recordConfirmPay(ws, proposal, payCoinInfo, chosenExchange) {
    return __awaiter(this, void 0, void 0, function* () {
        const d = proposal.download;
        if (!d) {
            throw Error("proposal is in invalid state");
        }
        const payReq = {
            coins: payCoinInfo.sigs,
            merchant_pub: d.contractTerms.merchant_pub,
            mode: "pay",
            order_id: d.contractTerms.order_id,
        };
        const t = {
            abortDone: false,
            abortRequested: false,
            contractTerms: d.contractTerms,
            contractTermsHash: d.contractTermsHash,
            finished: false,
            lastSessionId: undefined,
            merchantSig: d.merchantSig,
            payReq,
            refundsDone: {},
            refundsPending: {},
            timestamp: walletTypes.getTimestampNow(),
            timestamp_refund: undefined,
            proposalId: proposal.proposalId,
        };
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.coins, dbTypes.Stores.purchases, dbTypes.Stores.proposals], (tx) => __awaiter(this, void 0, void 0, function* () {
            const p = yield tx.get(dbTypes.Stores.proposals, proposal.proposalId);
            if (p) {
                p.proposalStatus = dbTypes.ProposalStatus.ACCEPTED;
                yield tx.put(dbTypes.Stores.proposals, p);
            }
            yield tx.put(dbTypes.Stores.purchases, t);
            for (let c of payCoinInfo.updatedCoins) {
                yield tx.put(dbTypes.Stores.coins, c);
            }
        }));
        ws.badge.showNotification();
        ws.notifier.notify();
        return t;
    });
}
function getNextUrl(contractTerms) {
    const f = contractTerms.fulfillment_url;
    if (f.startsWith("http://") || f.startsWith("https://")) {
        const fu = new URL(contractTerms.fulfillment_url);
        fu.searchParams.set("order_id", contractTerms.order_id);
        return fu.href;
    }
    else {
        return f;
    }
}
function abortFailedPayment(ws, proposalId) {
    return __awaiter(this, void 0, void 0, function* () {
        const purchase = yield query.oneShotGet(ws.db, dbTypes.Stores.purchases, proposalId);
        if (!purchase) {
            throw Error("Purchase not found, unable to abort with refund");
        }
        if (purchase.finished) {
            throw Error("Purchase already finished, not aborting");
        }
        if (purchase.abortDone) {
            console.warn("abort requested on already aborted purchase");
            return;
        }
        purchase.abortRequested = true;
        // From now on, we can't retry payment anymore,
        // so mark this in the DB in case the /pay abort
        // does not complete on the first try.
        yield query.oneShotPut(ws.db, dbTypes.Stores.purchases, purchase);
        let resp;
        const abortReq = Object.assign(Object.assign({}, purchase.payReq), { mode: "abort-refund" });
        const payUrl = new URL("pay", purchase.contractTerms.merchant_base_url).href;
        try {
            resp = yield ws.http.postJson(payUrl, abortReq);
        }
        catch (e) {
            // Gives the user the option to retry / abort and refresh
            console.log("aborting payment failed", e);
            throw e;
        }
        const refundResponse = talerTypes.MerchantRefundResponse.checked(resp.responseJson);
        yield acceptRefundResponse(ws, refundResponse);
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.purchases], (tx) => __awaiter(this, void 0, void 0, function* () {
            const p = yield tx.get(dbTypes.Stores.purchases, proposalId);
            if (!p) {
                return;
            }
            p.abortDone = true;
            yield tx.put(dbTypes.Stores.purchases, p);
        }));
    });
}
exports.abortFailedPayment = abortFailedPayment;
function processDownloadProposal(ws, proposalId) {
    return __awaiter(this, void 0, void 0, function* () {
        const proposal = yield query.oneShotGet(ws.db, dbTypes.Stores.proposals, proposalId);
        if (!proposal) {
            return;
        }
        if (proposal.proposalStatus != dbTypes.ProposalStatus.DOWNLOADING) {
            return;
        }
        const parsed_url = new URL(proposal.url);
        parsed_url.searchParams.set("nonce", proposal.noncePub);
        const urlWithNonce = parsed_url.href;
        console.log("downloading contract from '" + urlWithNonce + "'");
        let resp;
        try {
            resp = yield ws.http.get(urlWithNonce);
        }
        catch (e) {
            console.log("contract download failed", e);
            throw e;
        }
        const proposalResp = talerTypes.Proposal.checked(resp.responseJson);
        const contractTermsHash = yield ws.cryptoApi.hashString(helpers.canonicalJson(proposalResp.contract_terms));
        const fulfillmentUrl = proposalResp.contract_terms.fulfillment_url;
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.proposals, dbTypes.Stores.purchases], (tx) => __awaiter(this, void 0, void 0, function* () {
            const p = yield tx.get(dbTypes.Stores.proposals, proposalId);
            if (!p) {
                return;
            }
            if (p.proposalStatus !== dbTypes.ProposalStatus.DOWNLOADING) {
                return;
            }
            if (fulfillmentUrl.startsWith("http://") ||
                fulfillmentUrl.startsWith("https://")) {
                const differentPurchase = yield tx.getIndexed(dbTypes.Stores.purchases.fulfillmentUrlIndex, fulfillmentUrl);
                if (differentPurchase) {
                    p.proposalStatus = dbTypes.ProposalStatus.REPURCHASE;
                    p.repurchaseProposalId = differentPurchase.proposalId;
                    yield tx.put(dbTypes.Stores.proposals, p);
                    return;
                }
            }
            p.download = {
                contractTerms: proposalResp.contract_terms,
                merchantSig: proposalResp.sig,
                contractTermsHash,
            };
            p.proposalStatus = dbTypes.ProposalStatus.PROPOSED;
            yield tx.put(dbTypes.Stores.proposals, p);
        }));
        ws.notifier.notify();
    });
}
exports.processDownloadProposal = processDownloadProposal;
/**
 * Download a proposal and store it in the database.
 * Returns an id for it to retrieve it later.
 *
 * @param sessionId Current session ID, if the proposal is being
 *  downloaded in the context of a session ID.
 */
function startDownloadProposal(ws, url, sessionId) {
    return __awaiter(this, void 0, void 0, function* () {
        const oldProposal = yield query.oneShotGetIndexed(ws.db, dbTypes.Stores.proposals.urlIndex, url);
        if (oldProposal) {
            yield processDownloadProposal(ws, oldProposal.proposalId);
            return oldProposal.proposalId;
        }
        const { priv, pub } = yield ws.cryptoApi.createEddsaKeypair();
        const proposalId = talerCrypto.encodeCrock(talerCrypto.getRandomBytes(32));
        const proposalRecord = {
            download: undefined,
            noncePriv: priv,
            noncePub: pub,
            timestamp: walletTypes.getTimestampNow(),
            url,
            downloadSessionId: sessionId,
            proposalId: proposalId,
            proposalStatus: dbTypes.ProposalStatus.DOWNLOADING,
            repurchaseProposalId: undefined,
        };
        yield query.oneShotPut(ws.db, dbTypes.Stores.proposals, proposalRecord);
        yield processDownloadProposal(ws, proposalId);
        return proposalId;
    });
}
function submitPay(ws, proposalId, sessionId) {
    return __awaiter(this, void 0, void 0, function* () {
        const purchase = yield query.oneShotGet(ws.db, dbTypes.Stores.purchases, proposalId);
        if (!purchase) {
            throw Error("Purchase not found: " + proposalId);
        }
        if (purchase.abortRequested) {
            throw Error("not submitting payment for aborted purchase");
        }
        let resp;
        const payReq = Object.assign(Object.assign({}, purchase.payReq), { session_id: sessionId });
        const payUrl = new URL("pay", purchase.contractTerms.merchant_base_url).href;
        try {
            resp = yield ws.http.postJson(payUrl, payReq);
        }
        catch (e) {
            // Gives the user the option to retry / abort and refresh
            console.log("payment failed", e);
            throw e;
        }
        const merchantResp = resp.responseJson;
        console.log("got success from pay URL");
        const merchantPub = purchase.contractTerms.merchant_pub;
        const valid = yield ws.cryptoApi.isValidPaymentSignature(merchantResp.sig, purchase.contractTermsHash, merchantPub);
        if (!valid) {
            console.error("merchant payment signature invalid");
            // FIXME: properly display error
            throw Error("merchant payment signature invalid");
        }
        purchase.finished = true;
        const modifiedCoins = [];
        for (const pc of purchase.payReq.coins) {
            const c = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, pc.coin_pub);
            if (!c) {
                console.error("coin not found");
                throw Error("coin used in payment not found");
            }
            c.status = dbTypes.CoinStatus.Dirty;
            modifiedCoins.push(c);
        }
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.coins, dbTypes.Stores.purchases], (tx) => __awaiter(this, void 0, void 0, function* () {
            for (let c of modifiedCoins) {
                yield tx.put(dbTypes.Stores.coins, c);
            }
            yield tx.put(dbTypes.Stores.purchases, purchase);
        }));
        for (const c of purchase.payReq.coins) {
            refresh_1.refresh(ws, c.coin_pub).catch(e => {
                console.log("error in refreshing after payment:", e);
            });
        }
        const nextUrl = getNextUrl(purchase.contractTerms);
        ws.cachedNextUrl[purchase.contractTerms.fulfillment_url] = {
            nextUrl,
            lastSessionId: sessionId,
        };
        return { nextUrl };
    });
}
exports.submitPay = submitPay;
/**
 * Check if a payment for the given taler://pay/ URI is possible.
 *
 * If the payment is possible, the signature are already generated but not
 * yet send to the merchant.
 */
function preparePay(ws, talerPayUri) {
    return __awaiter(this, void 0, void 0, function* () {
        const uriResult = taleruri.parsePayUri(talerPayUri);
        if (!uriResult) {
            return {
                status: "error",
                error: "URI not supported",
            };
        }
        const proposalId = yield startDownloadProposal(ws, uriResult.downloadUrl, uriResult.sessionId);
        let proposal = yield query.oneShotGet(ws.db, dbTypes.Stores.proposals, proposalId);
        if (!proposal) {
            throw Error(`could not get proposal ${proposalId}`);
        }
        if (proposal.proposalStatus === dbTypes.ProposalStatus.REPURCHASE) {
            const existingProposalId = proposal.repurchaseProposalId;
            if (!existingProposalId) {
                throw Error("invalid proposal state");
            }
            proposal = yield query.oneShotGet(ws.db, dbTypes.Stores.proposals, existingProposalId);
            if (!proposal) {
                throw Error("existing proposal is in wrong state");
            }
        }
        const d = proposal.download;
        if (!d) {
            console.error("bad proposal", proposal);
            throw Error("proposal is in invalid state");
        }
        const contractTerms = d.contractTerms;
        const merchantSig = d.merchantSig;
        if (!contractTerms || !merchantSig) {
            throw Error("BUG: proposal is in invalid state");
        }
        console.log("proposal", proposal);
        // First check if we already payed for it.
        const purchase = yield query.oneShotGet(ws.db, dbTypes.Stores.purchases, proposalId);
        if (!purchase) {
            const paymentAmount = Amounts.parseOrThrow(contractTerms.amount);
            let wireFeeLimit;
            if (contractTerms.max_wire_fee) {
                wireFeeLimit = Amounts.parseOrThrow(contractTerms.max_wire_fee);
            }
            else {
                wireFeeLimit = Amounts.getZero(paymentAmount.currency);
            }
            // If not already payed, check if we could pay for it.
            const res = yield getCoinsForPayment(ws, {
                allowedAuditors: contractTerms.auditors,
                allowedExchanges: contractTerms.exchanges,
                depositFeeLimit: Amounts.parseOrThrow(contractTerms.max_fee),
                paymentAmount,
                wireFeeAmortization: contractTerms.wire_fee_amortization || 1,
                wireFeeLimit,
                wireFeeTime: helpers.extractTalerStampOrThrow(contractTerms.timestamp),
                wireMethod: contractTerms.wire_method,
            });
            if (!res) {
                console.log("not confirming payment, insufficient coins");
                return {
                    status: "insufficient-balance",
                    contractTerms: contractTerms,
                    proposalId: proposal.proposalId,
                };
            }
            // Only create speculative signature if we don't already have one for this proposal
            if (!ws.speculativePayData ||
                (ws.speculativePayData &&
                    ws.speculativePayData.orderDownloadId !== proposalId)) {
                const { exchangeUrl, cds, totalAmount } = res;
                const payCoinInfo = yield ws.cryptoApi.signDeposit(contractTerms, cds, totalAmount);
                ws.speculativePayData = {
                    exchangeUrl,
                    payCoinInfo,
                    proposal,
                    orderDownloadId: proposalId,
                };
                logger.trace("created speculative pay data for payment");
            }
            return {
                status: "payment-possible",
                contractTerms: contractTerms,
                proposalId: proposal.proposalId,
                totalFees: res.totalFees,
            };
        }
        if (uriResult.sessionId) {
            yield submitPay(ws, proposalId, uriResult.sessionId);
        }
        return {
            status: "paid",
            contractTerms: purchase.contractTerms,
            nextUrl: getNextUrl(purchase.contractTerms),
        };
    });
}
exports.preparePay = preparePay;
/**
 * Get the speculative pay data, but only if coins have not changed in between.
 */
function getSpeculativePayData(ws, proposalId) {
    return __awaiter(this, void 0, void 0, function* () {
        const sp = ws.speculativePayData;
        if (!sp) {
            return;
        }
        if (sp.orderDownloadId !== proposalId) {
            return;
        }
        const coinKeys = sp.payCoinInfo.updatedCoins.map(x => x.coinPub);
        const coins = [];
        for (let coinKey of coinKeys) {
            const cc = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, coinKey);
            if (cc) {
                coins.push(cc);
            }
        }
        for (let i = 0; i < coins.length; i++) {
            const specCoin = sp.payCoinInfo.originalCoins[i];
            const currentCoin = coins[i];
            // Coin does not exist anymore!
            if (!currentCoin) {
                return;
            }
            if (Amounts.cmp(specCoin.currentAmount, currentCoin.currentAmount) !== 0) {
                return;
            }
        }
        return sp;
    });
}
/**
 * Add a contract to the wallet and sign coins, and send them.
 */
function confirmPay(ws, proposalId, sessionIdOverride) {
    return __awaiter(this, void 0, void 0, function* () {
        logger.trace(`executing confirmPay with proposalId ${proposalId} and sessionIdOverride ${sessionIdOverride}`);
        const proposal = yield query.oneShotGet(ws.db, dbTypes.Stores.proposals, proposalId);
        if (!proposal) {
            throw Error(`proposal with id ${proposalId} not found`);
        }
        const d = proposal.download;
        if (!d) {
            throw Error("proposal is in invalid state");
        }
        const sessionId = sessionIdOverride || proposal.downloadSessionId;
        let purchase = yield query.oneShotGet(ws.db, dbTypes.Stores.purchases, d.contractTermsHash);
        if (purchase) {
            return submitPay(ws, proposalId, sessionId);
        }
        const contractAmount = Amounts.parseOrThrow(d.contractTerms.amount);
        let wireFeeLimit;
        if (!d.contractTerms.max_wire_fee) {
            wireFeeLimit = Amounts.getZero(contractAmount.currency);
        }
        else {
            wireFeeLimit = Amounts.parseOrThrow(d.contractTerms.max_wire_fee);
        }
        const res = yield getCoinsForPayment(ws, {
            allowedAuditors: d.contractTerms.auditors,
            allowedExchanges: d.contractTerms.exchanges,
            depositFeeLimit: Amounts.parseOrThrow(d.contractTerms.max_fee),
            paymentAmount: Amounts.parseOrThrow(d.contractTerms.amount),
            wireFeeAmortization: d.contractTerms.wire_fee_amortization || 1,
            wireFeeLimit,
            wireFeeTime: helpers.extractTalerStampOrThrow(d.contractTerms.timestamp),
            wireMethod: d.contractTerms.wire_method,
        });
        logger.trace("coin selection result", res);
        if (!res) {
            // Should not happen, since checkPay should be called first
            console.log("not confirming payment, insufficient coins");
            throw Error("insufficient balance");
        }
        const sd = yield getSpeculativePayData(ws, proposalId);
        if (!sd) {
            const { exchangeUrl, cds, totalAmount } = res;
            const payCoinInfo = yield ws.cryptoApi.signDeposit(d.contractTerms, cds, totalAmount);
            purchase = yield recordConfirmPay(ws, proposal, payCoinInfo);
        }
        else {
            purchase = yield recordConfirmPay(ws, sd.proposal, sd.payCoinInfo, sd.exchangeUrl);
        }
        return submitPay(ws, proposalId, sessionId);
    });
}
exports.confirmPay = confirmPay;
function getFullRefundFees(ws, refundPermissions) {
    return __awaiter(this, void 0, void 0, function* () {
        if (refundPermissions.length === 0) {
            throw Error("no refunds given");
        }
        const coin0 = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, refundPermissions[0].coin_pub);
        if (!coin0) {
            throw Error("coin not found");
        }
        let feeAcc = Amounts.getZero(Amounts.parseOrThrow(refundPermissions[0].refund_amount).currency);
        const denoms = yield query.oneShotIterIndex(ws.db, dbTypes.Stores.denominations.exchangeBaseUrlIndex, coin0.exchangeBaseUrl).toArray();
        for (const rp of refundPermissions) {
            const coin = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, rp.coin_pub);
            if (!coin) {
                throw Error("coin not found");
            }
            const denom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
                coin0.exchangeBaseUrl,
                coin.denomPub,
            ]);
            if (!denom) {
                throw Error(`denom not found (${coin.denomPub})`);
            }
            // FIXME:  this assumes that the refund already happened.
            // When it hasn't, the refresh cost is inaccurate.  To fix this,
            // we need introduce a flag to tell if a coin was refunded or
            // refreshed normally (and what about incremental refunds?)
            const refundAmount = Amounts.parseOrThrow(rp.refund_amount);
            const refundFee = Amounts.parseOrThrow(rp.refund_fee);
            const refreshCost = refresh_1.getTotalRefreshCost(denoms, denom, Amounts.sub(refundAmount, refundFee).amount);
            feeAcc = Amounts.add(feeAcc, refreshCost, refundFee).amount;
        }
        return feeAcc;
    });
}
exports.getFullRefundFees = getFullRefundFees;
function submitRefunds(ws, proposalId) {
    return __awaiter(this, void 0, void 0, function* () {
        const purchase = yield query.oneShotGet(ws.db, dbTypes.Stores.purchases, proposalId);
        if (!purchase) {
            console.error("not submitting refunds, payment not found:");
            return;
        }
        const pendingKeys = Object.keys(purchase.refundsPending);
        if (pendingKeys.length === 0) {
            return;
        }
        for (const pk of pendingKeys) {
            const perm = purchase.refundsPending[pk];
            const req = {
                coin_pub: perm.coin_pub,
                h_contract_terms: purchase.contractTermsHash,
                merchant_pub: purchase.contractTerms.merchant_pub,
                merchant_sig: perm.merchant_sig,
                refund_amount: perm.refund_amount,
                refund_fee: perm.refund_fee,
                rtransaction_id: perm.rtransaction_id,
            };
            console.log("sending refund permission", perm);
            // FIXME: not correct once we support multiple exchanges per payment
            const exchangeUrl = purchase.payReq.coins[0].exchange_url;
            const reqUrl = new URL("refund", exchangeUrl);
            const resp = yield ws.http.postJson(reqUrl.href, req);
            if (resp.status !== 200) {
                console.error("refund failed", resp);
                continue;
            }
            // Transactionally mark successful refunds as done
            const transformPurchase = (t) => {
                if (!t) {
                    console.warn("purchase not found, not updating refund");
                    return;
                }
                if (t.refundsPending[pk]) {
                    t.refundsDone[pk] = t.refundsPending[pk];
                    delete t.refundsPending[pk];
                }
                return t;
            };
            const transformCoin = (c) => {
                if (!c) {
                    console.warn("coin not found, can't apply refund");
                    return;
                }
                const refundAmount = Amounts.parseOrThrow(perm.refund_amount);
                const refundFee = Amounts.parseOrThrow(perm.refund_fee);
                c.status = dbTypes.CoinStatus.Dirty;
                c.currentAmount = Amounts.add(c.currentAmount, refundAmount).amount;
                c.currentAmount = Amounts.sub(c.currentAmount, refundFee).amount;
                return c;
            };
            yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.purchases, dbTypes.Stores.coins], (tx) => __awaiter(this, void 0, void 0, function* () {
                yield tx.mutate(dbTypes.Stores.purchases, proposalId, transformPurchase);
                yield tx.mutate(dbTypes.Stores.coins, perm.coin_pub, transformCoin);
            }));
            refresh_1.refresh(ws, perm.coin_pub);
        }
        ws.badge.showNotification();
        ws.notifier.notify();
    });
}
function acceptRefundResponse(ws, refundResponse) {
    return __awaiter(this, void 0, void 0, function* () {
        const refundPermissions = refundResponse.refund_permissions;
        if (!refundPermissions.length) {
            console.warn("got empty refund list");
            throw Error("empty refund");
        }
        /**
         * Add refund to purchase if not already added.
         */
        function f(t) {
            if (!t) {
                console.error("purchase not found, not adding refunds");
                return;
            }
            t.timestamp_refund = walletTypes.getTimestampNow();
            for (const perm of refundPermissions) {
                if (!t.refundsPending[perm.merchant_sig] &&
                    !t.refundsDone[perm.merchant_sig]) {
                    t.refundsPending[perm.merchant_sig] = perm;
                }
            }
            return t;
        }
        const hc = refundResponse.h_contract_terms;
        // Add the refund permissions to the purchase within a DB transaction
        yield query.oneShotMutate(ws.db, dbTypes.Stores.purchases, hc, f);
        ws.notifier.notify();
        yield submitRefunds(ws, hc);
        return hc;
    });
}
exports.acceptRefundResponse = acceptRefundResponse;
/**
 * Accept a refund, return the contract hash for the contract
 * that was involved in the refund.
 */
function applyRefund(ws, talerRefundUri) {
    return __awaiter(this, void 0, void 0, function* () {
        const parseResult = taleruri.parseRefundUri(talerRefundUri);
        if (!parseResult) {
            throw Error("invalid refund URI");
        }
        const refundUrl = parseResult.refundUrl;
        logger.trace("processing refund");
        let resp;
        try {
            resp = yield ws.http.get(refundUrl);
        }
        catch (e) {
            console.error("error downloading refund permission", e);
            throw e;
        }
        const refundResponse = talerTypes.MerchantRefundResponse.checked(resp.responseJson);
        return acceptRefundResponse(ws, refundResponse);
    });
}
exports.applyRefund = applyRefund;

});

unwrapExports(pay);
var pay_1 = pay.selectPayCoins;
var pay_2 = pay.abortFailedPayment;
var pay_3 = pay.processDownloadProposal;
var pay_4 = pay.submitPay;
var pay_5 = pay.preparePay;
var pay_6 = pay.confirmPay;
var pay_7 = pay.getFullRefundFees;
var pay_8 = pay.acceptRefundResponse;
var pay_9 = pay.applyRefund;

var asyncMemo = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
Object.defineProperty(exports, "__esModule", { value: true });
class AsyncOpMemo {
    constructor() {
        this.n = 0;
        this.memo = {};
    }
    put(key, p) {
        const n = this.n++;
        this.memo[key] = {
            p,
            n,
            t: new Date().getTime(),
        };
        p.finally(() => {
            const r = this.memo[key];
            if (r && r.n === n) {
                delete this.memo[key];
            }
        });
        return p;
    }
    find(key) {
        const res = this.memo[key];
        const tNow = new Date().getTime();
        if (res && res.t < tNow - 10 * 1000) {
            delete this.memo[key];
            return;
        }
        else if (res) {
            return res.p;
        }
        return;
    }
}
exports.AsyncOpMemo = AsyncOpMemo;

});

unwrapExports(asyncMemo);
var asyncMemo_1 = asyncMemo.AsyncOpMemo;

var history = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });


const Amounts = __importStar(amounts);
/**
 * Retrive the full event history for this wallet.
 */
function getHistory(ws, historyQuery) {
    return __awaiter(this, void 0, void 0, function* () {
        const history = [];
        // FIXME: do pagination instead of generating the full history
        // We uniquely identify history rows via their timestamp.
        // This works as timestamps are guaranteed to be monotonically
        // increasing even
        /*
        const proposals = await oneShotIter(ws.db, Stores.proposals).toArray();
        for (const p of proposals) {
          history.push({
            detail: {
              contractTermsHash: p.contractTermsHash,
              merchantName: p.contractTerms.merchant.name,
            },
            timestamp: p.timestamp,
            type: "claim-order",
            explicit: false,
          });
        }
        */
        const withdrawals = yield query.oneShotIter(ws.db, dbTypes.Stores.withdrawalSession).toArray();
        for (const w of withdrawals) {
            history.push({
                detail: {
                    withdrawalAmount: w.rawWithdrawalAmount,
                },
                timestamp: w.startTimestamp,
                type: "withdraw",
                explicit: false,
            });
        }
        const purchases = yield query.oneShotIter(ws.db, dbTypes.Stores.purchases).toArray();
        for (const p of purchases) {
            history.push({
                detail: {
                    amount: p.contractTerms.amount,
                    contractTermsHash: p.contractTermsHash,
                    fulfillmentUrl: p.contractTerms.fulfillment_url,
                    merchantName: p.contractTerms.merchant.name,
                },
                timestamp: p.timestamp,
                type: "pay",
                explicit: false,
            });
            if (p.timestamp_refund) {
                const contractAmount = Amounts.parseOrThrow(p.contractTerms.amount);
                const amountsPending = Object.keys(p.refundsPending).map(x => Amounts.parseOrThrow(p.refundsPending[x].refund_amount));
                const amountsDone = Object.keys(p.refundsDone).map(x => Amounts.parseOrThrow(p.refundsDone[x].refund_amount));
                const amounts = amountsPending.concat(amountsDone);
                const amount = Amounts.add(Amounts.getZero(contractAmount.currency), ...amounts).amount;
                history.push({
                    detail: {
                        contractTermsHash: p.contractTermsHash,
                        fulfillmentUrl: p.contractTerms.fulfillment_url,
                        merchantName: p.contractTerms.merchant.name,
                        refundAmount: amount,
                    },
                    timestamp: p.timestamp_refund,
                    type: "refund",
                    explicit: false,
                });
            }
        }
        const reserves = yield query.oneShotIter(ws.db, dbTypes.Stores.reserves).toArray();
        for (const r of reserves) {
            const reserveType = r.bankWithdrawStatusUrl ? "taler-bank" : "manual";
            history.push({
                detail: {
                    exchangeBaseUrl: r.exchangeBaseUrl,
                    requestedAmount: Amounts.toString(r.initiallyRequestedAmount),
                    reservePub: r.reservePub,
                    reserveType,
                    bankWithdrawStatusUrl: r.bankWithdrawStatusUrl,
                },
                timestamp: r.created,
                type: "reserve-created",
                explicit: false,
            });
            if (r.timestampConfirmed) {
                history.push({
                    detail: {
                        exchangeBaseUrl: r.exchangeBaseUrl,
                        requestedAmount: Amounts.toString(r.initiallyRequestedAmount),
                        reservePub: r.reservePub,
                        reserveType,
                        bankWithdrawStatusUrl: r.bankWithdrawStatusUrl,
                    },
                    timestamp: r.created,
                    type: "reserve-confirmed",
                    explicit: false,
                });
            }
        }
        const tips = yield query.oneShotIter(ws.db, dbTypes.Stores.tips).toArray();
        for (const tip of tips) {
            history.push({
                detail: {
                    accepted: tip.accepted,
                    amount: tip.amount,
                    merchantBaseUrl: tip.merchantBaseUrl,
                    tipId: tip.merchantTipId,
                },
                timestamp: tip.timestamp,
                explicit: false,
                type: "tip",
            });
        }
        yield query.oneShotIter(ws.db, dbTypes.Stores.exchanges).forEach(exchange => {
            history.push({
                type: "exchange-added",
                explicit: false,
                timestamp: exchange.timestampAdded,
                detail: {
                    exchangeBaseUrl: exchange.baseUrl,
                },
            });
        });
        history.sort((h1, h2) => Math.sign(h1.timestamp.t_ms - h2.timestamp.t_ms));
        return { history };
    });
}
exports.getHistory = getHistory;

});

unwrapExports(history);
var history_1 = history.getHistory;

var pending = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Imports.
 */



function getPendingOperations(ws) {
    return __awaiter(this, void 0, void 0, function* () {
        const pendingOperations = [];
        let minRetryDurationMs = 5000;
        yield query.runWithReadTransaction(ws.db, [
            dbTypes.Stores.exchanges,
            dbTypes.Stores.reserves,
            dbTypes.Stores.refresh,
            dbTypes.Stores.coins,
            dbTypes.Stores.withdrawalSession,
            dbTypes.Stores.proposals,
            dbTypes.Stores.tips,
        ], (tx) => __awaiter(this, void 0, void 0, function* () {
            yield tx.iter(dbTypes.Stores.exchanges).forEach(e => {
                switch (e.updateStatus) {
                    case dbTypes.ExchangeUpdateStatus.FINISHED:
                        if (e.lastError) {
                            pendingOperations.push({
                                type: "bug",
                                message: "Exchange record is in FINISHED state but has lastError set",
                                details: {
                                    exchangeBaseUrl: e.baseUrl,
                                },
                            });
                        }
                        if (!e.details) {
                            pendingOperations.push({
                                type: "bug",
                                message: "Exchange record does not have details, but no update in progress.",
                                details: {
                                    exchangeBaseUrl: e.baseUrl,
                                },
                            });
                        }
                        if (!e.wireInfo) {
                            pendingOperations.push({
                                type: "bug",
                                message: "Exchange record does not have wire info, but no update in progress.",
                                details: {
                                    exchangeBaseUrl: e.baseUrl,
                                },
                            });
                        }
                        break;
                    case dbTypes.ExchangeUpdateStatus.FETCH_KEYS:
                        pendingOperations.push({
                            type: "exchange-update",
                            stage: "fetch-keys",
                            exchangeBaseUrl: e.baseUrl,
                            lastError: e.lastError,
                            reason: e.updateReason || "unknown",
                        });
                        break;
                    case dbTypes.ExchangeUpdateStatus.FETCH_WIRE:
                        pendingOperations.push({
                            type: "exchange-update",
                            stage: "fetch-wire",
                            exchangeBaseUrl: e.baseUrl,
                            lastError: e.lastError,
                            reason: e.updateReason || "unknown",
                        });
                        break;
                    default:
                        pendingOperations.push({
                            type: "bug",
                            message: "Unknown exchangeUpdateStatus",
                            details: {
                                exchangeBaseUrl: e.baseUrl,
                                exchangeUpdateStatus: e.updateStatus,
                            },
                        });
                        break;
                }
            });
            yield tx.iter(dbTypes.Stores.reserves).forEach(reserve => {
                const reserveType = reserve.bankWithdrawStatusUrl
                    ? "taler-bank"
                    : "manual";
                const now = walletTypes.getTimestampNow();
                switch (reserve.reserveStatus) {
                    case dbTypes.ReserveRecordStatus.DORMANT:
                        // nothing to report as pending
                        break;
                    case dbTypes.ReserveRecordStatus.WITHDRAWING:
                    case dbTypes.ReserveRecordStatus.UNCONFIRMED:
                    case dbTypes.ReserveRecordStatus.QUERYING_STATUS:
                    case dbTypes.ReserveRecordStatus.REGISTERING_BANK:
                        pendingOperations.push({
                            type: "reserve",
                            stage: reserve.reserveStatus,
                            timestampCreated: reserve.created,
                            reserveType,
                            reservePub: reserve.reservePub,
                        });
                        if (reserve.created.t_ms < now.t_ms - 5000) {
                            minRetryDurationMs = 500;
                        }
                        else if (reserve.created.t_ms < now.t_ms - 30000) {
                            minRetryDurationMs = 2000;
                        }
                        break;
                    case dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK:
                        pendingOperations.push({
                            type: "reserve",
                            stage: reserve.reserveStatus,
                            timestampCreated: reserve.created,
                            reserveType,
                            reservePub: reserve.reservePub,
                            bankWithdrawConfirmUrl: reserve.bankWithdrawConfirmUrl,
                        });
                        if (reserve.created.t_ms < now.t_ms - 5000) {
                            minRetryDurationMs = 500;
                        }
                        else if (reserve.created.t_ms < now.t_ms - 30000) {
                            minRetryDurationMs = 2000;
                        }
                        break;
                    default:
                        pendingOperations.push({
                            type: "bug",
                            message: "Unknown reserve record status",
                            details: {
                                reservePub: reserve.reservePub,
                                reserveStatus: reserve.reserveStatus,
                            },
                        });
                        break;
                }
            });
            yield tx.iter(dbTypes.Stores.refresh).forEach(r => {
                if (r.finished) {
                    return;
                }
                let refreshStatus;
                if (r.norevealIndex === undefined) {
                    refreshStatus = "melt";
                }
                else {
                    refreshStatus = "reveal";
                }
                pendingOperations.push({
                    type: "refresh",
                    oldCoinPub: r.meltCoinPub,
                    refreshStatus,
                    refreshOutputSize: r.newDenoms.length,
                    refreshSessionId: r.refreshSessionId,
                });
            });
            yield tx.iter(dbTypes.Stores.coins).forEach(coin => {
                if (coin.status == dbTypes.CoinStatus.Dirty) {
                    pendingOperations.push({
                        type: "dirty-coin",
                        coinPub: coin.coinPub,
                    });
                }
            });
            yield tx.iter(dbTypes.Stores.withdrawalSession).forEach(ws => {
                const numCoinsWithdrawn = ws.withdrawn.reduce((a, x) => a + (x ? 1 : 0), 0);
                const numCoinsTotal = ws.withdrawn.length;
                if (numCoinsWithdrawn < numCoinsTotal) {
                    pendingOperations.push({
                        type: "withdraw",
                        numCoinsTotal,
                        numCoinsWithdrawn,
                        source: ws.source,
                        withdrawSessionId: ws.withdrawSessionId,
                    });
                }
            });
            yield tx.iter(dbTypes.Stores.proposals).forEach((proposal) => {
                if (proposal.proposalStatus == dbTypes.ProposalStatus.PROPOSED) {
                    pendingOperations.push({
                        type: "proposal-choice",
                        merchantBaseUrl: proposal.download.contractTerms.merchant_base_url,
                        proposalId: proposal.proposalId,
                        proposalTimestamp: proposal.timestamp,
                    });
                }
                else if (proposal.proposalStatus == dbTypes.ProposalStatus.DOWNLOADING) {
                    pendingOperations.push({
                        type: "proposal-download",
                        merchantBaseUrl: proposal.download.contractTerms.merchant_base_url,
                        proposalId: proposal.proposalId,
                        proposalTimestamp: proposal.timestamp,
                    });
                }
            });
            yield tx.iter(dbTypes.Stores.tips).forEach((tip) => {
                if (tip.accepted && !tip.pickedUp) {
                    pendingOperations.push({
                        type: "tip",
                        merchantBaseUrl: tip.merchantBaseUrl,
                        tipId: tip.tipId,
                        merchantTipId: tip.merchantTipId,
                    });
                }
            });
        }));
        return {
            pendingOperations,
            nextRetryDelay: {
                d_ms: minRetryDurationMs,
            },
        };
    });
}
exports.getPendingOperations = getPendingOperations;

});

unwrapExports(pending);
var pending_1 = pending.getPendingOperations;

var balance = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });


const Amounts = __importStar(amounts);

const logger = new logging.Logger("withdraw.ts");
/**
 * Get detailed balance information, sliced by exchange and by currency.
 */
function getBalances(ws) {
    return __awaiter(this, void 0, void 0, function* () {
        /**
         * Add amount to a balance field, both for
         * the slicing by exchange and currency.
         */
        function addTo(balance, field, amount, exchange) {
            const z = Amounts.getZero(amount.currency);
            const balanceIdentity = {
                available: z,
                paybackAmount: z,
                pendingIncoming: z,
                pendingPayment: z,
                pendingIncomingDirty: z,
                pendingIncomingRefresh: z,
                pendingIncomingWithdraw: z,
            };
            let entryCurr = balance.byCurrency[amount.currency];
            if (!entryCurr) {
                balance.byCurrency[amount.currency] = entryCurr = Object.assign({}, balanceIdentity);
            }
            let entryEx = balance.byExchange[exchange];
            if (!entryEx) {
                balance.byExchange[exchange] = entryEx = Object.assign({}, balanceIdentity);
            }
            entryCurr[field] = Amounts.add(entryCurr[field], amount).amount;
            entryEx[field] = Amounts.add(entryEx[field], amount).amount;
        }
        const balanceStore = {
            byCurrency: {},
            byExchange: {},
        };
        yield query.runWithReadTransaction(ws.db, [dbTypes.Stores.coins, dbTypes.Stores.refresh, dbTypes.Stores.reserves, dbTypes.Stores.purchases, dbTypes.Stores.withdrawalSession], (tx) => __awaiter(this, void 0, void 0, function* () {
            yield tx.iter(dbTypes.Stores.coins).forEach(c => {
                if (c.suspended) {
                    return;
                }
                if (c.status === dbTypes.CoinStatus.Fresh) {
                    addTo(balanceStore, "available", c.currentAmount, c.exchangeBaseUrl);
                }
                if (c.status === dbTypes.CoinStatus.Dirty) {
                    addTo(balanceStore, "pendingIncoming", c.currentAmount, c.exchangeBaseUrl);
                    addTo(balanceStore, "pendingIncomingDirty", c.currentAmount, c.exchangeBaseUrl);
                }
            });
            yield tx.iter(dbTypes.Stores.refresh).forEach(r => {
                // Don't count finished refreshes, since the refresh already resulted
                // in coins being added to the wallet.
                if (r.finished) {
                    return;
                }
                addTo(balanceStore, "pendingIncoming", r.valueOutput, r.exchangeBaseUrl);
                addTo(balanceStore, "pendingIncomingRefresh", r.valueOutput, r.exchangeBaseUrl);
            });
            yield tx.iter(dbTypes.Stores.withdrawalSession).forEach(wds => {
                let w = wds.totalCoinValue;
                for (let i = 0; i < wds.planchets.length; i++) {
                    if (wds.withdrawn[i]) {
                        const p = wds.planchets[i];
                        if (p) {
                            w = Amounts.sub(w, p.coinValue).amount;
                        }
                    }
                }
                addTo(balanceStore, "pendingIncoming", w, wds.exchangeBaseUrl);
            });
            yield tx.iter(dbTypes.Stores.purchases).forEach(t => {
                if (t.finished) {
                    return;
                }
                for (const c of t.payReq.coins) {
                    addTo(balanceStore, "pendingPayment", Amounts.parseOrThrow(c.contribution), c.exchange_url);
                }
            });
        }));
        logger.trace("computed balances:", balanceStore);
        return balanceStore;
    });
}
exports.getBalances = getBalances;

});

unwrapExports(balance);
var balance_1 = balance.getBalances;

var tip = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });




const Amounts = __importStar(amounts);





function getTipStatus(ws, talerTipUri) {
    return __awaiter(this, void 0, void 0, function* () {
        const res = taleruri.parseTipUri(talerTipUri);
        if (!res) {
            throw Error("invalid taler://tip URI");
        }
        const tipStatusUrl = new URL("tip-pickup", res.merchantBaseUrl);
        tipStatusUrl.searchParams.set("tip_id", res.merchantTipId);
        console.log("checking tip status from", tipStatusUrl.href);
        const merchantResp = yield ws.http.get(tipStatusUrl.href);
        console.log("resp:", merchantResp.responseJson);
        const tipPickupStatus = talerTypes.TipPickupGetResponse.checked(merchantResp.responseJson);
        console.log("status", tipPickupStatus);
        let amount = Amounts.parseOrThrow(tipPickupStatus.amount);
        let tipRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.tips, [
            res.merchantTipId,
            res.merchantOrigin,
        ]);
        if (!tipRecord) {
            const withdrawDetails = yield withdraw.getWithdrawDetailsForAmount(ws, tipPickupStatus.exchange_url, amount);
            const tipId = talerCrypto.encodeCrock(talerCrypto.getRandomBytes(32));
            tipRecord = {
                tipId,
                accepted: false,
                amount,
                deadline: helpers.getTalerStampSec(tipPickupStatus.stamp_expire),
                exchangeUrl: tipPickupStatus.exchange_url,
                merchantBaseUrl: res.merchantBaseUrl,
                nextUrl: undefined,
                pickedUp: false,
                planchets: undefined,
                response: undefined,
                timestamp: walletTypes.getTimestampNow(),
                merchantTipId: res.merchantTipId,
                totalFees: Amounts.add(withdrawDetails.overhead, withdrawDetails.withdrawFee).amount,
            };
            yield query.oneShotPut(ws.db, dbTypes.Stores.tips, tipRecord);
        }
        const tipStatus = {
            accepted: !!tipRecord && tipRecord.accepted,
            amount: Amounts.parseOrThrow(tipPickupStatus.amount),
            amountLeft: Amounts.parseOrThrow(tipPickupStatus.amount_left),
            exchangeUrl: tipPickupStatus.exchange_url,
            nextUrl: tipPickupStatus.extra.next_url,
            merchantOrigin: res.merchantOrigin,
            merchantTipId: res.merchantTipId,
            expirationTimestamp: helpers.getTalerStampSec(tipPickupStatus.stamp_expire),
            timestamp: helpers.getTalerStampSec(tipPickupStatus.stamp_created),
            totalFees: tipRecord.totalFees,
            tipId: tipRecord.tipId,
        };
        return tipStatus;
    });
}
exports.getTipStatus = getTipStatus;
function processTip(ws, tipId) {
    return __awaiter(this, void 0, void 0, function* () {
        let tipRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.tips, tipId);
        if (!tipRecord) {
            return;
        }
        if (tipRecord.pickedUp) {
            console.log("tip already picked up");
            return;
        }
        if (!tipRecord.planchets) {
            yield exchanges.updateExchangeFromUrl(ws, tipRecord.exchangeUrl);
            const denomsForWithdraw = yield withdraw.getVerifiedWithdrawDenomList(ws, tipRecord.exchangeUrl, tipRecord.amount);
            const planchets = yield Promise.all(denomsForWithdraw.map(d => ws.cryptoApi.createTipPlanchet(d)));
            yield query.oneShotMutate(ws.db, dbTypes.Stores.tips, tipId, r => {
                if (!r.planchets) {
                    r.planchets = planchets;
                }
                return r;
            });
        }
        tipRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.tips, tipId);
        if (!tipRecord) {
            throw Error("tip not in database");
        }
        if (!tipRecord.planchets) {
            throw Error("invariant violated");
        }
        console.log("got planchets for tip!");
        // Planchets in the form that the merchant expects
        const planchetsDetail = tipRecord.planchets.map(p => ({
            coin_ev: p.coinEv,
            denom_pub_hash: p.denomPubHash,
        }));
        let merchantResp;
        const tipStatusUrl = new URL("tip-pickup", tipRecord.merchantBaseUrl);
        try {
            const req = { planchets: planchetsDetail, tip_id: tipRecord.merchantTipId };
            merchantResp = yield ws.http.postJson(tipStatusUrl.href, req);
            console.log("got merchant resp:", merchantResp);
        }
        catch (e) {
            console.log("tipping failed", e);
            throw e;
        }
        const response = talerTypes.TipResponse.checked(merchantResp.responseJson);
        if (response.reserve_sigs.length !== tipRecord.planchets.length) {
            throw Error("number of tip responses does not match requested planchets");
        }
        const planchets = [];
        for (let i = 0; i < tipRecord.planchets.length; i++) {
            const tipPlanchet = tipRecord.planchets[i];
            const planchet = {
                blindingKey: tipPlanchet.blindingKey,
                coinEv: tipPlanchet.coinEv,
                coinPriv: tipPlanchet.coinPriv,
                coinPub: tipPlanchet.coinPub,
                coinValue: tipPlanchet.coinValue,
                denomPub: tipPlanchet.denomPub,
                denomPubHash: tipPlanchet.denomPubHash,
                reservePub: response.reserve_pub,
                withdrawSig: response.reserve_sigs[i].reserve_sig,
                isFromTip: true,
            };
            planchets.push(planchet);
        }
        const withdrawalSessionId = talerCrypto.encodeCrock(talerCrypto.getRandomBytes(32));
        const withdrawalSession = {
            denoms: planchets.map((x) => x.denomPub),
            exchangeBaseUrl: tipRecord.exchangeUrl,
            planchets: planchets,
            source: {
                type: "tip",
                tipId: tipRecord.tipId,
            },
            startTimestamp: walletTypes.getTimestampNow(),
            withdrawSessionId: withdrawalSessionId,
            rawWithdrawalAmount: tipRecord.amount,
            withdrawn: planchets.map((x) => false),
            totalCoinValue: Amounts.sum(planchets.map((p) => p.coinValue)).amount,
        };
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.tips, dbTypes.Stores.withdrawalSession], (tx) => __awaiter(this, void 0, void 0, function* () {
            const tr = yield tx.get(dbTypes.Stores.tips, tipId);
            if (!tr) {
                return;
            }
            if (tr.pickedUp) {
                return;
            }
            tr.pickedUp = true;
            yield tx.put(dbTypes.Stores.tips, tr);
            yield tx.put(dbTypes.Stores.withdrawalSession, withdrawalSession);
        }));
        yield withdraw.processWithdrawSession(ws, withdrawalSessionId);
        ws.notifier.notify();
        ws.badge.showNotification();
        return;
    });
}
exports.processTip = processTip;
function acceptTip(ws, tipId) {
    return __awaiter(this, void 0, void 0, function* () {
        const tipRecord = yield query.oneShotGet(ws.db, dbTypes.Stores.tips, tipId);
        if (!tipRecord) {
            console.log("tip not found");
            return;
        }
        tipRecord.accepted = true;
        yield query.oneShotPut(ws.db, dbTypes.Stores.tips, tipRecord);
        yield processTip(ws, tipId);
        return;
    });
}
exports.acceptTip = acceptTip;

});

unwrapExports(tip);
var tip_1 = tip.getTipStatus;
var tip_2 = tip.processTip;
var tip_3 = tip.acceptTip;

var _return = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });


const Amounts = __importStar(amounts);



const logger = new logging.Logger("return.ts");
function getCoinsForReturn(ws, exchangeBaseUrl, amount) {
    return __awaiter(this, void 0, void 0, function* () {
        const exchange = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, exchangeBaseUrl);
        if (!exchange) {
            throw Error(`Exchange ${exchangeBaseUrl} not known to the wallet`);
        }
        const coins = yield query.oneShotIterIndex(ws.db, dbTypes.Stores.coins.exchangeBaseUrlIndex, exchange.baseUrl).toArray();
        if (!coins || !coins.length) {
            return [];
        }
        const denoms = yield query.oneShotIterIndex(ws.db, dbTypes.Stores.denominations.exchangeBaseUrlIndex, exchange.baseUrl).toArray();
        // Denomination of the first coin, we assume that all other
        // coins have the same currency
        const firstDenom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
            exchange.baseUrl,
            coins[0].denomPub,
        ]);
        if (!firstDenom) {
            throw Error("db inconsistent");
        }
        const currency = firstDenom.value.currency;
        const cds = [];
        for (const coin of coins) {
            const denom = yield query.oneShotGet(ws.db, dbTypes.Stores.denominations, [
                exchange.baseUrl,
                coin.denomPub,
            ]);
            if (!denom) {
                throw Error("db inconsistent");
            }
            if (denom.value.currency !== currency) {
                console.warn(`same pubkey for different currencies at exchange ${exchange.baseUrl}`);
                continue;
            }
            if (coin.suspended) {
                continue;
            }
            if (coin.status !== dbTypes.CoinStatus.Fresh) {
                continue;
            }
            cds.push({ coin, denom });
        }
        const res = pay.selectPayCoins(denoms, cds, amount, amount);
        if (res) {
            return res.cds;
        }
        return undefined;
    });
}
/**
 * Trigger paying coins back into the user's account.
 */
function returnCoins(ws, req) {
    return __awaiter(this, void 0, void 0, function* () {
        logger.trace("got returnCoins request", req);
        const wireType = req.senderWire.type;
        logger.trace("wireType", wireType);
        if (!wireType || typeof wireType !== "string") {
            console.error(`wire type must be a non-empty string, not ${wireType}`);
            return;
        }
        const stampSecNow = Math.floor(new Date().getTime() / 1000);
        const exchange = yield query.oneShotGet(ws.db, dbTypes.Stores.exchanges, req.exchange);
        if (!exchange) {
            console.error(`Exchange ${req.exchange} not known to the wallet`);
            return;
        }
        const exchangeDetails = exchange.details;
        if (!exchangeDetails) {
            throw Error("exchange information needs to be updated first.");
        }
        logger.trace("selecting coins for return:", req);
        const cds = yield getCoinsForReturn(ws, req.exchange, req.amount);
        logger.trace(cds);
        if (!cds) {
            throw Error("coin return impossible, can't select coins");
        }
        const { priv, pub } = yield ws.cryptoApi.createEddsaKeypair();
        const wireHash = yield ws.cryptoApi.hashString(helpers.canonicalJson(req.senderWire));
        const contractTerms = {
            H_wire: wireHash,
            amount: Amounts.toString(req.amount),
            auditors: [],
            exchanges: [
                { master_pub: exchangeDetails.masterPublicKey, url: exchange.baseUrl },
            ],
            extra: {},
            fulfillment_url: "",
            locations: [],
            max_fee: Amounts.toString(req.amount),
            merchant: {},
            merchant_pub: pub,
            order_id: "none",
            pay_deadline: `/Date(${stampSecNow + 30 * 5})/`,
            wire_transfer_deadline: `/Date(${stampSecNow + 60 * 5})/`,
            merchant_base_url: "taler://return-to-account",
            products: [],
            refund_deadline: `/Date(${stampSecNow + 60 * 5})/`,
            timestamp: `/Date(${stampSecNow})/`,
            wire_method: wireType,
        };
        const contractTermsHash = yield ws.cryptoApi.hashString(helpers.canonicalJson(contractTerms));
        const payCoinInfo = yield ws.cryptoApi.signDeposit(contractTerms, cds, Amounts.parseOrThrow(contractTerms.amount));
        logger.trace("pci", payCoinInfo);
        const coins = payCoinInfo.sigs.map(s => ({ coinPaySig: s }));
        const coinsReturnRecord = {
            coins,
            contractTerms,
            contractTermsHash,
            exchange: exchange.baseUrl,
            merchantPriv: priv,
            wire: req.senderWire,
        };
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.coinsReturns, dbTypes.Stores.coins], (tx) => __awaiter(this, void 0, void 0, function* () {
            yield tx.put(dbTypes.Stores.coinsReturns, coinsReturnRecord);
            for (let c of payCoinInfo.updatedCoins) {
                yield tx.put(dbTypes.Stores.coins, c);
            }
        }));
        ws.badge.showNotification();
        ws.notifier.notify();
        depositReturnedCoins(ws, coinsReturnRecord);
    });
}
exports.returnCoins = returnCoins;
function depositReturnedCoins(ws, coinsReturnRecord) {
    return __awaiter(this, void 0, void 0, function* () {
        for (const c of coinsReturnRecord.coins) {
            if (c.depositedSig) {
                continue;
            }
            const req = {
                H_wire: coinsReturnRecord.contractTerms.H_wire,
                coin_pub: c.coinPaySig.coin_pub,
                coin_sig: c.coinPaySig.coin_sig,
                contribution: c.coinPaySig.contribution,
                denom_pub: c.coinPaySig.denom_pub,
                h_contract_terms: coinsReturnRecord.contractTermsHash,
                merchant_pub: coinsReturnRecord.contractTerms.merchant_pub,
                pay_deadline: coinsReturnRecord.contractTerms.pay_deadline,
                refund_deadline: coinsReturnRecord.contractTerms.refund_deadline,
                timestamp: coinsReturnRecord.contractTerms.timestamp,
                ub_sig: c.coinPaySig.ub_sig,
                wire: coinsReturnRecord.wire,
                wire_transfer_deadline: coinsReturnRecord.contractTerms.pay_deadline,
            };
            logger.trace("req", req);
            const reqUrl = new URL("deposit", coinsReturnRecord.exchange);
            const resp = yield ws.http.postJson(reqUrl.href, req);
            if (resp.status !== 200) {
                console.error("deposit failed due to status code", resp);
                continue;
            }
            const respJson = resp.responseJson;
            if (respJson.status !== "DEPOSIT_OK") {
                console.error("deposit failed", resp);
                continue;
            }
            if (!respJson.sig) {
                console.error("invalid 'sig' field", resp);
                continue;
            }
            // FIXME: verify signature
            // For every successful deposit, we replace the old record with an updated one
            const currentCrr = yield query.oneShotGet(ws.db, dbTypes.Stores.coinsReturns, coinsReturnRecord.contractTermsHash);
            if (!currentCrr) {
                console.error("database inconsistent");
                continue;
            }
            for (const nc of currentCrr.coins) {
                if (nc.coinPaySig.coin_pub === c.coinPaySig.coin_pub) {
                    nc.depositedSig = respJson.sig;
                }
            }
            yield query.oneShotPut(ws.db, dbTypes.Stores.coinsReturns, currentCrr);
            ws.notifier.notify();
        }
    });
}

});

unwrapExports(_return);
var _return_1 = _return.returnCoins;

var payback_1 = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Imports.
 */





const logger = new logging.Logger("payback.ts");
function payback(ws, coinPub) {
    return __awaiter(this, void 0, void 0, function* () {
        let coin = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, coinPub);
        if (!coin) {
            throw Error(`Coin ${coinPub} not found, can't request payback`);
        }
        const reservePub = coin.reservePub;
        if (!reservePub) {
            throw Error(`Can't request payback for a refreshed coin`);
        }
        const reserve = yield query.oneShotGet(ws.db, dbTypes.Stores.reserves, reservePub);
        if (!reserve) {
            throw Error(`Reserve of coin ${coinPub} not found`);
        }
        switch (coin.status) {
            case dbTypes.CoinStatus.Dormant:
                throw Error(`Can't do payback for coin ${coinPub} since it's dormant`);
        }
        coin.status = dbTypes.CoinStatus.Dormant;
        // Even if we didn't get the payback yet, we suspend withdrawal, since
        // technically we might update reserve status before we get the response
        // from the reserve for the payback request.
        reserve.hasPayback = true;
        yield query.runWithWriteTransaction(ws.db, [dbTypes.Stores.coins, dbTypes.Stores.reserves], (tx) => __awaiter(this, void 0, void 0, function* () {
            yield tx.put(dbTypes.Stores.coins, coin);
            yield tx.put(dbTypes.Stores.reserves, reserve);
        }));
        ws.notifier.notify();
        const paybackRequest = yield ws.cryptoApi.createPaybackRequest(coin);
        const reqUrl = new URL("payback", coin.exchangeBaseUrl);
        const resp = yield ws.http.postJson(reqUrl.href, paybackRequest);
        if (resp.status !== 200) {
            throw Error();
        }
        const paybackConfirmation = talerTypes.PaybackConfirmation.checked(resp.responseJson);
        if (paybackConfirmation.reserve_pub !== coin.reservePub) {
            throw Error(`Coin's reserve doesn't match reserve on payback`);
        }
        coin = yield query.oneShotGet(ws.db, dbTypes.Stores.coins, coinPub);
        if (!coin) {
            throw Error(`Coin ${coinPub} not found, can't confirm payback`);
        }
        coin.status = dbTypes.CoinStatus.Dormant;
        yield query.oneShotPut(ws.db, dbTypes.Stores.coins, coin);
        ws.notifier.notify();
        yield exchanges.updateExchangeFromUrl(ws, coin.exchangeBaseUrl, true);
    });
}
exports.payback = payback;

});

unwrapExports(payback_1);
var payback_2 = payback_1.payback;

var wallet = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2015-2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * High-level wallet operations that should be indepentent from the underlying
 * browser extension interface.
 */
/**
 * Imports.
 */


const Amounts = __importStar(amounts);








const reserves_2 = reserves;

const withdraw_2 = withdraw;








/**
 * Wallet protocol version spoken with the exchange
 * and merchant.
 *
 * Uses libtool's current:revision:age versioning.
 */
exports.WALLET_PROTOCOL_VERSION = "3:0:0";
exports.WALLET_CACHE_BREAKER_CLIENT_VERSION = "3";
const builtinCurrencies = [
    {
        auditors: [
            {
                auditorPub: "BW9DC48PHQY4NH011SHHX36DZZ3Q22Y6X7FZ1VD1CMZ2PTFZ6PN0",
                baseUrl: "https://auditor.demo.taler.net/",
                expirationStamp: new Date(2027, 1).getTime(),
            },
        ],
        exchanges: [],
        fractionalDigits: 2,
        name: "KUDOS",
    },
];
/**
 * This error is thrown when an
 */
class OperationFailedAndReportedError extends Error {
    constructor(message) {
        super(message);
        // Set the prototype explicitly.
        Object.setPrototypeOf(this, OperationFailedAndReportedError.prototype);
    }
}
exports.OperationFailedAndReportedError = OperationFailedAndReportedError;
const logger = new logging.Logger("wallet.ts");
/**
 * The platform-independent wallet implementation.
 */
class Wallet {
    constructor(db, http, badge, notifier, cryptoWorkerFactory) {
        this.timerGroup = new timer.TimerGroup();
        this.latch = new promiseUtils.AsyncCondition();
        this.stopped = false;
        this.ws = {
            badge,
            cachedNextUrl: {},
            cryptoApi: new cryptoApi.CryptoApi(cryptoWorkerFactory),
            db,
            http,
            notifier,
            speculativePayData: undefined,
            memoProcessReserve: new asyncMemo.AsyncOpMemo(),
            memoMakePlanchet: new asyncMemo.AsyncOpMemo(),
        };
    }
    get db() {
        return this.ws.db;
    }
    get badge() {
        return this.ws.badge;
    }
    get cryptoApi() {
        return this.ws.cryptoApi;
    }
    get notifier() {
        return this.ws.notifier;
    }
    getExchangePaytoUri(exchangeBaseUrl, supportedTargetTypes) {
        return exchanges.getExchangePaytoUri(this.ws, exchangeBaseUrl, supportedTargetTypes);
    }
    getWithdrawDetailsForAmount(baseUrl, amount) {
        return withdraw.getWithdrawDetailsForAmount(this.ws, baseUrl, amount);
    }
    /**
     * Execute one operation based on the pending operation info record.
     */
    processOnePendingOperation(pending, forceNow = false) {
        return __awaiter(this, void 0, void 0, function* () {
            switch (pending.type) {
                case "bug":
                    return;
                case "dirty-coin":
                    yield refresh_1.refresh(this.ws, pending.coinPub);
                    break;
                case "exchange-update":
                    yield exchanges.updateExchangeFromUrl(this.ws, pending.exchangeBaseUrl);
                    break;
                case "refresh":
                    yield refresh_1.processRefreshSession(this.ws, pending.refreshSessionId);
                    break;
                case "reserve":
                    yield reserves.processReserve(this.ws, pending.reservePub);
                    break;
                case "withdraw":
                    yield withdraw_2.processWithdrawSession(this.ws, pending.withdrawSessionId);
                    break;
                case "proposal-choice":
                    // Nothing to do, user needs to accept/reject
                    break;
                case "proposal-download":
                    yield pay.processDownloadProposal(this.ws, pending.proposalId);
                    break;
                case "tip":
                    yield tip.processTip(this.ws, pending.tipId);
                    break;
                case "pay":
                    break;
                default:
                    assertUnreachable_1.assertUnreachable(pending);
            }
        });
    }
    /**
     * Process pending operations.
     */
    runPending(forceNow = false) {
        return __awaiter(this, void 0, void 0, function* () {
            const pendingOpsResponse = yield this.getPendingOperations();
            for (const p of pendingOpsResponse.pendingOperations) {
                try {
                    yield this.processOnePendingOperation(p, forceNow);
                }
                catch (e) {
                    console.error(e);
                }
            }
        });
    }
    /**
     * Process pending operations and wait for scheduled operations in
     * a loop until the wallet is stopped explicitly.
     */
    runLoopScheduledRetries() {
        return __awaiter(this, void 0, void 0, function* () {
            while (!this.stopped) {
                console.log("running wallet retry loop iteration");
                let pending = yield this.getPendingOperations();
                console.log("waiting for", pending.nextRetryDelay);
                const timeout = this.timerGroup.resolveAfter(pending.nextRetryDelay.d_ms);
                yield Promise.race([timeout, this.latch.wait()]);
                pending = yield this.getPendingOperations();
                for (const p of pending.pendingOperations) {
                    try {
                        this.processOnePendingOperation(p);
                    }
                    catch (e) {
                        console.error(e);
                    }
                }
            }
        });
    }
    /**
     * Run until all coins have been withdrawn from the given reserve,
     * or an error has occured.
     */
    runUntilReserveDepleted(reservePub) {
        return __awaiter(this, void 0, void 0, function* () {
            while (true) {
                const r = yield this.getPendingOperations();
                const allPending = r.pendingOperations;
                const relevantPending = allPending.filter(x => {
                    switch (x.type) {
                        case "reserve":
                            return x.reservePub === reservePub;
                        case "withdraw":
                            return (x.source.type === "reserve" && x.source.reservePub === reservePub);
                        default:
                            return false;
                    }
                });
                if (relevantPending.length === 0) {
                    return;
                }
                for (const p of relevantPending) {
                    yield this.processOnePendingOperation(p);
                }
            }
        });
    }
    /**
     * Insert the hard-coded defaults for exchanges, coins and
     * auditors into the database, unless these defaults have
     * already been applied.
     */
    fillDefaults() {
        return __awaiter(this, void 0, void 0, function* () {
            yield query.runWithWriteTransaction(this.db, [dbTypes.Stores.config, dbTypes.Stores.currencies], (tx) => __awaiter(this, void 0, void 0, function* () {
                let applied = false;
                yield tx.iter(dbTypes.Stores.config).forEach(x => {
                    if (x.key == "currencyDefaultsApplied" && x.value == true) {
                        applied = true;
                    }
                });
                if (!applied) {
                    for (let c of builtinCurrencies) {
                        yield tx.put(dbTypes.Stores.currencies, c);
                    }
                }
            }));
        });
    }
    /**
     * Check if a payment for the given taler://pay/ URI is possible.
     *
     * If the payment is possible, the signature are already generated but not
     * yet send to the merchant.
     */
    preparePay(talerPayUri) {
        return __awaiter(this, void 0, void 0, function* () {
            return pay.preparePay(this.ws, talerPayUri);
        });
    }
    /**
     * Refresh all dirty coins.
     * The returned promise resolves only after all refresh
     * operations have completed.
     */
    refreshDirtyCoins() {
        return __awaiter(this, void 0, void 0, function* () {
            let n = 0;
            const coins = yield query.oneShotIter(this.db, dbTypes.Stores.coins).toArray();
            for (let coin of coins) {
                if (coin.status == dbTypes.CoinStatus.Dirty) {
                    try {
                        yield this.refresh(coin.coinPub);
                    }
                    catch (e) {
                        console.log("error during refresh");
                    }
                    n += 1;
                }
            }
            return { numRefreshed: n };
        });
    }
    /**
     * Add a contract to the wallet and sign coins, and send them.
     */
    confirmPay(proposalId, sessionIdOverride) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                return yield pay.confirmPay(this.ws, proposalId, sessionIdOverride);
            }
            finally {
                this.latch.trigger();
            }
        });
    }
    /**
     * First fetch information requred to withdraw from the reserve,
     * then deplete the reserve, withdrawing coins until it is empty.
     *
     * The returned promise resolves once the reserve is set to the
     * state DORMANT.
     */
    processReserve(reservePub) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                return yield reserves.processReserve(this.ws, reservePub);
            }
            finally {
                this.latch.trigger();
            }
        });
    }
    /**
     * Create a reserve, but do not flag it as confirmed yet.
     *
     * Adds the corresponding exchange as a trusted exchange if it is neither
     * audited nor trusted already.
     */
    createReserve(req) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                return reserves_2.createReserve(this.ws, req);
            }
            finally {
                this.latch.trigger();
            }
        });
    }
    /**
     * Mark an existing reserve as confirmed.  The wallet will start trying
     * to withdraw from that reserve.  This may not immediately succeed,
     * since the exchange might not know about the reserve yet, even though the
     * bank confirmed its creation.
     *
     * A confirmed reserve should be shown to the user in the UI, while
     * an unconfirmed reserve should be hidden.
     */
    confirmReserve(req) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                return reserves_2.confirmReserve(this.ws, req);
            }
            finally {
                this.latch.trigger();
            }
        });
    }
    /**
     * Check if and how an exchange is trusted and/or audited.
     */
    getExchangeTrust(exchangeInfo) {
        return __awaiter(this, void 0, void 0, function* () {
            return exchanges.getExchangeTrust(this.ws, exchangeInfo);
        });
    }
    getWithdrawDetailsForUri(talerWithdrawUri, maybeSelectedExchange) {
        return __awaiter(this, void 0, void 0, function* () {
            return withdraw.getWithdrawDetailsForUri(this.ws, talerWithdrawUri, maybeSelectedExchange);
        });
    }
    /**
     * Update or add exchange DB entry by fetching the /keys and /wire information.
     * Optionally link the reserve entry to the new or existing
     * exchange entry in then DB.
     */
    updateExchangeFromUrl(baseUrl, force = false) {
        return __awaiter(this, void 0, void 0, function* () {
            return exchanges.updateExchangeFromUrl(this.ws, baseUrl, force);
        });
    }
    /**
     * Get detailed balance information, sliced by exchange and by currency.
     */
    getBalances() {
        return __awaiter(this, void 0, void 0, function* () {
            return balance.getBalances(this.ws);
        });
    }
    refresh(oldCoinPub, force = false) {
        return __awaiter(this, void 0, void 0, function* () {
            return refresh_1.refresh(this.ws, oldCoinPub, force);
        });
    }
    findExchange(exchangeBaseUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield query.oneShotGet(this.db, dbTypes.Stores.exchanges, exchangeBaseUrl);
        });
    }
    /**
     * Retrive the full event history for this wallet.
     */
    getHistory(historyQuery) {
        return __awaiter(this, void 0, void 0, function* () {
            return history.getHistory(this.ws, historyQuery);
        });
    }
    getPendingOperations() {
        return __awaiter(this, void 0, void 0, function* () {
            return pending.getPendingOperations(this.ws);
        });
    }
    getDenoms(exchangeUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            const denoms = yield query.oneShotIterIndex(this.db, dbTypes.Stores.denominations.exchangeBaseUrlIndex, exchangeUrl).toArray();
            return denoms;
        });
    }
    getProposal(proposalId) {
        return __awaiter(this, void 0, void 0, function* () {
            const proposal = yield query.oneShotGet(this.db, dbTypes.Stores.proposals, proposalId);
            return proposal;
        });
    }
    getExchanges() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield query.oneShotIter(this.db, dbTypes.Stores.exchanges).toArray();
        });
    }
    getCurrencies() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield query.oneShotIter(this.db, dbTypes.Stores.currencies).toArray();
        });
    }
    updateCurrency(currencyRecord) {
        return __awaiter(this, void 0, void 0, function* () {
            logger.trace("updating currency to", currencyRecord);
            yield query.oneShotPut(this.db, dbTypes.Stores.currencies, currencyRecord);
            this.notifier.notify();
        });
    }
    getReserves(exchangeBaseUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield query.oneShotIter(this.db, dbTypes.Stores.reserves).filter(r => r.exchangeBaseUrl === exchangeBaseUrl);
        });
    }
    getCoinsForExchange(exchangeBaseUrl) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield query.oneShotIter(this.db, dbTypes.Stores.coins).filter(c => c.exchangeBaseUrl === exchangeBaseUrl);
        });
    }
    getCoins() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield query.oneShotIter(this.db, dbTypes.Stores.coins).toArray();
        });
    }
    payback(coinPub) {
        return __awaiter(this, void 0, void 0, function* () {
            return payback_1.payback(this.ws, coinPub);
        });
    }
    getPaybackReserves() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield query.oneShotIter(this.db, dbTypes.Stores.reserves).filter(r => r.hasPayback);
        });
    }
    /**
     * Stop ongoing processing.
     */
    stop() {
        this.stopped = true;
        this.timerGroup.stopCurrentAndFutureTimers();
        this.cryptoApi.stop();
    }
    getSenderWireInfos() {
        return __awaiter(this, void 0, void 0, function* () {
            const m = {};
            yield query.oneShotIter(this.db, dbTypes.Stores.exchanges).forEach(x => {
                const wi = x.wireInfo;
                if (!wi) {
                    return;
                }
                const s = (m[x.baseUrl] = m[x.baseUrl] || new Set());
                Object.keys(wi.feesForType).map(k => s.add(k));
            });
            const exchangeWireTypes = {};
            Object.keys(m).map(e => {
                exchangeWireTypes[e] = Array.from(m[e]);
            });
            const senderWiresSet = new Set();
            yield query.oneShotIter(this.db, dbTypes.Stores.senderWires).forEach(x => {
                senderWiresSet.add(x.paytoUri);
            });
            const senderWires = Array.from(senderWiresSet);
            return {
                exchangeWireTypes,
                senderWires,
            };
        });
    }
    /**
     * Trigger paying coins back into the user's account.
     */
    returnCoins(req) {
        return __awaiter(this, void 0, void 0, function* () {
            return _return.returnCoins(this.ws, req);
        });
    }
    /**
     * Accept a refund, return the contract hash for the contract
     * that was involved in the refund.
     */
    applyRefund(talerRefundUri) {
        return __awaiter(this, void 0, void 0, function* () {
            return pay.applyRefund(this.ws, talerRefundUri);
        });
    }
    getPurchase(contractTermsHash) {
        return __awaiter(this, void 0, void 0, function* () {
            return query.oneShotGet(this.db, dbTypes.Stores.purchases, contractTermsHash);
        });
    }
    getFullRefundFees(refundPermissions) {
        return __awaiter(this, void 0, void 0, function* () {
            return pay.getFullRefundFees(this.ws, refundPermissions);
        });
    }
    acceptTip(talerTipUri) {
        return __awaiter(this, void 0, void 0, function* () {
            return tip.acceptTip(this.ws, talerTipUri);
        });
    }
    getTipStatus(talerTipUri) {
        return __awaiter(this, void 0, void 0, function* () {
            return tip.getTipStatus(this.ws, talerTipUri);
        });
    }
    abortFailedPayment(contractTermsHash) {
        return __awaiter(this, void 0, void 0, function* () {
            return pay.abortFailedPayment(this.ws, contractTermsHash);
        });
    }
    handleNotifyReserve() {
        return __awaiter(this, void 0, void 0, function* () {
            const reserves = yield query.oneShotIter(this.db, dbTypes.Stores.reserves).toArray();
            for (const r of reserves) {
                if (r.reserveStatus === dbTypes.ReserveRecordStatus.WAIT_CONFIRM_BANK) {
                    try {
                        this.processReserve(r.reservePub);
                    }
                    catch (e) {
                        console.error(e);
                    }
                }
            }
        });
    }
    /**
     * Remove unreferenced / expired data from the wallet's database
     * based on the current system time.
     */
    collectGarbage() {
        return __awaiter(this, void 0, void 0, function* () {
            // FIXME(#5845)
            // We currently do not garbage-collect the wallet database.  This might change
            // after the feature has been properly re-designed, and we have come up with a
            // strategy to test it.
        });
    }
    /**
     * Get information about a withdrawal from
     * a taler://withdraw URI.
     */
    getWithdrawalInfo(talerWithdrawUri) {
        return __awaiter(this, void 0, void 0, function* () {
            return withdraw.getWithdrawalInfo(this.ws, talerWithdrawUri);
        });
    }
    acceptWithdrawal(talerWithdrawUri, selectedExchange) {
        return __awaiter(this, void 0, void 0, function* () {
            return withdraw.acceptWithdrawal(this.ws, talerWithdrawUri, selectedExchange);
        });
    }
    getPurchaseDetails(hc) {
        return __awaiter(this, void 0, void 0, function* () {
            const purchase = yield query.oneShotGet(this.db, dbTypes.Stores.purchases, hc);
            if (!purchase) {
                throw Error("unknown purchase");
            }
            const refundsDoneAmounts = Object.values(purchase.refundsDone).map(x => Amounts.parseOrThrow(x.refund_amount));
            const refundsPendingAmounts = Object.values(purchase.refundsPending).map(x => Amounts.parseOrThrow(x.refund_amount));
            const totalRefundAmount = Amounts.sum([
                ...refundsDoneAmounts,
                ...refundsPendingAmounts,
            ]).amount;
            const refundsDoneFees = Object.values(purchase.refundsDone).map(x => Amounts.parseOrThrow(x.refund_amount));
            const refundsPendingFees = Object.values(purchase.refundsPending).map(x => Amounts.parseOrThrow(x.refund_amount));
            const totalRefundFees = Amounts.sum([
                ...refundsDoneFees,
                ...refundsPendingFees,
            ]).amount;
            const totalFees = totalRefundFees;
            return {
                contractTerms: purchase.contractTerms,
                hasRefund: purchase.timestamp_refund !== undefined,
                totalRefundAmount: totalRefundAmount,
                totalRefundAndRefreshFees: totalFees,
            };
        });
    }
    clearNotification() {
        this.badge.clearNotification();
    }
    benchmarkCrypto(repetitions) {
        return this.cryptoApi.benchmark(repetitions);
    }
}
exports.Wallet = Wallet;

});

unwrapExports(wallet);
var wallet_1 = wallet.WALLET_PROTOCOL_VERSION;
var wallet_2 = wallet.WALLET_CACHE_BREAKER_CLIENT_VERSION;
var wallet_3 = wallet.OperationFailedAndReportedError;
var wallet_4 = wallet.Wallet;

var errors = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
/* tslint:disable: max-classes-per-file max-line-length */
var messages = {
    AbortError: "A request was aborted, for example through a call to IDBTransaction.abort.",
    ConstraintError: "A mutation operation in the transaction failed because a constraint was not satisfied. For example, an object such as an object store or index already exists and a request attempted to create a new one.",
    DataCloneError: "The data being stored could not be cloned by the internal structured cloning algorithm.",
    DataError: "Data provided to an operation does not meet requirements.",
    InvalidAccessError: "An invalid operation was performed on an object. For example transaction creation attempt was made, but an empty scope was provided.",
    InvalidStateError: "An operation was called on an object on which it is not allowed or at a time when it is not allowed. Also occurs if a request is made on a source object that has been deleted or removed. Use TransactionInactiveError or ReadOnlyError when possible, as they are more specific variations of InvalidStateError.",
    NotFoundError: "The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened.",
    ReadOnlyError: 'The mutating operation was attempted in a "readonly" transaction.',
    TransactionInactiveError: "A request was placed against a transaction which is currently not active, or which is finished.",
    VersionError: "An attempt was made to open a database using a lower version than the existing version.",
};
var AbortError = /** @class */ (function (_super) {
    __extends(AbortError, _super);
    function AbortError(message) {
        if (message === void 0) { message = messages.AbortError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, ConstraintError.prototype);
        _this.name = "AbortError";
        _this.message = message;
        return _this;
    }
    return AbortError;
}(Error));
exports.AbortError = AbortError;
var ConstraintError = /** @class */ (function (_super) {
    __extends(ConstraintError, _super);
    function ConstraintError(message) {
        if (message === void 0) { message = messages.ConstraintError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, ConstraintError.prototype);
        _this.name = "ConstraintError";
        _this.message = message;
        return _this;
    }
    return ConstraintError;
}(Error));
exports.ConstraintError = ConstraintError;
var DataCloneError = /** @class */ (function (_super) {
    __extends(DataCloneError, _super);
    function DataCloneError(message) {
        if (message === void 0) { message = messages.DataCloneError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, DataCloneError.prototype);
        _this.name = "DataCloneError";
        _this.message = message;
        return _this;
    }
    return DataCloneError;
}(Error));
exports.DataCloneError = DataCloneError;
var DataError = /** @class */ (function (_super) {
    __extends(DataError, _super);
    function DataError(message) {
        if (message === void 0) { message = messages.DataError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, DataError.prototype);
        _this.name = "DataError";
        _this.message = message;
        return _this;
    }
    return DataError;
}(Error));
exports.DataError = DataError;
var InvalidAccessError = /** @class */ (function (_super) {
    __extends(InvalidAccessError, _super);
    function InvalidAccessError(message) {
        if (message === void 0) { message = messages.InvalidAccessError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, InvalidAccessError.prototype);
        _this.name = "InvalidAccessError";
        _this.message = message;
        return _this;
    }
    return InvalidAccessError;
}(Error));
exports.InvalidAccessError = InvalidAccessError;
var InvalidStateError = /** @class */ (function (_super) {
    __extends(InvalidStateError, _super);
    function InvalidStateError(message) {
        if (message === void 0) { message = messages.InvalidStateError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, InvalidStateError.prototype);
        _this.name = "InvalidStateError";
        _this.message = message;
        return _this;
    }
    return InvalidStateError;
}(Error));
exports.InvalidStateError = InvalidStateError;
var NotFoundError = /** @class */ (function (_super) {
    __extends(NotFoundError, _super);
    function NotFoundError(message) {
        if (message === void 0) { message = messages.NotFoundError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, NotFoundError.prototype);
        _this.name = "NotFoundError";
        _this.message = message;
        return _this;
    }
    return NotFoundError;
}(Error));
exports.NotFoundError = NotFoundError;
var ReadOnlyError = /** @class */ (function (_super) {
    __extends(ReadOnlyError, _super);
    function ReadOnlyError(message) {
        if (message === void 0) { message = messages.ReadOnlyError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, ReadOnlyError.prototype);
        _this.name = "ReadOnlyError";
        _this.message = message;
        return _this;
    }
    return ReadOnlyError;
}(Error));
exports.ReadOnlyError = ReadOnlyError;
var TransactionInactiveError = /** @class */ (function (_super) {
    __extends(TransactionInactiveError, _super);
    function TransactionInactiveError(message) {
        if (message === void 0) { message = messages.TransactionInactiveError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, TransactionInactiveError.prototype);
        _this.name = "TransactionInactiveError";
        _this.message = message;
        return _this;
    }
    return TransactionInactiveError;
}(Error));
exports.TransactionInactiveError = TransactionInactiveError;
var VersionError = /** @class */ (function (_super) {
    __extends(VersionError, _super);
    function VersionError(message) {
        if (message === void 0) { message = messages.VersionError; }
        var _this = _super.call(this) || this;
        Object.setPrototypeOf(_this, VersionError.prototype);
        _this.name = "VersionError";
        _this.message = message;
        return _this;
    }
    return VersionError;
}(Error));
exports.VersionError = VersionError;

});

unwrapExports(errors);
var errors_1 = errors.AbortError;
var errors_2 = errors.ConstraintError;
var errors_3 = errors.DataCloneError;
var errors_4 = errors.DataError;
var errors_5 = errors.InvalidAccessError;
var errors_6 = errors.InvalidStateError;
var errors_7 = errors.NotFoundError;
var errors_8 = errors.ReadOnlyError;
var errors_9 = errors.TransactionInactiveError;
var errors_10 = errors.VersionError;

var valueToKey_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });

// https://w3c.github.io/IndexedDB/#convert-a-value-to-a-input
function valueToKey(input, seen) {
    if (typeof input === "number") {
        if (isNaN(input)) {
            throw new errors.DataError();
        }
        return input;
    }
    else if (input instanceof Date) {
        var ms = input.valueOf();
        if (isNaN(ms)) {
            throw new errors.DataError();
        }
        return new Date(ms);
    }
    else if (typeof input === "string") {
        return input;
    }
    else if (input instanceof ArrayBuffer ||
        (typeof ArrayBuffer !== "undefined" &&
            ArrayBuffer.isView &&
            ArrayBuffer.isView(input))) {
        if (input instanceof ArrayBuffer) {
            return new Uint8Array(input).buffer;
        }
        return new Uint8Array(input.buffer).buffer;
    }
    else if (Array.isArray(input)) {
        if (seen === undefined) {
            seen = new Set();
        }
        else if (seen.has(input)) {
            throw new errors.DataError();
        }
        seen.add(input);
        var keys = [];
        for (var i = 0; i < input.length; i++) {
            var hop = input.hasOwnProperty(i);
            if (!hop) {
                throw new errors.DataError();
            }
            var entry = input[i];
            var key = valueToKey(entry, seen);
            keys.push(key);
        }
        return keys;
    }
    else {
        throw new errors.DataError();
    }
}
exports.default = valueToKey;

});

unwrapExports(valueToKey_1);

var cmp = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });


var getType = function (x) {
    if (typeof x === "number") {
        return "Number";
    }
    if (x instanceof Date) {
        return "Date";
    }
    if (Array.isArray(x)) {
        return "Array";
    }
    if (typeof x === "string") {
        return "String";
    }
    if (x instanceof ArrayBuffer) {
        return "Binary";
    }
    throw new errors.DataError();
};
// https://w3c.github.io/IndexedDB/#compare-two-keys
var compareKeys = function (first, second) {
    if (second === undefined) {
        throw new TypeError();
    }
    first = valueToKey_1.default(first);
    second = valueToKey_1.default(second);
    var t1 = getType(first);
    var t2 = getType(second);
    if (t1 !== t2) {
        if (t1 === "Array") {
            return 1;
        }
        if (t1 === "Binary" &&
            (t2 === "String" || t2 === "Date" || t2 === "Number")) {
            return 1;
        }
        if (t1 === "String" && (t2 === "Date" || t2 === "Number")) {
            return 1;
        }
        if (t1 === "Date" && t2 === "Number") {
            return 1;
        }
        return -1;
    }
    if (t1 === "Binary") {
        first = new Uint8Array(first);
        second = new Uint8Array(second);
    }
    if (t1 === "Array" || t1 === "Binary") {
        var length = Math.min(first.length, second.length);
        for (var i = 0; i < length; i++) {
            var result = compareKeys(first[i], second[i]);
            if (result !== 0) {
                return result;
            }
        }
        if (first.length > second.length) {
            return 1;
        }
        if (first.length < second.length) {
            return -1;
        }
        return 0;
    }
    if (t1 === "Date") {
        if (first.getTime() === second.getTime()) {
            return 0;
        }
    }
    else {
        if (first === second) {
            return 0;
        }
    }
    return first > second ? 1 : -1;
};
exports.default = compareKeys;

});

unwrapExports(cmp);

var BridgeIDBKeyRange_1 = createCommonjsModule(function (module, exports) {
/*
  Copyright 2019 Florian Dold
  Copyright 2017 Jeremy Scheff

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing
  permissions and limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });



// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#range-concept
var BridgeIDBKeyRange = /** @class */ (function () {
    function BridgeIDBKeyRange(lower, upper, lowerOpen, upperOpen) {
        this.lower = lower;
        this.upper = upper;
        this.lowerOpen = lowerOpen;
        this.upperOpen = upperOpen;
    }
    BridgeIDBKeyRange.only = function (value) {
        if (arguments.length === 0) {
            throw new TypeError();
        }
        value = valueToKey_1.default(value);
        return new BridgeIDBKeyRange(value, value, false, false);
    };
    BridgeIDBKeyRange.lowerBound = function (lower, open) {
        if (open === void 0) { open = false; }
        if (arguments.length === 0) {
            throw new TypeError();
        }
        lower = valueToKey_1.default(lower);
        return new BridgeIDBKeyRange(lower, undefined, open, true);
    };
    BridgeIDBKeyRange.upperBound = function (upper, open) {
        if (open === void 0) { open = false; }
        if (arguments.length === 0) {
            throw new TypeError();
        }
        upper = valueToKey_1.default(upper);
        return new BridgeIDBKeyRange(undefined, upper, true, open);
    };
    BridgeIDBKeyRange.bound = function (lower, upper, lowerOpen, upperOpen) {
        if (lowerOpen === void 0) { lowerOpen = false; }
        if (upperOpen === void 0) { upperOpen = false; }
        if (arguments.length < 2) {
            throw new TypeError();
        }
        var cmpResult = cmp.default(lower, upper);
        if (cmpResult === 1 || (cmpResult === 0 && (lowerOpen || upperOpen))) {
            throw new errors.DataError();
        }
        lower = valueToKey_1.default(lower);
        upper = valueToKey_1.default(upper);
        return new BridgeIDBKeyRange(lower, upper, lowerOpen, upperOpen);
    };
    // https://w3c.github.io/IndexedDB/#dom-idbkeyrange-includes
    BridgeIDBKeyRange.prototype.includes = function (key) {
        if (arguments.length === 0) {
            throw new TypeError();
        }
        key = valueToKey_1.default(key);
        if (this.lower !== undefined) {
            var cmpResult = cmp.default(this.lower, key);
            if (cmpResult === 1 || (cmpResult === 0 && this.lowerOpen)) {
                return false;
            }
        }
        if (this.upper !== undefined) {
            var cmpResult = cmp.default(this.upper, key);
            if (cmpResult === -1 || (cmpResult === 0 && this.upperOpen)) {
                return false;
            }
        }
        return true;
    };
    BridgeIDBKeyRange.prototype.toString = function () {
        return "[object IDBKeyRange]";
    };
    BridgeIDBKeyRange._valueToKeyRange = function (value, nullDisallowedFlag) {
        if (nullDisallowedFlag === void 0) { nullDisallowedFlag = false; }
        if (value instanceof BridgeIDBKeyRange) {
            return value;
        }
        if (value === null || value === undefined) {
            if (nullDisallowedFlag) {
                throw new errors.DataError();
            }
            return new BridgeIDBKeyRange(undefined, undefined, false, false);
        }
        var key = valueToKey_1.default(value);
        return BridgeIDBKeyRange.only(key);
    };
    return BridgeIDBKeyRange;
}());
exports.default = BridgeIDBKeyRange;

});

unwrapExports(BridgeIDBKeyRange_1);

var structuredClone_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
function structuredCloneImpl(val, visited) {
    // FIXME: replace with real implementation!
    return JSON.parse(JSON.stringify(val));
}
/**
 * Structured clone for IndexedDB.
 */
function structuredClone(val) {
    return structuredCloneImpl(val);
}
exports.structuredClone = structuredClone;
exports.default = structuredClone;

});

unwrapExports(structuredClone_1);
var structuredClone_2 = structuredClone_1.structuredClone;

var backendInterface = createCommonjsModule(function (module, exports) {
/*
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var ResultLevel;
(function (ResultLevel) {
    ResultLevel[ResultLevel["OnlyCount"] = 0] = "OnlyCount";
    ResultLevel[ResultLevel["OnlyKeys"] = 1] = "OnlyKeys";
    ResultLevel[ResultLevel["Full"] = 2] = "Full";
})(ResultLevel = exports.ResultLevel || (exports.ResultLevel = {}));
var StoreLevel;
(function (StoreLevel) {
    StoreLevel[StoreLevel["NoOverwrite"] = 0] = "NoOverwrite";
    StoreLevel[StoreLevel["AllowOverwrite"] = 1] = "AllowOverwrite";
    StoreLevel[StoreLevel["UpdateExisting"] = 2] = "UpdateExisting";
})(StoreLevel = exports.StoreLevel || (exports.StoreLevel = {}));

});

unwrapExports(backendInterface);
var backendInterface_1 = backendInterface.ResultLevel;
var backendInterface_2 = backendInterface.StoreLevel;

var BridgeIDBCursor_1 = createCommonjsModule(function (module, exports) {
/*

 Copyright 2017 Jeremy Scheff
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });








/**
 * http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#cursor
 */
var BridgeIDBCursor = /** @class */ (function () {
    function BridgeIDBCursor(source, objectStoreName, indexName, range, direction, request, keyOnly) {
        this._gotValue = false;
        this._indexPosition = undefined; // Key of previously returned record
        this._objectStorePosition = undefined;
        this._key = undefined;
        this._primaryKey = undefined;
        this._value = undefined;
        this._indexName = indexName;
        this._objectStoreName = objectStoreName;
        this._range = range;
        this._source = source;
        this._direction = direction;
        this._request = request;
        this._keyOnly = keyOnly;
    }
    Object.defineProperty(BridgeIDBCursor.prototype, "_effectiveObjectStore", {
        get: function () {
            if (this.source instanceof BridgeIDBObjectStore_1.default) {
                return this.source;
            }
            return this.source.objectStore;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBCursor.prototype, "_backend", {
        get: function () {
            return this._source._backend;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBCursor.prototype, "source", {
        // Read only properties
        get: function () {
            return this._source;
        },
        set: function (val) {
            /* For babel */
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBCursor.prototype, "direction", {
        get: function () {
            return this._direction;
        },
        set: function (val) {
            /* For babel */
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBCursor.prototype, "key", {
        get: function () {
            return this._key;
        },
        set: function (val) {
            /* For babel */
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBCursor.prototype, "primaryKey", {
        get: function () {
            return this._primaryKey;
        },
        set: function (val) {
            /* For babel */
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBCursor.prototype, "_isValueCursor", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    /**
     * https://w3c.github.io/IndexedDB/#iterate-a-cursor
     */
    BridgeIDBCursor.prototype._iterate = function (key, primaryKey) {
        return __awaiter(this, void 0, void 0, function () {
            var recordGetRequest, btx, response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        BridgeIDBFactory_1.default.enableTracing &&
                            console.log("iterating cursor os=" + this._objectStoreName + ",idx=" + this._indexName);
                        BridgeIDBFactory_1.default.enableTracing && console.log("cursor type ", this.toString());
                        recordGetRequest = {
                            direction: this.direction,
                            indexName: this._indexName,
                            lastIndexPosition: this._indexPosition,
                            lastObjectStorePosition: this._objectStorePosition,
                            limit: 1,
                            range: this._range,
                            objectStoreName: this._objectStoreName,
                            advanceIndexKey: key,
                            advancePrimaryKey: primaryKey,
                            resultLevel: this._keyOnly ? backendInterface.ResultLevel.OnlyKeys : backendInterface.ResultLevel.Full,
                        };
                        btx = this.source._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.getRecords(btx, recordGetRequest)];
                    case 1:
                        response = _a.sent();
                        if (response.count === 0) {
                            if (BridgeIDBFactory_1.default.enableTracing) {
                                console.log("cursor is returning empty result");
                            }
                            this._gotValue = false;
                            return [2 /*return*/, null];
                        }
                        if (response.count !== 1) {
                            throw Error("invariant failed");
                        }
                        if (BridgeIDBFactory_1.default.enableTracing) {
                            console.log("request is:", JSON.stringify(recordGetRequest));
                            console.log("get response is:", JSON.stringify(response));
                        }
                        if (this._indexName !== undefined) {
                            this._key = response.indexKeys[0];
                        }
                        else {
                            this._key = response.primaryKeys[0];
                        }
                        this._primaryKey = response.primaryKeys[0];
                        if (!this._keyOnly) {
                            this._value = response.values[0];
                        }
                        this._gotValue = true;
                        this._objectStorePosition = structuredClone_1.default(response.primaryKeys[0]);
                        if (response.indexKeys !== undefined && response.indexKeys.length > 0) {
                            this._indexPosition = structuredClone_1.default(response.indexKeys[0]);
                        }
                        return [2 /*return*/, this];
                }
            });
        });
    };
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBCursor-update-IDBRequest-any-value
    BridgeIDBCursor.prototype.update = function (value) {
        var _this = this;
        if (value === undefined) {
            throw new TypeError();
        }
        var transaction = this._effectiveObjectStore.transaction;
        if (transaction._state !== "active") {
            throw new errors.TransactionInactiveError();
        }
        if (transaction.mode === "readonly") {
            throw new errors.ReadOnlyError();
        }
        if (this._effectiveObjectStore._deleted) {
            throw new errors.InvalidStateError();
        }
        if (!(this.source instanceof BridgeIDBObjectStore_1.default) &&
            this.source._deleted) {
            throw new errors.InvalidStateError();
        }
        if (!this._gotValue || !this._isValueCursor) {
            throw new errors.InvalidStateError();
        }
        var storeReq = {
            key: this._primaryKey,
            value: value,
            objectStoreName: this._objectStoreName,
            storeLevel: backendInterface.StoreLevel.UpdateExisting,
        };
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (BridgeIDBFactory_1.default.enableTracing) {
                            console.log("updating at cursor");
                        }
                        btx = this.source._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.storeRecord(btx, storeReq)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        }); };
        return transaction._execRequestAsync({
            operation: operation,
            source: this,
        });
    };
    /**
     * http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBCursor-advance-void-unsigned-long-count
     */
    BridgeIDBCursor.prototype.advance = function (count) {
        throw Error("not implemented");
    };
    /**
     * http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBCursor-continue-void-any-key
     */
    BridgeIDBCursor.prototype.continue = function (key) {
        var _this = this;
        var transaction = this._effectiveObjectStore.transaction;
        if (transaction._state !== "active") {
            throw new errors.TransactionInactiveError();
        }
        if (this._effectiveObjectStore._deleted) {
            throw new errors.InvalidStateError();
        }
        if (!(this.source instanceof BridgeIDBObjectStore_1.default) &&
            this.source._deleted) {
            throw new errors.InvalidStateError();
        }
        if (!this._gotValue) {
            throw new errors.InvalidStateError();
        }
        if (key !== undefined) {
            key = valueToKey_1.default(key);
            var lastKey = this._indexName === undefined
                ? this._objectStorePosition
                : this._indexPosition;
            var cmpResult = cmp.default(key, lastKey);
            if ((cmpResult <= 0 &&
                (this.direction === "next" || this.direction === "nextunique")) ||
                (cmpResult >= 0 &&
                    (this.direction === "prev" || this.direction === "prevunique"))) {
                throw new errors.DataError();
            }
        }
        if (this._request) {
            this._request.readyState = "pending";
        }
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, this._iterate(key)];
            });
        }); };
        transaction._execRequestAsync({
            operation: operation,
            request: this._request,
            source: this.source,
        });
        this._gotValue = false;
    };
    // https://w3c.github.io/IndexedDB/#dom-idbcursor-continueprimarykey
    BridgeIDBCursor.prototype.continuePrimaryKey = function (key, primaryKey) {
        throw Error("not implemented");
    };
    BridgeIDBCursor.prototype.delete = function () {
        var _this = this;
        var transaction = this._effectiveObjectStore.transaction;
        if (transaction._state !== "active") {
            throw new errors.TransactionInactiveError();
        }
        if (transaction.mode === "readonly") {
            throw new errors.ReadOnlyError();
        }
        if (this._effectiveObjectStore._deleted) {
            throw new errors.InvalidStateError();
        }
        if (!(this.source instanceof BridgeIDBObjectStore_1.default) &&
            this.source._deleted) {
            throw new errors.InvalidStateError();
        }
        if (!this._gotValue || !this._isValueCursor) {
            throw new errors.InvalidStateError();
        }
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx;
            return __generator(this, function (_a) {
                btx = this.source._confirmActiveTransaction().btx;
                this._backend.deleteRecord(btx, this._objectStoreName, BridgeIDBKeyRange_1.default._valueToKeyRange(this._primaryKey));
                return [2 /*return*/];
            });
        }); };
        return transaction._execRequestAsync({
            operation: operation,
            source: this,
        });
    };
    BridgeIDBCursor.prototype.toString = function () {
        return "[object IDBCursor]";
    };
    return BridgeIDBCursor;
}());
exports.BridgeIDBCursor = BridgeIDBCursor;
exports.default = BridgeIDBCursor;

});

unwrapExports(BridgeIDBCursor_1);
var BridgeIDBCursor_2 = BridgeIDBCursor_1.BridgeIDBCursor;

var BridgeIDBCursorWithValue_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });

var BridgeIDBCursorWithValue = /** @class */ (function (_super) {
    __extends(BridgeIDBCursorWithValue, _super);
    function BridgeIDBCursorWithValue(source, objectStoreName, indexName, range, direction, request) {
        return _super.call(this, source, objectStoreName, indexName, range, direction, request, false) || this;
    }
    Object.defineProperty(BridgeIDBCursorWithValue.prototype, "value", {
        get: function () {
            return this._value;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBCursorWithValue.prototype, "_isValueCursor", {
        get: function () {
            return true;
        },
        enumerable: true,
        configurable: true
    });
    BridgeIDBCursorWithValue.prototype.toString = function () {
        return "[object IDBCursorWithValue]";
    };
    return BridgeIDBCursorWithValue;
}(BridgeIDBCursor_1.default));
exports.default = BridgeIDBCursorWithValue;

});

unwrapExports(BridgeIDBCursorWithValue_1);

var FakeEventTarget_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });

var stopped = function (event, listener) {
    return (event.immediatePropagationStopped ||
        (event.eventPhase === event.CAPTURING_PHASE &&
            listener.capture === false) ||
        (event.eventPhase === event.BUBBLING_PHASE && listener.capture === true));
};
// http://www.w3.org/TR/dom/#concept-event-listener-invoke
var invokeEventListeners = function (event, obj) {
    event.currentTarget = obj;
    // The callback might cause obj.listeners to mutate as we traverse it.
    // Take a copy of the array so that nothing sneaks in and we don't lose
    // our place.
    for (var _i = 0, _a = obj.listeners.slice(); _i < _a.length; _i++) {
        var listener = _a[_i];
        if (event.type !== listener.type || stopped(event, listener)) {
            continue;
        }
        // @ts-ignore
        listener.callback.call(event.currentTarget, event);
    }
    var typeToProp = {
        abort: "onabort",
        blocked: "onblocked",
        complete: "oncomplete",
        error: "onerror",
        success: "onsuccess",
        upgradeneeded: "onupgradeneeded",
        versionchange: "onversionchange",
    };
    var prop = typeToProp[event.type];
    if (prop === undefined) {
        throw new Error("Unknown event type: \"" + event.type + "\"");
    }
    var callback = event.currentTarget[prop];
    if (callback) {
        var listener = {
            callback: callback,
            capture: false,
            type: event.type,
        };
        if (!stopped(event, listener)) {
            // @ts-ignore
            listener.callback.call(event.currentTarget, event);
        }
    }
};
var FakeEventTarget = /** @class */ (function () {
    function FakeEventTarget() {
        this.listeners = [];
    }
    FakeEventTarget.prototype.addEventListener = function (type, callback, capture) {
        if (capture === void 0) { capture = false; }
        this.listeners.push({
            callback: callback,
            capture: capture,
            type: type,
        });
    };
    FakeEventTarget.prototype.removeEventListener = function (type, callback, capture) {
        if (capture === void 0) { capture = false; }
        var i = this.listeners.findIndex(function (listener) {
            return (listener.type === type &&
                listener.callback === callback &&
                listener.capture === capture);
        });
        this.listeners.splice(i, 1);
    };
    // http://www.w3.org/TR/dom/#dispatching-events
    FakeEventTarget.prototype.dispatchEvent = function (event) {
        if (event.dispatched || !event.initialized) {
            throw new errors.InvalidStateError("The object is in an invalid state.");
        }
        event.isTrusted = false;
        event.dispatched = true;
        event.target = this;
        // NOT SURE WHEN THIS SHOULD BE SET        event.eventPath = [];
        event.eventPhase = event.CAPTURING_PHASE;
        if (FakeEventTarget.enableTracing) {
            console.log("dispatching '" + event.type + "' event along path with " + event.eventPath.length + " elements");
        }
        for (var _i = 0, _a = event.eventPath; _i < _a.length; _i++) {
            var obj = _a[_i];
            if (!event.propagationStopped) {
                invokeEventListeners(event, obj);
            }
        }
        event.eventPhase = event.AT_TARGET;
        if (!event.propagationStopped) {
            invokeEventListeners(event, event.target);
        }
        if (event.bubbles) {
            event.eventPath.reverse();
            event.eventPhase = event.BUBBLING_PHASE;
            if (event.eventPath.length === 0 && event.type === "error") {
                console.error("Unhandled error event: ", event.target);
            }
            for (var _b = 0, _c = event.eventPath; _b < _c.length; _b++) {
                var obj = _c[_b];
                if (!event.propagationStopped) {
                    invokeEventListeners(event, obj);
                }
            }
        }
        event.dispatched = false;
        event.eventPhase = event.NONE;
        event.currentTarget = null;
        if (event.canceled) {
            return false;
        }
        return true;
    };
    FakeEventTarget.enableTracing = false;
    return FakeEventTarget;
}());
exports.default = FakeEventTarget;

});

unwrapExports(FakeEventTarget_1);

var FakeEvent = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var Event = /** @class */ (function () {
    function Event(type, eventInitDict) {
        if (eventInitDict === void 0) { eventInitDict = {}; }
        this.eventPath = [];
        this.NONE = 0;
        this.CAPTURING_PHASE = 1;
        this.AT_TARGET = 2;
        this.BUBBLING_PHASE = 3;
        // Flags
        this.propagationStopped = false;
        this.immediatePropagationStopped = false;
        this.canceled = false;
        this.initialized = true;
        this.dispatched = false;
        this.target = null;
        this.currentTarget = null;
        this.eventPhase = 0;
        this.defaultPrevented = false;
        this.isTrusted = false;
        this.timeStamp = Date.now();
        this.type = type;
        this.bubbles =
            eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false;
        this.cancelable =
            eventInitDict.cancelable !== undefined
                ? eventInitDict.cancelable
                : false;
    }
    Event.prototype.preventDefault = function () {
        if (this.cancelable) {
            this.canceled = true;
        }
    };
    Event.prototype.stopPropagation = function () {
        this.propagationStopped = true;
    };
    Event.prototype.stopImmediatePropagation = function () {
        this.propagationStopped = true;
        this.immediatePropagationStopped = true;
    };
    return Event;
}());
exports.Event = Event;
exports.default = Event;

});

unwrapExports(FakeEvent);
var FakeEvent_1 = FakeEvent.Event;

var BridgeIDBRequest_1 = createCommonjsModule(function (module, exports) {
/*
 * Copyright 2017 Jeremy Scheff
 * Copyright 2019 Florian Dold
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });



var BridgeIDBRequest = /** @class */ (function (_super) {
    __extends(BridgeIDBRequest, _super);
    function BridgeIDBRequest() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._result = null;
        _this._error = null;
        _this.source = null;
        _this.transaction = null;
        _this.readyState = "pending";
        _this.onsuccess = null;
        _this.onerror = null;
        return _this;
    }
    Object.defineProperty(BridgeIDBRequest.prototype, "error", {
        get: function () {
            if (this.readyState === "pending") {
                throw new errors.InvalidStateError();
            }
            return this._error;
        },
        set: function (value) {
            this._error = value;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBRequest.prototype, "result", {
        get: function () {
            if (this.readyState === "pending") {
                throw new errors.InvalidStateError();
            }
            return this._result;
        },
        set: function (value) {
            this._result = value;
        },
        enumerable: true,
        configurable: true
    });
    BridgeIDBRequest.prototype.toString = function () {
        return "[object IDBRequest]";
    };
    BridgeIDBRequest.prototype._finishWithError = function (err) {
        this.result = undefined;
        this.readyState = "done";
        this.error = new Error(err.message);
        this.error.name = err.name;
        var event = new FakeEvent.default("error", {
            bubbles: true,
            cancelable: true,
        });
        event.eventPath = [];
        this.dispatchEvent(event);
    };
    BridgeIDBRequest.prototype._finishWithResult = function (result) {
        this.result = result;
        this.readyState = "done";
        var event = new FakeEvent.default("success");
        event.eventPath = [];
        this.dispatchEvent(event);
    };
    return BridgeIDBRequest;
}(FakeEventTarget_1.default));
exports.default = BridgeIDBRequest;

});

unwrapExports(BridgeIDBRequest_1);

var BridgeIDBIndex_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });







var confirmActiveTransaction = function (index) {
    if (index._deleted || index.objectStore._deleted) {
        throw new errors.InvalidStateError();
    }
    if (index.objectStore.transaction._state !== "active") {
        throw new errors.TransactionInactiveError();
    }
    return index.objectStore.transaction;
};
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#idl-def-IDBIndex
var BridgeIDBIndex = /** @class */ (function () {
    function BridgeIDBIndex(objectStore, name) {
        this._deleted = false;
        this._name = name;
        this.objectStore = objectStore;
    }
    Object.defineProperty(BridgeIDBIndex.prototype, "_schema", {
        get: function () {
            return this.objectStore.transaction.db._schema;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBIndex.prototype, "keyPath", {
        get: function () {
            return this._schema.objectStores[this.objectStore.name].indexes[this._name].keyPath;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBIndex.prototype, "multiEntry", {
        get: function () {
            return this._schema.objectStores[this.objectStore.name].indexes[this._name].multiEntry;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBIndex.prototype, "unique", {
        get: function () {
            return this._schema.objectStores[this.objectStore.name].indexes[this._name].unique;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBIndex.prototype, "_backend", {
        get: function () {
            return this.objectStore._backend;
        },
        enumerable: true,
        configurable: true
    });
    BridgeIDBIndex.prototype._confirmActiveTransaction = function () {
        return this.objectStore._confirmActiveTransaction();
    };
    Object.defineProperty(BridgeIDBIndex.prototype, "name", {
        get: function () {
            return this._name;
        },
        // https://w3c.github.io/IndexedDB/#dom-idbindex-name
        set: function (name) {
            var transaction = this.objectStore.transaction;
            if (!transaction.db._runningVersionchangeTransaction) {
                throw new errors.InvalidStateError();
            }
            if (transaction._state !== "active") {
                throw new errors.TransactionInactiveError();
            }
            var btx = this._confirmActiveTransaction().btx;
            var oldName = this._name;
            var newName = String(name);
            if (newName === oldName) {
                return;
            }
            this._backend.renameIndex(btx, this.objectStore.name, oldName, newName);
            if (this.objectStore.indexNames.indexOf(name) >= 0) {
                throw new errors.ConstraintError();
            }
        },
        enumerable: true,
        configurable: true
    });
    // tslint:disable-next-line max-line-length
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBIndex-openCursor-IDBRequest-any-range-IDBCursorDirection-direction
    BridgeIDBIndex.prototype.openCursor = function (range, direction) {
        var _this = this;
        if (direction === void 0) { direction = "next"; }
        confirmActiveTransaction(this);
        if (range === null) {
            range = undefined;
        }
        if (range !== undefined && !(range instanceof BridgeIDBKeyRange_1.default)) {
            range = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(range));
        }
        var request = new BridgeIDBRequest_1.default();
        request.source = this;
        request.transaction = this.objectStore.transaction;
        var cursor = new BridgeIDBCursorWithValue_1.default(this, this.objectStore.name, this._name, range, direction, request);
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, cursor._iterate()];
            });
        }); };
        return this.objectStore.transaction._execRequestAsync({
            operation: operation,
            request: request,
            source: this,
        });
    };
    // tslint:disable-next-line max-line-length
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBIndex-openKeyCursor-IDBRequest-any-range-IDBCursorDirection-direction
    BridgeIDBIndex.prototype.openKeyCursor = function (range, direction) {
        if (direction === void 0) { direction = "next"; }
        confirmActiveTransaction(this);
        if (range === null) {
            range = undefined;
        }
        if (range !== undefined && !(range instanceof BridgeIDBKeyRange_1.default)) {
            range = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(range));
        }
        var request = new BridgeIDBRequest_1.default();
        request.source = this;
        request.transaction = this.objectStore.transaction;
        var cursor = new BridgeIDBCursor_1.default(this, this.objectStore.name, this._name, range, direction, request, true);
        return this.objectStore.transaction._execRequestAsync({
            operation: cursor._iterate.bind(cursor),
            request: request,
            source: this,
        });
    };
    BridgeIDBIndex.prototype.get = function (key) {
        var _this = this;
        confirmActiveTransaction(this);
        if (!(key instanceof BridgeIDBKeyRange_1.default)) {
            key = BridgeIDBKeyRange_1.default._valueToKeyRange(key);
        }
        var getReq = {
            direction: "next",
            indexName: this._name,
            limit: 1,
            range: key,
            objectStoreName: this.objectStore._name,
            resultLevel: backendInterface.ResultLevel.Full,
        };
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx, result, values;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        btx = this._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.getRecords(btx, getReq)];
                    case 1:
                        result = _a.sent();
                        if (result.count == 0) {
                            return [2 /*return*/, undefined];
                        }
                        values = result.values;
                        if (!values) {
                            throw Error("invariant violated");
                        }
                        return [2 /*return*/, values[0]];
                }
            });
        }); };
        return this.objectStore.transaction._execRequestAsync({
            operation: operation,
            source: this,
        });
    };
    // http://w3c.github.io/IndexedDB/#dom-idbindex-getall
    BridgeIDBIndex.prototype.getAll = function (query, count) {
        throw Error("not implemented");
    };
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBIndex-getKey-IDBRequest-any-key
    BridgeIDBIndex.prototype.getKey = function (key) {
        var _this = this;
        confirmActiveTransaction(this);
        if (!(key instanceof BridgeIDBKeyRange_1.default)) {
            key = BridgeIDBKeyRange_1.default._valueToKeyRange(key);
        }
        var getReq = {
            direction: "next",
            indexName: this._name,
            limit: 1,
            range: key,
            objectStoreName: this.objectStore._name,
            resultLevel: backendInterface.ResultLevel.OnlyKeys,
        };
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx, result, primaryKeys;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        btx = this._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.getRecords(btx, getReq)];
                    case 1:
                        result = _a.sent();
                        if (result.count == 0) {
                            return [2 /*return*/, undefined];
                        }
                        primaryKeys = result.primaryKeys;
                        if (!primaryKeys) {
                            throw Error("invariant violated");
                        }
                        return [2 /*return*/, primaryKeys[0]];
                }
            });
        }); };
        return this.objectStore.transaction._execRequestAsync({
            operation: operation,
            source: this,
        });
    };
    // http://w3c.github.io/IndexedDB/#dom-idbindex-getallkeys
    BridgeIDBIndex.prototype.getAllKeys = function (query, count) {
        throw Error("not implemented");
    };
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBIndex-count-IDBRequest-any-key
    BridgeIDBIndex.prototype.count = function (key) {
        var _this = this;
        confirmActiveTransaction(this);
        if (key === null) {
            key = undefined;
        }
        if (key !== undefined && !(key instanceof BridgeIDBKeyRange_1.default)) {
            key = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(key));
        }
        var getReq = {
            direction: "next",
            indexName: this._name,
            limit: 1,
            range: key,
            objectStoreName: this.objectStore._name,
            resultLevel: backendInterface.ResultLevel.OnlyCount,
        };
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        btx = this._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.getRecords(btx, getReq)];
                    case 1:
                        result = _a.sent();
                        return [2 /*return*/, result.count];
                }
            });
        }); };
        return this.objectStore.transaction._execRequestAsync({
            operation: operation,
            source: this,
        });
    };
    BridgeIDBIndex.prototype.toString = function () {
        return "[object IDBIndex]";
    };
    return BridgeIDBIndex;
}());
exports.BridgeIDBIndex = BridgeIDBIndex;
exports.default = BridgeIDBIndex;

});

unwrapExports(BridgeIDBIndex_1);
var BridgeIDBIndex_2 = BridgeIDBIndex_1.BridgeIDBIndex;

var fakeDOMStringList_1 = createCommonjsModule(function (module, exports) {
/*
 * Copyright 2017 Jeremy Scheff
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
// Would be nicer to sublcass Array, but I'd have to sacrifice Node 4 support to do that.
var fakeDOMStringList = function (arr) {
    var arr2 = arr.slice();
    Object.defineProperty(arr2, "contains", {
        // tslint:disable-next-line object-literal-shorthand
        value: function (value) { return arr2.indexOf(value) >= 0; },
    });
    Object.defineProperty(arr2, "item", {
        // tslint:disable-next-line object-literal-shorthand
        value: function (i) { return arr2[i]; },
    });
    return arr2;
};
exports.default = fakeDOMStringList;

});

unwrapExports(fakeDOMStringList_1);

var validateKeyPath_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-valid-key-path
var validateKeyPath = function (keyPath, parent) {
    // This doesn't make sense to me based on the spec, but it is needed to pass the W3C KeyPath tests (see same
    // comment in extractKey)
    if (keyPath !== undefined &&
        keyPath !== null &&
        typeof keyPath !== "string" &&
        keyPath.toString &&
        (parent === "array" || !Array.isArray(keyPath))) {
        keyPath = keyPath.toString();
    }
    if (typeof keyPath === "string") {
        if (keyPath === "" && parent !== "string") {
            return;
        }
        try {
            // https://mathiasbynens.be/demo/javascript-identifier-regex for ECMAScript 5.1 / Unicode v7.0.0, with
            // reserved words at beginning removed
            // tslint:disable-next-line max-line-length
            var validIdentifierRegex = /^(?:[\$A-Z_a-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0-\u08B2\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA7AD\uA7B0\uA7B1\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64\uAB65\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC])(?:[\$0-9A-Z_a-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0-\u08B2\u08E4-\u0963\u0966-\u096F\u0971-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58\u0C59\u0C60-\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D01-\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D60-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191E\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19D9\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1AB0-\u1ABD\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1CF8\u1CF9\u1D00-\u1DF5\u1DFC-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u200C\u200D\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u2E2F\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099\u309A\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA69D\uA69F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA7AD\uA7B0\uA7B1\uA7F7-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uA9E0-\uA9FE\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64\uAB65\uABC0-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE2D\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC])*$/;
            if (keyPath.length >= 1 && validIdentifierRegex.test(keyPath)) {
                return;
            }
        }
        catch (err) {
            throw new SyntaxError(err.message);
        }
        if (keyPath.indexOf(" ") >= 0) {
            throw new SyntaxError("The keypath argument contains an invalid key path (no spaces allowed).");
        }
    }
    if (Array.isArray(keyPath) && keyPath.length > 0) {
        if (parent) {
            // No nested arrays
            throw new SyntaxError("The keypath argument contains an invalid key path (nested arrays).");
        }
        for (var _i = 0, keyPath_1 = keyPath; _i < keyPath_1.length; _i++) {
            var part = keyPath_1[_i];
            validateKeyPath(part, "array");
        }
        return;
    }
    else if (typeof keyPath === "string" && keyPath.indexOf(".") >= 0) {
        keyPath = keyPath.split(".");
        for (var _a = 0, keyPath_2 = keyPath; _a < keyPath_2.length; _a++) {
            var part = keyPath_2[_a];
            validateKeyPath(part, "string");
        }
        return;
    }
    throw new SyntaxError();
};
exports.default = validateKeyPath;

});

unwrapExports(validateKeyPath_1);

var BridgeIDBObjectStore_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2019 Florian Dold
 Copyright 2017 Jeremy Scheff

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });











// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#object-store
var BridgeIDBObjectStore = /** @class */ (function () {
    function BridgeIDBObjectStore(transaction, name) {
        this._indexesCache = new Map();
        this._deleted = false;
        this._name = name;
        this.transaction = transaction;
    }
    Object.defineProperty(BridgeIDBObjectStore.prototype, "autoIncrement", {
        get: function () {
            return this._schema.objectStores[this._name].autoIncrement;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBObjectStore.prototype, "indexNames", {
        get: function () {
            return fakeDOMStringList_1.default(Object.keys(this._schema.objectStores[this._name].indexes)).sort();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBObjectStore.prototype, "keyPath", {
        get: function () {
            return this._schema.objectStores[this._name].keyPath;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBObjectStore.prototype, "_schema", {
        get: function () {
            return this.transaction.db._schema;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBObjectStore.prototype, "name", {
        get: function () {
            return this._name;
        },
        // http://w3c.github.io/IndexedDB/#dom-idbobjectstore-name
        set: function (newName) {
            var transaction = this.transaction;
            if (!transaction.db._runningVersionchangeTransaction) {
                throw new errors.InvalidStateError();
            }
            var btx = this._confirmActiveTransaction().btx;
            newName = String(newName);
            var oldName = this._name;
            if (newName === oldName) {
                return;
            }
            this._backend.renameObjectStore(btx, oldName, newName);
            this.transaction.db._schema = this._backend.getSchema(this._backendConnection);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBObjectStore.prototype, "_backend", {
        get: function () {
            return this.transaction.db._backend;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBObjectStore.prototype, "_backendConnection", {
        get: function () {
            return this.transaction.db._backendConnection;
        },
        enumerable: true,
        configurable: true
    });
    BridgeIDBObjectStore.prototype._confirmActiveTransaction = function () {
        var btx = this.transaction._backendTransaction;
        if (!btx) {
            throw new errors.InvalidStateError();
        }
        return { btx: btx };
    };
    BridgeIDBObjectStore.prototype._store = function (value, key, overwrite) {
        var _this = this;
        if (BridgeIDBFactory_1.default.enableTracing) {
            console.log("TRACE: IDBObjectStore._store");
        }
        if (this.transaction.mode === "readonly") {
            throw new errors.ReadOnlyError();
        }
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        btx = this._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.storeRecord(btx, {
                                objectStoreName: this._name,
                                key: key,
                                value: value,
                                storeLevel: overwrite
                                    ? backendInterface.StoreLevel.AllowOverwrite
                                    : backendInterface.StoreLevel.NoOverwrite,
                            })];
                    case 1:
                        result = _a.sent();
                        return [2 /*return*/, result.key];
                }
            });
        }); };
        return this.transaction._execRequestAsync({ operation: operation, source: this });
    };
    BridgeIDBObjectStore.prototype.put = function (value, key) {
        if (arguments.length === 0) {
            throw new TypeError();
        }
        return this._store(value, key, true);
    };
    BridgeIDBObjectStore.prototype.add = function (value, key) {
        if (arguments.length === 0) {
            throw new TypeError();
        }
        return this._store(value, key, false);
    };
    BridgeIDBObjectStore.prototype.delete = function (key) {
        var _this = this;
        if (arguments.length === 0) {
            throw new TypeError();
        }
        if (this.transaction.mode === "readonly") {
            throw new errors.ReadOnlyError();
        }
        var keyRange;
        if (key instanceof BridgeIDBKeyRange_1.default) {
            keyRange = key;
        }
        else {
            keyRange = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(key));
        }
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx;
            return __generator(this, function (_a) {
                btx = this._confirmActiveTransaction().btx;
                return [2 /*return*/, this._backend.deleteRecord(btx, this._name, keyRange)];
            });
        }); };
        return this.transaction._execRequestAsync({
            operation: operation,
            source: this,
        });
    };
    BridgeIDBObjectStore.prototype.get = function (key) {
        var _this = this;
        if (BridgeIDBFactory_1.default.enableTracing) {
            console.log("getting from object store " + this._name + " key " + key);
        }
        if (arguments.length === 0) {
            throw new TypeError();
        }
        var keyRange;
        if (key instanceof BridgeIDBKeyRange_1.default) {
            keyRange = key;
        }
        else {
            keyRange = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(key));
        }
        var recordRequest = {
            objectStoreName: this._name,
            indexName: undefined,
            lastIndexPosition: undefined,
            lastObjectStorePosition: undefined,
            direction: "next",
            limit: 1,
            resultLevel: backendInterface.ResultLevel.Full,
            range: keyRange,
        };
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx, result, values;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (BridgeIDBFactory_1.default.enableTracing) {
                            console.log("running get operation:", recordRequest);
                        }
                        btx = this._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.getRecords(btx, recordRequest)];
                    case 1:
                        result = _a.sent();
                        if (BridgeIDBFactory_1.default.enableTracing) {
                            console.log("get operation result count:", result.count);
                        }
                        if (result.count === 0) {
                            return [2 /*return*/, undefined];
                        }
                        values = result.values;
                        if (!values) {
                            throw Error("invariant violated");
                        }
                        return [2 /*return*/, values[0]];
                }
            });
        }); };
        return this.transaction._execRequestAsync({
            operation: operation,
            source: this,
        });
    };
    // http://w3c.github.io/IndexedDB/#dom-idbobjectstore-getall
    BridgeIDBObjectStore.prototype.getAll = function (query, count) {
        throw Error("not implemented");
    };
    // http://w3c.github.io/IndexedDB/#dom-idbobjectstore-getkey
    BridgeIDBObjectStore.prototype.getKey = function (key) {
        throw Error("not implemented");
    };
    // http://w3c.github.io/IndexedDB/#dom-idbobjectstore-getallkeys
    BridgeIDBObjectStore.prototype.getAllKeys = function (query, count) {
        throw Error("not implemented");
    };
    BridgeIDBObjectStore.prototype.clear = function () {
        throw Error("not implemented");
    };
    BridgeIDBObjectStore.prototype.openCursor = function (range, direction) {
        if (direction === void 0) { direction = "next"; }
        if (range === null) {
            range = undefined;
        }
        if (range !== undefined && !(range instanceof BridgeIDBKeyRange_1.default)) {
            range = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(range));
        }
        var request = new BridgeIDBRequest_1.default();
        request.source = this;
        request.transaction = this.transaction;
        var cursor = new BridgeIDBCursorWithValue_1.default(this, this._name, undefined, range, direction, request);
        return this.transaction._execRequestAsync({
            operation: function () { return cursor._iterate(); },
            request: request,
            source: this,
        });
    };
    BridgeIDBObjectStore.prototype.openKeyCursor = function (range, direction) {
        if (range === null) {
            range = undefined;
        }
        if (range !== undefined && !(range instanceof BridgeIDBKeyRange_1.default)) {
            range = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(range));
        }
        if (!direction) {
            direction = "next";
        }
        var request = new BridgeIDBRequest_1.default();
        request.source = this;
        request.transaction = this.transaction;
        var cursor = new BridgeIDBCursor_1.default(this, this._name, undefined, range, direction, request, true);
        return this.transaction._execRequestAsync({
            operation: cursor._iterate.bind(cursor),
            request: request,
            source: this,
        });
    };
    // tslint:disable-next-line max-line-length
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBObjectStore-createIndex-IDBIndex-DOMString-name-DOMString-sequence-DOMString--keyPath-IDBIndexParameters-optionalParameters
    BridgeIDBObjectStore.prototype.createIndex = function (indexName, keyPath, optionalParameters) {
        if (optionalParameters === void 0) { optionalParameters = {}; }
        if (arguments.length < 2) {
            throw new TypeError();
        }
        if (!this.transaction.db._runningVersionchangeTransaction) {
            throw new errors.InvalidStateError();
        }
        var btx = this._confirmActiveTransaction().btx;
        var multiEntry = optionalParameters.multiEntry !== undefined
            ? optionalParameters.multiEntry
            : false;
        var unique = optionalParameters.unique !== undefined
            ? optionalParameters.unique
            : false;
        if (this.transaction.mode !== "versionchange") {
            throw new errors.InvalidStateError();
        }
        if (this.indexNames.indexOf(indexName) >= 0) {
            throw new errors.ConstraintError();
        }
        validateKeyPath_1.default(keyPath);
        if (Array.isArray(keyPath) && multiEntry) {
            throw new errors.InvalidAccessError();
        }
        this._backend.createIndex(btx, indexName, this._name, keyPath, multiEntry, unique);
        return new BridgeIDBIndex_1.default(this, indexName);
    };
    // https://w3c.github.io/IndexedDB/#dom-idbobjectstore-index
    BridgeIDBObjectStore.prototype.index = function (name) {
        if (arguments.length === 0) {
            throw new TypeError();
        }
        if (this.transaction._state === "finished") {
            throw new errors.InvalidStateError();
        }
        var index = this._indexesCache.get(name);
        if (index !== undefined) {
            return index;
        }
        return new BridgeIDBIndex_1.default(this, name);
    };
    BridgeIDBObjectStore.prototype.deleteIndex = function (indexName) {
        if (arguments.length === 0) {
            throw new TypeError();
        }
        if (this.transaction.mode !== "versionchange") {
            throw new errors.InvalidStateError();
        }
        if (!this.transaction.db._runningVersionchangeTransaction) {
            throw new errors.InvalidStateError();
        }
        var btx = this._confirmActiveTransaction().btx;
        var index = this._indexesCache.get(indexName);
        if (index !== undefined) {
            index._deleted = true;
        }
        this._backend.deleteIndex(btx, this._name, indexName);
    };
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBObjectStore-count-IDBRequest-any-key
    BridgeIDBObjectStore.prototype.count = function (key) {
        var _this = this;
        if (key === null) {
            key = undefined;
        }
        if (key !== undefined && !(key instanceof BridgeIDBKeyRange_1.default)) {
            key = BridgeIDBKeyRange_1.default.only(valueToKey_1.default(key));
        }
        var recordGetRequest = {
            direction: "next",
            indexName: undefined,
            lastIndexPosition: undefined,
            limit: -1,
            objectStoreName: this._name,
            lastObjectStorePosition: undefined,
            range: key,
            resultLevel: backendInterface.ResultLevel.OnlyCount,
        };
        var operation = function () { return __awaiter(_this, void 0, void 0, function () {
            var btx, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        btx = this._confirmActiveTransaction().btx;
                        return [4 /*yield*/, this._backend.getRecords(btx, recordGetRequest)];
                    case 1:
                        result = _a.sent();
                        return [2 /*return*/, result.count];
                }
            });
        }); };
        return this.transaction._execRequestAsync({ operation: operation, source: this });
    };
    BridgeIDBObjectStore.prototype.toString = function () {
        return "[object IDBObjectStore]";
    };
    return BridgeIDBObjectStore;
}());
exports.default = BridgeIDBObjectStore;

});

unwrapExports(BridgeIDBObjectStore_1);

var queueTask_1 = createCommonjsModule(function (module, exports) {
/*
  Copyright 2019 Florian Dold

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing
  permissions and limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
function queueTask(fn) {
    setImmediate(fn);
}
exports.queueTask = queueTask;
exports.default = queueTask;

});

unwrapExports(queueTask_1);
var queueTask_2 = queueTask_1.queueTask;

var openPromise_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
function openPromise() {
    var resolve;
    var reject;
    var promise = new Promise(function (resolve2, reject2) {
        resolve = resolve2;
        reject = reject2;
    });
    if (!resolve) {
        throw Error("broken invariant");
    }
    if (!reject) {
        throw Error("broken invariant");
    }
    return { promise: promise, resolve: resolve, reject: reject };
}
exports.default = openPromise;

});

unwrapExports(openPromise_1);

var BridgeIDBTransaction_1 = createCommonjsModule(function (module, exports) {
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });









// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#transaction
var BridgeIDBTransaction = /** @class */ (function (_super) {
    __extends(BridgeIDBTransaction, _super);
    function BridgeIDBTransaction(storeNames, mode, db, backendTransaction) {
        var _this = _super.call(this) || this;
        _this._state = "active";
        _this._started = false;
        _this._objectStoresCache = new Map();
        _this.error = null;
        _this.onabort = null;
        _this.oncomplete = null;
        _this.onerror = null;
        _this._requests = [];
        var myOpenPromise = openPromise_1.default();
        _this._waitPromise = myOpenPromise.promise;
        _this._resolveWait = myOpenPromise.resolve;
        _this._scope = new Set(storeNames);
        _this._backendTransaction = backendTransaction;
        _this.mode = mode;
        _this.db = db;
        _this.objectStoreNames = fakeDOMStringList_1.default(Array.from(_this._scope).sort());
        _this.db._transactions.push(_this);
        return _this;
    }
    Object.defineProperty(BridgeIDBTransaction.prototype, "_backend", {
        get: function () {
            return this.db._backend;
        },
        enumerable: true,
        configurable: true
    });
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-aborting-a-transaction
    BridgeIDBTransaction.prototype._abort = function (errName) {
        return __awaiter(this, void 0, void 0, function () {
            var e, _i, _a, request, event, maybeBtx;
            var _this = this;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        this._state = "finished";
                        if (errName !== null) {
                            e = new Error();
                            e.name = errName;
                            this.error = e;
                        }
                        // Should this directly remove from _requests?
                        for (_i = 0, _a = this._requests; _i < _a.length; _i++) {
                            request = _a[_i].request;
                            if (request.readyState !== "done") {
                                request.readyState = "done"; // This will cancel execution of this request's operation
                                if (request.source) {
                                    request.result = undefined;
                                    request.error = new errors.AbortError();
                                    event = new FakeEvent.default("error", {
                                        bubbles: true,
                                        cancelable: true,
                                    });
                                    event.eventPath = [this.db, this];
                                    request.dispatchEvent(event);
                                }
                            }
                        }
                        maybeBtx = this._backendTransaction;
                        if (!maybeBtx) return [3 /*break*/, 2];
                        return [4 /*yield*/, this._backend.rollback(maybeBtx)];
                    case 1:
                        _b.sent();
                        _b.label = 2;
                    case 2:
                        queueTask_1.default(function () {
                            var event = new FakeEvent.default("abort", {
                                bubbles: true,
                                cancelable: false,
                            });
                            event.eventPath = [_this.db];
                            _this.dispatchEvent(event);
                        });
                        return [2 /*return*/];
                }
            });
        });
    };
    BridgeIDBTransaction.prototype.abort = function () {
        if (this._state === "committing" || this._state === "finished") {
            throw new errors.InvalidStateError();
        }
        this._state = "active";
        this._abort(null);
    };
    // http://w3c.github.io/IndexedDB/#dom-idbtransaction-objectstore
    BridgeIDBTransaction.prototype.objectStore = function (name) {
        if (this._state !== "active") {
            throw new errors.InvalidStateError();
        }
        var objectStore = this._objectStoresCache.get(name);
        if (objectStore !== undefined) {
            return objectStore;
        }
        return new BridgeIDBObjectStore_1.default(this, name);
    };
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-asynchronously-executing-a-request
    BridgeIDBTransaction.prototype._execRequestAsync = function (obj) {
        var source = obj.source;
        var operation = obj.operation;
        var request = obj.hasOwnProperty("request") ? obj.request : null;
        if (this._state !== "active") {
            throw new errors.TransactionInactiveError();
        }
        // Request should only be passed for cursors
        if (!request) {
            if (!source) {
                // Special requests like indexes that just need to run some code
                request = new BridgeIDBRequest_1.default();
            }
            else {
                request = new BridgeIDBRequest_1.default();
                request.source = source;
                request.transaction = source.transaction;
            }
        }
        this._requests.push({
            operation: operation,
            request: request,
        });
        return request;
    };
    /**
     * Actually execute the scheduled work for this transaction.
     */
    BridgeIDBTransaction.prototype._start = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, operation, request, r, event, result, err_1, event, idx;
            var _this = this;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        if (BridgeIDBFactory_1.default.enableTracing) {
                            console.log("TRACE: IDBTransaction._start, " + this._requests.length + " queued");
                        }
                        this._started = true;
                        if (!!this._backendTransaction) return [3 /*break*/, 2];
                        _a = this;
                        return [4 /*yield*/, this._backend.beginTransaction(this.db._backendConnection, Array.from(this._scope), this.mode)];
                    case 1:
                        _a._backendTransaction = _b.sent();
                        _b.label = 2;
                    case 2:
                        while (this._requests.length > 0) {
                            r = this._requests.shift();
                            // This should only be false if transaction was aborted
                            if (r && r.request.readyState !== "done") {
                                request = r.request;
                                operation = r.operation;
                                break;
                            }
                        }
                        if (!(request && operation)) return [3 /*break*/, 9];
                        if (!!request.source) return [3 /*break*/, 4];
                        // Special requests like indexes that just need to run some code, with error handling already built into
                        // operation
                        return [4 /*yield*/, operation()];
                    case 3:
                        // Special requests like indexes that just need to run some code, with error handling already built into
                        // operation
                        _b.sent();
                        return [3 /*break*/, 8];
                    case 4:
                        event = void 0;
                        _b.label = 5;
                    case 5:
                        _b.trys.push([5, 7, , 8]);
                        BridgeIDBFactory_1.default.enableTracing &&
                            console.log("TRACE: running operation in transaction");
                        return [4 /*yield*/, operation()];
                    case 6:
                        result = _b.sent();
                        BridgeIDBFactory_1.default.enableTracing &&
                            console.log("TRACE: operation in transaction finished with success");
                        request.readyState = "done";
                        request.result = result;
                        request.error = undefined;
                        // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-a-success-event
                        if (this._state === "inactive") {
                            this._state = "active";
                        }
                        event = new FakeEvent.default("success", {
                            bubbles: false,
                            cancelable: false,
                        });
                        try {
                            event.eventPath = [request, this, this.db];
                            request.dispatchEvent(event);
                        }
                        catch (err) {
                            if (this._state !== "committing") {
                                this._abort("AbortError");
                            }
                            throw err;
                        }
                        return [3 /*break*/, 8];
                    case 7:
                        err_1 = _b.sent();
                        if (BridgeIDBFactory_1.default.enableTracing) {
                            console.log("TRACING: error during operation: ", err_1);
                        }
                        request.readyState = "done";
                        request.result = undefined;
                        request.error = err_1;
                        // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-fire-an-error-event
                        if (this._state === "inactive") {
                            this._state = "active";
                        }
                        event = new FakeEvent.default("error", {
                            bubbles: true,
                            cancelable: true,
                        });
                        try {
                            event.eventPath = [this.db, this];
                            request.dispatchEvent(event);
                        }
                        catch (err) {
                            if (this._state !== "committing") {
                                this._abort("AbortError");
                            }
                            throw err;
                        }
                        if (!event.canceled) {
                            this._abort(err_1.name);
                        }
                        return [3 /*break*/, 8];
                    case 8:
                        // On to the next one
                        if (this._requests.length > 0) {
                            this._start();
                        }
                        else {
                            // Give it another chance for new handlers to be set before finishing
                            queueTask_1.default(function () { return _this._start(); });
                        }
                        return [2 /*return*/];
                    case 9:
                        if (!(this._state !== "finished" && this._state !== "committing")) return [3 /*break*/, 11];
                        if (BridgeIDBFactory_1.default.enableTracing) {
                            console.log("finishing transaction");
                        }
                        this._state = "committing";
                        return [4 /*yield*/, this._backend.commit(this._backendTransaction)];
                    case 10:
                        _b.sent();
                        this._state = "finished";
                        if (!this.error) {
                            if (BridgeIDBFactory_1.default.enableTracing) {
                                console.log("dispatching 'complete' event on transaction");
                            }
                            event = new FakeEvent.default("complete");
                            event.eventPath = [this, this.db];
                            this.dispatchEvent(event);
                        }
                        idx = this.db._transactions.indexOf(this);
                        if (idx < 0) {
                            throw Error("invariant failed");
                        }
                        this.db._transactions.splice(idx, 1);
                        this._resolveWait();
                        _b.label = 11;
                    case 11: return [2 /*return*/];
                }
            });
        });
    };
    BridgeIDBTransaction.prototype.commit = function () {
        if (this._state !== "active") {
            throw new errors.InvalidStateError();
        }
        this._state = "committing";
        // We now just wait for auto-commit ...
    };
    BridgeIDBTransaction.prototype.toString = function () {
        return "[object IDBRequest]";
    };
    BridgeIDBTransaction.prototype._waitDone = function () {
        return this._waitPromise;
    };
    return BridgeIDBTransaction;
}(FakeEventTarget_1.default));
exports.default = BridgeIDBTransaction;

});

unwrapExports(BridgeIDBTransaction_1);

var BridgeIDBDatabase_1 = createCommonjsModule(function (module, exports) {
/*
 * Copyright 2017 Jeremy Scheff
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });






/**
 * Ensure that an active version change transaction is currently running.
 */
var confirmActiveVersionchangeTransaction = function (database) {
    if (!database._runningVersionchangeTransaction) {
        throw new errors.InvalidStateError();
    }
    // Find the latest versionchange transaction
    var transactions = database._transactions.filter(function (tx) {
        return tx.mode === "versionchange";
    });
    var transaction = transactions[transactions.length - 1];
    if (!transaction || transaction._state === "finished") {
        throw new errors.InvalidStateError();
    }
    if (transaction._state !== "active") {
        throw new errors.TransactionInactiveError();
    }
    return transaction;
};
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-interface
var BridgeIDBDatabase = /** @class */ (function (_super) {
    __extends(BridgeIDBDatabase, _super);
    function BridgeIDBDatabase(backend, backendConnection) {
        var _this = _super.call(this) || this;
        _this._closePending = false;
        _this._closed = false;
        _this._runningVersionchangeTransaction = false;
        _this._transactions = [];
        _this._schema = backend.getSchema(backendConnection);
        _this._backend = backend;
        _this._backendConnection = backendConnection;
        return _this;
    }
    Object.defineProperty(BridgeIDBDatabase.prototype, "name", {
        get: function () {
            return this._schema.databaseName;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBDatabase.prototype, "version", {
        get: function () {
            return this._schema.databaseVersion;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BridgeIDBDatabase.prototype, "objectStoreNames", {
        get: function () {
            return fakeDOMStringList_1.default(Object.keys(this._schema.objectStores)).sort();
        },
        enumerable: true,
        configurable: true
    });
    /**
     * http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-closing-steps
     */
    BridgeIDBDatabase.prototype._closeConnection = function () {
        var _this = this;
        this._closePending = true;
        var transactionsComplete = this._transactions.every(function (transaction) {
            return transaction._state === "finished";
        });
        if (transactionsComplete) {
            this._closed = true;
            this._backend.close(this._backendConnection);
        }
        else {
            queueTask_1.default(function () {
                _this._closeConnection();
            });
        }
    };
    // http://w3c.github.io/IndexedDB/#dom-idbdatabase-createobjectstore
    BridgeIDBDatabase.prototype.createObjectStore = function (name, options) {
        if (options === void 0) { options = {}; }
        if (name === undefined) {
            throw new TypeError();
        }
        var transaction = confirmActiveVersionchangeTransaction(this);
        var backendTx = transaction._backendTransaction;
        if (!backendTx) {
            throw Error("invariant violated");
        }
        var keyPath = options !== null && options.keyPath !== undefined
            ? options.keyPath
            : null;
        var autoIncrement = options !== null && options.autoIncrement !== undefined
            ? options.autoIncrement
            : false;
        if (keyPath !== null) {
            validateKeyPath_1.default(keyPath);
        }
        if (Object.keys(this._schema.objectStores).includes(name)) {
            throw new errors.ConstraintError();
        }
        if (autoIncrement && (keyPath === "" || Array.isArray(keyPath))) {
            throw new errors.InvalidAccessError();
        }
        transaction._backend.createObjectStore(backendTx, name, keyPath, autoIncrement);
        this._schema = this._backend.getSchema(this._backendConnection);
        return transaction.objectStore(name);
    };
    BridgeIDBDatabase.prototype.deleteObjectStore = function (name) {
        if (name === undefined) {
            throw new TypeError();
        }
        var transaction = confirmActiveVersionchangeTransaction(this);
        transaction._objectStoresCache.delete(name);
    };
    BridgeIDBDatabase.prototype._internalTransaction = function (storeNames, mode, backendTransaction) {
        var _this = this;
        mode = mode !== undefined ? mode : "readonly";
        if (mode !== "readonly" &&
            mode !== "readwrite" &&
            mode !== "versionchange") {
            throw new TypeError("Invalid mode: " + mode);
        }
        var hasActiveVersionchange = this._transactions.some(function (transaction) {
            return (transaction._state === "active" &&
                transaction.mode === "versionchange" &&
                transaction.db === _this);
        });
        if (hasActiveVersionchange) {
            throw new errors.InvalidStateError();
        }
        if (this._closePending) {
            throw new errors.InvalidStateError();
        }
        if (!Array.isArray(storeNames)) {
            storeNames = [storeNames];
        }
        if (storeNames.length === 0 && mode !== "versionchange") {
            throw new errors.InvalidAccessError();
        }
        for (var _i = 0, storeNames_1 = storeNames; _i < storeNames_1.length; _i++) {
            var storeName = storeNames_1[_i];
            if (this.objectStoreNames.indexOf(storeName) < 0) {
                throw new errors.NotFoundError("No objectStore named " + storeName + " in this database");
            }
        }
        var tx = new BridgeIDBTransaction_1.default(storeNames, mode, this, backendTransaction);
        this._transactions.push(tx);
        queueTask_1.default(function () { return tx._start(); });
        return tx;
    };
    BridgeIDBDatabase.prototype.transaction = function (storeNames, mode) {
        if (mode === "versionchange") {
            throw new TypeError("Invalid mode: " + mode);
        }
        return this._internalTransaction(storeNames, mode);
    };
    BridgeIDBDatabase.prototype.close = function () {
        this._closeConnection();
    };
    BridgeIDBDatabase.prototype.toString = function () {
        return "[object IDBDatabase]";
    };
    return BridgeIDBDatabase;
}(FakeEventTarget_1.default));
exports.default = BridgeIDBDatabase;

});

unwrapExports(BridgeIDBDatabase_1);

var BridgeIDBOpenDBRequest_1 = createCommonjsModule(function (module, exports) {
/*
  Copyright 2019 Florian Dold
  Copyright 2017 Jeremy Scheff

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing
  permissions and limitations under the License.
*/
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });

var BridgeIDBOpenDBRequest = /** @class */ (function (_super) {
    __extends(BridgeIDBOpenDBRequest, _super);
    function BridgeIDBOpenDBRequest() {
        var _this = _super.call(this) || this;
        _this.onupgradeneeded = null;
        _this.onblocked = null;
        // https://www.w3.org/TR/IndexedDB/#open-requests
        _this.source = null;
        return _this;
    }
    BridgeIDBOpenDBRequest.prototype.toString = function () {
        return "[object IDBOpenDBRequest]";
    };
    return BridgeIDBOpenDBRequest;
}(BridgeIDBRequest_1.default));
exports.default = BridgeIDBOpenDBRequest;

});

unwrapExports(BridgeIDBOpenDBRequest_1);

var BridgeIDBVersionChangeEvent_1 = createCommonjsModule(function (module, exports) {
/*
  Copyright 2019 Florian Dold
  Copyright 2017 Jeremy Scheff

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  or implied. See the License for the specific language governing
  permissions and limitations under the License.
 */
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });

var BridgeIDBVersionChangeEvent = /** @class */ (function (_super) {
    __extends(BridgeIDBVersionChangeEvent, _super);
    function BridgeIDBVersionChangeEvent(type, parameters) {
        if (parameters === void 0) { parameters = {}; }
        var _this = _super.call(this, type) || this;
        _this.newVersion =
            parameters.newVersion !== undefined ? parameters.newVersion : null;
        _this.oldVersion =
            parameters.oldVersion !== undefined ? parameters.oldVersion : 0;
        return _this;
    }
    BridgeIDBVersionChangeEvent.prototype.toString = function () {
        return "[object IDBVersionChangeEvent]";
    };
    return BridgeIDBVersionChangeEvent;
}(FakeEvent.default));
exports.default = BridgeIDBVersionChangeEvent;

});

unwrapExports(BridgeIDBVersionChangeEvent_1);

var enforceRange_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
// https://heycam.github.io/webidl/#EnforceRange
var enforceRange = function (num, type) {
    var min = 0;
    var max = type === "unsigned long" ? 4294967295 : 9007199254740991;
    if (isNaN(num) || num < min || num > max) {
        throw new TypeError();
    }
    if (num >= 0) {
        return Math.floor(num);
    }
};
exports.default = enforceRange;

});

unwrapExports(enforceRange_1);

var BridgeIDBFactory_1 = createCommonjsModule(function (module, exports) {
/*
 * Copyright 2017 Jeremy Scheff
 * Copyright 2019 Florian Dold
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });








var BridgeIDBFactory = /** @class */ (function () {
    function BridgeIDBFactory(backend) {
        this.cmp = cmp.default;
        this.connections = [];
        this.backend = backend;
    }
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBFactory-deleteDatabase-IDBOpenDBRequest-DOMString-name
    BridgeIDBFactory.prototype.deleteDatabase = function (name) {
        var _this = this;
        var request = new BridgeIDBOpenDBRequest_1.default();
        request.source = null;
        queueTask_1.default(function () { return __awaiter(_this, void 0, void 0, function () {
            var databases, dbInfo, event, oldVersion, dbconn, backendTransaction, event2, err_1, event;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.backend.getDatabases()];
                    case 1:
                        databases = _a.sent();
                        dbInfo = databases.find(function (x) { return x.name == name; });
                        if (!dbInfo) {
                            event = new BridgeIDBVersionChangeEvent_1.default("success", {
                                newVersion: null,
                                oldVersion: 0,
                            });
                            request.dispatchEvent(event);
                            return [2 /*return*/];
                        }
                        oldVersion = dbInfo.version;
                        _a.label = 2;
                    case 2:
                        _a.trys.push([2, 8, , 9]);
                        return [4 /*yield*/, this.backend.connectDatabase(name)];
                    case 3:
                        dbconn = _a.sent();
                        return [4 /*yield*/, this.backend.enterVersionChange(dbconn, 0)];
                    case 4:
                        backendTransaction = _a.sent();
                        return [4 /*yield*/, this.backend.deleteDatabase(backendTransaction, name)];
                    case 5:
                        _a.sent();
                        return [4 /*yield*/, this.backend.commit(backendTransaction)];
                    case 6:
                        _a.sent();
                        return [4 /*yield*/, this.backend.close(dbconn)];
                    case 7:
                        _a.sent();
                        request.result = undefined;
                        request.readyState = "done";
                        event2 = new BridgeIDBVersionChangeEvent_1.default("success", {
                            newVersion: null,
                            oldVersion: oldVersion,
                        });
                        request.dispatchEvent(event2);
                        return [3 /*break*/, 9];
                    case 8:
                        err_1 = _a.sent();
                        request.error = new Error();
                        request.error.name = err_1.name;
                        request.readyState = "done";
                        event = new FakeEvent.default("error", {
                            bubbles: true,
                            cancelable: true,
                        });
                        event.eventPath = [];
                        request.dispatchEvent(event);
                        return [3 /*break*/, 9];
                    case 9: return [2 /*return*/];
                }
            });
        }); });
        return request;
    };
    // tslint:disable-next-line max-line-length
    // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBFactory-open-IDBOpenDBRequest-DOMString-name-unsigned-long-long-version
    BridgeIDBFactory.prototype.open = function (name, version) {
        var _this = this;
        if (arguments.length > 1 && version !== undefined) {
            // Based on spec, not sure why "MAX_SAFE_INTEGER" instead of "unsigned long long", but it's needed to pass
            // tests
            version = enforceRange_1.default(version, "MAX_SAFE_INTEGER");
        }
        if (version === 0) {
            throw new TypeError();
        }
        var request = new BridgeIDBOpenDBRequest_1.default();
        queueTask_1.default(function () { return __awaiter(_this, void 0, void 0, function () {
            var dbconn, err_2, schema, existingVersion, requestedVersion, db, event2, _i, _a, otherConn, event_1, event_2, backendTransaction, transaction, event, event2;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _b.trys.push([0, 2, , 3]);
                        return [4 /*yield*/, this.backend.connectDatabase(name)];
                    case 1:
                        dbconn = _b.sent();
                        return [3 /*break*/, 3];
                    case 2:
                        err_2 = _b.sent();
                        request._finishWithError(err_2);
                        return [2 /*return*/];
                    case 3:
                        schema = this.backend.getSchema(dbconn);
                        existingVersion = schema.databaseVersion;
                        if (version === undefined) {
                            version = existingVersion !== 0 ? existingVersion : 1;
                        }
                        requestedVersion = version;
                        BridgeIDBFactory.enableTracing &&
                            console.log("TRACE: existing version " + existingVersion + ", requested version " + requestedVersion);
                        if (existingVersion > requestedVersion) {
                            request._finishWithError(new errors.VersionError());
                            return [2 /*return*/];
                        }
                        db = new BridgeIDBDatabase_1.default(this.backend, dbconn);
                        if (existingVersion == requestedVersion) {
                            request.result = db;
                            request.readyState = "done";
                            event2 = new FakeEvent.default("success", {
                                bubbles: false,
                                cancelable: false,
                            });
                            event2.eventPath = [request];
                            request.dispatchEvent(event2);
                        }
                        if (!(existingVersion < requestedVersion)) return [3 /*break*/, 6];
                        // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-running-a-versionchange-transaction
                        for (_i = 0, _a = this.connections; _i < _a.length; _i++) {
                            otherConn = _a[_i];
                            event_1 = new BridgeIDBVersionChangeEvent_1.default("versionchange", {
                                newVersion: version,
                                oldVersion: existingVersion,
                            });
                            otherConn.dispatchEvent(event_1);
                        }
                        if (this._anyOpen()) {
                            event_2 = new BridgeIDBVersionChangeEvent_1.default("blocked", {
                                newVersion: version,
                                oldVersion: existingVersion,
                            });
                            request.dispatchEvent(event_2);
                        }
                        return [4 /*yield*/, this.backend.enterVersionChange(dbconn, requestedVersion)];
                    case 4:
                        backendTransaction = _b.sent();
                        db._runningVersionchangeTransaction = true;
                        transaction = db._internalTransaction([], "versionchange", backendTransaction);
                        event = new BridgeIDBVersionChangeEvent_1.default("upgradeneeded", {
                            newVersion: version,
                            oldVersion: existingVersion,
                        });
                        request.result = db;
                        request.readyState = "done";
                        request.transaction = transaction;
                        request.dispatchEvent(event);
                        return [4 /*yield*/, transaction._waitDone()];
                    case 5:
                        _b.sent();
                        // We don't explicitly exit the versionchange transaction,
                        // since this is already done by the BridgeIDBTransaction.
                        db._runningVersionchangeTransaction = false;
                        event2 = new FakeEvent.default("success", {
                            bubbles: false,
                            cancelable: false,
                        });
                        event2.eventPath = [request];
                        request.dispatchEvent(event2);
                        _b.label = 6;
                    case 6:
                        this.connections.push(db);
                        return [2 /*return*/, db];
                }
            });
        }); });
        return request;
    };
    // https://w3c.github.io/IndexedDB/#dom-idbfactory-databases
    BridgeIDBFactory.prototype.databases = function () {
        return this.backend.getDatabases();
    };
    BridgeIDBFactory.prototype.toString = function () {
        return "[object IDBFactory]";
    };
    BridgeIDBFactory.prototype._anyOpen = function () {
        return this.connections.some(function (c) { return !c._closed && !c._closePending; });
    };
    BridgeIDBFactory.enableTracing = false;
    return BridgeIDBFactory;
}());
exports.BridgeIDBFactory = BridgeIDBFactory;
exports.default = BridgeIDBFactory;

});

unwrapExports(BridgeIDBFactory_1);
var BridgeIDBFactory_2 = BridgeIDBFactory_1.BridgeIDBFactory;

var b_tree = createCommonjsModule(function (module, exports) {
/*
Copyright (c) 2018 David Piepgrass

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

SPDX-License-Identifier: MIT
*/
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
// Informative microbenchmarks & stuff:
// http://www.jayconrod.com/posts/52/a-tour-of-v8-object-representation (very educational)
// https://blog.mozilla.org/luke/2012/10/02/optimizing-javascript-variable-access/ (local vars are faster than properties)
// http://benediktmeurer.de/2017/12/13/an-introduction-to-speculative-optimization-in-v8/ (other stuff)
// https://jsperf.com/js-in-operator-vs-alternatives (avoid 'in' operator; `.p!==undefined` faster than `hasOwnProperty('p')` in all browsers)
// https://jsperf.com/instanceof-vs-typeof-vs-constructor-vs-member (speed of type tests varies wildly across browsers)
// https://jsperf.com/detecting-arrays-new (a.constructor===Array is best across browsers, assuming a is an object)
// https://jsperf.com/shallow-cloning-methods (a constructor is faster than Object.create; hand-written clone faster than Object.assign)
// https://jsperf.com/ways-to-fill-an-array (slice-and-replace is fastest)
// https://jsperf.com/math-min-max-vs-ternary-vs-if (Math.min/max is slow on Edge)
// https://jsperf.com/array-vs-property-access-speed (v.x/v.y is faster than a[0]/a[1] in major browsers IF hidden class is constant)
// https://jsperf.com/detect-not-null-or-undefined (`x==null` slightly slower than `x===null||x===undefined` on all browsers)
// Overall, microbenchmarks suggest Firefox is the fastest browser for JavaScript and Edge is the slowest.
// Lessons from https://v8project.blogspot.com/2017/09/elements-kinds-in-v8.html:
//   - Avoid holes in arrays. Avoid `new Array(N)`, it will be "holey" permanently.
//   - Don't read outside bounds of an array (it scans prototype chain).
//   - Small integer arrays are stored differently from doubles
//   - Adding non-numbers to an array deoptimizes it permanently into a general array
//   - Objects can be used like arrays (e.g. have length property) but are slower
//   - V8 source (NewElementsCapacity in src/objects.h): arrays grow by 50% + 16 elements
/** Compares two numbers, strings, arrays of numbers/strings, Dates,
 *  or objects that have a valueOf() method returning a number or string.
 *  Optimized for numbers. Returns 1 if a>b, -1 if a<b, and 0 if a===b.
 */
function defaultComparator(a, b) {
    var c = a - b;
    if (c === c)
        return c; // a & b are number
    // General case (c is NaN): string / arrays / Date / incomparable things
    if (a)
        a = a.valueOf();
    if (b)
        b = b.valueOf();
    return a < b ? -1 : a > b ? 1 : a == b ? 0 : c;
}
exports.defaultComparator = defaultComparator;
/**
 * A reasonably fast collection of key-value pairs with a powerful API.
 * Largely compatible with the standard Map. BTree is a B+ tree data structure,
 * so the collection is sorted by key.
 *
 * B+ trees tend to use memory more efficiently than hashtables such as the
 * standard Map, especially when the collection contains a large number of
 * items. However, maintaining the sort order makes them modestly slower:
 * O(log size) rather than O(1). This B+ tree implementation supports O(1)
 * fast cloning. It also supports freeze(), which can be used to ensure that
 * a BTree is not changed accidentally.
 *
 * Confusingly, the ES6 Map.forEach(c) method calls c(value,key) instead of
 * c(key,value), in contrast to other methods such as set() and entries()
 * which put the key first. I can only assume that the order was reversed on
 * the theory that users would usually want to examine values and ignore keys.
 * BTree's forEach() therefore works the same way, but a second method
 * `.forEachPair((key,value)=>{...})` is provided which sends you the key
 * first and the value second; this method is slightly faster because it is
 * the "native" for-each method for this class.
 *
 * Out of the box, BTree supports keys that are numbers, strings, arrays of
 * numbers/strings, Date, and objects that have a valueOf() method returning a
 * number or string. Other data types, such as arrays of Date or custom
 * objects, require a custom comparator, which you must pass as the second
 * argument to the constructor (the first argument is an optional list of
 * initial items). Symbols cannot be used as keys because they are unordered
 * (one Symbol is never "greater" or "less" than another).
 *
 * @example
 * Given a {name: string, age: number} object, you can create a tree sorted by
 * name and then by age like this:
 *
 *     var tree = new BTree(undefined, (a, b) => {
 *       if (a.name > b.name)
 *         return 1; // Return a number >0 when a > b
 *       else if (a.name < b.name)
 *         return -1; // Return a number <0 when a < b
 *       else // names are equal (or incomparable)
 *         return a.age - b.age; // Return >0 when a.age > b.age
 *     });
 *
 *     tree.set({name:"Bill", age:17}, "happy");
 *     tree.set({name:"Fran", age:40}, "busy & stressed");
 *     tree.set({name:"Bill", age:55}, "recently laid off");
 *     tree.forEachPair((k, v) => {
 *       console.log(`Name: ${k.name} Age: ${k.age} Status: ${v}`);
 *     });
 *
 * @description
 * The "range" methods (`forEach, forRange, editRange`) will return the number
 * of elements that were scanned. In addition, the callback can return {break:R}
 * to stop early and return R from the outer function.
 *
 * - TODO: Test performance of preallocating values array at max size
 * - TODO: Add fast initialization when a sorted array is provided to constructor
 *
 * For more documentation see https://github.com/qwertie/btree-typescript
 *
 * Are you a C# developer? You might like the similar data structures I made for C#:
 * BDictionary, BList, etc. See http://core.loyc.net/collections/
 *
 * @author David Piepgrass
 */
var BTree = /** @class */ (function () {
    /**
     * Initializes an empty B+ tree.
     * @param compare Custom function to compare pairs of elements in the tree.
     *   This is not required for numbers, strings and arrays of numbers/strings.
     * @param entries A set of key-value pairs to initialize the tree
     * @param maxNodeSize Branching factor (maximum items or children per node)
     *   Must be in range 4..256. If undefined or <4 then default is used; if >256 then 256.
     */
    function BTree(entries, compare, maxNodeSize) {
        this._root = EmptyLeaf;
        this._size = 0;
        this._maxNodeSize = maxNodeSize >= 4 ? Math.min(maxNodeSize, 256) : 32;
        this._compare = compare || defaultComparator;
        if (entries)
            this.setPairs(entries);
    }
    Object.defineProperty(BTree.prototype, "size", {
        // ES6 Map<K,V> methods ///////////////////////////////////////////////////
        /** Gets the number of key-value pairs in the tree. */
        get: function () { return this._size; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BTree.prototype, "length", {
        /** Gets the number of key-value pairs in the tree. */
        get: function () { return this._size; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BTree.prototype, "isEmpty", {
        /** Returns true iff the tree contains no key-value pairs. */
        get: function () { return this._size === 0; },
        enumerable: true,
        configurable: true
    });
    /** Releases the tree so that its size is 0. */
    BTree.prototype.clear = function () {
        this._root = EmptyLeaf;
        this._size = 0;
    };
    /** Runs a function for each key-value pair, in order from smallest to
     *  largest key. For compatibility with ES6 Map, the argument order to
     *  the callback is backwards: value first, then key. Call forEachPair
     *  instead to receive the key as the first argument.
     * @param thisArg If provided, this parameter is assigned as the `this`
     *        value for each callback.
     * @returns the number of values that were sent to the callback,
     *        or the R value if the callback returned {break:R}. */
    BTree.prototype.forEach = function (callback, thisArg) {
        var _this = this;
        if (thisArg !== undefined)
            callback = callback.bind(thisArg);
        return this.forEachPair(function (k, v) { return callback(v, k, _this); });
    };
    /** Runs a function for each key-value pair, in order from smallest to
     *  largest key. The callback can return {break:R} (where R is any value
     *  except undefined) to stop immediately and return R from forEachPair.
     * @param onFound A function that is called for each key-value pair. This
     *        function can return {break:R} to stop early with result R.
     *        The reason that you must return {break:R} instead of simply R
     *        itself is for consistency with editRange(), which allows
     *        multiple actions, not just breaking.
     * @param initialCounter This is the value of the third argument of
     *        `onFound` the first time it is called. The counter increases
     *        by one each time `onFound` is called. Default value: 0
     * @returns the number of pairs sent to the callback (plus initialCounter,
     *        if you provided one). If the callback returned {break:R} then
     *        the R value is returned instead. */
    BTree.prototype.forEachPair = function (callback, initialCounter) {
        var low = this.minKey(), high = this.maxKey();
        return this.forRange(low, high, true, callback, initialCounter);
    };
    /**
     * Finds a pair in the tree and returns the associated value.
     * @param defaultValue a value to return if the key was not found.
     * @returns the value, or defaultValue if the key was not found.
     * @description Computational complexity: O(log size)
     */
    BTree.prototype.get = function (key, defaultValue) {
        return this._root.get(key, defaultValue, this);
    };
    /**
     * Adds or overwrites a key-value pair in the B+ tree.
     * @param key the key is used to determine the sort order of
     *        data in the tree.
     * @param value data to associate with the key (optional)
     * @param overwrite Whether to overwrite an existing key-value pair
     *        (default: true). If this is false and there is an existing
     *        key-value pair then this method has no effect.
     * @returns true if a new key-value pair was added.
     * @description Computational complexity: O(log size)
     * Note: when overwriting a previous entry, the key is updated
     * as well as the value. This has no effect unless the new key
     * has data that does not affect its sort order.
     */
    BTree.prototype.set = function (key, value, overwrite) {
        if (this._root.isShared)
            this._root = this._root.clone();
        var result = this._root.set(key, value, overwrite, this);
        if (result === true || result === false)
            return result;
        // Root node has split, so create a new root node.
        this._root = new BNodeInternal([this._root, result]);
        return true;
    };
    /**
     * Returns true if the key exists in the B+ tree, false if not.
     * Use get() for best performance; use has() if you need to
     * distinguish between "undefined value" and "key not present".
     * @param key Key to detect
     * @description Computational complexity: O(log size)
     */
    BTree.prototype.has = function (key) {
        return this.forRange(key, key, true, undefined) !== 0;
    };
    /**
     * Removes a single key-value pair from the B+ tree.
     * @param key Key to find
     * @returns true if a pair was found and removed, false otherwise.
     * @description Computational complexity: O(log size)
     */
    BTree.prototype.delete = function (key) {
        return this.editRange(key, key, true, DeleteRange) !== 0;
    };
    BTree.prototype.with = function (key, value, overwrite) {
        var nu = this.clone();
        return nu.set(key, value, overwrite) || overwrite ? nu : this;
    };
    /** Returns a copy of the tree with the specified key-value pairs set. */
    BTree.prototype.withPairs = function (pairs, overwrite) {
        var nu = this.clone();
        return nu.setPairs(pairs, overwrite) !== 0 || overwrite ? nu : this;
    };
    /** Returns a copy of the tree with the specified keys present.
     *  @param keys The keys to add. If a key is already present in the tree,
     *         neither the existing key nor the existing value is modified.
     *  @param returnThisIfUnchanged if true, returns this if all keys already
     *  existed. Performance note: due to the architecture of this class, all
     *  node(s) leading to existing keys are cloned even if the collection is
     *  ultimately unchanged.
    */
    BTree.prototype.withKeys = function (keys, returnThisIfUnchanged) {
        var nu = this.clone(), changed = false;
        for (var i = 0; i < keys.length; i++)
            changed = nu.set(keys[i], undefined, false) || changed;
        return returnThisIfUnchanged && !changed ? this : nu;
    };
    /** Returns a copy of the tree with the specified key removed.
     * @param returnThisIfUnchanged if true, returns this if the key didn't exist.
     *  Performance note: due to the architecture of this class, node(s) leading
     *  to where the key would have been stored are cloned even when the key
     *  turns out not to exist and the collection is unchanged.
     */
    BTree.prototype.without = function (key, returnThisIfUnchanged) {
        return this.withoutRange(key, key, true, returnThisIfUnchanged);
    };
    /** Returns a copy of the tree with the specified keys removed.
     * @param returnThisIfUnchanged if true, returns this if none of the keys
     *  existed. Performance note: due to the architecture of this class,
     *  node(s) leading to where the key would have been stored are cloned
     *  even when the key turns out not to exist.
     */
    BTree.prototype.withoutKeys = function (keys, returnThisIfUnchanged) {
        var nu = this.clone();
        return nu.deleteKeys(keys) || !returnThisIfUnchanged ? nu : this;
    };
    /** Returns a copy of the tree with the specified range of keys removed. */
    BTree.prototype.withoutRange = function (low, high, includeHigh, returnThisIfUnchanged) {
        var nu = this.clone();
        if (nu.deleteRange(low, high, includeHigh) === 0 && returnThisIfUnchanged)
            return this;
        return nu;
    };
    /** Returns a copy of the tree with pairs removed whenever the callback
     *  function returns false. `where()` is a synonym for this method. */
    BTree.prototype.filter = function (callback, returnThisIfUnchanged) {
        var nu = this.greedyClone();
        var del;
        nu.editAll(function (k, v, i) {
            if (!callback(k, v, i))
                return del = Delete;
        });
        if (!del && returnThisIfUnchanged)
            return this;
        return nu;
    };
    /** Returns a copy of the tree with all values altered by a callback function. */
    BTree.prototype.mapValues = function (callback) {
        var tmp = {};
        var nu = this.greedyClone();
        nu.editAll(function (k, v, i) {
            return tmp.value = callback(v, k, i), tmp;
        });
        return nu;
    };
    BTree.prototype.reduce = function (callback, initialValue) {
        var i = 0, p = initialValue;
        var it = this.entries(this.minKey(), ReusedArray), next;
        while (!(next = it.next()).done)
            p = callback(p, next.value, i++, this);
        return p;
    };
    // Iterator methods ///////////////////////////////////////////////////////
    /** Returns an iterator that provides items in order (ascending order if
     *  the collection's comparator uses ascending order, as is the default.)
     *  @param lowestKey First key to be iterated, or undefined to start at
     *         minKey(). If the specified key doesn't exist then iteration
     *         starts at the next higher key (according to the comparator).
     *  @param reusedArray Optional array used repeatedly to store key-value
     *         pairs, to avoid creating a new array on every iteration.
     */
    BTree.prototype.entries = function (lowestKey, reusedArray) {
        var info = this.findPath(lowestKey);
        if (info === undefined)
            return iterator();
        var nodequeue = info.nodequeue, nodeindex = info.nodeindex, leaf = info.leaf;
        var state = reusedArray !== undefined ? 1 : 0;
        var i = (lowestKey === undefined ? -1 : leaf.indexOf(lowestKey, 0, this._compare) - 1);
        return iterator(function () {
            jump: for (;;) {
                switch (state) {
                    case 0:
                        if (++i < leaf.keys.length)
                            return { done: false, value: [leaf.keys[i], leaf.values[i]] };
                        state = 2;
                        continue;
                    case 1:
                        if (++i < leaf.keys.length) {
                            reusedArray[0] = leaf.keys[i], reusedArray[1] = leaf.values[i];
                            return { done: false, value: reusedArray };
                        }
                        state = 2;
                    case 2:
                        // Advance to the next leaf node
                        for (var level = -1;;) {
                            if (++level >= nodequeue.length) {
                                state = 3;
                                continue jump;
                            }
                            if (++nodeindex[level] < nodequeue[level].length)
                                break;
                        }
                        for (; level > 0; level--) {
                            nodequeue[level - 1] = nodequeue[level][nodeindex[level]].children;
                            nodeindex[level - 1] = 0;
                        }
                        leaf = nodequeue[0][nodeindex[0]];
                        i = -1;
                        state = reusedArray !== undefined ? 1 : 0;
                        continue;
                    case 3:
                        return { done: true, value: undefined };
                }
            }
        });
    };
    /** Returns an iterator that provides items in reversed order.
     *  @param highestKey Key at which to start iterating, or undefined to
     *         start at minKey(). If the specified key doesn't exist then iteration
     *         starts at the next lower key (according to the comparator).
     *  @param reusedArray Optional array used repeatedly to store key-value
     *         pairs, to avoid creating a new array on every iteration.
     *  @param skipHighest Iff this flag is true and the highestKey exists in the
     *         collection, the pair matching highestKey is skipped, not iterated.
     */
    BTree.prototype.entriesReversed = function (highestKey, reusedArray, skipHighest) {
        if ((highestKey = highestKey || this.maxKey()) === undefined)
            return iterator(); // collection is empty
        var _a = this.findPath(highestKey) || this.findPath(this.maxKey()), nodequeue = _a.nodequeue, nodeindex = _a.nodeindex, leaf = _a.leaf;
        check(!nodequeue[0] || leaf === nodequeue[0][nodeindex[0]], "wat!");
        var i = leaf.indexOf(highestKey, 0, this._compare);
        if (!(skipHighest || this._compare(leaf.keys[i], highestKey) > 0))
            i++;
        var state = reusedArray !== undefined ? 1 : 0;
        return iterator(function () {
            jump: for (;;) {
                switch (state) {
                    case 0:
                        if (--i >= 0)
                            return { done: false, value: [leaf.keys[i], leaf.values[i]] };
                        state = 2;
                        continue;
                    case 1:
                        if (--i >= 0) {
                            reusedArray[0] = leaf.keys[i], reusedArray[1] = leaf.values[i];
                            return { done: false, value: reusedArray };
                        }
                        state = 2;
                    case 2:
                        // Advance to the next leaf node
                        for (var level = -1;;) {
                            if (++level >= nodequeue.length) {
                                state = 3;
                                continue jump;
                            }
                            if (--nodeindex[level] >= 0)
                                break;
                        }
                        for (; level > 0; level--) {
                            nodequeue[level - 1] = nodequeue[level][nodeindex[level]].children;
                            nodeindex[level - 1] = nodequeue[level - 1].length - 1;
                        }
                        leaf = nodequeue[0][nodeindex[0]];
                        i = leaf.keys.length;
                        state = reusedArray !== undefined ? 1 : 0;
                        continue;
                    case 3:
                        return { done: true, value: undefined };
                }
            }
        });
    };
    /* Used by entries() and entriesReversed() to prepare to start iterating.
     * It develops a "node queue" for each non-leaf level of the tree.
     * Levels are numbered "bottom-up" so that level 0 is a list of leaf
     * nodes from a low-level non-leaf node. The queue at a given level L
     * consists of nodequeue[L] which is the children of a BNodeInternal,
     * and nodeindex[L], the current index within that child list, such
     * such that nodequeue[L-1] === nodequeue[L][nodeindex[L]].children.
     * (However inside this function the order is reversed.)
     */
    BTree.prototype.findPath = function (key) {
        var nextnode = this._root;
        var nodequeue, nodeindex;
        if (nextnode.isLeaf) {
            nodequeue = EmptyArray, nodeindex = EmptyArray; // avoid allocations
        }
        else {
            nodequeue = [], nodeindex = [];
            for (var d = 0; !nextnode.isLeaf; d++) {
                nodequeue[d] = nextnode.children;
                nodeindex[d] = key === undefined ? 0 : nextnode.indexOf(key, 0, this._compare);
                if (nodeindex[d] >= nodequeue[d].length)
                    return; // first key > maxKey()
                nextnode = nodequeue[d][nodeindex[d]];
            }
            nodequeue.reverse();
            nodeindex.reverse();
        }
        return { nodequeue: nodequeue, nodeindex: nodeindex, leaf: nextnode };
    };
    /** Returns a new iterator for iterating the keys of each pair in ascending order.
     *  @param firstKey: Minimum key to include in the output. */
    BTree.prototype.keys = function (firstKey) {
        var it = this.entries(firstKey, ReusedArray);
        return iterator(function () {
            var n = it.next();
            if (n.value)
                n.value = n.value[0];
            return n;
        });
    };
    /** Returns a new iterator for iterating the values of each pair in order by key.
     *  @param firstKey: Minimum key whose associated value is included in the output. */
    BTree.prototype.values = function (firstKey) {
        var it = this.entries(firstKey, ReusedArray);
        return iterator(function () {
            var n = it.next();
            if (n.value)
                n.value = n.value[1];
            return n;
        });
    };
    Object.defineProperty(BTree.prototype, "maxNodeSize", {
        // Additional methods /////////////////////////////////////////////////////
        /** Returns the maximum number of children/values before nodes will split. */
        get: function () {
            return this._maxNodeSize;
        },
        enumerable: true,
        configurable: true
    });
    /** Gets the lowest key in the tree. Complexity: O(log size) */
    BTree.prototype.minKey = function () { return this._root.minKey(); };
    /** Gets the highest key in the tree. Complexity: O(1) */
    BTree.prototype.maxKey = function () { return this._root.maxKey(); };
    /** Quickly clones the tree by marking the root node as shared.
     *  Both copies remain editable. When you modify either copy, any
     *  nodes that are shared (or potentially shared) between the two
     *  copies are cloned so that the changes do not affect other copies.
     *  This is known as copy-on-write behavior, or "lazy copying". */
    BTree.prototype.clone = function () {
        this._root.isShared = true;
        var result = new BTree(undefined, this._compare, this._maxNodeSize);
        result._root = this._root;
        result._size = this._size;
        return result;
    };
    /** Performs a greedy clone, immediately duplicating any nodes that are
     *  not currently marked as shared, in order to avoid marking any nodes
     *  as shared.
     *  @param force Clone all nodes, even shared ones.
     */
    BTree.prototype.greedyClone = function (force) {
        var result = new BTree(undefined, this._compare, this._maxNodeSize);
        result._root = this._root.greedyClone(force);
        result._size = this._size;
        return result;
    };
    /** Gets an array filled with the contents of the tree, sorted by key */
    BTree.prototype.toArray = function (maxLength) {
        if (maxLength === void 0) { maxLength = 0x7FFFFFFF; }
        var min = this.minKey(), max = this.maxKey();
        if (min !== undefined)
            return this.getRange(min, max, true, maxLength);
        return [];
    };
    /** Gets an array of all keys, sorted */
    BTree.prototype.keysArray = function () {
        var results = [];
        this._root.forRange(this.minKey(), this.maxKey(), true, false, this, 0, function (k, v) { results.push(k); });
        return results;
    };
    /** Gets an array of all values, sorted by key */
    BTree.prototype.valuesArray = function () {
        var results = [];
        this._root.forRange(this.minKey(), this.maxKey(), true, false, this, 0, function (k, v) { results.push(v); });
        return results;
    };
    /** Gets a string representing the tree's data based on toArray(). */
    BTree.prototype.toString = function () {
        return this.toArray().toString();
    };
    /** Stores a key-value pair only if the key doesn't already exist in the tree.
     * @returns true if a new key was added
    */
    BTree.prototype.setIfNotPresent = function (key, value) {
        return this.set(key, value, false);
    };
    /** Returns the next pair whose key is larger than the specified key (or undefined if there is none) */
    BTree.prototype.nextHigherPair = function (key) {
        var it = this.entries(key, ReusedArray);
        var r = it.next();
        if (!r.done && this._compare(r.value[0], key) <= 0)
            r = it.next();
        return r.value;
    };
    /** Returns the next key larger than the specified key (or undefined if there is none) */
    BTree.prototype.nextHigherKey = function (key) {
        var p = this.nextHigherPair(key);
        return p ? p[0] : p;
    };
    /** Returns the next pair whose key is smaller than the specified key (or undefined if there is none) */
    BTree.prototype.nextLowerPair = function (key) {
        var it = this.entriesReversed(key, ReusedArray, true);
        return it.next().value;
    };
    /** Returns the next key smaller than the specified key (or undefined if there is none) */
    BTree.prototype.nextLowerKey = function (key) {
        var p = this.nextLowerPair(key);
        return p ? p[0] : p;
    };
    /** Edits the value associated with a key in the tree, if it already exists.
     * @returns true if the key existed, false if not.
    */
    BTree.prototype.changeIfPresent = function (key, value) {
        return this.editRange(key, key, true, function (k, v) { return ({ value: value }); }) !== 0;
    };
    /**
     * Builds an array of pairs from the specified range of keys, sorted by key.
     * Each returned pair is also an array: pair[0] is the key, pair[1] is the value.
     * @param low The first key in the array will be greater than or equal to `low`.
     * @param high This method returns when a key larger than this is reached.
     * @param includeHigh If the `high` key is present, its pair will be included
     *        in the output if and only if this parameter is true. Note: if the
     *        `low` key is present, it is always included in the output.
     * @param maxLength Length limit. getRange will stop scanning the tree when
     *                  the array reaches this size.
     * @description Computational complexity: O(result.length + log size)
     */
    BTree.prototype.getRange = function (low, high, includeHigh, maxLength) {
        if (maxLength === void 0) { maxLength = 0x3FFFFFF; }
        var results = [];
        this._root.forRange(low, high, includeHigh, false, this, 0, function (k, v) {
            results.push([k, v]);
            return results.length > maxLength ? Break : undefined;
        });
        return results;
    };
    /** Adds all pairs from a list of key-value pairs.
     * @param pairs Pairs to add to this tree. If there are duplicate keys,
     *        later pairs currently overwrite earlier ones (e.g. [[0,1],[0,7]]
     *        associates 0 with 7.)
     * @param overwrite Whether to overwrite pairs that already exist (if false,
     *        pairs[i] is ignored when the key pairs[i][0] already exists.)
     * @returns The number of pairs added to the collection.
     * @description Computational complexity: O(pairs.length * log(size + pairs.length))
     */
    BTree.prototype.setPairs = function (pairs, overwrite) {
        var added = 0;
        for (var i = 0; i < pairs.length; i++)
            if (this.set(pairs[i][0], pairs[i][1], overwrite))
                added++;
        return added;
    };
    /**
     * Scans the specified range of keys, in ascending order by key.
     * Note: the callback `onFound` must not insert or remove items in the
     * collection. Doing so may cause incorrect data to be sent to the
     * callback afterward.
     * @param low The first key scanned will be greater than or equal to `low`.
     * @param high Scanning stops when a key larger than this is reached.
     * @param includeHigh If the `high` key is present, `onFound` is called for
     *        that final pair if and only if this parameter is true.
     * @param onFound A function that is called for each key-value pair. This
     *        function can return {break:R} to stop early with result R.
     * @param initialCounter Initial third argument of onFound. This value
     *        increases by one each time `onFound` is called. Default: 0
     * @returns The number of values found, or R if the callback returned
     *        `{break:R}` to stop early.
     * @description Computational complexity: O(number of items scanned + log size)
     */
    BTree.prototype.forRange = function (low, high, includeHigh, onFound, initialCounter) {
        var r = this._root.forRange(low, high, includeHigh, false, this, initialCounter || 0, onFound);
        return typeof r === "number" ? r : r.break;
    };
    /**
     * Scans and potentially modifies values for a subsequence of keys.
     * Note: the callback `onFound` should ideally be a pure function.
     *   Specfically, it must not insert items, call clone(), or change
     *   the collection except via return value; out-of-band editing may
     *   cause an exception or may cause incorrect data to be sent to
     *   the callback (duplicate or missed items). It must not cause a
     *   clone() of the collection, otherwise the clone could be modified
     *   by changes requested by the callback.
     * @param low The first key scanned will be greater than or equal to `low`.
     * @param high Scanning stops when a key larger than this is reached.
     * @param includeHigh If the `high` key is present, `onFound` is called for
     *        that final pair if and only if this parameter is true.
     * @param onFound A function that is called for each key-value pair. This
     *        function can return `{value:v}` to change the value associated
     *        with the current key, `{delete:true}` to delete the current pair,
     *        `{break:R}` to stop early with result R, or it can return nothing
     *        (undefined or {}) to cause no effect and continue iterating.
     *        `{break:R}` can be combined with one of the other two commands.
     *        The third argument `counter` is the number of items iterated
     *        previously; it equals 0 when `onFound` is called the first time.
     * @returns The number of values scanned, or R if the callback returned
     *        `{break:R}` to stop early.
     * @description
     *   Computational complexity: O(number of items scanned + log size)
     *   Note: if the tree has been cloned with clone(), any shared
     *   nodes are copied before `onFound` is called. This takes O(n) time
     *   where n is proportional to the amount of shared data scanned.
     */
    BTree.prototype.editRange = function (low, high, includeHigh, onFound, initialCounter) {
        var root = this._root;
        if (root.isShared)
            this._root = root = root.clone();
        try {
            var r = root.forRange(low, high, includeHigh, true, this, initialCounter || 0, onFound);
            return typeof r === "number" ? r : r.break;
        }
        finally {
            while (root.keys.length <= 1 && !root.isLeaf)
                this._root = root = root.keys.length === 0 ? EmptyLeaf :
                    root.children[0];
        }
    };
    /** Same as `editRange` except that the callback is called for all pairs. */
    BTree.prototype.editAll = function (onFound, initialCounter) {
        return this.editRange(this.minKey(), this.maxKey(), true, onFound, initialCounter);
    };
    /**
     * Removes a range of key-value pairs from the B+ tree.
     * @param low The first key scanned will be greater than or equal to `low`.
     * @param high Scanning stops when a key larger than this is reached.
     * @param includeHigh Specifies whether the `high` key, if present, is deleted.
     * @returns The number of key-value pairs that were deleted.
     * @description Computational complexity: O(log size + number of items deleted)
     */
    BTree.prototype.deleteRange = function (low, high, includeHigh) {
        return this.editRange(low, high, includeHigh, DeleteRange);
    };
    /** Deletes a series of keys from the collection. */
    BTree.prototype.deleteKeys = function (keys) {
        for (var i = 0, r = 0; i < keys.length; i++)
            if (this.delete(keys[i]))
                r++;
        return r;
    };
    Object.defineProperty(BTree.prototype, "height", {
        /** Gets the height of the tree: the number of internal nodes between the
         *  BTree object and its leaf nodes (zero if there are no internal nodes). */
        get: function () {
            for (var node = this._root, h = -1; node != null; h++)
                node = node.children;
            return h;
        },
        enumerable: true,
        configurable: true
    });
    /** Makes the object read-only to ensure it is not accidentally modified.
     *  Freezing does not have to be permanent; unfreeze() reverses the effect.
     *  This is accomplished by replacing mutator functions with a function
     *  that throws an Error. Compared to using a property (e.g. this.isFrozen)
     *  this implementation gives better performance in non-frozen BTrees.
     */
    BTree.prototype.freeze = function () {
        var t = this;
        // Note: all other mutators ultimately call set() or editRange() 
        //       so we don't need to override those others.
        t.clear = t.set = t.editRange = function () {
            throw new Error("Attempted to modify a frozen BTree");
        };
    };
    /** Ensures mutations are allowed, reversing the effect of freeze(). */
    BTree.prototype.unfreeze = function () {
        delete this.clear;
        delete this.set;
        delete this.editRange;
    };
    Object.defineProperty(BTree.prototype, "isFrozen", {
        /** Returns true if the tree appears to be frozen. */
        get: function () {
            return this.hasOwnProperty('editRange');
        },
        enumerable: true,
        configurable: true
    });
    /** Scans the tree for signs of serious bugs (e.g. this.size doesn't match
     *  number of elements, internal nodes not caching max element properly...)
     *  Computational complexity: O(number of nodes), i.e. O(size). This method
     *  skips the most expensive test - whether all keys are sorted - but it
     *  does check that maxKey() of the children of internal nodes are sorted. */
    BTree.prototype.checkValid = function () {
        var size = this._root.checkValid(0, this);
        check(size === this.size, "size mismatch: counted ", size, "but stored", this.size);
    };
    return BTree;
}());
exports.default = BTree;
if (Symbol && Symbol.iterator) // iterator is equivalent to entries()
    BTree.prototype[Symbol.iterator] = BTree.prototype.entries;
BTree.prototype.where = BTree.prototype.filter;
BTree.prototype.setRange = BTree.prototype.setPairs;
BTree.prototype.add = BTree.prototype.set;
function iterator(next) {
    if (next === void 0) { next = (function () { return ({ done: true, value: undefined }); }); }
    var result = { next: next };
    if (Symbol && Symbol.iterator)
        result[Symbol.iterator] = function () { return this; };
    return result;
}
/** Leaf node / base class. **************************************************/
var BNode = /** @class */ (function () {
    function BNode(keys, values) {
        if (keys === void 0) { keys = []; }
        this.keys = keys;
        this.values = values || undefVals;
        this.isShared = undefined;
    }
    Object.defineProperty(BNode.prototype, "isLeaf", {
        get: function () { return this.children === undefined; },
        enumerable: true,
        configurable: true
    });
    // Shared methods /////////////////////////////////////////////////////////
    BNode.prototype.maxKey = function () {
        return this.keys[this.keys.length - 1];
    };
    // If key not found, returns i^failXor where i is the insertion index.
    // Callers that don't care whether there was a match will set failXor=0.
    BNode.prototype.indexOf = function (key, failXor, cmp) {
        // TODO: benchmark multiple search strategies
        var keys = this.keys;
        var lo = 0, hi = keys.length, mid = hi >> 1;
        while (lo < hi) {
            var c = cmp(keys[mid], key);
            if (c < 0)
                lo = mid + 1;
            else if (c > 0) // key < keys[mid]
                hi = mid;
            else if (c === 0)
                return mid;
            else {
                // c is NaN or otherwise invalid
                if (key === key) // at least the search key is not NaN
                    return keys.length;
                else
                    throw new Error("BTree: NaN was used as a key");
            }
            mid = (lo + hi) >> 1;
        }
        return mid ^ failXor;
        // Unrolled version: benchmarks show same speed, not worth using
        /*var i = 1, c: number = 0, sum = 0;
        if (keys.length >= 4) {
          i = 3;
          if (keys.length >= 8) {
            i = 7;
            if (keys.length >= 16) {
              i = 15;
              if (keys.length >= 32) {
                i = 31;
                if (keys.length >= 64) {
                  i = 127;
                  i += (c = i < keys.length ? cmp(keys[i], key) : 1) < 0 ? 64 : -64;
                  sum += c;
                  i += (c = i < keys.length ? cmp(keys[i], key) : 1) < 0 ? 32 : -32;
                  sum += c;
                }
                i += (c = i < keys.length ? cmp(keys[i], key) : 1) < 0 ? 16 : -16;
                sum += c;
              }
              i += (c = i < keys.length ? cmp(keys[i], key) : 1) < 0 ? 8 : -8;
              sum += c;
            }
            i += (c = i < keys.length ? cmp(keys[i], key) : 1) < 0 ? 4 : -4;
            sum += c;
          }
          i += (c = i < keys.length ? cmp(keys[i], key) : 1) < 0 ? 2 : -2;
          sum += c;
        }
        i += (c = i < keys.length ? cmp(keys[i], key) : 1) < 0 ? 1 : -1;
        c = i < keys.length ? cmp(keys[i], key) : 1;
        sum += c;
        if (c < 0) {
          ++i;
          c = i < keys.length ? cmp(keys[i], key) : 1;
          sum += c;
        }
        if (sum !== sum) {
          if (key === key) // at least the search key is not NaN
            return keys.length ^ failXor;
          else
            throw new Error("BTree: NaN was used as a key");
        }
        return c === 0 ? i : i ^ failXor;*/
    };
    // Leaf Node: misc //////////////////////////////////////////////////////////
    BNode.prototype.minKey = function () {
        return this.keys[0];
    };
    BNode.prototype.clone = function () {
        var v = this.values;
        return new BNode(this.keys.slice(0), v === undefVals ? v : v.slice(0));
    };
    BNode.prototype.greedyClone = function (force) {
        return this.isShared && !force ? this : this.clone();
    };
    BNode.prototype.get = function (key, defaultValue, tree) {
        var i = this.indexOf(key, -1, tree._compare);
        return i < 0 ? defaultValue : this.values[i];
    };
    BNode.prototype.checkValid = function (depth, tree) {
        var kL = this.keys.length, vL = this.values.length;
        check(this.values === undefVals ? kL <= vL : kL === vL, "keys/values length mismatch: depth", depth, "with lengths", kL, vL);
        // Note: we don't check for "node too small" because sometimes a node
        // can legitimately have size 1. This occurs if there is a batch 
        // deletion, leaving a node of size 1, and the siblings are full so
        // it can't be merged with adjacent nodes. However, the parent will
        // verify that the average node size is at least half of the maximum.
        check(depth == 0 || kL > 0, "empty leaf at depth", depth);
        return kL;
    };
    // Leaf Node: set & node splitting //////////////////////////////////////////
    BNode.prototype.set = function (key, value, overwrite, tree) {
        var i = this.indexOf(key, -1, tree._compare);
        if (i < 0) {
            // key does not exist yet
            i = ~i;
            tree._size++;
            if (this.keys.length < tree._maxNodeSize) {
                return this.insertInLeaf(i, key, value, tree);
            }
            else {
                // This leaf node is full and must split
                var newRightSibling = this.splitOffRightSide(), target = this;
                if (i > this.keys.length) {
                    i -= this.keys.length;
                    target = newRightSibling;
                }
                target.insertInLeaf(i, key, value, tree);
                return newRightSibling;
            }
        }
        else {
            // Key already exists
            if (overwrite !== false) {
                if (value !== undefined)
                    this.reifyValues();
                // usually this is a no-op, but some users may wish to edit the key
                this.keys[i] = key;
                this.values[i] = value;
            }
            return false;
        }
    };
    BNode.prototype.reifyValues = function () {
        if (this.values === undefVals)
            return this.values = this.values.slice(0, this.keys.length);
        return this.values;
    };
    BNode.prototype.insertInLeaf = function (i, key, value, tree) {
        this.keys.splice(i, 0, key);
        if (this.values === undefVals) {
            while (undefVals.length < tree._maxNodeSize)
                undefVals.push(undefined);
            if (value === undefined) {
                return true;
            }
            else {
                this.values = undefVals.slice(0, this.keys.length - 1);
            }
        }
        this.values.splice(i, 0, value);
        return true;
    };
    BNode.prototype.takeFromRight = function (rhs) {
        // Reminder: parent node must update its copy of key for this node
        // assert: neither node is shared
        // assert rhs.keys.length > (maxNodeSize/2 && this.keys.length<maxNodeSize)
        var v = this.values;
        if (rhs.values === undefVals) {
            if (v !== undefVals)
                v.push(undefined);
        }
        else {
            v = this.reifyValues();
            v.push(rhs.values.shift());
        }
        this.keys.push(rhs.keys.shift());
    };
    BNode.prototype.takeFromLeft = function (lhs) {
        // Reminder: parent node must update its copy of key for this node
        // assert: neither node is shared
        // assert rhs.keys.length > (maxNodeSize/2 && this.keys.length<maxNodeSize)
        var v = this.values;
        if (lhs.values === undefVals) {
            if (v !== undefVals)
                v.unshift(undefined);
        }
        else {
            v = this.reifyValues();
            v.unshift(lhs.values.pop());
        }
        this.keys.unshift(lhs.keys.pop());
    };
    BNode.prototype.splitOffRightSide = function () {
        // Reminder: parent node must update its copy of key for this node
        var half = this.keys.length >> 1, keys = this.keys.splice(half);
        var values = this.values === undefVals ? undefVals : this.values.splice(half);
        return new BNode(keys, values);
    };
    // Leaf Node: scanning & deletions //////////////////////////////////////////
    BNode.prototype.forRange = function (low, high, includeHigh, editMode, tree, count, onFound) {
        var cmp = tree._compare;
        var iLow, iHigh;
        if (high === low) {
            if (!includeHigh)
                return count;
            iHigh = (iLow = this.indexOf(low, -1, cmp)) + 1;
            if (iLow < 0)
                return count;
        }
        else {
            iLow = this.indexOf(low, 0, cmp);
            iHigh = this.indexOf(high, -1, cmp);
            if (iHigh < 0)
                iHigh = ~iHigh;
            else if (includeHigh === true)
                iHigh++;
        }
        var keys = this.keys, values = this.values;
        if (onFound !== undefined) {
            for (var i = iLow; i < iHigh; i++) {
                var key = keys[i];
                var result = onFound(key, values[i], count++);
                if (result !== undefined) {
                    if (editMode === true) {
                        if (key !== keys[i] || this.isShared === true)
                            throw new Error("BTree illegally changed or cloned in editRange");
                        if (result.delete) {
                            this.keys.splice(i, 1);
                            if (this.values !== undefVals)
                                this.values.splice(i, 1);
                            tree._size--;
                            i--;
                            iHigh--;
                        }
                        else if (result.hasOwnProperty('value')) {
                            values[i] = result.value;
                        }
                    }
                    if (result.break !== undefined)
                        return result;
                }
            }
        }
        else
            count += iHigh - iLow;
        return count;
    };
    /** Adds entire contents of right-hand sibling (rhs is left unchanged) */
    BNode.prototype.mergeSibling = function (rhs, _) {
        this.keys.push.apply(this.keys, rhs.keys);
        if (this.values === undefVals) {
            if (rhs.values === undefVals)
                return;
            this.values = this.values.slice(0, this.keys.length);
        }
        this.values.push.apply(this.values, rhs.reifyValues());
    };
    return BNode;
}());
/** Internal node (non-leaf node) ********************************************/
var BNodeInternal = /** @class */ (function (_super) {
    __extends(BNodeInternal, _super);
    function BNodeInternal(children, keys) {
        var _this = this;
        if (!keys) {
            keys = [];
            for (var i = 0; i < children.length; i++)
                keys[i] = children[i].maxKey();
        }
        _this = _super.call(this, keys) || this;
        _this.children = children;
        return _this;
    }
    BNodeInternal.prototype.clone = function () {
        var children = this.children.slice(0);
        for (var i = 0; i < children.length; i++)
            children[i].isShared = true;
        return new BNodeInternal(children, this.keys.slice(0));
    };
    BNodeInternal.prototype.greedyClone = function (force) {
        if (this.isShared && !force)
            return this;
        var nu = new BNodeInternal(this.children.slice(0), this.keys.slice(0));
        for (var i = 0; i < nu.children.length; i++)
            nu.children[i] = nu.children[i].greedyClone();
        return nu;
    };
    BNodeInternal.prototype.minKey = function () {
        return this.children[0].minKey();
    };
    BNodeInternal.prototype.get = function (key, defaultValue, tree) {
        var i = this.indexOf(key, 0, tree._compare), children = this.children;
        return i < children.length ? children[i].get(key, defaultValue, tree) : undefined;
    };
    BNodeInternal.prototype.checkValid = function (depth, tree) {
        var kL = this.keys.length, cL = this.children.length;
        check(kL === cL, "keys/children length mismatch: depth", depth, "lengths", kL, cL);
        check(kL > 1, "internal node has length", kL, "at depth", depth);
        var size = 0, c = this.children, k = this.keys, childSize = 0;
        for (var i = 0; i < cL; i++) {
            size += c[i].checkValid(depth + 1, tree);
            childSize += c[i].keys.length;
            check(size >= childSize, "wtf"); // no way this will ever fail
            check(i === 0 || c[i - 1].constructor === c[i].constructor, "type mismatch");
            if (c[i].maxKey() != k[i])
                check(false, "keys[", i, "] =", k[i], "is wrong, should be ", c[i].maxKey(), "at depth", depth);
            if (!(i === 0 || tree._compare(k[i - 1], k[i]) < 0))
                check(false, "sort violation at depth", depth, "index", i, "keys", k[i - 1], k[i]);
        }
        var toofew = childSize < (tree.maxNodeSize >> 1) * cL;
        if (toofew || childSize > tree.maxNodeSize * cL)
            check(false, toofew ? "too few" : "too many", "children (", childSize, size, ") at depth", depth, ", maxNodeSize:", tree.maxNodeSize, "children.length:", cL);
        return size;
    };
    // Internal Node: set & node splitting //////////////////////////////////////
    BNodeInternal.prototype.set = function (key, value, overwrite, tree) {
        var c = this.children, max = tree._maxNodeSize, cmp = tree._compare;
        var i = Math.min(this.indexOf(key, 0, cmp), c.length - 1), child = c[i];
        if (child.isShared)
            c[i] = child = child.clone();
        if (child.keys.length >= max) {
            // child is full; inserting anything else will cause a split.
            // Shifting an item to the left or right sibling may avoid a split.
            // We can do a shift if the adjacent node is not full and if the
            // current key can still be placed in the same node after the shift.
            var other;
            if (i > 0 && (other = c[i - 1]).keys.length < max && cmp(child.keys[0], key) < 0) {
                if (other.isShared)
                    c[i - 1] = other = other.clone();
                other.takeFromRight(child);
                this.keys[i - 1] = other.maxKey();
            }
            else if ((other = c[i + 1]) !== undefined && other.keys.length < max && cmp(child.maxKey(), key) < 0) {
                if (other.isShared)
                    c[i + 1] = other = other.clone();
                other.takeFromLeft(child);
                this.keys[i] = c[i].maxKey();
            }
        }
        var result = child.set(key, value, overwrite, tree);
        if (result === false)
            return false;
        this.keys[i] = child.maxKey();
        if (result === true)
            return true;
        // The child has split and `result` is a new right child... does it fit?
        if (this.keys.length < max) { // yes
            this.insert(i + 1, result);
            return true;
        }
        else { // no, we must split also
            var newRightSibling = this.splitOffRightSide(), target = this;
            if (cmp(result.maxKey(), this.maxKey()) > 0) {
                target = newRightSibling;
                i -= this.keys.length;
            }
            target.insert(i + 1, result);
            return newRightSibling;
        }
    };
    BNodeInternal.prototype.insert = function (i, child) {
        this.children.splice(i, 0, child);
        this.keys.splice(i, 0, child.maxKey());
    };
    BNodeInternal.prototype.splitOffRightSide = function () {
        var half = this.children.length >> 1;
        return new BNodeInternal(this.children.splice(half), this.keys.splice(half));
    };
    BNodeInternal.prototype.takeFromRight = function (rhs) {
        // Reminder: parent node must update its copy of key for this node
        // assert: neither node is shared
        // assert rhs.keys.length > (maxNodeSize/2 && this.keys.length<maxNodeSize)
        this.keys.push(rhs.keys.shift());
        this.children.push(rhs.children.shift());
    };
    BNodeInternal.prototype.takeFromLeft = function (lhs) {
        // Reminder: parent node must update its copy of key for this node
        // assert: neither node is shared
        // assert rhs.keys.length > (maxNodeSize/2 && this.keys.length<maxNodeSize)
        this.keys.unshift(lhs.keys.pop());
        this.children.unshift(lhs.children.pop());
    };
    // Internal Node: scanning & deletions //////////////////////////////////////
    BNodeInternal.prototype.forRange = function (low, high, includeHigh, editMode, tree, count, onFound) {
        var cmp = tree._compare;
        var iLow = this.indexOf(low, 0, cmp), i = iLow;
        var iHigh = Math.min(high === low ? iLow : this.indexOf(high, 0, cmp), this.keys.length - 1);
        var keys = this.keys, children = this.children;
        if (!editMode) {
            // Simple case
            for (; i <= iHigh; i++) {
                var result = children[i].forRange(low, high, includeHigh, editMode, tree, count, onFound);
                if (typeof result !== 'number')
                    return result;
                count = result;
            }
        }
        else if (i <= iHigh) {
            try {
                for (; i <= iHigh; i++) {
                    if (children[i].isShared)
                        children[i] = children[i].clone();
                    var result = children[i].forRange(low, high, includeHigh, editMode, tree, count, onFound);
                    keys[i] = children[i].maxKey();
                    if (typeof result !== 'number')
                        return result;
                    count = result;
                }
            }
            finally {
                // Deletions may have occurred, so look for opportunities to merge nodes.
                var half = tree._maxNodeSize >> 1;
                if (iLow > 0)
                    iLow--;
                for (i = iHigh; i >= iLow; i--) {
                    if (children[i].keys.length <= half)
                        this.tryMerge(i, tree._maxNodeSize);
                }
                // Are we completely empty?
                if (children[0].keys.length === 0) {
                    check(children.length === 1 && keys.length === 1, "emptiness bug");
                    children.shift();
                    keys.shift();
                }
            }
        }
        return count;
    };
    /** Merges child i with child i+1 if their combined size is not too large */
    BNodeInternal.prototype.tryMerge = function (i, maxSize) {
        var children = this.children;
        if (i >= 0 && i + 1 < children.length) {
            if (children[i].keys.length + children[i + 1].keys.length <= maxSize) {
                if (children[i].isShared) // cloned already UNLESS i is outside scan range
                    children[i] = children[i].clone();
                children[i].mergeSibling(children[i + 1], maxSize);
                children.splice(i + 1, 1);
                this.keys.splice(i + 1, 1);
                this.keys[i] = children[i].maxKey();
                return true;
            }
        }
        return false;
    };
    BNodeInternal.prototype.mergeSibling = function (rhs, maxNodeSize) {
        // assert !this.isShared;
        var oldLength = this.keys.length;
        this.keys.push.apply(this.keys, rhs.keys);
        this.children.push.apply(this.children, rhs.children);
        // If our children are themselves almost empty due to a mass-delete,
        // they may need to be merged too (but only the oldLength-1 and its
        // right sibling should need this).
        this.tryMerge(oldLength - 1, maxNodeSize);
    };
    return BNodeInternal;
}(BNode));
// Optimization: this array of `undefined`s is used instead of a normal
// array of values in nodes where `undefined` is the only value.
// Its length is extended to max node size on first use; since it can
// be shared between trees with different maximums, its length can only
// increase, never decrease. Its type should be undefined[] but strangely
// TypeScript won't allow the comparison V[] === undefined[]. To prevent
// users from making this array too large, BTree has a maximum node size.
var undefVals = [];
var Delete = { delete: true }, DeleteRange = function () { return Delete; };
var Break = { break: true };
var EmptyLeaf = (function () {
    var n = new BNode();
    n.isShared = true;
    return n;
})();
var EmptyArray = [];
var ReusedArray = []; // assumed thread-local
function check(fact) {
    var args = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        args[_i - 1] = arguments[_i];
    }
    if (!fact) {
        args.unshift('B+ tree '); // at beginning of message
        throw new Error(args.join(' '));
    }
}
/** A BTree frozen in the empty state. */
exports.EmptyBTree = (function () { var t = new BTree(); t.freeze(); return t; })();

});

unwrapExports(b_tree);
var b_tree_1 = b_tree.defaultComparator;
var b_tree_2 = b_tree.EmptyBTree;

var extractKey_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });

// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-extracting-a-key-from-a-value-using-a-key-path
var extractKey = function (keyPath, value) {
    if (Array.isArray(keyPath)) {
        var result = [];
        for (var _i = 0, keyPath_1 = keyPath; _i < keyPath_1.length; _i++) {
            var item = keyPath_1[_i];
            // This doesn't make sense to me based on the spec, but it is needed to pass the W3C KeyPath tests (see same
            // comment in validateKeyPath)
            if (item !== undefined &&
                item !== null &&
                typeof item !== "string" &&
                item.toString) {
                item = item.toString();
            }
            result.push(valueToKey_1.default(extractKey(item, value)));
        }
        return result;
    }
    if (keyPath === "") {
        return value;
    }
    var remainingKeyPath = keyPath;
    var object = value;
    while (remainingKeyPath !== null) {
        var identifier = void 0;
        var i = remainingKeyPath.indexOf(".");
        if (i >= 0) {
            identifier = remainingKeyPath.slice(0, i);
            remainingKeyPath = remainingKeyPath.slice(i + 1);
        }
        else {
            identifier = remainingKeyPath;
            remainingKeyPath = null;
        }
        if (!object.hasOwnProperty(identifier)) {
            return;
        }
        object = object[identifier];
    }
    return object;
};
exports.default = extractKey;

});

unwrapExports(extractKey_1);

var injectKey_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });

function injectKey(keyPath, value, key) {
    if (Array.isArray(keyPath)) {
        // tslint:disable-next-line max-line-length
        throw new Error("The key paths used in this section are always strings and never sequences, since it is not possible to create a object store which has a key generator and also has a key path that is a sequence.");
    }
    var identifiers = keyPath.split(".");
    if (identifiers.length === 0) {
        throw new Error("Assert: identifiers is not empty");
    }
    var lastIdentifier = identifiers.pop();
    if (lastIdentifier === null || lastIdentifier === undefined) {
        throw Error();
    }
    for (var _i = 0, identifiers_1 = identifiers; _i < identifiers_1.length; _i++) {
        var identifier = identifiers_1[_i];
        if (typeof value !== "object" && !Array.isArray(value)) {
            return false;
        }
        var hop = value.hasOwnProperty(identifier);
        if (!hop) {
            return true;
        }
        value = value[identifier];
    }
    if (!(typeof value === "object" || Array.isArray(value))) {
        throw new Error("can't inject key");
    }
    var newValue = structuredClone_1.default(value);
    newValue[lastIdentifier] = structuredClone_1.default(key);
    return newValue;
}
exports.injectKey = injectKey;
exports.default = injectKey;

});

unwrapExports(injectKey_1);
var injectKey_2 = injectKey_1.injectKey;

var makeStoreKeyValue_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });





function makeStoreKeyValue(value, key, currentKeyGenerator, autoIncrement, keyPath) {
    var haveKey = key !== null && key !== undefined;
    var haveKeyPath = keyPath !== null && keyPath !== undefined;
    // This models a decision table on (haveKey, haveKeyPath, autoIncrement)
    value = structuredClone_1.default(value);
    if (haveKey) {
        if (haveKeyPath) {
            // (yes, yes, no)
            // (yes, yes, yes)
            throw new errors.DataError();
        }
        else {
            if (autoIncrement) {
                // (yes, no, yes)
                key = valueToKey_1.default(key);
                var updatedKeyGenerator = void 0;
                if (typeof key !== "number") {
                    updatedKeyGenerator = currentKeyGenerator;
                }
                else {
                    updatedKeyGenerator = key;
                }
                return {
                    key: key,
                    value: value,
                    updatedKeyGenerator: updatedKeyGenerator,
                };
            }
            else {
                // (yes, no, no)
                throw new errors.DataError();
            }
        }
    }
    else {
        if (haveKeyPath) {
            if (autoIncrement) {
                // (no, yes, yes)
                var updatedKeyGenerator = void 0;
                var maybeInlineKey = extractKey_1.default(keyPath, value);
                if (maybeInlineKey === undefined) {
                    value = injectKey_1.default(keyPath, value, currentKeyGenerator);
                    key = currentKeyGenerator;
                    updatedKeyGenerator = currentKeyGenerator + 1;
                }
                else if (typeof maybeInlineKey === "number") {
                    key = maybeInlineKey;
                    if (maybeInlineKey >= currentKeyGenerator) {
                        updatedKeyGenerator = maybeInlineKey + 1;
                    }
                    else {
                        updatedKeyGenerator = currentKeyGenerator;
                    }
                }
                else {
                    key = maybeInlineKey;
                    updatedKeyGenerator = currentKeyGenerator;
                }
                return {
                    key: key,
                    value: value,
                    updatedKeyGenerator: updatedKeyGenerator,
                };
            }
            else {
                // (no, yes, no)
                key = extractKey_1.default(keyPath, value);
                key = valueToKey_1.default(key);
                return {
                    key: key,
                    value: value,
                    updatedKeyGenerator: currentKeyGenerator,
                };
            }
        }
        else {
            if (autoIncrement) {
                // (no, no, yes)
                return {
                    key: currentKeyGenerator,
                    value: value,
                    updatedKeyGenerator: currentKeyGenerator + 1,
                };
            }
            else {
                // (no, no, no)
                throw new errors.DataError();
            }
        }
    }
}
exports.makeStoreKeyValue = makeStoreKeyValue;

});

unwrapExports(makeStoreKeyValue_1);
var makeStoreKeyValue_2 = makeStoreKeyValue_1.makeStoreKeyValue;

var getIndexKeys_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2017 Jeremy Scheff
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });


function getIndexKeys(value, keyPath, multiEntry) {
    if (multiEntry && Array.isArray(keyPath)) {
        var keys = [];
        for (var _i = 0, keyPath_1 = keyPath; _i < keyPath_1.length; _i++) {
            var subkeyPath = keyPath_1[_i];
            var key = extractKey_1.default(subkeyPath, value);
            try {
                var k = valueToKey_1.default(key);
                keys.push(k);
            }
            catch (_a) {
                // Ignore invalid subkeys
            }
        }
        return keys;
    }
    else {
        var key = extractKey_1.default(keyPath, value);
        return [valueToKey_1.default(key)];
    }
}
exports.getIndexKeys = getIndexKeys;
exports.default = getIndexKeys;

});

unwrapExports(getIndexKeys_1);
var getIndexKeys_2 = getIndexKeys_1.getIndexKeys;

var MemoryBackend_1 = createCommonjsModule(function (module, exports) {
/*
 Copyright 2019 Florian Dold

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 or implied. See the License for the specific language governing
 permissions and limitations under the License.
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });









var TransactionLevel;
(function (TransactionLevel) {
    TransactionLevel[TransactionLevel["Disconnected"] = 0] = "Disconnected";
    TransactionLevel[TransactionLevel["Connected"] = 1] = "Connected";
    TransactionLevel[TransactionLevel["Read"] = 2] = "Read";
    TransactionLevel[TransactionLevel["Write"] = 3] = "Write";
    TransactionLevel[TransactionLevel["VersionChange"] = 4] = "VersionChange";
})(TransactionLevel || (TransactionLevel = {}));
var AsyncCondition = /** @class */ (function () {
    function AsyncCondition() {
        var op = openPromise_1.default();
        this._waitPromise = op.promise;
        this._resolveWaitPromise = op.resolve;
    }
    AsyncCondition.prototype.wait = function () {
        return this._waitPromise;
    };
    AsyncCondition.prototype.trigger = function () {
        this._resolveWaitPromise();
        var op = openPromise_1.default();
        this._waitPromise = op.promise;
        this._resolveWaitPromise = op.resolve;
    };
    return AsyncCondition;
}());
function nextStoreKey(forward, data, k) {
    if (k === undefined || k === null) {
        return undefined;
    }
    var res = forward ? data.nextHigherPair(k) : data.nextLowerPair(k);
    if (!res) {
        return undefined;
    }
    return res[1].primaryKey;
}
function furthestKey(forward, key1, key2) {
    if (key1 === undefined) {
        return key2;
    }
    if (key2 === undefined) {
        return key1;
    }
    var cmpResult = cmp.default(key1, key2);
    if (cmpResult === 0) {
        // Same result
        return key1;
    }
    if (forward && cmpResult === 1) {
        return key1;
    }
    if (forward && cmpResult === -1) {
        return key2;
    }
    if (!forward && cmpResult === 1) {
        return key2;
    }
    if (!forward && cmpResult === -1) {
        return key1;
    }
}
/**
 * Primitive in-memory backend.
 */
var MemoryBackend = /** @class */ (function () {
    function MemoryBackend() {
        this.databases = {};
        this.connectionIdCounter = 1;
        this.transactionIdCounter = 1;
        /**
         * Connections by connection cookie.
         */
        this.connections = {};
        /**
         * Connections by transaction (!!) cookie.  In this implementation,
         * at most one transaction can run at the same time per connection.
         */
        this.connectionsByTransaction = {};
        /**
         * Condition that is triggered whenever a client disconnects.
         */
        this.disconnectCond = new AsyncCondition();
        /**
         * Conditation that is triggered whenever a transaction finishes.
         */
        this.transactionDoneCond = new AsyncCondition();
        this.enableTracing = false;
    }
    /**
     * Load the data in this IndexedDB backend from a dump in JSON format.
     *
     * Must be called before any connections to the database backend have
     * been made.
     */
    MemoryBackend.prototype.importDump = function (data) {
        if (this.enableTracing) {
            console.log("importing dump (a)");
        }
        if (this.transactionIdCounter != 1 || this.connectionIdCounter != 1) {
            throw Error("data must be imported before first transaction or connection");
        }
        this.databases = {};
        for (var _i = 0, _a = Object.keys(data.databases); _i < _a.length; _i++) {
            var dbName = _a[_i];
            var schema = data.databases[dbName].schema;
            if (typeof schema !== "object") {
                throw Error("DB dump corrupt");
            }
            var objectStores = {};
            for (var _b = 0, _c = Object.keys(data.databases[dbName].objectStores); _b < _c.length; _b++) {
                var objectStoreName = _c[_b];
                var dumpedObjectStore = data.databases[dbName].objectStores[objectStoreName];
                var indexes = {};
                for (var _d = 0, _e = Object.keys(dumpedObjectStore.indexes); _d < _e.length; _d++) {
                    var indexName = _e[_d];
                    var dumpedIndex = dumpedObjectStore.indexes[indexName];
                    var pairs_1 = dumpedIndex.records.map(function (r) {
                        return structuredClone_1.default([r.indexKey, r]);
                    });
                    var indexData = new b_tree.default(pairs_1, cmp.default);
                    var index = {
                        deleted: false,
                        modifiedData: undefined,
                        modifiedName: undefined,
                        originalName: indexName,
                        originalData: indexData,
                    };
                    indexes[indexName] = index;
                }
                var pairs = dumpedObjectStore.records.map(function (r) {
                    return structuredClone_1.default([r.primaryKey, r]);
                });
                var objectStoreData = new b_tree.default(pairs, cmp.default);
                var objectStore = {
                    deleted: false,
                    modifiedData: undefined,
                    modifiedName: undefined,
                    modifiedKeyGenerator: undefined,
                    originalData: objectStoreData,
                    originalName: objectStoreName,
                    originalKeyGenerator: dumpedObjectStore.keyGenerator,
                    committedIndexes: indexes,
                    modifiedIndexes: {},
                };
                objectStores[objectStoreName] = objectStore;
            }
            var db = {
                deleted: false,
                committedObjectStores: objectStores,
                committedSchema: structuredClone_1.default(schema),
                connectionCookie: undefined,
                modifiedObjectStores: {},
                txLevel: TransactionLevel.Disconnected,
                txRestrictObjectStores: undefined,
            };
            this.databases[dbName] = db;
        }
    };
    MemoryBackend.prototype.makeObjectStoreMap = function (database) {
        var map = {};
        for (var objectStoreName in database.committedObjectStores) {
            var store = database.committedObjectStores[objectStoreName];
            var entry = {
                store: store,
                indexMap: Object.assign({}, store.committedIndexes),
            };
            map[objectStoreName] = entry;
        }
        return map;
    };
    /**
     * Export the contents of the database to JSON.
     *
     * Only exports data that has been committed.
     */
    MemoryBackend.prototype.exportDump = function () {
        this.enableTracing && console.log("exporting dump");
        var dbDumps = {};
        for (var _i = 0, _a = Object.keys(this.databases); _i < _a.length; _i++) {
            var dbName = _a[_i];
            var db = this.databases[dbName];
            var objectStores = {};
            var _loop_1 = function (objectStoreName) {
                var objectStore = db.committedObjectStores[objectStoreName];
                var indexes = {};
                var _loop_2 = function (indexName) {
                    var index = objectStore.committedIndexes[indexName];
                    var indexRecords = [];
                    index.originalData.forEach(function (v) {
                        indexRecords.push(structuredClone_1.default(v));
                    });
                    indexes[indexName] = { name: indexName, records: indexRecords };
                };
                for (var _i = 0, _a = Object.keys(objectStore.committedIndexes); _i < _a.length; _i++) {
                    var indexName = _a[_i];
                    _loop_2(indexName);
                }
                var objectStoreRecords = [];
                objectStore.originalData.forEach(function (v) {
                    objectStoreRecords.push(structuredClone_1.default(v));
                });
                objectStores[objectStoreName] = {
                    name: objectStoreName,
                    records: objectStoreRecords,
                    keyGenerator: objectStore.originalKeyGenerator,
                    indexes: indexes,
                };
            };
            for (var _b = 0, _c = Object.keys(db.committedObjectStores); _b < _c.length; _b++) {
                var objectStoreName = _c[_b];
                _loop_1(objectStoreName);
            }
            var dbDump = {
                objectStores: objectStores,
                schema: structuredClone_1.default(this.databases[dbName].committedSchema),
            };
            dbDumps[dbName] = dbDump;
        }
        return { databases: dbDumps };
    };
    MemoryBackend.prototype.getDatabases = function () {
        return __awaiter(this, void 0, void 0, function () {
            var dbList, name;
            return __generator(this, function (_a) {
                if (this.enableTracing) {
                    console.log("TRACING: getDatabase");
                }
                dbList = [];
                for (name in this.databases) {
                    dbList.push({
                        name: name,
                        version: this.databases[name].committedSchema.databaseVersion,
                    });
                }
                return [2 /*return*/, dbList];
            });
        });
    };
    MemoryBackend.prototype.deleteDatabase = function (tx, name) {
        return __awaiter(this, void 0, void 0, function () {
            var myConn, myDb;
            return __generator(this, function (_a) {
                if (this.enableTracing) {
                    console.log("TRACING: deleteDatabase");
                }
                myConn = this.connectionsByTransaction[tx.transactionCookie];
                if (!myConn) {
                    throw Error("no connection associated with transaction");
                }
                myDb = this.databases[name];
                if (!myDb) {
                    throw Error("db not found");
                }
                if (myDb.committedSchema.databaseName !== name) {
                    throw Error("name does not match");
                }
                if (myDb.txLevel < TransactionLevel.VersionChange) {
                    throw new errors.InvalidStateError();
                }
                if (myDb.connectionCookie !== tx.transactionCookie) {
                    throw new errors.InvalidAccessError();
                }
                myDb.deleted = true;
                return [2 /*return*/];
            });
        });
    };
    MemoryBackend.prototype.connectDatabase = function (name) {
        return __awaiter(this, void 0, void 0, function () {
            var connectionId, connectionCookie, database, schema, myConn;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.enableTracing) {
                            console.log("TRACING: connectDatabase(" + name + ")");
                        }
                        connectionId = this.connectionIdCounter++;
                        connectionCookie = "connection-" + connectionId;
                        database = this.databases[name];
                        if (!database) {
                            schema = {
                                databaseName: name,
                                databaseVersion: 0,
                                objectStores: {},
                            };
                            database = {
                                committedSchema: schema,
                                deleted: false,
                                committedObjectStores: {},
                                modifiedObjectStores: {},
                                txLevel: TransactionLevel.Disconnected,
                                connectionCookie: undefined,
                                txRestrictObjectStores: undefined,
                            };
                            this.databases[name] = database;
                        }
                        _a.label = 1;
                    case 1:
                        if (!(database.txLevel !== TransactionLevel.Disconnected)) return [3 /*break*/, 3];
                        return [4 /*yield*/, this.disconnectCond.wait()];
                    case 2:
                        _a.sent();
                        return [3 /*break*/, 1];
                    case 3:
                        database.txLevel = TransactionLevel.Connected;
                        database.txRestrictObjectStores = undefined;
                        database.connectionCookie = connectionCookie;
                        myConn = {
                            dbName: name,
                            deleted: false,
                            objectStoreMap: this.makeObjectStoreMap(database),
                            modifiedSchema: structuredClone_1.default(database.committedSchema),
                        };
                        this.connections[connectionCookie] = myConn;
                        return [2 /*return*/, { connectionCookie: connectionCookie }];
                }
            });
        });
    };
    MemoryBackend.prototype.beginTransaction = function (conn, objectStores, mode) {
        return __awaiter(this, void 0, void 0, function () {
            var transactionCookie, myConn, myDb;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.enableTracing) {
                            console.log("TRACING: beginTransaction");
                        }
                        transactionCookie = "tx-" + this.transactionIdCounter++;
                        myConn = this.connections[conn.connectionCookie];
                        if (!myConn) {
                            throw Error("connection not found");
                        }
                        myDb = this.databases[myConn.dbName];
                        if (!myDb) {
                            throw Error("db not found");
                        }
                        _a.label = 1;
                    case 1:
                        if (!(myDb.txLevel !== TransactionLevel.Connected)) return [3 /*break*/, 3];
                        if (this.enableTracing) {
                            console.log("TRACING: beginTransaction -- waiting for others to close");
                        }
                        return [4 /*yield*/, this.transactionDoneCond.wait()];
                    case 2:
                        _a.sent();
                        return [3 /*break*/, 1];
                    case 3:
                        if (mode === "readonly") {
                            myDb.txLevel = TransactionLevel.Read;
                        }
                        else if (mode === "readwrite") {
                            myDb.txLevel = TransactionLevel.Write;
                        }
                        else {
                            throw Error("unsupported transaction mode");
                        }
                        myDb.txRestrictObjectStores = __spreadArrays(objectStores);
                        this.connectionsByTransaction[transactionCookie] = myConn;
                        return [2 /*return*/, { transactionCookie: transactionCookie }];
                }
            });
        });
    };
    MemoryBackend.prototype.enterVersionChange = function (conn, newVersion) {
        return __awaiter(this, void 0, void 0, function () {
            var transactionCookie, myConn, myDb;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.enableTracing) {
                            console.log("TRACING: enterVersionChange");
                        }
                        transactionCookie = "tx-vc-" + this.transactionIdCounter++;
                        myConn = this.connections[conn.connectionCookie];
                        if (!myConn) {
                            throw Error("connection not found");
                        }
                        myDb = this.databases[myConn.dbName];
                        if (!myDb) {
                            throw Error("db not found");
                        }
                        _a.label = 1;
                    case 1:
                        if (!(myDb.txLevel !== TransactionLevel.Connected)) return [3 /*break*/, 3];
                        return [4 /*yield*/, this.transactionDoneCond.wait()];
                    case 2:
                        _a.sent();
                        return [3 /*break*/, 1];
                    case 3:
                        myDb.txLevel = TransactionLevel.VersionChange;
                        myDb.txRestrictObjectStores = undefined;
                        this.connectionsByTransaction[transactionCookie] = myConn;
                        myConn.modifiedSchema.databaseVersion = newVersion;
                        return [2 /*return*/, { transactionCookie: transactionCookie }];
                }
            });
        });
    };
    MemoryBackend.prototype.close = function (conn) {
        return __awaiter(this, void 0, void 0, function () {
            var myConn, myDb;
            return __generator(this, function (_a) {
                if (this.enableTracing) {
                    console.log("TRACING: close");
                }
                myConn = this.connections[conn.connectionCookie];
                if (!myConn) {
                    throw Error("connection not found - already closed?");
                }
                if (!myConn.deleted) {
                    myDb = this.databases[myConn.dbName];
                    if (myDb.txLevel != TransactionLevel.Connected) {
                        throw Error("invalid state");
                    }
                    myDb.txLevel = TransactionLevel.Disconnected;
                    myDb.txRestrictObjectStores = undefined;
                }
                delete this.connections[conn.connectionCookie];
                this.disconnectCond.trigger();
                return [2 /*return*/];
            });
        });
    };
    MemoryBackend.prototype.getSchema = function (dbConn) {
        if (this.enableTracing) {
            console.log("TRACING: getSchema");
        }
        var myConn = this.connections[dbConn.connectionCookie];
        if (!myConn) {
            throw Error("unknown connection");
        }
        var db = this.databases[myConn.dbName];
        if (!db) {
            throw Error("db not found");
        }
        return myConn.modifiedSchema;
    };
    MemoryBackend.prototype.renameIndex = function (btx, objectStoreName, oldName, newName) {
        if (this.enableTracing) {
            console.log("TRACING: renameIndex(?, " + oldName + ", " + newName + ")");
        }
        var myConn = this.connectionsByTransaction[btx.transactionCookie];
        if (!myConn) {
            throw Error("unknown connection");
        }
        var db = this.databases[myConn.dbName];
        if (!db) {
            throw Error("db not found");
        }
        if (db.txLevel < TransactionLevel.VersionChange) {
            throw Error("only allowed in versionchange transaction");
        }
        var schema = myConn.modifiedSchema;
        if (!schema) {
            throw Error();
        }
        var indexesSchema = schema.objectStores[objectStoreName].indexes;
        if (indexesSchema[newName]) {
            throw new Error("new index name already used");
        }
        if (!indexesSchema) {
            throw new Error("new index name already used");
        }
        var index = myConn.objectStoreMap[objectStoreName].indexMap[oldName];
        if (!index) {
            throw Error("old index missing in connection's index map");
        }
        indexesSchema[newName] = indexesSchema[newName];
        delete indexesSchema[oldName];
        myConn.objectStoreMap[objectStoreName].indexMap[newName] = index;
        delete myConn.objectStoreMap[objectStoreName].indexMap[oldName];
        index.modifiedName = newName;
    };
    MemoryBackend.prototype.deleteIndex = function (btx, objectStoreName, indexName) {
        if (this.enableTracing) {
            console.log("TRACING: deleteIndex(" + indexName + ")");
        }
        var myConn = this.connections[btx.transactionCookie];
        if (!myConn) {
            throw Error("unknown connection");
        }
        var db = this.databases[myConn.dbName];
        if (!db) {
            throw Error("db not found");
        }
        if (db.txLevel < TransactionLevel.VersionChange) {
            throw Error("only allowed in versionchange transaction");
        }
        var schema = myConn.modifiedSchema;
        if (!schema) {
            throw Error();
        }
        if (!schema.objectStores[objectStoreName].indexes[indexName]) {
            throw new Error("index does not exist");
        }
        var index = myConn.objectStoreMap[objectStoreName].indexMap[indexName];
        if (!index) {
            throw Error("old index missing in connection's index map");
        }
        index.deleted = true;
        delete schema.objectStores[objectStoreName].indexes[indexName];
        delete myConn.objectStoreMap[objectStoreName].indexMap[indexName];
    };
    MemoryBackend.prototype.deleteObjectStore = function (btx, name) {
        if (this.enableTracing) {
            console.log("TRACING: deleteObjectStore(" + name + ")");
        }
        var myConn = this.connections[btx.transactionCookie];
        if (!myConn) {
            throw Error("unknown connection");
        }
        var db = this.databases[myConn.dbName];
        if (!db) {
            throw Error("db not found");
        }
        if (db.txLevel < TransactionLevel.VersionChange) {
            throw Error("only allowed in versionchange transaction");
        }
        var schema = myConn.modifiedSchema;
        if (!schema) {
            throw Error();
        }
        var objectStoreProperties = schema.objectStores[name];
        if (!objectStoreProperties) {
            throw Error("object store not found");
        }
        var objectStoreMapEntry = myConn.objectStoreMap[name];
        if (!objectStoreMapEntry) {
            throw Error("object store not found in map");
        }
        var indexNames = Object.keys(objectStoreProperties.indexes);
        for (var _i = 0, indexNames_1 = indexNames; _i < indexNames_1.length; _i++) {
            var indexName = indexNames_1[_i];
            this.deleteIndex(btx, name, indexName);
        }
        objectStoreMapEntry.store.deleted = true;
        delete myConn.objectStoreMap[name];
        delete schema.objectStores[name];
    };
    MemoryBackend.prototype.renameObjectStore = function (btx, oldName, newName) {
        if (this.enableTracing) {
            console.log("TRACING: renameObjectStore(?, " + oldName + ", " + newName + ")");
        }
        var myConn = this.connections[btx.transactionCookie];
        if (!myConn) {
            throw Error("unknown connection");
        }
        var db = this.databases[myConn.dbName];
        if (!db) {
            throw Error("db not found");
        }
        if (db.txLevel < TransactionLevel.VersionChange) {
            throw Error("only allowed in versionchange transaction");
        }
        var schema = myConn.modifiedSchema;
        if (!schema) {
            throw Error();
        }
        if (!schema.objectStores[oldName]) {
            throw Error("object store not found");
        }
        if (schema.objectStores[newName]) {
            throw Error("new object store already exists");
        }
        var objectStoreMapEntry = myConn.objectStoreMap[oldName];
        if (!objectStoreMapEntry) {
            throw Error("object store not found in map");
        }
        objectStoreMapEntry.store.modifiedName = newName;
        schema.objectStores[newName] = schema.objectStores[oldName];
        delete schema.objectStores[oldName];
        delete myConn.objectStoreMap[oldName];
        myConn.objectStoreMap[newName] = objectStoreMapEntry;
    };
    MemoryBackend.prototype.createObjectStore = function (btx, name, keyPath, autoIncrement) {
        if (this.enableTracing) {
            console.log("TRACING: createObjectStore(" + btx.transactionCookie + ", " + name + ")");
        }
        var myConn = this.connectionsByTransaction[btx.transactionCookie];
        if (!myConn) {
            throw Error("unknown connection");
        }
        var db = this.databases[myConn.dbName];
        if (!db) {
            throw Error("db not found");
        }
        if (db.txLevel < TransactionLevel.VersionChange) {
            throw Error("only allowed in versionchange transaction");
        }
        var newObjectStore = {
            deleted: false,
            modifiedName: undefined,
            originalName: name,
            modifiedData: undefined,
            originalData: new b_tree.default([], cmp.default),
            modifiedKeyGenerator: undefined,
            originalKeyGenerator: 1,
            committedIndexes: {},
            modifiedIndexes: {},
        };
        var schema = myConn.modifiedSchema;
        if (!schema) {
            throw Error("no schema for versionchange tx");
        }
        schema.objectStores[name] = {
            autoIncrement: autoIncrement,
            keyPath: keyPath,
            indexes: {},
        };
        myConn.objectStoreMap[name] = { store: newObjectStore, indexMap: {} };
        db.modifiedObjectStores[name] = newObjectStore;
    };
    MemoryBackend.prototype.createIndex = function (btx, indexName, objectStoreName, keyPath, multiEntry, unique) {
        var _this = this;
        if (this.enableTracing) {
            console.log("TRACING: createIndex(" + indexName + ")");
        }
        var myConn = this.connectionsByTransaction[btx.transactionCookie];
        if (!myConn) {
            throw Error("unknown connection");
        }
        var db = this.databases[myConn.dbName];
        if (!db) {
            throw Error("db not found");
        }
        if (db.txLevel < TransactionLevel.VersionChange) {
            throw Error("only allowed in versionchange transaction");
        }
        var indexProperties = {
            keyPath: keyPath,
            multiEntry: multiEntry,
            unique: unique,
        };
        var newIndex = {
            deleted: false,
            modifiedData: undefined,
            modifiedName: undefined,
            originalData: new b_tree.default([], cmp.default),
            originalName: indexName,
        };
        myConn.objectStoreMap[objectStoreName].indexMap[indexName] = newIndex;
        db.modifiedObjectStores[objectStoreName].modifiedIndexes[indexName] = newIndex;
        var schema = myConn.modifiedSchema;
        if (!schema) {
            throw Error("no schema in versionchange tx");
        }
        var objectStoreProperties = schema.objectStores[objectStoreName];
        if (!objectStoreProperties) {
            throw Error("object store not found");
        }
        objectStoreProperties.indexes[indexName] = indexProperties;
        var objectStoreMapEntry = myConn.objectStoreMap[objectStoreName];
        if (!objectStoreMapEntry) {
            throw Error("object store does not exist");
        }
        var storeData = objectStoreMapEntry.store.modifiedData ||
            objectStoreMapEntry.store.originalData;
        storeData.forEach(function (v, k) {
            _this.insertIntoIndex(newIndex, k, v.value, indexProperties);
        });
    };
    MemoryBackend.prototype.deleteRecord = function (btx, objectStoreName, range) {
        return __awaiter(this, void 0, void 0, function () {
            var myConn, db, schema, objectStoreMapEntry, modifiedData, currKey, firstValue, storeEntry, _i, _a, indexName, index, indexProperties;
            return __generator(this, function (_b) {
                if (this.enableTracing) {
                    console.log("TRACING: deleteRecord from store " + objectStoreName);
                }
                myConn = this.connectionsByTransaction[btx.transactionCookie];
                if (!myConn) {
                    throw Error("unknown connection");
                }
                db = this.databases[myConn.dbName];
                if (!db) {
                    throw Error("db not found");
                }
                if (db.txLevel < TransactionLevel.Write) {
                    throw Error("only allowed in write transaction");
                }
                if (db.txRestrictObjectStores &&
                    !db.txRestrictObjectStores.includes(objectStoreName)) {
                    throw Error("Not allowed to access store '" + objectStoreName + "', transaction is over " + JSON.stringify(db.txRestrictObjectStores));
                }
                if (typeof range !== "object") {
                    throw Error("deleteRecord got invalid range (must be object)");
                }
                if (!("lowerOpen" in range)) {
                    throw Error("deleteRecord got invalid range (sanity check failed, 'lowerOpen' missing)");
                }
                schema = myConn.modifiedSchema;
                objectStoreMapEntry = myConn.objectStoreMap[objectStoreName];
                if (!objectStoreMapEntry.store.modifiedData) {
                    objectStoreMapEntry.store.modifiedData =
                        objectStoreMapEntry.store.originalData;
                }
                modifiedData = objectStoreMapEntry.store.modifiedData;
                if (range.lower === undefined || range.lower === null) {
                    currKey = modifiedData.minKey();
                }
                else {
                    currKey = range.lower;
                    // We have a range with an lowerOpen lower bound, so don't start
                    // deleting the lower bound.  Instead start with the next higher key.
                    if (range.lowerOpen && currKey !== undefined) {
                        currKey = modifiedData.nextHigherKey(currKey);
                    }
                }
                firstValue = modifiedData.get(currKey);
                if (!firstValue) {
                    if (currKey !== undefined) {
                        currKey = modifiedData.nextHigherKey(currKey);
                    }
                }
                // loop invariant: (currKey is undefined) or (currKey is a valid key)
                while (true) {
                    if (currKey === undefined) {
                        // nothing more to delete!
                        break;
                    }
                    if (range.upper !== null && range.upper !== undefined) {
                        if (range.upperOpen && cmp.default(currKey, range.upper) === 0) {
                            // We have a range that's upperOpen, so stop before we delete the upper bound.
                            break;
                        }
                        if (!range.upperOpen && cmp.default(currKey, range.upper) > 0) {
                            // The upper range is inclusive, only stop if we're after the upper range.
                            break;
                        }
                    }
                    storeEntry = modifiedData.get(currKey);
                    if (!storeEntry) {
                        throw Error("assertion failed");
                    }
                    for (_i = 0, _a = Object.keys(schema.objectStores[objectStoreName].indexes); _i < _a.length; _i++) {
                        indexName = _a[_i];
                        index = myConn.objectStoreMap[objectStoreName].indexMap[indexName];
                        if (!index) {
                            throw Error("index referenced by object store does not exist");
                        }
                        this.enableTracing &&
                            console.log("deleting from index " + indexName + " for object store " + objectStoreName);
                        indexProperties = schema.objectStores[objectStoreName].indexes[indexName];
                        this.deleteFromIndex(index, storeEntry.primaryKey, storeEntry.value, indexProperties);
                    }
                    modifiedData = modifiedData.without(currKey);
                    currKey = modifiedData.nextHigherKey(currKey);
                }
                objectStoreMapEntry.store.modifiedData = modifiedData;
                return [2 /*return*/];
            });
        });
    };
    MemoryBackend.prototype.deleteFromIndex = function (index, primaryKey, value, indexProperties) {
        if (this.enableTracing) {
            console.log("deleteFromIndex(" + (index.modifiedName || index.originalName) + ")");
        }
        if (value === undefined || value === null) {
            throw Error("cannot delete null/undefined value from index");
        }
        var indexData = index.modifiedData || index.originalData;
        var indexKeys = getIndexKeys_1.default(value, indexProperties.keyPath, indexProperties.multiEntry);
        for (var _i = 0, indexKeys_1 = indexKeys; _i < indexKeys_1.length; _i++) {
            var indexKey = indexKeys_1[_i];
            var existingRecord = indexData.get(indexKey);
            if (!existingRecord) {
                throw Error("db inconsistent: expected index entry missing");
            }
            var newPrimaryKeys = existingRecord.primaryKeys.filter(function (x) { return cmp.default(x, primaryKey) !== 0; });
            if (newPrimaryKeys.length === 0) {
                index.modifiedData = indexData.without(indexKey);
            }
            else {
                var newIndexRecord = {
                    indexKey: indexKey,
                    primaryKeys: newPrimaryKeys,
                };
                index.modifiedData = indexData.with(indexKey, newIndexRecord, true);
            }
        }
    };
    MemoryBackend.prototype.getRecords = function (btx, req) {
        return __awaiter(this, void 0, void 0, function () {
            var myConn, db, objectStoreMapEntry, range, numResults, indexKeys, primaryKeys, values, forward, unique, storeData, haveIndex, index, indexData, indexPos, primaryPos, compareResult, primCompareResult, indexEntry, res, primkeySubPos, pos, cmpResult, cmpResult, res, skip, i, result, storePos, storeEntry, res;
            return __generator(this, function (_a) {
                if (this.enableTracing) {
                    console.log("TRACING: getRecords");
                    console.log("query", req);
                }
                myConn = this.connectionsByTransaction[btx.transactionCookie];
                if (!myConn) {
                    throw Error("unknown connection");
                }
                db = this.databases[myConn.dbName];
                if (!db) {
                    throw Error("db not found");
                }
                if (db.txLevel < TransactionLevel.Read) {
                    throw Error("only allowed while running a transaction");
                }
                if (db.txRestrictObjectStores &&
                    !db.txRestrictObjectStores.includes(req.objectStoreName)) {
                    throw Error("Not allowed to access store '" + req.objectStoreName + "', transaction is over " + JSON.stringify(db.txRestrictObjectStores));
                }
                objectStoreMapEntry = myConn.objectStoreMap[req.objectStoreName];
                if (!objectStoreMapEntry) {
                    throw Error("object store not found");
                }
                if (req.range == null || req.range === undefined) {
                    range = new BridgeIDBKeyRange_1.default(undefined, undefined, true, true);
                }
                else {
                    range = req.range;
                }
                if (typeof range !== "object") {
                    throw Error("getRecords was given an invalid range (sanity check failed, not an object)");
                }
                if (!("lowerOpen" in range)) {
                    throw Error("getRecords was given an invalid range (sanity check failed, lowerOpen missing)");
                }
                numResults = 0;
                indexKeys = [];
                primaryKeys = [];
                values = [];
                forward = req.direction === "next" || req.direction === "nextunique";
                unique = req.direction === "prevunique" || req.direction === "nextunique";
                storeData = objectStoreMapEntry.store.modifiedData ||
                    objectStoreMapEntry.store.originalData;
                haveIndex = req.indexName !== undefined;
                if (haveIndex) {
                    index = myConn.objectStoreMap[req.objectStoreName].indexMap[req.indexName];
                    indexData = index.modifiedData || index.originalData;
                    indexPos = req.lastIndexPosition;
                    if (indexPos === undefined) {
                        // First time we iterate!  So start at the beginning (lower/upper)
                        // of our allowed range.
                        indexPos = forward ? range.lower : range.upper;
                    }
                    primaryPos = req.lastObjectStorePosition;
                    // We might have to advance the index key further!
                    if (req.advanceIndexKey !== undefined) {
                        compareResult = cmp.default(req.advanceIndexKey, indexPos);
                        if ((forward && compareResult > 0) || (!forward && compareResult > 0)) {
                            indexPos = req.advanceIndexKey;
                        }
                        else if (compareResult == 0 && req.advancePrimaryKey !== undefined) {
                            // index keys are the same, so advance the primary key
                            if (primaryPos === undefined) {
                                primaryPos = req.advancePrimaryKey;
                            }
                            else {
                                primCompareResult = cmp.default(req.advancePrimaryKey, primaryPos);
                                if ((forward && primCompareResult > 0) ||
                                    (!forward && primCompareResult < 0)) {
                                    primaryPos = req.advancePrimaryKey;
                                }
                            }
                        }
                    }
                    if (indexPos === undefined || indexPos === null) {
                        indexPos = forward ? indexData.minKey() : indexData.maxKey();
                    }
                    indexEntry = void 0;
                    indexEntry = indexData.get(indexPos);
                    if (!indexEntry) {
                        res = indexData.nextHigherPair(indexPos);
                        if (res) {
                            indexEntry = res[1];
                            indexPos = indexEntry.indexKey;
                        }
                    }
                    primkeySubPos = 0;
                    // Sort out the case where the index key is the same, so we have
                    // to get the prev/next primary key
                    if (indexEntry !== undefined &&
                        req.lastIndexPosition !== undefined &&
                        cmp.default(indexEntry.indexKey, req.lastIndexPosition) === 0) {
                        pos = forward ? 0 : indexEntry.primaryKeys.length - 1;
                        this.enableTracing &&
                            console.log("number of primary keys", indexEntry.primaryKeys.length);
                        this.enableTracing && console.log("start pos is", pos);
                        // Advance past the lastObjectStorePosition
                        do {
                            cmpResult = cmp.default(req.lastObjectStorePosition, indexEntry.primaryKeys[pos]);
                            this.enableTracing && console.log("cmp result is", cmpResult);
                            if ((forward && cmpResult < 0) || (!forward && cmpResult > 0)) {
                                break;
                            }
                            pos += forward ? 1 : -1;
                            this.enableTracing && console.log("now pos is", pos);
                        } while (pos >= 0 && pos < indexEntry.primaryKeys.length);
                        // Make sure we're at least at advancedPrimaryPos
                        while (primaryPos !== undefined &&
                            pos >= 0 &&
                            pos < indexEntry.primaryKeys.length) {
                            cmpResult = cmp.default(primaryPos, indexEntry.primaryKeys[pos]);
                            if ((forward && cmpResult <= 0) || (!forward && cmpResult >= 0)) {
                                break;
                            }
                            pos += forward ? 1 : -1;
                        }
                        primkeySubPos = pos;
                    }
                    else if (indexEntry !== undefined) {
                        primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1;
                    }
                    if (this.enableTracing) {
                        console.log("subPos=", primkeySubPos);
                        console.log("indexPos=", indexPos);
                    }
                    while (1) {
                        if (req.limit != 0 && numResults == req.limit) {
                            break;
                        }
                        if (indexPos === undefined) {
                            break;
                        }
                        if (!range.includes(indexPos)) {
                            break;
                        }
                        if (indexEntry === undefined) {
                            break;
                        }
                        if (primkeySubPos < 0 ||
                            primkeySubPos >= indexEntry.primaryKeys.length) {
                            res = forward
                                ? indexData.nextHigherPair(indexPos)
                                : indexData.nextLowerPair(indexPos);
                            if (res) {
                                indexPos = res[1].indexKey;
                                indexEntry = res[1];
                                primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1;
                                continue;
                            }
                            else {
                                break;
                            }
                        }
                        skip = false;
                        if (unique) {
                            if (indexKeys.length > 0 &&
                                cmp.default(indexEntry.indexKey, indexKeys[indexKeys.length - 1]) === 0) {
                                skip = true;
                            }
                            if (req.lastIndexPosition !== undefined &&
                                cmp.default(indexPos, req.lastIndexPosition) === 0) {
                                skip = true;
                            }
                        }
                        if (!skip) {
                            if (this.enableTracing) {
                                console.log("not skipping!, subPos=" + primkeySubPos);
                            }
                            indexKeys.push(indexEntry.indexKey);
                            primaryKeys.push(indexEntry.primaryKeys[primkeySubPos]);
                            numResults++;
                        }
                        else {
                            if (this.enableTracing) {
                                console.log("skipping!");
                            }
                        }
                        primkeySubPos += forward ? 1 : -1;
                    }
                    // Now we can collect the values based on the primary keys,
                    // if requested.
                    if (req.resultLevel === backendInterface.ResultLevel.Full) {
                        for (i = 0; i < numResults; i++) {
                            result = storeData.get(primaryKeys[i]);
                            if (!result) {
                                console.error("invariant violated during read");
                                console.error("request was", req);
                                throw Error("invariant violated during read");
                            }
                            values.push(structuredClone_1.default(result.value));
                        }
                    }
                }
                else {
                    storePos = req.lastObjectStorePosition;
                    if (storePos === undefined) {
                        storePos = forward ? range.lower : range.upper;
                    }
                    if (req.advanceIndexKey !== undefined) {
                        throw Error("unsupported request");
                    }
                    storePos = furthestKey(forward, req.advancePrimaryKey, storePos);
                    if (storePos !== null && storePos !== undefined) {
                        storeEntry = storeData.get(storePos);
                        if (this.enableTracing) {
                            console.log("store entry:", storeEntry);
                        }
                        if (!storeEntry ||
                            (req.lastObjectStorePosition !== undefined &&
                                cmp.default(req.lastObjectStorePosition, storePos) === 0)) {
                            storePos = storeData.nextHigherKey(storePos);
                        }
                    }
                    else {
                        storePos = forward ? storeData.minKey() : storeData.maxKey();
                        if (this.enableTracing) {
                            console.log("setting starting store pos to", storePos);
                        }
                    }
                    while (1) {
                        if (req.limit != 0 && numResults == req.limit) {
                            break;
                        }
                        if (storePos === null || storePos === undefined) {
                            break;
                        }
                        if (!range.includes(storePos)) {
                            break;
                        }
                        res = storeData.get(storePos);
                        if (res === undefined) {
                            break;
                        }
                        if (req.resultLevel >= backendInterface.ResultLevel.OnlyKeys) {
                            primaryKeys.push(structuredClone_1.default(storePos));
                        }
                        if (req.resultLevel >= backendInterface.ResultLevel.Full) {
                            values.push(structuredClone_1.default(res.value));
                        }
                        numResults++;
                        storePos = nextStoreKey(forward, storeData, storePos);
                    }
                }
                if (this.enableTracing) {
                    console.log("TRACING: getRecords got " + numResults + " results");
                }
                return [2 /*return*/, {
                        count: numResults,
                        indexKeys: req.resultLevel >= backendInterface.ResultLevel.OnlyKeys && haveIndex
                            ? indexKeys
                            : undefined,
                        primaryKeys: req.resultLevel >= backendInterface.ResultLevel.OnlyKeys ? primaryKeys : undefined,
                        values: req.resultLevel >= backendInterface.ResultLevel.Full ? values : undefined,
                    }];
            });
        });
    };
    MemoryBackend.prototype.storeRecord = function (btx, storeReq) {
        return __awaiter(this, void 0, void 0, function () {
            var myConn, db, schema, objectStoreMapEntry, modifiedData, key, value, keygen, autoIncrement, keyPath, storeKeyResult, kp, n, m, hasKey, objectStoreRecord, _i, _a, indexName, index, indexProperties;
            return __generator(this, function (_b) {
                if (this.enableTracing) {
                    console.log("TRACING: storeRecord");
                }
                myConn = this.connectionsByTransaction[btx.transactionCookie];
                if (!myConn) {
                    throw Error("unknown connection");
                }
                db = this.databases[myConn.dbName];
                if (!db) {
                    throw Error("db not found");
                }
                if (db.txLevel < TransactionLevel.Write) {
                    throw Error("only allowed while running a transaction");
                }
                if (db.txRestrictObjectStores &&
                    !db.txRestrictObjectStores.includes(storeReq.objectStoreName)) {
                    throw Error("Not allowed to access store '" + storeReq.objectStoreName + "', transaction is over " + JSON.stringify(db.txRestrictObjectStores));
                }
                schema = myConn.modifiedSchema;
                objectStoreMapEntry = myConn.objectStoreMap[storeReq.objectStoreName];
                if (!objectStoreMapEntry.store.modifiedData) {
                    objectStoreMapEntry.store.modifiedData =
                        objectStoreMapEntry.store.originalData;
                }
                modifiedData = objectStoreMapEntry.store.modifiedData;
                if (storeReq.storeLevel === backendInterface.StoreLevel.UpdateExisting) {
                    if (storeReq.key === null || storeReq.key === undefined) {
                        throw Error("invalid update request (key not given)");
                    }
                    if (!objectStoreMapEntry.store.modifiedData.has(storeReq.key)) {
                        throw Error("invalid update request (record does not exist)");
                    }
                    key = storeReq.key;
                    value = storeReq.value;
                }
                else {
                    keygen = objectStoreMapEntry.store.modifiedKeyGenerator ||
                        objectStoreMapEntry.store.originalKeyGenerator;
                    autoIncrement = schema.objectStores[storeReq.objectStoreName].autoIncrement;
                    keyPath = schema.objectStores[storeReq.objectStoreName].keyPath;
                    storeKeyResult = void 0;
                    try {
                        storeKeyResult = makeStoreKeyValue_1.makeStoreKeyValue(storeReq.value, storeReq.key, keygen, autoIncrement, keyPath);
                    }
                    catch (e) {
                        if (e instanceof errors.DataError) {
                            kp = JSON.stringify(keyPath);
                            n = storeReq.objectStoreName;
                            m = "Could not extract key from value, objectStore=" + n + ", keyPath=" + kp;
                            if (this.enableTracing) {
                                console.error(e);
                                console.error("value was:", storeReq.value);
                                console.error("key was:", storeReq.key);
                            }
                            throw new errors.DataError(m);
                        }
                        else {
                            throw e;
                        }
                    }
                    key = storeKeyResult.key;
                    value = storeKeyResult.value;
                    objectStoreMapEntry.store.modifiedKeyGenerator =
                        storeKeyResult.updatedKeyGenerator;
                    hasKey = modifiedData.has(key);
                    if (hasKey && storeReq.storeLevel !== backendInterface.StoreLevel.AllowOverwrite) {
                        throw Error("refusing to overwrite");
                    }
                }
                objectStoreRecord = {
                    primaryKey: structuredClone_1.default(key),
                    value: structuredClone_1.default(value),
                };
                objectStoreMapEntry.store.modifiedData = modifiedData.with(key, objectStoreRecord, true);
                for (_i = 0, _a = Object.keys(schema.objectStores[storeReq.objectStoreName].indexes); _i < _a.length; _i++) {
                    indexName = _a[_i];
                    index = myConn.objectStoreMap[storeReq.objectStoreName].indexMap[indexName];
                    if (!index) {
                        throw Error("index referenced by object store does not exist");
                    }
                    indexProperties = schema.objectStores[storeReq.objectStoreName].indexes[indexName];
                    this.insertIntoIndex(index, key, value, indexProperties);
                }
                return [2 /*return*/, { key: key }];
            });
        });
    };
    MemoryBackend.prototype.insertIntoIndex = function (index, primaryKey, value, indexProperties) {
        if (this.enableTracing) {
            console.log("insertIntoIndex(" + (index.modifiedName || index.originalName) + ")");
        }
        var indexData = index.modifiedData || index.originalData;
        var indexKeys;
        try {
            indexKeys = getIndexKeys_1.default(value, indexProperties.keyPath, indexProperties.multiEntry);
        }
        catch (e) {
            if (e instanceof errors.DataError) {
                var n = index.modifiedName || index.originalName;
                var p = JSON.stringify(indexProperties.keyPath);
                var m = "Failed to extract index keys from index " + n + " for keyPath " + p + ".";
                if (this.enableTracing) {
                    console.error(m);
                    console.error("value was", value);
                }
                throw new errors.DataError(m);
            }
            else {
                throw e;
            }
        }
        for (var _i = 0, indexKeys_2 = indexKeys; _i < indexKeys_2.length; _i++) {
            var indexKey = indexKeys_2[_i];
            var existingRecord = indexData.get(indexKey);
            if (existingRecord) {
                if (indexProperties.unique) {
                    throw new errors.ConstraintError();
                }
                else {
                    var pred = function (x) { return cmp.default(x, primaryKey) === 0; };
                    if (existingRecord.primaryKeys.findIndex(pred) === -1) {
                        var newIndexRecord = {
                            indexKey: indexKey,
                            primaryKeys: __spreadArrays(existingRecord.primaryKeys, [primaryKey]).sort(cmp.default),
                        };
                        index.modifiedData = indexData.with(indexKey, newIndexRecord, true);
                    }
                }
            }
            else {
                var newIndexRecord = {
                    indexKey: indexKey,
                    primaryKeys: [primaryKey],
                };
                index.modifiedData = indexData.with(indexKey, newIndexRecord, true);
            }
        }
    };
    MemoryBackend.prototype.rollback = function (btx) {
        return __awaiter(this, void 0, void 0, function () {
            var myConn, db, objectStoreName, objectStore, _i, _a, indexName, index;
            return __generator(this, function (_b) {
                if (this.enableTracing) {
                    console.log("TRACING: rollback");
                }
                myConn = this.connectionsByTransaction[btx.transactionCookie];
                if (!myConn) {
                    throw Error("unknown connection");
                }
                db = this.databases[myConn.dbName];
                if (!db) {
                    throw Error("db not found");
                }
                if (db.txLevel < TransactionLevel.Read) {
                    throw Error("only allowed while running a transaction");
                }
                db.modifiedObjectStores = {};
                db.txLevel = TransactionLevel.Connected;
                db.txRestrictObjectStores = undefined;
                myConn.modifiedSchema = structuredClone_1.default(db.committedSchema);
                myConn.objectStoreMap = this.makeObjectStoreMap(db);
                for (objectStoreName in db.committedObjectStores) {
                    objectStore = db.committedObjectStores[objectStoreName];
                    objectStore.deleted = false;
                    objectStore.modifiedData = undefined;
                    objectStore.modifiedName = undefined;
                    objectStore.modifiedKeyGenerator = undefined;
                    objectStore.modifiedIndexes = {};
                    for (_i = 0, _a = Object.keys(db.committedSchema.objectStores[objectStoreName].indexes); _i < _a.length; _i++) {
                        indexName = _a[_i];
                        index = objectStore.committedIndexes[indexName];
                        index.deleted = false;
                        index.modifiedData = undefined;
                        index.modifiedName = undefined;
                    }
                }
                delete this.connectionsByTransaction[btx.transactionCookie];
                this.transactionDoneCond.trigger();
                return [2 /*return*/];
            });
        });
    };
    MemoryBackend.prototype.commit = function (btx) {
        return __awaiter(this, void 0, void 0, function () {
            var myConn, db, txLevel, objectStoreName, objectStoreMapEntry, store, indexName, index;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.enableTracing) {
                            console.log("TRACING: commit");
                        }
                        myConn = this.connectionsByTransaction[btx.transactionCookie];
                        if (!myConn) {
                            throw Error("unknown connection");
                        }
                        db = this.databases[myConn.dbName];
                        if (!db) {
                            throw Error("db not found");
                        }
                        txLevel = db.txLevel;
                        if (txLevel < TransactionLevel.Read) {
                            throw Error("only allowed while running a transaction");
                        }
                        db.committedSchema = structuredClone_1.default(myConn.modifiedSchema);
                        db.txLevel = TransactionLevel.Connected;
                        db.txRestrictObjectStores = undefined;
                        db.committedObjectStores = {};
                        db.committedObjectStores = {};
                        for (objectStoreName in myConn.objectStoreMap) {
                            objectStoreMapEntry = myConn.objectStoreMap[objectStoreName];
                            store = objectStoreMapEntry.store;
                            store.deleted = false;
                            store.originalData = store.modifiedData || store.originalData;
                            store.originalName = store.modifiedName || store.originalName;
                            store.modifiedIndexes = {};
                            if (store.modifiedKeyGenerator !== undefined) {
                                store.originalKeyGenerator = store.modifiedKeyGenerator;
                            }
                            db.committedObjectStores[objectStoreName] = store;
                            for (indexName in objectStoreMapEntry.indexMap) {
                                index = objectStoreMapEntry.indexMap[indexName];
                                index.deleted = false;
                                index.originalData = index.modifiedData || index.originalData;
                                index.originalName = index.modifiedName || index.originalName;
                                store.committedIndexes[indexName] = index;
                            }
                        }
                        myConn.objectStoreMap = this.makeObjectStoreMap(db);
                        delete this.connectionsByTransaction[btx.transactionCookie];
                        this.transactionDoneCond.trigger();
                        if (!(this.afterCommitCallback && txLevel >= TransactionLevel.Write)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.afterCommitCallback()];
                    case 1:
                        _a.sent();
                        _a.label = 2;
                    case 2: return [2 /*return*/];
                }
            });
        });
    };
    return MemoryBackend;
}());
exports.MemoryBackend = MemoryBackend;
exports.default = MemoryBackend;

});

unwrapExports(MemoryBackend_1);
var MemoryBackend_2 = MemoryBackend_1.MemoryBackend;

var build = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });

exports.BridgeIDBFactory = BridgeIDBFactory_1.BridgeIDBFactory;

exports.BridgeIDBCursor = BridgeIDBCursor_1.BridgeIDBCursor;









exports.MemoryBackend = MemoryBackend_1.MemoryBackend;
// globalThis polyfill, see https://mathiasbynens.be/notes/globalthis
(function () {
    if (typeof globalThis === "object")
        return;
    Object.defineProperty(Object.prototype, "__magic__", {
        get: function () {
            return this;
        },
        configurable: true,
    });
    // @ts-ignore: polyfill magic
    __magic__.globalThis = __magic__; // lolwat
    // @ts-ignore: polyfill magic
    delete Object.prototype.__magic__;
})();
/**
 * Populate the global name space such that the given IndexedDB factory is made
 * available globally.
 */
function shimIndexedDB(factory) {
    // @ts-ignore: shimming
    globalThis.indexedDB = factory;
    // @ts-ignore: shimming
    globalThis.IDBCursor = BridgeIDBCursor_1.BridgeIDBCursor;
    // @ts-ignore: shimming
    globalThis.IDBKeyRange = BridgeIDBKeyRange_1.default;
    // @ts-ignore: shimming
    globalThis.IDBDatabase = BridgeIDBDatabase_1.default;
    // @ts-ignore: shimming
    globalThis.IDBFactory = BridgeIDBFactory_1.BridgeIDBFactory;
    // @ts-ignore: shimming
    globalThis.IDBIndex = BridgeIDBIndex_1.BridgeIDBIndex;
    // @ts-ignore: shimming
    globalThis.IDBKeyRange = BridgeIDBKeyRange_1.default;
    // @ts-ignore: shimming
    globalThis.IDBObjectStore = BridgeIDBObjectStore_1.default;
    // @ts-ignore: shimming
    globalThis.IDBOpenDBRequest = BridgeIDBOpenDBRequest_1.default;
    // @ts-ignore: shimming
    globalThis.IDBRequest = BridgeIDBRequest_1.default;
    // @ts-ignore: shimming
    globalThis.IDBTransaction = BridgeIDBTransaction_1.default;
    // @ts-ignore: shimming
    globalThis.IDBVersionChangeEvent = BridgeIDBVersionChangeEvent_1.default;
}
exports.shimIndexedDB = shimIndexedDB;

});

unwrapExports(build);
var build_1 = build.BridgeIDBFactory;
var build_2 = build.BridgeIDBCursor;
var build_3 = build.MemoryBackend;
var build_4 = build.shimIndexedDB;

var cryptoImplementation = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 TALER is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 TALER is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Synchronous implementation of crypto-related functions for the wallet.
 *
 * The functionality is parameterized over an Emscripten environment.
 */
/**
 * Imports.
 */


const Amounts = __importStar(amounts);
const timer$1 = __importStar(timer);


var SignaturePurpose;
(function (SignaturePurpose) {
    SignaturePurpose[SignaturePurpose["RESERVE_WITHDRAW"] = 1200] = "RESERVE_WITHDRAW";
    SignaturePurpose[SignaturePurpose["WALLET_COIN_DEPOSIT"] = 1201] = "WALLET_COIN_DEPOSIT";
    SignaturePurpose[SignaturePurpose["MASTER_DENOMINATION_KEY_VALIDITY"] = 1025] = "MASTER_DENOMINATION_KEY_VALIDITY";
    SignaturePurpose[SignaturePurpose["WALLET_COIN_MELT"] = 1202] = "WALLET_COIN_MELT";
    SignaturePurpose[SignaturePurpose["TEST"] = 4242] = "TEST";
    SignaturePurpose[SignaturePurpose["MERCHANT_PAYMENT_OK"] = 1104] = "MERCHANT_PAYMENT_OK";
    SignaturePurpose[SignaturePurpose["MASTER_WIRE_FEES"] = 1028] = "MASTER_WIRE_FEES";
    SignaturePurpose[SignaturePurpose["WALLET_COIN_PAYBACK"] = 1203] = "WALLET_COIN_PAYBACK";
    SignaturePurpose[SignaturePurpose["WALLET_COIN_LINK"] = 1204] = "WALLET_COIN_LINK";
})(SignaturePurpose || (SignaturePurpose = {}));
function amountToBuffer(amount) {
    const buffer = new ArrayBuffer(8 + 4 + 12);
    const dvbuf = new DataView(buffer);
    const u8buf = new Uint8Array(buffer);
    const te = new TextEncoder();
    const curr = te.encode(amount.currency);
    dvbuf.setBigUint64(0, BigInt(amount.value));
    dvbuf.setUint32(8, amount.fraction);
    u8buf.set(curr, 8 + 4);
    return u8buf;
}
function timestampToBuffer(ts) {
    const b = new ArrayBuffer(8);
    const v = new DataView(b);
    const s = BigInt(ts.t_ms) * BigInt(1000);
    v.setBigUint64(0, s);
    return new Uint8Array(b);
}
function talerTimestampStringToBuffer(ts) {
    const t_sec = helpers.getTalerStampSec(ts);
    if (t_sec === null || t_sec === undefined) {
        // Should have been validated before!
        throw Error("invalid timestamp");
    }
    const buffer = new ArrayBuffer(8);
    const dvbuf = new DataView(buffer);
    const s = BigInt(t_sec) * BigInt(1000 * 1000);
    dvbuf.setBigUint64(0, s);
    return new Uint8Array(buffer);
}
class SignaturePurposeBuilder {
    constructor(purposeNum) {
        this.purposeNum = purposeNum;
        this.chunks = [];
    }
    put(bytes) {
        this.chunks.push(Uint8Array.from(bytes));
        return this;
    }
    build() {
        let payloadLen = 0;
        for (let c of this.chunks) {
            payloadLen += c.byteLength;
        }
        const buf = new ArrayBuffer(4 + 4 + payloadLen);
        const u8buf = new Uint8Array(buf);
        let p = 8;
        for (let c of this.chunks) {
            u8buf.set(c, p);
            p += c.byteLength;
        }
        const dvbuf = new DataView(buf);
        dvbuf.setUint32(0, payloadLen + 4 + 4);
        dvbuf.setUint32(4, this.purposeNum);
        return u8buf;
    }
}
function buildSigPS(purposeNum) {
    return new SignaturePurposeBuilder(purposeNum);
}
class CryptoImplementation {
    constructor() {
    }
    /**
     * Create a pre-coin of the given denomination to be withdrawn from then given
     * reserve.
     */
    createPlanchet(req) {
        const reservePub = talerCrypto.decodeCrock(req.reservePub);
        const reservePriv = talerCrypto.decodeCrock(req.reservePriv);
        const denomPub = talerCrypto.decodeCrock(req.denomPub);
        const coinKeyPair = talerCrypto.createEddsaKeyPair();
        const blindingFactor = talerCrypto.createBlindingKeySecret();
        const coinPubHash = talerCrypto.hash(coinKeyPair.eddsaPub);
        const ev = talerCrypto.rsaBlind(coinPubHash, blindingFactor, denomPub);
        const amountWithFee = Amounts.add(req.value, req.feeWithdraw).amount;
        const denomPubHash = talerCrypto.hash(denomPub);
        const evHash = talerCrypto.hash(ev);
        const withdrawRequest = buildSigPS(SignaturePurpose.RESERVE_WITHDRAW)
            .put(reservePub)
            .put(amountToBuffer(amountWithFee))
            .put(amountToBuffer(req.feeWithdraw))
            .put(denomPubHash)
            .put(evHash)
            .build();
        const sig = talerCrypto.eddsaSign(withdrawRequest, reservePriv);
        const planchet = {
            blindingKey: talerCrypto.encodeCrock(blindingFactor),
            coinEv: talerCrypto.encodeCrock(ev),
            coinPriv: talerCrypto.encodeCrock(coinKeyPair.eddsaPriv),
            coinPub: talerCrypto.encodeCrock(coinKeyPair.eddsaPub),
            coinValue: req.value,
            denomPub: talerCrypto.encodeCrock(denomPub),
            denomPubHash: talerCrypto.encodeCrock(denomPubHash),
            reservePub: talerCrypto.encodeCrock(reservePub),
            withdrawSig: talerCrypto.encodeCrock(sig),
        };
        return planchet;
    }
    /**
     * Create a planchet used for tipping, including the private keys.
     */
    createTipPlanchet(denom) {
        const denomPub = talerCrypto.decodeCrock(denom.denomPub);
        const coinKeyPair = talerCrypto.createEddsaKeyPair();
        const blindingFactor = talerCrypto.createBlindingKeySecret();
        const coinPubHash = talerCrypto.hash(coinKeyPair.eddsaPub);
        const ev = talerCrypto.rsaBlind(coinPubHash, blindingFactor, denomPub);
        const tipPlanchet = {
            blindingKey: talerCrypto.encodeCrock(blindingFactor),
            coinEv: talerCrypto.encodeCrock(ev),
            coinPriv: talerCrypto.encodeCrock(coinKeyPair.eddsaPriv),
            coinPub: talerCrypto.encodeCrock(coinKeyPair.eddsaPub),
            coinValue: denom.value,
            denomPub: talerCrypto.encodeCrock(denomPub),
            denomPubHash: talerCrypto.encodeCrock(talerCrypto.hash(denomPub)),
        };
        return tipPlanchet;
    }
    /**
     * Create and sign a message to request payback for a coin.
     */
    createPaybackRequest(coin) {
        const p = buildSigPS(SignaturePurpose.WALLET_COIN_PAYBACK)
            .put(talerCrypto.decodeCrock(coin.coinPub))
            .put(talerCrypto.decodeCrock(coin.denomPubHash))
            .put(talerCrypto.decodeCrock(coin.blindingKey))
            .build();
        const coinPriv = talerCrypto.decodeCrock(coin.coinPriv);
        const coinSig = talerCrypto.eddsaSign(p, coinPriv);
        const paybackRequest = {
            coin_blind_key_secret: coin.blindingKey,
            coin_pub: coin.coinPub,
            coin_sig: talerCrypto.encodeCrock(coinSig),
            denom_pub: coin.denomPub,
            denom_sig: coin.denomSig,
        };
        return paybackRequest;
    }
    /**
     * Check if a payment signature is valid.
     */
    isValidPaymentSignature(sig, contractHash, merchantPub) {
        const p = buildSigPS(SignaturePurpose.MERCHANT_PAYMENT_OK)
            .put(talerCrypto.decodeCrock(contractHash))
            .build();
        const sigBytes = talerCrypto.decodeCrock(sig);
        const pubBytes = talerCrypto.decodeCrock(merchantPub);
        return talerCrypto.eddsaVerify(p, sigBytes, pubBytes);
    }
    /**
     * Check if a wire fee is correctly signed.
     */
    isValidWireFee(type, wf, masterPub) {
        const p = buildSigPS(SignaturePurpose.MASTER_WIRE_FEES)
            .put(talerCrypto.hash(talerCrypto.stringToBytes(type + "\0")))
            .put(timestampToBuffer(wf.startStamp))
            .put(timestampToBuffer(wf.endStamp))
            .put(amountToBuffer(wf.wireFee))
            .build();
        const sig = talerCrypto.decodeCrock(wf.sig);
        const pub = talerCrypto.decodeCrock(masterPub);
        return talerCrypto.eddsaVerify(p, sig, pub);
    }
    /**
     * Check if the signature of a denomination is valid.
     */
    isValidDenom(denom, masterPub) {
        const p = buildSigPS(SignaturePurpose.MASTER_DENOMINATION_KEY_VALIDITY)
            .put(talerCrypto.decodeCrock(masterPub))
            .put(timestampToBuffer(denom.stampStart))
            .put(timestampToBuffer(denom.stampExpireWithdraw))
            .put(timestampToBuffer(denom.stampExpireDeposit))
            .put(timestampToBuffer(denom.stampExpireLegal))
            .put(amountToBuffer(denom.value))
            .put(amountToBuffer(denom.feeWithdraw))
            .put(amountToBuffer(denom.feeDeposit))
            .put(amountToBuffer(denom.feeRefresh))
            .put(amountToBuffer(denom.feeRefund))
            .put(talerCrypto.decodeCrock(denom.denomPubHash))
            .build();
        const sig = talerCrypto.decodeCrock(denom.masterSig);
        const pub = talerCrypto.decodeCrock(masterPub);
        return talerCrypto.eddsaVerify(p, sig, pub);
    }
    /**
     * Create a new EdDSA key pair.
     */
    createEddsaKeypair() {
        const pair = talerCrypto.createEddsaKeyPair();
        return {
            priv: talerCrypto.encodeCrock(pair.eddsaPriv),
            pub: talerCrypto.encodeCrock(pair.eddsaPub),
        };
    }
    /**
     * Unblind a blindly signed value.
     */
    rsaUnblind(sig, bk, pk) {
        const denomSig = talerCrypto.rsaUnblind(talerCrypto.decodeCrock(sig), talerCrypto.decodeCrock(pk), talerCrypto.decodeCrock(bk));
        return talerCrypto.encodeCrock(denomSig);
    }
    /**
     * Generate updated coins (to store in the database)
     * and deposit permissions for each given coin.
     */
    signDeposit(contractTerms, cds, totalAmount) {
        const ret = {
            originalCoins: [],
            sigs: [],
            updatedCoins: [],
        };
        const contractTermsHash = this.hashString(helpers.canonicalJson(contractTerms));
        const feeList = cds.map(x => x.denom.feeDeposit);
        let fees = Amounts.add(Amounts.getZero(feeList[0].currency), ...feeList)
            .amount;
        // okay if saturates
        fees = Amounts.sub(fees, Amounts.parseOrThrow(contractTerms.max_fee))
            .amount;
        const total = Amounts.add(fees, totalAmount).amount;
        let amountSpent = Amounts.getZero(cds[0].coin.currentAmount.currency);
        let amountRemaining = total;
        for (const cd of cds) {
            const originalCoin = Object.assign({}, cd.coin);
            if (amountRemaining.value === 0 && amountRemaining.fraction === 0) {
                break;
            }
            let coinSpend;
            if (Amounts.cmp(amountRemaining, cd.coin.currentAmount) < 0) {
                coinSpend = amountRemaining;
            }
            else {
                coinSpend = cd.coin.currentAmount;
            }
            amountSpent = Amounts.add(amountSpent, coinSpend).amount;
            const feeDeposit = cd.denom.feeDeposit;
            // Give the merchant at least the deposit fee, otherwise it'll reject
            // the coin.
            if (Amounts.cmp(coinSpend, feeDeposit) < 0) {
                coinSpend = feeDeposit;
            }
            const newAmount = Amounts.sub(cd.coin.currentAmount, coinSpend).amount;
            cd.coin.currentAmount = newAmount;
            cd.coin.status = dbTypes.CoinStatus.Dirty;
            const d = buildSigPS(SignaturePurpose.WALLET_COIN_DEPOSIT)
                .put(talerCrypto.decodeCrock(contractTermsHash))
                .put(talerCrypto.decodeCrock(contractTerms.H_wire))
                .put(talerTimestampStringToBuffer(contractTerms.timestamp))
                .put(talerTimestampStringToBuffer(contractTerms.refund_deadline))
                .put(amountToBuffer(coinSpend))
                .put(amountToBuffer(cd.denom.feeDeposit))
                .put(talerCrypto.decodeCrock(contractTerms.merchant_pub))
                .put(talerCrypto.decodeCrock(cd.coin.coinPub))
                .build();
            const coinSig = talerCrypto.eddsaSign(d, talerCrypto.decodeCrock(cd.coin.coinPriv));
            const s = {
                coin_pub: cd.coin.coinPub,
                coin_sig: talerCrypto.encodeCrock(coinSig),
                contribution: Amounts.toString(coinSpend),
                denom_pub: cd.coin.denomPub,
                exchange_url: cd.denom.exchangeBaseUrl,
                ub_sig: cd.coin.denomSig,
            };
            ret.sigs.push(s);
            ret.updatedCoins.push(cd.coin);
            ret.originalCoins.push(originalCoin);
        }
        return ret;
    }
    /**
     * Create a new refresh session.
     */
    createRefreshSession(exchangeBaseUrl, kappa, meltCoin, newCoinDenoms, meltFee) {
        let valueWithFee = Amounts.getZero(newCoinDenoms[0].value.currency);
        for (const ncd of newCoinDenoms) {
            valueWithFee = Amounts.add(valueWithFee, ncd.value, ncd.feeWithdraw)
                .amount;
        }
        // melt fee
        valueWithFee = Amounts.add(valueWithFee, meltFee).amount;
        const sessionHc = talerCrypto.createHashContext();
        const transferPubs = [];
        const transferPrivs = [];
        const planchetsForGammas = [];
        for (let i = 0; i < kappa; i++) {
            const transferKeyPair = talerCrypto.createEcdheKeyPair();
            sessionHc.update(transferKeyPair.ecdhePub);
            transferPrivs.push(talerCrypto.encodeCrock(transferKeyPair.ecdhePriv));
            transferPubs.push(talerCrypto.encodeCrock(transferKeyPair.ecdhePub));
        }
        for (const denom of newCoinDenoms) {
            const r = talerCrypto.decodeCrock(denom.denomPub);
            sessionHc.update(r);
        }
        sessionHc.update(talerCrypto.decodeCrock(meltCoin.coinPub));
        sessionHc.update(amountToBuffer(valueWithFee));
        for (let i = 0; i < kappa; i++) {
            const planchets = [];
            for (let j = 0; j < newCoinDenoms.length; j++) {
                const transferPriv = talerCrypto.decodeCrock(transferPrivs[i]);
                const oldCoinPub = talerCrypto.decodeCrock(meltCoin.coinPub);
                const transferSecret = talerCrypto.keyExchangeEcdheEddsa(transferPriv, oldCoinPub);
                const fresh = talerCrypto.setupRefreshPlanchet(transferSecret, j);
                const coinPriv = fresh.coinPriv;
                const coinPub = fresh.coinPub;
                const blindingFactor = fresh.bks;
                const pubHash = talerCrypto.hash(coinPub);
                const denomPub = talerCrypto.decodeCrock(newCoinDenoms[j].denomPub);
                const ev = talerCrypto.rsaBlind(pubHash, blindingFactor, denomPub);
                const planchet = {
                    blindingKey: talerCrypto.encodeCrock(blindingFactor),
                    coinEv: talerCrypto.encodeCrock(ev),
                    privateKey: talerCrypto.encodeCrock(coinPriv),
                    publicKey: talerCrypto.encodeCrock(coinPub),
                };
                planchets.push(planchet);
                sessionHc.update(ev);
            }
            planchetsForGammas.push(planchets);
        }
        const sessionHash = sessionHc.finish();
        const confirmData = buildSigPS(SignaturePurpose.WALLET_COIN_MELT)
            .put(sessionHash)
            .put(amountToBuffer(valueWithFee))
            .put(amountToBuffer(meltFee))
            .put(talerCrypto.decodeCrock(meltCoin.coinPub))
            .build();
        const confirmSig = talerCrypto.eddsaSign(confirmData, talerCrypto.decodeCrock(meltCoin.coinPriv));
        let valueOutput = Amounts.getZero(newCoinDenoms[0].value.currency);
        for (const denom of newCoinDenoms) {
            valueOutput = Amounts.add(valueOutput, denom.value).amount;
        }
        const refreshSessionId = talerCrypto.encodeCrock(talerCrypto.getRandomBytes(32));
        const refreshSession = {
            refreshSessionId,
            confirmSig: talerCrypto.encodeCrock(confirmSig),
            exchangeBaseUrl,
            finished: false,
            hash: talerCrypto.encodeCrock(sessionHash),
            meltCoinPub: meltCoin.coinPub,
            newDenomHashes: newCoinDenoms.map(d => d.denomPubHash),
            newDenoms: newCoinDenoms.map(d => d.denomPub),
            norevealIndex: undefined,
            planchetsForGammas: planchetsForGammas,
            transferPrivs,
            transferPubs,
            valueOutput,
            valueWithFee,
        };
        return refreshSession;
    }
    /**
     * Hash a string including the zero terminator.
     */
    hashString(str) {
        const ts = new TextEncoder();
        const b = ts.encode(str + "\0");
        return talerCrypto.encodeCrock(talerCrypto.hash(b));
    }
    /**
     * Hash a denomination public key.
     */
    hashDenomPub(denomPub) {
        return talerCrypto.encodeCrock(talerCrypto.hash(talerCrypto.decodeCrock(denomPub)));
    }
    signCoinLink(oldCoinPriv, newDenomHash, oldCoinPub, transferPub, coinEv) {
        const coinEvHash = talerCrypto.hash(talerCrypto.decodeCrock(coinEv));
        const coinLink = buildSigPS(SignaturePurpose.WALLET_COIN_LINK)
            .put(talerCrypto.decodeCrock(newDenomHash))
            .put(talerCrypto.decodeCrock(oldCoinPub))
            .put(talerCrypto.decodeCrock(transferPub))
            .put(coinEvHash)
            .build();
        const coinPriv = talerCrypto.decodeCrock(oldCoinPriv);
        const sig = talerCrypto.eddsaSign(coinLink, coinPriv);
        return talerCrypto.encodeCrock(sig);
    }
    benchmark(repetitions) {
        let time_hash = 0;
        for (let i = 0; i < repetitions; i++) {
            const start = timer$1.performanceNow();
            this.hashString("hello world");
            time_hash += timer$1.performanceNow() - start;
        }
        let time_hash_big = 0;
        for (let i = 0; i < repetitions; i++) {
            const ba = naclFast.randomBytes(4096);
            const start = timer$1.performanceNow();
            talerCrypto.hash(ba);
            time_hash_big += timer$1.performanceNow() - start;
        }
        let time_eddsa_create = 0;
        for (let i = 0; i < repetitions; i++) {
            const start = timer$1.performanceNow();
            const pair = talerCrypto.createEddsaKeyPair();
            time_eddsa_create += timer$1.performanceNow() - start;
        }
        let time_eddsa_sign = 0;
        const p = naclFast.randomBytes(4096);
        const pair = talerCrypto.createEddsaKeyPair();
        for (let i = 0; i < repetitions; i++) {
            const start = timer$1.performanceNow();
            talerCrypto.eddsaSign(p, pair.eddsaPriv);
            time_eddsa_sign += timer$1.performanceNow() - start;
        }
        const sig = talerCrypto.eddsaSign(p, pair.eddsaPriv);
        let time_eddsa_verify = 0;
        for (let i = 0; i < repetitions; i++) {
            const start = timer$1.performanceNow();
            talerCrypto.eddsaVerify(p, sig, pair.eddsaPub);
            time_eddsa_verify += timer$1.performanceNow() - start;
        }
        return {
            repetitions,
            time: {
                hash_small: time_hash,
                hash_big: time_hash_big,
                eddsa_create: time_eddsa_create,
                eddsa_sign: time_eddsa_sign,
                eddsa_verify: time_eddsa_verify,
            },
        };
    }
}
exports.CryptoImplementation = CryptoImplementation;
CryptoImplementation.enableTracing = false;

});

unwrapExports(cryptoImplementation);
var cryptoImplementation_1 = cryptoImplementation.CryptoImplementation;

var synchronousWorker = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });

/**
 * The synchronous crypto worker produced by this factory doesn't run in the
 * background, but actually blocks the caller until the operation is done.
 */
class SynchronousCryptoWorkerFactory {
    startWorker() {
        if (typeof require === "undefined") {
            throw Error("cannot make worker, require(...) not defined");
        }
        const workerCtor = synchronousWorker.SynchronousCryptoWorker;
        return new workerCtor();
    }
    getConcurrency() {
        return 1;
    }
}
exports.SynchronousCryptoWorkerFactory = SynchronousCryptoWorkerFactory;
/**
 * Worker implementation that uses node subprocesses.
 */
class SynchronousCryptoWorker {
    constructor() {
        this.onerror = undefined;
        this.onmessage = undefined;
    }
    /**
     * Add an event listener for either an "error" or "message" event.
     */
    addEventListener(event, fn) {
        switch (event) {
            case "message":
                this.onmessage = fn;
                break;
            case "error":
                this.onerror = fn;
                break;
        }
    }
    dispatchMessage(msg) {
        if (this.onmessage) {
            this.onmessage({ data: msg });
        }
    }
    handleRequest(operation, id, args) {
        return __awaiter(this, void 0, void 0, function* () {
            const impl = new cryptoImplementation.CryptoImplementation();
            if (!(operation in impl)) {
                console.error(`crypto operation '${operation}' not found`);
                return;
            }
            let result;
            try {
                result = impl[operation](...args);
            }
            catch (e) {
                console.log("error during operation", e);
                return;
            }
            try {
                setImmediate(() => this.dispatchMessage({ result, id }));
            }
            catch (e) {
                console.log("got error during dispatch", e);
            }
        });
    }
    /**
     * Send a message to the worker thread.
     */
    postMessage(msg) {
        const args = msg.args;
        if (!Array.isArray(args)) {
            console.error("args must be array");
            return;
        }
        const id = msg.id;
        if (typeof id !== "number") {
            console.error("RPC id must be number");
            return;
        }
        const operation = msg.operation;
        if (typeof operation !== "string") {
            console.error("RPC operation must be string");
            return;
        }
        this.handleRequest(operation, id, args).catch(e => {
            console.error("Error while handling crypto request:", e);
        });
    }
    /**
     * Forcibly terminate the worker thread.
     */
    terminate() {
        // This is a no-op.
    }
}
exports.SynchronousCryptoWorker = SynchronousCryptoWorker;

});

unwrapExports(synchronousWorker);
var synchronousWorker_1 = synchronousWorker.SynchronousCryptoWorkerFactory;
var synchronousWorker_2 = synchronousWorker.SynchronousCryptoWorker;

var db = createCommonjsModule(function (module, exports) {
Object.defineProperty(exports, "__esModule", { value: true });


const DB_NAME = "taler";
/**
 * Return a promise that resolves
 * to the taler wallet db.
 */
function openTalerDb(idbFactory, onVersionChange, onUpgradeUnsupported) {
    return new Promise((resolve, reject) => {
        const req = idbFactory.open(DB_NAME, dbTypes.WALLET_DB_VERSION);
        req.onerror = e => {
            console.log("taler database error", e);
            reject(new Error("database error"));
        };
        req.onsuccess = e => {
            req.result.onversionchange = (evt) => {
                console.log(`handling live db version change from ${evt.oldVersion} to ${evt.newVersion}`);
                req.result.close();
                onVersionChange();
            };
            resolve(req.result);
        };
        req.onupgradeneeded = e => {
            const db = req.result;
            console.log(`DB: upgrade needed: oldVersion=${e.oldVersion}, newVersion=${e.newVersion}`);
            switch (e.oldVersion) {
                case 0: // DB does not exist yet
                    for (const n in dbTypes.Stores) {
                        if (dbTypes.Stores[n] instanceof query.Store) {
                            const si = dbTypes.Stores[n];
                            const s = db.createObjectStore(si.name, si.storeParams);
                            for (const indexName in si) {
                                if (si[indexName] instanceof query.Index) {
                                    const ii = si[indexName];
                                    s.createIndex(ii.indexName, ii.keyPath, ii.options);
                                }
                            }
                        }
                    }
                    break;
                default:
                    if (e.oldVersion !== dbTypes.WALLET_DB_VERSION) {
                        onUpgradeUnsupported(e.oldVersion, dbTypes.WALLET_DB_VERSION);
                        throw Error("incompatible DB");
                    }
                    break;
            }
        };
    });
}
exports.openTalerDb = openTalerDb;
function exportDb(db) {
    const dump = {
        name: db.name,
        stores: {},
        version: db.version,
    };
    return new Promise((resolve, reject) => {
        const tx = db.transaction(Array.from(db.objectStoreNames));
        tx.addEventListener("complete", () => {
            resolve(dump);
        });
        // tslint:disable-next-line:prefer-for-of
        for (let i = 0; i < db.objectStoreNames.length; i++) {
            const name = db.objectStoreNames[i];
            const storeDump = {};
            dump.stores[name] = storeDump;
            tx.objectStore(name)
                .openCursor()
                .addEventListener("success", (e) => {
                const cursor = e.target.result;
                if (cursor) {
                    storeDump[cursor.key] = cursor.value;
                    cursor.continue();
                }
            });
        }
    });
}
exports.exportDb = exportDb;
function importDb(db, dump) {
    console.log("importing db", dump);
    return new Promise((resolve, reject) => {
        const tx = db.transaction(Array.from(db.objectStoreNames), "readwrite");
        if (dump.stores) {
            for (const storeName in dump.stores) {
                const objects = [];
                const dumpStore = dump.stores[storeName];
                for (const key in dumpStore) {
                    objects.push(dumpStore[key]);
                }
                console.log(`importing ${objects.length} records into ${storeName}`);
                const store = tx.objectStore(storeName);
                for (const obj of objects) {
                    store.put(obj);
                }
            }
        }
        tx.addEventListener("complete", () => {
            resolve();
        });
    });
}
exports.importDb = importDb;
function deleteDb(idbFactory) {
    idbFactory.deleteDatabase(DB_NAME);
}
exports.deleteDb = deleteDb;

});

unwrapExports(db);
var db_1 = db.openTalerDb;
var db_2 = db.exportDb;
var db_3 = db.importDb;
var db_4 = db.deleteDb;

var bind = function bind(fn, thisArg) {
  return function wrap() {
    var args = new Array(arguments.length);
    for (var i = 0; i < args.length; i++) {
      args[i] = arguments[i];
    }
    return fn.apply(thisArg, args);
  };
};

/*!
 * Determine if an object is a Buffer
 *
 * @author   Feross Aboukhadijeh <https://feross.org>
 * @license  MIT
 */

var isBuffer = function isBuffer (obj) {
  return obj != null && obj.constructor != null &&
    typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
};

/*global toString:true*/

// utils is a library of generic helper functions non-specific to axios

var toString = Object.prototype.toString;

/**
 * Determine if a value is an Array
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an Array, otherwise false
 */
function isArray(val) {
  return toString.call(val) === '[object Array]';
}

/**
 * Determine if a value is an ArrayBuffer
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an ArrayBuffer, otherwise false
 */
function isArrayBuffer(val) {
  return toString.call(val) === '[object ArrayBuffer]';
}

/**
 * Determine if a value is a FormData
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an FormData, otherwise false
 */
function isFormData(val) {
  return (typeof FormData !== 'undefined') && (val instanceof FormData);
}

/**
 * Determine if a value is a view on an ArrayBuffer
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
 */
function isArrayBufferView(val) {
  var result;
  if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
    result = ArrayBuffer.isView(val);
  } else {
    result = (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
  }
  return result;
}

/**
 * Determine if a value is a String
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a String, otherwise false
 */
function isString(val) {
  return typeof val === 'string';
}

/**
 * Determine if a value is a Number
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a Number, otherwise false
 */
function isNumber(val) {
  return typeof val === 'number';
}

/**
 * Determine if a value is undefined
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if the value is undefined, otherwise false
 */
function isUndefined(val) {
  return typeof val === 'undefined';
}

/**
 * Determine if a value is an Object
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an Object, otherwise false
 */
function isObject(val) {
  return val !== null && typeof val === 'object';
}

/**
 * Determine if a value is a Date
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a Date, otherwise false
 */
function isDate(val) {
  return toString.call(val) === '[object Date]';
}

/**
 * Determine if a value is a File
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a File, otherwise false
 */
function isFile(val) {
  return toString.call(val) === '[object File]';
}

/**
 * Determine if a value is a Blob
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a Blob, otherwise false
 */
function isBlob(val) {
  return toString.call(val) === '[object Blob]';
}

/**
 * Determine if a value is a Function
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a Function, otherwise false
 */
function isFunction(val) {
  return toString.call(val) === '[object Function]';
}

/**
 * Determine if a value is a Stream
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a Stream, otherwise false
 */
function isStream(val) {
  return isObject(val) && isFunction(val.pipe);
}

/**
 * Determine if a value is a URLSearchParams object
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a URLSearchParams object, otherwise false
 */
function isURLSearchParams(val) {
  return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
}

/**
 * Trim excess whitespace off the beginning and end of a string
 *
 * @param {String} str The String to trim
 * @returns {String} The String freed of excess whitespace
 */
function trim(str) {
  return str.replace(/^\s*/, '').replace(/\s*$/, '');
}

/**
 * Determine if we're running in a standard browser environment
 *
 * This allows axios to run in a web worker, and react-native.
 * Both environments support XMLHttpRequest, but not fully standard globals.
 *
 * web workers:
 *  typeof window -> undefined
 *  typeof document -> undefined
 *
 * react-native:
 *  navigator.product -> 'ReactNative'
 * nativescript
 *  navigator.product -> 'NativeScript' or 'NS'
 */
function isStandardBrowserEnv() {
  if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
                                           navigator.product === 'NativeScript' ||
                                           navigator.product === 'NS')) {
    return false;
  }
  return (
    typeof window !== 'undefined' &&
    typeof document !== 'undefined'
  );
}

/**
 * Iterate over an Array or an Object invoking a function for each item.
 *
 * If `obj` is an Array callback will be called passing
 * the value, index, and complete array for each item.
 *
 * If 'obj' is an Object callback will be called passing
 * the value, key, and complete object for each property.
 *
 * @param {Object|Array} obj The object to iterate
 * @param {Function} fn The callback to invoke for each item
 */
function forEach(obj, fn) {
  // Don't bother if no value provided
  if (obj === null || typeof obj === 'undefined') {
    return;
  }

  // Force an array if not already something iterable
  if (typeof obj !== 'object') {
    /*eslint no-param-reassign:0*/
    obj = [obj];
  }

  if (isArray(obj)) {
    // Iterate over array values
    for (var i = 0, l = obj.length; i < l; i++) {
      fn.call(null, obj[i], i, obj);
    }
  } else {
    // Iterate over object keys
    for (var key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        fn.call(null, obj[key], key, obj);
      }
    }
  }
}

/**
 * Accepts varargs expecting each argument to be an object, then
 * immutably merges the properties of each object and returns result.
 *
 * When multiple objects contain the same key the later object in
 * the arguments list will take precedence.
 *
 * Example:
 *
 * ```js
 * var result = merge({foo: 123}, {foo: 456});
 * console.log(result.foo); // outputs 456
 * ```
 *
 * @param {Object} obj1 Object to merge
 * @returns {Object} Result of all merge properties
 */
function merge(/* obj1, obj2, obj3, ... */) {
  var result = {};
  function assignValue(val, key) {
    if (typeof result[key] === 'object' && typeof val === 'object') {
      result[key] = merge(result[key], val);
    } else {
      result[key] = val;
    }
  }

  for (var i = 0, l = arguments.length; i < l; i++) {
    forEach(arguments[i], assignValue);
  }
  return result;
}

/**
 * Function equal to merge with the difference being that no reference
 * to original objects is kept.
 *
 * @see merge
 * @param {Object} obj1 Object to merge
 * @returns {Object} Result of all merge properties
 */
function deepMerge(/* obj1, obj2, obj3, ... */) {
  var result = {};
  function assignValue(val, key) {
    if (typeof result[key] === 'object' && typeof val === 'object') {
      result[key] = deepMerge(result[key], val);
    } else if (typeof val === 'object') {
      result[key] = deepMerge({}, val);
    } else {
      result[key] = val;
    }
  }

  for (var i = 0, l = arguments.length; i < l; i++) {
    forEach(arguments[i], assignValue);
  }
  return result;
}

/**
 * Extends object a by mutably adding to it the properties of object b.
 *
 * @param {Object} a The object to be extended
 * @param {Object} b The object to copy properties from
 * @param {Object} thisArg The object to bind function to
 * @return {Object} The resulting value of object a
 */
function extend(a, b, thisArg) {
  forEach(b, function assignValue(val, key) {
    if (thisArg && typeof val === 'function') {
      a[key] = bind(val, thisArg);
    } else {
      a[key] = val;
    }
  });
  return a;
}

var utils = {
  isArray: isArray,
  isArrayBuffer: isArrayBuffer,
  isBuffer: isBuffer,
  isFormData: isFormData,
  isArrayBufferView: isArrayBufferView,
  isString: isString,
  isNumber: isNumber,
  isObject: isObject,
  isUndefined: isUndefined,
  isDate: isDate,
  isFile: isFile,
  isBlob: isBlob,
  isFunction: isFunction,
  isStream: isStream,
  isURLSearchParams: isURLSearchParams,
  isStandardBrowserEnv: isStandardBrowserEnv,
  forEach: forEach,
  merge: merge,
  deepMerge: deepMerge,
  extend: extend,
  trim: trim
};

function encode(val) {
  return encodeURIComponent(val).
    replace(/%40/gi, '@').
    replace(/%3A/gi, ':').
    replace(/%24/g, '$').
    replace(/%2C/gi, ',').
    replace(/%20/g, '+').
    replace(/%5B/gi, '[').
    replace(/%5D/gi, ']');
}

/**
 * Build a URL by appending params to the end
 *
 * @param {string} url The base of the url (e.g., http://www.google.com)
 * @param {object} [params] The params to be appended
 * @returns {string} The formatted url
 */
var buildURL = function buildURL(url, params, paramsSerializer) {
  /*eslint no-param-reassign:0*/
  if (!params) {
    return url;
  }

  var serializedParams;
  if (paramsSerializer) {
    serializedParams = paramsSerializer(params);
  } else if (utils.isURLSearchParams(params)) {
    serializedParams = params.toString();
  } else {
    var parts = [];

    utils.forEach(params, function serialize(val, key) {
      if (val === null || typeof val === 'undefined') {
        return;
      }

      if (utils.isArray(val)) {
        key = key + '[]';
      } else {
        val = [val];
      }

      utils.forEach(val, function parseValue(v) {
        if (utils.isDate(v)) {
          v = v.toISOString();
        } else if (utils.isObject(v)) {
          v = JSON.stringify(v);
        }
        parts.push(encode(key) + '=' + encode(v));
      });
    });

    serializedParams = parts.join('&');
  }

  if (serializedParams) {
    var hashmarkIndex = url.indexOf('#');
    if (hashmarkIndex !== -1) {
      url = url.slice(0, hashmarkIndex);
    }

    url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
  }

  return url;
};

function InterceptorManager() {
  this.handlers = [];
}

/**
 * Add a new interceptor to the stack
 *
 * @param {Function} fulfilled The function to handle `then` for a `Promise`
 * @param {Function} rejected The function to handle `reject` for a `Promise`
 *
 * @return {Number} An ID used to remove interceptor later
 */
InterceptorManager.prototype.use = function use(fulfilled, rejected) {
  this.handlers.push({
    fulfilled: fulfilled,
    rejected: rejected
  });
  return this.handlers.length - 1;
};

/**
 * Remove an interceptor from the stack
 *
 * @param {Number} id The ID that was returned by `use`
 */
InterceptorManager.prototype.eject = function eject(id) {
  if (this.handlers[id]) {
    this.handlers[id] = null;
  }
};

/**
 * Iterate over all the registered interceptors
 *
 * This method is particularly useful for skipping over any
 * interceptors that may have become `null` calling `eject`.
 *
 * @param {Function} fn The function to call for each interceptor
 */
InterceptorManager.prototype.forEach = function forEach(fn) {
  utils.forEach(this.handlers, function forEachHandler(h) {
    if (h !== null) {
      fn(h);
    }
  });
};

var InterceptorManager_1 = InterceptorManager;

/**
 * Transform the data for a request or a response
 *
 * @param {Object|String} data The data to be transformed
 * @param {Array} headers The headers for the request or response
 * @param {Array|Function} fns A single function or Array of functions
 * @returns {*} The resulting transformed data
 */
var transformData = function transformData(data, headers, fns) {
  /*eslint no-param-reassign:0*/
  utils.forEach(fns, function transform(fn) {
    data = fn(data, headers);
  });

  return data;
};

var isCancel = function isCancel(value) {
  return !!(value && value.__CANCEL__);
};

var normalizeHeaderName = function normalizeHeaderName(headers, normalizedName) {
  utils.forEach(headers, function processHeader(value, name) {
    if (name !== normalizedName && name.toUpperCase() === normalizedName.toUpperCase()) {
      headers[normalizedName] = value;
      delete headers[name];
    }
  });
};

/**
 * Update an Error with the specified config, error code, and response.
 *
 * @param {Error} error The error to update.
 * @param {Object} config The config.
 * @param {string} [code] The error code (for example, 'ECONNABORTED').
 * @param {Object} [request] The request.
 * @param {Object} [response] The response.
 * @returns {Error} The error.
 */
var enhanceError = function enhanceError(error, config, code, request, response) {
  error.config = config;
  if (code) {
    error.code = code;
  }

  error.request = request;
  error.response = response;
  error.isAxiosError = true;

  error.toJSON = function() {
    return {
      // Standard
      message: this.message,
      name: this.name,
      // Microsoft
      description: this.description,
      number: this.number,
      // Mozilla
      fileName: this.fileName,
      lineNumber: this.lineNumber,
      columnNumber: this.columnNumber,
      stack: this.stack,
      // Axios
      config: this.config,
      code: this.code
    };
  };
  return error;
};

/**
 * Create an Error with the specified message, config, error code, request and response.
 *
 * @param {string} message The error message.
 * @param {Object} config The config.
 * @param {string} [code] The error code (for example, 'ECONNABORTED').
 * @param {Object} [request] The request.
 * @param {Object} [response] The response.
 * @returns {Error} The created error.
 */
var createError = function createError(message, config, code, request, response) {
  var error = new Error(message);
  return enhanceError(error, config, code, request, response);
};

/**
 * Resolve or reject a Promise based on response status.
 *
 * @param {Function} resolve A function that resolves the promise.
 * @param {Function} reject A function that rejects the promise.
 * @param {object} response The response.
 */
var settle = function settle(resolve, reject, response) {
  var validateStatus = response.config.validateStatus;
  if (!validateStatus || validateStatus(response.status)) {
    resolve(response);
  } else {
    reject(createError(
      'Request failed with status code ' + response.status,
      response.config,
      null,
      response.request,
      response
    ));
  }
};

/**
 * Helpers.
 */

var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var y = d * 365.25;

/**
 * Parse or format the given `val`.
 *
 * Options:
 *
 *  - `long` verbose formatting [false]
 *
 * @param {String|Number} val
 * @param {Object} [options]
 * @throws {Error} throw an error if val is not a non-empty string or a number
 * @return {String|Number}
 * @api public
 */

var ms = function(val, options) {
  options = options || {};
  var type = typeof val;
  if (type === 'string' && val.length > 0) {
    return parse(val);
  } else if (type === 'number' && isNaN(val) === false) {
    return options.long ? fmtLong(val) : fmtShort(val);
  }
  throw new Error(
    'val is not a non-empty string or a valid number. val=' +
      JSON.stringify(val)
  );
};

/**
 * Parse the given `str` and return milliseconds.
 *
 * @param {String} str
 * @return {Number}
 * @api private
 */

function parse(str) {
  str = String(str);
  if (str.length > 100) {
    return;
  }
  var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(
    str
  );
  if (!match) {
    return;
  }
  var n = parseFloat(match[1]);
  var type = (match[2] || 'ms').toLowerCase();
  switch (type) {
    case 'years':
    case 'year':
    case 'yrs':
    case 'yr':
    case 'y':
      return n * y;
    case 'days':
    case 'day':
    case 'd':
      return n * d;
    case 'hours':
    case 'hour':
    case 'hrs':
    case 'hr':
    case 'h':
      return n * h;
    case 'minutes':
    case 'minute':
    case 'mins':
    case 'min':
    case 'm':
      return n * m;
    case 'seconds':
    case 'second':
    case 'secs':
    case 'sec':
    case 's':
      return n * s;
    case 'milliseconds':
    case 'millisecond':
    case 'msecs':
    case 'msec':
    case 'ms':
      return n;
    default:
      return undefined;
  }
}

/**
 * Short format for `ms`.
 *
 * @param {Number} ms
 * @return {String}
 * @api private
 */

function fmtShort(ms) {
  if (ms >= d) {
    return Math.round(ms / d) + 'd';
  }
  if (ms >= h) {
    return Math.round(ms / h) + 'h';
  }
  if (ms >= m) {
    return Math.round(ms / m) + 'm';
  }
  if (ms >= s) {
    return Math.round(ms / s) + 's';
  }
  return ms + 'ms';
}

/**
 * Long format for `ms`.
 *
 * @param {Number} ms
 * @return {String}
 * @api private
 */

function fmtLong(ms) {
  return plural(ms, d, 'day') ||
    plural(ms, h, 'hour') ||
    plural(ms, m, 'minute') ||
    plural(ms, s, 'second') ||
    ms + ' ms';
}

/**
 * Pluralization helper.
 */

function plural(ms, n, name) {
  if (ms < n) {
    return;
  }
  if (ms < n * 1.5) {
    return Math.floor(ms / n) + ' ' + name;
  }
  return Math.ceil(ms / n) + ' ' + name + 's';
}

var debug = createCommonjsModule(function (module, exports) {
/**
 * This is the common logic for both the Node.js and web browser
 * implementations of `debug()`.
 *
 * Expose `debug()` as the module.
 */

exports = module.exports = createDebug.debug = createDebug['default'] = createDebug;
exports.coerce = coerce;
exports.disable = disable;
exports.enable = enable;
exports.enabled = enabled;
exports.humanize = ms;

/**
 * Active `debug` instances.
 */
exports.instances = [];

/**
 * The currently active debug mode names, and names to skip.
 */

exports.names = [];
exports.skips = [];

/**
 * Map of special "%n" handling functions, for the debug "format" argument.
 *
 * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
 */

exports.formatters = {};

/**
 * Select a color.
 * @param {String} namespace
 * @return {Number}
 * @api private
 */

function selectColor(namespace) {
  var hash = 0, i;

  for (i in namespace) {
    hash  = ((hash << 5) - hash) + namespace.charCodeAt(i);
    hash |= 0; // Convert to 32bit integer
  }

  return exports.colors[Math.abs(hash) % exports.colors.length];
}

/**
 * Create a debugger with the given `namespace`.
 *
 * @param {String} namespace
 * @return {Function}
 * @api public
 */

function createDebug(namespace) {

  var prevTime;

  function debug() {
    // disabled?
    if (!debug.enabled) return;

    var self = debug;

    // set `diff` timestamp
    var curr = +new Date();
    var ms = curr - (prevTime || curr);
    self.diff = ms;
    self.prev = prevTime;
    self.curr = curr;
    prevTime = curr;

    // turn the `arguments` into a proper Array
    var args = new Array(arguments.length);
    for (var i = 0; i < args.length; i++) {
      args[i] = arguments[i];
    }

    args[0] = exports.coerce(args[0]);

    if ('string' !== typeof args[0]) {
      // anything else let's inspect with %O
      args.unshift('%O');
    }

    // apply any `formatters` transformations
    var index = 0;
    args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
      // if we encounter an escaped % then don't increase the array index
      if (match === '%%') return match;
      index++;
      var formatter = exports.formatters[format];
      if ('function' === typeof formatter) {
        var val = args[index];
        match = formatter.call(self, val);

        // now we need to remove `args[index]` since it's inlined in the `format`
        args.splice(index, 1);
        index--;
      }
      return match;
    });

    // apply env-specific formatting (colors, etc.)
    exports.formatArgs.call(self, args);

    var logFn = debug.log || exports.log || console.log.bind(console);
    logFn.apply(self, args);
  }

  debug.namespace = namespace;
  debug.enabled = exports.enabled(namespace);
  debug.useColors = exports.useColors();
  debug.color = selectColor(namespace);
  debug.destroy = destroy;

  // env-specific initialization logic for debug instances
  if ('function' === typeof exports.init) {
    exports.init(debug);
  }

  exports.instances.push(debug);

  return debug;
}

function destroy () {
  var index = exports.instances.indexOf(this);
  if (index !== -1) {
    exports.instances.splice(index, 1);
    return true;
  } else {
    return false;
  }
}

/**
 * Enables a debug mode by namespaces. This can include modes
 * separated by a colon and wildcards.
 *
 * @param {String} namespaces
 * @api public
 */

function enable(namespaces) {
  exports.save(namespaces);

  exports.names = [];
  exports.skips = [];

  var i;
  var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  var len = split.length;

  for (i = 0; i < len; i++) {
    if (!split[i]) continue; // ignore empty strings
    namespaces = split[i].replace(/\*/g, '.*?');
    if (namespaces[0] === '-') {
      exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
    } else {
      exports.names.push(new RegExp('^' + namespaces + '$'));
    }
  }

  for (i = 0; i < exports.instances.length; i++) {
    var instance = exports.instances[i];
    instance.enabled = exports.enabled(instance.namespace);
  }
}

/**
 * Disable debug output.
 *
 * @api public
 */

function disable() {
  exports.enable('');
}

/**
 * Returns true if the given mode name is enabled, false otherwise.
 *
 * @param {String} name
 * @return {Boolean}
 * @api public
 */

function enabled(name) {
  if (name[name.length - 1] === '*') {
    return true;
  }
  var i, len;
  for (i = 0, len = exports.skips.length; i < len; i++) {
    if (exports.skips[i].test(name)) {
      return false;
    }
  }
  for (i = 0, len = exports.names.length; i < len; i++) {
    if (exports.names[i].test(name)) {
      return true;
    }
  }
  return false;
}

/**
 * Coerce `val`.
 *
 * @param {Mixed} val
 * @return {Mixed}
 * @api private
 */

function coerce(val) {
  if (val instanceof Error) return val.stack || val.message;
  return val;
}
});
var debug_1 = debug.coerce;
var debug_2 = debug.disable;
var debug_3 = debug.enable;
var debug_4 = debug.enabled;
var debug_5 = debug.humanize;
var debug_6 = debug.instances;
var debug_7 = debug.names;
var debug_8 = debug.skips;
var debug_9 = debug.formatters;

var browser = createCommonjsModule(function (module, exports) {
/**
 * This is the web browser implementation of `debug()`.
 *
 * Expose `debug()` as the module.
 */

exports = module.exports = debug;
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = 'undefined' != typeof chrome
               && 'undefined' != typeof chrome.storage
                  ? chrome.storage.local
                  : localstorage();

/**
 * Colors.
 */

exports.colors = [
  '#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC',
  '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF',
  '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC',
  '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF',
  '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC',
  '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033',
  '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366',
  '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933',
  '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC',
  '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF',
  '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33'
];

/**
 * Currently only WebKit-based Web Inspectors, Firefox >= v31,
 * and the Firebug extension (any Firefox version) are known
 * to support "%c" CSS customizations.
 *
 * TODO: add a `localStorage` variable to explicitly enable/disable colors
 */

function useColors() {
  // NB: In an Electron preload script, document will be defined but not fully
  // initialized. Since we know we're in Chrome, we'll just detect this case
  // explicitly
  if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') {
    return true;
  }

  // Internet Explorer and Edge do not support colors.
  if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
    return false;
  }

  // is webkit? http://stackoverflow.com/a/16459606/376773
  // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
    // is firebug? http://stackoverflow.com/a/398120/376773
    (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
    // is firefox >= v31?
    // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
    (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
    // double check webkit in userAgent just in case we are in a worker
    (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
}

/**
 * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
 */

exports.formatters.j = function(v) {
  try {
    return JSON.stringify(v);
  } catch (err) {
    return '[UnexpectedJSONParseError]: ' + err.message;
  }
};


/**
 * Colorize log arguments if enabled.
 *
 * @api public
 */

function formatArgs(args) {
  var useColors = this.useColors;

  args[0] = (useColors ? '%c' : '')
    + this.namespace
    + (useColors ? ' %c' : ' ')
    + args[0]
    + (useColors ? '%c ' : ' ')
    + '+' + exports.humanize(this.diff);

  if (!useColors) return;

  var c = 'color: ' + this.color;
  args.splice(1, 0, c, 'color: inherit');

  // the final "%c" is somewhat tricky, because there could be other
  // arguments passed either before or after the %c, so we need to
  // figure out the correct index to insert the CSS into
  var index = 0;
  var lastC = 0;
  args[0].replace(/%[a-zA-Z%]/g, function(match) {
    if ('%%' === match) return;
    index++;
    if ('%c' === match) {
      // we only are interested in the *last* %c
      // (the user may have provided their own)
      lastC = index;
    }
  });

  args.splice(lastC, 0, c);
}

/**
 * Invokes `console.log()` when available.
 * No-op when `console.log` is not a "function".
 *
 * @api public
 */

function log() {
  // this hackery is required for IE8/9, where
  // the `console.log` function doesn't have 'apply'
  return 'object' === typeof console
    && console.log
    && Function.prototype.apply.call(console.log, console, arguments);
}

/**
 * Save `namespaces`.
 *
 * @param {String} namespaces
 * @api private
 */

function save(namespaces) {
  try {
    if (null == namespaces) {
      exports.storage.removeItem('debug');
    } else {
      exports.storage.debug = namespaces;
    }
  } catch(e) {}
}

/**
 * Load `namespaces`.
 *
 * @return {String} returns the previously persisted debug modes
 * @api private
 */

function load() {
  var r;
  try {
    r = exports.storage.debug;
  } catch(e) {}

  // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  if (!r && typeof process !== 'undefined' && 'env' in process) {
    r = process.env.DEBUG;
  }

  return r;
}

/**
 * Enable namespaces listed in `localStorage.debug` initially.
 */

exports.enable(load());

/**
 * Localstorage attempts to return the localstorage.
 *
 * This is necessary because safari throws
 * when a user disables cookies/localstorage
 * and you attempt to access it.
 *
 * @return {LocalStorage}
 * @api private
 */

function localstorage() {
  try {
    return window.localStorage;
  } catch (e) {}
}
});
var browser_1 = browser.log;
var browser_2 = browser.formatArgs;
var browser_3 = browser.save;
var browser_4 = browser.load;
var browser_5 = browser.useColors;
var browser_6 = browser.storage;
var browser_7 = browser.colors;

var hasFlag = (flag, argv) => {
	argv = argv || process.argv;
	const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
	const pos = argv.indexOf(prefix + flag);
	const terminatorPos = argv.indexOf('--');
	return pos !== -1 && (terminatorPos === -1 ? true : pos < terminatorPos);
};

const {env} = process;

let forceColor;
if (hasFlag('no-color') ||
	hasFlag('no-colors') ||
	hasFlag('color=false') ||
	hasFlag('color=never')) {
	forceColor = 0;
} else if (hasFlag('color') ||
	hasFlag('colors') ||
	hasFlag('color=true') ||
	hasFlag('color=always')) {
	forceColor = 1;
}
if ('FORCE_COLOR' in env) {
	if (env.FORCE_COLOR === true || env.FORCE_COLOR === 'true') {
		forceColor = 1;
	} else if (env.FORCE_COLOR === false || env.FORCE_COLOR === 'false') {
		forceColor = 0;
	} else {
		forceColor = env.FORCE_COLOR.length === 0 ? 1 : Math.min(parseInt(env.FORCE_COLOR, 10), 3);
	}
}

function translateLevel(level) {
	if (level === 0) {
		return false;
	}

	return {
		level,
		hasBasic: true,
		has256: level >= 2,
		has16m: level >= 3
	};
}

function supportsColor(stream) {
	if (forceColor === 0) {
		return 0;
	}

	if (hasFlag('color=16m') ||
		hasFlag('color=full') ||
		hasFlag('color=truecolor')) {
		return 3;
	}

	if (hasFlag('color=256')) {
		return 2;
	}

	if (stream && !stream.isTTY && forceColor === undefined) {
		return 0;
	}

	const min = forceColor || 0;

	if (env.TERM === 'dumb') {
		return min;
	}

	if (process.platform === 'win32') {
		// Node.js 7.5.0 is the first version of Node.js to include a patch to
		// libuv that enables 256 color output on Windows. Anything earlier and it
		// won't work. However, here we target Node.js 8 at minimum as it is an LTS
		// release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows
		// release that supports 256 colors. Windows 10 build 14931 is the first release
		// that supports 16m/TrueColor.
		const osRelease = os.release().split('.');
		if (
			Number(process.versions.node.split('.')[0]) >= 8 &&
			Number(osRelease[0]) >= 10 &&
			Number(osRelease[2]) >= 10586
		) {
			return Number(osRelease[2]) >= 14931 ? 3 : 2;
		}

		return 1;
	}

	if ('CI' in env) {
		if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
			return 1;
		}

		return min;
	}

	if ('TEAMCITY_VERSION' in env) {
		return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
	}

	if (env.COLORTERM === 'truecolor') {
		return 3;
	}

	if ('TERM_PROGRAM' in env) {
		const version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);

		switch (env.TERM_PROGRAM) {
			case 'iTerm.app':
				return version >= 3 ? 3 : 2;
			case 'Apple_Terminal':
				return 2;
			// No default
		}
	}

	if (/-256(color)?$/i.test(env.TERM)) {
		return 2;
	}

	if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
		return 1;
	}

	if ('COLORTERM' in env) {
		return 1;
	}

	return min;
}

function getSupportLevel(stream) {
	const level = supportsColor(stream);
	return translateLevel(level);
}

var supportsColor_1 = {
	supportsColor: getSupportLevel,
	stdout: getSupportLevel(process.stdout),
	stderr: getSupportLevel(process.stderr)
};

var node = createCommonjsModule(function (module, exports) {
/**
 * Module dependencies.
 */




/**
 * This is the Node.js implementation of `debug()`.
 *
 * Expose `debug()` as the module.
 */

exports = module.exports = debug;
exports.init = init;
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;

/**
 * Colors.
 */

exports.colors = [ 6, 2, 3, 4, 5, 1 ];

try {
  var supportsColor = supportsColor_1;
  if (supportsColor && supportsColor.level >= 2) {
    exports.colors = [
      20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68,
      69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113, 128, 129, 134,
      135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171,
      172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200, 201, 202, 203, 204,
      205, 206, 207, 208, 209, 214, 215, 220, 221
    ];
  }
} catch (err) {
  // swallow - we only care if `supports-color` is available; it doesn't have to be.
}

/**
 * Build up the default `inspectOpts` object from the environment variables.
 *
 *   $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
 */

exports.inspectOpts = Object.keys(process.env).filter(function (key) {
  return /^debug_/i.test(key);
}).reduce(function (obj, key) {
  // camel-case
  var prop = key
    .substring(6)
    .toLowerCase()
    .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });

  // coerce string value into JS value
  var val = process.env[key];
  if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
  else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
  else if (val === 'null') val = null;
  else val = Number(val);

  obj[prop] = val;
  return obj;
}, {});

/**
 * Is stdout a TTY? Colored output is enabled when `true`.
 */

function useColors() {
  return 'colors' in exports.inspectOpts
    ? Boolean(exports.inspectOpts.colors)
    : tty.isatty(process.stderr.fd);
}

/**
 * Map %o to `util.inspect()`, all on a single line.
 */

exports.formatters.o = function(v) {
  this.inspectOpts.colors = this.useColors;
  return util.inspect(v, this.inspectOpts)
    .split('\n').map(function(str) {
      return str.trim()
    }).join(' ');
};

/**
 * Map %o to `util.inspect()`, allowing multiple lines if needed.
 */

exports.formatters.O = function(v) {
  this.inspectOpts.colors = this.useColors;
  return util.inspect(v, this.inspectOpts);
};

/**
 * Adds ANSI color escape codes if enabled.
 *
 * @api public
 */

function formatArgs(args) {
  var name = this.namespace;
  var useColors = this.useColors;

  if (useColors) {
    var c = this.color;
    var colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
    var prefix = '  ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';

    args[0] = prefix + args[0].split('\n').join('\n' + prefix);
    args.push(colorCode + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
  } else {
    args[0] = getDate() + name + ' ' + args[0];
  }
}

function getDate() {
  if (exports.inspectOpts.hideDate) {
    return '';
  } else {
    return new Date().toISOString() + ' ';
  }
}

/**
 * Invokes `util.format()` with the specified arguments and writes to stderr.
 */

function log() {
  return process.stderr.write(util.format.apply(util, arguments) + '\n');
}

/**
 * Save `namespaces`.
 *
 * @param {String} namespaces
 * @api private
 */

function save(namespaces) {
  if (null == namespaces) {
    // If you set a process.env field to null or undefined, it gets cast to the
    // string 'null' or 'undefined'. Just delete instead.
    delete process.env.DEBUG;
  } else {
    process.env.DEBUG = namespaces;
  }
}

/**
 * Load `namespaces`.
 *
 * @return {String} returns the previously persisted debug modes
 * @api private
 */

function load() {
  return process.env.DEBUG;
}

/**
 * Init logic for `debug` instances.
 *
 * Create a new `inspectOpts` object in case `useColors` is set
 * differently for a particular `debug` instance.
 */

function init (debug) {
  debug.inspectOpts = {};

  var keys = Object.keys(exports.inspectOpts);
  for (var i = 0; i < keys.length; i++) {
    debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  }
}

/**
 * Enable namespaces listed in `process.env.DEBUG` initially.
 */

exports.enable(load());
});
var node_1 = node.init;
var node_2 = node.log;
var node_3 = node.formatArgs;
var node_4 = node.save;
var node_5 = node.load;
var node_6 = node.useColors;
var node_7 = node.colors;
var node_8 = node.inspectOpts;

var src = createCommonjsModule(function (module) {
/**
 * Detect Electron renderer process, which is node, but we should
 * treat as a browser.
 */

if (typeof process === 'undefined' || process.type === 'renderer') {
  module.exports = browser;
} else {
  module.exports = node;
}
});

var Writable = stream.Writable;
var debug$1 = src("follow-redirects");

// RFC7231§4.2.1: Of the request methods defined by this specification,
// the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.
var SAFE_METHODS = { GET: true, HEAD: true, OPTIONS: true, TRACE: true };

// Create handlers that pass events from native requests
var eventHandlers = Object.create(null);
["abort", "aborted", "error", "socket", "timeout"].forEach(function (event) {
  eventHandlers[event] = function (arg) {
    this._redirectable.emit(event, arg);
  };
});

// An HTTP(S) request that can be redirected
function RedirectableRequest(options, responseCallback) {
  // Initialize the request
  Writable.call(this);
  options.headers = options.headers || {};
  this._options = options;
  this._redirectCount = 0;
  this._redirects = [];
  this._requestBodyLength = 0;
  this._requestBodyBuffers = [];

  // Since http.request treats host as an alias of hostname,
  // but the url module interprets host as hostname plus port,
  // eliminate the host property to avoid confusion.
  if (options.host) {
    // Use hostname if set, because it has precedence
    if (!options.hostname) {
      options.hostname = options.host;
    }
    delete options.host;
  }

  // Attach a callback if passed
  if (responseCallback) {
    this.on("response", responseCallback);
  }

  // React to responses of native requests
  var self = this;
  this._onNativeResponse = function (response) {
    self._processResponse(response);
  };

  // Complete the URL object when necessary
  if (!options.pathname && options.path) {
    var searchPos = options.path.indexOf("?");
    if (searchPos < 0) {
      options.pathname = options.path;
    }
    else {
      options.pathname = options.path.substring(0, searchPos);
      options.search = options.path.substring(searchPos);
    }
  }

  // Perform the first request
  this._performRequest();
}
RedirectableRequest.prototype = Object.create(Writable.prototype);

// Writes buffered data to the current native request
RedirectableRequest.prototype.write = function (data, encoding, callback) {
  // Validate input and shift parameters if necessary
  if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
    throw new Error("data should be a string, Buffer or Uint8Array");
  }
  if (typeof encoding === "function") {
    callback = encoding;
    encoding = null;
  }

  // Ignore empty buffers, since writing them doesn't invoke the callback
  // https://github.com/nodejs/node/issues/22066
  if (data.length === 0) {
    if (callback) {
      callback();
    }
    return;
  }
  // Only write when we don't exceed the maximum body length
  if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
    this._requestBodyLength += data.length;
    this._requestBodyBuffers.push({ data: data, encoding: encoding });
    this._currentRequest.write(data, encoding, callback);
  }
  // Error when we exceed the maximum body length
  else {
    this.emit("error", new Error("Request body larger than maxBodyLength limit"));
    this.abort();
  }
};

// Ends the current native request
RedirectableRequest.prototype.end = function (data, encoding, callback) {
  // Shift parameters if necessary
  if (typeof data === "function") {
    callback = data;
    data = encoding = null;
  }
  else if (typeof encoding === "function") {
    callback = encoding;
    encoding = null;
  }

  // Write data and end
  var currentRequest = this._currentRequest;
  this.write(data || "", encoding, function () {
    currentRequest.end(null, null, callback);
  });
};

// Sets a header value on the current native request
RedirectableRequest.prototype.setHeader = function (name, value) {
  this._options.headers[name] = value;
  this._currentRequest.setHeader(name, value);
};

// Clears a header value on the current native request
RedirectableRequest.prototype.removeHeader = function (name) {
  delete this._options.headers[name];
  this._currentRequest.removeHeader(name);
};

// Proxy all other public ClientRequest methods
[
  "abort", "flushHeaders", "getHeader",
  "setNoDelay", "setSocketKeepAlive", "setTimeout",
].forEach(function (method) {
  RedirectableRequest.prototype[method] = function (a, b) {
    return this._currentRequest[method](a, b);
  };
});

// Proxy all public ClientRequest properties
["aborted", "connection", "socket"].forEach(function (property) {
  Object.defineProperty(RedirectableRequest.prototype, property, {
    get: function () { return this._currentRequest[property]; },
  });
});

// Executes the next native request (initial or redirect)
RedirectableRequest.prototype._performRequest = function () {
  // Load the native protocol
  var protocol = this._options.protocol;
  var nativeProtocol = this._options.nativeProtocols[protocol];
  if (!nativeProtocol) {
    this.emit("error", new Error("Unsupported protocol " + protocol));
    return;
  }

  // If specified, use the agent corresponding to the protocol
  // (HTTP and HTTPS use different types of agents)
  if (this._options.agents) {
    var scheme = protocol.substr(0, protocol.length - 1);
    this._options.agent = this._options.agents[scheme];
  }

  // Create the native request
  var request = this._currentRequest =
        nativeProtocol.request(this._options, this._onNativeResponse);
  this._currentUrl = url.format(this._options);

  // Set up event handlers
  request._redirectable = this;
  for (var event in eventHandlers) {
    /* istanbul ignore else */
    if (event) {
      request.on(event, eventHandlers[event]);
    }
  }

  // End a redirected request
  // (The first request must be ended explicitly with RedirectableRequest#end)
  if (this._isRedirect) {
    // Write the request entity and end.
    var i = 0;
    var buffers = this._requestBodyBuffers;
    (function writeNext() {
      if (i < buffers.length) {
        var buffer = buffers[i++];
        request.write(buffer.data, buffer.encoding, writeNext);
      }
      else {
        request.end();
      }
    }());
  }
};

// Processes a response from the current native request
RedirectableRequest.prototype._processResponse = function (response) {
  // Store the redirected response
  if (this._options.trackRedirects) {
    this._redirects.push({
      url: this._currentUrl,
      headers: response.headers,
      statusCode: response.statusCode,
    });
  }

  // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  // that further action needs to be taken by the user agent in order to
  // fulfill the request. If a Location header field is provided,
  // the user agent MAY automatically redirect its request to the URI
  // referenced by the Location field value,
  // even if the specific status code is not understood.
  var location = response.headers.location;
  if (location && this._options.followRedirects !== false &&
      response.statusCode >= 300 && response.statusCode < 400) {
    // RFC7231§6.4: A client SHOULD detect and intervene
    // in cyclical redirections (i.e., "infinite" redirection loops).
    if (++this._redirectCount > this._options.maxRedirects) {
      this.emit("error", new Error("Max redirects exceeded."));
      return;
    }

    // RFC7231§6.4: Automatic redirection needs to done with
    // care for methods not known to be safe […],
    // since the user might not wish to redirect an unsafe request.
    // RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates
    // that the target resource resides temporarily under a different URI
    // and the user agent MUST NOT change the request method
    // if it performs an automatic redirection to that URI.
    var header;
    var headers = this._options.headers;
    if (response.statusCode !== 307 && !(this._options.method in SAFE_METHODS)) {
      this._options.method = "GET";
      // Drop a possible entity and headers related to it
      this._requestBodyBuffers = [];
      for (header in headers) {
        if (/^content-/i.test(header)) {
          delete headers[header];
        }
      }
    }

    // Drop the Host header, as the redirect might lead to a different host
    if (!this._isRedirect) {
      for (header in headers) {
        if (/^host$/i.test(header)) {
          delete headers[header];
        }
      }
    }

    // Perform the redirected request
    var redirectUrl = url.resolve(this._currentUrl, location);
    debug$1("redirecting to", redirectUrl);
    Object.assign(this._options, url.parse(redirectUrl));
    this._isRedirect = true;
    this._performRequest();

    // Discard the remainder of the response to avoid waiting for data
    response.destroy();
  }
  else {
    // The response is not a redirect; return it as-is
    response.responseUrl = this._currentUrl;
    response.redirects = this._redirects;
    this.emit("response", response);

    // Clean up
    this._requestBodyBuffers = [];
  }
};

// Wraps the key/value object of protocols with redirect functionality
function wrap(protocols) {
  // Default settings
  var exports = {
    maxRedirects: 21,
    maxBodyLength: 10 * 1024 * 1024,
  };

  // Wrap each protocol
  var nativeProtocols = {};
  Object.keys(protocols).forEach(function (scheme) {
    var protocol = scheme + ":";
    var nativeProtocol = nativeProtocols[protocol] = protocols[scheme];
    var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);

    // Executes a request, following redirects
    wrappedProtocol.request = function (options, callback) {
      if (typeof options === "string") {
        options = url.parse(options);
        options.maxRedirects = exports.maxRedirects;
      }
      else {
        options = Object.assign({
          protocol: protocol,
          maxRedirects: exports.maxRedirects,
          maxBodyLength: exports.maxBodyLength,
        }, options);
      }
      options.nativeProtocols = nativeProtocols;
      assert.equal(options.protocol, protocol, "protocol mismatch");
      debug$1("options", options);
      return new RedirectableRequest(options, callback);
    };

    // Executes a GET request, following redirects
    wrappedProtocol.get = function (options, callback) {
      var request = wrappedProtocol.request(options, callback);
      request.end();
      return request;
    };
  });
  return exports;
}

// Exports
var followRedirects = wrap({ http: http, https: https });
var wrap_1 = wrap;
followRedirects.wrap = wrap_1;

var name = "axios";
var version = "0.19.0";
var description = "Promise based HTTP client for the browser and node.js";
var main = "index.js";
var scripts = {
	test: "grunt test && bundlesize",
	start: "node ./sandbox/server.js",
	build: "NODE_ENV=production grunt build",
	preversion: "npm test",
	version: "npm run build && grunt version && git add -A dist && git add CHANGELOG.md bower.json package.json",
	postversion: "git push && git push --tags",
	examples: "node ./examples/server.js",
	coveralls: "cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
	fix: "eslint --fix lib/**/*.js"
};
var repository = {
	type: "git",
	url: "https://github.com/axios/axios.git"
};
var keywords = [
	"xhr",
	"http",
	"ajax",
	"promise",
	"node"
];
var author = "Matt Zabriskie";
var license = "MIT";
var bugs = {
	url: "https://github.com/axios/axios/issues"
};
var homepage = "https://github.com/axios/axios";
var devDependencies = {
	bundlesize: "^0.17.0",
	coveralls: "^3.0.0",
	"es6-promise": "^4.2.4",
	grunt: "^1.0.2",
	"grunt-banner": "^0.6.0",
	"grunt-cli": "^1.2.0",
	"grunt-contrib-clean": "^1.1.0",
	"grunt-contrib-watch": "^1.0.0",
	"grunt-eslint": "^20.1.0",
	"grunt-karma": "^2.0.0",
	"grunt-mocha-test": "^0.13.3",
	"grunt-ts": "^6.0.0-beta.19",
	"grunt-webpack": "^1.0.18",
	"istanbul-instrumenter-loader": "^1.0.0",
	"jasmine-core": "^2.4.1",
	karma: "^1.3.0",
	"karma-chrome-launcher": "^2.2.0",
	"karma-coverage": "^1.1.1",
	"karma-firefox-launcher": "^1.1.0",
	"karma-jasmine": "^1.1.1",
	"karma-jasmine-ajax": "^0.1.13",
	"karma-opera-launcher": "^1.0.0",
	"karma-safari-launcher": "^1.0.0",
	"karma-sauce-launcher": "^1.2.0",
	"karma-sinon": "^1.0.5",
	"karma-sourcemap-loader": "^0.3.7",
	"karma-webpack": "^1.7.0",
	"load-grunt-tasks": "^3.5.2",
	minimist: "^1.2.0",
	mocha: "^5.2.0",
	sinon: "^4.5.0",
	typescript: "^2.8.1",
	"url-search-params": "^0.10.0",
	webpack: "^1.13.1",
	"webpack-dev-server": "^1.14.1"
};
var browser$1 = {
	"./lib/adapters/http.js": "./lib/adapters/xhr.js"
};
var typings = "./index.d.ts";
var dependencies = {
	"follow-redirects": "1.5.10",
	"is-buffer": "^2.0.2"
};
var bundlesize = [
	{
		path: "./dist/axios.min.js",
		threshold: "5kB"
	}
];
var _package = {
	name: name,
	version: version,
	description: description,
	main: main,
	scripts: scripts,
	repository: repository,
	keywords: keywords,
	author: author,
	license: license,
	bugs: bugs,
	homepage: homepage,
	devDependencies: devDependencies,
	browser: browser$1,
	typings: typings,
	dependencies: dependencies,
	bundlesize: bundlesize
};

var _package$1 = /*#__PURE__*/Object.freeze({
	__proto__: null,
	name: name,
	version: version,
	description: description,
	main: main,
	scripts: scripts,
	repository: repository,
	keywords: keywords,
	author: author,
	license: license,
	bugs: bugs,
	homepage: homepage,
	devDependencies: devDependencies,
	browser: browser$1,
	typings: typings,
	dependencies: dependencies,
	bundlesize: bundlesize,
	'default': _package
});

var pkg = getCjsExportFromNamespace(_package$1);

var httpFollow = followRedirects.http;
var httpsFollow = followRedirects.https;






var isHttps = /https:?/;

/*eslint consistent-return:0*/
var http_1 = function httpAdapter(config) {
  return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
    var timer;
    var resolve = function resolve(value) {
      clearTimeout(timer);
      resolvePromise(value);
    };
    var reject = function reject(value) {
      clearTimeout(timer);
      rejectPromise(value);
    };
    var data = config.data;
    var headers = config.headers;

    // Set User-Agent (required by some servers)
    // Only set header if it hasn't been set in config
    // See https://github.com/axios/axios/issues/69
    if (!headers['User-Agent'] && !headers['user-agent']) {
      headers['User-Agent'] = 'axios/' + pkg.version;
    }

    if (data && !utils.isStream(data)) {
      if (Buffer.isBuffer(data)) ; else if (utils.isArrayBuffer(data)) {
        data = Buffer.from(new Uint8Array(data));
      } else if (utils.isString(data)) {
        data = Buffer.from(data, 'utf-8');
      } else {
        return reject(createError(
          'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
          config
        ));
      }

      // Add Content-Length header if data exists
      headers['Content-Length'] = data.length;
    }

    // HTTP basic authentication
    var auth = undefined;
    if (config.auth) {
      var username = config.auth.username || '';
      var password = config.auth.password || '';
      auth = username + ':' + password;
    }

    // Parse url
    var parsed = url.parse(config.url);
    var protocol = parsed.protocol || 'http:';

    if (!auth && parsed.auth) {
      var urlAuth = parsed.auth.split(':');
      var urlUsername = urlAuth[0] || '';
      var urlPassword = urlAuth[1] || '';
      auth = urlUsername + ':' + urlPassword;
    }

    if (auth) {
      delete headers.Authorization;
    }

    var isHttpsRequest = isHttps.test(protocol);
    var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;

    var options = {
      path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
      method: config.method.toUpperCase(),
      headers: headers,
      agent: agent,
      auth: auth
    };

    if (config.socketPath) {
      options.socketPath = config.socketPath;
    } else {
      options.hostname = parsed.hostname;
      options.port = parsed.port;
    }

    var proxy = config.proxy;
    if (!proxy && proxy !== false) {
      var proxyEnv = protocol.slice(0, -1) + '_proxy';
      var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
      if (proxyUrl) {
        var parsedProxyUrl = url.parse(proxyUrl);
        var noProxyEnv = process.env.no_proxy || process.env.NO_PROXY;
        var shouldProxy = true;

        if (noProxyEnv) {
          var noProxy = noProxyEnv.split(',').map(function trim(s) {
            return s.trim();
          });

          shouldProxy = !noProxy.some(function proxyMatch(proxyElement) {
            if (!proxyElement) {
              return false;
            }
            if (proxyElement === '*') {
              return true;
            }
            if (proxyElement[0] === '.' &&
                parsed.hostname.substr(parsed.hostname.length - proxyElement.length) === proxyElement &&
                proxyElement.match(/\./g).length === parsed.hostname.match(/\./g).length) {
              return true;
            }

            return parsed.hostname === proxyElement;
          });
        }


        if (shouldProxy) {
          proxy = {
            host: parsedProxyUrl.hostname,
            port: parsedProxyUrl.port
          };

          if (parsedProxyUrl.auth) {
            var proxyUrlAuth = parsedProxyUrl.auth.split(':');
            proxy.auth = {
              username: proxyUrlAuth[0],
              password: proxyUrlAuth[1]
            };
          }
        }
      }
    }

    if (proxy) {
      options.hostname = proxy.host;
      options.host = proxy.host;
      options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
      options.port = proxy.port;
      options.path = protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;

      // Basic proxy authorization
      if (proxy.auth) {
        var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
        options.headers['Proxy-Authorization'] = 'Basic ' + base64;
      }
    }

    var transport;
    var isHttpsProxy = isHttpsRequest && (proxy ? isHttps.test(proxy.protocol) : true);
    if (config.transport) {
      transport = config.transport;
    } else if (config.maxRedirects === 0) {
      transport = isHttpsProxy ? https : http;
    } else {
      if (config.maxRedirects) {
        options.maxRedirects = config.maxRedirects;
      }
      transport = isHttpsProxy ? httpsFollow : httpFollow;
    }

    if (config.maxContentLength && config.maxContentLength > -1) {
      options.maxBodyLength = config.maxContentLength;
    }

    // Create the request
    var req = transport.request(options, function handleResponse(res) {
      if (req.aborted) return;

      // uncompress the response body transparently if required
      var stream = res;
      switch (res.headers['content-encoding']) {
      /*eslint default-case:0*/
      case 'gzip':
      case 'compress':
      case 'deflate':
        // add the unzipper to the body stream processing pipeline
        stream = (res.statusCode === 204) ? stream : stream.pipe(zlib.createUnzip());

        // remove the content-encoding in order to not confuse downstream operations
        delete res.headers['content-encoding'];
        break;
      }

      // return the last request in case of redirects
      var lastRequest = res.req || req;

      var response = {
        status: res.statusCode,
        statusText: res.statusMessage,
        headers: res.headers,
        config: config,
        request: lastRequest
      };

      if (config.responseType === 'stream') {
        response.data = stream;
        settle(resolve, reject, response);
      } else {
        var responseBuffer = [];
        stream.on('data', function handleStreamData(chunk) {
          responseBuffer.push(chunk);

          // make sure the content length is not over the maxContentLength if specified
          if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
            stream.destroy();
            reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
              config, null, lastRequest));
          }
        });

        stream.on('error', function handleStreamError(err) {
          if (req.aborted) return;
          reject(enhanceError(err, config, null, lastRequest));
        });

        stream.on('end', function handleStreamEnd() {
          var responseData = Buffer.concat(responseBuffer);
          if (config.responseType !== 'arraybuffer') {
            responseData = responseData.toString(config.responseEncoding);
          }

          response.data = responseData;
          settle(resolve, reject, response);
        });
      }
    });

    // Handle errors
    req.on('error', function handleRequestError(err) {
      if (req.aborted) return;
      reject(enhanceError(err, config, null, req));
    });

    // Handle request timeout
    if (config.timeout) {
      timer = setTimeout(function handleRequestTimeout() {
        req.abort();
        reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
      }, config.timeout);
    }

    if (config.cancelToken) {
      // Handle cancellation
      config.cancelToken.promise.then(function onCanceled(cancel) {
        if (req.aborted) return;

        req.abort();
        reject(cancel);
      });
    }

    // Send the request
    if (utils.isStream(data)) {
      data.on('error', function handleStreamError(err) {
        reject(enhanceError(err, config, null, req));
      }).pipe(req);
    } else {
      req.end(data);
    }
  });
};

// Headers whose duplicates are ignored by node
// c.f. https://nodejs.org/api/http.html#http_message_headers
var ignoreDuplicateOf = [
  'age', 'authorization', 'content-length', 'content-type', 'etag',
  'expires', 'from', 'host', 'if-modified-since', 'if-unmodified-since',
  'last-modified', 'location', 'max-forwards', 'proxy-authorization',
  'referer', 'retry-after', 'user-agent'
];

/**
 * Parse headers into an object
 *
 * ```
 * Date: Wed, 27 Aug 2014 08:58:49 GMT
 * Content-Type: application/json
 * Connection: keep-alive
 * Transfer-Encoding: chunked
 * ```
 *
 * @param {String} headers Headers needing to be parsed
 * @returns {Object} Headers parsed into an object
 */
var parseHeaders = function parseHeaders(headers) {
  var parsed = {};
  var key;
  var val;
  var i;

  if (!headers) { return parsed; }

  utils.forEach(headers.split('\n'), function parser(line) {
    i = line.indexOf(':');
    key = utils.trim(line.substr(0, i)).toLowerCase();
    val = utils.trim(line.substr(i + 1));

    if (key) {
      if (parsed[key] && ignoreDuplicateOf.indexOf(key) >= 0) {
        return;
      }
      if (key === 'set-cookie') {
        parsed[key] = (parsed[key] ? parsed[key] : []).concat([val]);
      } else {
        parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
      }
    }
  });

  return parsed;
};

var isURLSameOrigin = (
  utils.isStandardBrowserEnv() ?

  // Standard browser envs have full support of the APIs needed to test
  // whether the request URL is of the same origin as current location.
    (function standardBrowserEnv() {
      var msie = /(msie|trident)/i.test(navigator.userAgent);
      var urlParsingNode = document.createElement('a');
      var originURL;

      /**
    * Parse a URL to discover it's components
    *
    * @param {String} url The URL to be parsed
    * @returns {Object}
    */
      function resolveURL(url) {
        var href = url;

        if (msie) {
        // IE needs attribute set twice to normalize properties
          urlParsingNode.setAttribute('href', href);
          href = urlParsingNode.href;
        }

        urlParsingNode.setAttribute('href', href);

        // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
        return {
          href: urlParsingNode.href,
          protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
          host: urlParsingNode.host,
          search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
          hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
          hostname: urlParsingNode.hostname,
          port: urlParsingNode.port,
          pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
            urlParsingNode.pathname :
            '/' + urlParsingNode.pathname
        };
      }

      originURL = resolveURL(window.location.href);

      /**
    * Determine if a URL shares the same origin as the current location
    *
    * @param {String} requestURL The URL to test
    * @returns {boolean} True if URL shares the same origin, otherwise false
    */
      return function isURLSameOrigin(requestURL) {
        var parsed = (utils.isString(requestURL)) ? resolveURL(requestURL) : requestURL;
        return (parsed.protocol === originURL.protocol &&
            parsed.host === originURL.host);
      };
    })() :

  // Non standard browser envs (web workers, react-native) lack needed support.
    (function nonStandardBrowserEnv() {
      return function isURLSameOrigin() {
        return true;
      };
    })()
);

var cookies = (
  utils.isStandardBrowserEnv() ?

  // Standard browser envs support document.cookie
    (function standardBrowserEnv() {
      return {
        write: function write(name, value, expires, path, domain, secure) {
          var cookie = [];
          cookie.push(name + '=' + encodeURIComponent(value));

          if (utils.isNumber(expires)) {
            cookie.push('expires=' + new Date(expires).toGMTString());
          }

          if (utils.isString(path)) {
            cookie.push('path=' + path);
          }

          if (utils.isString(domain)) {
            cookie.push('domain=' + domain);
          }

          if (secure === true) {
            cookie.push('secure');
          }

          document.cookie = cookie.join('; ');
        },

        read: function read(name) {
          var match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
          return (match ? decodeURIComponent(match[3]) : null);
        },

        remove: function remove(name) {
          this.write(name, '', Date.now() - 86400000);
        }
      };
    })() :

  // Non standard browser env (web workers, react-native) lack needed support.
    (function nonStandardBrowserEnv() {
      return {
        write: function write() {},
        read: function read() { return null; },
        remove: function remove() {}
      };
    })()
);

var xhr = function xhrAdapter(config) {
  return new Promise(function dispatchXhrRequest(resolve, reject) {
    var requestData = config.data;
    var requestHeaders = config.headers;

    if (utils.isFormData(requestData)) {
      delete requestHeaders['Content-Type']; // Let the browser set it
    }

    var request = new XMLHttpRequest();

    // HTTP basic authentication
    if (config.auth) {
      var username = config.auth.username || '';
      var password = config.auth.password || '';
      requestHeaders.Authorization = 'Basic ' + btoa(username + ':' + password);
    }

    request.open(config.method.toUpperCase(), buildURL(config.url, config.params, config.paramsSerializer), true);

    // Set the request timeout in MS
    request.timeout = config.timeout;

    // Listen for ready state
    request.onreadystatechange = function handleLoad() {
      if (!request || request.readyState !== 4) {
        return;
      }

      // The request errored out and we didn't get a response, this will be
      // handled by onerror instead
      // With one exception: request that using file: protocol, most browsers
      // will return status as 0 even though it's a successful request
      if (request.status === 0 && !(request.responseURL && request.responseURL.indexOf('file:') === 0)) {
        return;
      }

      // Prepare the response
      var responseHeaders = 'getAllResponseHeaders' in request ? parseHeaders(request.getAllResponseHeaders()) : null;
      var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response;
      var response = {
        data: responseData,
        status: request.status,
        statusText: request.statusText,
        headers: responseHeaders,
        config: config,
        request: request
      };

      settle(resolve, reject, response);

      // Clean up request
      request = null;
    };

    // Handle browser request cancellation (as opposed to a manual cancellation)
    request.onabort = function handleAbort() {
      if (!request) {
        return;
      }

      reject(createError('Request aborted', config, 'ECONNABORTED', request));

      // Clean up request
      request = null;
    };

    // Handle low level network errors
    request.onerror = function handleError() {
      // Real errors are hidden from us by the browser
      // onerror should only fire if it's a network error
      reject(createError('Network Error', config, null, request));

      // Clean up request
      request = null;
    };

    // Handle timeout
    request.ontimeout = function handleTimeout() {
      reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED',
        request));

      // Clean up request
      request = null;
    };

    // Add xsrf header
    // This is only done if running in a standard browser environment.
    // Specifically not if we're in a web worker, or react-native.
    if (utils.isStandardBrowserEnv()) {
      var cookies$1 = cookies;

      // Add xsrf header
      var xsrfValue = (config.withCredentials || isURLSameOrigin(config.url)) && config.xsrfCookieName ?
        cookies$1.read(config.xsrfCookieName) :
        undefined;

      if (xsrfValue) {
        requestHeaders[config.xsrfHeaderName] = xsrfValue;
      }
    }

    // Add headers to the request
    if ('setRequestHeader' in request) {
      utils.forEach(requestHeaders, function setRequestHeader(val, key) {
        if (typeof requestData === 'undefined' && key.toLowerCase() === 'content-type') {
          // Remove Content-Type if data is undefined
          delete requestHeaders[key];
        } else {
          // Otherwise add header to the request
          request.setRequestHeader(key, val);
        }
      });
    }

    // Add withCredentials to request if needed
    if (config.withCredentials) {
      request.withCredentials = true;
    }

    // Add responseType to request if needed
    if (config.responseType) {
      try {
        request.responseType = config.responseType;
      } catch (e) {
        // Expected DOMException thrown by browsers not compatible XMLHttpRequest Level 2.
        // But, this can be suppressed for 'json' type as it can be parsed by default 'transformResponse' function.
        if (config.responseType !== 'json') {
          throw e;
        }
      }
    }

    // Handle progress if needed
    if (typeof config.onDownloadProgress === 'function') {
      request.addEventListener('progress', config.onDownloadProgress);
    }

    // Not all browsers support upload events
    if (typeof config.onUploadProgress === 'function' && request.upload) {
      request.upload.addEventListener('progress', config.onUploadProgress);
    }

    if (config.cancelToken) {
      // Handle cancellation
      config.cancelToken.promise.then(function onCanceled(cancel) {
        if (!request) {
          return;
        }

        request.abort();
        reject(cancel);
        // Clean up request
        request = null;
      });
    }

    if (requestData === undefined) {
      requestData = null;
    }

    // Send the request
    request.send(requestData);
  });
};

var DEFAULT_CONTENT_TYPE = {
  'Content-Type': 'application/x-www-form-urlencoded'
};

function setContentTypeIfUnset(headers, value) {
  if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
    headers['Content-Type'] = value;
  }
}

function getDefaultAdapter() {
  var adapter;
  // Only Node.JS has a process variable that is of [[Class]] process
  if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
    // For node use HTTP adapter
    adapter = http_1;
  } else if (typeof XMLHttpRequest !== 'undefined') {
    // For browsers use XHR adapter
    adapter = xhr;
  }
  return adapter;
}

var defaults = {
  adapter: getDefaultAdapter(),

  transformRequest: [function transformRequest(data, headers) {
    normalizeHeaderName(headers, 'Accept');
    normalizeHeaderName(headers, 'Content-Type');
    if (utils.isFormData(data) ||
      utils.isArrayBuffer(data) ||
      utils.isBuffer(data) ||
      utils.isStream(data) ||
      utils.isFile(data) ||
      utils.isBlob(data)
    ) {
      return data;
    }
    if (utils.isArrayBufferView(data)) {
      return data.buffer;
    }
    if (utils.isURLSearchParams(data)) {
      setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
      return data.toString();
    }
    if (utils.isObject(data)) {
      setContentTypeIfUnset(headers, 'application/json;charset=utf-8');
      return JSON.stringify(data);
    }
    return data;
  }],

  transformResponse: [function transformResponse(data) {
    /*eslint no-param-reassign:0*/
    if (typeof data === 'string') {
      try {
        data = JSON.parse(data);
      } catch (e) { /* Ignore */ }
    }
    return data;
  }],

  /**
   * A timeout in milliseconds to abort a request. If set to 0 (default) a
   * timeout is not created.
   */
  timeout: 0,

  xsrfCookieName: 'XSRF-TOKEN',
  xsrfHeaderName: 'X-XSRF-TOKEN',

  maxContentLength: -1,

  validateStatus: function validateStatus(status) {
    return status >= 200 && status < 300;
  }
};

defaults.headers = {
  common: {
    'Accept': 'application/json, text/plain, */*'
  }
};

utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
  defaults.headers[method] = {};
});

utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
});

var defaults_1 = defaults;

/**
 * Determines whether the specified URL is absolute
 *
 * @param {string} url The URL to test
 * @returns {boolean} True if the specified URL is absolute, otherwise false
 */
var isAbsoluteURL = function isAbsoluteURL(url) {
  // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
  // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
  // by any combination of letters, digits, plus, period, or hyphen.
  return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
};

/**
 * Creates a new URL by combining the specified URLs
 *
 * @param {string} baseURL The base URL
 * @param {string} relativeURL The relative URL
 * @returns {string} The combined URL
 */
var combineURLs = function combineURLs(baseURL, relativeURL) {
  return relativeURL
    ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
    : baseURL;
};

/**
 * Throws a `Cancel` if cancellation has been requested.
 */
function throwIfCancellationRequested(config) {
  if (config.cancelToken) {
    config.cancelToken.throwIfRequested();
  }
}

/**
 * Dispatch a request to the server using the configured adapter.
 *
 * @param {object} config The config that is to be used for the request
 * @returns {Promise} The Promise to be fulfilled
 */
var dispatchRequest = function dispatchRequest(config) {
  throwIfCancellationRequested(config);

  // Support baseURL config
  if (config.baseURL && !isAbsoluteURL(config.url)) {
    config.url = combineURLs(config.baseURL, config.url);
  }

  // Ensure headers exist
  config.headers = config.headers || {};

  // Transform request data
  config.data = transformData(
    config.data,
    config.headers,
    config.transformRequest
  );

  // Flatten headers
  config.headers = utils.merge(
    config.headers.common || {},
    config.headers[config.method] || {},
    config.headers || {}
  );

  utils.forEach(
    ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
    function cleanHeaderConfig(method) {
      delete config.headers[method];
    }
  );

  var adapter = config.adapter || defaults_1.adapter;

  return adapter(config).then(function onAdapterResolution(response) {
    throwIfCancellationRequested(config);

    // Transform response data
    response.data = transformData(
      response.data,
      response.headers,
      config.transformResponse
    );

    return response;
  }, function onAdapterRejection(reason) {
    if (!isCancel(reason)) {
      throwIfCancellationRequested(config);

      // Transform response data
      if (reason && reason.response) {
        reason.response.data = transformData(
          reason.response.data,
          reason.response.headers,
          config.transformResponse
        );
      }
    }

    return Promise.reject(reason);
  });
};

/**
 * Config-specific merge-function which creates a new config-object
 * by merging two configuration objects together.
 *
 * @param {Object} config1
 * @param {Object} config2
 * @returns {Object} New object resulting from merging config2 to config1
 */
var mergeConfig = function mergeConfig(config1, config2) {
  // eslint-disable-next-line no-param-reassign
  config2 = config2 || {};
  var config = {};

  utils.forEach(['url', 'method', 'params', 'data'], function valueFromConfig2(prop) {
    if (typeof config2[prop] !== 'undefined') {
      config[prop] = config2[prop];
    }
  });

  utils.forEach(['headers', 'auth', 'proxy'], function mergeDeepProperties(prop) {
    if (utils.isObject(config2[prop])) {
      config[prop] = utils.deepMerge(config1[prop], config2[prop]);
    } else if (typeof config2[prop] !== 'undefined') {
      config[prop] = config2[prop];
    } else if (utils.isObject(config1[prop])) {
      config[prop] = utils.deepMerge(config1[prop]);
    } else if (typeof config1[prop] !== 'undefined') {
      config[prop] = config1[prop];
    }
  });

  utils.forEach([
    'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
    'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
    'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'maxContentLength',
    'validateStatus', 'maxRedirects', 'httpAgent', 'httpsAgent', 'cancelToken',
    'socketPath'
  ], function defaultToConfig2(prop) {
    if (typeof config2[prop] !== 'undefined') {
      config[prop] = config2[prop];
    } else if (typeof config1[prop] !== 'undefined') {
      config[prop] = config1[prop];
    }
  });

  return config;
};

/**
 * Create a new instance of Axios
 *
 * @param {Object} instanceConfig The default config for the instance
 */
function Axios(instanceConfig) {
  this.defaults = instanceConfig;
  this.interceptors = {
    request: new InterceptorManager_1(),
    response: new InterceptorManager_1()
  };
}

/**
 * Dispatch a request
 *
 * @param {Object} config The config specific for this request (merged with this.defaults)
 */
Axios.prototype.request = function request(config) {
  /*eslint no-param-reassign:0*/
  // Allow for axios('example/url'[, config]) a la fetch API
  if (typeof config === 'string') {
    config = arguments[1] || {};
    config.url = arguments[0];
  } else {
    config = config || {};
  }

  config = mergeConfig(this.defaults, config);
  config.method = config.method ? config.method.toLowerCase() : 'get';

  // Hook up interceptors middleware
  var chain = [dispatchRequest, undefined];
  var promise = Promise.resolve(config);

  this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
    chain.unshift(interceptor.fulfilled, interceptor.rejected);
  });

  this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
    chain.push(interceptor.fulfilled, interceptor.rejected);
  });

  while (chain.length) {
    promise = promise.then(chain.shift(), chain.shift());
  }

  return promise;
};

Axios.prototype.getUri = function getUri(config) {
  config = mergeConfig(this.defaults, config);
  return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
};

// Provide aliases for supported request methods
utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
  /*eslint func-names:0*/
  Axios.prototype[method] = function(url, config) {
    return this.request(utils.merge(config || {}, {
      method: method,
      url: url
    }));
  };
});

utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  /*eslint func-names:0*/
  Axios.prototype[method] = function(url, data, config) {
    return this.request(utils.merge(config || {}, {
      method: method,
      url: url,
      data: data
    }));
  };
});

var Axios_1 = Axios;

/**
 * A `Cancel` is an object that is thrown when an operation is canceled.
 *
 * @class
 * @param {string=} message The message.
 */
function Cancel(message) {
  this.message = message;
}

Cancel.prototype.toString = function toString() {
  return 'Cancel' + (this.message ? ': ' + this.message : '');
};

Cancel.prototype.__CANCEL__ = true;

var Cancel_1 = Cancel;

/**
 * A `CancelToken` is an object that can be used to request cancellation of an operation.
 *
 * @class
 * @param {Function} executor The executor function.
 */
function CancelToken(executor) {
  if (typeof executor !== 'function') {
    throw new TypeError('executor must be a function.');
  }

  var resolvePromise;
  this.promise = new Promise(function promiseExecutor(resolve) {
    resolvePromise = resolve;
  });

  var token = this;
  executor(function cancel(message) {
    if (token.reason) {
      // Cancellation has already been requested
      return;
    }

    token.reason = new Cancel_1(message);
    resolvePromise(token.reason);
  });
}

/**
 * Throws a `Cancel` if cancellation has been requested.
 */
CancelToken.prototype.throwIfRequested = function throwIfRequested() {
  if (this.reason) {
    throw this.reason;
  }
};

/**
 * Returns an object that contains a new `CancelToken` and a function that, when called,
 * cancels the `CancelToken`.
 */
CancelToken.source = function source() {
  var cancel;
  var token = new CancelToken(function executor(c) {
    cancel = c;
  });
  return {
    token: token,
    cancel: cancel
  };
};

var CancelToken_1 = CancelToken;

/**
 * Syntactic sugar for invoking a function and expanding an array for arguments.
 *
 * Common use case would be to use `Function.prototype.apply`.
 *
 *  ```js
 *  function f(x, y, z) {}
 *  var args = [1, 2, 3];
 *  f.apply(null, args);
 *  ```
 *
 * With `spread` this example can be re-written.
 *
 *  ```js
 *  spread(function(x, y, z) {})([1, 2, 3]);
 *  ```
 *
 * @param {Function} callback
 * @returns {Function}
 */
var spread = function spread(callback) {
  return function wrap(arr) {
    return callback.apply(null, arr);
  };
};

/**
 * Create an instance of Axios
 *
 * @param {Object} defaultConfig The default config for the instance
 * @return {Axios} A new instance of Axios
 */
function createInstance(defaultConfig) {
  var context = new Axios_1(defaultConfig);
  var instance = bind(Axios_1.prototype.request, context);

  // Copy axios.prototype to instance
  utils.extend(instance, Axios_1.prototype, context);

  // Copy context to instance
  utils.extend(instance, context);

  return instance;
}

// Create the default instance to be exported
var axios = createInstance(defaults_1);

// Expose Axios class to allow class inheritance
axios.Axios = Axios_1;

// Factory for creating new instances
axios.create = function create(instanceConfig) {
  return createInstance(mergeConfig(axios.defaults, instanceConfig));
};

// Expose Cancel & CancelToken
axios.Cancel = Cancel_1;
axios.CancelToken = CancelToken_1;
axios.isCancel = isCancel;

// Expose all/spread
axios.all = function all(promises) {
  return Promise.all(promises);
};
axios.spread = spread;

var axios_1 = axios;

// Allow use of default import syntax in TypeScript
var default_1 = axios;
axios_1.default = default_1;

var axios$1 = axios_1;

var bank = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Helper functions to deal with the GNU Taler demo bank.
 *
 * Mostly useful for automated tests.
 */
/**
 * Imports.
 */
const axios_1 = __importDefault(axios$1);

function makeId(length) {
    let result = "";
    const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * characters.length));
    }
    return result;
}
class Bank {
    constructor(bankBaseUrl) {
        this.bankBaseUrl = bankBaseUrl;
    }
    generateWithdrawUri(bankUser, amount) {
        return __awaiter(this, void 0, void 0, function* () {
            const body = {
                amount,
            };
            const reqUrl = new URL("api/withdraw-headless-uri", this.bankBaseUrl).href;
            const resp = yield axios_1.default({
                method: "post",
                url: reqUrl,
                data: body,
                responseType: "json",
                headers: {
                    "X-Taler-Bank-Username": bankUser.username,
                    "X-Taler-Bank-Password": bankUser.password,
                },
            });
            if (resp.status != 200) {
                throw Error("failed to create bank reserve");
            }
            const withdrawUri = resp.data["taler_withdraw_uri"];
            if (!withdrawUri) {
                throw Error("Bank's response did not include withdraw URI");
            }
            return withdrawUri;
        });
    }
    createReserve(bankUser, amount, reservePub, exchangePaytoUri) {
        return __awaiter(this, void 0, void 0, function* () {
            const reqUrl = new URL("api/withdraw-headless", this.bankBaseUrl).href;
            const body = {
                auth: { type: "basic" },
                username: bankUser,
                amount,
                reserve_pub: reservePub,
                exchange_wire_detail: exchangePaytoUri,
            };
            const resp = yield axios_1.default({
                method: "post",
                url: reqUrl,
                data: body,
                responseType: "json",
                headers: {
                    "X-Taler-Bank-Username": bankUser.username,
                    "X-Taler-Bank-Password": bankUser.password,
                },
            });
            if (resp.status != 200) {
                throw Error("failed to create bank reserve");
            }
        });
    }
    registerRandomUser() {
        return __awaiter(this, void 0, void 0, function* () {
            const reqUrl = new URL("api/register", this.bankBaseUrl).href;
            const randId = makeId(8);
            const bankUser = {
                username: `testuser-${randId}`,
                password: `testpw-${randId}`,
            };
            const resp = yield axios_1.default({
                method: "post",
                url: reqUrl,
                data: querystring.stringify(bankUser),
                responseType: "json",
            });
            if (resp.status != 200) {
                throw Error("could not register bank user");
            }
            return bankUser;
        });
    }
}
exports.Bank = Bank;

});

unwrapExports(bank);
var bank_1 = bank.Bank;

var helpers$1 = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (commonjsGlobal && commonjsGlobal.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Helpers to create headless wallets.
 */
/**
 * Imports.
 */




const axios_1 = __importDefault(axios$1);
const amounts$1 = __importStar(amounts);



const logger = new logging.Logger("helpers.ts");
class ConsoleBadge {
    startBusy() {
    }
    stopBusy() {
    }
    showNotification() {
    }
    clearNotification() {
    }
}
class NodeHttpLib {
    get(url) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const resp = yield axios_1.default({
                    method: "get",
                    url: url,
                    responseType: "json",
                });
                return {
                    responseJson: resp.data,
                    status: resp.status,
                };
            }
            catch (e) {
                throw e;
            }
        });
    }
    postJson(url, body) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const resp = yield axios_1.default({
                    method: "post",
                    url: url,
                    responseType: "json",
                    data: body,
                });
                return {
                    responseJson: resp.data,
                    status: resp.status,
                };
            }
            catch (e) {
                throw e;
            }
        });
    }
}
exports.NodeHttpLib = NodeHttpLib;
/**
 * Get a wallet instance with default settings for node.
 */
function getDefaultNodeWallet(args = {}) {
    return __awaiter(this, void 0, void 0, function* () {
        const myNotifier = {
            notify() {
                if (args.notifyHandler) {
                    args.notifyHandler("");
                }
            }
        };
        const myBadge = new ConsoleBadge();
        build.BridgeIDBFactory.enableTracing = false;
        const myBackend = new build.MemoryBackend();
        myBackend.enableTracing = false;
        const storagePath = args.persistentStoragePath;
        if (storagePath) {
            try {
                const dbContentStr = fs.readFileSync(storagePath, { encoding: "utf-8" });
                const dbContent = JSON.parse(dbContentStr);
                myBackend.importDump(dbContent);
            }
            catch (e) {
                console.error("could not read wallet file");
            }
            myBackend.afterCommitCallback = () => __awaiter(this, void 0, void 0, function* () {
                // Allow caller to stop persisting the wallet.
                if (args.persistentStoragePath === undefined) {
                    return;
                }
                const dbContent = myBackend.exportDump();
                fs.writeFileSync(storagePath, JSON.stringify(dbContent, undefined, 2), { encoding: "utf-8" });
            });
        }
        build.BridgeIDBFactory.enableTracing = false;
        const myBridgeIdbFactory = new build.BridgeIDBFactory(myBackend);
        const myIdbFactory = myBridgeIdbFactory;
        let myHttpLib;
        if (args.httpLib) {
            myHttpLib = args.httpLib;
        }
        else {
            myHttpLib = new NodeHttpLib();
        }
        const myVersionChange = () => {
            console.error("version change requested, should not happen");
            throw Error();
        };
        const myUnsupportedUpgrade = () => {
            console.error("unsupported database migration");
            throw Error();
        };
        build.shimIndexedDB(myBridgeIdbFactory);
        const myDb = yield db.openTalerDb(myIdbFactory, myVersionChange, myUnsupportedUpgrade);
        const worker = new synchronousWorker.SynchronousCryptoWorkerFactory();
        //const worker = new NodeCryptoWorkerFactory();
        return new wallet.Wallet(myDb, myHttpLib, myBadge, myNotifier, worker);
    });
}
exports.getDefaultNodeWallet = getDefaultNodeWallet;
function withdrawTestBalance(myWallet, amount = "TESTKUDOS:10", bankBaseUrl = "https://bank.test.taler.net/", exchangeBaseUrl = "https://exchange.test.taler.net/") {
    return __awaiter(this, void 0, void 0, function* () {
        const reserveResponse = yield myWallet.createReserve({
            amount: amounts$1.parseOrThrow(amount),
            exchange: exchangeBaseUrl,
            exchangeWire: "payto://unknown",
        });
        const reservePub = reserveResponse.reservePub;
        const bank$1 = new bank.Bank(bankBaseUrl);
        const bankUser = yield bank$1.registerRandomUser();
        logger.trace(`Registered bank user ${JSON.stringify(bankUser)}`);
        const exchangePaytoUri = yield myWallet.getExchangePaytoUri(exchangeBaseUrl, ["x-taler-bank"]);
        yield bank$1.createReserve(bankUser, amount, reservePub, exchangePaytoUri);
        yield myWallet.confirmReserve({ reservePub: reserveResponse.reservePub });
        yield myWallet.runUntilReserveDepleted(reservePub);
    });
}
exports.withdrawTestBalance = withdrawTestBalance;

});

unwrapExports(helpers$1);
var helpers_1$1 = helpers$1.NodeHttpLib;
var helpers_2$1 = helpers$1.getDefaultNodeWallet;
var helpers_3$1 = helpers$1.withdrawTestBalance;

var android = createCommonjsModule(function (module, exports) {
/*
 This file is part of GNU Taler
 (C) 2019 GNUnet e.V.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });



// @ts-ignore: special built-in module
//import akono = require("akono");
class AndroidHttpLib {
    constructor(sendMessage) {
        this.sendMessage = sendMessage;
        this.useNfcTunnel = false;
        this.nodeHttpLib = new helpers$1.NodeHttpLib();
        this.requestId = 1;
        this.requestMap = {};
    }
    get(url) {
        if (this.useNfcTunnel) {
            const myId = this.requestId++;
            const p = promiseUtils.openPromise();
            this.requestMap[myId] = p;
            const request = {
                method: "get",
                url,
            };
            this.sendMessage(JSON.stringify({
                type: "tunnelHttp",
                request,
                id: myId,
            }));
            return p.promise;
        }
        else {
            return this.nodeHttpLib.get(url);
        }
    }
    postJson(url, body) {
        if (this.useNfcTunnel) {
            const myId = this.requestId++;
            const p = promiseUtils.openPromise();
            this.requestMap[myId] = p;
            const request = {
                method: "postJson",
                url,
                body,
            };
            this.sendMessage(JSON.stringify({ type: "tunnelHttp", request, id: myId }));
            return p.promise;
        }
        else {
            return this.nodeHttpLib.postJson(url, body);
        }
    }
    handleTunnelResponse(msg) {
        const myId = msg.id;
        const p = this.requestMap[myId];
        if (!p) {
            console.error(`no matching request for tunneled HTTP response, id=${myId}`);
        }
        if (msg.status == 200) {
            p.resolve({ responseJson: msg.responseJson, status: msg.status });
        }
        else {
            p.reject(new Error(`unexpected HTTP status code ${msg.status}`));
        }
        delete this.requestMap[myId];
    }
}
exports.AndroidHttpLib = AndroidHttpLib;
function installAndroidWalletListener() {
    // @ts-ignore
    const sendMessage = globalThis.__akono_sendMessage;
    if (typeof sendMessage !== "function") {
        const errMsg = "FATAL: cannot install android wallet listener: akono functions missing";
        console.error(errMsg);
        throw new Error(errMsg);
    }
    let wp = promiseUtils.openPromise();
    let httpLib = new AndroidHttpLib(sendMessage);
    let walletArgs;
    const onMessage = (msgStr) => __awaiter(this, void 0, void 0, function* () {
        if (typeof msgStr !== "string") {
            console.error("expected string as message");
            return;
        }
        const msg = JSON.parse(msgStr);
        const operation = msg.operation;
        if (typeof operation !== "string") {
            console.error("message to android wallet helper must contain operation of type string");
            return;
        }
        const id = msg.id;
        let result;
        switch (operation) {
            case "init": {
                walletArgs = {
                    notifyHandler: () => __awaiter(this, void 0, void 0, function* () {
                        sendMessage(JSON.stringify({ type: "notification" }));
                    }),
                    persistentStoragePath: msg.args.persistentStoragePath,
                    httpLib: httpLib,
                };
                const w = yield helpers$1.getDefaultNodeWallet(walletArgs);
                w.runLoopScheduledRetries().catch((e) => {
                    console.error("Error during wallet retry loop", e);
                });
                wp.resolve(w);
                result = true;
                break;
            }
            case "getBalances": {
                const wallet = yield wp.promise;
                result = yield wallet.getBalances();
                break;
            }
            case "getPendingOperations": {
                const wallet = yield wp.promise;
                result = yield wallet.getPendingOperations();
                break;
            }
            case "withdrawTestkudos": {
                const wallet = yield wp.promise;
                yield helpers$1.withdrawTestBalance(wallet);
                result = {};
                break;
            }
            case "getHistory": {
                const wallet = yield wp.promise;
                result = yield wallet.getHistory();
                break;
            }
            case "retryPendingNow": {
                const wallet = yield wp.promise;
                yield wallet.runPending(true);
                result = {};
                break;
            }
            case "preparePay": {
                const wallet = yield wp.promise;
                result = yield wallet.preparePay(msg.args.url);
                break;
            }
            case "confirmPay": {
                const wallet = yield wp.promise;
                result = yield wallet.confirmPay(msg.args.proposalId, msg.args.sessionId);
                break;
            }
            case "startTunnel": {
                httpLib.useNfcTunnel = true;
                break;
            }
            case "stopTunnel": {
                httpLib.useNfcTunnel = false;
                break;
            }
            case "tunnelResponse": {
                httpLib.handleTunnelResponse(msg.args);
                break;
            }
            case "getWithdrawalInfo": {
                const wallet = yield wp.promise;
                result = yield wallet.getWithdrawalInfo(msg.args.talerWithdrawUri);
                break;
            }
            case "acceptWithdrawal": {
                const wallet = yield wp.promise;
                result = yield wallet.acceptWithdrawal(msg.args.talerWithdrawUri, msg.args.selectedExchange);
                break;
            }
            case "reset": {
                const oldArgs = walletArgs;
                walletArgs = Object.assign({}, oldArgs);
                if (oldArgs && oldArgs.persistentStoragePath) {
                    try {
                        fs.unlinkSync(oldArgs.persistentStoragePath);
                    }
                    catch (e) {
                        console.error("Error while deleting the wallet db:", e);
                    }
                    // Prevent further storage!
                    walletArgs.persistentStoragePath = undefined;
                }
                const wallet = yield wp.promise;
                wallet.stop();
                wp = promiseUtils.openPromise();
                const w = yield helpers$1.getDefaultNodeWallet(walletArgs);
                w.runLoopScheduledRetries().catch((e) => {
                    console.error("Error during wallet retry loop", e);
                });
                wp.resolve(w);
                result = {};
                break;
            }
            default:
                console.error(`operation "${operation}" not understood`);
                return;
        }
        const respMsg = { result, id, operation, type: "response" };
        sendMessage(JSON.stringify(respMsg));
    });
    // @ts-ignore
    globalThis.__akono_onMessage = onMessage;
    console.log("android wallet listener installed");
}
exports.installAndroidWalletListener = installAndroidWalletListener;

});

var index = unwrapExports(android);
var android_1 = android.AndroidHttpLib;
var android_2 = android.installAndroidWalletListener;

exports.AndroidHttpLib = android_1;
exports.default = index;
exports.installAndroidWalletListener = android_2;