IPFS & Decentralised Storage — Web3

IPFS is a peer-to-peer distributed file system with content addressing: CID = b + base32(version 01, codec, a SHA-256 multihash of the content). kubo 0.43.1…

How do you ask for a book when you don't know who has it? You don't say "the book on shelf 3 at Raka's house", you say "the book whose fingerprint is X". Anyone who has a copy may hand it over, and you can recompute the fingerprint to make sure the book wasn't swapped. But if nobody keeps a copy, the book is gone. IPFS (InterPlanetary File System) works like that: a peer-to-peer distributed file system that uses content addressing. A file is looked up by the fingerprint of its content, not by the server it sits on.

Location addresses vs content addresses

In the ERC-721 lesson, tokenURI(1) was https://raka-shirt-shop.test/coffee/1: a location address. That server can go down, or its content can be swapped quietly, and the NFT's owner can't tell. On IPFS the address is a CID (Content Identifier), a hash of the file's content. Same content, same CID, on any computer. Change one byte and the CID changes completely.

The CID: a fingerprint of the content

The shirt picture for the COFFEE certificates is a 236-byte SVG file, coffee-shirt.svg. Raka computes its CID with kubo 0.43.1 (the Go implementation of IPFS) offline (--offline: not connected to the network, nothing gets uploaded):

$ ipfs add --offline --cid-version 1 coffee-shirt.svg
added bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a coffee-shirt.svg
$ ipfs add --offline coffee-shirt.svg
added QmcCysJt3W5GcCKAMAfVevMEvT18CxeQRRgtMZ1Z7uq17H coffee-shirt.svg

The first is a CIDv1, the second a CIDv0 (still kubo's default without --cid-version 1). Same content, different text, so pick one version and stick to it. The CIDv1 above takes apart like this:

Part Value Meaning
multibase b the rest is written in lowercase base32
version 0x01 CIDv1
codec 0x55 raw: the file's bytes as they are
multihash 0x12 0x20 + 32 bytes SHA-256, 32 bytes long: 9689152d...83d6f9e8

So CID = b + base32(01 55 12 20 + sha256(file content)). The multiformats library computes the same thing: CID.create(1, raw.code, await sha256.digest(bytes)). The result matches kubo exactly.

COFFEE's metadata on IPFS

Metadata #1 now points to its picture with ipfs://, not a server URL. The file coffee/1 (393 bytes) holds:

{
  "name": "Coffee Edition Shirt #1",
  "description": "Numbered certificate, limited Coffee Print Shirt, Raka's Shirt Shop. 1 of 100.",
  "image": "ipfs://bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a",
  "attributes": [
    {
      "trait_type": "Size",
      "value": "L"
    },
    {
      "trait_type": "Number",
      "value": 1,
      "display_type": "number"
    }
  ]
}

Raka puts metadata #1 and #2 in one folder, coffee/, then adds the folder:

$ ipfs add --offline --cid-version 1 -r coffee
added bafkreifjshuco474wgqkcmh3yrwdwvvuv7iruchlwhgskxa7xvigg7gi5u coffee/1
added bafkreihs4neiqj6b7b55dk35ufdvcyp3faxtdgqs5wo2nhsfw4g2fkby24 coffee/2
added bafybeicske33e77e3uujpvrkwi6ynpy3dpo7hbkaelixuxkaxfvebus37q coffee

A folder has a CID too (bafybei..., codec dag-pb), computed from the names and CIDs of the files inside. CoffeeEditionShirt only needs one line changed:

function _baseURI() internal pure override returns (string memory) {
    return "ipfs://bafybeicske33e77e3uujpvrkwi6ynpy3dpo7hbkaelixuxkaxfvebus37q/";
}

On the local Hardhat chain, tokenURI(1) returns ipfs://bafybeicske33e77e3uujpvrkwi6ynpy3dpo7hbkaelixuxkaxfvebus37q/1. Why not keep the JSON in the contract? Storing those 393 bytes in a mapping(uint256 => string) costs 339,798 gas per token, because every 32 bytes of storage is kept by every node. With _baseURI the extra cost per token is zero, and the CID still guarantees the content can't be swapped.

Changing the content means a new CID

Change "L" to "XL" and the CID of coffee/1 changes completely, and so does the folder's CID, because a folder stores its children's CIDs. CoffeeEditionShirt's _baseURI is fixed text in the bytecode, so tokenURI(1) keeps pointing at the old folder, size L. And that is the guarantee: a buyer knows the metadata they bought can't be changed quietly. If Raka wants to be able to change it, he has to write a setBaseURI function (onlyOwner), and that guarantee is gone.

Pinning: without a pin, a file can disappear

An IPFS node only has to keep the files it has pinned. Everything else may be thrown away by garbage collection. Raka tries it on his local node:

$ ipfs pin rm bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a
unpinned bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a
$ ipfs repo gc
removed bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a
...
$ ipfs cat --offline bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a
Error: block was not found locally (offline): ipld: could not find bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a

The coffee folder is still pinned and ipfs cat .../1 still works, but the picture is gone. Pinning a folder only holds the files inside it; a picture named through the "image" text doesn't come along. So pin the picture and the metadata, in more than one place: your own node and a pinning service.

Gateways and ipfs://

An ordinary browser can't open ipfs:// yet. A gateway translates it to HTTP: https://ipfs.io/ipfs/<CID>/1 or https://<CID>.ipfs.dweb.link/1. Wallets and marketplaces do that automatically. What goes in the contract is still ipfs://<CID>, not a gateway URL, so the NFT isn't tied to one company. A public gateway has to be trusted, too; clients such as @helia/verified-fetch check the hash themselves.

What "decentralised" does guarantee

Arweave is an alternative with another model: its own blockchain network where you pay once, up front, for long-term storage funded by the network's endowment. The data is public and can't be deleted, and its address is a transaction ID (ar://...). It suits metadata that really must be permanent, but you pay per byte in advance and depend on that network's economics.

⚠️ Why is the NFT's image blank?

TL;DR: IPFS is a peer-to-peer distributed file system with content addressing: CID = b + base32(version 01, codec, a SHA-256 multihash of the content). kubo 0.43.1 still gives a CIDv0 (Qm...) unless you pass --cid-version 1 (bafkrei... for a file, bafybei... for a folder). COFFEE's metadata sits in the folder coffee/ with CID bafybeicske3..., the picture at ipfs://bafkreiewre..., and _baseURI returns ipfs://<folder CID>/, so tokenURI(1) = ipfs://.../1. Storing the 393-byte JSON in the contract takes 339,798 gas per token; IPFS is cheap and the CID guarantees the content isn't swapped, but only while someone pins it. Changing the content means a new CID. Write ipfs:// in the contract and use a gateway only for display. Arweave trades an up-front cost for long-term storage.

⚡ Looked up by content, not location

Meaning: IPFS is a peer-to-peer distributed file system that uses content addressing. A file is looked up by the fingerprint of its content (its CID), not by the server it sits on.

⚡ The CID: a hash of the file's content

Write:

$ ipfs add --offline --cid-version 1 coffee-shirt.svg
added bafkreiewreks3ehlxj4pymdqga5w2fjy2wtpwsrerjr5v4dnyfjyhvxz5a coffee-shirt.svg

Meaning: CID = b + base32(01 55 12 20 + sha256(file content)). Same content, same CID, on any computer. Change one byte and the CID changes completely.

⚡ _baseURI points at an IPFS folder

Write:

function _baseURI() internal pure override returns (string memory) {
    return "ipfs://bafybeicske33e77e3uujpvrkwi6ynpy3dpo7hbkaelixuxkaxfvebus37q/";
}

Meaning: tokenURI(1) becomes ipfs://<folder CID>/1. Its content can't be swapped quietly, because new content means a new CID.

⚡ Without a pin, a file can disappear

Guess first: the coffee folder is still pinned, but the picture's pin is removed and ipfs repo gc runs. Is the picture still there?

It's gone. Pinning a folder only holds the files inside it. A picture named through the "image" text doesn't come along.

Meaning: Pin the picture and the metadata, in more than one place: your own node and a pinning service.

What you will learn