Lighthouse
Ask or search…
K
📝

Update Content with Lighthouse IPNS

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. 1.
    Basic understanding of IPFS and IPNS concepts.
  2. 2.
    Node.js installed on your system (for Node.js method).
  3. 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. 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. 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.
Note: Skip to Store and Update content on IPNS using Lighthouseif you already have a Lighthouse API Key

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

  1. 1.
    Go on https://files.lighthouse.storage/ and Click on Login
  1. 2.
    Select any of the login method and perform verification steps
  1. 3.
    Click on API Key on the left side panel on the dashboard.
  1. 4.
    Insert name for your API
  1. 5.
    Copy the API Key

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.
  • 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
  • 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.

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
  • 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.
Remove an IPNS record:
lighthouse-web3 ipns --remove 8f4f116282a24cec99bcad73a317a3f4
This step allows you to remove an IPNS record if needed.

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!