-
Notifications
You must be signed in to change notification settings - Fork 3
/
datanft.ts
634 lines (558 loc) · 21.9 KB
/
datanft.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import {
AbiRegistry,
BinaryCodec,
SignableMessage
} from '@multiversx/sdk-core/out';
import minterAbi from './abis/datanftmint.abi.json';
import {
checkStatus,
createTokenIdentifier,
numberToPaddedHex,
overrideMarshalUrl,
parseDataNft,
validateSpecificParamsViewData
} from './common/utils';
import {
Config,
EnvironmentsEnum,
MAX_ITEMS,
apiConfiguration,
dataNftTokenIdentifier,
networkConfiguration
} from './config';
import {
ErrAttributeNotSet,
ErrDataNftCreate,
ErrDecodeAttributes,
ErrNetworkConfig,
ErrTooManyItems
} from './errors';
import { DataNftType, NftType, ViewDataReturnType } from './interfaces';
import BigNumber from 'bignumber.js';
export class DataNft implements DataNftType {
readonly tokenIdentifier: string = '';
readonly nftImgUrl: string = '';
readonly dataPreview: string = '';
readonly dataStream: string = '';
readonly dataMarshal: string = '';
readonly tokenName: string = '';
readonly creator: string = '';
readonly creationTime: Date = new Date();
readonly supply: BigNumber.Value = 0;
readonly description: string = '';
readonly title: string = '';
readonly royalties: number = 0;
readonly nonce: number = 0;
readonly collection: string = '';
readonly balance: BigNumber.Value = 0;
readonly owner: string = ''; // works if tokenIdentifier is an NFT
readonly overrideDataMarshal: string = '';
readonly overrideDataMarshalChainId: string = '';
readonly isDataNFTPH: boolean = false;
readonly extraAssets: string[] = [];
readonly media: {
url: string;
originalUrl: string;
thumbnailUrl: string;
fileType: string;
fileSize: number;
}[] = [];
static networkConfiguration: Config;
static apiConfiguration: string;
static env: string;
/**
* Creates an instance of DataNft. Can be partially initialized.
* @param init Partial<DataNft>
*/
constructor(init?: Partial<DataNft>) {
Object.assign(this, init);
const override = overrideMarshalUrl(
DataNft.env,
this.collection,
this.nonce
);
this.overrideDataMarshal = override.url;
this.overrideDataMarshalChainId = override.chainId;
}
/**
* Update any attributes for DataNft
*
*/
updateDataNft(init: Partial<DataNft>) {
Object.assign(this, init);
}
/**
* Sets the network configuration for the DataNft class.
* @param env 'devnet' | 'mainnet' | 'testnet'
*/
static setNetworkConfig(env: string) {
if (!(env in EnvironmentsEnum)) {
throw new ErrNetworkConfig(
`Invalid environment: ${env}, Expected: 'devnet' | 'mainnet' | 'testnet'`
);
}
this.env = env;
this.networkConfiguration = networkConfiguration[env as EnvironmentsEnum];
this.apiConfiguration = apiConfiguration[env as EnvironmentsEnum];
}
/**
* Creates a DataNft calling the API and also decoding the attributes.
*
* Not useful for creating an array of DataNft, because it calls the API every single time.
* @param token Object should have a `nonce` property representing the token nonce. An optional `tokenIdentifier` property can be provided to specify the token identifier.
* If not provided, the default token identifier based on the {@link EnvironmentsEnum}
*/
static async createFromApi(token: {
nonce: number;
tokenIdentifier?: string;
}): Promise<DataNft> {
this.ensureNetworkConfigSet();
const identifier = createTokenIdentifier(
token.tokenIdentifier ||
dataNftTokenIdentifier[this.env as EnvironmentsEnum],
token.nonce
);
const response = await fetch(`${this.apiConfiguration}/nfts/${identifier}`);
checkStatus(response);
const data: NftType = await response.json();
try {
const dataNft = parseDataNft(data);
return dataNft;
} catch (error: any) {
throw new ErrDataNftCreate('Response could not be parsed');
}
}
/**
* Creates an array of DataNfts by calling the API and decoding the attributes.
*
* @param tokens An array of objects containing token nonces and optional token identifiers.
* Each object should have a `nonce` property representing the token nonce.
* An optional `tokenIdentifier` property can be provided to specify the token identifier.
* If not provided, the default token identifier based on the {@link EnvironmentsEnum}
* @returns An array of {@link DataNft} objects
*/
static async createManyFromApi(
tokens: { nonce: number; tokenIdentifier?: string }[]
): Promise<DataNft[]> {
this.ensureNetworkConfigSet();
const identifiers = tokens.map(({ nonce, tokenIdentifier }) =>
createTokenIdentifier(
tokenIdentifier || dataNftTokenIdentifier[this.env as EnvironmentsEnum],
nonce
)
);
if (identifiers.length > MAX_ITEMS) {
throw new ErrTooManyItems();
}
const response = await fetch(
`${this.apiConfiguration}/nfts?identifiers=${identifiers.join(
','
)}&withSupply=true&size=${identifiers.length}`
);
checkStatus(response);
const data: NftType[] = await response.json();
try {
const dataNfts = data.map((value) => parseDataNft(value));
return dataNfts;
} catch (error: any) {
throw new ErrDataNftCreate('Response could not be parsed');
}
}
/**
* Creates a DataNft or an array of DataNft from either a single NFT details API response or an array of NFT details API response.
*
* @param payload NFT details API response, can be a single item or an array of items
*/
static createFromApiResponseOrBulk(payload: NftType | NftType[]): DataNft[] {
const dataNfts: DataNft[] = [];
try {
const parseNft = (nft: NftType) => {
const dataNft = parseDataNft(nft);
dataNfts.push(dataNft);
};
if (Array.isArray(payload)) {
payload.forEach(parseNft);
return dataNfts;
} else {
parseNft(payload as NftType);
return dataNfts;
}
} catch (error: any) {
throw new ErrDataNftCreate(
'Payload could not be parsed: ' + error.message
);
}
}
/**
* Static method to decode the attributes of a DataNft
* @param attributes Attributes of the DataNft
*/
static decodeAttributes(attributes: any): Partial<DataNft> {
try {
const codec = new BinaryCodec();
const abiRegistry = AbiRegistry.create(minterAbi);
const dataNftAttributes = abiRegistry.getStruct('DataNftAttributes');
const decodedAttributes = codec
.decodeTopLevel(Buffer.from(attributes, 'base64'), dataNftAttributes)
.valueOf();
return {
dataPreview: decodedAttributes['data_preview_url'].toString(),
dataStream: decodedAttributes['data_stream_url'].toString(),
dataMarshal: decodedAttributes['data_marshal_url'].toString(),
creator: decodedAttributes['creator'].toString(),
creationTime: new Date(
Number(decodedAttributes['creation_time']) * 1000
),
description: decodedAttributes['description'].toString(),
title: decodedAttributes['title'].toString()
};
} catch (error: any) {
throw new ErrDecodeAttributes(error.message);
}
}
/**
* Returns an array of `DataNft` objects owned by the address
* @param address the address to query
* @param collections the collection identifiers to query. If not provided, the default collection identifier based on the {@link EnvironmentsEnum}
*/
static async ownedByAddress(
address: string,
collections?: string[]
): Promise<DataNft[]> {
this.ensureNetworkConfigSet();
const identifiersMap =
collections?.join(',') ||
dataNftTokenIdentifier[this.env as EnvironmentsEnum];
const res = await fetch(
`${this.apiConfiguration}/accounts/${address}/nfts?size=10000&collections=${identifiersMap}&withSupply=true`
);
checkStatus(res);
const data = await res.json();
const dataNfts: DataNft[] = this.createFromApiResponseOrBulk(data);
return dataNfts;
}
/**
* Returns an array of `{address:string,balance:number}` representing the addresses that own the token
*/
async getOwners(): Promise<{ address: string; balance: number }[]> {
if (!this.tokenIdentifier && !this.nonce) {
throw new ErrAttributeNotSet('tokenIdentifier, nonce');
}
const identifier = createTokenIdentifier(this.tokenIdentifier, this.nonce);
const response = await fetch(
`${DataNft.apiConfiguration}/nfts/${identifier}/accounts`
);
checkStatus(response);
const data = await response.json();
return data;
}
/**
* Gets the message to sign from the data marshal of the DataNft
*/
async getMessageToSign(): Promise<string> {
DataNft.ensureNetworkConfigSet();
if (!this.dataMarshal) {
throw new ErrAttributeNotSet('dataMarshal');
}
const res = await fetch(
`${this.dataMarshal}/preaccess?chainId=${
DataNft.networkConfiguration.chainID == 'D'
? 'ED'
: DataNft.networkConfiguration.chainID
}`
);
checkStatus(res);
const data = await res.json();
return data.nonce;
}
/**
* Method to get the data via the Data Marshal.
* @param signedMessage Signed message from the data marshal
* @param signableMessage Signable message from the wallet
* @param stream [optional] Instead of auto-downloading if possible, request if data should always be streamed or not. i.e. true=stream, false/undefined=default behavior
* @param fwdAllHeaders [optional] Forward all request headers to the Origin Data Stream server.
* @param fwdHeaderKeys [optional] Forward only selected headers to the Origin Data Stream server. Has priority over fwdAllHeaders param. A comma separated lowercase string with less than 5 items. e.g. cookie,authorization
* @param fwdHeaderMapLookup [optional] Used with fwdHeaderKeys to set a front-end client side lookup map of headers the SDK uses to setup the forward. e.g. { cookie : "xyz", authorization : "Bearer zxy" }. Note that these are case-sensitive and need to match fwdHeaderKeys exactly.
* @param nestedIdxToStream [optional] If you are accessing a "nested stream", this is the index of the nested item you want drill into and fetch
* @param asDeputyOnAppointerAddr [optional] Put caller in the "deputy persona" of this deputy appointer address (which should be a smart contract that holds the Data NFT Id)
*/
async viewData(p: {
signedMessage: string;
signableMessage: SignableMessage;
stream?: boolean;
fwdAllHeaders?: boolean;
fwdHeaderKeys?: string;
fwdHeaderMapLookup?: {
[key: string]: any;
};
nestedIdxToStream?: number;
asDeputyOnAppointerAddr?: string;
}): Promise<ViewDataReturnType> {
DataNft.ensureNetworkConfigSet();
if (!this.dataMarshal) {
throw new ErrAttributeNotSet('dataMarshal');
}
const signResult = {
signature: '',
addrInHex: '',
success: false,
exception: ''
};
// S: run any format specific validation
const { allPassed, validationMessages } = validateSpecificParamsViewData({
signedMessage: p.signedMessage,
signableMessage: p.signableMessage,
stream: p.stream,
fwdAllHeaders: p.fwdAllHeaders,
fwdHeaderKeys: p.fwdHeaderKeys,
fwdHeaderMapLookup: p.fwdHeaderMapLookup,
nestedIdxToStream: p.nestedIdxToStream,
asDeputyOnAppointerAddr: p.asDeputyOnAppointerAddr,
_mandatoryParamsList: ['signedMessage', 'signableMessage']
});
if (!allPassed) {
throw new Error(`params have validation issues = ${validationMessages}`);
}
// E: run any format specific validation...
try {
if (p.signableMessage?.signature && p.signableMessage?.address) {
if (p.signableMessage.signature instanceof Buffer) {
signResult.signature = p.signableMessage.signature.toString('hex');
} else if (
typeof (p.signableMessage.signature as any).hex === 'function'
) {
signResult.signature = (p.signableMessage.signature as any).hex();
}
signResult.addrInHex = p.signableMessage.address.hex();
signResult.success = true;
} else {
signResult.exception = 'Some Error';
}
} catch (e: any) {
signResult.success = false;
signResult.exception = e.toString();
}
try {
let url = `${this.dataMarshal}/access?nonce=${p.signedMessage}&NFTId=${
this.collection
}-${numberToPaddedHex(this.nonce)}&signature=${
signResult.signature
}&chainId=${
DataNft.networkConfiguration.chainID == 'D'
? 'ED'
: DataNft.networkConfiguration.chainID
}&accessRequesterAddr=${signResult.addrInHex}`;
type FetchConfig = {
[key: string]: any;
};
const fetchConfig: FetchConfig = {
method: 'GET'
};
// S: append optional params if found
if (typeof p.stream !== 'undefined') {
url += p.stream ? '&streamInLine=1' : '';
}
if (typeof p.fwdAllHeaders !== 'undefined') {
url += p.fwdAllHeaders ? '&fwdAllHeaders=1' : '';
}
if (typeof p.nestedIdxToStream !== 'undefined') {
url += `&nestedIdxToStream=${p.nestedIdxToStream}`;
}
if (typeof p.fwdHeaderKeys !== 'undefined') {
url += `&fwdHeaderKeys=${p.fwdHeaderKeys}`;
// if fwdHeaderMapLookup exists, send these headers and values to the data marshal for forwarding
if (
typeof p.fwdHeaderMapLookup !== 'undefined' &&
Object.keys(p.fwdHeaderMapLookup).length > 0
) {
fetchConfig.headers = {};
Object.keys(p.fwdHeaderMapLookup).forEach((headerKey: string) => {
fetchConfig.headers[headerKey] = p.fwdHeaderMapLookup?.[headerKey];
});
}
}
if (typeof p.asDeputyOnAppointerAddr !== 'undefined') {
url += `&asDeputyOnAppointerAddr=${p.asDeputyOnAppointerAddr}`;
}
// E: append optional params...
const response = await fetch(url, fetchConfig);
const contentType = response.headers.get('content-type');
const data = await response.blob();
// if the marshal returned a error, we should throw it here so that the SDK integrator can handle it
// ... if we don't, the marshal error response is just passed through as a normal data stream response
// ... and the user won't know what went wrong
try {
checkStatus(response);
} catch (e: any) {
// as it's a data marshal error, we get it's payload which is in JSON and send that thrown as text
const errorPayload = await (data as Blob).text();
throw new Error(
`${e.toString()}. Detailed error trace follows : ${errorPayload}`
);
}
return {
data: data,
contentType: contentType || ''
};
} catch (err) {
return {
data: undefined,
contentType: '',
error: (err as Error).message
};
}
}
/**
* Method to get the data from the data marshal by authenticating and authorizing via MultiversX Native Auth. This has a better UX as it does not need a manually signed signableMessage
* @param mvxNativeAuthOrigins An string array of domains that the access token is validated against. e.g. ["http://localhost:3000", "https://mycoolsite.com"]
* @param mvxNativeAuthMaxExpirySeconds An number of that represents the "max expiry seconds" of your access token. e.g. if your client side access token is set for 5 mins then send in 300
* @param fwdHeaderMapLookup Used with fwdHeaderKeys to set a front-end client side lookup map of headers the SDK uses to setup the forward. e.g. { cookie : "xyz", authorization : "Bearer zxy" }. As it's Native Auth, you must sent in the authorization : "Bearer zxy" entry. Note that these are case-sensitive and need to match fwdHeaderKeys exactly for other entries.
* @param fwdHeaderKeys [optional] Forward only selected headers to the Origin Data Stream server. Has priority over fwdAllHeaders param. A comma separated lowercase string with less than 5 items. e.g. cookie,authorization
* @param fwdAllHeaders [optional] Forward all request headers to the Origin Data Stream server.
* @param stream [optional] Instead of auto-downloading if possible, request if data should always be streamed or not.i.e true=stream, false/undefined=default behavior
* @param nestedIdxToStream [optional] If you are accessing a "nested stream", this is the index of the nested item you want drill into and fetch
* @param asDeputyOnAppointerAddr [optional] Put caller in the "deputy persona" of this deputy appointer address (which should be a smart contract that holds the Data NFT Id)
*/
async viewDataViaMVXNativeAuth(p: {
mvxNativeAuthOrigins: string[];
mvxNativeAuthMaxExpirySeconds: number;
fwdHeaderMapLookup: {
[key: string]: any;
};
fwdHeaderKeys?: string;
fwdAllHeaders?: boolean;
stream?: boolean;
nestedIdxToStream?: number;
asDeputyOnAppointerAddr?: string;
}): Promise<ViewDataReturnType> {
try {
// S: run any format specific validation
const { allPassed, validationMessages } = validateSpecificParamsViewData({
mvxNativeAuthOrigins: p.mvxNativeAuthOrigins,
mvxNativeAuthMaxExpirySeconds: p.mvxNativeAuthMaxExpirySeconds,
fwdHeaderKeys: p.fwdHeaderKeys,
fwdHeaderMapLookup: p.fwdHeaderMapLookup,
fwdAllHeaders: p.fwdAllHeaders,
stream: p.stream,
nestedIdxToStream: p.nestedIdxToStream,
asDeputyOnAppointerAddr: p.asDeputyOnAppointerAddr,
_fwdHeaderMapLookupMustContainBearerAuthHeader: true,
_mandatoryParamsList: [
'mvxNativeAuthOrigins',
'mvxNativeAuthMaxExpirySeconds',
'fwdHeaderMapLookup'
]
});
if (!allPassed) {
throw new Error(
`params have validation issues = ${validationMessages}`
);
}
// E: run any format specific validation...
// convert mvxNativeAuthOrigins from a string array to API required base64 format
let mvxNativeAuthOriginsToBase64 = p.mvxNativeAuthOrigins.join(','); // convert the array to a string
mvxNativeAuthOriginsToBase64 = mvxNativeAuthOriginsToBase64
.trim()
.replaceAll(' ', ''); // remove all spaces
mvxNativeAuthOriginsToBase64 = Buffer.from(
mvxNativeAuthOriginsToBase64
).toString('base64');
let chainId;
if (this.overrideDataMarshalChainId === '') {
chainId =
DataNft.networkConfiguration.chainID === 'D'
? 'ED'
: DataNft.networkConfiguration.chainID;
} else if (this.overrideDataMarshalChainId === 'D') {
chainId = 'ED';
} else {
chainId = this.overrideDataMarshalChainId;
}
let dataMarshal;
if (this.overrideDataMarshal === '') {
dataMarshal = this.dataMarshal;
} else {
dataMarshal = this.overrideDataMarshal;
}
// construct the api url
let url = `${dataMarshal}/access?NFTId=${
this.collection
}-${numberToPaddedHex(
this.nonce
)}&chainId=${chainId}&mvxNativeAuthEnable=1&mvxNativeAuthMaxExpirySeconds=${
p.mvxNativeAuthMaxExpirySeconds
}&mvxNativeAuthOrigins=${mvxNativeAuthOriginsToBase64}`;
type FetchConfig = {
[key: string]: any;
};
const fetchConfig: FetchConfig = {
method: 'GET'
};
// S: append optional params if found
if (typeof p.stream !== 'undefined') {
url += p.stream ? '&streamInLine=1' : '';
}
if (typeof p.fwdAllHeaders !== 'undefined') {
url += p.fwdAllHeaders ? '&fwdAllHeaders=1' : '';
}
if (typeof p.nestedIdxToStream !== 'undefined') {
url += `&nestedIdxToStream=${p.nestedIdxToStream}`;
}
// if fwdHeaderMapLookup exists, send these headers and values to the data marshal for forwarding
if (
typeof p.fwdHeaderMapLookup !== 'undefined' &&
Object.keys(p.fwdHeaderMapLookup).length > 0
) {
// authorization WILL be present based on validation, so let's fwd this as a request header param
fetchConfig.headers = {};
fetchConfig.headers['authorization'] =
p.fwdHeaderMapLookup['authorization'];
// ... and forward any OTHER params user wanted to forward to the origin server via the marshal
if (typeof p.fwdHeaderKeys !== 'undefined') {
url += `&fwdHeaderKeys=${p.fwdHeaderKeys}`;
Object.keys(p.fwdHeaderMapLookup).forEach((headerKey: string) => {
// already appended above so skip it...
if (headerKey !== 'authorization') {
fetchConfig.headers[headerKey] =
p.fwdHeaderMapLookup?.[headerKey];
}
});
}
}
if (typeof p.asDeputyOnAppointerAddr !== 'undefined') {
url += `&asDeputyOnAppointerAddr=${p.asDeputyOnAppointerAddr}`;
}
// E: append optional params...
const response = await fetch(url, fetchConfig);
const contentType = response.headers.get('content-type');
const data = await response.blob();
// if the marshal returned a error, we should throw it here so that the SDK integrator can handle it
// ... if we don't, the marshal error response is just passed through as a normal data stream response
// ... and the user won't know what went wrong
try {
checkStatus(response);
} catch (e: any) {
// as it's a data marshal error, we get it's payload which is in JSON and send that thrown as text
const errorPayload = await (data as Blob).text();
throw new Error(
`${e.toString()}. Detailed error trace follows : ${errorPayload}`
);
}
return {
data: data,
contentType: contentType || ''
};
} catch (err) {
return {
data: undefined,
contentType: '',
error: (err as Error).message
};
}
}
private static ensureNetworkConfigSet() {
if (!this.env || !this.apiConfiguration) {
throw new ErrNetworkConfig();
}
}
}