npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lukasz.glen/ipfs-url-encoder

v1.0.0

Published

Save storage and generate ipfs urls from hashes in solidity smart contracts

Downloads

1

Readme

ipfs-url-encoder

Save storage and generate ipfs urls from hashes in solidity smart contracts.

Could not find any solidity base58btc encoder for ipfs urls, so I developed it by myself. Hope you enjoy it.

The repo contains base58btc encoder that transforms ipfs hashes into ipfs urls and two ERC721 metadata implementations.

Currently, this lib supports CID v0 only.

Add to your dependencies

npm install @lukasz.glen/ipfs-url-encoder

Running

npm install
npx hardhat compile
npx hardhat test

Content

  • contracts/IpfsUrlEncoderV0.sol - base58 encoder that transforms bytes32 hash into ipfs url v0,
  • contracts/v0/ERC721HashUrl.sol - implementation of tokenURI(),
  • contracts/v0/ERC721IdUrl.sol - implementation of tokenURI() that identifies tokenId with ipfs hash.

IpfsUrlEncoderV0 is gas efficient. It is not multipurpose, it only takes bytes32 input. CID v0 uses sha2-256, but it is obvious that the hash type is irrelevant here, only matters that it is 256 bits.

ERC721HashUrl and ERC721IdUrl implements only tokenURI(), not full ERC721Metadata. It is enough to inherit from one of these contracts. Exemplary ERC721 implementations are delivered at contracts/test/v0/ERC721HashUrlTest.sol and contracts/test/v0/ERC721IdUrlTest.sol.

Storage saving

When ipfs url is stored in a contract, it takes at least 3 slots. It is enough to store just ipfs hash, which takes 1 slot - and encode it with base58btc in tokenUIR() function on a call. The contract ERC721IdUrl goes even further. It uses tokenId as an ipfs hash and it takes 0 slots.

Note that CID v0 uses sha2-256, it fits in 32 bytes. CID v1 can use hashes with greater length - this would not be so gas saving.

Use cases

It is common to mint nfts in a batch and/or use common urls like this one

ipfs://QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPU/21

In such cases there is little profit using this lib.

It is useful when each token has a specific url, for instance when they are minted independently. Then the profit is 2 or 3 slots, it is 45k gas, on a single mint.

Background

This is how CID v0 url are produced.

  1. A user wants to publish some content.
  2. Content is hashed with sha2-256.
  3. The hash is prefixed with constants 0x12 and 0x20. 0x12 means that sha2-256 was used. 0x20 is the length of the output hash. This is so called multihash.
  4. Multihash is encoded with base58btc. The result is CID, for instance QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPU.

Note that having a hash, you can compute CID v0 uniquely, and conversely. So it is enough to store hashes instead of CIDs - CIDs take more bytes and cost more gas.

See this and this for reference. To retrieve a hash from CID you can use this.