Adding access control with zkTLS for encrypting a file involves two primary steps: creating access conditions for a Content Identifier (CID) and submitting proofs for verification.
Step 1: Creating Access Conditions for a CID
To enable zkTLS-based access control for any file or data, you must define access conditions for the respective CID.
import*as dotenv from'dotenv'dotenv.config()import { ethers } from"ethers"import axios from"axios";constsignAuthMessage=async (privateKey) => {constsigner=newethers.Wallet(privateKey);constmessageRequested=awaitaxios.get(`https://encryption.lighthouse.storage/api/message/${signer.address}` );constsignedMessage=awaitsigner.signMessage(messageRequested.data[0].message );return signedMessage;};constapplyzkconditions=async () => {// CID of encrypted file// CID is generated by uploading a file with encryption// Only the owner of the file can apply access conditionsconstcid="bafkreibwimyyrqiqhl7difiu3gl6a5znf2ml7tgrh3uekfsitvrqczkzr4";constpublicKey="0xa3c960b3ba29367ecbcaf1430452c6cd7516f588";constprivateKey=process.env.PRIVATE_KEY_WALLET1;constnodeId= [1,2,3,4,5];constnodeUrl=nodeId.map( (elem) =>`https://encryption.lighthouse.storage/api/setZkConditions/${elem}` );constsignedMessage=awaitsignAuthMessage(privateKey);constconfig= { method:"post", headers: { Accept:"application/json", Authorization:`Bearer ${signedMessage}`, }, };constapidata= { address: publicKey, cid: cid, conditions: [ { id:1, method:"City", returnValueTest: { comparator:"==", value:"New York", }, }, ], };constrequestData=async (url) => {try {returnawaitaxios({ url, data: apidata,...config, }); } catch (error) {console.log(error);return { isSuccess:false, error:JSON.parse(error.message), }; } };constdata= [];for (const [index,url] ofnodeUrl.entries()) {constresponse=awaitrequestData(url);data.push(response.data); }};
This step sets the access rules for the data based on zkTLS proofs, such as verifying a user's location or other verifiable attributes.
Step 2: Verifying zkTLS Proof and Accessing the File
Once the conditions are set, users must submit their zkTLS proof along with the CID to gain access to the data.