Secure File Sharing using Lighthouse SDK: A Step-by-Step Guide
In the realm of decentralized technology and applications, ensuring secure and efficient file-sharing has always been a top priority. Lighthouse, a notable player in this domain, has developed a robust SDK that aids developers in achieving this. This SDK leverages blockchain principles and IPFS, which stands for InterPlanetary File System, to ensure that files shared across networks are not only secure but also immutable and tamper-proof.
In traditional file-sharing systems, there's a central server where files are stored. This poses several challenges: from server downtimes to the risk of central point failures and vulnerabilities. IPFS and blockchain, as adopted by Lighthouse, circumvent these challenges by storing files across a network, ensuring redundancy and security.
Moreover, with the increasing emphasis on privacy and data protection regulations worldwide, tools like the Lighthouse SDK empower developers to build applications that prioritize user data security. By using encryption and decentralized storage, we can ensure that our users' files remain confidential, accessible only by intended recipients.
This tutorial will guide you step-by-step on how to integrate the Lighthouse SDK into your Node.js application. By the end, you'll be able to securely share encrypted files with specified recipients, leveraging Lighthouse's decentralized storage and Ethereum's robust authentication mechanisms. Whether you're a seasoned developer or just starting out in the world of decentralized apps, this guide aims to make the process straightforward and intuitive.
Let's get started by setting the foundation for our file-sharing application!
Install the SDK globally:
npm install -g @lighthouse-web3/sdk
Generate a new Lighthouse wallet. Safeguard the provided Public Key
and Private Key
:
lighthouse-web3 create-wallet
Create and navigate to a new directory for your endeavor:
mkdir lighthouse-encryption && cd lighthouse-encryption
Commence a new Node.js project:
npm init -y
Install the necessary local packages:
npm install dotenv ethers
Generate a .env
file within your project directory.
Populate .env
with your Lighthouse private key:
PRIVATE_KEY=Your_Private_Key
To maintain security, add .env
to your .gitignore
file, especially vital if using a version control platform.
Within your project directory:
fileSharing.js
.In fileSharing.js
, write:
import * as dotenv from 'dotenv';
dotenv.config();
import { ethers } from "ethers";
import lighthouse from '@lighthouse-web3/sdk';
This helper function will assist in the authentication process:
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;
}
Implement the function to handle file sharing via Lighthouse SDK
To ensure clarity and simplicity, let's break down the file sharing procedure into three distinct points:
Set up the fundamental variables required for our function. Each variable holds specific data crucial for the operation:
const cid = "QmS2NzycJoA7De33qMWwqyE2w3BL1i396qfwZiHBb1KuZh";
// CID: Unique identifier for content on IPFS.
const publicKey = "0x5D62F371206306F1ebd4573803F70772f1153186";
// PublicKey: Your Lighthouse identity.
const privateKey = process.env.PRIVATE_KEY;
// PrivateKey: Secured key for authentication, stored away from the codebase.
const receiverPublicKey = ["0xea447D81825282D3ec02772f1ab045ec6227F3e4"];
// ReceiverPublicKey: Intended recipient's Lighthouse identity.
With our variables set, the next step is to authenticate our actions by signing the message:
const signedMessage = await signAuthMessage(publicKey, privateKey);
// SignedMessage: A verified authentication message for security.
Having our signed message and our initialized variables, we're ready to share our encrypted file securely:
const shareResponse = await lighthouse.shareFile(
publicKey,
receiverPublicKey,
cid,
signedMessage
);
// ShareFile: Lighthouse function to securely share your file.
console.log(shareResponse);
// ResponseOutput: Shows the result of the file-sharing action.
// To view the shared file, navigate to:
// https://files.lighthouse.storage/viewFile/<cid>
In the event of an error or an issue during this process, the catch block will capture and display it for our reference:
} catch (error) {
console.log(error);
}
Lastly, initiate the function:
shareFile();
import * as dotenv from 'dotenv';
dotenv.config();
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 shareFile = async () => {
try {
const cid = "QmS2NzycJoA7De33qMWwqyE2w3BL1i396qfwZiHBb1KuZh";
// CID: Unique identifier for content on IPFS.
const publicKey = "0x5D62F371206306F1ebd4573803F70772f1153186";
// PublicKey: Your Lighthouse identity.
const privateKey = process.env.PRIVATE_KEY;
// PrivateKey: Secured key for authentication, stored away from the codebase.
const signedMessage = await signAuthMessage(publicKey, privateKey);
// SignedMessage: A verified authentication message for security.
const receiverPublicKey = ["0xea447D81825282D3ec02772f1ab045ec6227F3e4"];
// ReceiverPublicKey: Intended recipient's Lighthouse identity.
const shareResponse = await lighthouse.shareFile(
publicKey,
receiverPublicKey,
cid,
signedMessage
);
// ShareFile: Lighthouse function to securely share your file.
console.log(shareResponse);
// ResponseOutput: Shows the result of the file-sharing action.
// Navigate to view the shared file:
// https://files.lighthouse.storage/viewFile/<cid>
} catch (error) {
console.log(error);
}
};
shareFile();
Execute the script:
node fileSharing.js
Observe the file-sharing response and ensure you can access the CID link to validate the secure file sharing.
Congratulations! You've adeptly shared an encrypted file using the Lighthouse SDK. Always prioritize the security of your private and API keys.
Our Blogs
Read our latest blog
Nandit Mehra
Encryption and Access Control for Web3 using Lighthouse
Lighthouse
How To Migrate Your Files To Lighthouse
Nandit Mehra
Decentralized storage for the Ocean Protocol
Ravish Sharma
Creating a Pay-to-View Model Using Lighthouse Storage
Aryaman Raj
Getting Started with Lighthouse Python SDK
Aryaman Raj
A Comprehensive Guide to Publishing and Updating Content with Lighthouse IPNS
Aryaman Raj, Nandit Mehra
Time Lock Encryption using Lighthouse Access Control
Aryaman Raj
Secure File Sharing using Lighthouse SDK: A Step-by-Step Guide
Aryaman Raj
Passkey Demo App with WebAuthn and Ethereum
Ishika Rathi
Web3 Storage: IPFS and Filecoin Guide
Ishika Rathi
Understanding How web3 storage Operates
Ishika Rathi
Lighthouse: Secure Web3 Storage for Your AI Data
Ishika Rathi
Decentralized Storage: A Smarter, Safer, and Cheaper Way to Manage Your Data
Ishika Rathi
Unveiling the Mechanics of Perpetual Storage
Ishika Rathi
Navigating Permanent Storage: Harnessing the Power of Filecoin and IPFS
Ishika Rathi
Decentralized Excellence: Elevating Data Storage with Lighthouse
Ishika Rathi
Revolutionizing Permanence in Data Storage
Ishika Rathi
Eternalizing Data: A Permanent storage
Ishika Rathi
Exploring Web3 Advancements in Storage Solutions
Ishika Rathi
NFT Storage Strategies
Ishika Rathi
On-Chain Encryption: Security Unveiled
BananaCircle
Web2 Storage Challenges Versus Web3 Solutions Ft. Lighthouse
Nandit Mehra
Discover How the Endowment Pool Makes Your Data Immortal
Nandit Mehra
What is FHE and how Lighthouse plans to use it
Nandit Mehra
AI Meets Blockchain: Beyond the Hype & Into the Future