-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtoken.helpers.ts
More file actions
122 lines (104 loc) · 3.98 KB
/
token.helpers.ts
File metadata and controls
122 lines (104 loc) · 3.98 KB
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
import * as crypto from 'crypto-js';
import { Nft } from "src/endpoints/nfts/entities/nft";
import { NftType } from "src/endpoints/nfts/entities/nft.type";
import { CollectionRoles } from "src/endpoints/tokens/entities/collection.roles";
import { TokenRoles } from "src/endpoints/tokens/entities/token.roles";
import { ApiUtils } from '@multiversx/sdk-nestjs-http';
import '@multiversx/sdk-nestjs-common/lib/utils/extensions/string.extensions';
import { TokenUtils, BinaryUtils } from "@multiversx/sdk-nestjs-common";
export class TokenHelpers {
static canBool(string: string) {
return string.split('-').pop() === 'true';
}
static computeNftUri(uri: string, prefix: string) {
uri = ApiUtils.replaceUri(uri, 'https://ipfs.io/ipfs', prefix);
uri = ApiUtils.replaceUri(uri, 'https://gateway.ipfs.io/ipfs', prefix);
uri = ApiUtils.replaceUri(uri, 'https://gateway.pinata.cloud/ipfs', prefix);
uri = ApiUtils.replaceUri(uri, 'https://dweb.link/ipfs', prefix);
uri = ApiUtils.replaceUri(uri, 'ipfs:/', prefix);
uri = ApiUtils.replaceUri(uri, 'https://media.elrond.com/nfts/asset', prefix);
uri = ApiUtils.replaceUri(uri, 'https://devnet-media.elrond.com/nfts/asset', prefix);
uri = ApiUtils.replaceUri(uri, 'https://testnet-media.elrond.com/nfts/asset', prefix);
uri = ApiUtils.replaceUri(uri, 'https://api.multiversx.com/media/nfts/asset', prefix);
uri = ApiUtils.replaceUri(uri, 'https://devnet-api.multiversx.com/media/nfts/asset', prefix);
uri = ApiUtils.replaceUri(uri, 'https://testnet-api.multiversx.com/media/nfts/asset', prefix);
uri = uri.replace(/https\:\/\/\w*\.mypinata\.cloud\/ipfs/, prefix);
if (uri.endsWith('.ipfs.dweb.link')) {
const id = uri.removeSuffix('.ipfs.dweb.link').removePrefix('https://');
uri = `${prefix}/${id}`;
}
return uri;
}
static getUrlHash(url: string) {
return crypto.SHA256(url.trim()).toString().slice(0, 8);
}
static getThumbnailUrlIdentifier(nftIdentifier: string, fileUrl: string) {
let collectionIdentifier = nftIdentifier.split('-').slice(0, 2).join('-');
if (TokenUtils.isSovereignIdentifier(nftIdentifier)) {
collectionIdentifier = nftIdentifier.split('-').slice(0, 3).join('-');
}
const urlHash = TokenHelpers.getUrlHash(fileUrl);
return `${collectionIdentifier}-${urlHash}`;
}
static needsDefaultMedia(nft: Nft): boolean {
if (nft.type === NftType.MetaESDT) {
return false;
}
if (nft.media && nft.media.length > 0) {
return false;
}
return true;
}
static setTokenRole(tokenRoles: TokenRoles, role: string) {
tokenRoles.roles.push(role);
switch (role) {
case 'ESDTRoleLocalMint':
tokenRoles.canLocalMint = true;
break;
case 'ESDTRoleLocalBurn':
tokenRoles.canLocalBurn = true;
break;
}
}
static setCollectionRole(tokenRoles: CollectionRoles, role: string) {
tokenRoles.roles.push(role);
switch (role) {
case 'ESDTRoleNFTCreate':
tokenRoles.canCreate = true;
break;
case 'ESDTRoleNFTBurn':
tokenRoles.canBurn = true;
break;
case 'ESDTRoleNFTAddQuantity':
tokenRoles.canAddQuantity = true;
break;
case 'ESDTRoleNFTAddURI':
tokenRoles.canAddUri = true;
break;
case 'ESDTRoleNFTUpdateAttributes':
tokenRoles.canUpdateAttributes = true;
break;
case 'ESDTTransferRole':
tokenRoles.canTransfer = true;
break;
}
}
static tokenNonce(tokenID: string): number {
const tokenNonceHex = tokenID.split('-')[2];
return parseInt(tokenNonceHex, 16);
}
static getCollectionIdentifier(nftIdentifier: string): string {
return nftIdentifier.split('-').slice(0, 2).join('-');
}
static getNftProof(hash: string): string | undefined {
if (!hash) {
return undefined;
}
const decodedHex = BinaryUtils.base64Decode(hash);
if (decodedHex.startsWith('proof:')) {
return decodedHex;
} else {
return hash;
}
}
}