@lightsparkdev/core 1.0.3 → 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/CHANGELOG.md +7 -0
- package/dist/chunk-J6VQYCQJ.js +338 -0
- package/dist/index.cjs +273 -129
- package/dist/index.d.ts +2 -77
- package/dist/index.js +27 -185
- package/dist/utils/index.cjs +389 -0
- package/dist/utils/index.d.ts +88 -0
- package/dist/utils/index.js +38 -0
- package/package.json +16 -2
- package/src/Logger.ts +3 -1
- package/src/crypto/SigningKey.ts +2 -6
- package/src/requester/Requester.ts +2 -2
- package/src/utils/environment.ts +2 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/pollUntil.ts +54 -0
- package/src/utils/sleep.ts +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// src/utils/base64.ts
|
|
2
|
+
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
3
|
+
var Base64 = {
|
|
4
|
+
btoa: (input = "") => {
|
|
5
|
+
const str = input;
|
|
6
|
+
let output = "";
|
|
7
|
+
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)) {
|
|
8
|
+
charCode = str.charCodeAt(i += 3 / 4);
|
|
9
|
+
if (charCode > 255) {
|
|
10
|
+
throw new Error(
|
|
11
|
+
"'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
block = block << 8 | charCode;
|
|
15
|
+
}
|
|
16
|
+
return output;
|
|
17
|
+
},
|
|
18
|
+
atob: (input = "") => {
|
|
19
|
+
const str = input.replace(/=+$/, "");
|
|
20
|
+
let output = "";
|
|
21
|
+
if (str.length % 4 == 1) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
"'atob' failed: The string to be decoded is not correctly encoded."
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
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) {
|
|
27
|
+
buffer = chars.indexOf(buffer);
|
|
28
|
+
}
|
|
29
|
+
return output;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var b64decode = (encoded) => {
|
|
33
|
+
return Uint8Array.from(Base64.atob(encoded), (c) => c.charCodeAt(0));
|
|
34
|
+
};
|
|
35
|
+
var urlsafe_b64decode = (encoded) => {
|
|
36
|
+
return b64decode(encoded.replace(/_/g, "/").replace(/-/g, "+"));
|
|
37
|
+
};
|
|
38
|
+
var b64encode = (data) => {
|
|
39
|
+
return Base64.btoa(
|
|
40
|
+
String.fromCharCode.apply(null, Array.from(new Uint8Array(data)))
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/utils/environment.ts
|
|
45
|
+
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
46
|
+
var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
47
|
+
var isTest = isNode && process.env.NODE_ENV === "test";
|
|
48
|
+
|
|
49
|
+
// src/utils/createHash.ts
|
|
50
|
+
var createSha256Hash = async (data) => {
|
|
51
|
+
if (isBrowser) {
|
|
52
|
+
return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
|
|
53
|
+
} else {
|
|
54
|
+
const { createHash } = await import("crypto");
|
|
55
|
+
const buffer = createHash("sha256").update(data).digest();
|
|
56
|
+
return new Uint8Array(buffer);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/LightsparkException.ts
|
|
61
|
+
var LightsparkException = class extends Error {
|
|
62
|
+
code;
|
|
63
|
+
message;
|
|
64
|
+
extraInfo;
|
|
65
|
+
constructor(code, message, extraInfo) {
|
|
66
|
+
super(message);
|
|
67
|
+
this.code = code;
|
|
68
|
+
this.message = message;
|
|
69
|
+
this.extraInfo = extraInfo;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var LightsparkException_default = LightsparkException;
|
|
73
|
+
|
|
74
|
+
// src/utils/currency.ts
|
|
75
|
+
var CONVERSION_MAP = {
|
|
76
|
+
["BITCOIN" /* BITCOIN */]: {
|
|
77
|
+
["BITCOIN" /* BITCOIN */]: (v) => v,
|
|
78
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e6,
|
|
79
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
|
|
80
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
|
|
81
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
|
|
82
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
|
|
83
|
+
},
|
|
84
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: {
|
|
85
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
|
|
86
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
|
|
87
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
88
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
|
|
89
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
|
|
90
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 100
|
|
91
|
+
},
|
|
92
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: {
|
|
93
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
|
|
94
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
|
|
95
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
|
|
96
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
|
|
97
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
|
|
98
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
|
|
99
|
+
},
|
|
100
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: {
|
|
101
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
|
|
102
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
103
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e8),
|
|
104
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
|
|
105
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
|
|
106
|
+
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
|
|
107
|
+
},
|
|
108
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: {
|
|
109
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
|
|
110
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
111
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e6),
|
|
112
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
|
|
113
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
|
|
114
|
+
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
|
|
115
|
+
},
|
|
116
|
+
["SATOSHI" /* SATOSHI */]: {
|
|
117
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
|
|
118
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
|
|
119
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
120
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
|
|
121
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
|
|
122
|
+
["SATOSHI" /* SATOSHI */]: (v) => v
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
var convertCurrencyAmount = (from, toUnit) => {
|
|
126
|
+
if (from.originalUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || from.originalUnit === "USD" /* USD */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "USD" /* USD */) {
|
|
127
|
+
throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
|
|
128
|
+
}
|
|
129
|
+
const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
|
|
130
|
+
if (!conversionFn) {
|
|
131
|
+
throw new LightsparkException_default(
|
|
132
|
+
"CurrencyError",
|
|
133
|
+
`Cannot convert from ${from.originalUnit} to ${toUnit}`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
...from,
|
|
138
|
+
preferredCurrencyUnit: toUnit,
|
|
139
|
+
preferredCurrencyValueApprox: conversionFn(from.originalValue),
|
|
140
|
+
preferredCurrencyValueRounded: conversionFn(from.originalValue)
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// src/utils/errors.ts
|
|
145
|
+
var isError = (e) => {
|
|
146
|
+
return Boolean(
|
|
147
|
+
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")
|
|
148
|
+
);
|
|
149
|
+
};
|
|
150
|
+
var isErrorWithMessage = (e) => {
|
|
151
|
+
return Boolean(
|
|
152
|
+
typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
|
|
153
|
+
);
|
|
154
|
+
};
|
|
155
|
+
var getErrorMsg = (e) => {
|
|
156
|
+
return isErrorWithMessage(e) ? e.message : "Unknown error";
|
|
157
|
+
};
|
|
158
|
+
var isErrorMsg = (e, msg) => {
|
|
159
|
+
if (isError(e)) {
|
|
160
|
+
return e.message === msg;
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/utils/hex.ts
|
|
166
|
+
var bytesToHex = (bytes) => {
|
|
167
|
+
return bytes.reduce((acc, byte) => {
|
|
168
|
+
return acc += ("0" + byte.toString(16)).slice(-2);
|
|
169
|
+
}, "");
|
|
170
|
+
};
|
|
171
|
+
var hexToBytes = (hex) => {
|
|
172
|
+
const bytes = [];
|
|
173
|
+
for (let c = 0; c < hex.length; c += 2) {
|
|
174
|
+
bytes.push(parseInt(hex.substr(c, 2), 16));
|
|
175
|
+
}
|
|
176
|
+
return Uint8Array.from(bytes);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// ../../node_modules/lodash-es/_freeGlobal.js
|
|
180
|
+
var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
|
|
181
|
+
var freeGlobal_default = freeGlobal;
|
|
182
|
+
|
|
183
|
+
// ../../node_modules/lodash-es/_root.js
|
|
184
|
+
var freeSelf = typeof self == "object" && self && self.Object === Object && self;
|
|
185
|
+
var root = freeGlobal_default || freeSelf || Function("return this")();
|
|
186
|
+
var root_default = root;
|
|
187
|
+
|
|
188
|
+
// ../../node_modules/lodash-es/_Symbol.js
|
|
189
|
+
var Symbol = root_default.Symbol;
|
|
190
|
+
var Symbol_default = Symbol;
|
|
191
|
+
|
|
192
|
+
// ../../node_modules/lodash-es/_getRawTag.js
|
|
193
|
+
var objectProto = Object.prototype;
|
|
194
|
+
var hasOwnProperty = objectProto.hasOwnProperty;
|
|
195
|
+
var nativeObjectToString = objectProto.toString;
|
|
196
|
+
var symToStringTag = Symbol_default ? Symbol_default.toStringTag : void 0;
|
|
197
|
+
function getRawTag(value) {
|
|
198
|
+
var isOwn = hasOwnProperty.call(value, symToStringTag), tag = value[symToStringTag];
|
|
199
|
+
try {
|
|
200
|
+
value[symToStringTag] = void 0;
|
|
201
|
+
var unmasked = true;
|
|
202
|
+
} catch (e) {
|
|
203
|
+
}
|
|
204
|
+
var result = nativeObjectToString.call(value);
|
|
205
|
+
if (unmasked) {
|
|
206
|
+
if (isOwn) {
|
|
207
|
+
value[symToStringTag] = tag;
|
|
208
|
+
} else {
|
|
209
|
+
delete value[symToStringTag];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
var getRawTag_default = getRawTag;
|
|
215
|
+
|
|
216
|
+
// ../../node_modules/lodash-es/_objectToString.js
|
|
217
|
+
var objectProto2 = Object.prototype;
|
|
218
|
+
var nativeObjectToString2 = objectProto2.toString;
|
|
219
|
+
function objectToString(value) {
|
|
220
|
+
return nativeObjectToString2.call(value);
|
|
221
|
+
}
|
|
222
|
+
var objectToString_default = objectToString;
|
|
223
|
+
|
|
224
|
+
// ../../node_modules/lodash-es/_baseGetTag.js
|
|
225
|
+
var nullTag = "[object Null]";
|
|
226
|
+
var undefinedTag = "[object Undefined]";
|
|
227
|
+
var symToStringTag2 = Symbol_default ? Symbol_default.toStringTag : void 0;
|
|
228
|
+
function baseGetTag(value) {
|
|
229
|
+
if (value == null) {
|
|
230
|
+
return value === void 0 ? undefinedTag : nullTag;
|
|
231
|
+
}
|
|
232
|
+
return symToStringTag2 && symToStringTag2 in Object(value) ? getRawTag_default(value) : objectToString_default(value);
|
|
233
|
+
}
|
|
234
|
+
var baseGetTag_default = baseGetTag;
|
|
235
|
+
|
|
236
|
+
// ../../node_modules/lodash-es/isObject.js
|
|
237
|
+
function isObject(value) {
|
|
238
|
+
var type = typeof value;
|
|
239
|
+
return value != null && (type == "object" || type == "function");
|
|
240
|
+
}
|
|
241
|
+
var isObject_default = isObject;
|
|
242
|
+
|
|
243
|
+
// ../../node_modules/lodash-es/isFunction.js
|
|
244
|
+
var asyncTag = "[object AsyncFunction]";
|
|
245
|
+
var funcTag = "[object Function]";
|
|
246
|
+
var genTag = "[object GeneratorFunction]";
|
|
247
|
+
var proxyTag = "[object Proxy]";
|
|
248
|
+
function isFunction(value) {
|
|
249
|
+
if (!isObject_default(value)) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
var tag = baseGetTag_default(value);
|
|
253
|
+
return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
|
|
254
|
+
}
|
|
255
|
+
var isFunction_default = isFunction;
|
|
256
|
+
|
|
257
|
+
// src/utils/sleep.ts
|
|
258
|
+
function sleep(ms) {
|
|
259
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/utils/pollUntil.ts
|
|
263
|
+
function getDefaultMaxPollsError() {
|
|
264
|
+
return new Error("pollUntil: Max polls reached");
|
|
265
|
+
}
|
|
266
|
+
function pollUntil(asyncFn, getValue, maxPolls = 60, pollIntervalMs = 500, ignoreErrors = false, getMaxPollsError = getDefaultMaxPollsError) {
|
|
267
|
+
return new Promise((resolve, reject) => {
|
|
268
|
+
let polls = 0;
|
|
269
|
+
let stopPolling = false;
|
|
270
|
+
(async function() {
|
|
271
|
+
while (!stopPolling) {
|
|
272
|
+
polls += 1;
|
|
273
|
+
if (polls > maxPolls) {
|
|
274
|
+
stopPolling = true;
|
|
275
|
+
const maxPollsError = getMaxPollsError(maxPolls);
|
|
276
|
+
reject(maxPollsError);
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const asyncResult = await asyncFn();
|
|
281
|
+
const result = getValue(asyncResult, {
|
|
282
|
+
stopPolling: false,
|
|
283
|
+
value: null
|
|
284
|
+
});
|
|
285
|
+
if (result.stopPolling) {
|
|
286
|
+
stopPolling = true;
|
|
287
|
+
resolve(result.value);
|
|
288
|
+
}
|
|
289
|
+
} catch (e) {
|
|
290
|
+
if (!ignoreErrors || isFunction_default(ignoreErrors) && !ignoreErrors(e)) {
|
|
291
|
+
stopPolling = true;
|
|
292
|
+
reject(e);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
await sleep(pollIntervalMs);
|
|
296
|
+
}
|
|
297
|
+
})();
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/utils/types.ts
|
|
302
|
+
var isType = (typename) => (node) => {
|
|
303
|
+
return node?.__typename === typename;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
export {
|
|
307
|
+
LightsparkException_default,
|
|
308
|
+
b64decode,
|
|
309
|
+
urlsafe_b64decode,
|
|
310
|
+
b64encode,
|
|
311
|
+
isBrowser,
|
|
312
|
+
isNode,
|
|
313
|
+
isTest,
|
|
314
|
+
createSha256Hash,
|
|
315
|
+
convertCurrencyAmount,
|
|
316
|
+
isError,
|
|
317
|
+
isErrorWithMessage,
|
|
318
|
+
getErrorMsg,
|
|
319
|
+
isErrorMsg,
|
|
320
|
+
bytesToHex,
|
|
321
|
+
hexToBytes,
|
|
322
|
+
sleep,
|
|
323
|
+
pollUntil,
|
|
324
|
+
isType
|
|
325
|
+
};
|
|
326
|
+
/*! Bundled license information:
|
|
327
|
+
|
|
328
|
+
lodash-es/lodash.js:
|
|
329
|
+
(**
|
|
330
|
+
* @license
|
|
331
|
+
* Lodash (Custom Build) <https://lodash.com/>
|
|
332
|
+
* Build: `lodash modularize exports="es" -o ./`
|
|
333
|
+
* Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
|
|
334
|
+
* Released under MIT license <https://lodash.com/license>
|
|
335
|
+
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
336
|
+
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
337
|
+
*)
|
|
338
|
+
*/
|