Lighthouse
  • 👋Introduction
  • Quick Start
  • How To
    • 🔑Create an API Key
    • 🔼Upload Data
      • 📁File
      • 🔤Text/JSON
      • 🔀Buffer
    • 🔒Upload Encrypted Data
      • 📁File
      • 🔤Text/JSON
      • 🔐Encryption Authentication
        • 📝Method 1: Signed Message
        • ⚕️Method 2: JWT
        • 📲Method 3: Passkey
    • Encryption Features
      • 👯Share File
      • ❌Revoke Access
      • 🔑Check Access Conditions
      • 🚪Token Gating
      • Chains Supported
      • 📃Access Control Conditions
      • 🔓Decrypt File
        • 🌐Browser Decrypt File
        • 💻NodeJS Decrypt File
      • 🚪Access control with zkTLS
      • 👬Account Delegation Tutorial
    • 📂List Files
    • 💁File Info
    • 💰Get Balance
    • 🔁Retrieve File
    • 💾Check for Filecoin Deals
    • 🔄IPNS - Handle Mutable Data
    • 📦Migrate Files
    • 📌Pin CID
    • 💸Pay per use
    • Resize Image
    • 💻Use CLI Tools
  • zkTLS
  • 🤝Account Delegation
  • 💾Filecoin First
    • Usage
    • 💰Pay Per Deal
  • Tutorials
    • 💸Pay to View Application
    • Use Python SDK
    • 📝Update Content with Lighthouse IPNS
    • 📹Add Video Player in UI
    • ✅Document Verification with PoDSI
    • 🎨Minting NFTs on EVM Chains
    • 🪙Minting NFTs on Solana
    • 👩‍💻Programmable Storage with Lighthouse SDK and Filecoin
    • 🔐Secure File Sharing
    • Upload Encrypted Files
    • 📄Token Gating and Custom Contract
    • 🔑Token Gating NFTs
    • 🏖️Pushing File Metadata Onchain
    • Use Radix Wallet on Lighthouse Filesdapp
  • Concepts
    • Glossary
    • IPFS CID Transition to v1
  • Support
    • 📞Contact
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. How To
  2. Encryption Features
  3. Decrypt File

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'
dotenv.config()
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(
    cid,
    publicKey,
    signedMessage
  )

  // Decrypt File
  const decrypted = await lighthouse.decryptFile(
    cid,
    fileEncryptionKey.data.key
  )

  // Save File
  fs.createWriteStream("fileName.png").write(Buffer.from(decrypted))
}

decrypt()

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:

PRIVATE_KEY=YOUR_PRIVATE_KEY

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.

PreviousBrowser Decrypt FileNextAccess control with zkTLS

Last updated 1 year ago

Was this helpful?

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