@lightsparkdev/core 1.4.2 → 1.4.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 +14 -0
- package/README.md +0 -2
- package/dist/{chunk-CP4LQTTD.js → chunk-ADHQHZNM.js} +50 -18
- package/dist/chunk-VY7NND44.js +713 -0
- package/dist/{index-BCTAeaWD.d.cts → index-CFQtMxrx.d.cts} +18 -2
- package/dist/{index-BCTAeaWD.d.ts → index-CFQtMxrx.d.ts} +18 -2
- package/dist/index.cjs +2586 -2532
- package/dist/index.d.cts +8 -171
- package/dist/index.d.ts +8 -171
- package/dist/index.js +34 -674
- package/dist/react-native/index.cjs +3111 -0
- package/dist/react-native/index.d.cts +175 -0
- package/dist/react-native/index.d.ts +175 -0
- package/dist/react-native/index.js +154 -0
- package/dist/utils/index.cjs +45 -10
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +7 -1
- package/package.json +15 -1
- package/src/Logger.ts +2 -1
- package/src/crypto/SigningKey.ts +4 -2
- package/src/index.ts +3 -11
- package/src/react-native/index.ts +2 -0
- package/src/requester/DefaultRequester.ts +46 -0
- package/src/requester/Requester.ts +21 -37
- package/src/requester/tests/DefaultRequester.test.ts +129 -0
- package/src/requester/tests/Requester.test.ts +23 -32
- package/src/shared.ts +10 -0
- package/src/utils/currency.ts +42 -1
- package/src/utils/environment.ts +10 -0
package/dist/index.js
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConfigKeys,
|
|
3
|
+
DefaultCrypto,
|
|
4
|
+
KeyOrAlias,
|
|
5
|
+
LightsparkAuthException_default,
|
|
6
|
+
LightsparkSigningException_default,
|
|
7
|
+
Logger,
|
|
8
|
+
LoggingLevel,
|
|
9
|
+
NodeKeyCache_default,
|
|
10
|
+
RSASigningKey,
|
|
11
|
+
Requester_default,
|
|
12
|
+
Secp256k1SigningKey,
|
|
13
|
+
ServerEnvironment_default,
|
|
14
|
+
SigningKey,
|
|
15
|
+
SigningKeyType,
|
|
16
|
+
StubAuthProvider,
|
|
17
|
+
apiDomainForEnvironment,
|
|
18
|
+
logger
|
|
19
|
+
} from "./chunk-VY7NND44.js";
|
|
1
20
|
import {
|
|
2
21
|
CurrencyUnit,
|
|
3
22
|
LightsparkException_default,
|
|
@@ -15,12 +34,14 @@ import {
|
|
|
15
34
|
ensureArray,
|
|
16
35
|
errorToJSON,
|
|
17
36
|
formatCurrencyStr,
|
|
37
|
+
formatInviteAmount,
|
|
18
38
|
getCurrencyAmount,
|
|
19
39
|
getCurrentLocale,
|
|
20
40
|
getErrorMsg,
|
|
21
41
|
getLocalStorageBoolean,
|
|
22
42
|
getLocalStorageConfigItem,
|
|
23
43
|
hexToBytes,
|
|
44
|
+
isBare,
|
|
24
45
|
isBrowser,
|
|
25
46
|
isCurrencyAmountInputObj,
|
|
26
47
|
isCurrencyAmountObj,
|
|
@@ -33,6 +54,7 @@ import {
|
|
|
33
54
|
isNode,
|
|
34
55
|
isNumber,
|
|
35
56
|
isObject,
|
|
57
|
+
isReactNative,
|
|
36
58
|
isRecord,
|
|
37
59
|
isSDKCurrencyAmount,
|
|
38
60
|
isTest,
|
|
@@ -53,468 +75,17 @@ import {
|
|
|
53
75
|
urlsafe_b64decode,
|
|
54
76
|
zipcodeToState,
|
|
55
77
|
zipcodeToStateCode
|
|
56
|
-
} from "./chunk-
|
|
57
|
-
|
|
58
|
-
// src/auth/LightsparkAuthException.ts
|
|
59
|
-
var LightsparkAuthException = class extends LightsparkException_default {
|
|
60
|
-
constructor(message, extraInfo) {
|
|
61
|
-
super("AuthException", message, extraInfo);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
var LightsparkAuthException_default = LightsparkAuthException;
|
|
65
|
-
|
|
66
|
-
// src/auth/StubAuthProvider.ts
|
|
67
|
-
var StubAuthProvider = class {
|
|
68
|
-
addAuthHeaders(headers) {
|
|
69
|
-
return Promise.resolve(headers);
|
|
70
|
-
}
|
|
71
|
-
isAuthorized() {
|
|
72
|
-
return Promise.resolve(false);
|
|
73
|
-
}
|
|
74
|
-
addWsConnectionParams(params) {
|
|
75
|
-
return Promise.resolve(params);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// src/constants/index.ts
|
|
80
|
-
var ConfigKeys = {
|
|
81
|
-
LoggingEnabled: "lightspark-logging-enabled",
|
|
82
|
-
ConsoleToolsEnabled: "lightspark-console-tools-enabled"
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
// src/crypto/LightsparkSigningException.ts
|
|
86
|
-
var LightsparkSigningException = class extends LightsparkException_default {
|
|
87
|
-
constructor(message, extraInfo) {
|
|
88
|
-
super("SigningException", message, extraInfo);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
var LightsparkSigningException_default = LightsparkSigningException;
|
|
92
|
-
|
|
93
|
-
// src/crypto/crypto.ts
|
|
94
|
-
var getCrypto = () => {
|
|
95
|
-
let cryptoImplPromise;
|
|
96
|
-
if (typeof crypto !== "undefined") {
|
|
97
|
-
cryptoImplPromise = Promise.resolve(crypto);
|
|
98
|
-
} else {
|
|
99
|
-
cryptoImplPromise = import("crypto").then((nodeCrypto) => {
|
|
100
|
-
let cryptoModule = nodeCrypto;
|
|
101
|
-
if (!nodeCrypto.subtle) {
|
|
102
|
-
cryptoModule = Object.assign({}, cryptoModule, {
|
|
103
|
-
subtle: nodeCrypto.webcrypto.subtle
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
if (!nodeCrypto.getRandomValues) {
|
|
107
|
-
cryptoModule = Object.assign({}, cryptoModule, {
|
|
108
|
-
getRandomValues: (array) => {
|
|
109
|
-
if (!array) {
|
|
110
|
-
return array;
|
|
111
|
-
}
|
|
112
|
-
const buffer = Buffer.from(array.buffer);
|
|
113
|
-
const view = new Uint8Array(buffer);
|
|
114
|
-
nodeCrypto.randomFillSync(view);
|
|
115
|
-
return array;
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
return cryptoModule;
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
return cryptoImplPromise;
|
|
123
|
-
};
|
|
124
|
-
var getRandomValues32 = async (arr) => {
|
|
125
|
-
if (typeof crypto !== "undefined") {
|
|
126
|
-
return crypto.getRandomValues(arr);
|
|
127
|
-
} else {
|
|
128
|
-
const cryptoImpl = await getCrypto();
|
|
129
|
-
return cryptoImpl.getRandomValues(arr);
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
var deriveKey = async (password, salt, iterations, algorithm, bit_len) => {
|
|
133
|
-
const enc = new TextEncoder();
|
|
134
|
-
const cryptoImpl = await getCrypto();
|
|
135
|
-
const password_key = await cryptoImpl.subtle.importKey(
|
|
136
|
-
"raw",
|
|
137
|
-
enc.encode(password),
|
|
138
|
-
"PBKDF2",
|
|
139
|
-
false,
|
|
140
|
-
["deriveBits", "deriveKey"]
|
|
141
|
-
);
|
|
142
|
-
const derived = await cryptoImpl.subtle.deriveBits(
|
|
143
|
-
{
|
|
144
|
-
name: "PBKDF2",
|
|
145
|
-
salt,
|
|
146
|
-
iterations,
|
|
147
|
-
hash: "SHA-256"
|
|
148
|
-
},
|
|
149
|
-
password_key,
|
|
150
|
-
bit_len
|
|
151
|
-
);
|
|
152
|
-
const key = await cryptoImpl.subtle.importKey(
|
|
153
|
-
"raw",
|
|
154
|
-
derived.slice(0, 32),
|
|
155
|
-
{ name: algorithm, length: 256 },
|
|
156
|
-
false,
|
|
157
|
-
["encrypt", "decrypt"]
|
|
158
|
-
);
|
|
159
|
-
const iv = derived.slice(32);
|
|
160
|
-
return [key, iv];
|
|
161
|
-
};
|
|
162
|
-
var decrypt = async (header_json, ciphertext, password) => {
|
|
163
|
-
let decoded = b64decode(ciphertext);
|
|
164
|
-
let header;
|
|
165
|
-
if (header_json === "AES_256_CBC_PBKDF2_5000_SHA256") {
|
|
166
|
-
header = {
|
|
167
|
-
v: 0,
|
|
168
|
-
i: 5e3
|
|
169
|
-
};
|
|
170
|
-
decoded = decoded.slice(8);
|
|
171
|
-
} else {
|
|
172
|
-
header = JSON.parse(header_json);
|
|
173
|
-
}
|
|
174
|
-
if (header.v < 0 || header.v > 4) {
|
|
175
|
-
throw new LightsparkException_default(
|
|
176
|
-
"DecryptionError",
|
|
177
|
-
`Unknown version ${header.v}`
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
const cryptoImpl = await getCrypto();
|
|
181
|
-
const algorithm = header.v < 2 ? "AES-CBC" : "AES-GCM";
|
|
182
|
-
const bit_len = header.v < 4 ? 384 : 352;
|
|
183
|
-
const salt_len = header.v < 4 ? 8 : 16;
|
|
184
|
-
if (header.lsv === 2 || header.v === 3) {
|
|
185
|
-
const salt = decoded.slice(decoded.length - 8, decoded.length);
|
|
186
|
-
const nonce = decoded.slice(0, 12);
|
|
187
|
-
const cipherText = decoded.slice(12, decoded.length - 8);
|
|
188
|
-
const [
|
|
189
|
-
key
|
|
190
|
-
/* , _iv */
|
|
191
|
-
] = await deriveKey(
|
|
192
|
-
password,
|
|
193
|
-
salt,
|
|
194
|
-
header.i,
|
|
195
|
-
algorithm,
|
|
196
|
-
256
|
|
197
|
-
);
|
|
198
|
-
return await cryptoImpl.subtle.decrypt(
|
|
199
|
-
{ name: algorithm, iv: nonce.buffer },
|
|
200
|
-
key,
|
|
201
|
-
cipherText
|
|
202
|
-
);
|
|
203
|
-
} else {
|
|
204
|
-
const salt = decoded.slice(0, salt_len);
|
|
205
|
-
const encrypted = decoded.slice(salt_len);
|
|
206
|
-
const [key, iv] = await deriveKey(
|
|
207
|
-
password,
|
|
208
|
-
salt,
|
|
209
|
-
header.i,
|
|
210
|
-
algorithm,
|
|
211
|
-
bit_len
|
|
212
|
-
);
|
|
213
|
-
return await cryptoImpl.subtle.decrypt(
|
|
214
|
-
{ name: algorithm, iv },
|
|
215
|
-
key,
|
|
216
|
-
encrypted
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
async function decryptSecretWithNodePassword(cipher, encryptedSecret, nodePassword) {
|
|
221
|
-
let decryptedValue = null;
|
|
222
|
-
try {
|
|
223
|
-
decryptedValue = await decrypt(cipher, encryptedSecret, nodePassword);
|
|
224
|
-
} catch (ex) {
|
|
225
|
-
console.error(ex);
|
|
226
|
-
}
|
|
227
|
-
return decryptedValue;
|
|
228
|
-
}
|
|
229
|
-
var generateSigningKeyPair = async () => {
|
|
230
|
-
const cryptoImpl = await getCrypto();
|
|
231
|
-
return await cryptoImpl.subtle.generateKey(
|
|
232
|
-
/*algorithm:*/
|
|
233
|
-
{
|
|
234
|
-
name: "RSA-PSS",
|
|
235
|
-
modulusLength: 4096,
|
|
236
|
-
publicExponent: new Uint8Array([1, 0, 1]),
|
|
237
|
-
hash: "SHA-256"
|
|
238
|
-
},
|
|
239
|
-
/*extractable*/
|
|
240
|
-
true,
|
|
241
|
-
/*keyUsages*/
|
|
242
|
-
["sign", "verify"]
|
|
243
|
-
);
|
|
244
|
-
};
|
|
245
|
-
var serializeSigningKey = async (key, format) => {
|
|
246
|
-
const cryptoImpl = await getCrypto();
|
|
247
|
-
return await cryptoImpl.subtle.exportKey(
|
|
248
|
-
/*format*/
|
|
249
|
-
format,
|
|
250
|
-
/*key*/
|
|
251
|
-
key
|
|
252
|
-
);
|
|
253
|
-
};
|
|
254
|
-
var getNonce = async () => {
|
|
255
|
-
const nonceSt = await getRandomValues32(new Uint32Array(2));
|
|
256
|
-
const [upper, lower] = nonceSt;
|
|
257
|
-
const nonce = BigInt(upper) << 32n | BigInt(lower);
|
|
258
|
-
return Number(nonce);
|
|
259
|
-
};
|
|
260
|
-
var sign = async (keyOrAlias, data) => {
|
|
261
|
-
if (typeof keyOrAlias === "string") {
|
|
262
|
-
throw new LightsparkSigningException_default(
|
|
263
|
-
"Key alias not supported for default crypto."
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
const cryptoImpl = await getCrypto();
|
|
267
|
-
return await cryptoImpl.subtle.sign(
|
|
268
|
-
{
|
|
269
|
-
name: "RSA-PSS",
|
|
270
|
-
saltLength: 32
|
|
271
|
-
},
|
|
272
|
-
keyOrAlias,
|
|
273
|
-
data
|
|
274
|
-
);
|
|
275
|
-
};
|
|
276
|
-
var importPrivateSigningKey = async (keyData) => {
|
|
277
|
-
const cryptoImpl = await getCrypto();
|
|
278
|
-
return await cryptoImpl.subtle.importKey(
|
|
279
|
-
/*format*/
|
|
280
|
-
"pkcs8",
|
|
281
|
-
/*keyData*/
|
|
282
|
-
keyData,
|
|
283
|
-
/*algorithm*/
|
|
284
|
-
{
|
|
285
|
-
name: "RSA-PSS",
|
|
286
|
-
hash: "SHA-256"
|
|
287
|
-
},
|
|
288
|
-
/*extractable*/
|
|
289
|
-
true,
|
|
290
|
-
/*keyUsages*/
|
|
291
|
-
["sign"]
|
|
292
|
-
);
|
|
293
|
-
};
|
|
294
|
-
var DefaultCrypto = {
|
|
295
|
-
decryptSecretWithNodePassword,
|
|
296
|
-
generateSigningKeyPair,
|
|
297
|
-
serializeSigningKey,
|
|
298
|
-
getNonce,
|
|
299
|
-
sign,
|
|
300
|
-
importPrivateSigningKey
|
|
301
|
-
};
|
|
78
|
+
} from "./chunk-ADHQHZNM.js";
|
|
302
79
|
|
|
303
|
-
// src/
|
|
304
|
-
var
|
|
305
|
-
key: (key) => ({ key }),
|
|
306
|
-
alias: (alias) => ({ alias })
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
// ../../node_modules/auto-bind/index.js
|
|
310
|
-
var getAllProperties = (object) => {
|
|
311
|
-
const properties = /* @__PURE__ */ new Set();
|
|
312
|
-
do {
|
|
313
|
-
for (const key of Reflect.ownKeys(object)) {
|
|
314
|
-
properties.add([object, key]);
|
|
315
|
-
}
|
|
316
|
-
} while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);
|
|
317
|
-
return properties;
|
|
318
|
-
};
|
|
319
|
-
function autoBind(self, { include, exclude } = {}) {
|
|
320
|
-
const filter = (key) => {
|
|
321
|
-
const match = (pattern) => typeof pattern === "string" ? key === pattern : pattern.test(key);
|
|
322
|
-
if (include) {
|
|
323
|
-
return include.some(match);
|
|
324
|
-
}
|
|
325
|
-
if (exclude) {
|
|
326
|
-
return !exclude.some(match);
|
|
327
|
-
}
|
|
328
|
-
return true;
|
|
329
|
-
};
|
|
330
|
-
for (const [object, key] of getAllProperties(self.constructor.prototype)) {
|
|
331
|
-
if (key === "constructor" || !filter(key)) {
|
|
332
|
-
continue;
|
|
333
|
-
}
|
|
334
|
-
const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
|
|
335
|
-
if (descriptor && typeof descriptor.value === "function") {
|
|
336
|
-
self[key] = self[key].bind(self);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return self;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// src/crypto/SigningKey.ts
|
|
343
|
-
import secp256k1 from "secp256k1";
|
|
344
|
-
function isAlias(key) {
|
|
345
|
-
return "alias" in key;
|
|
346
|
-
}
|
|
347
|
-
var SigningKey = class {
|
|
348
|
-
type;
|
|
349
|
-
constructor(type) {
|
|
350
|
-
this.type = type;
|
|
351
|
-
}
|
|
352
|
-
};
|
|
353
|
-
var RSASigningKey = class extends SigningKey {
|
|
354
|
-
constructor(privateKey, cryptoImpl) {
|
|
355
|
-
super("RSASigningKey" /* RSASigningKey */);
|
|
356
|
-
this.privateKey = privateKey;
|
|
357
|
-
this.cryptoImpl = cryptoImpl;
|
|
358
|
-
}
|
|
359
|
-
async sign(data) {
|
|
360
|
-
const key = isAlias(this.privateKey) ? this.privateKey.alias : this.privateKey;
|
|
361
|
-
return this.cryptoImpl.sign(key, data);
|
|
362
|
-
}
|
|
363
|
-
};
|
|
364
|
-
var Secp256k1SigningKey = class extends SigningKey {
|
|
365
|
-
constructor(privateKey) {
|
|
366
|
-
super("Secp256k1SigningKey" /* Secp256k1SigningKey */);
|
|
367
|
-
this.privateKey = privateKey;
|
|
368
|
-
}
|
|
369
|
-
async sign(data) {
|
|
370
|
-
const keyBytes = new Uint8Array(hexToBytes(this.privateKey));
|
|
371
|
-
const hash = await createSha256Hash(data);
|
|
372
|
-
const signResult = secp256k1.ecdsaSign(hash, keyBytes);
|
|
373
|
-
return secp256k1.signatureExport(signResult.signature);
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
// src/crypto/types.ts
|
|
378
|
-
var SigningKeyType = /* @__PURE__ */ ((SigningKeyType2) => {
|
|
379
|
-
SigningKeyType2["RSASigningKey"] = "RSASigningKey";
|
|
380
|
-
SigningKeyType2["Secp256k1SigningKey"] = "Secp256k1SigningKey";
|
|
381
|
-
return SigningKeyType2;
|
|
382
|
-
})(SigningKeyType || {});
|
|
383
|
-
|
|
384
|
-
// src/crypto/NodeKeyCache.ts
|
|
385
|
-
var NodeKeyCache = class {
|
|
386
|
-
constructor(cryptoImpl = DefaultCrypto) {
|
|
387
|
-
this.cryptoImpl = cryptoImpl;
|
|
388
|
-
this.idToKey = /* @__PURE__ */ new Map();
|
|
389
|
-
autoBind(this);
|
|
390
|
-
}
|
|
391
|
-
idToKey;
|
|
392
|
-
async loadKey(id, keyOrAlias, signingKeyType) {
|
|
393
|
-
let signingKey;
|
|
394
|
-
if (keyOrAlias.alias !== void 0) {
|
|
395
|
-
switch (signingKeyType) {
|
|
396
|
-
case "RSASigningKey" /* RSASigningKey */:
|
|
397
|
-
signingKey = new RSASigningKey(
|
|
398
|
-
{ alias: keyOrAlias.alias },
|
|
399
|
-
this.cryptoImpl
|
|
400
|
-
);
|
|
401
|
-
break;
|
|
402
|
-
default:
|
|
403
|
-
throw new LightsparkSigningException_default(
|
|
404
|
-
`Aliases are not supported for signing key type ${signingKeyType}`
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
this.idToKey.set(id, signingKey);
|
|
408
|
-
return signingKey;
|
|
409
|
-
}
|
|
410
|
-
try {
|
|
411
|
-
if (signingKeyType === "Secp256k1SigningKey" /* Secp256k1SigningKey */) {
|
|
412
|
-
signingKey = new Secp256k1SigningKey(keyOrAlias.key);
|
|
413
|
-
} else {
|
|
414
|
-
const decoded = b64decode(this.stripPemTags(keyOrAlias.key));
|
|
415
|
-
const cryptoKeyOrAlias = await this.cryptoImpl.importPrivateSigningKey(decoded);
|
|
416
|
-
const key = typeof cryptoKeyOrAlias === "string" ? { alias: cryptoKeyOrAlias } : cryptoKeyOrAlias;
|
|
417
|
-
signingKey = new RSASigningKey(key, this.cryptoImpl);
|
|
418
|
-
}
|
|
419
|
-
this.idToKey.set(id, signingKey);
|
|
420
|
-
return signingKey;
|
|
421
|
-
} catch (e) {
|
|
422
|
-
console.log("Error importing key: ", e);
|
|
423
|
-
}
|
|
424
|
-
return null;
|
|
425
|
-
}
|
|
426
|
-
getKey(id) {
|
|
427
|
-
return this.idToKey.get(id);
|
|
428
|
-
}
|
|
429
|
-
hasKey(id) {
|
|
430
|
-
return this.idToKey.has(id);
|
|
431
|
-
}
|
|
432
|
-
stripPemTags(pem) {
|
|
433
|
-
return pem.replace(/-----BEGIN (.*)-----/, "").replace(/-----END (.*)----/, "");
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
var NodeKeyCache_default = NodeKeyCache;
|
|
437
|
-
|
|
438
|
-
// src/Logger.ts
|
|
439
|
-
var LoggingLevel = /* @__PURE__ */ ((LoggingLevel2) => {
|
|
440
|
-
LoggingLevel2[LoggingLevel2["Trace"] = 0] = "Trace";
|
|
441
|
-
LoggingLevel2[LoggingLevel2["Info"] = 1] = "Info";
|
|
442
|
-
return LoggingLevel2;
|
|
443
|
-
})(LoggingLevel || {});
|
|
444
|
-
var Logger = class {
|
|
445
|
-
context;
|
|
446
|
-
loggingEnabled = false;
|
|
447
|
-
loggingLevel = 1 /* Info */;
|
|
448
|
-
constructor(loggerContext, getLoggingEnabled) {
|
|
449
|
-
this.context = loggerContext;
|
|
450
|
-
void this.updateLoggingEnabled(getLoggingEnabled);
|
|
451
|
-
}
|
|
452
|
-
setLevel(level) {
|
|
453
|
-
this.loggingLevel = level;
|
|
454
|
-
}
|
|
455
|
-
setEnabled(enabled, level = 1 /* Info */) {
|
|
456
|
-
this.loggingEnabled = enabled;
|
|
457
|
-
this.loggingLevel = level;
|
|
458
|
-
}
|
|
459
|
-
async updateLoggingEnabled(getLoggingEnabled) {
|
|
460
|
-
if (getLoggingEnabled) {
|
|
461
|
-
this.loggingEnabled = await getLoggingEnabled();
|
|
462
|
-
} else if (isTest) {
|
|
463
|
-
this.loggingEnabled = true;
|
|
464
|
-
} else if (isBrowser) {
|
|
465
|
-
try {
|
|
466
|
-
this.loggingEnabled = getLocalStorageConfigItem(
|
|
467
|
-
ConfigKeys.LoggingEnabled
|
|
468
|
-
);
|
|
469
|
-
} catch (e) {
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
if (this.loggingEnabled) {
|
|
473
|
-
console.log(`[${this.context}] Logging enabled`);
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
trace(message, ...rest) {
|
|
477
|
-
if (this.loggingEnabled && this.loggingLevel === 0 /* Trace */) {
|
|
478
|
-
console.log(`[${this.context}] ${message}`, ...rest);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
info(message, ...rest) {
|
|
482
|
-
if (this.loggingEnabled && this.loggingLevel <= 1 /* Info */) {
|
|
483
|
-
console.log(`[${this.context}] ${message}`, ...rest);
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
var logger = new Logger("@lightsparkdev/core");
|
|
488
|
-
|
|
489
|
-
// src/requester/Requester.ts
|
|
490
|
-
import dayjs from "dayjs";
|
|
491
|
-
import utc from "dayjs/plugin/utc.js";
|
|
492
|
-
import { createClient } from "graphql-ws";
|
|
493
|
-
import { Observable } from "zen-observable-ts";
|
|
494
|
-
var DEFAULT_BASE_URL = "api.lightspark.com";
|
|
495
|
-
dayjs.extend(utc);
|
|
496
|
-
var Requester = class {
|
|
497
|
-
constructor(nodeKeyCache, schemaEndpoint, sdkUserAgent, authProvider = new StubAuthProvider(), baseUrl = DEFAULT_BASE_URL, cryptoImpl = DefaultCrypto, signingKey, fetchImpl = fetch) {
|
|
498
|
-
this.nodeKeyCache = nodeKeyCache;
|
|
499
|
-
this.schemaEndpoint = schemaEndpoint;
|
|
500
|
-
this.sdkUserAgent = sdkUserAgent;
|
|
501
|
-
this.authProvider = authProvider;
|
|
502
|
-
this.baseUrl = baseUrl;
|
|
503
|
-
this.cryptoImpl = cryptoImpl;
|
|
504
|
-
this.signingKey = signingKey;
|
|
505
|
-
this.fetchImpl = fetchImpl;
|
|
506
|
-
this.wsClient = new Promise((resolve) => {
|
|
507
|
-
this.resolveWsClient = resolve;
|
|
508
|
-
});
|
|
509
|
-
void this.initWsClient(baseUrl, authProvider);
|
|
510
|
-
autoBind(this);
|
|
511
|
-
}
|
|
512
|
-
wsClient;
|
|
513
|
-
resolveWsClient = null;
|
|
80
|
+
// src/requester/DefaultRequester.ts
|
|
81
|
+
var DefaultRequester = class extends Requester_default {
|
|
514
82
|
async initWsClient(baseUrl, authProvider) {
|
|
515
83
|
if (!this.resolveWsClient) {
|
|
516
84
|
return this.wsClient;
|
|
517
85
|
}
|
|
86
|
+
if (isBare) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
518
89
|
let websocketImpl;
|
|
519
90
|
if (isNode && typeof WebSocket === "undefined") {
|
|
520
91
|
const wsModule = await import("ws");
|
|
@@ -524,6 +95,8 @@ var Requester = class {
|
|
|
524
95
|
if (baseUrl.startsWith("http://")) {
|
|
525
96
|
websocketProtocol = "ws";
|
|
526
97
|
}
|
|
98
|
+
const graphqlWsModule = await import("graphql-ws");
|
|
99
|
+
const { createClient } = graphqlWsModule;
|
|
527
100
|
const wsClient = createClient({
|
|
528
101
|
url: `${websocketProtocol}://${this.stripProtocol(this.baseUrl)}/${this.schemaEndpoint}`,
|
|
529
102
|
connectionParams: () => authProvider.addWsConnectionParams({}),
|
|
@@ -535,223 +108,7 @@ var Requester = class {
|
|
|
535
108
|
}
|
|
536
109
|
return wsClient;
|
|
537
110
|
}
|
|
538
|
-
async executeQuery(query) {
|
|
539
|
-
const data = await this.makeRawRequest(
|
|
540
|
-
query.queryPayload,
|
|
541
|
-
query.variables || {},
|
|
542
|
-
query.signingNodeId,
|
|
543
|
-
!!query.skipAuth
|
|
544
|
-
);
|
|
545
|
-
return query.constructObject(data);
|
|
546
|
-
}
|
|
547
|
-
subscribe(queryPayload, variables = {}) {
|
|
548
|
-
logger.trace(`Requester.subscribe variables`, variables);
|
|
549
|
-
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
550
|
-
const operationMatch = queryPayload.match(operationNameRegex);
|
|
551
|
-
if (!operationMatch || operationMatch.length < 3) {
|
|
552
|
-
throw new LightsparkException_default("InvalidQuery", "Invalid query payload");
|
|
553
|
-
}
|
|
554
|
-
const operationType = operationMatch[1];
|
|
555
|
-
logger.trace(`Requester.subscribe operationType`, operationType);
|
|
556
|
-
if (operationType == "mutation") {
|
|
557
|
-
throw new LightsparkException_default(
|
|
558
|
-
"InvalidQuery",
|
|
559
|
-
"Mutation queries should call makeRawRequest instead"
|
|
560
|
-
);
|
|
561
|
-
}
|
|
562
|
-
for (const key in variables) {
|
|
563
|
-
if (variables[key] === void 0) {
|
|
564
|
-
variables[key] = null;
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
const operation = operationMatch[2];
|
|
568
|
-
const bodyData = {
|
|
569
|
-
query: queryPayload,
|
|
570
|
-
variables,
|
|
571
|
-
operationName: operation
|
|
572
|
-
};
|
|
573
|
-
return new Observable((observer) => {
|
|
574
|
-
logger.trace(`Requester.subscribe observer`, observer);
|
|
575
|
-
let cleanup = null;
|
|
576
|
-
let canceled = false;
|
|
577
|
-
void (async () => {
|
|
578
|
-
try {
|
|
579
|
-
const wsClient = await this.wsClient;
|
|
580
|
-
if (!canceled) {
|
|
581
|
-
cleanup = wsClient.subscribe(bodyData, {
|
|
582
|
-
next: (data) => observer.next(data),
|
|
583
|
-
error: (err) => observer.error(err),
|
|
584
|
-
complete: () => observer.complete()
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
} catch (err) {
|
|
588
|
-
observer.error(err);
|
|
589
|
-
}
|
|
590
|
-
})();
|
|
591
|
-
return () => {
|
|
592
|
-
canceled = true;
|
|
593
|
-
if (cleanup) {
|
|
594
|
-
cleanup();
|
|
595
|
-
}
|
|
596
|
-
};
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
|
|
600
|
-
logger.trace(`Requester.makeRawRequest args`, {
|
|
601
|
-
queryPayload,
|
|
602
|
-
variables,
|
|
603
|
-
signingNodeId,
|
|
604
|
-
skipAuth
|
|
605
|
-
});
|
|
606
|
-
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
607
|
-
const operationMatch = queryPayload.match(operationNameRegex);
|
|
608
|
-
if (!operationMatch || operationMatch.length < 3) {
|
|
609
|
-
throw new LightsparkException_default("InvalidQuery", "Invalid query payload");
|
|
610
|
-
}
|
|
611
|
-
const operationType = operationMatch[1];
|
|
612
|
-
if (operationType == "subscription") {
|
|
613
|
-
throw new LightsparkException_default(
|
|
614
|
-
"InvalidQuery",
|
|
615
|
-
"Subscription queries should call subscribe instead"
|
|
616
|
-
);
|
|
617
|
-
}
|
|
618
|
-
for (const key in variables) {
|
|
619
|
-
if (variables[key] === void 0) {
|
|
620
|
-
variables[key] = null;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
const operation = operationMatch[2];
|
|
624
|
-
const payload = {
|
|
625
|
-
query: queryPayload,
|
|
626
|
-
variables,
|
|
627
|
-
operationName: operation
|
|
628
|
-
};
|
|
629
|
-
const browserUserAgent = typeof navigator !== "undefined" ? navigator.userAgent : "";
|
|
630
|
-
const sdkUserAgent = this.getSdkUserAgent();
|
|
631
|
-
const baseHeaders = {
|
|
632
|
-
"Content-Type": "application/json",
|
|
633
|
-
"X-Lightspark-SDK": sdkUserAgent,
|
|
634
|
-
"User-Agent": browserUserAgent || sdkUserAgent,
|
|
635
|
-
"X-GraphQL-Operation": operation
|
|
636
|
-
};
|
|
637
|
-
const headers = skipAuth ? baseHeaders : await this.authProvider.addAuthHeaders(baseHeaders);
|
|
638
|
-
let bodyData = await this.addSigningDataIfNeeded(
|
|
639
|
-
payload,
|
|
640
|
-
headers,
|
|
641
|
-
signingNodeId
|
|
642
|
-
);
|
|
643
|
-
if (bodyData.length > 1024 && typeof CompressionStream != "undefined") {
|
|
644
|
-
bodyData = await compress(bodyData);
|
|
645
|
-
headers["Content-Encoding"] = "deflate";
|
|
646
|
-
}
|
|
647
|
-
let urlWithProtocol = this.baseUrl;
|
|
648
|
-
if (!urlWithProtocol.startsWith("https://") && !urlWithProtocol.startsWith("http://")) {
|
|
649
|
-
urlWithProtocol = `https://${urlWithProtocol}`;
|
|
650
|
-
}
|
|
651
|
-
const url = `${urlWithProtocol}/${this.schemaEndpoint}`;
|
|
652
|
-
logger.trace(`Requester.makeRawRequest`, {
|
|
653
|
-
url,
|
|
654
|
-
operationName: operation,
|
|
655
|
-
variables,
|
|
656
|
-
headers
|
|
657
|
-
});
|
|
658
|
-
const response = await this.fetchImpl(url, {
|
|
659
|
-
method: "POST",
|
|
660
|
-
headers,
|
|
661
|
-
body: bodyData
|
|
662
|
-
});
|
|
663
|
-
if (!response.ok) {
|
|
664
|
-
throw new LightsparkException_default(
|
|
665
|
-
"RequestFailed",
|
|
666
|
-
`Request ${operation} failed. ${response.statusText}`
|
|
667
|
-
);
|
|
668
|
-
}
|
|
669
|
-
const responseJson = await response.json();
|
|
670
|
-
const data = responseJson.data;
|
|
671
|
-
if (!data) {
|
|
672
|
-
let firstErrorName = void 0;
|
|
673
|
-
if (Array.isArray(responseJson.errors) && responseJson.errors.length > 0) {
|
|
674
|
-
const firstError = responseJson.errors[0];
|
|
675
|
-
firstErrorName = firstError["extensions"]?.["error_name"];
|
|
676
|
-
}
|
|
677
|
-
throw new LightsparkException_default(
|
|
678
|
-
"RequestFailed",
|
|
679
|
-
`Request ${operation} failed. ${JSON.stringify(responseJson.errors)}`,
|
|
680
|
-
{ errorName: firstErrorName }
|
|
681
|
-
);
|
|
682
|
-
}
|
|
683
|
-
return data;
|
|
684
|
-
}
|
|
685
|
-
getSdkUserAgent() {
|
|
686
|
-
const platform = isNode ? "NodeJS" : "Browser";
|
|
687
|
-
const platformVersion = isNode ? process.version : "";
|
|
688
|
-
return `${this.sdkUserAgent} ${platform}/${platformVersion}`;
|
|
689
|
-
}
|
|
690
|
-
stripProtocol(url) {
|
|
691
|
-
return url.replace(/.*?:\/\//g, "");
|
|
692
|
-
}
|
|
693
|
-
async addSigningDataIfNeeded(queryPayload, headers, signingNodeId) {
|
|
694
|
-
if (!signingNodeId) {
|
|
695
|
-
return new TextEncoder().encode(JSON.stringify(queryPayload));
|
|
696
|
-
}
|
|
697
|
-
const query = queryPayload.query;
|
|
698
|
-
const variables = queryPayload.variables;
|
|
699
|
-
const operationName = queryPayload.operationName;
|
|
700
|
-
const nonce = await this.cryptoImpl.getNonce();
|
|
701
|
-
const expiration = dayjs.utc().add(1, "hour").format();
|
|
702
|
-
const payload = {
|
|
703
|
-
query,
|
|
704
|
-
variables,
|
|
705
|
-
operationName,
|
|
706
|
-
nonce,
|
|
707
|
-
expires_at: expiration
|
|
708
|
-
};
|
|
709
|
-
const key = this.signingKey ?? this.nodeKeyCache.getKey(signingNodeId);
|
|
710
|
-
if (!key) {
|
|
711
|
-
throw new LightsparkSigningException_default(
|
|
712
|
-
"Missing node of encrypted_signing_private_key"
|
|
713
|
-
);
|
|
714
|
-
}
|
|
715
|
-
const encodedPayload = new TextEncoder().encode(JSON.stringify(payload));
|
|
716
|
-
const signedPayload = await key.sign(encodedPayload);
|
|
717
|
-
const encodedSignedPayload = b64encode(signedPayload);
|
|
718
|
-
headers["X-Lightspark-Signing"] = JSON.stringify({
|
|
719
|
-
v: "1",
|
|
720
|
-
signature: encodedSignedPayload
|
|
721
|
-
});
|
|
722
|
-
return encodedPayload;
|
|
723
|
-
}
|
|
724
|
-
};
|
|
725
|
-
async function compress(data) {
|
|
726
|
-
const stream = new Blob([data]).stream();
|
|
727
|
-
const compressedStream = stream.pipeThrough(new CompressionStream("deflate"));
|
|
728
|
-
const reader = compressedStream.getReader();
|
|
729
|
-
const chunks = [];
|
|
730
|
-
let done, value;
|
|
731
|
-
while (!done) {
|
|
732
|
-
({ done, value } = await reader.read());
|
|
733
|
-
chunks.push(value);
|
|
734
|
-
}
|
|
735
|
-
const blob = new Blob(chunks);
|
|
736
|
-
return new Uint8Array(await blob.arrayBuffer());
|
|
737
|
-
}
|
|
738
|
-
var Requester_default = Requester;
|
|
739
|
-
|
|
740
|
-
// src/ServerEnvironment.ts
|
|
741
|
-
var ServerEnvironment = /* @__PURE__ */ ((ServerEnvironment2) => {
|
|
742
|
-
ServerEnvironment2["PRODUCTION"] = "production";
|
|
743
|
-
ServerEnvironment2["DEV"] = "dev";
|
|
744
|
-
return ServerEnvironment2;
|
|
745
|
-
})(ServerEnvironment || {});
|
|
746
|
-
var apiDomainForEnvironment = (environment) => {
|
|
747
|
-
switch (environment) {
|
|
748
|
-
case "dev" /* DEV */:
|
|
749
|
-
return "api.dev.dev.sparkinfra.net";
|
|
750
|
-
case "production" /* PRODUCTION */:
|
|
751
|
-
return "api.lightspark.com";
|
|
752
|
-
}
|
|
753
111
|
};
|
|
754
|
-
var ServerEnvironment_default = ServerEnvironment;
|
|
755
112
|
export {
|
|
756
113
|
ConfigKeys,
|
|
757
114
|
CurrencyUnit,
|
|
@@ -764,7 +121,7 @@ export {
|
|
|
764
121
|
LoggingLevel,
|
|
765
122
|
NodeKeyCache_default as NodeKeyCache,
|
|
766
123
|
RSASigningKey,
|
|
767
|
-
|
|
124
|
+
DefaultRequester as Requester,
|
|
768
125
|
Secp256k1SigningKey,
|
|
769
126
|
ServerEnvironment_default as ServerEnvironment,
|
|
770
127
|
SigningKey,
|
|
@@ -785,12 +142,14 @@ export {
|
|
|
785
142
|
ensureArray,
|
|
786
143
|
errorToJSON,
|
|
787
144
|
formatCurrencyStr,
|
|
145
|
+
formatInviteAmount,
|
|
788
146
|
getCurrencyAmount,
|
|
789
147
|
getCurrentLocale,
|
|
790
148
|
getErrorMsg,
|
|
791
149
|
getLocalStorageBoolean,
|
|
792
150
|
getLocalStorageConfigItem,
|
|
793
151
|
hexToBytes,
|
|
152
|
+
isBare,
|
|
794
153
|
isBrowser,
|
|
795
154
|
isCurrencyAmountInputObj,
|
|
796
155
|
isCurrencyAmountObj,
|
|
@@ -803,6 +162,7 @@ export {
|
|
|
803
162
|
isNode,
|
|
804
163
|
isNumber,
|
|
805
164
|
isObject,
|
|
165
|
+
isReactNative,
|
|
806
166
|
isRecord,
|
|
807
167
|
isSDKCurrencyAmount,
|
|
808
168
|
isTest,
|