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

Pushing File Metadata Onchain

PreviousToken Gating NFTsNextUse Radix Wallet on Lighthouse Filesdapp

Last updated 2 months ago

Was this helpful?

In this section, we will push uploaded file metadata on-chain through the . Initially, we will push the file info like filename, filesize, mime-type, etc on-chain followed by updating dealIDs for that CID after a successful deal creation of the file.

Make sure to go through the section before proceeding. The complete script can be copied from

Install and import all the required modules

const lighthouse = require("@lighthouse-web3/sdk");
const ethers = require("ethers");
const OnChainCIDabi = require("./utils/abi");
const chainConfig = require("./utils/chainConfig");
require("dotenv").config();

Paste the Lighthouse API Key and signing wallet private key

const API_KEY = process.env.LIGHTHOUSE_API_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

Declare the Rpc Url and onchainCID contract address either by passing the chain name or pasting it from .

const chain = "baseSepolia"; // Update chain here or

// Update rpc and contract address here
const RPC_URL = chainConfig[chain].rpcUrl;
const CONTRACT_ADDRESS = chainConfig[chain].contractAddress;

Supported chains include -

  • Mainnet - polygon, base, filecoin

  • Testnet - polygonAmoy, baseSepolia, calibration

Creating the signer and contract instance

const provider = new ethers.JsonRpcProvider(RPC_URL);
const signer = new ethers.Wallet(PRIVATE_KEY, provider);
const contract = new ethers.Contract(CONTRACT_ADDRESS, OnChainCIDabi, signer);

Now to add a file CID and metadata on-chain, either upload a new file using Lighthouse SDK

const filePath = "./uploadSample.txt";
const uploadResponse = await lighthouse.upload(filePath, API_KEY);
console.log("File uploaded to Lighthouse:", uploadResponse);
const cid = uploadResponse.data.Hash;

Or paste an uploaded file’s CID

const cid = "Qmah99npVfj9WRMfc172Ghk1qKdxF7BTYFLTD9Ph4wseTJ";

Retrieve the file info and execute the pushCIDOnchain function

const fileInfo = await lighthouse.getFileInfo(cid);

const tx = await contract.pushCIDOnchain(
      cid,
      fileInfo.data.fileName,
      fileInfo.data.fileSizeInBytes,
      fileInfo.data.encryption,
      fileInfo.data.mimeType,
      [] // initial empty dealIDs array
);
await tx.wait();

View the file metadata on the chain

const details = await contract.getFileDetails(signer.address, cid);
console.log(details)

you will get a response like this -

{
  filename: 'uploadSample.txt',
  size: '71',
  encryption: false,
  mimeType: 'text/plain',
  dealIDs: []
}

After the successful deal creation of the file, deal IDs can be updated for that CID. Fetch the deal status and then execute the updateDealID function of the contract.

 const dealStatus = await lighthouse.dealStatus(cid)
 
 // aggregating dealIDs
 const dealIds = [];
 dealStatus.data.forEach((sector) => {
    sector.deal.forEach((deal) => {
      dealIds.push(deal.dealId);
    });
 });
  
// update the dealIDs in the contract
const tx = await contract.updateDealID(cid, dealIds);
await tx.wait();

Now the file metadata can be viewed again

const details = await contract.getFileDetails(signer.address, cid);
console.log(details)

This time, the response will include the dealIDs

{
  filename: 'beanMeUp.jpeg',
  size: '2056258',
  encryption: false,
  mimeType: 'image/jpeg',
  dealIDs: [ '93785930', '93782071', '93877438', '93871617' ]
}
πŸ–οΈ
OnchainCID Contract
quick start
here
chain config