đŸ’ģNodeJS Decrypt File

Files can be fetched from Lighthouse node and decrypt using NodeJS, similar to the example given below.

Project Setup

Step 1: Create a new Node.js application and initialize it:

  • Open your terminal or command prompt.

  • Navigate to the desired directory where you want to create the application.

  • Run the following command to create a new Node.js application

cd lighthouse-upload-app
npm init -y

Step 2: Install the required dependencies:

npm install fs ethers @lighthouse-web3/sdk

Step 3: Import the necessary dependencies and configure the environment variables in your Node.js application:

Note: In this example, we are using ES6 so we have to save the file as filename.mjs or define "type": "module", in the package.json file.

import * as dotenv from 'dotenv'
import fs from "fs"
import { ethers } from "ethers"
import lighthouse from '@lighthouse-web3/sdk'

const signAuthMessage = async (publicKey, privateKey) => {
  const provider = new ethers.JsonRpcProvider()
  const signer = new ethers.Wallet(privateKey, provider)
  const messageRequested = (await lighthouse.getAuthMessage(publicKey)).data.message
  const signedMessage = await signer.signMessage(messageRequested)
  return signedMessage

const decrypt = async () => {
  const cid = "YOUR_CID" //Example: 'QmbGN1YcBM25s6Ry9V2iMMsBpDEAPzWRiYQQwCTx7PPXRZ'
  const publicKey = "YOUR_PUBLIC_KEY" //Example: '0xa3c960b3ba29367ecbcaf1430452c6cd7516f588'
  const privateKey = process.env.PRIVATE_KEY

  // Get file encryption key
  const signedMessage = await signAuthMessage(publicKey, privateKey)
  const fileEncryptionKey = await lighthouse.fetchEncryptionKey(

  // Decrypt File
  const decrypted = await lighthouse.decryptFile(

  // Save File


Step 4: Customize the code:

  • Replace YOUR_CID with the actual CID of the file you want to decrypt.

  • Replace YOUR_PUBLIC_KEY with your own public key.

  • Ensure you have the corresponding private key stored in the PRIVATE_KEY environment variable.

Step 5: Configure the Private Key:

  • Create a .env file in your project's root directory.

  • Add the following content to the .env file:

  • Replace YOUR_PRIVATE_KEY with your own private key corresponding to the public key used in the code (Can be obtained from the wallet.json file made while creating a wallet).

Step 6: Run the Node.js application to upload the file:

  • In the terminal, while in the lighthouse-decrypt-app directory, run the following command:

node app.js
  • The decrypted file will be saved as fileName.png in the current directory.

Note: Ensure that you have the correct CID, private key, and necessary configurations set before running the application.

With this code, you can fetch a file from the Lighthouse node and decrypt it using Node.js.

Last updated