IPFS Content IDs

The Irys platform enables data addressing using IPFS Content IDs (CIDs) as an alternative to transaction IDs.

Note
Note
Irys does not currently verify that CIDs match their uploaded data. This will be added in a future release.

Content IDs vs Transaction IDs

IPFS and Irys employ distinct identification methodologies:

  • IPFS CIDs
    Generated through content hashing; identical data produces identical CIDs across uploads.
  • Irys Transaction IDs
    Created by hashing content plus metadata; each upload receives a unique transaction ID.

Uploading with a CID

To upload data using a CID, embed it as the IPFS-CID tag value during upload.

Note
Note
Use a token-specific getIrysUploader() version to connect to an Irys Bundler before uploading.
import IPFS from "ipfs-only-hash";

const generateCID = async (content) => {
  return await IPFS.of(content);
};

const uploadToIrysWithCID = async () => {
  const irys = await getIrysUploader();

  const dataToUpload = "Irys + IPFS Content ID";
  const contentID = await generateCID(dataToUpload);
  console.log(`ContentID=${contentID}`);

  const tags = [
    { name: "Content-Type", value: "text/html" },
    { name: "IPFS-CID", value: contentID },
  ];
  const receipt = await irys.upload(dataToUpload, { tags: tags });

  console.log(`Transaction ID URL https://gateway.irys.xyz/${receipt.id}`);
  console.log(`Content ID URL https://gateway.irys.xyz/ipfs/${contentID}`);
};
Info
Info
The ipfs-only-hash package uses SHA-256 by default. IPFS also supports SHA3 and Blake2 for CID generation, all compatible with Irys uploads.

Downloading with a CID

Retrieve CID-tagged data from the Irys gateway using the URL format: https://gateway.irys.xyz/ipfs/:contentID

Note
Note
When multiple transactions share the same CID, the gateway returns the earliest-timestamped version.
const fetchData = async (ipfsCID) => {
  const url = `https://gateway.irys.xyz/ipfs/${ipfsCID}`;
  console.log(`URL: ${url}`);

  const response = await fetch(url);
  const data = await response.text();
  console.log(`DATA: ${data}`);
};

Migrating Data from IPFS to Irys

Users can transfer data from IPFS to Irys by downloading from IPFS gateways and re-uploading to Irys. Original CIDs can be preserved as tags for continued CID-based retrieval, or users may switch to Irys transaction IDs.

Process:

  1. 1
    Download data from an IPFS gateway.
  2. 2
    Determine the data's content type (e.g., image/png).
  3. 3
    Re-upload to Irys while tagging with existing content type.
Note
Note
Irys nodes must be pre-funded. Most users choose up-front funding for full migration coverage, though lazy-funding per upload is also available.
import fetch from "node-fetch";
import { fileTypeFromBuffer } from "file-type";

const uploadToIrysWithCID = async (dataToUpload, contentType, contentID) => {
  const irys = await getIrysUploader();

  const tags = [
    { name: "Content-Type", value: contentType },
    { name: "IPFS-CID", value: contentID },
  ];
  const receipt = await irys.upload(dataToUpload, { tags: tags });
  console.log(`Direct URL: https://gateway.irys.xyz/${receipt.id}`);
  console.log(`Content ID URL: https://gateway.irys.xyz/ipfs/${contentID}`);
};

const downloadAndDetermineContentType = async (ipfsCID) => {
  try {
    const ipfsURL = `https://ipfs.io/ipfs/${ipfsCID}`;
    const response = await fetch(ipfsURL);
    const arrayBuffer = await response.arrayBuffer();
    const buffer = Buffer.from(arrayBuffer);

    const contentType = await fileTypeFromBuffer(buffer);

    if (contentType) {
      console.log(`Content Type: ${contentType.mime}`);
      await uploadToIrysWithCID(buffer, contentType.mime, ipfsCID);
    } else {
      console.error("Unable to determine content type");
    }
  } catch (error) {
    console.error("Error:", error);
  }
};

const ipfsCID = "QmUgL4YbnW9vMWZXLdAFzgxJwxpxJapZRLpjoT2ubU5WmF";
downloadAndDetermineContentType(ipfsCID);