ipfs-docs icon indicating copy to clipboard operation
ipfs-docs copied to clipboard

Explain common pitfalls surrounding CIDs and how to "update" them.

Open mishmosh opened this issue 4 years ago • 2 comments

The IPFS discord has been seeing multiple questions about updating CIDs and IPNS, and @Jorropo has written an outline addressing these Qs.

https://hackmd.io/SuF_fe82Qeamj2nSXmMWiw?both

Can we figure out the best place to incorporate this content? These 2 articles seem like the closest I can find:

  • https://docs.ipfs.io/concepts/immutability/ (expand last paragraph, and link to...)
  • https://docs.ipfs.io/concepts/ipns/ (maybe add "IPNS with Smart Contracts" as a new section there?)

mishmosh avatar Oct 11 '21 18:10 mishmosh

Thank you for submitting your first issue to this repository! A maintainer will be here shortly to triage and review. In the meantime, please double-check that you have provided all the necessary information to make this process easy! Any information that can help save additiona round trips is useful! We currently aim to give initial feedback within two business days. If this does not happen, feel free to leave a comment. Please keep an eye on how this issue will be labeled, as labels give an overview of priorities, assignments and additional actions requested by the maintainers:

  • "Priority" labels will show how urgent this is for the team.
  • "Status" labels will show if this is ready to be worked on, blocked, or in progress.
  • "Need" labels will indicate if additional input or analysis is required.

Finally, remember to use https://discuss.ipfs.io if you just need general support.

welcome[bot] avatar Oct 11 '21 18:10 welcome[bot]

Here's a copy of the above-linked post, just in case it goes missing/get's deleted:

# 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.
- [IPNS records](https://docs.ipfs.io/concepts/ipns/#interplanetary-name-system-ipns)
- [ENS](https://docs.ipfs.io/how-to/websites-on-ipfs/link-a-domain/#ethereum-naming-service-ens)
- [DNSLink](https://docs.ipfs.io/how-to/websites-on-ipfs/link-a-domain/#domain-name-service-dns)

## 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) :
```py
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`:
```py
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`.

### --- Link the video NFT example about how to do it in remix ! ---

johnnymatthews avatar Oct 12 '21 15:10 johnnymatthews