Step 1: Upload your first file using Lighthouse SDK
Firstly, you'll need to get a picture of your favorite pupper whose picture you'll want to store on the decentralized web.
The Lighthouse SDK is a JavaScript library that allows you to upload files to the Filecoin network. It's open source and available here
A. Uploading a file is as simple as:
import lighthouse from"@lighthouse-web3/sdk";// ... other codeconstuploadResponse=awaitlighthouse.upload('/path/to/adorable/dog.jpg','YOUR_API_KEY');
Step 2: Understanding PoDSI: Getting the PoDSI for your file
Now that you've registered the picture of your puppy, how would you know that it's actually being maintained on the Filecoin network? This is where the PoDSI comes in. The PoDSI is a proof that your file is being maintained on the Filecoin network.
Proof of Data Segment Inclusion (PoDSI) is like a certificate of authenticity. It assures that your file is safely tucked inside a special package, known as a "deal", made by the Lighthouse Node. This node combines several files, gives them a unique ID, offers proof of their inclusion, and even throws a mini-proof of the entire package's structure.
The time between uploading and being able to get your PoDSI should only be a few minutes. You can get the PoDSI for your file by calling the getProof function in one of the following ways:
via Axios in node.js
let response =awaitaxios.get("https://api.lighthouse.storage/api/lighthouse/get_proof", { params: { cid: lighthouse_cid, network:"testnet"// Change the network to mainnet when ready }})
or via curl
# Assumes that uploaded your file to mainnet.# Alternatively, if you are using testnet, add &network=testnet to the end of the URL.curlhttps://api.lighthouse.storage/api/lighthouse/get_proof?cid=<puppy_CID>
curl example:
# An example of how to get the PoDSI for a file uploaded to testnetcurlhttps://api.lighthouse.storage/api/lighthouse/get_proof?cid=QmYTaCnjNrrKCwXzC8ZLiiNJ78rsobXtfKwN8s9qCLBzVA&network=testnet
The response, an example of a PoDSI proof on Calibration, should look something like this:
The proof contains information that can be used to confirm whether your file was included in a specific aggregated data bundle.
The dealInfo provides details about the file's storage deal. If the "dealId" is null, it means that the storage deal has been initiated but the miner hasn't started the sealing process yet.
Step 3: Get your deal ID of your upload
When you upload the picture of your puppy, the on-chain deal that is made to store it on the Filecoin network is assigned a unique deal ID. You can get this deal ID from the PODSI response above.
Under the hood, the node infrastructure is working hard to ensure that your file is included on-chain. The process of deal making can take up to about a day.
Step 4: Download your file using the fileâs CID
Now that your file is stored on the Filecoin network, you can retrieve it using its CID. You can do this by calling the download function in one of the following ways:
via CLI:
# Assumes that you have lighthouse-cli installed. If not, feel free to download it using # npm install -g @lighthouse-web3/sdkcurl-ofileNamehttps://gateway.lighthouse.storage/ipfs/<cid>
or via Code:
constlighthouseDealDownloadEndpoint= https://gateway.lighthouse.storage/ipfs/'let response =awaitaxios({ method:'GET', url:`${lighthouseDealDownloadEndpoint}${lighthouse_cid}`, responseType:'stream',});try {constfilePath=awaitthis.saveResponseToFile(response, downloadPath);console.log(`File saved at ${filePath}`);return filePath} catch (err) {console.error(`Error saving file: ${err}`);}saveResponseToFile(response, filePath) {constwriter=fs.createWriteStream(filePath);// Pipe the response data to the fileresponse.data.pipe(writer);returnnewPromise((resolve, reject) => {writer.on('finish', () =>resolve(filePath));writer.on('error', (err) => {console.error(err);reject(err); }); });}
You can also work with PODSI, and deal making on-chain with help of smart contracts which we will discuss thoroughly in next section.