@lightsparkdev/core 1.0.3 → 1.0.5
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 +13 -0
- package/dist/chunk-5P2KZ44N.js +348 -0
- package/dist/index.cjs +283 -129
- package/dist/index.d.ts +2 -77
- package/dist/index.js +27 -185
- package/dist/utils/index.cjs +399 -0
- package/dist/utils/index.d.ts +90 -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/createHash.ts +26 -5
- 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/dist/index.js
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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-5P2KZ44N.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) {
|
|
@@ -404,6 +368,8 @@ var Logger = class {
|
|
|
404
368
|
async updateLoggingEnabled(getLoggingEnabled) {
|
|
405
369
|
if (getLoggingEnabled) {
|
|
406
370
|
this.loggingEnabled = await getLoggingEnabled();
|
|
371
|
+
} else if (isTest) {
|
|
372
|
+
this.loggingEnabled = true;
|
|
407
373
|
} else if (isBrowser) {
|
|
408
374
|
try {
|
|
409
375
|
this.loggingEnabled = localStorage.getItem("lightspark-logging-enabled") === "1";
|
|
@@ -429,12 +395,6 @@ import utc from "dayjs/plugin/utc.js";
|
|
|
429
395
|
import { createClient } from "graphql-ws";
|
|
430
396
|
import NodeWebSocket from "ws";
|
|
431
397
|
import { Observable } from "zen-observable-ts";
|
|
432
|
-
|
|
433
|
-
// src/utils/environment.ts
|
|
434
|
-
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
435
|
-
var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
436
|
-
|
|
437
|
-
// src/requester/Requester.ts
|
|
438
398
|
var DEFAULT_BASE_URL = "api.lightspark.com";
|
|
439
399
|
var LIGHTSPARK_BETA_HEADER_KEY = "X-Lightspark-Beta";
|
|
440
400
|
var LIGHTSPARK_BETA_HEADER_VALUE = "z2h0BBYxTA83cjW7fi8QwWtBPCzkQKiemcuhKY08LOo";
|
|
@@ -473,13 +433,14 @@ var Requester = class {
|
|
|
473
433
|
return query.constructObject(data);
|
|
474
434
|
}
|
|
475
435
|
subscribe(queryPayload, variables = {}) {
|
|
476
|
-
logger.info(`Requester.subscribe
|
|
436
|
+
logger.info(`Requester.subscribe variables`, variables);
|
|
477
437
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
478
438
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
479
439
|
if (!operationMatch || operationMatch.length < 3) {
|
|
480
440
|
throw new LightsparkException_default("InvalidQuery", "Invalid query payload");
|
|
481
441
|
}
|
|
482
442
|
const operationType = operationMatch[1];
|
|
443
|
+
logger.info(`Requester.subscribe operationType`, operationType);
|
|
483
444
|
if (operationType == "mutation") {
|
|
484
445
|
throw new LightsparkException_default(
|
|
485
446
|
"InvalidQuery",
|
|
@@ -497,7 +458,6 @@ var Requester = class {
|
|
|
497
458
|
variables,
|
|
498
459
|
operationName: operation
|
|
499
460
|
};
|
|
500
|
-
logger.info(`Requester.subscribe bodyData`, bodyData);
|
|
501
461
|
return new Observable((observer) => {
|
|
502
462
|
logger.info(`Requester.subscribe observer`, observer);
|
|
503
463
|
return this.wsClient.subscribe(bodyData, {
|
|
@@ -633,127 +593,6 @@ var apiDomainForEnvironment = (environment) => {
|
|
|
633
593
|
}
|
|
634
594
|
};
|
|
635
595
|
var ServerEnvironment_default = ServerEnvironment;
|
|
636
|
-
|
|
637
|
-
// src/utils/createHash.ts
|
|
638
|
-
var createSha256Hash = async (data) => {
|
|
639
|
-
if (isBrowser) {
|
|
640
|
-
return new Uint8Array(await window.crypto.subtle.digest("SHA-256", data));
|
|
641
|
-
} else {
|
|
642
|
-
const { createHash } = await import("crypto");
|
|
643
|
-
const buffer = createHash("sha256").update(data).digest();
|
|
644
|
-
return new Uint8Array(buffer);
|
|
645
|
-
}
|
|
646
|
-
};
|
|
647
|
-
|
|
648
|
-
// src/utils/currency.ts
|
|
649
|
-
var CONVERSION_MAP = {
|
|
650
|
-
["BITCOIN" /* BITCOIN */]: {
|
|
651
|
-
["BITCOIN" /* BITCOIN */]: (v) => v,
|
|
652
|
-
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e6,
|
|
653
|
-
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
|
|
654
|
-
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
|
|
655
|
-
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
|
|
656
|
-
["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
|
|
657
|
-
},
|
|
658
|
-
["MICROBITCOIN" /* MICROBITCOIN */]: {
|
|
659
|
-
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
|
|
660
|
-
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
|
|
661
|
-
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
662
|
-
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
|
|
663
|
-
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
|
|
664
|
-
["SATOSHI" /* SATOSHI */]: (v) => v * 100
|
|
665
|
-
},
|
|
666
|
-
["MILLIBITCOIN" /* MILLIBITCOIN */]: {
|
|
667
|
-
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
|
|
668
|
-
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
|
|
669
|
-
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
|
|
670
|
-
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
|
|
671
|
-
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
|
|
672
|
-
["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
|
|
673
|
-
},
|
|
674
|
-
["MILLISATOSHI" /* MILLISATOSHI */]: {
|
|
675
|
-
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
|
|
676
|
-
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
677
|
-
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e8),
|
|
678
|
-
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
|
|
679
|
-
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
|
|
680
|
-
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
|
|
681
|
-
},
|
|
682
|
-
["NANOBITCOIN" /* NANOBITCOIN */]: {
|
|
683
|
-
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
|
|
684
|
-
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
685
|
-
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e6),
|
|
686
|
-
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
|
|
687
|
-
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
|
|
688
|
-
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
|
|
689
|
-
},
|
|
690
|
-
["SATOSHI" /* SATOSHI */]: {
|
|
691
|
-
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
|
|
692
|
-
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
|
|
693
|
-
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
694
|
-
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
|
|
695
|
-
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
|
|
696
|
-
["SATOSHI" /* SATOSHI */]: (v) => v
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
var convertCurrencyAmount = (from, toUnit) => {
|
|
700
|
-
if (from.originalUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || from.originalUnit === "USD" /* USD */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "USD" /* USD */) {
|
|
701
|
-
throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
|
|
702
|
-
}
|
|
703
|
-
const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
|
|
704
|
-
if (!conversionFn) {
|
|
705
|
-
throw new LightsparkException_default(
|
|
706
|
-
"CurrencyError",
|
|
707
|
-
`Cannot convert from ${from.originalUnit} to ${toUnit}`
|
|
708
|
-
);
|
|
709
|
-
}
|
|
710
|
-
return {
|
|
711
|
-
...from,
|
|
712
|
-
preferredCurrencyUnit: toUnit,
|
|
713
|
-
preferredCurrencyValueApprox: conversionFn(from.originalValue),
|
|
714
|
-
preferredCurrencyValueRounded: conversionFn(from.originalValue)
|
|
715
|
-
};
|
|
716
|
-
};
|
|
717
|
-
|
|
718
|
-
// src/utils/errors.ts
|
|
719
|
-
var isError = (e) => {
|
|
720
|
-
return Boolean(
|
|
721
|
-
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")
|
|
722
|
-
);
|
|
723
|
-
};
|
|
724
|
-
var isErrorWithMessage = (e) => {
|
|
725
|
-
return Boolean(
|
|
726
|
-
typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
|
|
727
|
-
);
|
|
728
|
-
};
|
|
729
|
-
var getErrorMsg = (e) => {
|
|
730
|
-
return isErrorWithMessage(e) ? e.message : "Unknown error";
|
|
731
|
-
};
|
|
732
|
-
var isErrorMsg = (e, msg) => {
|
|
733
|
-
if (isError(e)) {
|
|
734
|
-
return e.message === msg;
|
|
735
|
-
}
|
|
736
|
-
return false;
|
|
737
|
-
};
|
|
738
|
-
|
|
739
|
-
// src/utils/hex.ts
|
|
740
|
-
var bytesToHex = (bytes) => {
|
|
741
|
-
return bytes.reduce((acc, byte) => {
|
|
742
|
-
return acc += ("0" + byte.toString(16)).slice(-2);
|
|
743
|
-
}, "");
|
|
744
|
-
};
|
|
745
|
-
var hexToBytes = (hex) => {
|
|
746
|
-
const bytes = [];
|
|
747
|
-
for (let c = 0; c < hex.length; c += 2) {
|
|
748
|
-
bytes.push(parseInt(hex.substr(c, 2), 16));
|
|
749
|
-
}
|
|
750
|
-
return Uint8Array.from(bytes);
|
|
751
|
-
};
|
|
752
|
-
|
|
753
|
-
// src/utils/types.ts
|
|
754
|
-
var isType = (typename) => (node) => {
|
|
755
|
-
return node?.__typename === typename;
|
|
756
|
-
};
|
|
757
596
|
export {
|
|
758
597
|
DefaultCrypto,
|
|
759
598
|
KeyOrAlias,
|
|
@@ -782,6 +621,9 @@ export {
|
|
|
782
621
|
isErrorMsg,
|
|
783
622
|
isErrorWithMessage,
|
|
784
623
|
isNode,
|
|
624
|
+
isTest,
|
|
785
625
|
isType,
|
|
626
|
+
pollUntil,
|
|
627
|
+
sleep,
|
|
786
628
|
urlsafe_b64decode
|
|
787
629
|
};
|
|
@@ -0,0 +1,399 @@
|
|
|
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/hex.ts
|
|
102
|
+
var bytesToHex = (bytes) => {
|
|
103
|
+
return bytes.reduce((acc, byte) => {
|
|
104
|
+
return acc += ("0" + byte.toString(16)).slice(-2);
|
|
105
|
+
}, "");
|
|
106
|
+
};
|
|
107
|
+
var hexToBytes = (hex) => {
|
|
108
|
+
const bytes = [];
|
|
109
|
+
for (let c = 0; c < hex.length; c += 2) {
|
|
110
|
+
bytes.push(parseInt(hex.substr(c, 2), 16));
|
|
111
|
+
}
|
|
112
|
+
return Uint8Array.from(bytes);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// src/utils/createHash.ts
|
|
116
|
+
async function createSha256Hash(data, asHex) {
|
|
117
|
+
if (isBrowser) {
|
|
118
|
+
const source = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
119
|
+
const buffer = await window.crypto.subtle.digest("SHA-256", source);
|
|
120
|
+
const arr = new Uint8Array(buffer);
|
|
121
|
+
if (asHex) {
|
|
122
|
+
return bytesToHex(arr);
|
|
123
|
+
}
|
|
124
|
+
return arr;
|
|
125
|
+
} else {
|
|
126
|
+
const { createHash } = await import("crypto");
|
|
127
|
+
if (asHex) {
|
|
128
|
+
const hexStr = createHash("sha256").update(data).digest("hex");
|
|
129
|
+
return hexStr;
|
|
130
|
+
}
|
|
131
|
+
const buffer = createHash("sha256").update(data).digest();
|
|
132
|
+
return new Uint8Array(buffer);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/LightsparkException.ts
|
|
137
|
+
var LightsparkException = class extends Error {
|
|
138
|
+
code;
|
|
139
|
+
message;
|
|
140
|
+
extraInfo;
|
|
141
|
+
constructor(code, message, extraInfo) {
|
|
142
|
+
super(message);
|
|
143
|
+
this.code = code;
|
|
144
|
+
this.message = message;
|
|
145
|
+
this.extraInfo = extraInfo;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
var LightsparkException_default = LightsparkException;
|
|
149
|
+
|
|
150
|
+
// src/utils/currency.ts
|
|
151
|
+
var CONVERSION_MAP = {
|
|
152
|
+
["BITCOIN" /* BITCOIN */]: {
|
|
153
|
+
["BITCOIN" /* BITCOIN */]: (v) => v,
|
|
154
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e6,
|
|
155
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
|
|
156
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
|
|
157
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
|
|
158
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
|
|
159
|
+
},
|
|
160
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: {
|
|
161
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
|
|
162
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
|
|
163
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
164
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
|
|
165
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
|
|
166
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 100
|
|
167
|
+
},
|
|
168
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: {
|
|
169
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
|
|
170
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
|
|
171
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
|
|
172
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
|
|
173
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
|
|
174
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
|
|
175
|
+
},
|
|
176
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: {
|
|
177
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
|
|
178
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
179
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e8),
|
|
180
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
|
|
181
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
|
|
182
|
+
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
|
|
183
|
+
},
|
|
184
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: {
|
|
185
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
|
|
186
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
187
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e6),
|
|
188
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
|
|
189
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
|
|
190
|
+
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
|
|
191
|
+
},
|
|
192
|
+
["SATOSHI" /* SATOSHI */]: {
|
|
193
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
|
|
194
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
|
|
195
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
196
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
|
|
197
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
|
|
198
|
+
["SATOSHI" /* SATOSHI */]: (v) => v
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
var convertCurrencyAmount = (from, toUnit) => {
|
|
202
|
+
if (from.originalUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || from.originalUnit === "USD" /* USD */ || toUnit === "FUTURE_VALUE" /* FUTURE_VALUE */ || toUnit === "USD" /* USD */) {
|
|
203
|
+
throw new LightsparkException_default("CurrencyError", `Unsupported CurrencyUnit.`);
|
|
204
|
+
}
|
|
205
|
+
const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
|
|
206
|
+
if (!conversionFn) {
|
|
207
|
+
throw new LightsparkException_default(
|
|
208
|
+
"CurrencyError",
|
|
209
|
+
`Cannot convert from ${from.originalUnit} to ${toUnit}`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
...from,
|
|
214
|
+
preferredCurrencyUnit: toUnit,
|
|
215
|
+
preferredCurrencyValueApprox: conversionFn(from.originalValue),
|
|
216
|
+
preferredCurrencyValueRounded: conversionFn(from.originalValue)
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/utils/errors.ts
|
|
221
|
+
var isError = (e) => {
|
|
222
|
+
return Boolean(
|
|
223
|
+
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")
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
var isErrorWithMessage = (e) => {
|
|
227
|
+
return Boolean(
|
|
228
|
+
typeof e === "object" && e !== null && "message" in e && typeof e.message === "string"
|
|
229
|
+
);
|
|
230
|
+
};
|
|
231
|
+
var getErrorMsg = (e) => {
|
|
232
|
+
return isErrorWithMessage(e) ? e.message : "Unknown error";
|
|
233
|
+
};
|
|
234
|
+
var isErrorMsg = (e, msg) => {
|
|
235
|
+
if (isError(e)) {
|
|
236
|
+
return e.message === msg;
|
|
237
|
+
}
|
|
238
|
+
return false;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// ../../node_modules/lodash-es/_freeGlobal.js
|
|
242
|
+
var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
|
|
243
|
+
var freeGlobal_default = freeGlobal;
|
|
244
|
+
|
|
245
|
+
// ../../node_modules/lodash-es/_root.js
|
|
246
|
+
var freeSelf = typeof self == "object" && self && self.Object === Object && self;
|
|
247
|
+
var root = freeGlobal_default || freeSelf || Function("return this")();
|
|
248
|
+
var root_default = root;
|
|
249
|
+
|
|
250
|
+
// ../../node_modules/lodash-es/_Symbol.js
|
|
251
|
+
var Symbol2 = root_default.Symbol;
|
|
252
|
+
var Symbol_default = Symbol2;
|
|
253
|
+
|
|
254
|
+
// ../../node_modules/lodash-es/_getRawTag.js
|
|
255
|
+
var objectProto = Object.prototype;
|
|
256
|
+
var hasOwnProperty = objectProto.hasOwnProperty;
|
|
257
|
+
var nativeObjectToString = objectProto.toString;
|
|
258
|
+
var symToStringTag = Symbol_default ? Symbol_default.toStringTag : void 0;
|
|
259
|
+
function getRawTag(value) {
|
|
260
|
+
var isOwn = hasOwnProperty.call(value, symToStringTag), tag = value[symToStringTag];
|
|
261
|
+
try {
|
|
262
|
+
value[symToStringTag] = void 0;
|
|
263
|
+
var unmasked = true;
|
|
264
|
+
} catch (e) {
|
|
265
|
+
}
|
|
266
|
+
var result = nativeObjectToString.call(value);
|
|
267
|
+
if (unmasked) {
|
|
268
|
+
if (isOwn) {
|
|
269
|
+
value[symToStringTag] = tag;
|
|
270
|
+
} else {
|
|
271
|
+
delete value[symToStringTag];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
var getRawTag_default = getRawTag;
|
|
277
|
+
|
|
278
|
+
// ../../node_modules/lodash-es/_objectToString.js
|
|
279
|
+
var objectProto2 = Object.prototype;
|
|
280
|
+
var nativeObjectToString2 = objectProto2.toString;
|
|
281
|
+
function objectToString(value) {
|
|
282
|
+
return nativeObjectToString2.call(value);
|
|
283
|
+
}
|
|
284
|
+
var objectToString_default = objectToString;
|
|
285
|
+
|
|
286
|
+
// ../../node_modules/lodash-es/_baseGetTag.js
|
|
287
|
+
var nullTag = "[object Null]";
|
|
288
|
+
var undefinedTag = "[object Undefined]";
|
|
289
|
+
var symToStringTag2 = Symbol_default ? Symbol_default.toStringTag : void 0;
|
|
290
|
+
function baseGetTag(value) {
|
|
291
|
+
if (value == null) {
|
|
292
|
+
return value === void 0 ? undefinedTag : nullTag;
|
|
293
|
+
}
|
|
294
|
+
return symToStringTag2 && symToStringTag2 in Object(value) ? getRawTag_default(value) : objectToString_default(value);
|
|
295
|
+
}
|
|
296
|
+
var baseGetTag_default = baseGetTag;
|
|
297
|
+
|
|
298
|
+
// ../../node_modules/lodash-es/isObject.js
|
|
299
|
+
function isObject(value) {
|
|
300
|
+
var type = typeof value;
|
|
301
|
+
return value != null && (type == "object" || type == "function");
|
|
302
|
+
}
|
|
303
|
+
var isObject_default = isObject;
|
|
304
|
+
|
|
305
|
+
// ../../node_modules/lodash-es/isFunction.js
|
|
306
|
+
var asyncTag = "[object AsyncFunction]";
|
|
307
|
+
var funcTag = "[object Function]";
|
|
308
|
+
var genTag = "[object GeneratorFunction]";
|
|
309
|
+
var proxyTag = "[object Proxy]";
|
|
310
|
+
function isFunction(value) {
|
|
311
|
+
if (!isObject_default(value)) {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
var tag = baseGetTag_default(value);
|
|
315
|
+
return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
|
|
316
|
+
}
|
|
317
|
+
var isFunction_default = isFunction;
|
|
318
|
+
|
|
319
|
+
// src/utils/sleep.ts
|
|
320
|
+
function sleep(ms) {
|
|
321
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// src/utils/pollUntil.ts
|
|
325
|
+
function getDefaultMaxPollsError() {
|
|
326
|
+
return new Error("pollUntil: Max polls reached");
|
|
327
|
+
}
|
|
328
|
+
function pollUntil(asyncFn, getValue, maxPolls = 60, pollIntervalMs = 500, ignoreErrors = false, getMaxPollsError = getDefaultMaxPollsError) {
|
|
329
|
+
return new Promise((resolve, reject) => {
|
|
330
|
+
let polls = 0;
|
|
331
|
+
let stopPolling = false;
|
|
332
|
+
(async function() {
|
|
333
|
+
while (!stopPolling) {
|
|
334
|
+
polls += 1;
|
|
335
|
+
if (polls > maxPolls) {
|
|
336
|
+
stopPolling = true;
|
|
337
|
+
const maxPollsError = getMaxPollsError(maxPolls);
|
|
338
|
+
reject(maxPollsError);
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
const asyncResult = await asyncFn();
|
|
343
|
+
const result = getValue(asyncResult, {
|
|
344
|
+
stopPolling: false,
|
|
345
|
+
value: null
|
|
346
|
+
});
|
|
347
|
+
if (result.stopPolling) {
|
|
348
|
+
stopPolling = true;
|
|
349
|
+
resolve(result.value);
|
|
350
|
+
}
|
|
351
|
+
} catch (e) {
|
|
352
|
+
if (!ignoreErrors || isFunction_default(ignoreErrors) && !ignoreErrors(e)) {
|
|
353
|
+
stopPolling = true;
|
|
354
|
+
reject(e);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
await sleep(pollIntervalMs);
|
|
358
|
+
}
|
|
359
|
+
})();
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// src/utils/types.ts
|
|
364
|
+
var isType = (typename) => (node) => {
|
|
365
|
+
return node?.__typename === typename;
|
|
366
|
+
};
|
|
367
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
368
|
+
0 && (module.exports = {
|
|
369
|
+
b64decode,
|
|
370
|
+
b64encode,
|
|
371
|
+
bytesToHex,
|
|
372
|
+
convertCurrencyAmount,
|
|
373
|
+
createSha256Hash,
|
|
374
|
+
getErrorMsg,
|
|
375
|
+
hexToBytes,
|
|
376
|
+
isBrowser,
|
|
377
|
+
isError,
|
|
378
|
+
isErrorMsg,
|
|
379
|
+
isErrorWithMessage,
|
|
380
|
+
isNode,
|
|
381
|
+
isTest,
|
|
382
|
+
isType,
|
|
383
|
+
pollUntil,
|
|
384
|
+
sleep,
|
|
385
|
+
urlsafe_b64decode
|
|
386
|
+
});
|
|
387
|
+
/*! Bundled license information:
|
|
388
|
+
|
|
389
|
+
lodash-es/lodash.js:
|
|
390
|
+
(**
|
|
391
|
+
* @license
|
|
392
|
+
* Lodash (Custom Build) <https://lodash.com/>
|
|
393
|
+
* Build: `lodash modularize exports="es" -o ./`
|
|
394
|
+
* Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
|
|
395
|
+
* Released under MIT license <https://lodash.com/license>
|
|
396
|
+
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
397
|
+
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
398
|
+
*)
|
|
399
|
+
*/
|