Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support on-chain metadata as strong instead of erroring #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 22 additions & 14 deletions src/components/HeroSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,25 @@ export default function HeroSection(props) {
let [uriURL, uriProtocol] = await getURLFromURI(tokenURI);

let uriResponse;
try {
uriResponse = await fetch(uriURL, { method: "GET" });
} catch (e) {
console.error(e);
setFetchError(createMainError(nftAddress));
// setFetchError("Could not fetch NFT URI " + tokenURI);
setIsLoading(false);
return;
}
let imgURI;

let uriInfo = await uriResponse.json();
let imgURI = uriInfo.image;
if (uriProtocol !== "On-chain") {
try {
uriResponse = await fetch(uriURL, { method: "GET" });
} catch (e) {
console.error(e);
setFetchError(createMainError(nftAddress));
// setFetchError("Could not fetch NFT URI " + tokenURI);
setIsLoading(false);
return;
}

let uriInfo = await uriResponse.json();
imgURI = uriInfo.image;
} else {
// On-chain metadata can pass the image uri directly
imgURI = uriURL;
}

let [imageURIURL, protocol] = await getURLFromURI(imgURI);

Expand Down Expand Up @@ -311,12 +318,13 @@ export default function HeroSection(props) {

let severity = "undefined";
switch (uriProtocol) {
case "ipfs":
severity = "medium";
break;
case "On-chain":
case "arweave":
severity = "strong";
break;
case "ipfs":
severity = "medium";
break;
case "centralized":
severity = "poor";
break;
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const arweaveEndpoint = "https://arweave.net";

const CID = require("cids");
const arweave = Arweave.init();
const BASE64_JSON_HEADER = "data:application/json;base64,";

export const buildQuery = (ipfsHash) => `
query {
Expand Down Expand Up @@ -72,13 +73,24 @@ export const getURLFromURI = async (uri) => {
if (!uri) {
return ["", "undefined"];
}

if (uri.indexOf(BASE64_JSON_HEADER) === 0) {
debugger;
var base64Data = uri.replace(BASE64_JSON_HEADER, "");
var jsonData = JSON.parse(atob(base64Data));
var imageData = await getURLFromURI(jsonData.image);
imageData[1] = "On-chain";
return imageData;
}

// if correct URI we get the protocol
let url = new URL(uri);
// if protocol other IPFS -- get the ipfs hash
if (url.protocol === "ipfs:") {
// ipfs://ipfs/Qm

let ipfsHash = url.href.replace("ipfs://ipfs/", "");
ipfsHash = ipfsHash.replace("ipfs://", "");

return [ipfsGetEndpoint + ipfsHash, "ipfs"];
}
Expand Down