@imtbl/sdk 1.43.8 → 1.44.0

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.
Files changed (53) hide show
  1. package/dist/blockchain_data.d.ts +7950 -3
  2. package/dist/blockchain_data.js +6058 -1
  3. package/dist/browser/checkout/sdk.js +4 -4
  4. package/dist/checkout.d.ts +16887 -7
  5. package/dist/checkout.js +37177 -1
  6. package/dist/config.d.ts +30 -1
  7. package/dist/config.js +394 -1
  8. package/dist/index.browser.js +4 -4
  9. package/dist/index.cjs +7 -7
  10. package/dist/index.d.ts +32632 -18
  11. package/dist/index.js +64112 -1
  12. package/dist/minting_backend.d.ts +3535 -5
  13. package/dist/minting_backend.js +6756 -1
  14. package/dist/orderbook.d.ts +1713 -5
  15. package/dist/orderbook.js +2462 -1
  16. package/dist/passport.d.ts +13703 -6
  17. package/dist/passport.js +23137 -1
  18. package/dist/webhook.d.ts +1265 -4
  19. package/dist/webhook.js +488 -1
  20. package/dist/x.d.ts +18663 -6
  21. package/dist/x.js +19242 -1
  22. package/package.json +1 -1
  23. package/dist/Passport.d-011d5035.d.ts +0 -224
  24. package/dist/blockchain-data.d-1634b683.d.ts +0 -3406
  25. package/dist/blockchain_data-711b321c.js +0 -1
  26. package/dist/blockchain_data.d-d538f8d4.d.ts +0 -4543
  27. package/dist/checkout-de10dc4a.js +0 -16
  28. package/dist/checkout.d-7613e722.d.ts +0 -3397
  29. package/dist/config-9a0c2ad4.js +0 -1
  30. package/dist/config.d-65420620.d.ts +0 -18
  31. package/dist/event-types.d-42520276.d.ts +0 -332
  32. package/dist/imxProvider.d-cac9e315.d.ts +0 -12538
  33. package/dist/index-02b46fbb.js +0 -1
  34. package/dist/index-04c3c2f1.js +0 -1
  35. package/dist/index-261492de.js +0 -1
  36. package/dist/index-97ccc0d4.js +0 -1
  37. package/dist/index-cce11a83.js +0 -1
  38. package/dist/index-e9713a8c.js +0 -1
  39. package/dist/index.d-c4a4c17d.d.ts +0 -277
  40. package/dist/index.d-f0845744.d.ts +0 -30
  41. package/dist/index.d-f1471830.d.ts +0 -376
  42. package/dist/json-rpc-provider.d-5c038bd9.d.ts +0 -249
  43. package/dist/minting_backend-ae1f9ecf.js +0 -1
  44. package/dist/minting_backend.d-4754ffee.d.ts +0 -104
  45. package/dist/orderbook-f5332f8b.js +0 -1
  46. package/dist/orderbook.d-77162c6c.d.ts +0 -1257
  47. package/dist/passport-cc0116e8.js +0 -1
  48. package/dist/passport.d-d3f44798.d.ts +0 -67
  49. package/dist/transfer.d-87728423.d.ts +0 -898
  50. package/dist/webhook-84b4501d.js +0 -1
  51. package/dist/webhook.d-4c3cb340.d.ts +0 -75
  52. package/dist/x-ed03fd76.js +0 -1
  53. package/dist/x.d-1b51f0c3.d.ts +0 -4879
package/dist/webhook.js CHANGED
@@ -1 +1,488 @@
1
- export{h as handle}from"./index-cce11a83.js";import"sns-validator";import"./index-97ccc0d4.js";import"axios";import"lru-memorise";import"global-const";
1
+ import MessageValidator from 'sns-validator';
2
+ import axios from 'axios';
3
+ import { memorise } from 'lru-memorise';
4
+ import { getGlobalisedValue as getGlobalisedValue$1 } from 'global-const';
5
+
6
+ const isNode = () => typeof window === 'undefined';
7
+ const isBrowser = () => !isNode();
8
+
9
+ var Detail;
10
+ (function (Detail) {
11
+ Detail["RUNTIME_ID"] = "rid";
12
+ Detail["PASSPORT_CLIENT_ID"] = "passportClientId";
13
+ Detail["ENVIRONMENT"] = "env";
14
+ Detail["PUBLISHABLE_API_KEY"] = "pak";
15
+ Detail["IDENTITY"] = "uid";
16
+ Detail["DOMAIN"] = "domain";
17
+ Detail["SDK_VERSION"] = "sdkVersion";
18
+ })(Detail || (Detail = {}));
19
+
20
+ const IMTBL_API = 'https://api.immutable.com';
21
+ async function post(path, data) {
22
+ const client = axios.create({
23
+ baseURL: IMTBL_API,
24
+ });
25
+ const payload = JSON.stringify(data);
26
+ const body = {
27
+ payload: Buffer.from(payload).toString('base64'),
28
+ };
29
+ const response = await client.post(path, body);
30
+ return response.data;
31
+ }
32
+
33
+ /**
34
+ * Abstraction on localstorage
35
+ */
36
+ const localStoragePrefix = '__IMX-';
37
+ const hasLocalstorage = () => isBrowser() && window.localStorage;
38
+ const parseItem = (payload) => {
39
+ // Try to parse, if can't be parsed assume string
40
+ // and return string
41
+ if (payload === null)
42
+ return undefined;
43
+ try {
44
+ return JSON.parse(payload);
45
+ }
46
+ catch (error) {
47
+ // Assumes it's a string.
48
+ return payload;
49
+ }
50
+ };
51
+ const serialiseItem = (payload) => {
52
+ if (typeof payload === 'string') {
53
+ return payload;
54
+ }
55
+ return JSON.stringify(payload);
56
+ };
57
+ /**
58
+ * GenKey will take into account the namespace
59
+ * as well as if being run in the Link, it will tap into the link
60
+ * @param {string} key
61
+ * @returns key
62
+ */
63
+ const genKey = (key) => `${localStoragePrefix}${key}`;
64
+ function getItem(key) {
65
+ if (hasLocalstorage()) {
66
+ return parseItem(window.localStorage.getItem(genKey(key)));
67
+ }
68
+ return undefined;
69
+ }
70
+ const setItem = (key, payload) => {
71
+ if (hasLocalstorage()) {
72
+ window.localStorage.setItem(genKey(key), serialiseItem(payload));
73
+ return true;
74
+ }
75
+ return false;
76
+ };
77
+
78
+ var Store;
79
+ (function (Store) {
80
+ Store["EVENTS"] = "events";
81
+ Store["RUNTIME"] = "runtime";
82
+ })(Store || (Store = {}));
83
+ // In memory storage for events and other data
84
+ let EVENT_STORE;
85
+ let RUNTIME_DETAILS;
86
+ // Initialise store and runtime
87
+ const initialise$1 = () => {
88
+ EVENT_STORE = getItem(Store.EVENTS) || [];
89
+ RUNTIME_DETAILS = getItem(Store.RUNTIME) || {};
90
+ };
91
+ initialise$1();
92
+ // Runtime Details
93
+ const storeDetail = (key, value) => {
94
+ RUNTIME_DETAILS = {
95
+ ...RUNTIME_DETAILS,
96
+ [key]: value,
97
+ };
98
+ setItem(Store.RUNTIME, RUNTIME_DETAILS);
99
+ };
100
+ const getDetail$1 = (key) => {
101
+ // Handle the scenario where detail is a falsy value
102
+ if (RUNTIME_DETAILS[key] === undefined) {
103
+ return undefined;
104
+ }
105
+ return RUNTIME_DETAILS[key];
106
+ };
107
+ const getAllDetails = () => RUNTIME_DETAILS;
108
+ // Events
109
+ const getEvents = () => EVENT_STORE;
110
+ const addEvent = (event) => {
111
+ EVENT_STORE.push(event);
112
+ setItem(Store.EVENTS, EVENT_STORE);
113
+ };
114
+ const removeSentEvents = (numberOfEvents) => {
115
+ EVENT_STORE = EVENT_STORE.slice(numberOfEvents);
116
+ setItem(Store.EVENTS, EVENT_STORE);
117
+ };
118
+ const flattenProperties = (properties) => {
119
+ const propertyMap = [];
120
+ Object.entries(properties).forEach(([key, value]) => {
121
+ if (typeof key === 'string'
122
+ || typeof value === 'string'
123
+ || typeof value === 'number'
124
+ || typeof value === 'boolean') {
125
+ propertyMap.push([key, value.toString()]);
126
+ }
127
+ });
128
+ return propertyMap;
129
+ };
130
+
131
+ // WARNING: DO NOT CHANGE THE STRING BELOW. IT GETS REPLACED AT BUILD TIME.
132
+ const SDK_VERSION = '1.44.0';
133
+ const getFrameParentDomain = () => {
134
+ if (isNode()) {
135
+ return '';
136
+ }
137
+ // If available for supported browsers (all except Firefox)
138
+ if (window.location.ancestorOrigins
139
+ && window.location.ancestorOrigins.length > 0) {
140
+ return new URL(window.location.ancestorOrigins[0]).hostname;
141
+ }
142
+ // Fallback to using the referrer
143
+ return document.referrer ? new URL(window.document.referrer).hostname : '';
144
+ };
145
+ const runtimeHost = () => {
146
+ if (isNode()) {
147
+ return '';
148
+ }
149
+ let domain;
150
+ try {
151
+ if (window.self !== window.top) {
152
+ domain = getFrameParentDomain();
153
+ }
154
+ }
155
+ catch (error) {
156
+ // Do nothing
157
+ }
158
+ // Fallback to current domain if can't detect parent domain
159
+ if (!domain) {
160
+ domain = window.location.hostname;
161
+ }
162
+ return domain;
163
+ };
164
+ const getRuntimeDetails = () => {
165
+ storeDetail(Detail.SDK_VERSION, SDK_VERSION);
166
+ if (isNode()) {
167
+ return { browser: 'nodejs', sdkVersion: SDK_VERSION };
168
+ }
169
+ const domain = runtimeHost();
170
+ if (domain) {
171
+ storeDetail(Detail.DOMAIN, domain);
172
+ }
173
+ return {
174
+ sdkVersion: SDK_VERSION,
175
+ browser: window.navigator.userAgent,
176
+ domain,
177
+ tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
178
+ screen: `${window.screen.width}x${window.screen.height}`,
179
+ };
180
+ };
181
+ let initialised = false;
182
+ const isInitialised = () => initialised;
183
+ const initialise = async () => {
184
+ initialised = true;
185
+ try {
186
+ const runtimeDetails = flattenProperties(getRuntimeDetails());
187
+ const existingRuntimeId = getDetail$1(Detail.RUNTIME_ID);
188
+ const body = {
189
+ version: 1,
190
+ data: {
191
+ runtimeDetails,
192
+ runtimeId: existingRuntimeId,
193
+ },
194
+ };
195
+ const response = await post('/v1/sdk/initialise', body);
196
+ // Get runtimeId and store it
197
+ const { runtimeId } = response;
198
+ storeDetail(Detail.RUNTIME_ID, runtimeId);
199
+ }
200
+ catch (error) {
201
+ initialised = false;
202
+ }
203
+ };
204
+
205
+ function errorBoundary(fn, fallbackResult) {
206
+ return (...args) => {
207
+ try {
208
+ // Execute the original function
209
+ const result = fn(...args);
210
+ if (result instanceof Promise) {
211
+ // Silent fail for now, in future
212
+ // we can send errors to a logging service
213
+ return result.catch(() => fallbackResult);
214
+ }
215
+ return result;
216
+ }
217
+ catch (error) {
218
+ // As above, fail silently for now
219
+ return fallbackResult;
220
+ }
221
+ };
222
+ }
223
+
224
+ function isTestEnvironmentFn() {
225
+ if (isBrowser()) {
226
+ return false;
227
+ }
228
+ if (typeof process === 'undefined') {
229
+ return false;
230
+ }
231
+ // Consider using `ci-info` package for better results, though might fail as not browser safe.
232
+ // Just use process.env.CI for now.
233
+ return process.env.JEST_WORKER_ID !== undefined;
234
+ }
235
+ const isTestEnvironment = errorBoundary(isTestEnvironmentFn, false);
236
+
237
+ const GLOBALISE_KEY = 'imtbl__metrics';
238
+ const MEMORISE_TIMEFRAME = 5000;
239
+ const MEMORISE_MAX = 1000;
240
+ const getGlobalisedValue = (key, value) => getGlobalisedValue$1(GLOBALISE_KEY, key, value);
241
+ const getGlobalisedCachedFunction = (key, fn) => {
242
+ // Some applications (esp backend, or frontends using the split bundles) can sometimes
243
+ // initialise the same request multiple times. This will prevent multiple of the
244
+ // same event,value from being reported in a 1 second period.
245
+ const memorisedFn = memorise(fn, {
246
+ lruOptions: { ttl: MEMORISE_TIMEFRAME, max: MEMORISE_MAX },
247
+ });
248
+ return getGlobalisedValue$1(GLOBALISE_KEY, key, memorisedFn);
249
+ };
250
+
251
+ const POLLING_FREQUENCY = 5000;
252
+ const trackFn = (moduleName, eventName, properties) => {
253
+ const event = {
254
+ event: `${moduleName}.${eventName}`,
255
+ time: new Date().toISOString(),
256
+ ...(properties && { properties: flattenProperties(properties) }),
257
+ };
258
+ addEvent(event);
259
+ };
260
+ /**
261
+ * Track an event completion.
262
+ * @param moduleName Name of the module being tracked (for namespacing purposes), e.g. `passport`
263
+ * @param eventName Name of the event, use camelCase e.g. `clickItem`
264
+ * @param properties Other properties to be sent with the event
265
+ *
266
+ * e.g.
267
+ *
268
+ * ```ts
269
+ * track("passport", "performTransaction");
270
+ * track("passport", "performTransaction", { transationType: "transfer" });
271
+ * ```
272
+ */
273
+ errorBoundary(getGlobalisedCachedFunction('track', trackFn));
274
+ // Sending events to the server
275
+ const flushFn = async () => {
276
+ // Don't flush if not initialised
277
+ if (isInitialised() === false) {
278
+ await initialise();
279
+ return;
280
+ }
281
+ const events = getEvents();
282
+ if (events.length === 0) {
283
+ return;
284
+ }
285
+ // Track events length here, incase
286
+ const numEvents = events.length;
287
+ // Get details and send it with the track request
288
+ const details = getAllDetails();
289
+ const metricsPayload = {
290
+ version: 1,
291
+ data: {
292
+ events,
293
+ details,
294
+ },
295
+ };
296
+ const response = await post('/v1/sdk/metrics', metricsPayload);
297
+ if (response instanceof Error) {
298
+ return;
299
+ }
300
+ // Clear events if successfully posted
301
+ removeSentEvents(numEvents);
302
+ };
303
+ const flush = errorBoundary(flushFn);
304
+ // Flush events every 5 seconds
305
+ const flushPoll = async () => {
306
+ await flush();
307
+ setTimeout(flushPoll, POLLING_FREQUENCY);
308
+ };
309
+ let flushingStarted = false;
310
+ const startFlushing = () => {
311
+ if (flushingStarted) {
312
+ return;
313
+ }
314
+ flushingStarted = true;
315
+ flushPoll();
316
+ };
317
+ // This will get initialised when module is imported.
318
+ if (!isTestEnvironment()) {
319
+ errorBoundary(getGlobalisedValue('startFlushing', startFlushing))();
320
+ }
321
+
322
+ const setEnvironmentFn = (env) => {
323
+ storeDetail(Detail.ENVIRONMENT, env);
324
+ };
325
+ errorBoundary(getGlobalisedValue('setEnvironment', setEnvironmentFn));
326
+ const setPassportClientIdFn = (passportClientId) => {
327
+ storeDetail(Detail.PASSPORT_CLIENT_ID, passportClientId);
328
+ };
329
+ errorBoundary(getGlobalisedValue('setPassportClientId', setPassportClientIdFn));
330
+ const setPublishableApiKeyFn = (publishableApiKey) => {
331
+ storeDetail(Detail.PUBLISHABLE_API_KEY, publishableApiKey);
332
+ };
333
+ errorBoundary(getGlobalisedValue('setPublishableApiKey', setPublishableApiKeyFn));
334
+ errorBoundary(getGlobalisedValue('getDetail', getDetail$1));
335
+
336
+ var Environment;
337
+ (function (Environment) {
338
+ Environment["PRODUCTION"] = "production";
339
+ Environment["SANDBOX"] = "sandbox";
340
+ })(Environment || (Environment = {}));
341
+ var KeyHeaders;
342
+ (function (KeyHeaders) {
343
+ KeyHeaders["API_KEY"] = "x-immutable-api-key";
344
+ KeyHeaders["PUBLISHABLE_KEY"] = "x-immutable-publishable-key";
345
+ KeyHeaders["RATE_LIMITING_KEY"] = "x-api-key";
346
+ })(KeyHeaders || (KeyHeaders = {}));
347
+
348
+ const validator = new MessageValidator();
349
+ const allowedTopicArnPrefix = {
350
+ [Environment.PRODUCTION]: 'arn:aws:sns:us-east-2:362750628221:',
351
+ [Environment.SANDBOX]: 'arn:aws:sns:us-east-2:783421985614:'
352
+ };
353
+ /**
354
+ * handle will validate webhook message origin and verify signature of the message and calls corresponding handlers passed in.
355
+ * @param body The request body to a webhook endpoint in json string or js object form.
356
+ * @param env The Immutable environment the webhook is set up for.
357
+ * @param handlers The optional handlers object for different events. The `all` handler will be triggered for all event types.
358
+ * @returns The event object from the webhook message after validation and verification.
359
+ */
360
+ const handle = async (body, env, handlers) => {
361
+ const msg = await new Promise((resolve, reject) => {
362
+ validator.validate(body, (err, message) => {
363
+ if (err) {
364
+ return reject(err);
365
+ }
366
+ // check for topic arn prefix
367
+ if (!message.TopicArn.startsWith(allowedTopicArnPrefix[env])) {
368
+ throw new Error('Invalid topic arn');
369
+ }
370
+ if (message?.Type === 'SubscriptionConfirmation') {
371
+ fetch(message.SubscribeURL).then(() => {
372
+ resolve(message);
373
+ }).catch((e) => {
374
+ reject(e);
375
+ });
376
+ }
377
+ return resolve(message);
378
+ });
379
+ });
380
+ if (msg.Type === 'Notification') {
381
+ // the msg.Message is only a valid JSON string when the message is of Type Notification
382
+ const event = JSON.parse(msg.Message);
383
+ switch (event.event_name) {
384
+ case 'imtbl_zkevm_mint_request_updated':
385
+ if (handlers?.zkevmMintRequestUpdated) {
386
+ await handlers?.zkevmMintRequestUpdated(event);
387
+ }
388
+ break;
389
+ case 'imtbl_zkevm_activity_mint':
390
+ if (handlers?.zkEvmActivityMint) {
391
+ await handlers?.zkEvmActivityMint(event);
392
+ }
393
+ break;
394
+ case 'imtbl_zkevm_activity_burn':
395
+ if (handlers?.zkEvmActivityBurn) {
396
+ await handlers?.zkEvmActivityBurn(event);
397
+ }
398
+ break;
399
+ case 'imtbl_zkevm_activity_transfer':
400
+ if (handlers?.zkEvmActivityTransfer) {
401
+ await handlers?.zkEvmActivityTransfer(event);
402
+ }
403
+ break;
404
+ case 'imtbl_zkevm_activity_sale':
405
+ if (handlers?.zkEvmActivitySale) {
406
+ await handlers?.zkEvmActivitySale(event);
407
+ }
408
+ break;
409
+ case 'imtbl_zkevm_activity_deposit':
410
+ if (handlers?.zkEvmActivityDeposit) {
411
+ await handlers?.zkEvmActivityDeposit(event);
412
+ }
413
+ break;
414
+ case 'imtbl_zkevm_activity_withdrawal':
415
+ if (handlers?.zkEvmActivityWithdrawal) {
416
+ await handlers?.zkEvmActivityWithdrawal(event);
417
+ }
418
+ break;
419
+ case 'imtbl_zkevm_collection_updated':
420
+ if (handlers?.zkEvmCollectionUpdated) {
421
+ await handlers?.zkEvmCollectionUpdated(event);
422
+ }
423
+ break;
424
+ case 'imtbl_zkevm_nft_updated':
425
+ if (handlers?.zkEvmNftUpdated) {
426
+ await handlers?.zkEvmNftUpdated(event);
427
+ }
428
+ break;
429
+ case 'imtbl_zkevm_metadata_updated':
430
+ if (handlers?.zkEvmMetadataUpdated) {
431
+ await handlers?.zkEvmMetadataUpdated(event);
432
+ }
433
+ break;
434
+ case 'imtbl_zkevm_token_updated':
435
+ if (handlers?.zkEvmTokenUpdated) {
436
+ await handlers?.zkEvmTokenUpdated(event);
437
+ }
438
+ break;
439
+ case 'imtbl_zkevm_order_updated':
440
+ if (handlers?.zkEvmOrderUpdated) {
441
+ await handlers?.zkEvmOrderUpdated(event);
442
+ }
443
+ break;
444
+ case 'imtbl_zkevm_trade_created':
445
+ if (handlers?.zkEvmTradeCreated) {
446
+ await handlers?.zkEvmTradeCreated(event);
447
+ }
448
+ break;
449
+ case 'imtbl_x_nft_created':
450
+ if (handlers?.xNftCreated) {
451
+ await handlers?.xNftCreated(event);
452
+ }
453
+ break;
454
+ case 'imtbl_x_nft_updated':
455
+ if (handlers?.xNftUpdated) {
456
+ await handlers?.xNftUpdated(event);
457
+ }
458
+ break;
459
+ case 'imtbl_x_order_accepted':
460
+ if (handlers?.xOrderAccepted) {
461
+ await handlers?.xOrderAccepted(event);
462
+ }
463
+ break;
464
+ case 'imtbl_x_order_filled':
465
+ if (handlers?.xOrderFilled) {
466
+ await handlers?.xOrderFilled(event);
467
+ }
468
+ break;
469
+ case 'imtbl_x_order_cancelled':
470
+ if (handlers?.xOrderCancelled) {
471
+ await handlers?.xOrderCancelled(event);
472
+ }
473
+ break;
474
+ case 'imtbl_x_transfer_created':
475
+ if (handlers?.xTransferCreated) {
476
+ await handlers?.xTransferCreated(event);
477
+ }
478
+ break;
479
+ }
480
+ if (handlers?.all) {
481
+ await handlers?.all(event);
482
+ }
483
+ return event;
484
+ }
485
+ return null;
486
+ };
487
+
488
+ export { handle };