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 { ethers } from"ethers"import lighthouse from'@lighthouse-web3/sdk'constsignAuthMessage=async (privateKey) => {constprovider=newethers.JsonRpcProvider()constsigner=newethers.Wallet(privateKey, provider)constmessageRequested= (awaitlighthouse.getAuthMessage(signer.address)).data.messageconstsignedMessage=awaitsigner.signMessage(messageRequested)return signedMessage}constaccessControl=async () => {try {// CID of encrypted file// CID is generated by uploading a file with encryption// Only the owner of the file can apply access conditionsconstcid="Qma7Na9sEdeM6aQeu6bUFW54HktNnW2k8g226VunXBhrn7"constpublicKey="0xa3c960b3ba29367ecbcaf1430452c6cd7516f588"constprivateKey=process.env.PRIVATE_KEY_WALLET1// Conditions to addconstconditions= [ { id:1, chain:"Optimism", method:"getBlockNumber", standardContractType:"", returnValueTest: { comparator:">=", value:"13349" }, }, ]// Aggregator is what kind of operation to apply to access conditions// Suppose there are two conditions then you can apply ([1] and [2]), ([1] or [2]), !([1] and [2]).constaggregator="([1])"constsignedMessage=awaitsignAuthMessage(privateKey)/* accessCondition(publicKey, cid, signedMessage, conditions, aggregator) Parameters: publicKey: owner's public key CID: CID of the file to apply access control on signedMessage: message signed by the owner of publicKey conditions: access conditions in the specified format aggregator: aggregator to apply conditions */constresponse=awaitlighthouse.applyAccessCondition( publicKey, cid, signedMessage, conditions, aggregator )// Display responseconsole.log(response) } catch (error) {console.log(error) }}constgetFileEncryptionKey=async () => {try {// Get key back after passing access control conditionconstcid='YOUR_CID'// Example: "Qma7Na9sEdeM6aQeu6bUFW54HktNnW2k8g226VunXBhrn7";constpublicKey='YOUR_PUBLIC_KEY'// Example: "0x969e19A952A9aeF004e4F711eE481D72A59470B1";constprivateKey=process.env.PRIVATE_KEY_WALLET2constsignedMessage=awaitsignAuthMessage(privateKey)/* fetchEncryptionKey(cid, publicKey, signedMessage) Parameters: cid: CID of the file publicKey: your public key signedMessage: message signed by the owner of the public key */constkey=awaitlighthouse.fetchEncryptionKey( cid, publicKey, signedMessage )console.log(key) } catch (error) {console.log(error) }}accessControl()
Replace YOUR_PRIVATE_KEY_WALLET1 with your own private key corresponding to the public key used for Wallet 1 (Can be obtained from the wallet.json file made while creating a wallet).
Replace YOUR_PRIVATE_KEY_WALLET2 with your own private key corresponding to the public key used for Wallet 2.
Step 6:Run the Node.js application to upload the file:
In the terminal, while in the lighthouse-access-control-app directory, run the following command:
nodeapp.js
The response from the access control process will be displayed in the console, indicating the status of the operation.
Note: Ensure that you have the correct CID, private keys, and necessary configurations set before running the application.
With this code, you can apply access control to a file on the Lighthouse node using Node.js.
Supported Chains
Mainnet: Ethereum (ethereum), Filecoin, Polygon (polygon), Solana (solana), Fantom (fantom), Optimism (optimism), BSC (binance chain), AVAX, Fuji, Forma, Destra
Testnet: Mumbai (polygon-testnet), Filecoin_Testnet, Shardeum_Lib1, Shardeum_Lib2, Calibration, Goerli, BSCTest (binance-testnet), FantomTest, Rinkeby, OptimismGoerli, OptimismKovan, Forma Testnet (Forma_Sketchpad), Destra_Testnet,Zircuit_Testnet,Scroll_Testnet
Note: Name is not case sensitive.