From 445cece68355898099cf7ecf3e0d8dcb95869e1e Mon Sep 17 00:00:00 2001 From: thibautbremand Date: Fri, 11 Aug 2023 22:16:05 +0200 Subject: [PATCH] Handle parsing of XLS-24 standard --- packages/constants/src/xrpl/nft.types.ts | 2 +- .../molecules/NFTCard/NFTCard.test.tsx | 2 +- .../src/contexts/LedgerContext/LedgerContext.tsx | 4 ++-- ...eResolver.test.ts => NFTDataResolver.test.ts} | 16 ++++++++-------- .../{NFTImageResolver.ts => NFTDataResolver.ts} | 4 +++- 5 files changed, 15 insertions(+), 13 deletions(-) rename packages/extension/src/utils/{NFTImageResolver.test.ts => NFTDataResolver.test.ts} (86%) rename packages/extension/src/utils/{NFTImageResolver.ts => NFTDataResolver.ts} (87%) diff --git a/packages/constants/src/xrpl/nft.types.ts b/packages/constants/src/xrpl/nft.types.ts index ac4591936..502c8b83f 100644 --- a/packages/constants/src/xrpl/nft.types.ts +++ b/packages/constants/src/xrpl/nft.types.ts @@ -14,7 +14,7 @@ export interface AccountNFTokenResponse { export interface NFTData { NFTokenID: string; - NFType?: string; + nftType?: string; schema?: string; name?: string; description?: string; diff --git a/packages/extension/src/components/molecules/NFTCard/NFTCard.test.tsx b/packages/extension/src/components/molecules/NFTCard/NFTCard.test.tsx index 12fed7635..8deba0c75 100644 --- a/packages/extension/src/components/molecules/NFTCard/NFTCard.test.tsx +++ b/packages/extension/src/components/molecules/NFTCard/NFTCard.test.tsx @@ -18,7 +18,7 @@ const mockNFT = { const mockNFTData = { NFTokenID: 'fake', schema: 'ipfs://QmNpi8rcXEkohca8iXu7zysKKSJYqCvBJn3xJwga8jXqWU', - NFType: 'art.v0', + nftType: 'art.v0', name: "Ekiserrepe's Oniric Lo-Fi Rooms Vol.1 NFT #1", description: "Room #1 of Ekiserrepe's Oniric Lo-Fi Rooms Vol.1", image: 'ipfs://bafybeie6pmuddco552t4u7oc7anryqohuj6vl42ngct6ve3q4bjet5piam/1.png', diff --git a/packages/extension/src/contexts/LedgerContext/LedgerContext.tsx b/packages/extension/src/contexts/LedgerContext/LedgerContext.tsx index 1187b6c04..58cb50397 100644 --- a/packages/extension/src/contexts/LedgerContext/LedgerContext.tsx +++ b/packages/extension/src/contexts/LedgerContext/LedgerContext.tsx @@ -43,7 +43,7 @@ import { import { AccountTransaction, WalletLedger } from '../../types'; import { toXRPLMemos, toXRPLSigners } from '../../utils'; -import { resolveNFTImage } from '../../utils/NFTImageResolver'; +import { resolveNFTData } from '../../utils/NFTDataResolver'; import { useNetwork } from '../NetworkContext'; import { useWallet } from '../WalletContext'; @@ -799,7 +799,7 @@ const LedgerProvider: FC = ({ children }) => { const getNFTData = useCallback(async ({ NFT }: NFTImageRequest) => { try { - return resolveNFTImage(NFT); + return resolveNFTData(NFT); } catch (e) { Sentry.captureException(e); throw e; diff --git a/packages/extension/src/utils/NFTImageResolver.test.ts b/packages/extension/src/utils/NFTDataResolver.test.ts similarity index 86% rename from packages/extension/src/utils/NFTImageResolver.test.ts rename to packages/extension/src/utils/NFTDataResolver.test.ts index dd9c6c965..4263b6ae9 100644 --- a/packages/extension/src/utils/NFTImageResolver.test.ts +++ b/packages/extension/src/utils/NFTDataResolver.test.ts @@ -2,7 +2,7 @@ import { convertHexToString } from 'xrpl'; import { AccountNFToken } from '@gemwallet/constants'; -import { resolveNFTImage } from './NFTImageResolver'; +import { resolveNFTData } from './NFTDataResolver'; import { parseJSON } from './NFTViewer'; jest.mock('xrpl'); @@ -24,7 +24,7 @@ describe('resolveNFTImage', () => { NFTokenID: '1234', URI: '' }; - const result = await resolveNFTImage(NFT as AccountNFToken); + const result = await resolveNFTData(NFT as AccountNFToken); expect(result).toEqual({ NFTokenID: '1234', description: 'No data' @@ -39,7 +39,7 @@ describe('resolveNFTImage', () => { NFTokenID: '1234', URI: 'mockHex' }; - const result = await resolveNFTImage(NFT as AccountNFToken); + const result = await resolveNFTData(NFT as AccountNFToken); expect(result).toEqual({ NFTokenID: '1234', image: 'https://test.com/image.png' @@ -54,7 +54,7 @@ describe('resolveNFTImage', () => { NFTokenID: '1234', URI: 'mockHex' }; - const result = await resolveNFTImage(NFT as AccountNFToken); + const result = await resolveNFTData(NFT as AccountNFToken); expect(result).toEqual({ NFTokenID: '1234', image: 'https://test.com/image.jpg' @@ -71,7 +71,7 @@ describe('resolveNFTImage', () => { NFTokenID: '1234', URI: 'mockHex' }; - const result = await resolveNFTImage(NFT as AccountNFToken); + const result = await resolveNFTData(NFT as AccountNFToken); expect(result).toEqual({ name: 'test' }); }); @@ -83,7 +83,7 @@ describe('resolveNFTImage', () => { NFTokenID: '1234', URI: 'mockHex' }; - const result = await resolveNFTImage(NFT as AccountNFToken); + const result = await resolveNFTData(NFT as AccountNFToken); expect(result).toEqual({ NFTokenID: '1234', description: 'ipfs://testData' @@ -100,7 +100,7 @@ describe('resolveNFTImage', () => { NFTokenID: '1234', URI: 'mockHex' }; - const result = await resolveNFTImage(NFT as AccountNFToken); + const result = await resolveNFTData(NFT as AccountNFToken); expect(result).toEqual({ name: 'test' }); }); @@ -112,7 +112,7 @@ describe('resolveNFTImage', () => { NFTokenID: '1234', URI: 'mockHex' }; - const result = await resolveNFTImage(NFT as AccountNFToken); + const result = await resolveNFTData(NFT as AccountNFToken); expect(result).toEqual({ NFTokenID: '1234', description: 'ipfs://testData' diff --git a/packages/extension/src/utils/NFTImageResolver.ts b/packages/extension/src/utils/NFTDataResolver.ts similarity index 87% rename from packages/extension/src/utils/NFTImageResolver.ts rename to packages/extension/src/utils/NFTDataResolver.ts index ce6b9559a..a0ed7b094 100644 --- a/packages/extension/src/utils/NFTImageResolver.ts +++ b/packages/extension/src/utils/NFTDataResolver.ts @@ -7,7 +7,7 @@ import { parseJSON } from './NFTViewer'; import { isImageUrl } from '.'; -export const resolveNFTImage = async (NFT: AccountNFToken): Promise => { +export const resolveNFTData = async (NFT: AccountNFToken): Promise => { const { NFTokenID, URI } = NFT; let URL = URI ? await convertHexToString(URI) : ''; @@ -47,12 +47,14 @@ export const resolveNFTImage = async (NFT: AccountNFToken): Promise => try { await fetch(URL); // Case 2.1 - The URL is directly a JSON + // If it follows the XLS-24 standard, it will be automatically parsed return parseJSON(URL, NFTokenID); } catch (e) {} // Case 2.2 - The URL is an IPFS hash if (!URL.startsWith(IPFSResolverPrefix) && !URL.startsWith('http')) { try { await fetch(`${IPFSResolverPrefix}${URL}`); + // If it follows the XLS-24 standard, it will be automatically parsed return parseJSON(`${IPFSResolverPrefix}${URL}`, NFTokenID); } catch (e) {} }