Skip to content

Commit

Permalink
Handle parsing of XLS-24 standard
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibautBremand committed Aug 11, 2023
1 parent 1d5db00 commit 445cece
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/constants/src/xrpl/nft.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface AccountNFTokenResponse {

export interface NFTData {
NFTokenID: string;
NFType?: string;
nftType?: string;
schema?: string;
name?: string;
description?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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' });
});

Expand All @@ -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'
Expand All @@ -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' });
});

Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { parseJSON } from './NFTViewer';

import { isImageUrl } from '.';

export const resolveNFTImage = async (NFT: AccountNFToken): Promise<NFTData> => {
export const resolveNFTData = async (NFT: AccountNFToken): Promise<NFTData> => {
const { NFTokenID, URI } = NFT;
let URL = URI ? await convertHexToString(URI) : '';

Expand Down Expand Up @@ -47,12 +47,14 @@ export const resolveNFTImage = async (NFT: AccountNFToken): Promise<NFTData> =>
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) {}
}
Expand Down

0 comments on commit 445cece

Please sign in to comment.