@lightsparkdev/core 1.0.2 → 1.0.4

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.js CHANGED
@@ -1,16 +1,23 @@
1
- // src/LightsparkException.ts
2
- var LightsparkException = class extends Error {
3
- code;
4
- message;
5
- extraInfo;
6
- constructor(code, message, extraInfo) {
7
- super(message);
8
- this.code = code;
9
- this.message = message;
10
- this.extraInfo = extraInfo;
11
- }
12
- };
13
- var LightsparkException_default = LightsparkException;
1
+ import {
2
+ LightsparkException_default,
3
+ b64decode,
4
+ b64encode,
5
+ bytesToHex,
6
+ convertCurrencyAmount,
7
+ createSha256Hash,
8
+ getErrorMsg,
9
+ hexToBytes,
10
+ isBrowser,
11
+ isError,
12
+ isErrorMsg,
13
+ isErrorWithMessage,
14
+ isNode,
15
+ isTest,
16
+ isType,
17
+ pollUntil,
18
+ sleep,
19
+ urlsafe_b64decode
20
+ } from "./chunk-J6VQYCQJ.js";
14
21
 
15
22
  // src/auth/LightsparkAuthException.ts
16
23
  var LightsparkAuthException = class extends LightsparkException_default {
@@ -33,49 +40,6 @@ var StubAuthProvider = class {
33
40
  }
34
41
  };
35
42
 
36
- // src/utils/base64.ts
37
- var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
38
- var Base64 = {
39
- btoa: (input = "") => {
40
- const str = input;
41
- let output = "";
42
- for (let block = 0, charCode, i = 0, map = chars; str.charAt(i | 0) || (map = "=", i % 1); output += map.charAt(63 & block >> 8 - i % 1 * 8)) {
43
- charCode = str.charCodeAt(i += 3 / 4);
44
- if (charCode > 255) {
45
- throw new Error(
46
- "'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
47
- );
48
- }
49
- block = block << 8 | charCode;
50
- }
51
- return output;
52
- },
53
- atob: (input = "") => {
54
- const str = input.replace(/=+$/, "");
55
- let output = "";
56
- if (str.length % 4 == 1) {
57
- throw new Error(
58
- "'atob' failed: The string to be decoded is not correctly encoded."
59
- );
60
- }
61
- for (let bc = 0, bs = 0, buffer, i = 0; buffer = str.charAt(i++); ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
62
- buffer = chars.indexOf(buffer);
63
- }
64
- return output;
65
- }
66
- };
67
- var b64decode = (encoded) => {
68
- return Uint8Array.from(Base64.atob(encoded), (c) => c.charCodeAt(0));
69
- };
70
- var urlsafe_b64decode = (encoded) => {
71
- return b64decode(encoded.replace(/_/g, "/").replace(/-/g, "+"));
72
- };
73
- var b64encode = (data) => {
74
- return Base64.btoa(
75
- String.fromCharCode.apply(null, Array.from(new Uint8Array(data)))
76
- );
77
- };
78
-
79
43
  // src/crypto/LightsparkSigningException.ts
80
44
  var LightsparkSigningException = class extends LightsparkException_default {
81
45
  constructor(message, extraInfo) {
@@ -397,9 +361,16 @@ var NodeKeyCache_default = NodeKeyCache;
397
361
  var Logger = class {
398
362
  context;
399
363
  loggingEnabled = false;
400
- constructor(loggerContext) {
364
+ constructor(loggerContext, getLoggingEnabled) {
401
365
  this.context = loggerContext;
402
- if (isBrowser) {
366
+ this.updateLoggingEnabled(getLoggingEnabled);
367
+ }
368
+ async updateLoggingEnabled(getLoggingEnabled) {
369
+ if (getLoggingEnabled) {
370
+ this.loggingEnabled = await getLoggingEnabled();
371
+ } else if (isTest) {
372
+ this.loggingEnabled = true;
373
+ } else if (isBrowser) {
403
374
  try {
404
375
  this.loggingEnabled = localStorage.getItem("lightspark-logging-enabled") === "1";
405
376
  } catch (e) {
@@ -424,12 +395,6 @@ import utc from "dayjs/plugin/utc.js";
424
395
  import { createClient } from "graphql-ws";
425
396
  import NodeWebSocket from "ws";
426
397
  import { Observable } from "zen-observable-ts";
427
-
428
- // src/utils/environment.ts
429
- var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
430
- var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
431
-
432
- // src/requester/Requester.ts
433
398
  var DEFAULT_BASE_URL = "api.lightspark.com";
434
399
  var LIGHTSPARK_BETA_HEADER_KEY = "X-Lightspark-Beta";
435
400
  var LIGHTSPARK_BETA_HEADER_VALUE = "z2h0BBYxTA83cjW7fi8QwWtBPCzkQKiemcuhKY08LOo";
@@ -468,13 +433,14 @@ var Requester = class {
468
433
  return query.constructObject(data);
469
434
  }
470
435
  subscribe(queryPayload, variables = {}) {
471
- logger.info(`Requester.subscribe params`, queryPayload, variables);
436
+ logger.info(`Requester.subscribe variables`, variables);
472
437
  const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
473
438
  const operationMatch = queryPayload.match(operationNameRegex);
474
439
  if (!operationMatch || operationMatch.length < 3) {
475
440
  throw new LightsparkException_default("InvalidQuery", "Invalid query payload");
476
441
  }
477
442
  const operationType = operationMatch[1];
443
+ logger.info(`Requester.subscribe operationType`, operationType);
478
444
  if (operationType == "mutation") {
479
445
  throw new LightsparkException_default(
480
446
  "InvalidQuery",
@@ -492,7 +458,6 @@ var Requester = class {
492
458
  variables,
493
459
  operationName: operation
494
460
  };
495
- logger.info(`Requester.subscribe bodyData`, bodyData);
496
461
  return new Observable((observer) => {
497
462
  logger.info(`Requester.subscribe observer`, observer);
498
463
  return this.wsClient.subscribe(bodyData, {
@@ -628,127 +593,6 @@ var apiDomainForEnvironment = (environment) => {
628
593
  }
629
594
  };
630
595
  var ServerEnvironment_default = ServerEnvironment;
631
-
632
- // src/utils/createHash.ts
633
- var createSha256Hash = async (data) => {
634
- if (isBrowser) {
635
- return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
636
- } else {
637
- const { createHash } = await import("crypto");
638
- const buffer = createHash("sha256").update(data).digest();
639
- return new Uint8Array(buffer);
640
- }
641
- };
642
-
643
- // src/utils/currency.ts
644
- var CONVERSION_MAP = {
645
- ["BITCOIN" /* BITCOIN */]: {
646
- ["BITCOIN" /* BITCOIN */]: (v) => v,
647
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e6,
648
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
649
- ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
650
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
651
- ["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
652
- },
653
- ["MICROBITCOIN" /* MICROBITCOIN */]: {
654
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
655
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
656
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
657
- ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
658
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
659
- ["SATOSHI" /* SATOSHI */]: (v) => v * 100
660
- },
661
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: {
662
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
663
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
664
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
665
- ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
666
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
667
- ["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
668
- },
669
- ["MILLISATOSHI" /* MILLISATOSHI */]: {
670
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
671
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
672
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e8),
673
- ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
674
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
675
- ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
676
- },
677
- ["NANOBITCOIN" /* NANOBITCOIN */]: {
678
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
679
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
680
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e6),
681
- ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
682
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
683
- ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
684
- },
685
- ["SATOSHI" /* SATOSHI */]: {
686
- ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
687
- ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
688
- ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e5),
689
- ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
690
- ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
691
- ["SATOSHI" /* SATOSHI */]: (v) => v
692
- }
693
- };
694
- var convertCurrencyAmount = (from, toUnit) => {
695
- if (from.originalUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || from.originalUnit === "USD" /* USD */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "USD" /* USD */) {
696
- throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
697
- }
698
- const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
699
- if (!conversionFn) {
700
- throw new LightsparkException_default(
701
- "CurrencyError",
702
- `Cannot convert from ${from.originalUnit} to ${toUnit}`
703
- );
704
- }
705
- return {
706
- ...from,
707
- preferredCurrencyUnit: toUnit,
708
- preferredCurrencyValueApprox: conversionFn(from.originalValue),
709
- preferredCurrencyValueRounded: conversionFn(from.originalValue)
710
- };
711
- };
712
-
713
- // src/utils/errors.ts
714
- var isError = (e) => {
715
- return Boolean(
716
- typeof e === "object" && e !== null && "name" in e && typeof e.name === "string" && "message" in e && typeof e.message === "string" && "stack" in e && (!e.stack || typeof e.stack === "string")
717
- );
718
- };
719
- var isErrorWithMessage = (e) => {
720
- return Boolean(
721
- typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
722
- );
723
- };
724
- var getErrorMsg = (e) => {
725
- return isErrorWithMessage(e) ? e.message : "Unknown error";
726
- };
727
- var isErrorMsg = (e, msg) => {
728
- if (isError(e)) {
729
- return e.message === msg;
730
- }
731
- return false;
732
- };
733
-
734
- // src/utils/hex.ts
735
- var bytesToHex = (bytes) => {
736
- return bytes.reduce((acc, byte) => {
737
- return acc += ("0" + byte.toString(16)).slice(-2);
738
- }, "");
739
- };
740
- var hexToBytes = (hex) => {
741
- const bytes = [];
742
- for (let c = 0; c < hex.length; c += 2) {
743
- bytes.push(parseInt(hex.substr(c, 2), 16));
744
- }
745
- return Uint8Array.from(bytes);
746
- };
747
-
748
- // src/utils/types.ts
749
- var isType = (typename) => (node) => {
750
- return node?.__typename === typename;
751
- };
752
596
  export {
753
597
  DefaultCrypto,
754
598
  KeyOrAlias,
@@ -777,6 +621,9 @@ export {
777
621
  isErrorMsg,
778
622
  isErrorWithMessage,
779
623
  isNode,
624
+ isTest,
780
625
  isType,
626
+ pollUntil,
627
+ sleep,
781
628
  urlsafe_b64decode
782
629
  };
@@ -0,0 +1,389 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/utils/index.ts
31
+ var utils_exports = {};
32
+ __export(utils_exports, {
33
+ b64decode: () => b64decode,
34
+ b64encode: () => b64encode,
35
+ bytesToHex: () => bytesToHex,
36
+ convertCurrencyAmount: () => convertCurrencyAmount,
37
+ createSha256Hash: () => createSha256Hash,
38
+ getErrorMsg: () => getErrorMsg,
39
+ hexToBytes: () => hexToBytes,
40
+ isBrowser: () => isBrowser,
41
+ isError: () => isError,
42
+ isErrorMsg: () => isErrorMsg,
43
+ isErrorWithMessage: () => isErrorWithMessage,
44
+ isNode: () => isNode,
45
+ isTest: () => isTest,
46
+ isType: () => isType,
47
+ pollUntil: () => pollUntil,
48
+ sleep: () => sleep,
49
+ urlsafe_b64decode: () => urlsafe_b64decode
50
+ });
51
+ module.exports = __toCommonJS(utils_exports);
52
+
53
+ // src/utils/base64.ts
54
+ var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
55
+ var Base64 = {
56
+ btoa: (input = "") => {
57
+ const str = input;
58
+ let output = "";
59
+ for (let block = 0, charCode, i = 0, map = chars; str.charAt(i | 0) || (map = "=", i % 1); output += map.charAt(63 & block >> 8 - i % 1 * 8)) {
60
+ charCode = str.charCodeAt(i += 3 / 4);
61
+ if (charCode > 255) {
62
+ throw new Error(
63
+ "'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
64
+ );
65
+ }
66
+ block = block << 8 | charCode;
67
+ }
68
+ return output;
69
+ },
70
+ atob: (input = "") => {
71
+ const str = input.replace(/=+$/, "");
72
+ let output = "";
73
+ if (str.length % 4 == 1) {
74
+ throw new Error(
75
+ "'atob' failed: The string to be decoded is not correctly encoded."
76
+ );
77
+ }
78
+ for (let bc = 0, bs = 0, buffer, i = 0; buffer = str.charAt(i++); ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
79
+ buffer = chars.indexOf(buffer);
80
+ }
81
+ return output;
82
+ }
83
+ };
84
+ var b64decode = (encoded) => {
85
+ return Uint8Array.from(Base64.atob(encoded), (c) => c.charCodeAt(0));
86
+ };
87
+ var urlsafe_b64decode = (encoded) => {
88
+ return b64decode(encoded.replace(/_/g, "/").replace(/-/g, "+"));
89
+ };
90
+ var b64encode = (data) => {
91
+ return Base64.btoa(
92
+ String.fromCharCode.apply(null, Array.from(new Uint8Array(data)))
93
+ );
94
+ };
95
+
96
+ // src/utils/environment.ts
97
+ var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
98
+ var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
99
+ var isTest = isNode && process.env.NODE_ENV === "test";
100
+
101
+ // src/utils/createHash.ts
102
+ var createSha256Hash = async (data) => {
103
+ if (isBrowser) {
104
+ return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
105
+ } else {
106
+ const { createHash } = await import("crypto");
107
+ const buffer = createHash("sha256").update(data).digest();
108
+ return new Uint8Array(buffer);
109
+ }
110
+ };
111
+
112
+ // src/LightsparkException.ts
113
+ var LightsparkException = class extends Error {
114
+ code;
115
+ message;
116
+ extraInfo;
117
+ constructor(code, message, extraInfo) {
118
+ super(message);
119
+ this.code = code;
120
+ this.message = message;
121
+ this.extraInfo = extraInfo;
122
+ }
123
+ };
124
+ var LightsparkException_default = LightsparkException;
125
+
126
+ // src/utils/currency.ts
127
+ var CONVERSION_MAP = {
128
+ ["BITCOIN" /* BITCOIN */]: {
129
+ ["BITCOIN" /* BITCOIN */]: (v) => v,
130
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e6,
131
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
132
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
133
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
134
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
135
+ },
136
+ ["MICROBITCOIN" /* MICROBITCOIN */]: {
137
+ ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
138
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
139
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
140
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
141
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
142
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 100
143
+ },
144
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: {
145
+ ["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
146
+ ["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
147
+ ["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
148
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
149
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
150
+ ["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
151
+ },
152
+ ["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),
156
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
157
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
158
+ ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
159
+ },
160
+ ["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),
164
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
165
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
166
+ ["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
167
+ },
168
+ ["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),
172
+ ["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
173
+ ["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
174
+ ["SATOSHI" /* SATOSHI */]: (v) => v
175
+ }
176
+ };
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 */) {
179
+ throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
180
+ }
181
+ const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
182
+ if (!conversionFn) {
183
+ throw new LightsparkException_default(
184
+ "CurrencyError",
185
+ `Cannot convert from ${from.originalUnit} to ${toUnit}`
186
+ );
187
+ }
188
+ return {
189
+ ...from,
190
+ preferredCurrencyUnit: toUnit,
191
+ preferredCurrencyValueApprox: conversionFn(from.originalValue),
192
+ preferredCurrencyValueRounded: conversionFn(from.originalValue)
193
+ };
194
+ };
195
+
196
+ // src/utils/errors.ts
197
+ var isError = (e) => {
198
+ return Boolean(
199
+ typeof e === "object" && e !== null && "name" in e && typeof e.name === "string" && "message" in e && typeof e.message === "string" && "stack" in e && (!e.stack || typeof e.stack === "string")
200
+ );
201
+ };
202
+ var isErrorWithMessage = (e) => {
203
+ return Boolean(
204
+ typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
205
+ );
206
+ };
207
+ var getErrorMsg = (e) => {
208
+ return isErrorWithMessage(e) ? e.message : "Unknown error";
209
+ };
210
+ var isErrorMsg = (e, msg) => {
211
+ if (isError(e)) {
212
+ return e.message === msg;
213
+ }
214
+ return false;
215
+ };
216
+
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
+ // ../../node_modules/lodash-es/_freeGlobal.js
232
+ var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
233
+ var freeGlobal_default = freeGlobal;
234
+
235
+ // ../../node_modules/lodash-es/_root.js
236
+ var freeSelf = typeof self == "object" && self && self.Object === Object && self;
237
+ var root = freeGlobal_default || freeSelf || Function("return this")();
238
+ var root_default = root;
239
+
240
+ // ../../node_modules/lodash-es/_Symbol.js
241
+ var Symbol2 = root_default.Symbol;
242
+ var Symbol_default = Symbol2;
243
+
244
+ // ../../node_modules/lodash-es/_getRawTag.js
245
+ var objectProto = Object.prototype;
246
+ var hasOwnProperty = objectProto.hasOwnProperty;
247
+ var nativeObjectToString = objectProto.toString;
248
+ var symToStringTag = Symbol_default ? Symbol_default.toStringTag : void 0;
249
+ function getRawTag(value) {
250
+ var isOwn = hasOwnProperty.call(value, symToStringTag), tag = value[symToStringTag];
251
+ try {
252
+ value[symToStringTag] = void 0;
253
+ var unmasked = true;
254
+ } catch (e) {
255
+ }
256
+ var result = nativeObjectToString.call(value);
257
+ if (unmasked) {
258
+ if (isOwn) {
259
+ value[symToStringTag] = tag;
260
+ } else {
261
+ delete value[symToStringTag];
262
+ }
263
+ }
264
+ return result;
265
+ }
266
+ var getRawTag_default = getRawTag;
267
+
268
+ // ../../node_modules/lodash-es/_objectToString.js
269
+ var objectProto2 = Object.prototype;
270
+ var nativeObjectToString2 = objectProto2.toString;
271
+ function objectToString(value) {
272
+ return nativeObjectToString2.call(value);
273
+ }
274
+ var objectToString_default = objectToString;
275
+
276
+ // ../../node_modules/lodash-es/_baseGetTag.js
277
+ var nullTag = "[object Null]";
278
+ var undefinedTag = "[object Undefined]";
279
+ var symToStringTag2 = Symbol_default ? Symbol_default.toStringTag : void 0;
280
+ function baseGetTag(value) {
281
+ if (value == null) {
282
+ return value === void 0 ? undefinedTag : nullTag;
283
+ }
284
+ return symToStringTag2 && symToStringTag2 in Object(value) ? getRawTag_default(value) : objectToString_default(value);
285
+ }
286
+ var baseGetTag_default = baseGetTag;
287
+
288
+ // ../../node_modules/lodash-es/isObject.js
289
+ function isObject(value) {
290
+ var type = typeof value;
291
+ return value != null && (type == "object" || type == "function");
292
+ }
293
+ var isObject_default = isObject;
294
+
295
+ // ../../node_modules/lodash-es/isFunction.js
296
+ var asyncTag = "[object AsyncFunction]";
297
+ var funcTag = "[object Function]";
298
+ var genTag = "[object GeneratorFunction]";
299
+ var proxyTag = "[object Proxy]";
300
+ function isFunction(value) {
301
+ if (!isObject_default(value)) {
302
+ return false;
303
+ }
304
+ var tag = baseGetTag_default(value);
305
+ return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
306
+ }
307
+ var isFunction_default = isFunction;
308
+
309
+ // src/utils/sleep.ts
310
+ function sleep(ms) {
311
+ return new Promise((resolve) => setTimeout(resolve, ms));
312
+ }
313
+
314
+ // src/utils/pollUntil.ts
315
+ function getDefaultMaxPollsError() {
316
+ return new Error("pollUntil: Max polls reached");
317
+ }
318
+ function pollUntil(asyncFn, getValue, maxPolls = 60, pollIntervalMs = 500, ignoreErrors = false, getMaxPollsError = getDefaultMaxPollsError) {
319
+ return new Promise((resolve, reject) => {
320
+ let polls = 0;
321
+ let stopPolling = false;
322
+ (async function() {
323
+ while (!stopPolling) {
324
+ polls += 1;
325
+ if (polls > maxPolls) {
326
+ stopPolling = true;
327
+ const maxPollsError = getMaxPollsError(maxPolls);
328
+ reject(maxPollsError);
329
+ break;
330
+ }
331
+ try {
332
+ const asyncResult = await asyncFn();
333
+ const result = getValue(asyncResult, {
334
+ stopPolling: false,
335
+ value: null
336
+ });
337
+ if (result.stopPolling) {
338
+ stopPolling = true;
339
+ resolve(result.value);
340
+ }
341
+ } catch (e) {
342
+ if (!ignoreErrors || isFunction_default(ignoreErrors) && !ignoreErrors(e)) {
343
+ stopPolling = true;
344
+ reject(e);
345
+ }
346
+ }
347
+ await sleep(pollIntervalMs);
348
+ }
349
+ })();
350
+ });
351
+ }
352
+
353
+ // src/utils/types.ts
354
+ var isType = (typename) => (node) => {
355
+ return node?.__typename === typename;
356
+ };
357
+ // Annotate the CommonJS export names for ESM import in node:
358
+ 0 && (module.exports = {
359
+ b64decode,
360
+ b64encode,
361
+ bytesToHex,
362
+ convertCurrencyAmount,
363
+ createSha256Hash,
364
+ getErrorMsg,
365
+ hexToBytes,
366
+ isBrowser,
367
+ isError,
368
+ isErrorMsg,
369
+ isErrorWithMessage,
370
+ isNode,
371
+ isTest,
372
+ isType,
373
+ pollUntil,
374
+ sleep,
375
+ urlsafe_b64decode
376
+ });
377
+ /*! Bundled license information:
378
+
379
+ lodash-es/lodash.js:
380
+ (**
381
+ * @license
382
+ * Lodash (Custom Build) <https://lodash.com/>
383
+ * Build: `lodash modularize exports="es" -o ./`
384
+ * Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
385
+ * Released under MIT license <https://lodash.com/license>
386
+ * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
387
+ * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
388
+ *)
389
+ */