@lightsparkdev/core 0.1.3
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/.prettierrc +1 -0
- package/.turbo/turbo-build.log +14 -0
- package/CHANGELOG.md +19 -0
- package/LICENSE +201 -0
- package/README.md +11 -0
- package/dist/index.cjs +663 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +602 -0
- package/package.json +63 -0
- package/src/LightsparkException.ts +16 -0
- package/src/ServerEnvironment.ts +17 -0
- package/src/auth/AuthProvider.ts +7 -0
- package/src/auth/LightsparkAuthException.ts +11 -0
- package/src/auth/StubAuthProvider.ts +16 -0
- package/src/auth/index.ts +5 -0
- package/src/crypto/LightsparkSigningException.ts +11 -0
- package/src/crypto/NodeKeyCache.ts +46 -0
- package/src/crypto/crypto.ts +258 -0
- package/src/crypto/index.ts +5 -0
- package/src/index.ts +11 -0
- package/src/requester/Query.ts +17 -0
- package/src/requester/Requester.ts +226 -0
- package/src/requester/index.ts +4 -0
- package/src/utils/base64.ts +15 -0
- package/src/utils/currency.ts +119 -0
- package/src/utils/environment.ts +7 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/types.ts +23 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
|
|
2
|
+
|
|
3
|
+
import LightsparkException from "../LightsparkException.js";
|
|
4
|
+
|
|
5
|
+
/** Represents the value and unit for an amount of currency. **/
|
|
6
|
+
type CurrencyAmount = {
|
|
7
|
+
/** The original numeric value for this CurrencyAmount. **/
|
|
8
|
+
originalValue: number;
|
|
9
|
+
|
|
10
|
+
/** The original unit of currency for this CurrencyAmount. **/
|
|
11
|
+
originalUnit: CurrencyUnit;
|
|
12
|
+
|
|
13
|
+
/** The unit of user's preferred currency. **/
|
|
14
|
+
preferredCurrencyUnit: CurrencyUnit;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The rounded numeric value for this CurrencyAmount in the very base level of user's preferred
|
|
18
|
+
* currency. For example, for USD, the value will be in cents.
|
|
19
|
+
**/
|
|
20
|
+
preferredCurrencyValueRounded: number;
|
|
21
|
+
|
|
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
|
+
|
|
29
|
+
enum CurrencyUnit {
|
|
30
|
+
/**
|
|
31
|
+
* This is an enum value that represents values that could be added in the future.
|
|
32
|
+
* Clients should support unknown values as more of them could be added without notice.
|
|
33
|
+
*/
|
|
34
|
+
FUTURE_VALUE = "FUTURE_VALUE",
|
|
35
|
+
/** Bitcoin is the cryptocurrency native to the Bitcoin network. It is used as the native medium for value transfer for the Lightning Network. **/
|
|
36
|
+
BITCOIN = "BITCOIN",
|
|
37
|
+
/** 0.00000001 (10e-8) Bitcoin or one hundred millionth of a Bitcoin. This is the unit most commonly used in Lightning transactions. **/
|
|
38
|
+
SATOSHI = "SATOSHI",
|
|
39
|
+
/** 0.001 Satoshi, or 10e-11 Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
40
|
+
MILLISATOSHI = "MILLISATOSHI",
|
|
41
|
+
/** United States Dollar. **/
|
|
42
|
+
USD = "USD",
|
|
43
|
+
/** 0.000000001 (10e-9) Bitcoin or a billionth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
44
|
+
NANOBITCOIN = "NANOBITCOIN",
|
|
45
|
+
/** 0.000001 (10e-6) Bitcoin or a millionth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
46
|
+
MICROBITCOIN = "MICROBITCOIN",
|
|
47
|
+
/** 0.001 (10e-3) Bitcoin or a thousandth of a Bitcoin. We recommend using the Satoshi unit instead when possible. **/
|
|
48
|
+
MILLIBITCOIN = "MILLIBITCOIN",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const CONVERSION_MAP = {
|
|
52
|
+
[CurrencyUnit.BITCOIN]: {
|
|
53
|
+
[CurrencyUnit.BITCOIN]: (v: number) => v,
|
|
54
|
+
[CurrencyUnit.MICROBITCOIN]: (v: number) => v * 1_000_000,
|
|
55
|
+
[CurrencyUnit.MILLIBITCOIN]: (v: number) => v * 1000,
|
|
56
|
+
[CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100_000_000_000,
|
|
57
|
+
[CurrencyUnit.NANOBITCOIN]: (v: number) => v * 1_000_000_000,
|
|
58
|
+
[CurrencyUnit.SATOSHI]: (v: number) => v * 100_000_000,
|
|
59
|
+
},
|
|
60
|
+
[CurrencyUnit.MICROBITCOIN]: {
|
|
61
|
+
[CurrencyUnit.BITCOIN]: (v: number) => Math.round(v / 1_000_000),
|
|
62
|
+
[CurrencyUnit.MICROBITCOIN]: (v: number) => v,
|
|
63
|
+
[CurrencyUnit.MILLIBITCOIN]: (v: number) => Math.round(v / 1000),
|
|
64
|
+
[CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100_000,
|
|
65
|
+
[CurrencyUnit.NANOBITCOIN]: (v: number) => v * 1000,
|
|
66
|
+
[CurrencyUnit.SATOSHI]: (v: number) => v * 100,
|
|
67
|
+
},
|
|
68
|
+
[CurrencyUnit.MILLIBITCOIN]: {
|
|
69
|
+
[CurrencyUnit.BITCOIN]: (v: number) => Math.round(v / 1_000),
|
|
70
|
+
[CurrencyUnit.MICROBITCOIN]: (v: number) => v * 1000,
|
|
71
|
+
[CurrencyUnit.MILLIBITCOIN]: (v: number) => v,
|
|
72
|
+
[CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100_000_000,
|
|
73
|
+
[CurrencyUnit.NANOBITCOIN]: (v: number) => v * 1_000_000,
|
|
74
|
+
[CurrencyUnit.SATOSHI]: (v: number) => v * 100_000,
|
|
75
|
+
},
|
|
76
|
+
[CurrencyUnit.MILLISATOSHI]: {
|
|
77
|
+
[CurrencyUnit.BITCOIN]: (v: number) => Math.round(v / 100_000_000_000),
|
|
78
|
+
[CurrencyUnit.MICROBITCOIN]: (v: number) => Math.round(v / 100_000),
|
|
79
|
+
[CurrencyUnit.MILLIBITCOIN]: (v: number) => Math.round(v / 100_000_000),
|
|
80
|
+
[CurrencyUnit.MILLISATOSHI]: (v: number) => v,
|
|
81
|
+
[CurrencyUnit.NANOBITCOIN]: (v: number) => Math.round(v / 100),
|
|
82
|
+
[CurrencyUnit.SATOSHI]: (v: number) => Math.round(v / 1000),
|
|
83
|
+
},
|
|
84
|
+
[CurrencyUnit.NANOBITCOIN]: {
|
|
85
|
+
[CurrencyUnit.BITCOIN]: (v: number) => Math.round(v / 1_000_000_000),
|
|
86
|
+
[CurrencyUnit.MICROBITCOIN]: (v: number) => Math.round(v / 1000),
|
|
87
|
+
[CurrencyUnit.MILLIBITCOIN]: (v: number) => Math.round(v / 1_000_000),
|
|
88
|
+
[CurrencyUnit.MILLISATOSHI]: (v: number) => v * 100,
|
|
89
|
+
[CurrencyUnit.NANOBITCOIN]: (v: number) => v,
|
|
90
|
+
[CurrencyUnit.SATOSHI]: (v: number) => Math.round(v / 10),
|
|
91
|
+
},
|
|
92
|
+
[CurrencyUnit.SATOSHI]: {
|
|
93
|
+
[CurrencyUnit.BITCOIN]: (v: number) => Math.round(v / 100_000_000),
|
|
94
|
+
[CurrencyUnit.MICROBITCOIN]: (v: number) => Math.round(v / 100),
|
|
95
|
+
[CurrencyUnit.MILLIBITCOIN]: (v: number) => Math.round(v / 100_000),
|
|
96
|
+
[CurrencyUnit.MILLISATOSHI]: (v: number) => v * 1000,
|
|
97
|
+
[CurrencyUnit.NANOBITCOIN]: (v: number) => v * 10,
|
|
98
|
+
[CurrencyUnit.SATOSHI]: (v: number) => v,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const convertCurrencyAmount = (
|
|
103
|
+
from: CurrencyAmount,
|
|
104
|
+
toUnit: CurrencyUnit
|
|
105
|
+
): CurrencyAmount => {
|
|
106
|
+
const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
|
|
107
|
+
if (!conversionFn) {
|
|
108
|
+
throw new LightsparkException(
|
|
109
|
+
"CurrencyError",
|
|
110
|
+
`Cannot convert from ${from.originalUnit} to ${toUnit}`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
...from,
|
|
115
|
+
preferredCurrencyUnit: toUnit,
|
|
116
|
+
preferredCurrencyValueApprox: conversionFn(from.originalValue),
|
|
117
|
+
preferredCurrencyValueRounded: conversionFn(from.originalValue),
|
|
118
|
+
};
|
|
119
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright ©, 2023, Lightspark Group, Inc. - All Rights Reserved
|
|
2
|
+
|
|
3
|
+
export type Maybe<T> = T | null | undefined;
|
|
4
|
+
|
|
5
|
+
export type ExpandRecursively<T> = T extends object
|
|
6
|
+
? T extends infer O
|
|
7
|
+
? { [K in keyof O]: ExpandRecursively<O[K]> } // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
8
|
+
: never
|
|
9
|
+
: T;
|
|
10
|
+
|
|
11
|
+
export type ById<T> = {
|
|
12
|
+
[id: string]: T;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type OmitTypename<T> = Omit<T, "__typename">;
|
|
16
|
+
|
|
17
|
+
export const isType =
|
|
18
|
+
<T extends string>(typename: T) =>
|
|
19
|
+
<N extends { __typename: string }>(
|
|
20
|
+
node: N | undefined | null
|
|
21
|
+
): node is Extract<N, { __typename: T }> => {
|
|
22
|
+
return node?.__typename === typename;
|
|
23
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
|
|
4
|
+
"module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
|
|
5
|
+
"lib": [
|
|
6
|
+
"DOM",
|
|
7
|
+
"ESNext"
|
|
8
|
+
] /* Specify library files to be included in the compilation. */,
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"declaration": true /* Generates corresponding '.d.ts' file. */,
|
|
11
|
+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
|
12
|
+
"sourceMap": true /* Generates corresponding '.map' file. */,
|
|
13
|
+
// "outFile": "./", /* Concatenate and emit output to single file. */
|
|
14
|
+
"outDir": "dist" /* Redirect output structure to the directory. */,
|
|
15
|
+
/* Strict Type-Checking Options */
|
|
16
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
17
|
+
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
|
|
18
|
+
"strictNullChecks": true /* Enable strict null checks. */,
|
|
19
|
+
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
|
|
20
|
+
/* Module Resolution Options */
|
|
21
|
+
"baseUrl": "./src" /* Base directory to resolve non-absolute module names. */,
|
|
22
|
+
"moduleResolution": "node16",
|
|
23
|
+
"types": [
|
|
24
|
+
"node"
|
|
25
|
+
] /* Type declaration files to be included in compilation. */,
|
|
26
|
+
"resolveJsonModule": true,
|
|
27
|
+
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
|
|
28
|
+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
|
|
29
|
+
/* Advanced Options */
|
|
30
|
+
"skipLibCheck": true /* Skip type checking of declaration files. */,
|
|
31
|
+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
|
32
|
+
},
|
|
33
|
+
"include": ["src"],
|
|
34
|
+
"exclude": ["test", "node_modules", "dist"]
|
|
35
|
+
}
|