@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
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,663 @@
|
|
|
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/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
LightsparkAuthException: () => LightsparkAuthException_default,
|
|
34
|
+
LightsparkException: () => LightsparkException_default,
|
|
35
|
+
LightsparkSigningException: () => LightsparkSigningException_default,
|
|
36
|
+
NodeKeyCache: () => NodeKeyCache_default,
|
|
37
|
+
Requester: () => Requester_default,
|
|
38
|
+
ServerEnvironment: () => ServerEnvironment_default,
|
|
39
|
+
StubAuthProvider: () => StubAuthProvider,
|
|
40
|
+
apiDomainForEnvironment: () => apiDomainForEnvironment,
|
|
41
|
+
b64decode: () => b64decode,
|
|
42
|
+
b64encode: () => b64encode,
|
|
43
|
+
convertCurrencyAmount: () => convertCurrencyAmount,
|
|
44
|
+
decode: () => decode,
|
|
45
|
+
decrypt: () => decrypt,
|
|
46
|
+
decryptSecretWithNodePassword: () => decryptSecretWithNodePassword,
|
|
47
|
+
encrypt: () => encrypt,
|
|
48
|
+
encryptWithNodeKey: () => encryptWithNodeKey,
|
|
49
|
+
generateSigningKeyPair: () => generateSigningKeyPair,
|
|
50
|
+
getCrypto: () => getCrypto,
|
|
51
|
+
getNonce: () => getNonce,
|
|
52
|
+
isBrowser: () => isBrowser,
|
|
53
|
+
isNode: () => isNode,
|
|
54
|
+
isType: () => isType,
|
|
55
|
+
loadNodeEncryptionKey: () => loadNodeEncryptionKey,
|
|
56
|
+
serializeSigningKey: () => serializeSigningKey,
|
|
57
|
+
urlsafe_b64decode: () => urlsafe_b64decode
|
|
58
|
+
});
|
|
59
|
+
module.exports = __toCommonJS(src_exports);
|
|
60
|
+
|
|
61
|
+
// src/LightsparkException.ts
|
|
62
|
+
var LightsparkException = class extends Error {
|
|
63
|
+
code;
|
|
64
|
+
message;
|
|
65
|
+
extraInfo;
|
|
66
|
+
constructor(code, message, extraInfo) {
|
|
67
|
+
super(message);
|
|
68
|
+
this.code = code;
|
|
69
|
+
this.message = message;
|
|
70
|
+
this.extraInfo = extraInfo;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var LightsparkException_default = LightsparkException;
|
|
74
|
+
|
|
75
|
+
// src/auth/LightsparkAuthException.ts
|
|
76
|
+
var LightsparkAuthException = class extends LightsparkException_default {
|
|
77
|
+
constructor(message, extraInfo) {
|
|
78
|
+
super("AuthException", message, extraInfo);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var LightsparkAuthException_default = LightsparkAuthException;
|
|
82
|
+
|
|
83
|
+
// src/auth/StubAuthProvider.ts
|
|
84
|
+
var StubAuthProvider = class {
|
|
85
|
+
async addAuthHeaders(headers) {
|
|
86
|
+
return headers;
|
|
87
|
+
}
|
|
88
|
+
async isAuthorized() {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
addWsConnectionParams(params) {
|
|
92
|
+
return params;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/utils/base64.ts
|
|
97
|
+
var b64decode = (encoded) => {
|
|
98
|
+
return Uint8Array.from(atob(encoded), (c) => c.charCodeAt(0));
|
|
99
|
+
};
|
|
100
|
+
var urlsafe_b64decode = (encoded) => {
|
|
101
|
+
return b64decode(encoded.replace(/_/g, "/").replace(/-/g, "+"));
|
|
102
|
+
};
|
|
103
|
+
var b64encode = (data) => {
|
|
104
|
+
return btoa(
|
|
105
|
+
String.fromCharCode.apply(null, Array.from(new Uint8Array(data)))
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/crypto/LightsparkSigningException.ts
|
|
110
|
+
var LightsparkSigningException = class extends LightsparkException_default {
|
|
111
|
+
constructor(message, extraInfo) {
|
|
112
|
+
super("SigningException", message, extraInfo);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
var LightsparkSigningException_default = LightsparkSigningException;
|
|
116
|
+
|
|
117
|
+
// src/crypto/crypto.ts
|
|
118
|
+
var ITERATIONS = 5e5;
|
|
119
|
+
function getCrypto() {
|
|
120
|
+
let cryptoImplPromise;
|
|
121
|
+
if (typeof crypto !== "undefined") {
|
|
122
|
+
cryptoImplPromise = Promise.resolve(crypto);
|
|
123
|
+
} else {
|
|
124
|
+
cryptoImplPromise = import("crypto").then((nodeCrypto) => {
|
|
125
|
+
return nodeCrypto;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return cryptoImplPromise;
|
|
129
|
+
}
|
|
130
|
+
var getRandomValues = async (arr) => {
|
|
131
|
+
if (typeof crypto !== "undefined") {
|
|
132
|
+
return crypto.getRandomValues(arr);
|
|
133
|
+
} else {
|
|
134
|
+
const cryptoImpl2 = await getCrypto();
|
|
135
|
+
return cryptoImpl2.getRandomValues(arr);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
var getRandomValues32 = async (arr) => {
|
|
139
|
+
if (typeof crypto !== "undefined") {
|
|
140
|
+
return crypto.getRandomValues(arr);
|
|
141
|
+
} else {
|
|
142
|
+
const cryptoImpl2 = await getCrypto();
|
|
143
|
+
return cryptoImpl2.getRandomValues(arr);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
var deriveKey = async (password, salt, iterations, algorithm, bit_len) => {
|
|
147
|
+
const enc = new TextEncoder();
|
|
148
|
+
const cryptoImpl2 = await getCrypto();
|
|
149
|
+
const password_key = await cryptoImpl2.subtle.importKey(
|
|
150
|
+
"raw",
|
|
151
|
+
enc.encode(password),
|
|
152
|
+
"PBKDF2",
|
|
153
|
+
false,
|
|
154
|
+
["deriveBits", "deriveKey"]
|
|
155
|
+
);
|
|
156
|
+
const derived = await cryptoImpl2.subtle.deriveBits(
|
|
157
|
+
{
|
|
158
|
+
name: "PBKDF2",
|
|
159
|
+
salt,
|
|
160
|
+
iterations,
|
|
161
|
+
hash: "SHA-256"
|
|
162
|
+
},
|
|
163
|
+
password_key,
|
|
164
|
+
bit_len
|
|
165
|
+
);
|
|
166
|
+
const key = await cryptoImpl2.subtle.importKey(
|
|
167
|
+
"raw",
|
|
168
|
+
derived.slice(0, 32),
|
|
169
|
+
{ name: algorithm, length: 256 },
|
|
170
|
+
false,
|
|
171
|
+
["encrypt", "decrypt"]
|
|
172
|
+
);
|
|
173
|
+
const iv = derived.slice(32);
|
|
174
|
+
return [key, iv];
|
|
175
|
+
};
|
|
176
|
+
var encrypt = async (plaintext, password, salt) => {
|
|
177
|
+
if (!salt) {
|
|
178
|
+
salt = new Uint8Array(16);
|
|
179
|
+
getRandomValues(salt);
|
|
180
|
+
}
|
|
181
|
+
const [key, iv] = await deriveKey(password, salt, ITERATIONS, "AES-GCM", 352);
|
|
182
|
+
const cryptoImpl2 = await getCrypto();
|
|
183
|
+
const encrypted = new Uint8Array(
|
|
184
|
+
await cryptoImpl2.subtle.encrypt({ name: "AES-GCM", iv }, key, plaintext)
|
|
185
|
+
);
|
|
186
|
+
const output = new Uint8Array(salt.byteLength + encrypted.byteLength);
|
|
187
|
+
output.set(salt);
|
|
188
|
+
output.set(encrypted, salt.byteLength);
|
|
189
|
+
const header = {
|
|
190
|
+
v: 4,
|
|
191
|
+
i: ITERATIONS
|
|
192
|
+
};
|
|
193
|
+
return [JSON.stringify(header), b64encode(output)];
|
|
194
|
+
};
|
|
195
|
+
var decrypt = async (header_json, ciphertext, password) => {
|
|
196
|
+
var decoded = b64decode(ciphertext);
|
|
197
|
+
var header;
|
|
198
|
+
if (header_json === "AES_256_CBC_PBKDF2_5000_SHA256") {
|
|
199
|
+
header = {
|
|
200
|
+
v: 0,
|
|
201
|
+
i: 5e3
|
|
202
|
+
};
|
|
203
|
+
decoded = decoded.slice(8);
|
|
204
|
+
} else {
|
|
205
|
+
header = JSON.parse(header_json);
|
|
206
|
+
}
|
|
207
|
+
if (header.v < 0 || header.v > 4) {
|
|
208
|
+
throw new LightsparkException_default(
|
|
209
|
+
"DecryptionError",
|
|
210
|
+
"Unknown version ".concat(header.v)
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
const cryptoImpl2 = await getCrypto();
|
|
214
|
+
const algorithm = header.v < 2 ? "AES-CBC" : "AES-GCM";
|
|
215
|
+
const bit_len = header.v < 4 ? 384 : 352;
|
|
216
|
+
const salt_len = header.v < 4 ? 8 : 16;
|
|
217
|
+
if (header.lsv === 2 || header.v === 3) {
|
|
218
|
+
const salt = decoded.slice(decoded.length - 8, decoded.length);
|
|
219
|
+
const nonce = decoded.slice(0, 12);
|
|
220
|
+
const cipherText = decoded.slice(12, decoded.length - 8);
|
|
221
|
+
const [key, _iv] = await deriveKey(
|
|
222
|
+
password,
|
|
223
|
+
salt,
|
|
224
|
+
header.i,
|
|
225
|
+
algorithm,
|
|
226
|
+
256
|
|
227
|
+
);
|
|
228
|
+
return await cryptoImpl2.subtle.decrypt(
|
|
229
|
+
{ name: algorithm, iv: nonce.buffer },
|
|
230
|
+
key,
|
|
231
|
+
cipherText
|
|
232
|
+
);
|
|
233
|
+
} else {
|
|
234
|
+
const salt = decoded.slice(0, salt_len);
|
|
235
|
+
const encrypted = decoded.slice(salt_len);
|
|
236
|
+
const [key, iv] = await deriveKey(
|
|
237
|
+
password,
|
|
238
|
+
salt,
|
|
239
|
+
header.i,
|
|
240
|
+
algorithm,
|
|
241
|
+
bit_len
|
|
242
|
+
);
|
|
243
|
+
return await cryptoImpl2.subtle.decrypt(
|
|
244
|
+
{ name: algorithm, iv },
|
|
245
|
+
key,
|
|
246
|
+
encrypted
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
async function decryptSecretWithNodePassword(cipher, encryptedSecret, nodePassword) {
|
|
251
|
+
let decryptedValue = null;
|
|
252
|
+
try {
|
|
253
|
+
decryptedValue = await decrypt(cipher, encryptedSecret, nodePassword);
|
|
254
|
+
} catch (ex) {
|
|
255
|
+
console.error(ex);
|
|
256
|
+
}
|
|
257
|
+
return decryptedValue;
|
|
258
|
+
}
|
|
259
|
+
function decode(arrBuff) {
|
|
260
|
+
const dec = new TextDecoder();
|
|
261
|
+
return dec.decode(arrBuff);
|
|
262
|
+
}
|
|
263
|
+
var generateSigningKeyPair = async () => {
|
|
264
|
+
const cryptoImpl2 = await getCrypto();
|
|
265
|
+
return await cryptoImpl2.subtle.generateKey(
|
|
266
|
+
/*algorithm:*/
|
|
267
|
+
{
|
|
268
|
+
name: "RSA-PSS",
|
|
269
|
+
modulusLength: 4096,
|
|
270
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
271
|
+
hash: "SHA-256"
|
|
272
|
+
},
|
|
273
|
+
/*extractable*/
|
|
274
|
+
true,
|
|
275
|
+
/*keyUsages*/
|
|
276
|
+
["sign", "verify"]
|
|
277
|
+
);
|
|
278
|
+
};
|
|
279
|
+
var serializeSigningKey = async (key, format) => {
|
|
280
|
+
const cryptoImpl2 = await getCrypto();
|
|
281
|
+
return await cryptoImpl2.subtle.exportKey(
|
|
282
|
+
/*format*/
|
|
283
|
+
format,
|
|
284
|
+
/*key*/
|
|
285
|
+
key
|
|
286
|
+
);
|
|
287
|
+
};
|
|
288
|
+
var encryptWithNodeKey = async (key, data) => {
|
|
289
|
+
const enc = new TextEncoder();
|
|
290
|
+
const encoded = enc.encode(data);
|
|
291
|
+
const encrypted = await cryptoImpl.subtle.encrypt(
|
|
292
|
+
/*algorithm:*/
|
|
293
|
+
{
|
|
294
|
+
name: "RSA-OAEP"
|
|
295
|
+
},
|
|
296
|
+
/*key*/
|
|
297
|
+
key,
|
|
298
|
+
/*data*/
|
|
299
|
+
encoded
|
|
300
|
+
);
|
|
301
|
+
return b64encode(encrypted);
|
|
302
|
+
};
|
|
303
|
+
var loadNodeEncryptionKey = async (rawPublicKey) => {
|
|
304
|
+
const encoded = b64decode(rawPublicKey);
|
|
305
|
+
const cryptoImpl2 = await getCrypto();
|
|
306
|
+
return await cryptoImpl2.subtle.importKey(
|
|
307
|
+
/*format*/
|
|
308
|
+
"spki",
|
|
309
|
+
/*keyData*/
|
|
310
|
+
encoded,
|
|
311
|
+
/*algorithm:*/
|
|
312
|
+
{
|
|
313
|
+
name: "RSA-OAEP",
|
|
314
|
+
hash: "SHA-256"
|
|
315
|
+
},
|
|
316
|
+
/*extractable*/
|
|
317
|
+
true,
|
|
318
|
+
/*keyUsages*/
|
|
319
|
+
["encrypt"]
|
|
320
|
+
);
|
|
321
|
+
};
|
|
322
|
+
var getNonce = async () => {
|
|
323
|
+
const nonceSt = await getRandomValues32(new Uint32Array(1));
|
|
324
|
+
return Number(nonceSt);
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// src/crypto/NodeKeyCache.ts
|
|
328
|
+
var import_auto_bind = __toESM(require("auto-bind"), 1);
|
|
329
|
+
var NodeKeyCache = class {
|
|
330
|
+
idToKey;
|
|
331
|
+
constructor() {
|
|
332
|
+
this.idToKey = /* @__PURE__ */ new Map();
|
|
333
|
+
(0, import_auto_bind.default)(this);
|
|
334
|
+
}
|
|
335
|
+
async loadKey(id, rawKey) {
|
|
336
|
+
const decoded = b64decode(rawKey);
|
|
337
|
+
try {
|
|
338
|
+
const cryptoImpl2 = await getCrypto();
|
|
339
|
+
const key = await cryptoImpl2.subtle.importKey(
|
|
340
|
+
"pkcs8",
|
|
341
|
+
decoded,
|
|
342
|
+
{
|
|
343
|
+
name: "RSA-PSS",
|
|
344
|
+
hash: "SHA-256"
|
|
345
|
+
},
|
|
346
|
+
true,
|
|
347
|
+
["sign"]
|
|
348
|
+
);
|
|
349
|
+
this.idToKey.set(id, key);
|
|
350
|
+
return key;
|
|
351
|
+
} catch (e) {
|
|
352
|
+
console.log("Error importing key: ", e);
|
|
353
|
+
}
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
getKey(id) {
|
|
357
|
+
return this.idToKey.get(id);
|
|
358
|
+
}
|
|
359
|
+
hasKey(id) {
|
|
360
|
+
return this.idToKey.has(id);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
var NodeKeyCache_default = NodeKeyCache;
|
|
364
|
+
|
|
365
|
+
// src/requester/Requester.ts
|
|
366
|
+
var import_auto_bind2 = __toESM(require("auto-bind"), 1);
|
|
367
|
+
var import_dayjs = __toESM(require("dayjs"), 1);
|
|
368
|
+
var import_utc = __toESM(require("dayjs/plugin/utc.js"), 1);
|
|
369
|
+
var import_graphql_ws = require("graphql-ws");
|
|
370
|
+
var import_ws = __toESM(require("ws"), 1);
|
|
371
|
+
var import_zen_observable_ts = require("zen-observable-ts");
|
|
372
|
+
|
|
373
|
+
// src/utils/environment.ts
|
|
374
|
+
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
375
|
+
var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
376
|
+
|
|
377
|
+
// src/requester/Requester.ts
|
|
378
|
+
var DEFAULT_BASE_URL = "api.lightspark.com";
|
|
379
|
+
var LIGHTSPARK_BETA_HEADER_KEY = "X-Lightspark-Beta";
|
|
380
|
+
var LIGHTSPARK_BETA_HEADER_VALUE = "z2h0BBYxTA83cjW7fi8QwWtBPCzkQKiemcuhKY08LOo";
|
|
381
|
+
import_dayjs.default.extend(import_utc.default);
|
|
382
|
+
var Requester = class {
|
|
383
|
+
constructor(nodeKeyCache, schemaEndpoint, authProvider = new StubAuthProvider(), baseUrl = DEFAULT_BASE_URL) {
|
|
384
|
+
this.nodeKeyCache = nodeKeyCache;
|
|
385
|
+
this.schemaEndpoint = schemaEndpoint;
|
|
386
|
+
this.authProvider = authProvider;
|
|
387
|
+
this.baseUrl = baseUrl;
|
|
388
|
+
let websocketImpl;
|
|
389
|
+
if (typeof WebSocket === "undefined" && typeof window === "undefined") {
|
|
390
|
+
websocketImpl = import_ws.default;
|
|
391
|
+
}
|
|
392
|
+
this.wsClient = (0, import_graphql_ws.createClient)({
|
|
393
|
+
url: `wss://${this.baseUrl}/${this.schemaEndpoint}`,
|
|
394
|
+
connectionParams: authProvider.addWsConnectionParams({}),
|
|
395
|
+
webSocketImpl: websocketImpl
|
|
396
|
+
});
|
|
397
|
+
(0, import_auto_bind2.default)(this);
|
|
398
|
+
}
|
|
399
|
+
wsClient;
|
|
400
|
+
async executeQuery(query) {
|
|
401
|
+
const data = await this.makeRawRequest(
|
|
402
|
+
query.queryPayload,
|
|
403
|
+
query.variables || {},
|
|
404
|
+
query.signingNodeId
|
|
405
|
+
);
|
|
406
|
+
return query.constructObject(data);
|
|
407
|
+
}
|
|
408
|
+
subscribe(queryPayload, variables = {}) {
|
|
409
|
+
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
410
|
+
const operationMatch = queryPayload.match(operationNameRegex);
|
|
411
|
+
if (!operationMatch || operationMatch.length < 3) {
|
|
412
|
+
throw new LightsparkException_default("InvalidQuery", "Invalid query payload");
|
|
413
|
+
}
|
|
414
|
+
const operationType = operationMatch[1];
|
|
415
|
+
if (operationType == "mutation") {
|
|
416
|
+
throw new LightsparkException_default(
|
|
417
|
+
"InvalidQuery",
|
|
418
|
+
"Mutation queries should call makeRawRequest instead"
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
for (const key in variables) {
|
|
422
|
+
if (variables[key] === void 0) {
|
|
423
|
+
variables[key] = null;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
const operation = operationMatch[2];
|
|
427
|
+
let bodyData = {
|
|
428
|
+
query: queryPayload,
|
|
429
|
+
variables,
|
|
430
|
+
operationName: operation
|
|
431
|
+
};
|
|
432
|
+
return new import_zen_observable_ts.Observable(
|
|
433
|
+
(observer) => this.wsClient.subscribe(bodyData, {
|
|
434
|
+
next: (data) => observer.next(data),
|
|
435
|
+
error: (err) => observer.error(err),
|
|
436
|
+
complete: () => observer.complete()
|
|
437
|
+
})
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0) {
|
|
441
|
+
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
442
|
+
const operationMatch = queryPayload.match(operationNameRegex);
|
|
443
|
+
if (!operationMatch || operationMatch.length < 3) {
|
|
444
|
+
throw new LightsparkException_default("InvalidQuery", "Invalid query payload");
|
|
445
|
+
}
|
|
446
|
+
const operationType = operationMatch[1];
|
|
447
|
+
if (operationType == "subscription") {
|
|
448
|
+
throw new LightsparkException_default(
|
|
449
|
+
"InvalidQuery",
|
|
450
|
+
"Subscription queries should call subscribe instead"
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
for (const key in variables) {
|
|
454
|
+
if (variables[key] === void 0) {
|
|
455
|
+
variables[key] = null;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
const operation = operationMatch[2];
|
|
459
|
+
let bodyData = {
|
|
460
|
+
query: queryPayload,
|
|
461
|
+
variables,
|
|
462
|
+
operationName: operation
|
|
463
|
+
};
|
|
464
|
+
const browserUserAgent = typeof navigator !== "undefined" ? navigator.userAgent : "";
|
|
465
|
+
const sdkUserAgent = this.getSdkUserAgent();
|
|
466
|
+
const headers = await this.authProvider.addAuthHeaders({
|
|
467
|
+
"Content-Type": "application/json",
|
|
468
|
+
[LIGHTSPARK_BETA_HEADER_KEY]: LIGHTSPARK_BETA_HEADER_VALUE,
|
|
469
|
+
"X-Lightspark-SDK": sdkUserAgent,
|
|
470
|
+
"User-Agent": browserUserAgent || sdkUserAgent
|
|
471
|
+
});
|
|
472
|
+
bodyData = await this.addSigningDataIfNeeded(
|
|
473
|
+
bodyData,
|
|
474
|
+
headers,
|
|
475
|
+
signingNodeId
|
|
476
|
+
);
|
|
477
|
+
const response = await fetch(
|
|
478
|
+
`https://${this.baseUrl}/${this.schemaEndpoint}`,
|
|
479
|
+
{
|
|
480
|
+
method: "POST",
|
|
481
|
+
headers,
|
|
482
|
+
body: JSON.stringify(bodyData)
|
|
483
|
+
}
|
|
484
|
+
);
|
|
485
|
+
if (!response.ok) {
|
|
486
|
+
throw new LightsparkException_default(
|
|
487
|
+
"RequestFailed",
|
|
488
|
+
`Request ${operation} failed. ${response.statusText}`
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
const responseJson = await response.json();
|
|
492
|
+
const data = responseJson.data;
|
|
493
|
+
if (!data) {
|
|
494
|
+
throw new LightsparkException_default(
|
|
495
|
+
"RequestFailed",
|
|
496
|
+
`Request ${operation} failed. ${JSON.stringify(responseJson.errors)}`
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
return data;
|
|
500
|
+
}
|
|
501
|
+
getSdkUserAgent() {
|
|
502
|
+
const platform = isNode ? "NodeJS" : "Browser";
|
|
503
|
+
const platformVersion = isNode ? process.version : "";
|
|
504
|
+
const sdkVersion = "1.0.4";
|
|
505
|
+
return `lightspark-js-sdk/${sdkVersion} ${platform}/${platformVersion}`;
|
|
506
|
+
}
|
|
507
|
+
async addSigningDataIfNeeded(queryPayload, headers, signingNodeId) {
|
|
508
|
+
if (!signingNodeId) {
|
|
509
|
+
return queryPayload;
|
|
510
|
+
}
|
|
511
|
+
const query = queryPayload.query;
|
|
512
|
+
const variables = queryPayload.variables;
|
|
513
|
+
const operationName = queryPayload.operationName;
|
|
514
|
+
const nonce = await getNonce();
|
|
515
|
+
const expiration = import_dayjs.default.utc().add(1, "hour").format();
|
|
516
|
+
const payload = {
|
|
517
|
+
query,
|
|
518
|
+
variables,
|
|
519
|
+
operationName,
|
|
520
|
+
nonce,
|
|
521
|
+
expires_at: expiration
|
|
522
|
+
};
|
|
523
|
+
const key = await this.nodeKeyCache.getKey(signingNodeId);
|
|
524
|
+
if (!key) {
|
|
525
|
+
throw new LightsparkSigningException_default(
|
|
526
|
+
"Missing node of encrypted_signing_private_key"
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
const encodedPayload = new TextEncoder().encode(JSON.stringify(payload));
|
|
530
|
+
const cryptoImpl2 = await getCrypto();
|
|
531
|
+
const signedPayload = await cryptoImpl2.subtle.sign(
|
|
532
|
+
{
|
|
533
|
+
name: "RSA-PSS",
|
|
534
|
+
saltLength: 32
|
|
535
|
+
},
|
|
536
|
+
key,
|
|
537
|
+
encodedPayload
|
|
538
|
+
);
|
|
539
|
+
const encodedSignedPayload = b64encode(signedPayload);
|
|
540
|
+
headers["X-Lightspark-Signing"] = JSON.stringify({
|
|
541
|
+
v: "1",
|
|
542
|
+
signature: encodedSignedPayload
|
|
543
|
+
});
|
|
544
|
+
return payload;
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
var Requester_default = Requester;
|
|
548
|
+
|
|
549
|
+
// src/ServerEnvironment.ts
|
|
550
|
+
var ServerEnvironment = /* @__PURE__ */ ((ServerEnvironment2) => {
|
|
551
|
+
ServerEnvironment2["PRODUCTION"] = "production";
|
|
552
|
+
ServerEnvironment2["DEV"] = "dev";
|
|
553
|
+
return ServerEnvironment2;
|
|
554
|
+
})(ServerEnvironment || {});
|
|
555
|
+
var apiDomainForEnvironment = (environment) => {
|
|
556
|
+
switch (environment) {
|
|
557
|
+
case "dev" /* DEV */:
|
|
558
|
+
return "api.dev.dev.sparkinfra.net";
|
|
559
|
+
case "production" /* PRODUCTION */:
|
|
560
|
+
return "api.lightspark.com";
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
var ServerEnvironment_default = ServerEnvironment;
|
|
564
|
+
|
|
565
|
+
// src/utils/currency.ts
|
|
566
|
+
var CONVERSION_MAP = {
|
|
567
|
+
["BITCOIN" /* BITCOIN */]: {
|
|
568
|
+
["BITCOIN" /* BITCOIN */]: (v) => v,
|
|
569
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e6,
|
|
570
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v * 1e3,
|
|
571
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e11,
|
|
572
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e9,
|
|
573
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 1e8
|
|
574
|
+
},
|
|
575
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: {
|
|
576
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e6),
|
|
577
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v,
|
|
578
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
579
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e5,
|
|
580
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e3,
|
|
581
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 100
|
|
582
|
+
},
|
|
583
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: {
|
|
584
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e3),
|
|
585
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => v * 1e3,
|
|
586
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => v,
|
|
587
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e8,
|
|
588
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 1e6,
|
|
589
|
+
["SATOSHI" /* SATOSHI */]: (v) => v * 1e5
|
|
590
|
+
},
|
|
591
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: {
|
|
592
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e11),
|
|
593
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
594
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e8),
|
|
595
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v,
|
|
596
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => Math.round(v / 100),
|
|
597
|
+
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 1e3)
|
|
598
|
+
},
|
|
599
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: {
|
|
600
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e9),
|
|
601
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 1e3),
|
|
602
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e6),
|
|
603
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 100,
|
|
604
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v,
|
|
605
|
+
["SATOSHI" /* SATOSHI */]: (v) => Math.round(v / 10)
|
|
606
|
+
},
|
|
607
|
+
["SATOSHI" /* SATOSHI */]: {
|
|
608
|
+
["BITCOIN" /* BITCOIN */]: (v) => Math.round(v / 1e8),
|
|
609
|
+
["MICROBITCOIN" /* MICROBITCOIN */]: (v) => Math.round(v / 100),
|
|
610
|
+
["MILLIBITCOIN" /* MILLIBITCOIN */]: (v) => Math.round(v / 1e5),
|
|
611
|
+
["MILLISATOSHI" /* MILLISATOSHI */]: (v) => v * 1e3,
|
|
612
|
+
["NANOBITCOIN" /* NANOBITCOIN */]: (v) => v * 10,
|
|
613
|
+
["SATOSHI" /* SATOSHI */]: (v) => v
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
var convertCurrencyAmount = (from, toUnit) => {
|
|
617
|
+
const conversionFn = CONVERSION_MAP[from.originalUnit][toUnit];
|
|
618
|
+
if (!conversionFn) {
|
|
619
|
+
throw new LightsparkException_default(
|
|
620
|
+
"CurrencyError",
|
|
621
|
+
`Cannot convert from ${from.originalUnit} to ${toUnit}`
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
return {
|
|
625
|
+
...from,
|
|
626
|
+
preferredCurrencyUnit: toUnit,
|
|
627
|
+
preferredCurrencyValueApprox: conversionFn(from.originalValue),
|
|
628
|
+
preferredCurrencyValueRounded: conversionFn(from.originalValue)
|
|
629
|
+
};
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
// src/utils/types.ts
|
|
633
|
+
var isType = (typename) => (node) => {
|
|
634
|
+
return node?.__typename === typename;
|
|
635
|
+
};
|
|
636
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
637
|
+
0 && (module.exports = {
|
|
638
|
+
LightsparkAuthException,
|
|
639
|
+
LightsparkException,
|
|
640
|
+
LightsparkSigningException,
|
|
641
|
+
NodeKeyCache,
|
|
642
|
+
Requester,
|
|
643
|
+
ServerEnvironment,
|
|
644
|
+
StubAuthProvider,
|
|
645
|
+
apiDomainForEnvironment,
|
|
646
|
+
b64decode,
|
|
647
|
+
b64encode,
|
|
648
|
+
convertCurrencyAmount,
|
|
649
|
+
decode,
|
|
650
|
+
decrypt,
|
|
651
|
+
decryptSecretWithNodePassword,
|
|
652
|
+
encrypt,
|
|
653
|
+
encryptWithNodeKey,
|
|
654
|
+
generateSigningKeyPair,
|
|
655
|
+
getCrypto,
|
|
656
|
+
getNonce,
|
|
657
|
+
isBrowser,
|
|
658
|
+
isNode,
|
|
659
|
+
isType,
|
|
660
|
+
loadNodeEncryptionKey,
|
|
661
|
+
serializeSigningKey,
|
|
662
|
+
urlsafe_b64decode
|
|
663
|
+
});
|