@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.
@@ -30,21 +30,38 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/utils/index.ts
31
31
  var utils_exports = {};
32
32
  __export(utils_exports, {
33
+ CurrencyUnit: () => CurrencyUnit,
34
+ abbrCurrencyUnit: () => abbrCurrencyUnit,
33
35
  b64decode: () => b64decode,
34
36
  b64encode: () => b64encode,
35
37
  bytesToHex: () => bytesToHex,
38
+ clamp: () => clamp,
36
39
  convertCurrencyAmount: () => convertCurrencyAmount,
40
+ convertCurrencyAmountValue: () => convertCurrencyAmountValue,
41
+ countryCodesToCurrencyCodes: () => countryCodesToCurrencyCodes,
37
42
  createSha256Hash: () => createSha256Hash,
43
+ defaultCurrencyCode: () => defaultCurrencyCode,
44
+ formatCurrencyStr: () => formatCurrencyStr,
45
+ getCurrentLocale: () => getCurrentLocale,
38
46
  getErrorMsg: () => getErrorMsg,
39
47
  hexToBytes: () => hexToBytes,
40
48
  isBrowser: () => isBrowser,
49
+ isCurrencyAmount: () => isCurrencyAmount,
50
+ isCurrencyAmountObj: () => isCurrencyAmountObj,
51
+ isCurrencyMap: () => isCurrencyMap,
41
52
  isError: () => isError,
42
53
  isErrorMsg: () => isErrorMsg,
43
54
  isErrorWithMessage: () => isErrorWithMessage,
44
55
  isNode: () => isNode,
56
+ isNumber: () => isNumber,
45
57
  isTest: () => isTest,
46
58
  isType: () => isType,
59
+ linearInterpolate: () => linearInterpolate,
60
+ localeToCurrencyCode: () => localeToCurrencyCode,
61
+ localeToCurrencySymbol: () => localeToCurrencySymbol,
62
+ mapCurrencyAmount: () => mapCurrencyAmount,
47
63
  pollUntil: () => pollUntil,
64
+ round: () => round,
48
65
  sleep: () => sleep,
49
66
  urlsafe_b64decode: () => urlsafe_b64decode
50
67
  });
@@ -98,16 +115,40 @@ var isBrowser = typeof window !== "undefined" && typeof window.document !== "und
98
115
  var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
99
116
  var isTest = isNode && process.env.NODE_ENV === "test";
100
117
 
118
+ // src/utils/hex.ts
119
+ var bytesToHex = (bytes) => {
120
+ return bytes.reduce((acc, byte) => {
121
+ return acc += ("0" + byte.toString(16)).slice(-2);
122
+ }, "");
123
+ };
124
+ var hexToBytes = (hex) => {
125
+ const bytes = [];
126
+ for (let c = 0; c < hex.length; c += 2) {
127
+ bytes.push(parseInt(hex.substr(c, 2), 16));
128
+ }
129
+ return Uint8Array.from(bytes);
130
+ };
131
+
101
132
  // src/utils/createHash.ts
102
- var createSha256Hash = async (data) => {
133
+ async function createSha256Hash(data, asHex) {
103
134
  if (isBrowser) {
104
- return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
135
+ const source = typeof data === "string" ? new TextEncoder().encode(data) : data;
136
+ const buffer = await window.crypto.subtle.digest("SHA-256", source);
137
+ const arr = new Uint8Array(buffer);
138
+ if (asHex) {
139
+ return bytesToHex(arr);
140
+ }
141
+ return arr;
105
142
  } else {
106
143
  const { createHash } = await import("crypto");
144
+ if (asHex) {
145
+ const hexStr = createHash("sha256").update(data).digest("hex");
146
+ return hexStr;
147
+ }
107
148
  const buffer = createHash("sha256").update(data).digest();
108
149
  return new Uint8Array(buffer);
109
150
  }
110
- };
151
+ }
111
152
 
112
153
  // src/LightsparkException.ts
113
154
  var LightsparkException = class extends Error {
@@ -123,7 +164,314 @@ var LightsparkException = class extends Error {
123
164
  };
124
165
  var LightsparkException_default = LightsparkException;
125
166
 
167
+ // src/utils/locale.ts
168
+ function getCurrentLocale() {
169
+ return Intl.NumberFormat().resolvedOptions().locale;
170
+ }
171
+
172
+ // src/utils/localeToCurrencyCodes.ts
173
+ var countryCodesToCurrencyCodes = {
174
+ AD: "EUR",
175
+ // AE: "AED",
176
+ // AF: "AFN",
177
+ // AG: "XCD",
178
+ // AI: "XCD",
179
+ // AL: "ALL",
180
+ // AM: "AMD",
181
+ // AO: "AOA",
182
+ AR: "ARS",
183
+ AS: "USD",
184
+ AT: "EUR",
185
+ AU: "AUD",
186
+ // AW: "AWG",
187
+ AX: "EUR",
188
+ // AZ: "AZN",
189
+ // BA: "BAM",
190
+ // BB: "BBD",
191
+ // BD: "BDT",
192
+ BE: "EUR",
193
+ // BF: "XOF",
194
+ // BG: "BGN",
195
+ // BH: "BHD",
196
+ // BI: "BIF",
197
+ // BJ: "XOF",
198
+ BL: "EUR",
199
+ // BM: "BMD",
200
+ // BN: "BND",
201
+ // BO: "BOB",
202
+ BQ: "USD",
203
+ BR: "BRL",
204
+ // BS: "BSD",
205
+ // BT: "BTN",
206
+ // BV: "NOK",
207
+ // BW: "BWP",
208
+ // BY: "BYN",
209
+ // BZ: "BZD",
210
+ CA: "CAD",
211
+ // CC: "AUD",
212
+ // CD: "CDF",
213
+ // CF: "XAF",
214
+ // CG: "XAF",
215
+ // CH: "CHF",
216
+ // CI: "XOF",
217
+ // CK: "NZD",
218
+ // CL: "CLP",
219
+ // CM: "XAF",
220
+ // CN: "CNY",
221
+ CO: "COP",
222
+ // CR: "CRC",
223
+ // CU: "CUP",
224
+ // CV: "CVE",
225
+ // CW: "ANG",
226
+ // CX: "AUD",
227
+ CY: "EUR",
228
+ // CZ: "CZK",
229
+ DE: "EUR",
230
+ // DJ: "DJF",
231
+ // DK: "DKK",
232
+ // DM: "XCD",
233
+ // DO: "DOP",
234
+ // DZ: "DZD",
235
+ EC: "USD",
236
+ EE: "EUR",
237
+ // EG: "EGP",
238
+ // EH: "MAD",
239
+ // ER: "ERN",
240
+ ES: "EUR",
241
+ // ET: "ETB",
242
+ FI: "EUR",
243
+ // FJ: "FJD",
244
+ // FK: "FKP",
245
+ FM: "USD",
246
+ // FO: "DKK",
247
+ FR: "EUR",
248
+ // GA: "XAF",
249
+ GB: "GBP",
250
+ // GD: "XCD",
251
+ // GE: "GEL",
252
+ GF: "EUR",
253
+ GG: "GBP",
254
+ // GH: "GHS",
255
+ // GI: "GIP",
256
+ // GL: "DKK",
257
+ // GM: "GMD",
258
+ // GN: "GNF",
259
+ GP: "EUR",
260
+ // GQ: "XAF",
261
+ GR: "EUR",
262
+ GS: "GBP",
263
+ // GT: "GTQ",
264
+ GU: "USD",
265
+ // GW: "XOF",
266
+ // GY: "GYD",
267
+ // HK: "HKD",
268
+ // HM: "AUD",
269
+ // HN: "HNL",
270
+ // HR: "HRK",
271
+ // HT: "HTG",
272
+ // HU: "HUF",
273
+ // ID: "IDR",
274
+ IE: "EUR",
275
+ // IL: "ILS",
276
+ IM: "GBP",
277
+ IN: "INR",
278
+ IO: "USD",
279
+ // IQ: "IQD",
280
+ // IR: "IRR",
281
+ // IS: "ISK",
282
+ IT: "EUR",
283
+ JE: "GBP",
284
+ // JM: "JMD",
285
+ // JO: "JOD",
286
+ // JP: "JPY",
287
+ // KE: "KES",
288
+ // KG: "KGS",
289
+ // KH: "KHR",
290
+ // KI: "AUD",
291
+ // KM: "KMF",
292
+ // KN: "XCD",
293
+ // KP: "KPW",
294
+ // KR: "KRW",
295
+ // KW: "KWD",
296
+ // KY: "KYD",
297
+ // KZ: "KZT",
298
+ // LA: "LAK",
299
+ // LB: "LBP",
300
+ // LC: "XCD",
301
+ // LI: "CHF",
302
+ // LK: "LKR",
303
+ // LR: "LRD",
304
+ // LS: "LSL",
305
+ LT: "EUR",
306
+ LU: "EUR",
307
+ LV: "EUR",
308
+ // LY: "LYD",
309
+ // MA: "MAD",
310
+ MC: "EUR",
311
+ // MD: "MDL",
312
+ ME: "EUR",
313
+ MF: "EUR",
314
+ // MG: "MGA",
315
+ MH: "USD",
316
+ // MK: "MKD",
317
+ // ML: "XOF",
318
+ // MM: "MMK",
319
+ // MN: "MNT",
320
+ // MO: "MOP",
321
+ MP: "USD",
322
+ MQ: "EUR",
323
+ // MR: "MRO",
324
+ // MS: "XCD",
325
+ MT: "EUR",
326
+ // MU: "MUR",
327
+ // MV: "MVR",
328
+ // MW: "MWK",
329
+ MX: "MXN",
330
+ // MY: "MYR",
331
+ // MZ: "MZN",
332
+ // NA: "NAD",
333
+ // NC: "XPF",
334
+ // NE: "XOF",
335
+ NF: "AUD",
336
+ // NG: "NGN",
337
+ // NI: "NIO",
338
+ NL: "EUR",
339
+ // NO: "NOK",
340
+ // NP: "NPR",
341
+ NR: "AUD",
342
+ // NU: "NZD",
343
+ // NZ: "NZD",
344
+ // OM: "OMR",
345
+ // PA: "PAB",
346
+ // PE: "PEN",
347
+ // PF: "XPF",
348
+ // PG: "PGK",
349
+ // PH: "PHP",
350
+ // PK: "PKR",
351
+ // PL: "PLN",
352
+ PM: "EUR",
353
+ // PN: "NZD",
354
+ PR: "USD",
355
+ // PS: "ILS",
356
+ PT: "EUR",
357
+ PW: "USD",
358
+ // PY: "PYG",
359
+ // QA: "QAR",
360
+ RE: "EUR",
361
+ // RO: "RON",
362
+ // RS: "RSD",
363
+ // RU: "RUB",
364
+ // RW: "RWF",
365
+ // SA: "SAR",
366
+ // SB: "SBD",
367
+ // SC: "SCR",
368
+ // SD: "SDG",
369
+ // SE: "SEK",
370
+ // SG: "SGD",
371
+ // SH: "SHP",
372
+ SI: "EUR",
373
+ // SJ: "NOK",
374
+ SK: "EUR",
375
+ // SL: "SLL",
376
+ SM: "EUR",
377
+ // SN: "XOF",
378
+ // SO: "SOS",
379
+ // SR: "SRD",
380
+ // ST: "STD",
381
+ // SV: "SVC",
382
+ // SX: "ANG",
383
+ // SY: "SYP",
384
+ // SZ: "SZL",
385
+ TC: "USD",
386
+ // TD: "XAF",
387
+ TF: "EUR",
388
+ // TG: "XOF",
389
+ // TH: "THB",
390
+ // TJ: "TJS",
391
+ // TK: "NZD",
392
+ TL: "USD",
393
+ // TM: "TMT",
394
+ // TN: "TND",
395
+ // TO: "TOP",
396
+ // TR: "TRY",
397
+ // TT: "TTD",
398
+ TV: "AUD",
399
+ // TW: "TWD",
400
+ // TZ: "TZS",
401
+ // UA: "UAH",
402
+ // UG: "UGX",
403
+ UM: "USD",
404
+ US: "USD",
405
+ // UY: "UYU",
406
+ // UZ: "UZS",
407
+ VA: "EUR",
408
+ // VC: "XCD",
409
+ // VE: "VEF",
410
+ VG: "USD",
411
+ VI: "USD",
412
+ // VN: "VND",
413
+ // VU: "VUV",
414
+ // WF: "XPF",
415
+ // WS: "WST",
416
+ // YE: "YER",
417
+ YT: "EUR"
418
+ // ZA: "ZAR",
419
+ // ZM: "ZMW",
420
+ // ZW: "ZWL",
421
+ };
422
+ function getCountryCode(localeString) {
423
+ let components = localeString.split("_");
424
+ if (components.length === 2) {
425
+ return components.pop();
426
+ }
427
+ components = localeString.split("-");
428
+ if (components.length === 2) {
429
+ return components.pop();
430
+ }
431
+ return localeString;
432
+ }
433
+ function localeToCurrencyCode(locale) {
434
+ const countryCode = getCountryCode(locale);
435
+ return countryCodesToCurrencyCodes[countryCode];
436
+ }
437
+
438
+ // src/utils/numbers.ts
439
+ function clamp(val, min, max) {
440
+ return val > max ? max : val < min ? min : val;
441
+ }
442
+ function linearInterpolate(value, fromRangeStart, fromRangeEnd, toRangeStart, toRangeEnd) {
443
+ const fromRangeMax = fromRangeEnd - fromRangeStart;
444
+ const fromRangeVal = value - fromRangeStart;
445
+ const toRangeMax = toRangeEnd - toRangeStart;
446
+ const val = fromRangeVal / fromRangeMax * toRangeMax + toRangeStart;
447
+ return clamp(
448
+ val,
449
+ Math.min(toRangeStart, toRangeEnd),
450
+ Math.max(toRangeStart, toRangeEnd)
451
+ );
452
+ }
453
+ function round(num, decimalPlaces = 0) {
454
+ const p = Math.pow(10, decimalPlaces);
455
+ const n = num * p * (1 + Number.EPSILON);
456
+ return Math.round(n) / p;
457
+ }
458
+ function isNumber(value) {
459
+ return typeof value === "number";
460
+ }
461
+
126
462
  // src/utils/currency.ts
463
+ var defaultCurrencyCode = "USD";
464
+ var CurrencyUnit = /* @__PURE__ */ ((CurrencyUnit2) => {
465
+ CurrencyUnit2["FUTURE_VALUE"] = "FUTURE_VALUE";
466
+ CurrencyUnit2["BITCOIN"] = "BITCOIN";
467
+ CurrencyUnit2["SATOSHI"] = "SATOSHI";
468
+ CurrencyUnit2["MILLISATOSHI"] = "MILLISATOSHI";
469
+ CurrencyUnit2["USD"] = "USD";
470
+ CurrencyUnit2["NANOBITCOIN"] = "NANOBITCOIN";
471
+ CurrencyUnit2["MICROBITCOIN"] = "MICROBITCOIN";
472
+ CurrencyUnit2["MILLIBITCOIN"] = "MILLIBITCOIN";
473
+ return CurrencyUnit2;
474
+ })(CurrencyUnit || {});
127
475
  var CONVERSION_MAP = {
128
476
  ["BITCOIN" /* BITCOIN */]: {
129
477
  ["BITCOIN" /* BITCOIN */]: (v) => v,
@@ -131,67 +479,298 @@ var CONVERSION_MAP = {
131
479
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
132
480
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
133
481
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
134
- ["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
482
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 1e8,
483
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
484
+ /* Round without decimals since we're returning cents: */
485
+ round(v * centsPerBtc, 2)
486
+ )
135
487
  },
136
488
  ["MICROBITCOIN" /* MICROBITCOIN */]: {
137
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
489
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e6,
138
490
  ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
139
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
491
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e3,
140
492
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
141
493
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
142
- ["SATOSHI" /* SATOSHI */]: (v) => v * 100
494
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 100,
495
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
496
+ /* Round without decimals since we're returning cents: */
497
+ round(v / 1e6 * centsPerBtc)
498
+ )
143
499
  },
144
500
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: {
145
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
501
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e3,
146
502
  ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
147
503
  ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
148
504
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
149
505
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
150
- ["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
506
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 1e5,
507
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
508
+ /* Round without decimals since we're returning cents: */
509
+ round(v / 1e3 * centsPerBtc)
510
+ )
151
511
  },
152
512
  ["MILLISATOSHI" /* MILLISATOSHI */]: {
153
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
154
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
155
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e8),
513
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e11,
514
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v / 1e5,
515
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e8,
156
516
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
157
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
158
- ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
517
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v / 100,
518
+ ["SATOSHI" /* SATOSHI */]: (v) => v / 1e3,
519
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
520
+ /* Round without decimals since we're returning cents: */
521
+ round(v / 1e11 * centsPerBtc)
522
+ )
159
523
  },
160
524
  ["NANOBITCOIN" /* NANOBITCOIN */]: {
161
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
162
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
163
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e6),
525
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e9,
526
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v / 1e3,
527
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e6,
164
528
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
165
529
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
166
- ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
530
+ ["SATOSHI" /* SATOSHI */]: (v) => v / 10,
531
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
532
+ /* Round without decimals since we're returning cents: */
533
+ round(v / 1e9 * centsPerBtc)
534
+ )
167
535
  },
168
536
  ["SATOSHI" /* SATOSHI */]: {
169
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
170
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
171
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e5),
537
+ ["BITCOIN" /* BITCOIN */]: (v) => v / 1e8,
538
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v / 100,
539
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v / 1e5,
172
540
  ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
173
541
  ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
174
- ["SATOSHI" /* SATOSHI */]: (v) => v
542
+ ["SATOSHI" /* SATOSHI */]: (v) => v,
543
+ ["USD" /* USD */]: (v, centsPerBtc = 1) => (
544
+ /* Round without decimals since we're returning cents: */
545
+ round(v / 1e8 * centsPerBtc)
546
+ )
547
+ },
548
+ ["USD" /* USD */]: {
549
+ ["BITCOIN" /* BITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc,
550
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e6,
551
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e3,
552
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e11,
553
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e9,
554
+ ["SATOSHI" /* SATOSHI */]: (v, centsPerBtc = 1) => v / centsPerBtc * 1e8,
555
+ ["USD" /* USD */]: (v) => v
175
556
  }
176
557
  };
177
- var convertCurrencyAmount = (from, toUnit) => {
178
- if (from.originalUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || from.originalUnit === "USD" /* USD */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "USD" /* USD */) {
558
+ function convertCurrencyAmountValue(fromUnit, toUnit, amount, centsPerBtc = 1) {
559
+ if (fromUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */) {
179
560
  throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
180
561
  }
181
- const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
562
+ if (fromUnit === toUnit) {
563
+ return amount;
564
+ }
565
+ const conversionFn = CONVERSION_MAP[fromUnit][toUnit];
182
566
  if (!conversionFn) {
183
567
  throw new LightsparkException_default(
184
568
  "CurrencyError",
185
- `Cannot convert from ${from.originalUnit} to ${toUnit}`
569
+ `Cannot convert from ${fromUnit} to ${toUnit}`
186
570
  );
187
571
  }
572
+ return conversionFn(amount, centsPerBtc);
573
+ }
574
+ var convertCurrencyAmount = (from, toUnit) => {
575
+ const value = convertCurrencyAmountValue(
576
+ from.originalUnit,
577
+ toUnit,
578
+ from.originalValue
579
+ );
188
580
  return {
189
581
  ...from,
190
582
  preferredCurrencyUnit: toUnit,
191
- preferredCurrencyValueApprox: conversionFn(from.originalValue),
192
- preferredCurrencyValueRounded: conversionFn(from.originalValue)
583
+ preferredCurrencyValueApprox: value,
584
+ preferredCurrencyValueRounded: value
585
+ };
586
+ };
587
+ function isCurrencyAmountObj(arg) {
588
+ return typeof arg === "object" && arg !== null && "value" in arg && "unit" in arg;
589
+ }
590
+ function isCurrencyAmount(arg) {
591
+ return typeof arg === "object" && arg !== null && "originalValue" in arg && "originalUnit" in arg && "preferredCurrencyUnit" in arg && "preferredCurrencyValueRounded" in arg && "preferredCurrencyValueApprox" in arg;
592
+ }
593
+ function asNumber(value) {
594
+ if (typeof value === "string") {
595
+ return Number(value);
596
+ }
597
+ return value || 0;
598
+ }
599
+ function getCurrencyAmount(currencyAmountArg) {
600
+ let value = 0;
601
+ let unit = void 0;
602
+ if (isCurrencyAmountObj(currencyAmountArg)) {
603
+ value = asNumber(currencyAmountArg.value);
604
+ unit = currencyAmountArg.unit;
605
+ } else if (isCurrencyAmount(currencyAmountArg)) {
606
+ value = currencyAmountArg.originalValue;
607
+ unit = currencyAmountArg.originalUnit;
608
+ }
609
+ return {
610
+ value: asNumber(value),
611
+ unit: unit || "SATOSHI" /* SATOSHI */
612
+ };
613
+ }
614
+ function mapCurrencyAmount(currencyAmountArg, centsPerBtc = 1) {
615
+ const { value, unit } = getCurrencyAmount(currencyAmountArg);
616
+ const convert = convertCurrencyAmountValue;
617
+ const sats = convert(unit, "SATOSHI" /* SATOSHI */, value, centsPerBtc);
618
+ const btc = convert(unit, "BITCOIN" /* BITCOIN */, value, centsPerBtc);
619
+ const msats = convert(unit, "MILLISATOSHI" /* MILLISATOSHI */, value, centsPerBtc);
620
+ const usd = convert(unit, "USD" /* USD */, value, centsPerBtc);
621
+ const mibtc = convert(unit, "MICROBITCOIN" /* MICROBITCOIN */, value, centsPerBtc);
622
+ const mlbtc = convert(unit, "MILLIBITCOIN" /* MILLIBITCOIN */, value, centsPerBtc);
623
+ const nbtc = convert(unit, "NANOBITCOIN" /* NANOBITCOIN */, value, centsPerBtc);
624
+ const mapWithCurrencyUnits = {
625
+ ["BITCOIN" /* BITCOIN */]: btc,
626
+ ["SATOSHI" /* SATOSHI */]: sats,
627
+ ["MILLISATOSHI" /* MILLISATOSHI */]: msats,
628
+ ["USD" /* USD */]: usd,
629
+ ["MICROBITCOIN" /* MICROBITCOIN */]: mibtc,
630
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: mlbtc,
631
+ ["NANOBITCOIN" /* NANOBITCOIN */]: nbtc,
632
+ ["FUTURE_VALUE" /* FUTURE_VALUE */]: NaN,
633
+ formatted: {
634
+ ["BITCOIN" /* BITCOIN */]: formatCurrencyStr({
635
+ value: btc,
636
+ unit: "BITCOIN" /* BITCOIN */
637
+ }),
638
+ ["SATOSHI" /* SATOSHI */]: formatCurrencyStr({
639
+ value: sats,
640
+ unit: "SATOSHI" /* SATOSHI */
641
+ }),
642
+ ["MILLISATOSHI" /* MILLISATOSHI */]: formatCurrencyStr({
643
+ value: msats,
644
+ unit: "MILLISATOSHI" /* MILLISATOSHI */
645
+ }),
646
+ ["MICROBITCOIN" /* MICROBITCOIN */]: formatCurrencyStr({
647
+ value: mibtc,
648
+ unit: "MICROBITCOIN" /* MICROBITCOIN */
649
+ }),
650
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: formatCurrencyStr({
651
+ value: mlbtc,
652
+ unit: "MILLIBITCOIN" /* MILLIBITCOIN */
653
+ }),
654
+ ["NANOBITCOIN" /* NANOBITCOIN */]: formatCurrencyStr({
655
+ value: nbtc,
656
+ unit: "NANOBITCOIN" /* NANOBITCOIN */
657
+ }),
658
+ ["USD" /* USD */]: formatCurrencyStr({
659
+ value: usd,
660
+ unit: "USD" /* USD */
661
+ }),
662
+ ["FUTURE_VALUE" /* FUTURE_VALUE */]: "-"
663
+ }
193
664
  };
665
+ return {
666
+ ...mapWithCurrencyUnits,
667
+ btc,
668
+ sats,
669
+ msats,
670
+ isZero: msats === 0,
671
+ isLessThan: (other) => {
672
+ if (isNumber(other)) {
673
+ return msats < other;
674
+ }
675
+ if (isCurrencyAmountObj(other)) {
676
+ other = mapCurrencyAmount(other);
677
+ }
678
+ return msats < other.msats;
679
+ },
680
+ isGreaterThan: (other) => {
681
+ if (isNumber(other)) {
682
+ return msats > other;
683
+ }
684
+ if (isCurrencyAmountObj(other)) {
685
+ other = mapCurrencyAmount(other);
686
+ }
687
+ return msats > other.msats;
688
+ },
689
+ isEqualTo: (other) => {
690
+ if (isNumber(other)) {
691
+ return msats === other;
692
+ }
693
+ if (isCurrencyAmountObj(other)) {
694
+ other = mapCurrencyAmount(other);
695
+ }
696
+ return msats === other.msats;
697
+ },
698
+ formatted: {
699
+ ...mapWithCurrencyUnits.formatted,
700
+ btc: mapWithCurrencyUnits.formatted["BITCOIN" /* BITCOIN */],
701
+ sats: mapWithCurrencyUnits.formatted["SATOSHI" /* SATOSHI */],
702
+ msats: mapWithCurrencyUnits.formatted["MILLISATOSHI" /* MILLISATOSHI */]
703
+ },
704
+ type: "CurrencyMap"
705
+ };
706
+ }
707
+ var isCurrencyMap = (currencyMap) => typeof currencyMap === "object" && currencyMap !== null && "type" in currencyMap && typeof currencyMap.type === "string" && currencyMap.type === "CurrencyMap";
708
+ var abbrCurrencyUnit = (unit) => {
709
+ switch (unit) {
710
+ case "BITCOIN" /* BITCOIN */:
711
+ return "BTC";
712
+ case "SATOSHI" /* SATOSHI */:
713
+ return "SAT";
714
+ case "MILLISATOSHI" /* MILLISATOSHI */:
715
+ return "MSAT";
716
+ case "USD" /* USD */:
717
+ return "USD";
718
+ }
719
+ return "Unsupported CurrencyUnit";
194
720
  };
721
+ function formatCurrencyStr(amount, maxFractionDigits, compact, showBtcSymbol = false, options = {}) {
722
+ const currencyAmount = getCurrencyAmount(amount);
723
+ let { value: num } = currencyAmount;
724
+ const { unit } = currencyAmount;
725
+ if (unit === "USD" /* USD */) {
726
+ num = num / 100;
727
+ }
728
+ function getDefaultMaxFractionDigits(defaultDigits) {
729
+ return typeof maxFractionDigits === "undefined" ? compact ? 1 : defaultDigits : maxFractionDigits;
730
+ }
731
+ const symbol = !showBtcSymbol ? "" : unit === "BITCOIN" /* BITCOIN */ ? "\uE903" : unit === "SATOSHI" /* SATOSHI */ ? "\uE902" : "";
732
+ const currentLocale = getCurrentLocale();
733
+ switch (unit) {
734
+ case "BITCOIN" /* BITCOIN */:
735
+ return `${symbol}${num.toLocaleString(currentLocale, {
736
+ notation: compact ? "compact" : void 0,
737
+ maximumFractionDigits: getDefaultMaxFractionDigits(4),
738
+ ...options
739
+ })}`;
740
+ case "MILLISATOSHI" /* MILLISATOSHI */:
741
+ case "SATOSHI" /* SATOSHI */:
742
+ case "MICROBITCOIN" /* MICROBITCOIN */:
743
+ case "MILLIBITCOIN" /* MILLIBITCOIN */:
744
+ case "NANOBITCOIN" /* NANOBITCOIN */:
745
+ default:
746
+ return `${symbol}${num.toLocaleString(currentLocale, {
747
+ notation: compact ? "compact" : void 0,
748
+ maximumFractionDigits: getDefaultMaxFractionDigits(0),
749
+ ...options
750
+ })}`;
751
+ case "USD" /* USD */:
752
+ return num.toLocaleString(currentLocale, {
753
+ style: "currency",
754
+ currency: defaultCurrencyCode,
755
+ notation: compact ? "compact" : void 0,
756
+ maximumFractionDigits: getDefaultMaxFractionDigits(2),
757
+ ...options
758
+ });
759
+ }
760
+ }
761
+ function localeToCurrencySymbol(locale) {
762
+ const currencyCode = localeToCurrencyCode(locale);
763
+ const formatted = new Intl.NumberFormat(locale, {
764
+ style: "currency",
765
+ currency: currencyCode,
766
+ useGrouping: false,
767
+ // to avoid thousands separators
768
+ minimumFractionDigits: 0,
769
+ maximumFractionDigits: 0
770
+ }).format(0);
771
+ const symbol = formatted.replace(/[0-9\s\u00a0]/g, "");
772
+ return symbol;
773
+ }
195
774
 
196
775
  // src/utils/errors.ts
197
776
  var isError = (e) => {
@@ -214,20 +793,6 @@ var isErrorMsg = (e, msg) => {
214
793
  return false;
215
794
  };
216
795
 
217
- // src/utils/hex.ts
218
- var bytesToHex = (bytes) => {
219
- return bytes.reduce((acc, byte) => {
220
- return acc += ("0" + byte.toString(16)).slice(-2);
221
- }, "");
222
- };
223
- var hexToBytes = (hex) => {
224
- const bytes = [];
225
- for (let c = 0; c < hex.length; c += 2) {
226
- bytes.push(parseInt(hex.substr(c, 2), 16));
227
- }
228
- return Uint8Array.from(bytes);
229
- };
230
-
231
796
  // ../../node_modules/lodash-es/_freeGlobal.js
232
797
  var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
233
798
  var freeGlobal_default = freeGlobal;
@@ -356,21 +921,38 @@ var isType = (typename) => (node) => {
356
921
  };
357
922
  // Annotate the CommonJS export names for ESM import in node:
358
923
  0 && (module.exports = {
924
+ CurrencyUnit,
925
+ abbrCurrencyUnit,
359
926
  b64decode,
360
927
  b64encode,
361
928
  bytesToHex,
929
+ clamp,
362
930
  convertCurrencyAmount,
931
+ convertCurrencyAmountValue,
932
+ countryCodesToCurrencyCodes,
363
933
  createSha256Hash,
934
+ defaultCurrencyCode,
935
+ formatCurrencyStr,
936
+ getCurrentLocale,
364
937
  getErrorMsg,
365
938
  hexToBytes,
366
939
  isBrowser,
940
+ isCurrencyAmount,
941
+ isCurrencyAmountObj,
942
+ isCurrencyMap,
367
943
  isError,
368
944
  isErrorMsg,
369
945
  isErrorWithMessage,
370
946
  isNode,
947
+ isNumber,
371
948
  isTest,
372
949
  isType,
950
+ linearInterpolate,
951
+ localeToCurrencyCode,
952
+ localeToCurrencySymbol,
953
+ mapCurrencyAmount,
373
954
  pollUntil,
955
+ round,
374
956
  sleep,
375
957
  urlsafe_b64decode
376
958
  });