@eluvio/elv-client-js 3.2.4 → 3.2.7

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 (46) hide show
  1. package/dist/ElvClient-min.js +15 -11
  2. package/dist/ElvClient-node-min.js +17 -13
  3. package/dist/ElvFrameClient-min.js +12 -8
  4. package/dist/ElvPermissionsClient-min.js +13 -9
  5. package/dist/ElvWalletClient-min.js +58 -0
  6. package/dist/ElvWalletClient-node-min.js +78 -0
  7. package/dist/src/AuthorizationClient.js +2248 -1990
  8. package/dist/src/ContentObjectVerification.js +164 -173
  9. package/dist/src/Crypto.js +376 -324
  10. package/dist/src/ElvClient.js +1198 -1019
  11. package/dist/src/ElvWallet.js +119 -95
  12. package/dist/src/EthClient.js +1040 -896
  13. package/dist/src/FrameClient.js +331 -300
  14. package/dist/src/HttpClient.js +153 -147
  15. package/dist/src/Id.js +1 -3
  16. package/dist/src/PermissionsClient.js +1294 -1168
  17. package/dist/src/RemoteSigner.js +263 -211
  18. package/dist/src/UserProfileClient.js +1164 -1023
  19. package/dist/src/Utils.js +229 -184
  20. package/dist/src/client/ABRPublishing.js +895 -858
  21. package/dist/src/client/AccessGroups.js +1102 -959
  22. package/dist/src/client/ContentAccess.js +3724 -3431
  23. package/dist/src/client/ContentManagement.js +2252 -2068
  24. package/dist/src/client/Contracts.js +647 -563
  25. package/dist/src/client/Files.js +1886 -1757
  26. package/dist/src/client/NFT.js +126 -112
  27. package/dist/src/client/NTP.js +478 -422
  28. package/dist/src/index.js +11 -0
  29. package/dist/src/marketplaceClient/ClientMethods.js +1918 -0
  30. package/dist/src/marketplaceClient/Configuration.js +29 -0
  31. package/dist/src/marketplaceClient/Utils.js +304 -0
  32. package/dist/src/marketplaceClient/index.js +1553 -0
  33. package/dist/src/walletClient/ClientMethods.js +2080 -0
  34. package/dist/src/walletClient/Configuration.js +29 -0
  35. package/dist/src/walletClient/Utils.js +304 -0
  36. package/dist/src/walletClient/index.js +1770 -0
  37. package/package.json +8 -4
  38. package/src/ElvClient.js +5 -2
  39. package/src/Utils.js +22 -4
  40. package/src/index.js +7 -0
  41. package/src/walletClient/ClientMethods.js +1111 -0
  42. package/src/walletClient/Configuration.js +40 -0
  43. package/src/walletClient/README.md +191 -0
  44. package/src/walletClient/Utils.js +234 -0
  45. package/src/walletClient/index.js +977 -0
  46. package/testScripts/TestMarketplaceClient.js +25 -0
@@ -0,0 +1,977 @@
1
+ const {ElvClient} = require("../ElvClient");
2
+ const Configuration = require("./Configuration");
3
+ const {LinkTargetHash, FormatNFT, ActionPopup} = require("./Utils");
4
+ const UrlJoin = require("url-join");
5
+ const Utils = require("../Utils");
6
+ const Ethers = require("ethers");
7
+
8
+
9
+ /**
10
+ * Use the <a href="#.Initialize">Initialize</a> method to initialize a new client.
11
+ *
12
+ *
13
+ * See the Modules section on the sidebar for all client methods unrelated to login and authorization
14
+ */
15
+ class ElvWalletClient {
16
+ constructor({client, network, mode, marketplaceInfo, storeAuthToken}) {
17
+ this.client = client;
18
+ this.loggedIn = false;
19
+
20
+ this.network = network;
21
+ this.mode = mode;
22
+ this.purchaseMode = Configuration[network][mode].purchaseMode;
23
+ this.mainSiteId = Configuration[network][mode].siteId;
24
+ this.appUrl = Configuration[network][mode].appUrl;
25
+ this.publicStaticToken = client.staticToken;
26
+ this.storeAuthToken = storeAuthToken;
27
+
28
+ this.selectedMarketplaceInfo = marketplaceInfo;
29
+
30
+ this.availableMarketplaces = {};
31
+ this.availableMarketplacesById = {};
32
+ this.marketplaceHashes = {};
33
+
34
+ // Caches
35
+ this.cachedMarketplaces = {};
36
+ this.cachedCSS = {};
37
+
38
+ this.utils = client.utils;
39
+ }
40
+
41
+ Log(message, error=false) {
42
+ if(error) {
43
+ // eslint-disable-next-line no-console
44
+ console.error("Eluvio Wallet Client:", message);
45
+ } else {
46
+ // eslint-disable-next-line no-console
47
+ console.log("Eluvio Wallet Client:", message);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Initialize the wallet client.
53
+ *
54
+ * Specify tenantSlug and marketplaceSlug to automatically associate this tenant with a particular marketplace.
55
+ *
56
+ * @methodGroup Initialization
57
+ * @namedParams
58
+ * @param {string} network=main - Name of the Fabric network to use (`main`, `demo`)
59
+ * @param {string} mode=production - Environment to use (`production`, `staging`)
60
+ * @param {Object=} marketplaceParams - Marketplace parameters
61
+ * @param {boolean=} storeAuthToken=true - If specified, auth tokens will be stored in localstorage (if available)
62
+ *
63
+ * @returns {Promise<ElvWalletClient>}
64
+ */
65
+ static async Initialize({
66
+ network="main",
67
+ mode="production",
68
+ marketplaceParams,
69
+ storeAuthToken=true
70
+ }) {
71
+ let { tenantSlug, marketplaceSlug, marketplaceId, marketplaceHash } = (marketplaceParams || {});
72
+
73
+ if(!Configuration[network]) {
74
+ throw Error(`ElvWalletClient: Invalid network ${network}`);
75
+ } else if(!Configuration[network][mode]) {
76
+ throw Error(`ElvWalletClient: Invalid mode ${mode}`);
77
+ }
78
+
79
+ const client = await ElvClient.FromNetworkName({networkName: network, assumeV3: true});
80
+
81
+ const walletClient = new ElvWalletClient({
82
+ client,
83
+ network,
84
+ mode,
85
+ marketplaceInfo: {
86
+ tenantSlug,
87
+ marketplaceSlug,
88
+ marketplaceId: marketplaceHash ? client.utils.DecodeVersionHash(marketplaceHash).objectId : marketplaceId,
89
+ marketplaceHash
90
+ },
91
+ storeAuthToken
92
+ });
93
+
94
+ if(window && window.location && window.location.href) {
95
+ let url = new URL(window.location.href);
96
+ if(url.searchParams.get("elvToken")) {
97
+ await walletClient.Authenticate({token: url.searchParams.get("elvToken")});
98
+
99
+ url.searchParams.delete("elvToken");
100
+
101
+ window.history.replaceState("", "", url);
102
+ } else if(storeAuthToken && typeof localStorage !== "undefined") {
103
+ try {
104
+ // Load saved auth token
105
+ let savedToken = localStorage.getItem(`__elv-token-${network}`);
106
+ if(savedToken) {
107
+ await walletClient.Authenticate({token: savedToken});
108
+ }
109
+ // eslint-disable-next-line no-empty
110
+ } catch(error) {}
111
+ }
112
+ }
113
+
114
+ await walletClient.LoadAvailableMarketplaces();
115
+
116
+ return walletClient;
117
+ }
118
+
119
+ /* Login and authorization */
120
+
121
+ /**
122
+ * Check if this client can sign without opening a popup.
123
+ *
124
+ * Generally, Eluvio custodial wallet users will require a popup prompt, while Metamask and custom OAuth users will not.
125
+ *
126
+ * @methodGroup Signatures
127
+ * @returns {boolean} - Whether or not this client can sign a message without a popup.
128
+ */
129
+ CanSign() {
130
+ if(!this.loggedIn) { return false; }
131
+
132
+ return !!this.__authorization.clusterToken ||
133
+ !!(this.UserInfo().walletName.toLowerCase() === "metamask" && window.ethereum && window.ethereum.isMetaMask && window.ethereum.chainId);
134
+ }
135
+
136
+ /**
137
+ * <b><i>Requires login</i></b>
138
+ *
139
+ * Request the current user sign the specified message.
140
+ *
141
+ * If this client is not able to perform the signature (Eluvio custodial OAuth users), a popup will be opened and the user will be prompted to sign.
142
+ *
143
+ * To check if the signature can be done without a popup, use the <a href="#CanSign">CanSign</a> method.
144
+ *
145
+ * @methodGroup Signatures
146
+ * @namedParams
147
+ * @param {string} message - The message to sign
148
+ *
149
+ * @throws - If the user rejects the signature or closes the popup, an error will be thrown.
150
+ *
151
+ * @returns {Promise<string>} - The signature of the message
152
+ */
153
+ async PersonalSign({message}) {
154
+ if(!this.loggedIn) { throw Error("ElvWalletClient: Unable to perform signature - Not logged in"); }
155
+
156
+ // Able to sign locally with either cluster token or metamask
157
+ if(this.CanSign()) {
158
+ if(this.__authorization.clusterToken) {
159
+ // Custodial wallet sign
160
+
161
+ message = typeof message === "object" ? JSON.stringify(message) : message;
162
+ message = Ethers.utils.keccak256(Buffer.from(`\x19Ethereum Signed Message:\n${message.length}${message}`, "utf-8"));
163
+
164
+ return await this.client.authClient.Sign(message);
165
+ } else if(this.UserInfo().walletName.toLowerCase() === "metamask") {
166
+ return this.SignMetamask({message, address: this.UserAddress()});
167
+ } else {
168
+ throw Error("ElvWalletClient: Unable to sign");
169
+ }
170
+ }
171
+
172
+ const parameters = {
173
+ action: "personal-sign",
174
+ message,
175
+ logIn: true
176
+ };
177
+
178
+ let url = new URL(this.appUrl);
179
+ url.hash = UrlJoin("/action", "sign", Utils.B58(JSON.stringify(parameters)));
180
+ url.searchParams.set("origin", window.location.origin);
181
+
182
+ return await new Promise(async (resolve, reject) => {
183
+ await ActionPopup({
184
+ mode: "tab",
185
+ url: url.toString(),
186
+ onCancel: () => reject("User cancelled sign"),
187
+ onMessage: async (event, Close) => {
188
+ if(!event || !event.data || event.data.type !== "FlowResponse") {
189
+ return;
190
+ }
191
+
192
+ try {
193
+ resolve(event.data.response);
194
+ } catch(error) {
195
+ reject(error);
196
+ } finally {
197
+ Close();
198
+ }
199
+ }
200
+ });
201
+ });
202
+ }
203
+
204
+ /**
205
+ * Direct the user to the Eluvio Media Wallet login page.
206
+ *
207
+ * <b>NOTE:</b> The domain of the opening window (popup flow) or domain of the `callbackUrl` (redirect flow) MUST be allowed in the metadata of the specified marketplace.
208
+ *
209
+ * @methodGroup Login
210
+ * @namedParams
211
+ * @param {string=} method=redirect - How to present the login page.
212
+ * - `redirect` - Redirect to the wallet login page. Upon login, the page will be redirected back to the specified `redirectUrl` with the authorization token.
213
+ * - `popup` - Open the wallet login page in a new tab. Upon login, authorization information will be sent back to the client via message and the tab will be closed.
214
+ * @param {string=} provider - If logging in via a specific method, specify the provider and mode. Options: `oauth`, `metamask`
215
+ * @param {string=} mode - If logging in via a specific method, specify the mode. Options `login` (Log In), `create` (Sign Up)
216
+ * @param {string=} callbackUrl - If using the redirect flow, the URL to redirect back to after login.
217
+ * @param {Object=} marketplaceParams - Parameters of a marketplace to associate the login with. If not specified, the marketplace parameters used upon client initialization will be used. A marketplace is required when using the redirect flow.
218
+ * @param {boolean=} clearLogin=false - If specified, the user will be prompted to log in anew even if they are already logged in on the Eluvio Media Wallet app
219
+ *
220
+ * @throws - If using the popup flow and the user closes the popup, this method will throw an error.
221
+ */
222
+ async LogIn({
223
+ method="redirect",
224
+ provider,
225
+ mode="login",
226
+ callbackUrl,
227
+ marketplaceParams,
228
+ clearLogin=false,
229
+ callback
230
+ }) {
231
+ let loginUrl = new URL(this.appUrl);
232
+ loginUrl.hash = "/login";
233
+
234
+ loginUrl.searchParams.set("origin", window.location.origin);
235
+ loginUrl.searchParams.set("action", "login");
236
+
237
+ if(provider) {
238
+ loginUrl.searchParams.set("provider", provider);
239
+ }
240
+
241
+ if(mode) {
242
+ loginUrl.searchParams.set("mode", mode);
243
+ }
244
+
245
+ if(marketplaceParams) {
246
+ loginUrl.searchParams.set("mid", (await this.MarketplaceInfo({marketplaceParams})).marketplaceHash);
247
+ } else if((this.selectedMarketplaceInfo || {}).marketplaceHash) {
248
+ loginUrl.searchParams.set("mid", this.selectedMarketplaceInfo.marketplaceHash);
249
+ }
250
+
251
+ if(clearLogin) {
252
+ loginUrl.searchParams.set("clear", "");
253
+ }
254
+
255
+ if(method === "redirect") {
256
+ loginUrl.searchParams.set("response", "redirect");
257
+ loginUrl.searchParams.set("source", "origin");
258
+ loginUrl.searchParams.set("redirect", callbackUrl);
259
+
260
+ window.location = loginUrl;
261
+ } else {
262
+ loginUrl.searchParams.set("response", "message");
263
+ loginUrl.searchParams.set("source", "parent");
264
+
265
+ await new Promise(async (resolve, reject) => {
266
+ await ActionPopup({
267
+ mode: "tab",
268
+ url: loginUrl.toString(),
269
+ onCancel: () => reject("User cancelled login"),
270
+ onMessage: async (event, Close) => {
271
+ if(!event || !event.data || event.data.type !== "LoginResponse") {
272
+ return;
273
+ }
274
+
275
+ try {
276
+ if(callback) {
277
+ await callback(event.data.params);
278
+ } else {
279
+ await this.Authenticate({token: event.data.params.clientSigningToken || event.data.params.clientAuthToken});
280
+ }
281
+
282
+ resolve();
283
+ } catch(error) {
284
+ reject(error);
285
+ } finally {
286
+ Close();
287
+ }
288
+ }
289
+ });
290
+ });
291
+ }
292
+ }
293
+
294
+ /**
295
+ * Remove authorization for the current user.
296
+ *
297
+ * @methodGroup Login
298
+ */
299
+ LogOut() {
300
+ this.__authorization = {};
301
+ this.loggedIn = false;
302
+
303
+ this.cachedMarketplaces = {};
304
+
305
+ // Delete saved auth token
306
+ if(typeof localStorage !== "undefined") {
307
+ try {
308
+ localStorage.removeItem(`__elv-token-${this.network}`);
309
+ // eslint-disable-next-line no-empty
310
+ } catch(error) {}
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Authenticate with an ElvWalletClient authorization token
316
+ *
317
+ * @methodGroup Authorization
318
+ * @namedParams
319
+ * @param {string} token - A previously generated ElvWalletClient authorization token;
320
+ */
321
+ async Authenticate({token}) {
322
+ let decodedToken;
323
+ try {
324
+ decodedToken = JSON.parse(this.utils.FromB58ToStr(token)) || {};
325
+ } catch(error) {
326
+ throw new Error("Invalid authorization token " + token);
327
+ }
328
+
329
+ if(!decodedToken.expiresAt || Date.now() > decodedToken.expiresAt) {
330
+ throw Error("ElvWalletClient: Provided authorization token has expired");
331
+ }
332
+
333
+ if(decodedToken.clusterToken) {
334
+ await this.client.SetRemoteSigner({authToken: decodedToken.clusterToken});
335
+ }
336
+
337
+ this.client.SetStaticToken({token: decodedToken.fabricToken});
338
+
339
+ return this.SetAuthorization(decodedToken);
340
+ }
341
+
342
+ /**
343
+ * Authenticate with an OAuth ID token
344
+ *
345
+ * @methodGroup Authorization
346
+ * @namedParams
347
+ * @param {string} idToken - An OAuth ID token
348
+ * @param {string=} tenantId - ID of tenant with which to associate the user. If marketplace info was set upon initialization, this will be determined automatically.
349
+ * @param {string=} email - Email address of the user. If not specified, this method will attempt to extract the email from the ID token.
350
+ * @param {boolean=} shareEmail=false - Whether or not the user consents to sharing their email
351
+ * @param {number=} tokenDuration=24 - Number of hours the generated authorization token will last before expiring
352
+ *
353
+ * @returns {Promise<Object>} - Returns an authorization tokens that can be used to initialize the client using <a href="#Authenticate">Authenticate</a>.
354
+ * Save this token to avoid having to reauthenticate with OAuth. This token expires after 24 hours.
355
+ *
356
+ * The result includes two tokens:
357
+ * - token - Standard client auth token used to access content and perform actions on behalf of the user.
358
+ * - signingToken - Identical to `authToken`, but also includes the ability to perform arbitrary signatures with the custodial wallet. This token should be protected and should not be
359
+ * shared with third parties.
360
+ */
361
+ async AuthenticateOAuth({idToken, tenantId, email, shareEmail=false, tokenDuration=24}) {
362
+ if(!tenantId && this.selectedMarketplaceInfo) {
363
+ // Load tenant ID automatically from selected marketplace
364
+ await this.AvailableMarketplaces();
365
+ tenantId = this.selectedMarketplaceInfo.tenantId;
366
+ }
367
+
368
+ await this.client.SetRemoteSigner({idToken, tenantId, extraData: { share_email: shareEmail }, unsignedPublicAuth: true});
369
+
370
+ const expiresAt = Date.now() + tokenDuration * 60 * 60 * 1000;
371
+ const fabricToken = await this.client.CreateFabricToken({duration: tokenDuration * 60 * 60 * 1000});
372
+ const address = this.client.utils.FormatAddress(this.client.CurrentAccountAddress());
373
+
374
+ if(!email) {
375
+ try {
376
+ const decodedToken = JSON.parse(this.utils.FromB64URL(idToken.split(".")[1]));
377
+ email = decodedToken.email;
378
+ } catch(error) {
379
+ throw Error("Failed to decode ID token");
380
+ }
381
+ }
382
+
383
+ this.client.SetStaticToken({token: fabricToken});
384
+
385
+ return {
386
+ authToken: this.SetAuthorization({
387
+ fabricToken,
388
+ tenantId,
389
+ address,
390
+ email,
391
+ expiresAt,
392
+ walletType: "Custodial",
393
+ walletName: "Eluvio"
394
+ }),
395
+ signingToken: this.SetAuthorization({
396
+ clusterToken: this.client.signer.authToken,
397
+ fabricToken,
398
+ tenantId,
399
+ address,
400
+ email,
401
+ expiresAt,
402
+ walletType: "Custodial",
403
+ walletName: "Eluvio"
404
+ })
405
+ };
406
+ }
407
+
408
+ /**
409
+ * Authenticate with an external Ethereum compatible wallet, like Metamask.
410
+ *
411
+ * @methodGroup Authorization
412
+ * @namedParams
413
+ * @param {string} address - The address of the wallet
414
+ * @param {number=} tokenDuration=24 - Number of hours the generated authorization token will last before expiring
415
+ * @param {string=} walletName=Metamask - Name of the external wallet
416
+ * @param {function=} Sign - The method used for signing by the wallet. If not specified, will attempt to sign with Metamask.
417
+ *
418
+ * @returns {Promise<string>} - Returns an authorization token that can be used to initialize the client using <a href="#Authenticate">Authenticate</a>.
419
+ * Save this token to avoid having to reauthenticate. This token expires after 24 hours.
420
+ */
421
+ async AuthenticateExternalWallet({address, tokenDuration=24, walletName="Metamask", Sign}) {
422
+ if(!address) {
423
+ address = window.ethereum.selectedAddress;
424
+ }
425
+
426
+ address = this.utils.FormatAddress(address);
427
+
428
+ if(!Sign) {
429
+ Sign = async message => this.SignMetamask({message, address});
430
+ }
431
+
432
+ const expiresAt = Date.now() + tokenDuration * 60 * 60 * 1000;
433
+ const fabricToken = await this.client.CreateFabricToken({
434
+ address,
435
+ duration: tokenDuration * 60 * 60 * 1000,
436
+ Sign,
437
+ addEthereumPrefix: false
438
+ });
439
+
440
+ return this.SetAuthorization({fabricToken, address, expiresAt, walletType: "External", walletName});
441
+ }
442
+
443
+ /**
444
+ * <b><i>Requires login</i></b>
445
+ *
446
+ * Retrieve the current client auth token
447
+ *
448
+ * @returns {string} - The client auth token
449
+ */
450
+ ClientAuthToken() {
451
+ if(!this.loggedIn) { return ""; }
452
+
453
+ return this.utils.B58(JSON.stringify(this.__authorization));
454
+ }
455
+
456
+ AuthToken() {
457
+ if(!this.loggedIn) {
458
+ return this.publicStaticToken;
459
+ }
460
+
461
+ return this.__authorization.fabricToken;
462
+ }
463
+
464
+ SetAuthorization({clusterToken, fabricToken, tenantId, address, email, expiresAt, walletType, walletName}) {
465
+ address = this.client.utils.FormatAddress(address);
466
+
467
+ this.__authorization = {
468
+ fabricToken,
469
+ tenantId,
470
+ address,
471
+ email,
472
+ expiresAt,
473
+ walletType,
474
+ walletName
475
+ };
476
+
477
+ if(clusterToken) {
478
+ this.__authorization.clusterToken = clusterToken;
479
+ }
480
+
481
+ this.loggedIn = true;
482
+
483
+ this.cachedMarketplaces = {};
484
+
485
+ const token = this.ClientAuthToken();
486
+
487
+ if(this.storeAuthToken && typeof localStorage !== "undefined") {
488
+ try {
489
+ localStorage.setItem(`__elv-token-${this.network}`, token);
490
+ // eslint-disable-next-line no-empty
491
+ } catch(error) {}
492
+ }
493
+
494
+ return token;
495
+ }
496
+
497
+ async SignMetamask({message, address}) {
498
+ if(!window.ethereum) {
499
+ throw Error("ElvWalletClient: Unable to initialize - Metamask not available");
500
+ }
501
+
502
+ address = address || this.UserAddress();
503
+
504
+ const accounts = await window.ethereum.request({method: "eth_requestAccounts"});
505
+ if(address && !Utils.EqualAddress(accounts[0], address)) {
506
+ throw Error(`ElvWalletClient: Incorrect MetaMask account selected. Expected ${address}, got ${accounts[0]}`);
507
+ }
508
+
509
+ return await window.ethereum.request({
510
+ method: "personal_sign",
511
+ params: [message, address, ""],
512
+ });
513
+ }
514
+
515
+
516
+
517
+ // Internal loading methods
518
+
519
+
520
+
521
+ // If marketplace slug is specified, load only that marketplace. Otherwise load all
522
+ async LoadAvailableMarketplaces(forceReload=false) {
523
+ if(!forceReload && Object.keys(this.availableMarketplaces) > 0) {
524
+ return;
525
+ }
526
+
527
+ const mainSiteHash = await this.client.LatestVersionHash({objectId: this.mainSiteId});
528
+ const metadata = await this.client.ContentObjectMetadata({
529
+ versionHash: mainSiteHash,
530
+ metadataSubtree: "public/asset_metadata/tenants",
531
+ resolveLinks: true,
532
+ linkDepthLimit: 2,
533
+ resolveIncludeSource: true,
534
+ resolveIgnoreErrors: true,
535
+ produceLinkUrls: true,
536
+ authorizationToken: this.publicStaticToken,
537
+ noAuth: true,
538
+ select: [
539
+ "*/.",
540
+ "*/marketplaces/*/.",
541
+ "*/marketplaces/*/info/tenant_id",
542
+ "*/marketplaces/*/info/tenant_name",
543
+ "*/marketplaces/*/info/branding"
544
+ ],
545
+ remove: [
546
+ "*/marketplaces/*/info/branding/custom_css"
547
+ ]
548
+ });
549
+
550
+ let availableMarketplaces = { ...(this.availableMarketplaces || {}) };
551
+ let availableMarketplacesById = { ...(this.availableMarketplacesById || {}) };
552
+ Object.keys(metadata || {}).forEach(tenantSlug => {
553
+ try {
554
+ availableMarketplaces[tenantSlug] = {
555
+ versionHash: metadata[tenantSlug]["."].source
556
+ };
557
+
558
+ Object.keys(metadata[tenantSlug].marketplaces || {}).forEach(marketplaceSlug => {
559
+ try {
560
+ const versionHash = metadata[tenantSlug].marketplaces[marketplaceSlug]["."].source;
561
+ const objectId = this.utils.DecodeVersionHash(versionHash).objectId;
562
+
563
+ availableMarketplaces[tenantSlug][marketplaceSlug] = {
564
+ ...(metadata[tenantSlug].marketplaces[marketplaceSlug].info || {}),
565
+ tenantName: metadata[tenantSlug].marketplaces[marketplaceSlug].info.tenant_name,
566
+ tenantId: metadata[tenantSlug].marketplaces[marketplaceSlug].info.tenant_id,
567
+ tenantSlug,
568
+ marketplaceSlug,
569
+ marketplaceId: objectId,
570
+ marketplaceHash: versionHash,
571
+ order: Configuration.__MARKETPLACE_ORDER.findIndex(slug => slug === marketplaceSlug)
572
+ };
573
+
574
+ availableMarketplacesById[objectId] = availableMarketplaces[tenantSlug][marketplaceSlug];
575
+
576
+ this.marketplaceHashes[objectId] = versionHash;
577
+
578
+ // Fill out selected marketplace info
579
+ if(this.selectedMarketplaceInfo) {
580
+ if((this.selectedMarketplaceInfo.tenantSlug === tenantSlug && this.selectedMarketplaceInfo.marketplaceSlug === marketplaceSlug) || this.selectedMarketplaceInfo.marketplaceId === objectId) {
581
+ this.selectedMarketplaceInfo = availableMarketplaces[tenantSlug][marketplaceSlug];
582
+ }
583
+ }
584
+ } catch(error) {
585
+ this.Log(`Eluvio Wallet Client: Unable to load info for marketplace ${tenantSlug}/${marketplaceSlug}`, true);
586
+ }
587
+ });
588
+ } catch(error) {
589
+ this.Log(`Eluvio Wallet Client: Failed to load tenant info ${tenantSlug}`, true);
590
+ this.Log(error, true);
591
+ }
592
+ });
593
+
594
+ this.availableMarketplaces = availableMarketplaces;
595
+ this.availableMarketplacesById = availableMarketplacesById;
596
+ }
597
+
598
+ // Get the hash of the currently linked marketplace
599
+ async LatestMarketplaceHash({tenantSlug, marketplaceSlug}) {
600
+ const mainSiteHash = await this.client.LatestVersionHash({objectId: this.mainSiteId});
601
+ const marketplaceLink = await this.client.ContentObjectMetadata({
602
+ versionHash: mainSiteHash,
603
+ metadataSubtree: UrlJoin("/public", "asset_metadata", "tenants", tenantSlug, "marketplaces", marketplaceSlug),
604
+ resolveLinks: false
605
+ });
606
+
607
+ return LinkTargetHash(marketplaceLink);
608
+ }
609
+
610
+ async LoadMarketplace(marketplaceParams) {
611
+ const marketplaceInfo = this.MarketplaceInfo({marketplaceParams});
612
+
613
+ const marketplaceId = marketplaceInfo.marketplaceId;
614
+ const marketplaceHash = await this.LatestMarketplaceHash({tenantSlug: marketplaceInfo.tenantSlug, marketplaceSlug: marketplaceInfo.marketplaceSlug});
615
+
616
+ if(this.cachedMarketplaces[marketplaceId] && this.cachedMarketplaces[marketplaceId].versionHash !== marketplaceHash) {
617
+ delete this.cachedMarketplaces[marketplaceId];
618
+ }
619
+
620
+ if(!this.cachedMarketplaces[marketplaceId]) {
621
+ let marketplace = await this.client.ContentObjectMetadata({
622
+ versionHash: marketplaceHash,
623
+ metadataSubtree: "public/asset_metadata/info",
624
+ linkDepthLimit: 2,
625
+ resolveLinks: true,
626
+ resolveIgnoreErrors: true,
627
+ resolveIncludeSource: true,
628
+ produceLinkUrls: true,
629
+ authorizationToken: this.publicStaticToken
630
+ });
631
+
632
+ marketplace.items = await Promise.all(
633
+ marketplace.items.map(async (item, index) => {
634
+ if(item.requires_permissions) {
635
+ if(!this.loggedIn) {
636
+ item.authorized = false;
637
+ } else {
638
+ try {
639
+ await this.client.ContentObjectMetadata({
640
+ versionHash: LinkTargetHash(item.nft_template),
641
+ metadataSubtree: "permissioned"
642
+ });
643
+
644
+ item.authorized = true;
645
+ } catch(error) {
646
+ item.authorized = false;
647
+ }
648
+ }
649
+ }
650
+
651
+ item.nftTemplateMetadata = ((item.nft_template || {}).nft || {});
652
+ item.itemIndex = index;
653
+
654
+ return item;
655
+ })
656
+ );
657
+
658
+ marketplace.collections = (marketplace.collections || []).map((collection, collectionIndex) => ({
659
+ ...collection,
660
+ collectionIndex
661
+ }));
662
+
663
+ marketplace.retrievedAt = Date.now();
664
+ marketplace.marketplaceId = marketplaceId;
665
+ marketplace.versionHash = marketplaceHash;
666
+
667
+ // Generate embed URLs for pack opening animations
668
+ ["purchase_animation", "purchase_animation__mobile", "reveal_animation", "reveal_animation_mobile"].forEach(key => {
669
+ try {
670
+ if(marketplace.storefront[key]) {
671
+ let embedUrl = new URL("https://embed.v3.contentfabric.io");
672
+ const targetHash = LinkTargetHash(marketplace.storefront[key]);
673
+ embedUrl.searchParams.set("p", "");
674
+ embedUrl.searchParams.set("net", this.network === "main" ? "main" : "demo");
675
+ embedUrl.searchParams.set("ath", (this.__authorization || {}).authToken || this.publicStaticToken);
676
+ embedUrl.searchParams.set("vid", targetHash);
677
+ embedUrl.searchParams.set("ap", "");
678
+
679
+ if(!key.startsWith("reveal")) {
680
+ embedUrl.searchParams.set("m", "");
681
+ embedUrl.searchParams.set("lp", "");
682
+ }
683
+
684
+ marketplace.storefront[`${key}_embed_url`] = embedUrl.toString();
685
+ }
686
+ // eslint-disable-next-line no-empty
687
+ } catch(error) {
688
+ }
689
+ });
690
+
691
+ this.cachedMarketplaces[marketplaceId] = marketplace;
692
+ }
693
+
694
+ return this.cachedMarketplaces[marketplaceId];
695
+ }
696
+
697
+ async FilteredQuery({
698
+ mode="listings",
699
+ sortBy="created",
700
+ sortDesc=false,
701
+ filter,
702
+ editionFilter,
703
+ attributeFilters,
704
+ contractAddress,
705
+ tokenId,
706
+ currency,
707
+ marketplaceParams,
708
+ tenantId,
709
+ collectionIndex=-1,
710
+ sellerAddress,
711
+ lastNDays=-1,
712
+ start=0,
713
+ limit=50
714
+ }={}) {
715
+ collectionIndex = parseInt(collectionIndex);
716
+
717
+ let params = {
718
+ sort_by: sortBy,
719
+ sort_descending: sortDesc,
720
+ start,
721
+ limit
722
+ };
723
+
724
+ let marketplaceInfo, marketplace;
725
+ if(marketplaceParams) {
726
+ marketplaceInfo = await this.MarketplaceInfo({marketplaceParams});
727
+
728
+ if(collectionIndex >= 0) {
729
+ marketplace = await this.Marketplace({marketplaceParams});
730
+ }
731
+ }
732
+
733
+ try {
734
+ let filters = [];
735
+
736
+ if(sellerAddress) {
737
+ filters.push(`seller:eq:${this.client.utils.FormatAddress(sellerAddress)}`);
738
+ }
739
+
740
+ if(marketplace && collectionIndex >= 0) {
741
+ const collection = marketplace.collections[collectionIndex];
742
+
743
+ collection.items.forEach(sku => {
744
+ if(!sku) { return; }
745
+
746
+ const item = marketplace.items.find(item => item.sku === sku);
747
+
748
+ if(!item) { return; }
749
+
750
+ const address = Utils.SafeTraverse(item, "nft_template", "nft", "address");
751
+
752
+ if(address) {
753
+ filters.push(
754
+ `${mode === "owned" ? "contract_addr": "contract"}:eq:${Utils.FormatAddress(address)}`
755
+ );
756
+ }
757
+ });
758
+
759
+ // No valid items, so there must not be anything relevant in the collection
760
+ if(filters.length === 0) {
761
+ if(mode.includes("stats")) {
762
+ return {};
763
+ } else {
764
+ return {
765
+ paging: {
766
+ start: params.start,
767
+ limit: params.limit,
768
+ total: 0,
769
+ more: false
770
+ },
771
+ results: []
772
+ };
773
+ }
774
+ }
775
+ } else if(mode !== "owned" && marketplaceInfo || tenantId) {
776
+ filters.push(`tenant:eq:${marketplaceInfo ? marketplaceInfo.tenantId : tenantId}`);
777
+ }
778
+
779
+ if(contractAddress) {
780
+ if(mode === "owned") {
781
+ filters.push(`contract_addr:eq:${Utils.FormatAddress(contractAddress)}`);
782
+ } else {
783
+ filters.push(`contract:eq:${Utils.FormatAddress(contractAddress)}`);
784
+ }
785
+
786
+ if(tokenId) {
787
+ filters.push(`token:eq:${tokenId}`);
788
+ }
789
+ } else if(filter) {
790
+ if(mode.includes("listing")) {
791
+ filters.push(`nft/display_name:eq:${filter}`);
792
+ } else if(mode === "owned") {
793
+ filters.push(`meta:@>:{"display_name":"${filter}"}`);
794
+ params.exact = false;
795
+ } else {
796
+ filters.push(`name:eq:${filter}`);
797
+ }
798
+ }
799
+
800
+ if(editionFilter) {
801
+ if(mode.includes("listing")) {
802
+ filters.push(`nft/edition_name:eq:${editionFilter}`);
803
+ } else if(mode === "owned") {
804
+ filters.push(`meta:@>:{"edition_name":"${editionFilter}"}`);
805
+ params.exact = false;
806
+ } else {
807
+ filters.push(`edition:eq:${editionFilter}`);
808
+ }
809
+ }
810
+
811
+ if(attributeFilters) {
812
+ attributeFilters.map(({name, value}) => {
813
+ if(!name || !value) { return; }
814
+
815
+ filters.push(`nft/attributes/${name}:eq:${value}`);
816
+ });
817
+ }
818
+
819
+ if(currency) {
820
+ filters.push("link_type:eq:sol");
821
+ }
822
+
823
+ if(lastNDays && lastNDays > 0) {
824
+ filters.push(`created:gt:${((Date.now() / 1000) - ( lastNDays * 24 * 60 * 60 )).toFixed(0)}`);
825
+ }
826
+
827
+ let path;
828
+ switch(mode) {
829
+ case "owned":
830
+ path = UrlJoin("as", "wlt", "nfts");
831
+
832
+ if(marketplaceInfo) {
833
+ path = UrlJoin("as", "wlt", "nfts", marketplaceInfo.tenantId);
834
+ }
835
+
836
+ break;
837
+
838
+ case "listings":
839
+ path = UrlJoin("as", "mkt", "f");
840
+ break;
841
+
842
+ case "transfers":
843
+ path = UrlJoin("as", "mkt", "hst", "f");
844
+ break;
845
+
846
+ case "sales":
847
+ path = UrlJoin("as", "mkt", "hst", "f");
848
+ filters.push("action:eq:SOLD");
849
+ break;
850
+
851
+ case "listing-stats":
852
+ path = UrlJoin("as", "mkt", "stats", "listed");
853
+ break;
854
+
855
+ case "sales-stats":
856
+ path = UrlJoin("as", "mkt", "stats", "sold");
857
+ break;
858
+ }
859
+
860
+ if(filters.length > 0) {
861
+ params.filter = filters;
862
+ }
863
+
864
+ if(mode.includes("stats")) {
865
+ return await Utils.ResponseToJson(
866
+ this.client.authClient.MakeAuthServiceRequest({
867
+ path,
868
+ method: "GET",
869
+ queryParams: params
870
+ })
871
+ );
872
+ }
873
+
874
+ const { contents, paging } = await Utils.ResponseToJson(
875
+ await this.client.authClient.MakeAuthServiceRequest({
876
+ path,
877
+ method: "GET",
878
+ queryParams: params,
879
+ headers: mode === "owned" ?
880
+ { Authorization: `Bearer ${this.AuthToken()}` } :
881
+ {}
882
+ })
883
+ ) || [];
884
+
885
+ return {
886
+ paging: {
887
+ start: params.start,
888
+ limit: params.limit,
889
+ total: paging.total,
890
+ more: paging.total > start + limit
891
+ },
892
+ results: (contents || []).map(item => ["owned", "listings"].includes(mode) ? FormatNFT(item) : item)
893
+ };
894
+ } catch(error) {
895
+ if(error.status && error.status.toString() === "404") {
896
+ return {
897
+ paging: {
898
+ start: params.start,
899
+ limit: params.limit,
900
+ total: 0,
901
+ more: false
902
+ },
903
+ results: []
904
+ };
905
+ }
906
+
907
+ throw error;
908
+ }
909
+ }
910
+
911
+ async MintingStatus({marketplaceParams, tenantId}) {
912
+ if(!tenantId) {
913
+ const marketplaceInfo = await this.MarketplaceInfo({marketplaceParams: marketplaceParams || this.selectedMarketplaceInfo});
914
+ tenantId = marketplaceInfo.tenantId;
915
+ }
916
+
917
+ try {
918
+ const response = await Utils.ResponseToJson(
919
+ this.client.authClient.MakeAuthServiceRequest({
920
+ path: UrlJoin("as", "wlt", "status", "act", tenantId),
921
+ method: "GET",
922
+ headers: {
923
+ Authorization: `Bearer ${this.AuthToken()}`
924
+ }
925
+ })
926
+ );
927
+
928
+ return response
929
+ .map(status => {
930
+ let [op, address, id] = status.op.split(":");
931
+ address = address.startsWith("0x") ? Utils.FormatAddress(address) : address;
932
+
933
+ let confirmationId, tokenId;
934
+ if(op === "nft-buy") {
935
+ confirmationId = id;
936
+ } else if(op === "nft-claim") {
937
+ confirmationId = id;
938
+ status.marketplaceId = address;
939
+
940
+ if(status.extra && status.extra["0"]) {
941
+ address = status.extra.token_addr;
942
+ tokenId = status.extra.token_id_str;
943
+ }
944
+ } else if(op === "nft-redeem") {
945
+ confirmationId = status.op.split(":").slice(-1)[0];
946
+ } else {
947
+ tokenId = id;
948
+ }
949
+
950
+ if(op === "nft-transfer") {
951
+ confirmationId = status.extra && status.extra.trans_id;
952
+ }
953
+
954
+ return {
955
+ ...status,
956
+ timestamp: new Date(status.ts),
957
+ state: status.state && typeof status.state === "object" ? Object.values(status.state) : status.state,
958
+ extra: status.extra && typeof status.extra === "object" ? Object.values(status.extra) : status.extra,
959
+ confirmationId,
960
+ op,
961
+ address,
962
+ tokenId
963
+ };
964
+ })
965
+ .sort((a, b) => a.ts < b.ts ? 1 : -1);
966
+ } catch(error) {
967
+ this.Log("Failed to retrieve minting status", true);
968
+ this.Log(error);
969
+
970
+ return [];
971
+ }
972
+ }
973
+ }
974
+
975
+ Object.assign(ElvWalletClient.prototype, require("./ClientMethods"));
976
+
977
+ exports.ElvWalletClient = ElvWalletClient;