paint-brush
Get Started in Web3 by Building a Web3 Storage App in 4 Stepsby@heypran
2,353 reads
2,353 reads

Get Started in Web3 by Building a Web3 Storage App in 4 Steps

by Pran B.October 10th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article we will be creating a notes app using IPFS. IPFS is a decentralized storage.:::info. We will be using the web3 storage library for IPFS and provides an easy and scalable way to store data on IPFS::::We wont be diving into UI or ReactJs part of the app. As these are outside the scope of the article. We will have to deal with two tings that we need to do - storing data and retrieving data.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Get Started in Web3 by Building a Web3 Storage App in 4 Steps
Pran B. HackerNoon profile picture



In this article we will be creating a notes app using IPFS. IPFS is a decentralized storage.


To create this app we will be using following tools-

  1. zk-block - boilerplate for zk and web3 dapps. ( NextJS, React, Typescript, Metamask)
  2. web3 storage - library for IPFS and provides an easy and scalable way to store data on IPFS


We wont be diving into UI or ReactJs part of the app. As these are outside the scope of the article.


When dealing with any storage, there are mainly two tings that we need to deal with -

  1. Storing data
  2. Retrieving data


Assuming you can handle creating a UI and user inputs in the react, we can just focus on the code that will help you interact with the IPFS using web3.storage

1. Install the web3.storage

npm install web3.storage


2. You need to get an access token

Getting an access token is free, and you can get yours at https://web3.storage/account/

3. Create a storage client object

 const storage = new Web3Storage({ token: process.env.NEXT_PUBLIC_WEB3_KEY });

4. Store & Retrieve Files

Storing

The Web3.Storage client's put method accepts an array of File objects. So, we will have to convert any data that we want to store in a File object.


const finalContent = JSON.stringify(content);
const file = new File([finalContent], await sha256(finalContent), {
      type: 'text/plain',
    });


Once, we have the File object. we can simply call put to store or upload the files to IPFS


const cid = await storage.put([file]);


CID you get back from the client when uploading is the CID of the directory and not the file itself.

To link to the file itself using an IPFS URI, just add the filename to the CID, separated by a / like this: ipfs://<cid>/<filename>.

For eg, to access this blog link stored on ipfs you can use the below link -

https://ipfs.io/ipfs/bafybeicbv3pjrmkx7vy27puybjitqibzo6r5hrlc2exp6jh4kf4ijhpaqe


Optional: put also provides callback functions to show upload progress.


const onRootCidReady = cid => {
    console.log('uploading files with cid:', cid)
  }

const onStoredChunk = size => {
    uploaded += size
    const pct = totalSize / uploaded
    console.log(`Uploading... ${pct.toFixed(2)}% complete`)
  }

await storage.put(files, { onRootCidReady, onStoredChunk });

Retrieving

You can check your uploaded data by -



or via code

const web3ResponseObject = await storage.get(cid); // Web3Response object


The web3ResponseObject extends the Response object from the Web Fetch API with two methods that provide access to the retrieved IPFS data: files and unixFsIterator().

The files method returns an array of Web3File objects, which represent all files contained in the content archive identified by the given CID. A Web3File is just like a regular web File object, with the addition of path and cid properties. These contain the relative path of the file within the archive and the CID of the file, respectively.


const files = await web3ResponseObject.files(); // return your files


You can checkout the notes app in action here and the source code of notes app ( called Dotes).


Subscribe to more education content - https://zkblock.app/subscribe

Optional: Encrypting the data

Since, all the data is publicly accessible and sometimes its best to store data in encrypted form.


const stringifiedContent = JSON.stringify(content);
const finalContent = CryptoJS.AES.encrypt(stringifiedContent, password).toString();


and you can decrypt like below,

const retrievedContent = CryptoJS.AES.decrypt(reader.result, password).toString(CryptoJS.enc.Utf8);
const actualContent = JSON.parse(retrievedContent);


For more content like this checkout - https://blog.zkblock.app/


Thank you for reading. Have an amazing day!

Twitter: https://twitter.com/heypran