From 03545aa78b0e7bd177e22432e4842b0580a11d7d Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Mon, 24 Jan 2022 16:39:50 -0500 Subject: [PATCH] Support invalid but popular IPFS URI format (#2271, #2527, #2590). --- packages/providers/src.ts/base-provider.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/providers/src.ts/base-provider.ts b/packages/providers/src.ts/base-provider.ts index 299b2ab6cd..79bdad55b0 100644 --- a/packages/providers/src.ts/base-provider.ts +++ b/packages/providers/src.ts/base-provider.ts @@ -267,7 +267,15 @@ function _parseBytes(result: string): null | string { // Trim off the ipfs:// prefix and return the default gateway URL function getIpfsLink(link: string): string { - return `https:/\/gateway.ipfs.io/ipfs/${ link.substring(7) }`; + if (link.match(/^ipfs:\/\/ipfs\//i)) { + link = link.substring(12); + } else if (link.match(/^ipfs:\/\//i)) { + link = link.substring(7); + } else { + logger.throwArgumentError("unsupported IPFS format", "link", link); + } + + return `https:/\/gateway.ipfs.io/ipfs/${ link }`; } export class Resolver implements EnsResolver { @@ -471,9 +479,10 @@ export class Resolver implements EnsResolver { to: this.provider.formatter.address(comps[0]), data: hexConcat([ selector, tokenId ]) }; + let metadataUrl = _parseString(await this.provider.call(tx)) if (metadataUrl == null) { return null; } - linkage.push({ type: "metadata-url", content: metadataUrl }); + linkage.push({ type: "metadata-url-base", content: metadataUrl }); // ERC-1155 allows a generic {id} in the URL if (scheme === "erc1155") { @@ -481,6 +490,13 @@ export class Resolver implements EnsResolver { linkage.push({ type: "metadata-url-expanded", content: metadataUrl }); } + // Transform IPFS metadata links + if (metadataUrl.match(/^ipfs:/i)) { + metadataUrl = getIpfsLink(metadataUrl); + } + + linkage.push({ type: "metadata-url", content: metadataUrl }); + // Get the token metadata const metadata = await fetchJson(metadataUrl); if (!metadata) { return null; }