Skip to content

Commit

Permalink
Support invalid but popular IPFS URI format (#2271, #2527, #2590).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 24, 2022
1 parent 0578a88 commit 03545aa
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/providers/src.ts/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -471,16 +479,24 @@ 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") {
metadataUrl = metadataUrl.replace("{id}", tokenId.substring(2));
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; }
Expand Down

0 comments on commit 03545aa

Please sign in to comment.