@lightsparkdev/core 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
+ CurrencyUnit: () => CurrencyUnit,
33
34
  DefaultCrypto: () => DefaultCrypto,
34
35
  KeyOrAlias: () => KeyOrAlias,
35
36
  LightsparkAuthException: () => LightsparkAuthException_default,
@@ -44,22 +45,38 @@ __export(src_exports, {
44
45
  SigningKey: () => SigningKey,
45
46
  SigningKeyType: () => SigningKeyType,
46
47
  StubAuthProvider: () => StubAuthProvider,
48
+ abbrCurrencyUnit: () => abbrCurrencyUnit,
47
49
  apiDomainForEnvironment: () => apiDomainForEnvironment,
48
50
  b64decode: () => b64decode,
49
51
  b64encode: () => b64encode,
50
52
  bytesToHex: () => bytesToHex,
53
+ clamp: () => clamp,
51
54
  convertCurrencyAmount: () => convertCurrencyAmount,
55
+ convertCurrencyAmountValue: () => convertCurrencyAmountValue,
56
+ countryCodesToCurrencyCodes: () => countryCodesToCurrencyCodes,
52
57
  createSha256Hash: () => createSha256Hash,
58
+ defaultCurrencyCode: () => defaultCurrencyCode,
59
+ formatCurrencyStr: () => formatCurrencyStr,
60
+ getCurrentLocale: () => getCurrentLocale,
53
61
  getErrorMsg: () => getErrorMsg,
54
62
  hexToBytes: () => hexToBytes,
55
63
  isBrowser: () => isBrowser,
64
+ isCurrencyAmount: () => isCurrencyAmount,
65
+ isCurrencyAmountObj: () => isCurrencyAmountObj,
66
+ isCurrencyMap: () => isCurrencyMap,
56
67
  isError: () => isError,
57
68
  isErrorMsg: () => isErrorMsg,
58
69
  isErrorWithMessage: () => isErrorWithMessage,
59
70
  isNode: () => isNode,
71
+ isNumber: () => isNumber,
60
72
  isTest: () => isTest,
61
73
  isType: () => isType,
74
+ linearInterpolate: () => linearInterpolate,
75
+ localeToCurrencyCode: () => localeToCurrencyCode,
76
+ localeToCurrencySymbol: () => localeToCurrencySymbol,
77
+ mapCurrencyAmount: () => mapCurrencyAmount,
62
78
  pollUntil: () => pollUntil,
79
+ round: () => round,
63
80
  sleep: () => sleep,
64
81
  urlsafe_b64decode: () => urlsafe_b64decode
65
82
  });
@@ -407,7 +424,314 @@ async function createSha256Hash(data, asHex) {
407
424
  }
408
425
  }
409
426
 
427
+ // src/utils/locale.ts
428
+ function getCurrentLocale() {
429
+ return Intl.NumberFormat().resolvedOptions().locale;
430
+ }
431
+
432
+ // src/utils/localeToCurrencyCodes.ts
433
+ var countryCodesToCurrencyCodes = {
434
+ AD: "EUR",
435
+ // AE: "AED",
436
+ // AF: "AFN",
437
+ // AG: "XCD",
438
+ // AI: "XCD",
439
+ // AL: "ALL",
440
+ // AM: "AMD",
441
+ // AO: "AOA",
442
+ AR: "ARS",
443
+ AS: "USD",
444
+ AT: "EUR",
445
+ AU: "AUD",
446
+ // AW: "AWG",
447
+ AX: "EUR",
448
+ // AZ: "AZN",
449
+ // BA: "BAM",
450
+ // BB: "BBD",
451
+ // BD: "BDT",
452
+ BE: "EUR",
453
+ // BF: "XOF",
454
+ // BG: "BGN",
455
+ // BH: "BHD",
456
+ // BI: "BIF",
457
+ // BJ: "XOF",
458
+ BL: "EUR",
459
+ // BM: "BMD",
460
+ // BN: "BND",
461
+ // BO: "BOB",
462
+ BQ: "USD",
463
+ BR: "BRL",
464
+ // BS: "BSD",
465
+ // BT: "BTN",
466
+ // BV: "NOK",
467
+ // BW: "BWP",
468
+ // BY: "BYN",
469
+ // BZ: "BZD",
470
+ CA: "CAD",
471
+ // CC: "AUD",
472
+ // CD: "CDF",
473
+ // CF: "XAF",
474
+ // CG: "XAF",
475
+ // CH: "CHF",
476
+ // CI: "XOF",
477
+ // CK: "NZD",
478
+ // CL: "CLP",
479
+ // CM: "XAF",
480
+ // CN: "CNY",
481
+ CO: "COP",
482
+ // CR: "CRC",
483
+ // CU: "CUP",
484
+ // CV: "CVE",
485
+ // CW: "ANG",
486
+ // CX: "AUD",
487
+ CY: "EUR",
488
+ // CZ: "CZK",
489
+ DE: "EUR",
490
+ // DJ: "DJF",
491
+ // DK: "DKK",
492
+ // DM: "XCD",
493
+ // DO: "DOP",
494
+ // DZ: "DZD",
495
+ EC: "USD",
496
+ EE: "EUR",
497
+ // EG: "EGP",
498
+ // EH: "MAD",
499
+ // ER: "ERN",
500
+ ES: "EUR",
501
+ // ET: "ETB",
502
+ FI: "EUR",
503
+ // FJ: "FJD",
504
+ // FK: "FKP",
505
+ FM: "USD",
506
+ // FO: "DKK",
507
+ FR: "EUR",
508
+ // GA: "XAF",
509
+ GB: "GBP",
510
+ // GD: "XCD",
511
+ // GE: "GEL",
512
+ GF: "EUR",
513
+ GG: "GBP",
514
+ // GH: "GHS",
515
+ // GI: "GIP",
516
+ // GL: "DKK",
517
+ // GM: "GMD",
518
+ // GN: "GNF",
519
+ GP: "EUR",
520
+ // GQ: "XAF",
521
+ GR: "EUR",
522
+ GS: "GBP",
523
+ // GT: "GTQ",
524
+ GU: "USD",
525
+ // GW: "XOF",
526
+ // GY: "GYD",
527
+ // HK: "HKD",
528
+ // HM: "AUD",
529
+ // HN: "HNL",
530
+ // HR: "HRK",
531
+ // HT: "HTG",
532
+ // HU: "HUF",
533
+ // ID: "IDR",
534
+ IE: "EUR",
535
+ // IL: "ILS",
536
+ IM: "GBP",
537
+ IN: "INR",
538
+ IO: "USD",
539
+ // IQ: "IQD",
540
+ // IR: "IRR",
541
+ // IS: "ISK",
542
+ IT: "EUR",
543
+ JE: "GBP",
544
+ // JM: "JMD",
545
+ // JO: "JOD",
546
+ // JP: "JPY",
547
+ // KE: "KES",
548
+ // KG: "KGS",
549
+ // KH: "KHR",
550
+ // KI: "AUD",
551
+ // KM: "KMF",
552
+ // KN: "XCD",
553
+ // KP: "KPW",
554
+ // KR: "KRW",
555
+ // KW: "KWD",
556
+ // KY: "KYD",
557
+ // KZ: "KZT",
558
+ // LA: "LAK",
559
+ // LB: "LBP",
560
+ // LC: "XCD",
561
+ // LI: "CHF",
562
+ // LK: "LKR",
563
+ // LR: "LRD",
564
+ // LS: "LSL",
565
+ LT: "EUR",
566
+ LU: "EUR",
567
+ LV: "EUR",
568
+ // LY: "LYD",
569
+ // MA: "MAD",
570
+ MC: "EUR",
571
+ // MD: "MDL",
572
+ ME: "EUR",
573
+ MF: "EUR",
574
+ // MG: "MGA",
575
+ MH: "USD",
576
+ // MK: "MKD",
577
+ // ML: "XOF",
578
+ // MM: "MMK",
579
+ // MN: "MNT",
580
+ // MO: "MOP",
581
+ MP: "USD",
582
+ MQ: "EUR",
583
+ // MR: "MRO",
584
+ // MS: "XCD",
585
+ MT: "EUR",
586
+ // MU: "MUR",
587
+ // MV: "MVR",
588
+ // MW: "MWK",
589
+ MX: "MXN",
590
+ // MY: "MYR",
591
+ // MZ: "MZN",
592
+ // NA: "NAD",
593
+ // NC: "XPF",
594
+ // NE: "XOF",
595
+ NF: "AUD",
596
+ // NG: "NGN",
597
+ // NI: "NIO",
598
+ NL: "EUR",
599
+ // NO: "NOK",
600
+ // NP: "NPR",
601
+ NR: "AUD",
602
+ // NU: "NZD",
603
+ // NZ: "NZD",
604
+ // OM: "OMR",
605
+ // PA: "PAB",
606
+ // PE: "PEN",
607
+ // PF: "XPF",
608
+ // PG: "PGK",
609
+ // PH: "PHP",
610
+ // PK: "PKR",
611
+ // PL: "PLN",
612
+ PM: "EUR",
613
+ // PN: "NZD",
614
+ PR: "USD",
615
+ // PS: "ILS",
616
+ PT: "EUR",
617
+ PW: "USD",
618
+ // PY: "PYG",
619
+ // QA: "QAR",
620
+ RE: "EUR",
621
+ // RO: "RON",
622
+ // RS: "RSD",
623
+ // RU: "RUB",
624
+ // RW: "RWF",
625
+ // SA: "SAR",
626
+ // SB: "SBD",
627
+ // SC: "SCR",
628
+ // SD: "SDG",
629
+ // SE: "SEK",
630
+ // SG: "SGD",
631
+ // SH: "SHP",
632
+ SI: "EUR",
633
+ // SJ: "NOK",
634
+ SK: "EUR",
635
+ // SL: "SLL",
636
+ SM: "EUR",
637
+ // SN: "XOF",
638
+ // SO: "SOS",
639
+ // SR: "SRD",
640
+ // ST: "STD",
641
+ // SV: "SVC",
642
+ // SX: "ANG",
643
+ // SY: "SYP",
644
+ // SZ: "SZL",
645
+ TC: "USD",
646
+ // TD: "XAF",
647
+ TF: "EUR",
648
+ // TG: "XOF",
649
+ // TH: "THB",
650
+ // TJ: "TJS",
651
+ // TK: "NZD",
652
+ TL: "USD",
653
+ // TM: "TMT",
654
+ // TN: "TND",
655
+ // TO: "TOP",
656
+ // TR: "TRY",
657
+ // TT: "TTD",
658
+ TV: "AUD",
659
+ // TW: "TWD",
660
+ // TZ: "TZS",
661
+ // UA: "UAH",
662
+ // UG: "UGX",
663
+ UM: "USD",
664
+ US: "USD",
665
+ // UY: "UYU",
666
+ // UZ: "UZS",
667
+ VA: "EUR",
668
+ // VC: "XCD",
669
+ // VE: "VEF",
670
+ VG: "USD",
671
+ VI: "USD",
672
+ // VN: "VND",
673
+ // VU: "VUV",
674
+ // WF: "XPF",
675
+ // WS: "WST",
676
+ // YE: "YER",
677
+ YT: "EUR"
678
+ // ZA: "ZAR",
679
+ // ZM: "ZMW",
680
+ // ZW: "ZWL",
681
+ };
682
+ function getCountryCode(localeString) {
683
+ let components = localeString.split("_");
684
+ if (components.length === 2) {
685
+ return components.pop();
686
+ }
687
+ components = localeString.split("-");
688
+ if (components.length === 2) {
689
+ return components.pop();
690
+ }
691
+ return localeString;
692
+ }
693
+ function localeToCurrencyCode(locale) {
694
+ const countryCode = getCountryCode(locale);
695
+ return countryCodesToCurrencyCodes[countryCode];
696
+ }
697
+
698
+ // src/utils/numbers.ts
699
+ function clamp(val, min, max) {
700
+ return val > max ? max : val < min ? min : val;
701
+ }
702
+ function linearInterpolate(value, fromRangeStart, fromRangeEnd, toRangeStart, toRangeEnd) {
703
+ const fromRangeMax = fromRangeEnd - fromRangeStart;
704
+ const fromRangeVal = value - fromRangeStart;
705
+ const toRangeMax = toRangeEnd - toRangeStart;
706
+ const val = fromRangeVal / fromRangeMax * toRangeMax + toRangeStart;
707
+ return clamp(
708
+ val,
709
+ Math.min(toRangeStart, toRangeEnd),
710
+ Math.max(toRangeStart, toRangeEnd)
711
+ );
712
+ }
713
+ function round(num, decimalPlaces = 0) {
714
+ const p = Math.pow(10, decimalPlaces);
715
+ const n = num * p * (1 + Number.EPSILON);
716
+ return Math.round(n) / p;
717
+ }
718
+ function isNumber(value) {
719
+ return typeof value === "number";
720
+ }
721
+
410
722
  // src/utils/currency.ts
723
+ var defaultCurrencyCode = "USD";
724
+ var CurrencyUnit = /* @__PURE__ */ ((CurrencyUnit2) => {
725
+ CurrencyUnit2["FUTURE_VALUE"] = "FUTURE_VALUE";
726
+ CurrencyUnit2["BITCOIN"] = "BITCOIN";
727
+ CurrencyUnit2["SATOSHI"] = "SATOSHI";
728
+ CurrencyUnit2["MILLISATOSHI"] = "MILLISATOSHI";
729
+ CurrencyUnit2["USD"] = "USD";
730
+ CurrencyUnit2["NANOBITCOIN"] = "NANOBITCOIN";
731
+ CurrencyUnit2["MICROBITCOIN"] = "MICROBITCOIN";
732
+ CurrencyUnit2["MILLIBITCOIN"] = "MILLIBITCOIN";
733
+ return CurrencyUnit2;
734
+ })(CurrencyUnit || {});
411
735
  var CONVERSION_MAP = {
412
736
  ["BITCOIN" /* BITCOIN */]: {
413
737
  ["BITCOIN" /* BITCOIN */]: (v) => v,
@@ -415,67 +739,298 @@ var CONVERSION_MAP = {
415
739
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
416
740
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
417
741
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
418
- ["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
742
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 1e8,
743
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
744
+ /* Round without decimals since we're returning cents: */
745
+ round(v * centsPerBtc, 2)
746
+ )
419
747
  },
420
748
  ["MICROBITCOIN" /* MICROBITCOIN */]: {
421
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
749
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e6,
422
750
  ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
423
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
751
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e3,
424
752
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
425
753
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
426
- ["SATOSHI" /* SATOSHI */]: (v) => v * 100
754
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 100,
755
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
756
+ /* Round without decimals since we're returning cents: */
757
+ round(v / 1e6 * centsPerBtc)
758
+ )
427
759
  },
428
760
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: {
429
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
761
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e3,
430
762
  ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
431
763
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
432
764
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
433
765
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
434
- ["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
766
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 1e5,
767
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
768
+ /* Round without decimals since we're returning cents: */
769
+ round(v / 1e3 * centsPerBtc)
770
+ )
435
771
  },
436
772
  ["MILLISATOSHI" /* MILLISATOSHI */]: {
437
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
438
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
439
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e8),
773
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e11,
774
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v / 1e5,
775
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e8,
440
776
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
441
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
442
- ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
777
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v / 100,
778
+ ["SATOSHI" /* SATOSHI */]: (v) => v / 1e3,
779
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
780
+ /* Round without decimals since we're returning cents: */
781
+ round(v / 1e11 * centsPerBtc)
782
+ )
443
783
  },
444
784
  ["NANOBITCOIN" /* NANOBITCOIN */]: {
445
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
446
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
447
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e6),
785
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e9,
786
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v / 1e3,
787
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e6,
448
788
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
449
789
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
450
- ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
790
+ ["SATOSHI" /* SATOSHI */]: (v) => v / 10,
791
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
792
+ /* Round without decimals since we're returning cents: */
793
+ round(v / 1e9 * centsPerBtc)
794
+ )
451
795
  },
452
796
  ["SATOSHI" /* SATOSHI */]: {
453
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
454
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
455
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e5),
797
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e8,
798
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v / 100,
799
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e5,
456
800
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
457
801
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
458
- ["SATOSHI" /* SATOSHI */]: (v) => v
802
+ ["SATOSHI" /* SATOSHI */]: (v) => v,
803
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
804
+ /* Round without decimals since we're returning cents: */
805
+ round(v / 1e8 * centsPerBtc)
806
+ )
807
+ },
808
+ ["USD" /* USD */]: {
809
+ ["BITCOIN" /* BITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc,
810
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e6,
811
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e3,
812
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e11,
813
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e9,
814
+ ["SATOSHI" /* SATOSHI */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e8,
815
+ ["USD" /* USD */]: (v) => v
459
816
  }
460
817
  };
461
- var convertCurrencyAmount = (from, toUnit) => {
462
- if (from.originalUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || from.originalUnit === "USD" /* USD */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "USD" /* USD */) {
818
+ function convertCurrencyAmountValue(fromUnit, toUnit, amount, centsPerBtc = 1) {
819
+ if (fromUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */) {
463
820
  throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
464
821
  }
465
- const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
822
+ if (fromUnit === toUnit) {
823
+ return amount;
824
+ }
825
+ const conversionFn = CONVERSION_MAP[fromUnit][toUnit];
466
826
  if (!conversionFn) {
467
827
  throw new LightsparkException_default(
468
828
  "CurrencyError",
469
- `Cannot convert from ${from.originalUnit} to ${toUnit}`
829
+ `Cannot convert from ${fromUnit} to ${toUnit}`
470
830
  );
471
831
  }
832
+ return conversionFn(amount, centsPerBtc);
833
+ }
834
+ var convertCurrencyAmount = (from, toUnit) => {
835
+ const value = convertCurrencyAmountValue(
836
+ from.originalUnit,
837
+ toUnit,
838
+ from.originalValue
839
+ );
472
840
  return {
473
841
  ...from,
474
842
  preferredCurrencyUnit: toUnit,
475
- preferredCurrencyValueApprox: conversionFn(from.originalValue),
476
- preferredCurrencyValueRounded: conversionFn(from.originalValue)
843
+ preferredCurrencyValueApprox: value,
844
+ preferredCurrencyValueRounded: value
477
845
  };
478
846
  };
847
+ function isCurrencyAmountObj(arg) {
848
+ return typeof arg === "object" && arg !== null && "value" in arg && "unit" in arg;
849
+ }
850
+ function isCurrencyAmount(arg) {
851
+ return typeof arg === "object" && arg !== null && "originalValue" in arg && "originalUnit" in arg && "preferredCurrencyUnit" in arg && "preferredCurrencyValueRounded" in arg && "preferredCurrencyValueApprox" in arg;
852
+ }
853
+ function asNumber(value) {
854
+ if (typeof value === "string") {
855
+ return Number(value);
856
+ }
857
+ return value || 0;
858
+ }
859
+ function getCurrencyAmount(currencyAmountArg) {
860
+ let value = 0;
861
+ let unit = void 0;
862
+ if (isCurrencyAmountObj(currencyAmountArg)) {
863
+ value = asNumber(currencyAmountArg.value);
864
+ unit = currencyAmountArg.unit;
865
+ } else if (isCurrencyAmount(currencyAmountArg)) {
866
+ value = currencyAmountArg.originalValue;
867
+ unit = currencyAmountArg.originalUnit;
868
+ }
869
+ return {
870
+ value: asNumber(value),
871
+ unit: unit || "SATOSHI" /* SATOSHI */
872
+ };
873
+ }
874
+ function mapCurrencyAmount(currencyAmountArg, centsPerBtc = 1) {
875
+ const { value, unit } = getCurrencyAmount(currencyAmountArg);
876
+ const convert = convertCurrencyAmountValue;
877
+ const sats = convert(unit, "SATOSHI" /* SATOSHI */, value, centsPerBtc);
878
+ const btc = convert(unit, "BITCOIN" /* BITCOIN */, value, centsPerBtc);
879
+ const msats = convert(unit, "MILLISATOSHI" /* MILLISATOSHI */, value, centsPerBtc);
880
+ const usd = convert(unit, "USD" /* USD */, value, centsPerBtc);
881
+ const mibtc = convert(unit, "MICROBITCOIN" /* MICROBITCOIN */, value, centsPerBtc);
882
+ const mlbtc = convert(unit, "MILLIBITCOIN" /* MILLIBITCOIN */, value, centsPerBtc);
883
+ const nbtc = convert(unit, "NANOBITCOIN" /* NANOBITCOIN */, value, centsPerBtc);
884
+ const mapWithCurrencyUnits = {
885
+ ["BITCOIN" /* BITCOIN */]: btc,
886
+ ["SATOSHI" /* SATOSHI */]: sats,
887
+ ["MILLISATOSHI" /* MILLISATOSHI */]: msats,
888
+ ["USD" /* USD */]: usd,
889
+ ["MICROBITCOIN" /* MICROBITCOIN */]: mibtc,
890
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: mlbtc,
891
+ ["NANOBITCOIN" /* NANOBITCOIN */]: nbtc,
892
+ ["FUTURE_VALUE" /* FUTURE_VALUE */]: NaN,
893
+ formatted: {
894
+ ["BITCOIN" /* BITCOIN */]: formatCurrencyStr({
895
+ value: btc,
896
+ unit: "BITCOIN" /* BITCOIN */
897
+ }),
898
+ ["SATOSHI" /* SATOSHI */]: formatCurrencyStr({
899
+ value: sats,
900
+ unit: "SATOSHI" /* SATOSHI */
901
+ }),
902
+ ["MILLISATOSHI" /* MILLISATOSHI */]: formatCurrencyStr({
903
+ value: msats,
904
+ unit: "MILLISATOSHI" /* MILLISATOSHI */
905
+ }),
906
+ ["MICROBITCOIN" /* MICROBITCOIN */]: formatCurrencyStr({
907
+ value: mibtc,
908
+ unit: "MICROBITCOIN" /* MICROBITCOIN */
909
+ }),
910
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: formatCurrencyStr({
911
+ value: mlbtc,
912
+ unit: "MILLIBITCOIN" /* MILLIBITCOIN */
913
+ }),
914
+ ["NANOBITCOIN" /* NANOBITCOIN */]: formatCurrencyStr({
915
+ value: nbtc,
916
+ unit: "NANOBITCOIN" /* NANOBITCOIN */
917
+ }),
918
+ ["USD" /* USD */]: formatCurrencyStr({
919
+ value: usd,
920
+ unit: "USD" /* USD */
921
+ }),
922
+ ["FUTURE_VALUE" /* FUTURE_VALUE */]: "-"
923
+ }
924
+ };
925
+ return {
926
+ ...mapWithCurrencyUnits,
927
+ btc,
928
+ sats,
929
+ msats,
930
+ isZero: msats === 0,
931
+ isLessThan: (other) => {
932
+ if (isNumber(other)) {
933
+ return msats < other;
934
+ }
935
+ if (isCurrencyAmountObj(other)) {
936
+ other = mapCurrencyAmount(other);
937
+ }
938
+ return msats < other.msats;
939
+ },
940
+ isGreaterThan: (other) => {
941
+ if (isNumber(other)) {
942
+ return msats > other;
943
+ }
944
+ if (isCurrencyAmountObj(other)) {
945
+ other = mapCurrencyAmount(other);
946
+ }
947
+ return msats > other.msats;
948
+ },
949
+ isEqualTo: (other) => {
950
+ if (isNumber(other)) {
951
+ return msats === other;
952
+ }
953
+ if (isCurrencyAmountObj(other)) {
954
+ other = mapCurrencyAmount(other);
955
+ }
956
+ return msats === other.msats;
957
+ },
958
+ formatted: {
959
+ ...mapWithCurrencyUnits.formatted,
960
+ btc: mapWithCurrencyUnits.formatted["BITCOIN" /* BITCOIN */],
961
+ sats: mapWithCurrencyUnits.formatted["SATOSHI" /* SATOSHI */],
962
+ msats: mapWithCurrencyUnits.formatted["MILLISATOSHI" /* MILLISATOSHI */]
963
+ },
964
+ type: "CurrencyMap"
965
+ };
966
+ }
967
+ var isCurrencyMap = (currencyMap) => typeof currencyMap === "object" && currencyMap !== null && "type" in currencyMap && typeof currencyMap.type === "string" && currencyMap.type === "CurrencyMap";
968
+ var abbrCurrencyUnit = (unit) => {
969
+ switch (unit) {
970
+ case "BITCOIN" /* BITCOIN */:
971
+ return "BTC";
972
+ case "SATOSHI" /* SATOSHI */:
973
+ return "SAT";
974
+ case "MILLISATOSHI" /* MILLISATOSHI */:
975
+ return "MSAT";
976
+ case "USD" /* USD */:
977
+ return "USD";
978
+ }
979
+ return "Unsupported CurrencyUnit";
980
+ };
981
+ function formatCurrencyStr(amount, maxFractionDigits, compact, showBtcSymbol = false, options = {}) {
982
+ const currencyAmount = getCurrencyAmount(amount);
983
+ let { value: num } = currencyAmount;
984
+ const { unit } = currencyAmount;
985
+ if (unit === "USD" /* USD */) {
986
+ num = num / 100;
987
+ }
988
+ function getDefaultMaxFractionDigits(defaultDigits) {
989
+ return typeof maxFractionDigits === "undefined" ? compact ? 1 : defaultDigits : maxFractionDigits;
990
+ }
991
+ const symbol = !showBtcSymbol ? "" : unit === "BITCOIN" /* BITCOIN */ ? "\uE903" : unit === "SATOSHI" /* SATOSHI */ ? "\uE902" : "";
992
+ const currentLocale = getCurrentLocale();
993
+ switch (unit) {
994
+ case "BITCOIN" /* BITCOIN */:
995
+ return `${symbol}${num.toLocaleString(currentLocale, {
996
+ notation: compact ? "compact" : void 0,
997
+ maximumFractionDigits: getDefaultMaxFractionDigits(4),
998
+ ...options
999
+ })}`;
1000
+ case "MILLISATOSHI" /* MILLISATOSHI */:
1001
+ case "SATOSHI" /* SATOSHI */:
1002
+ case "MICROBITCOIN" /* MICROBITCOIN */:
1003
+ case "MILLIBITCOIN" /* MILLIBITCOIN */:
1004
+ case "NANOBITCOIN" /* NANOBITCOIN */:
1005
+ default:
1006
+ return `${symbol}${num.toLocaleString(currentLocale, {
1007
+ notation: compact ? "compact" : void 0,
1008
+ maximumFractionDigits: getDefaultMaxFractionDigits(0),
1009
+ ...options
1010
+ })}`;
1011
+ case "USD" /* USD */:
1012
+ return num.toLocaleString(currentLocale, {
1013
+ style: "currency",
1014
+ currency: defaultCurrencyCode,
1015
+ notation: compact ? "compact" : void 0,
1016
+ maximumFractionDigits: getDefaultMaxFractionDigits(2),
1017
+ ...options
1018
+ });
1019
+ }
1020
+ }
1021
+ function localeToCurrencySymbol(locale) {
1022
+ const currencyCode = localeToCurrencyCode(locale);
1023
+ const formatted = new Intl.NumberFormat(locale, {
1024
+ style: "currency",
1025
+ currency: currencyCode,
1026
+ useGrouping: false,
1027
+ // to avoid thousands separators
1028
+ minimumFractionDigits: 0,
1029
+ maximumFractionDigits: 0
1030
+ }).format(0);
1031
+ const symbol = formatted.replace(/[0-9\s\u00a0]/g, "");
1032
+ return symbol;
1033
+ }
479
1034
 
480
1035
  // src/utils/errors.ts
481
1036
  var isError = (e) => {
@@ -958,6 +1513,7 @@ var apiDomainForEnvironment = (environment) => {
958
1513
  var ServerEnvironment_default = ServerEnvironment;
959
1514
  // Annotate the CommonJS export names for ESM import in node:
960
1515
  0 && (module.exports = {
1516
+ CurrencyUnit,
961
1517
  DefaultCrypto,
962
1518
  KeyOrAlias,
963
1519
  LightsparkAuthException,
@@ -972,22 +1528,38 @@ var ServerEnvironment_default = ServerEnvironment;
972
1528
  SigningKey,
973
1529
  SigningKeyType,
974
1530
  StubAuthProvider,
1531
+ abbrCurrencyUnit,
975
1532
  apiDomainForEnvironment,
976
1533
  b64decode,
977
1534
  b64encode,
978
1535
  bytesToHex,
1536
+ clamp,
979
1537
  convertCurrencyAmount,
1538
+ convertCurrencyAmountValue,
1539
+ countryCodesToCurrencyCodes,
980
1540
  createSha256Hash,
1541
+ defaultCurrencyCode,
1542
+ formatCurrencyStr,
1543
+ getCurrentLocale,
981
1544
  getErrorMsg,
982
1545
  hexToBytes,
983
1546
  isBrowser,
1547
+ isCurrencyAmount,
1548
+ isCurrencyAmountObj,
1549
+ isCurrencyMap,
984
1550
  isError,
985
1551
  isErrorMsg,
986
1552
  isErrorWithMessage,
987
1553
  isNode,
1554
+ isNumber,
988
1555
  isTest,
989
1556
  isType,
1557
+ linearInterpolate,
1558
+ localeToCurrencyCode,
1559
+ localeToCurrencySymbol,
1560
+ mapCurrencyAmount,
990
1561
  pollUntil,
1562
+ round,
991
1563
  sleep,
992
1564
  urlsafe_b64decode
993
1565
  });