icon

A Comprehensive Guide to Publishing and Updating Content with Lighthouse IPNS

Introduction:

Lighthouse IPNS (InterPlanetary Naming System) is a valuable tool that enables the creation of mutable pointers to content-addressed data in the IPFS (InterPlanetary File System) network. While IPFS ensures content immutability by generating unique CIDs for each piece of data, IPNS allows for regular updates to the content while retaining a consistent address. In this tutorial, we will explore two methods to publish and update content with Lighthouse IPNS: using the CLI (Command Line Interface) and Node.js. By the end of this guide, you will be able to effectively publish and manage IPNS records, making your content easily accessible and updatable.

Prerequisites:

Before we get started, ensure you have the following:

  1. Basic understanding of IPFS and IPNS concepts.
  2. Node.js installed on your system (for Node.js method).
  3. Lighthouse CLI installed (for CLI method).

Understanding Mutability in IPFS:

In IPFS, content is typically addressed using CIDs, making it immutable. However, there are scenarios where content needs to be regularly updated, such as publishing a frequently changing website. IPNS addresses this challenge by creating mutable pointers to CIDs, known as IPNS names. These names act as links that can be updated over time while maintaining the verifiability of content addressing. Essentially, IPNS enables the sharing of a single address that can be updated to point to the new CID whenever content changes.

How IPNS Works:

  1. Anatomy of an IPNS Name: An IPNS name is essentially the hash of a public key. It is associated with an IPNS record that contains various information, including the content path (/ipfs/CID) it links to, expiration details, version number, and a cryptographic signature signed by the corresponding private key. The owner of the private key can sign and publish new records at any time.
  2. IPNS Names and Content Paths: IPNS records can point to either immutable or mutable paths. When using IPNS, the CID's meaning in the path depends on the namespace used:
    • /ipfs/cid: Refers to immutable content on IPFS, with the CID containing a multihash.
    • /ipns/cid-of-libp2p-key: Represents a mutable, cryptographic IPNS name that corresponds to a libp2p public key.

Step 0: Getting your lighthouse API key Files-Lighthouse-storage:

  1. Go on https://files.lighthouse.storage/ and Click on Login

    Untitled (2).png

  2. Select any of the login method and perform verification steps

    Untitled (3).png

  3. Click on API Key on the left side panel on the dashboard.

    Untitled.png

  4. Insert name for your API

    Untitled (1).png

  5. Copy the API Key

    Untitled design.png

Store and Update content on IPNS using Lighthouse:

Method 1: Using Lighthouse CLI

  • Step 1: Generate an IPNS key using the Lighthouse CLI:

    lighthouse-web3 ipns --generate-key
    

    This command will return an IPNS name and ID, which we will use later to publish the content.

    Untitled (4).png

  • Step 2: Make a test file, text.txt:

    echo "Hello World" >> text.txt
    
  • Step 3: Publish this file to the IPFS using lighthouse upload:

    lighthouse-web3 upload ./text.txt
    

    Untitled (5).png

  • Step 4: Publish the content using the generated IPNS key and the CID of the data you want to publish:

    lighthouse-web3 ipns --publish --key=8f4f116282a24cec99bcad73a317a3f4 --cid=QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u
    

    You will receive a link that can be used to access the published content. This link will remain valid even if the content's IPFS hash changes.

    Untitled (6).png

Updating CID:

  • Upload another file text2.txt:

    echo "Hello World2" >> text2.txt
    
  • Publish this file to the IPFS using lighthouse upload:

    lighthouse-web3 upload ./text2.txt
    

    Untitled (7).png

  • Update the content using the generated IPNS key and the CID of the data you want to publish:

    lighthouse-web3 ipns --publish --key=8f4f116282a24cec99bcad73a317a3f4 --cid=QmanCeGkwsaCUHaNT24ndriYTYSwZuAy4JDifdYZpHdmRa
    

    You will receive a link that can be used to access the published content. This link will remain valid even if the content's IPFS hash changes.

List all IPNS records associated with your Lighthouse account:

lighthouse-web3 ipns --list

This will display a list of IPNS records with their corresponding keys and CIDs.

Untitled (8).png

Remove an IPNS record:

lighthouse-web3 ipns --remove 8f4f116282a24cec99bcad73a317a3f4

This step allows you to remove an IPNS record if needed.

Untitled (9).png

Method 2: Using Node.js

Step 0: Get API keys from Lighthouse as explained above.

Step 1: Import the Lighthouse package and set up your API key:

import lighthouse from '@lighthouse-web3/sdk';
const apiKey = process.env.API_KEY; // Replace this with your actual API key

Step 2: Generate an IPNS key using the Lighthouse SDK:

const keyResponse = await lighthouse.generateKey(apiKey);
console.log(keyResponse.data);

This will return an IPNS name and ID, which we will use in the next steps.

Step 3: Publish the content using the generated IPNS key and the CID:

const pubResponse = await lighthouse.publishRecord(
  "QmWC9AkGa6vSbR4yizoJrFMfmZh4XjZXxvRDknk2LdJffc",
  keyResponse.data.ipnsName,
  apiKey
);
console.log(pubResponse.data);

You will receive a response containing the IPNS name and the link to access the published content.

Step 4: Get all IPNS keys associated with your Lighthouse account:

const allKeys = await lighthouse.getAllKeys(apiKey);
console.log(allKeys.data);

This step allows you to retrieve a list of all IPNS keys associated with your account.

Step 5: (Optional) Remove an IPNS key:

const removeRes = await lighthouse.removeKey(keyResponse.data.ipnsName, apiKey);
console.log(removeRes.data);

This step enables you to remove an IPNS key if necessary.

Conclusion:

Lighthouse IPNS is a powerful mechanism for publishing and updating content on the IPFS network. By combining the benefits of content-addressing with the flexibility of mutable pointers, IPNS ensures your content remains accessible and updatable. In this guide, we covered two methods to utilize Lighthouse IPNS: the CLI and Node.js. Armed with this knowledge, you can confidently publish and manage IPNS records, creating a more dynamic and user-friendly experience on the decentralized web.

Remember to keep your API key secure and use it responsibly. Happy publishing!

Our Blogs

Read our latest blog

icon

Nandit Mehra

Encryption and Access Control for Web3 using Lighthouse

icon

Lighthouse

How To Migrate Your Files To Lighthouse

icon

Nandit Mehra

Decentralized storage for the Ocean Protocol

icon

Ravish Sharma

Creating a Pay-to-View Model Using Lighthouse Storage

icon

Aryaman Raj

Getting Started with Lighthouse Python SDK

icon

Aryaman Raj

A Comprehensive Guide to Publishing and Updating Content with Lighthouse IPNS

icon

Aryaman Raj, Nandit Mehra

Time Lock Encryption using Lighthouse Access Control

icon

Aryaman Raj

Secure File Sharing using Lighthouse SDK: A Step-by-Step Guide

icon

Aryaman Raj

Passkey Demo App with WebAuthn and Ethereum

icon

Ishika Rathi

Web3 Storage: IPFS and Filecoin Guide

icon

Ishika Rathi

Understanding How web3 storage Operates

icon

Ishika Rathi

Lighthouse: Secure Web3 Storage for Your AI Data

icon

Ishika Rathi

Decentralized Storage: A Smarter, Safer, and Cheaper Way to Manage Your Data

icon

Ishika Rathi

Unveiling the Mechanics of Perpetual Storage

icon

Ishika Rathi

Navigating Permanent Storage: Harnessing the Power of Filecoin and IPFS

icon

Ishika Rathi

Decentralized Excellence: Elevating Data Storage with Lighthouse

icon

Ishika Rathi

Revolutionizing Permanence in Data Storage

icon

Ishika Rathi

Eternalizing Data: A Permanent storage

icon

Ishika Rathi

Exploring Web3 Advancements in Storage Solutions

icon

Ishika Rathi

NFT Storage Strategies

icon

Ishika Rathi

On-Chain Encryption: Security Unveiled