Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Within each top-level data structure, handle:

- [ ] IPFS

- Image stored on that IPFS hash
- Image stored on anohter IPFS
- Image stored on rando server
- [ ] Image stored on that IPFS hash
- [x] Image stored on anohter IPFS
- [ ] Image stored on rando server

- [ ] Http

Expand Down
9 changes: 8 additions & 1 deletion src/constants/reasons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ const tokenUriIsHttp: Reason = {
const imageUriIsHttp: Reason = {
id: "image-uri-is-http",
severity: Severity.Critical,
message: "imageURI is hosted on a private server over HTTP",
message: "ImageURI is hosted on a private server over HTTP",
};

const imageUriIsIpfs: Reason = {
id: "image-uri-is-ipfs",
severity: Severity.Good,
message: "ImageURI is hosted on IPFS",
};

export {
Expand All @@ -36,4 +42,5 @@ export {
tokenUriIsIpfs,
tokenUriIsHttp,
imageUriIsHttp,
imageUriIsIpfs,
};
27 changes: 19 additions & 8 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import {
metadataOnChain,
tokenUriIsHttp,
tokenUriIsIpfs,
imageUriIsIpfs,
} from "./constants/reasons";

import { analyzeTokenUri, isTokenUriBase64Json, isTokenUriHttp } from "./index";

import {
ALL_ON_CHAIN,
HTTP_WITH_HTTP_RESPONSE,
IMAGE_IS_IPFS_RESPONSE,
} from "../tests/fixtures/sample_token_uris";

import axios from "axios";
jest.mock("axios");

afterEach(() => {
jest.clearAllMocks();
});

describe("#isTokenUriBase64Json", () => {
test("valid base64", async () => {
expect(
Expand Down Expand Up @@ -62,24 +68,29 @@ describe("All on chain", () => {
});

describe("IPFS tokenURI", () => {
test("IPFS url as the top level", async () => {
let result = await analyzeTokenUri("ipfs://bafybeic26wp7ck2/1234");
test("with IPFS URL for the image", async () => {
// @ts-ignore
axios.get.mockResolvedValueOnce(IMAGE_IS_IPFS_RESPONSE);

let result = await analyzeTokenUri("ipfs://blabhalsdkj/1234");

expect(axios.get).toHaveBeenCalledWith("ipfs://blabhalsdkj/1234");
expect(result.grade).toBe(GradeLetter.B);
expect(result.reasons.length).toEqual(1);
expect(result.reasons.length).toEqual(2);

const reason = result.reasons.find(
const reason1 = result.reasons.find(
(reason) => reason.id === tokenUriIsIpfs.id
);
const reason2 = result.reasons.find(
(reason) => reason.id === imageUriIsIpfs.id
);

expect(reason).toMatchObject(tokenUriIsIpfs);
expect(reason1).toMatchObject(tokenUriIsIpfs);
expect(reason2).toMatchObject(imageUriIsIpfs);
});
});

describe("HTTP link for tokenURI", () => {
afterEach(() => {
jest.clearAllMocks();
});
test("Random URL at top level results in poor grad", async () => {
// @ts-ignore
axios.get.mockResolvedValueOnce(HTTP_WITH_HTTP_RESPONSE);
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export const handleBase64Json = (tokenUri: string): Reason[] => {
return reasons;
};

export const handleIpfs = async (tokenUri: string): Promise<Reason[]> => {
let reasons: Reason[] = [Reasons.tokenUriIsIpfs];

let res: Metadata = await axios.get(tokenUri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add an issue to test fail case here


if (res.image && isTokenUriIpfs(res.image)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is there is no res.image? Is that an error or a /shrug?

This would be nice to have a non-counted issue or warning

reasons = [...reasons, Reasons.imageUriIsIpfs];
}

return reasons;
};

export const handleHttp = async (tokenUri: string): Promise<Reason[]> => {
let reasons: Reason[] = [Reasons.tokenUriIsHttp];

Expand All @@ -50,7 +62,7 @@ const analyzeTokenUri = async (tokenUri: string): Promise<Grade> => {
if (isTokenUriBase64Json(tokenUri)) {
reasons = handleBase64Json(tokenUri);
} else if (isTokenUriIpfs(tokenUri)) {
reasons = [Reasons.tokenUriIsIpfs];
reasons = await handleIpfs(tokenUri);
} else if (isTokenUriHttp(tokenUri)) {
reasons = await handleHttp(tokenUri);
} else {
Expand Down
10 changes: 9 additions & 1 deletion tests/fixtures/sample_token_uris.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ const HTTP_WITH_HTTP_RESPONSE = {
attributes: [{ trait_type: "Lazy", value: "true" }],
};

export { ALL_ON_CHAIN, HTTP_WITH_HTTP_RESPONSE };
const IMAGE_IS_IPFS_RESPONSE = {
name: "#1234",
description: "Not Lazy Noodles",
external_url: "#",
image: "ipfs://bloopbllooopbloop/1234",
attributes: [{ trait_type: "Not Lazy", value: "true" }],
};

export { ALL_ON_CHAIN, HTTP_WITH_HTTP_RESPONSE, IMAGE_IS_IPFS_RESPONSE };