Privacy & Access Control
Learn about encrypting data to be stored on Filecoin and gating access to data already stored on Filecoin.
Encrypting data for storing on Filecoin
Ensuring your dataset is encrypted is critical to good privacy hygiene when storing files on decentralized networks, including Filecoin and IPFS. Uploading an unencrypted file would allow the storage provider to read the files you store with them, and allow them to send copies to unknown third parties.
The Lighthouse team developed the Kavach encryption SDK, which is included in the Lighthouse SDK by default, to enable encryption of files pinned to IPFS or stored on Filecoin. The below examples are pulled directly from their documentation, you can read more here.
Ingredients:
Instructions:
There are two options for encrypting files being uploaded to Filecoin.
The first option is encrypting your uploaded file using the Kavach SDK in the backend of your app.
import {ethers} from "ethers"
import lighthouse from '@lighthouse-web3/sdk'
import kavach from "@lighthouse-web3/kavach"
const signAuthMessage = async(privateKey) =>{
const signer = new ethers.Wallet(privateKey)
const authMessage = await kavach.getAuthMessage(signer.address)
const signedMessage = await signer.signMessage(authMessage.message)
const { JWT, error } = await kavach.getJWT(signer.address, signedMessage)
return(JWT)
}
const uploadEncrypted = async() =>{
/**
* This function lets you upload a file to Lighthouse with encryption enabled.
*
* @param {string} path - Location of your file.
* @param {string} apiKey - Your unique Lighthouse API key.
* @param {string} publicKey - User's public key for encryption.
* @param {string} signedMessage - A signed message or JWT used for authentication at encryption nodes.
*
* @return {object} - Returns details of the encrypted uploaded file.
*/
const pathToFile = '/home/cosmos/Desktop/wow.jpg'
const apiKey = 'YOUR_API_KEY_HERE'
const publicKey = 'YOUR_PUBLIC_KEY_HERE'
const signedMessage = await signAuthMessage(privateKey)
const response = await lighthouse.uploadEncrypted(pathToFile, apiKey, publicKey, signedMessage)
console.log(response)
/* Sample Response
{
data: [
{
Name: 'decrypt.js',
Hash: 'QmeLFQxitPyEeF9XQEEpMot3gfUgsizmXbLha8F5DLH1ta',
Size: '1198'
}
]
}
*/
}
uploadEncrypted()Alternatively, files can be encrypted with MetaMask in your browser application.
The following code also demonstrates how to encrypt JSON / text files stored on IPFS or Filecoin.
Gated access to your dataset
Lighthouse also provides a number of methods to gate access a given data set. In the below code, the variables are:
id
the condition number
chain
the current blockchain network
method
function used to check a condition
standardContractType
the type of contract being checked. Options include ERC20, ERC1155, ERC721 or Custom
returnValueTest
details what is being compared
parameters
allow for the function to take in any data it may need
inputArrayType
the type of the parameter being taken as input
outputType
the type of response returned by the function
Sample Code:
The first method is “NFT-based access,” where a user is able to access a file if they own at least one NFT of a given ERC721 contract.
The second method is “Custom contract,” where a user is able to access a file if the returned value of a given function in the custom contract satisfies a certain condition. In the below example, the condition being checked is whether the “get()” function returns “1”.
The third method is to check native tokens. In the below example, the condition being checked is whether the wallet address looking to access a file owns at least 1 ETH.
The fourth and final method is to condition the access of a file on the block height, which is effectively time-based gate access. In the example below, a user can access the file above the block height of 133494.
To review the Lighthouse documentation in its entirety, please visit: https://docs.lighthouse.storage/lighthouse-1/
Last updated
Was this helpful?