| | 1 | | namespace DCLServices.WearablesCatalogService |
| | 2 | | { |
| | 3 | | public static class ExtendedUrnParser |
| | 4 | | { |
| | 5 | | private const int REGULAR_NFTS_SHORT_PARTS = 6; |
| | 6 | | private const int THIRD_PARTY_V2_SHORTEN_URN_PARTS = 7; |
| | 7 | | private const string COLLECTIONS_THIRD_PARTY = "collections-thirdparty"; |
| | 8 | |
|
| | 9 | | public static string GetShortenedUrn(string urnReceived) |
| | 10 | | { |
| 114 | 11 | | if (string.IsNullOrEmpty(urnReceived)) return urnReceived; |
| 228 | 12 | | if (CountParts(urnReceived) <= REGULAR_NFTS_SHORT_PARTS) return urnReceived; |
| | 13 | |
|
| | 14 | | int index; |
| | 15 | |
|
| 0 | 16 | | if (IsThirdPartyCollection(urnReceived)) |
| | 17 | | { |
| 0 | 18 | | index = -1; |
| | 19 | |
|
| | 20 | | // Third party v2 contains 10 parts, on which 3 are reserved for the tokenId |
| | 21 | | // "id": urn:decentraland:amoy:collections-thirdparty:back-to-the-future:amoy-eb54:tuxedo-6751:amoy:0x1d |
| | 22 | | // "tokenId": amoy:0x1d9fb685c257e74f869ba302e260c0b68f5ebb37:12 |
| 0 | 23 | | for (var i = 0; i < THIRD_PARTY_V2_SHORTEN_URN_PARTS; i++) |
| | 24 | | { |
| 0 | 25 | | index = urnReceived.IndexOf(':', index + 1); |
| 0 | 26 | | if (index == -1) break; |
| | 27 | | } |
| | 28 | |
|
| 0 | 29 | | return index != -1 ? urnReceived[..index] : urnReceived; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | // TokenId is always placed in the last part for regular nfts |
| 0 | 33 | | index = urnReceived.LastIndexOf(':'); |
| | 34 | |
|
| 0 | 35 | | return index != -1 ? urnReceived[..index] : urnReceived; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | private static int CountParts(string urn) |
| | 39 | | { |
| 114 | 40 | | int count = 1; |
| 114 | 41 | | int index = urn.IndexOf(':'); |
| | 42 | |
|
| 342 | 43 | | while (index != -1) |
| | 44 | | { |
| 228 | 45 | | count++; |
| 228 | 46 | | index = urn.IndexOf(':', index + 1); |
| | 47 | | } |
| | 48 | |
|
| 114 | 49 | | return count; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private static bool IsThirdPartyCollection(string urn) => |
| 0 | 53 | | !string.IsNullOrEmpty(urn) && urn.Contains(COLLECTIONS_THIRD_PARTY); |
| | 54 | | } |
| | 55 | | } |