Try โ€‚โ€‰HackMD

Why you can't update a CID and how to do it.

A CID (Content IDentifier) is the hash of the content of the files so if you change the content the CID must change.

But how to do it anyway

The goal should to not share a CID but share something else that points to a CID that is updatable.

IPNS

IPNS (InterPlanetary Naming System) is a stack that allows to create things that points to a CID, so you instead of updating the CID you update the IPNS address,
IPNS supports multiple resolving technology.

Using CIDs with Smart Contracts

If your application is already receiving the CID from a smart contract (like an NFT for example).

You can just use smart contract logic to update the CID you give to peoples.

For example many NFTs has a function looking like this (python pseudocode) :

def tokenURI(this, tokenID: uint256) -> string:
    return this.baseURI + str(tokenID) + ".json"

So this function just grabs baseURI from the storage of the contract and appends the token id and .json extension, which is a metadata file for the NFT.

This will return something like this : ipfs://Qmfoo/1234.json for the NFT ID 1234.

So you can just add a setter function that updates this.baseURI:

def setBaseURI(this, newBaseURI: string):
    require(msg.sender == this.owner)
    this.baseURI = newBaseURI

So basically we just updated what we use to construct our tokenURI for our NFT returning a new CID.

So if I do contract.setBaseURI("ipfs://Qmbar/"), tokenURI will now return ipfs://Qmbar/1234.json instead of Qmfoo.