@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
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
declare const b64decode: (encoded: string) => Uint8Array;
|
|
2
|
+
declare const urlsafe_b64decode: (encoded: string) => Uint8Array;
|
|
3
|
+
declare const b64encode: (data: ArrayBuffer) => string;
|
|
4
|
+
|
|
5
|
+
type SourceData = Uint8Array | string;
|
|
6
|
+
declare function createSha256Hash(data: SourceData): Promise<Uint8Array>;
|
|
7
|
+
declare function createSha256Hash(data: SourceData, asHex: true): Promise<string>;
|
|
8
|
+
|
|
9
|
+
/** Represents the value and unit for an amount of currency. **/
|
|
10
|
+
type CurrencyAmount = {
|
|
11
|
+
/** The original numeric value for this CurrencyAmount. **/
|
|
12
|
+
originalValue: number;
|
|
13
|
+
/** The original unit of currency for this CurrencyAmount. **/
|
|
14
|
+
originalUnit: CurrencyUnit;
|
|
15
|
+
/** The unit of user's preferred currency. **/
|
|
16
|
+
preferredCurrencyUnit: CurrencyUnit;
|
|
17
|
+
/**
|
|
18
|
+
* The rounded numeric value for this CurrencyAmount in the very base level of user's preferred
|
|
19
|
+
* currency. For example, for USD, the value will be in cents.
|
|
20
|
+
**/
|
|
21
|
+
preferredCurrencyValueRounded: number;
|
|
22
|
+
/**
|
|
23
|
+
* The approximate float value for this CurrencyAmount in the very base level of user's preferred
|
|
24
|
+
* currency. For example, for USD, the value will be in cents.
|
|
25
|
+
**/
|
|
26
|
+
preferredCurrencyValueApprox: number;
|
|
27
|
+
};
|
|
28
|
+
declare enum CurrencyUnit {
|
|
29
|
+
/**
|
|
30
|
+
* This is an enum value that represents values that could be added in the future.
|
|
31
|
+
* Clients should support unknown values as more of them could be added without notice.
|
|
32
|
+
*/
|
|
33
|
+
FUTURE_VALUE = "FUTURE_VALUE",
|
|
34
|
+
/** Bitcoin is the cryptocurrency native to the Bitcoin network. It is used as the native medium for value transfer for the Lightning Network. **/
|
|
35
|
+
BITCOIN = "BITCOIN",
|
|
36
|
+
/** 0.00000001 (10e-8) Bitcoin or one hundred millionth of a Bitcoin. This is the unit most commonly used in Lightning transactions. **/
|
|
37
|
+
SATOSHI = "SATOSHI",
|
|
38
|
+
/** 0.001 Satoshi, or 10e-11 Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
39
|
+
MILLISATOSHI = "MILLISATOSHI",
|
|
40
|
+
/** United States Dollar. **/
|
|
41
|
+
USD = "USD",
|
|
42
|
+
/** 0.000000001 (10e-9) Bitcoin or a billionth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
43
|
+
NANOBITCOIN = "NANOBITCOIN",
|
|
44
|
+
/** 0.000001 (10e-6) Bitcoin or a millionth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
45
|
+
MICROBITCOIN = "MICROBITCOIN",
|
|
46
|
+
/** 0.001 (10e-3) Bitcoin or a thousandth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
47
|
+
MILLIBITCOIN = "MILLIBITCOIN"
|
|
48
|
+
}
|
|
49
|
+
declare const convertCurrencyAmount: (from: CurrencyAmount, toUnit: CurrencyUnit) => CurrencyAmount;
|
|
50
|
+
|
|
51
|
+
declare const isBrowser: boolean;
|
|
52
|
+
declare const isNode: boolean;
|
|
53
|
+
declare const isTest: boolean;
|
|
54
|
+
|
|
55
|
+
declare const isError: (e: unknown) => e is Error;
|
|
56
|
+
type ErrorWithMessage = {
|
|
57
|
+
message: string;
|
|
58
|
+
};
|
|
59
|
+
declare const isErrorWithMessage: (e: unknown) => e is ErrorWithMessage;
|
|
60
|
+
declare const getErrorMsg: (e: unknown) => string;
|
|
61
|
+
declare const isErrorMsg: (e: unknown, msg: string) => boolean;
|
|
62
|
+
|
|
63
|
+
declare const bytesToHex: (bytes: Uint8Array) => string;
|
|
64
|
+
declare const hexToBytes: (hex: string) => Uint8Array;
|
|
65
|
+
|
|
66
|
+
declare function pollUntil<D extends () => Promise<unknown>, T>(asyncFn: D, getValue: (data: Awaited<ReturnType<D>>, response: {
|
|
67
|
+
stopPolling: boolean;
|
|
68
|
+
value: null | T;
|
|
69
|
+
}) => {
|
|
70
|
+
stopPolling: boolean;
|
|
71
|
+
value: null | T;
|
|
72
|
+
}, maxPolls?: number, pollIntervalMs?: number, ignoreErrors?: boolean | ((e: unknown) => boolean), getMaxPollsError?: (maxPolls: number) => Error): Promise<T>;
|
|
73
|
+
|
|
74
|
+
declare function sleep(ms: number): Promise<unknown>;
|
|
75
|
+
|
|
76
|
+
type Maybe<T> = T | null | undefined;
|
|
77
|
+
type ExpandRecursively<T> = T extends object ? T extends infer O ? {
|
|
78
|
+
[K in keyof O]: ExpandRecursively<O[K]>;
|
|
79
|
+
} : never : T;
|
|
80
|
+
type ById<T> = {
|
|
81
|
+
[id: string]: T;
|
|
82
|
+
};
|
|
83
|
+
type OmitTypename<T> = Omit<T, "__typename">;
|
|
84
|
+
declare const isType: <T extends string>(typename: T) => <N extends {
|
|
85
|
+
__typename: string;
|
|
86
|
+
}>(node: N | null | undefined) => node is Extract<N, {
|
|
87
|
+
__typename: T;
|
|
88
|
+
}>;
|
|
89
|
+
|
|
90
|
+
export { ById, ExpandRecursively, Maybe, OmitTypename, b64decode, b64encode, bytesToHex, convertCurrencyAmount, createSha256Hash, getErrorMsg, hexToBytes, isBrowser, isError, isErrorMsg, isErrorWithMessage, isNode, isTest, isType, pollUntil, sleep, urlsafe_b64decode };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
b64decode,
|
|
3
|
+
b64encode,
|
|
4
|
+
bytesToHex,
|
|
5
|
+
convertCurrencyAmount,
|
|
6
|
+
createSha256Hash,
|
|
7
|
+
getErrorMsg,
|
|
8
|
+
hexToBytes,
|
|
9
|
+
isBrowser,
|
|
10
|
+
isError,
|
|
11
|
+
isErrorMsg,
|
|
12
|
+
isErrorWithMessage,
|
|
13
|
+
isNode,
|
|
14
|
+
isTest,
|
|
15
|
+
isType,
|
|
16
|
+
pollUntil,
|
|
17
|
+
sleep,
|
|
18
|
+
urlsafe_b64decode
|
|
19
|
+
} from "../chunk-5P2KZ44N.js";
|
|
20
|
+
export {
|
|
21
|
+
b64decode,
|
|
22
|
+
b64encode,
|
|
23
|
+
bytesToHex,
|
|
24
|
+
convertCurrencyAmount,
|
|
25
|
+
createSha256Hash,
|
|
26
|
+
getErrorMsg,
|
|
27
|
+
hexToBytes,
|
|
28
|
+
isBrowser,
|
|
29
|
+
isError,
|
|
30
|
+
isErrorMsg,
|
|
31
|
+
isErrorWithMessage,
|
|
32
|
+
isNode,
|
|
33
|
+
isTest,
|
|
34
|
+
isType,
|
|
35
|
+
pollUntil,
|
|
36
|
+
sleep,
|
|
37
|
+
urlsafe_b64decode
|
|
38
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightsparkdev/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Lightspark JS SDK",
|
|
5
5
|
"author": "Lightspark Inc.",
|
|
6
6
|
"keywords": [
|
|
@@ -33,6 +33,19 @@
|
|
|
33
33
|
"types": "./dist/index.d.ts",
|
|
34
34
|
"default": "./dist/index.cjs"
|
|
35
35
|
}
|
|
36
|
+
},
|
|
37
|
+
"./utils": {
|
|
38
|
+
"types": "./dist/utils/index.d.ts",
|
|
39
|
+
"import": {
|
|
40
|
+
"types": "./dist/utils/index.d.ts",
|
|
41
|
+
"default": "./dist/utils/index.js"
|
|
42
|
+
},
|
|
43
|
+
"module": "./dist/utils/index.js",
|
|
44
|
+
"require": {
|
|
45
|
+
"types": "./dist/utils/index.d.ts",
|
|
46
|
+
"default": "./dist/utils/index.cjs"
|
|
47
|
+
},
|
|
48
|
+
"default": "./dist/utils/index.cjs"
|
|
36
49
|
}
|
|
37
50
|
},
|
|
38
51
|
"engines": {
|
|
@@ -47,7 +60,8 @@
|
|
|
47
60
|
"CHANGELOG.md"
|
|
48
61
|
],
|
|
49
62
|
"scripts": {
|
|
50
|
-
"build": "yarn tsc && tsup
|
|
63
|
+
"build": "yarn tsc && tsup",
|
|
64
|
+
"build:watch": "yarn build --watch",
|
|
51
65
|
"clean": "rm -rf .turbo && rm -rf dist",
|
|
52
66
|
"dev": "yarn build -- --watch",
|
|
53
67
|
"format:fix": "prettier src --write",
|
package/src/Logger.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isBrowser } from "./
|
|
1
|
+
import { isBrowser, isTest } from "./utils/environment.js";
|
|
2
2
|
|
|
3
3
|
type GetLoggingEnabled = (() => Promise<boolean> | boolean) | undefined;
|
|
4
4
|
|
|
@@ -14,6 +14,8 @@ export class Logger {
|
|
|
14
14
|
async updateLoggingEnabled(getLoggingEnabled: GetLoggingEnabled) {
|
|
15
15
|
if (getLoggingEnabled) {
|
|
16
16
|
this.loggingEnabled = await getLoggingEnabled();
|
|
17
|
+
} else if (isTest) {
|
|
18
|
+
this.loggingEnabled = true;
|
|
17
19
|
} else if (isBrowser) {
|
|
18
20
|
try {
|
|
19
21
|
this.loggingEnabled =
|
package/src/crypto/SigningKey.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import secp256k1 from "secp256k1";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
hexToBytes,
|
|
5
|
-
SigningKeyType,
|
|
6
|
-
type CryptoInterface,
|
|
7
|
-
} from "../index.js";
|
|
2
|
+
import { SigningKeyType, type CryptoInterface } from "../index.js";
|
|
3
|
+
import { createSha256Hash, hexToBytes } from "../utils/index.js";
|
|
8
4
|
|
|
9
5
|
interface Alias {
|
|
10
6
|
alias: string;
|
|
@@ -68,13 +68,14 @@ class Requester {
|
|
|
68
68
|
queryPayload: string,
|
|
69
69
|
variables: { [key: string]: unknown } = {},
|
|
70
70
|
) {
|
|
71
|
-
logger.info(`Requester.subscribe
|
|
71
|
+
logger.info(`Requester.subscribe variables`, variables);
|
|
72
72
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
73
73
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
74
74
|
if (!operationMatch || operationMatch.length < 3) {
|
|
75
75
|
throw new LightsparkException("InvalidQuery", "Invalid query payload");
|
|
76
76
|
}
|
|
77
77
|
const operationType = operationMatch[1];
|
|
78
|
+
logger.info(`Requester.subscribe operationType`, operationType);
|
|
78
79
|
if (operationType == "mutation") {
|
|
79
80
|
throw new LightsparkException(
|
|
80
81
|
"InvalidQuery",
|
|
@@ -94,7 +95,6 @@ class Requester {
|
|
|
94
95
|
operationName: operation,
|
|
95
96
|
};
|
|
96
97
|
|
|
97
|
-
logger.info(`Requester.subscribe bodyData`, bodyData);
|
|
98
98
|
return new Observable<{ data: T }>((observer) => {
|
|
99
99
|
logger.info(`Requester.subscribe observer`, observer);
|
|
100
100
|
return this.wsClient.subscribe(bodyData, {
|
package/src/utils/createHash.ts
CHANGED
|
@@ -1,13 +1,34 @@
|
|
|
1
1
|
import { isBrowser } from "./environment.js";
|
|
2
|
+
import { bytesToHex } from "./hex.js";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
): Promise<Uint8Array
|
|
4
|
+
type SourceData = Uint8Array | string;
|
|
5
|
+
|
|
6
|
+
export async function createSha256Hash(data: SourceData): Promise<Uint8Array>;
|
|
7
|
+
export async function createSha256Hash(
|
|
8
|
+
data: SourceData,
|
|
9
|
+
asHex: true,
|
|
10
|
+
): Promise<string>;
|
|
11
|
+
|
|
12
|
+
export async function createSha256Hash(
|
|
13
|
+
data: SourceData,
|
|
14
|
+
asHex?: boolean,
|
|
15
|
+
): Promise<Uint8Array | string> {
|
|
6
16
|
if (isBrowser) {
|
|
7
|
-
|
|
17
|
+
const source =
|
|
18
|
+
typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
19
|
+
const buffer = await window.crypto.subtle.digest("SHA-256", source);
|
|
20
|
+
const arr = new Uint8Array(buffer);
|
|
21
|
+
if (asHex) {
|
|
22
|
+
return bytesToHex(arr);
|
|
23
|
+
}
|
|
24
|
+
return arr;
|
|
8
25
|
} else {
|
|
9
26
|
const { createHash } = await import("crypto");
|
|
27
|
+
if (asHex) {
|
|
28
|
+
const hexStr = createHash("sha256").update(data).digest("hex");
|
|
29
|
+
return hexStr;
|
|
30
|
+
}
|
|
10
31
|
const buffer = createHash("sha256").update(data).digest();
|
|
11
32
|
return new Uint8Array(buffer);
|
|
12
33
|
}
|
|
13
|
-
}
|
|
34
|
+
}
|
package/src/utils/environment.ts
CHANGED
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { isFunction } from "lodash-es";
|
|
2
|
+
import { sleep } from "./sleep.js";
|
|
3
|
+
|
|
4
|
+
function getDefaultMaxPollsError() {
|
|
5
|
+
return new Error("pollUntil: Max polls reached");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function pollUntil<D extends () => Promise<unknown>, T>(
|
|
9
|
+
asyncFn: D,
|
|
10
|
+
getValue: (
|
|
11
|
+
data: Awaited<ReturnType<D>>,
|
|
12
|
+
response: { stopPolling: boolean; value: null | T },
|
|
13
|
+
) => {
|
|
14
|
+
stopPolling: boolean;
|
|
15
|
+
value: null | T;
|
|
16
|
+
},
|
|
17
|
+
maxPolls = 60,
|
|
18
|
+
pollIntervalMs = 500,
|
|
19
|
+
ignoreErrors: boolean | ((e: unknown) => boolean) = false,
|
|
20
|
+
getMaxPollsError: (maxPolls: number) => Error = getDefaultMaxPollsError,
|
|
21
|
+
): Promise<T> {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
let polls = 0;
|
|
24
|
+
let stopPolling = false;
|
|
25
|
+
(async function () {
|
|
26
|
+
while (!stopPolling) {
|
|
27
|
+
polls += 1;
|
|
28
|
+
if (polls > maxPolls) {
|
|
29
|
+
stopPolling = true;
|
|
30
|
+
const maxPollsError = getMaxPollsError(maxPolls);
|
|
31
|
+
reject(maxPollsError);
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const asyncResult = await asyncFn();
|
|
36
|
+
const result = getValue(asyncResult as Awaited<ReturnType<D>>, {
|
|
37
|
+
stopPolling: false,
|
|
38
|
+
value: null,
|
|
39
|
+
});
|
|
40
|
+
if (result.stopPolling) {
|
|
41
|
+
stopPolling = true;
|
|
42
|
+
resolve(result.value as T);
|
|
43
|
+
}
|
|
44
|
+
} catch (e) {
|
|
45
|
+
if (!ignoreErrors || (isFunction(ignoreErrors) && !ignoreErrors(e))) {
|
|
46
|
+
stopPolling = true;
|
|
47
|
+
reject(e);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
await sleep(pollIntervalMs);
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
53
|
+
});
|
|
54
|
+
}
|