'How to extract image URL from ens avatar text record

When using ethers JavaScript library to retrieve ENS text records from the Ethereum Name Service. I got the NFT URI from the text record but not the image URL. Please is there any way/service to convert this metadata URI to an HTTPS image URL.

TO RECREATE:

TERMINAL : npm install --save ethers

Code:

var ethers = require('ethers');
var url = 'https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161';
var provider = new ethers.providers.JsonRpcProvider(url);


async function main() {
    const resolver = await provider.getResolver("brantly.eth");
    const avatar = await resolver.getAvatar();
    const avatarMetaData = await resolver.getText("avatar");
    console.log(`Avatar Metadata: ${avatarMetaData}`);
}

main();

.

Output: Avatar Metadata: eip155:1/erc721:0xb7F7F6C52F2e2fdb1963Eab30438024864c313F6/2430

The EIP155... Is not an imageURL. I'd prefer it came in the form of {https://...}. Is there any way to achieve this or convert the NFT URI EIP155... To an image or image URL.



Solution 1:[1]

As far as I know you will have to go through the entire process to get that IPFS URI.

You will want to take the contract address and token ID from that returned avatar string (the last 2 parts) and call the tokenURI ABI method, parse the JSON and read image there:

const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(address, abi, signer);

const uri = await contract.tokenURI(tokenId)
const data = (await axios.get(uri)).data;
const image = data.image;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kamin0_0