Add support for IPFS metadata imageUrl in getAvatar (#2426).
This commit is contained in:
parent
2f57c6a4ab
commit
3d6a7ec020
@ -242,10 +242,11 @@ export interface Avatar {
|
|||||||
linkage: Array<{ type: string, content: string }>;
|
linkage: Array<{ type: string, content: string }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const matcherIpfs = new RegExp("^(ipfs):/\/(.*)$", "i");
|
||||||
const matchers = [
|
const matchers = [
|
||||||
new RegExp("^(https):/\/(.*)$", "i"),
|
new RegExp("^(https):/\/(.*)$", "i"),
|
||||||
new RegExp("^(data):(.*)$", "i"),
|
new RegExp("^(data):(.*)$", "i"),
|
||||||
new RegExp("^(ipfs):/\/(.*)$", "i"),
|
matcherIpfs,
|
||||||
new RegExp("^eip155:[0-9]+/(erc[0-9]+):(.*)$", "i"),
|
new RegExp("^eip155:[0-9]+/(erc[0-9]+):(.*)$", "i"),
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -264,6 +265,10 @@ function _parseBytes(result: string): null | string {
|
|||||||
return hexDataSlice(result, offset + 32, offset + 32 + length);
|
return hexDataSlice(result, offset + 32, offset + 32 + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) }`;
|
||||||
|
}
|
||||||
|
|
||||||
export class Resolver implements EnsResolver {
|
export class Resolver implements EnsResolver {
|
||||||
readonly provider: BaseProvider;
|
readonly provider: BaseProvider;
|
||||||
@ -402,7 +407,7 @@ export class Resolver implements EnsResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getAvatar(): Promise<null | Avatar> {
|
async getAvatar(): Promise<null | Avatar> {
|
||||||
const linkage: Array<{ type: string, content: string }> = [ ];
|
const linkage: Array<{ type: string, content: string }> = [ { type: "name", content: this.name } ];
|
||||||
try {
|
try {
|
||||||
// test data for ricmoo.eth
|
// test data for ricmoo.eth
|
||||||
//const avatar = "eip155:1/erc721:0x265385c7f4132228A0d54EB1A9e7460b91c0cC68/29233";
|
//const avatar = "eip155:1/erc721:0x265385c7f4132228A0d54EB1A9e7460b91c0cC68/29233";
|
||||||
@ -411,8 +416,8 @@ export class Resolver implements EnsResolver {
|
|||||||
|
|
||||||
for (let i = 0; i < matchers.length; i++) {
|
for (let i = 0; i < matchers.length; i++) {
|
||||||
const match = avatar.match(matchers[i]);
|
const match = avatar.match(matchers[i]);
|
||||||
|
|
||||||
if (match == null) { continue; }
|
if (match == null) { continue; }
|
||||||
|
|
||||||
switch (match[1]) {
|
switch (match[1]) {
|
||||||
case "https":
|
case "https":
|
||||||
linkage.push({ type: "url", content: avatar });
|
linkage.push({ type: "url", content: avatar });
|
||||||
@ -424,7 +429,7 @@ export class Resolver implements EnsResolver {
|
|||||||
|
|
||||||
case "ipfs":
|
case "ipfs":
|
||||||
linkage.push({ type: "ipfs", content: avatar });
|
linkage.push({ type: "ipfs", content: avatar });
|
||||||
return { linkage, url: `https:/\/gateway.ipfs.io/ipfs/${ avatar.substring(7) }` }
|
return { linkage, url: getIpfsLink(avatar) };
|
||||||
|
|
||||||
case "erc721":
|
case "erc721":
|
||||||
case "erc1155": {
|
case "erc1155": {
|
||||||
@ -471,19 +476,32 @@ export class Resolver implements EnsResolver {
|
|||||||
// ERC-1155 allows a generic {id} in the URL
|
// ERC-1155 allows a generic {id} in the URL
|
||||||
if (match[1] === "erc1155") {
|
if (match[1] === "erc1155") {
|
||||||
metadataUrl = metadataUrl.replace("{id}", tokenId.substring(2));
|
metadataUrl = metadataUrl.replace("{id}", tokenId.substring(2));
|
||||||
|
linkage.push({ type: "metadata-url-expanded", content: metadataUrl });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the token metadata
|
// Get the token metadata
|
||||||
const metadata = await fetchJson(metadataUrl);
|
const metadata = await fetchJson(metadataUrl);
|
||||||
|
if (!metadata) { return null; }
|
||||||
|
linkage.push({ type: "metadata", content: JSON.stringify(metadata) });
|
||||||
|
|
||||||
// Pull the image URL out
|
// Pull the image URL out
|
||||||
if (!metadata || typeof(metadata.image) !== "string" || !metadata.image.match(/^(https:\/\/|data:)/i)) {
|
let imageUrl = metadata.image;
|
||||||
return null;
|
if (typeof(imageUrl) !== "string") { return null; }
|
||||||
}
|
|
||||||
linkage.push({ type: "metadata", content: JSON.stringify(metadata) });
|
|
||||||
linkage.push({ type: "url", content: metadata.image });
|
|
||||||
|
|
||||||
return { linkage, url: metadata.image };
|
if (imageUrl.match(/^(https:\/\/|data:)/i)) {
|
||||||
|
// Allow
|
||||||
|
} else {
|
||||||
|
// Transform IPFS link to gateway
|
||||||
|
const ipfs = imageUrl.match(matcherIpfs);
|
||||||
|
if (ipfs == null) { return null; }
|
||||||
|
|
||||||
|
linkage.push({ type: "url-ipfs", content: imageUrl });
|
||||||
|
imageUrl = getIpfsLink(imageUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
linkage.push({ type: "url", content: imageUrl });
|
||||||
|
|
||||||
|
return { linkage, url: imageUrl };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user