@lightsparkdev/core 1.0.4 → 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
  });
@@ -372,18 +389,349 @@ var isBrowser = typeof window !== "undefined" && typeof window.document !== "und
372
389
  var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
373
390
  var isTest = isNode && process.env.NODE_ENV === "test";
374
391
 
392
+ // src/utils/hex.ts
393
+ var bytesToHex = (bytes) => {
394
+ return bytes.reduce((acc, byte) => {
395
+ return acc += ("0" + byte.toString(16)).slice(-2);
396
+ }, "");
397
+ };
398
+ var hexToBytes = (hex) => {
399
+ const bytes = [];
400
+ for (let c = 0; c < hex.length; c += 2) {
401
+ bytes.push(parseInt(hex.substr(c, 2), 16));
402
+ }
403
+ return Uint8Array.from(bytes);
404
+ };
405
+
375
406
  // src/utils/createHash.ts
376
- var createSha256Hash = async (data) => {
407
+ async function createSha256Hash(data, asHex) {
377
408
  if (isBrowser) {
378
- return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
409
+ const source = typeof data === "string" ? new TextEncoder().encode(data) : data;
410
+ const buffer = await window.crypto.subtle.digest("SHA-256", source);
411
+ const arr = new Uint8Array(buffer);
412
+ if (asHex) {
413
+ return bytesToHex(arr);
414
+ }
415
+ return arr;
379
416
  } else {
380
417
  const { createHash } = await import("crypto");
418
+ if (asHex) {
419
+ const hexStr = createHash("sha256").update(data).digest("hex");
420
+ return hexStr;
421
+ }
381
422
  const buffer = createHash("sha256").update(data).digest();
382
423
  return new Uint8Array(buffer);
383
424
  }
425
+ }
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",
384
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
+ }
385
721
 
386
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 || {});
387
735
  var CONVERSION_MAP = {
388
736
  ["BITCOIN" /* BITCOIN */]: {
389
737
  ["BITCOIN" /* BITCOIN */]: (v) => v,
@@ -391,67 +739,298 @@ var CONVERSION_MAP = {
391
739
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
392
740
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
393
741
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
394
- ["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
+ )
395
747
  },
396
748
  ["MICROBITCOIN" /* MICROBITCOIN */]: {
397
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
749
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e6,
398
750
  ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
399
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
751
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e3,
400
752
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
401
753
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
402
- ["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
+ )
403
759
  },
404
760
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: {
405
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
761
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e3,
406
762
  ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
407
763
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
408
764
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
409
765
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
410
- ["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
+ )
411
771
  },
412
772
  ["MILLISATOSHI" /* MILLISATOSHI */]: {
413
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
414
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
415
- ["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,
416
776
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
417
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
418
- ["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
+ )
419
783
  },
420
784
  ["NANOBITCOIN" /* NANOBITCOIN */]: {
421
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
422
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
423
- ["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,
424
788
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
425
789
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
426
- ["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
+ )
427
795
  },
428
796
  ["SATOSHI" /* SATOSHI */]: {
429
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
430
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
431
- ["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,
432
800
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
433
801
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
434
- ["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
435
816
  }
436
817
  };
437
- var convertCurrencyAmount = (from, toUnit) => {
438
- 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 */) {
439
820
  throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
440
821
  }
441
- const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
822
+ if (fromUnit === toUnit) {
823
+ return amount;
824
+ }
825
+ const conversionFn = CONVERSION_MAP[fromUnit][toUnit];
442
826
  if (!conversionFn) {
443
827
  throw new LightsparkException_default(
444
828
  "CurrencyError",
445
- `Cannot convert from ${from.originalUnit} to ${toUnit}`
829
+ `Cannot convert from ${fromUnit} to ${toUnit}`
446
830
  );
447
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
+ );
448
840
  return {
449
841
  ...from,
450
842
  preferredCurrencyUnit: toUnit,
451
- preferredCurrencyValueApprox: conversionFn(from.originalValue),
452
- preferredCurrencyValueRounded: conversionFn(from.originalValue)
843
+ preferredCurrencyValueApprox: value,
844
+ preferredCurrencyValueRounded: value
453
845
  };
454
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
+ }
455
1034
 
456
1035
  // src/utils/errors.ts
457
1036
  var isError = (e) => {
@@ -474,20 +1053,6 @@ var isErrorMsg = (e, msg) => {
474
1053
  return false;
475
1054
  };
476
1055
 
477
- // src/utils/hex.ts
478
- var bytesToHex = (bytes) => {
479
- return bytes.reduce((acc, byte) => {
480
- return acc += ("0" + byte.toString(16)).slice(-2);
481
- }, "");
482
- };
483
- var hexToBytes = (hex) => {
484
- const bytes = [];
485
- for (let c = 0; c < hex.length; c += 2) {
486
- bytes.push(parseInt(hex.substr(c, 2), 16));
487
- }
488
- return Uint8Array.from(bytes);
489
- };
490
-
491
1056
  // ../../node_modules/lodash-es/_freeGlobal.js
492
1057
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
493
1058
  var freeGlobal_default = freeGlobal;
@@ -948,6 +1513,7 @@ var apiDomainForEnvironment = (environment) => {
948
1513
  var ServerEnvironment_default = ServerEnvironment;
949
1514
  // Annotate the CommonJS export names for ESM import in node:
950
1515
  0 && (module.exports = {
1516
+ CurrencyUnit,
951
1517
  DefaultCrypto,
952
1518
  KeyOrAlias,
953
1519
  LightsparkAuthException,
@@ -962,22 +1528,38 @@ var ServerEnvironment_default = ServerEnvironment;
962
1528
  SigningKey,
963
1529
  SigningKeyType,
964
1530
  StubAuthProvider,
1531
+ abbrCurrencyUnit,
965
1532
  apiDomainForEnvironment,
966
1533
  b64decode,
967
1534
  b64encode,
968
1535
  bytesToHex,
1536
+ clamp,
969
1537
  convertCurrencyAmount,
1538
+ convertCurrencyAmountValue,
1539
+ countryCodesToCurrencyCodes,
970
1540
  createSha256Hash,
1541
+ defaultCurrencyCode,
1542
+ formatCurrencyStr,
1543
+ getCurrentLocale,
971
1544
  getErrorMsg,
972
1545
  hexToBytes,
973
1546
  isBrowser,
1547
+ isCurrencyAmount,
1548
+ isCurrencyAmountObj,
1549
+ isCurrencyMap,
974
1550
  isError,
975
1551
  isErrorMsg,
976
1552
  isErrorWithMessage,
977
1553
  isNode,
1554
+ isNumber,
978
1555
  isTest,
979
1556
  isType,
1557
+ linearInterpolate,
1558
+ localeToCurrencyCode,
1559
+ localeToCurrencySymbol,
1560
+ mapCurrencyAmount,
980
1561
  pollUntil,
1562
+ round,
981
1563
  sleep,
982
1564
  urlsafe_b64decode
983
1565
  });