Building a dapp with an encrypted Web3 native database

ETHCC 6 · July 2023 · 25:30

Giovanni Fu Lin demonstrates building a decentralized application with an encrypted web3-native database. The workshop considers how to retain a familiar developer experience while using decentralized storage.

Speakers

Topics

From ETHCC 6, hosted by Ethereum France.

Transcript

foreign thank you for being here uh just a quick introduction of myself I'm Giovanni and I come from Italy and I think I have been in the website space for like two years and I've built several side projects um one of the issues that I encountered was storage like uh we come with a lot of type of applications and what we are going to talk about today is to be the application that is built on

top of the central database so what started I started building applications and I told myself well I can store the data so the first place where I went was to go that smart contract and saw them on chain but soon I hit with some issues you know studying chain can cost a lot for example another solution is to store on the centralized databases like a falcon or R wave when we need to store images or files so anything that consume a lot of megabytes and you cannot

install them on chain so by selling on file storage you can get an URL and you store the URL on chain but what about structure data so that is what we are going to talk today how we can tackle the storage data in the centralized way and what I I write as criteria when I'm searching up for solution for this the Privacy the speed and the cost so let's start with the first one when we're studying data in a traditional database so in the web 2

space we can restrict the access matter on the blockchain everything is public so studying sensible data it can be hard then another problem that we want to solve is the user experience I think most of you have tried the app data we have here like a messaging app I account or a marketplace where you can buy something and experience so smooth to update the UI I just feels uh within one second and when you are making like a transaction on the website space it will pop up a

metamask wallet you have to upload have to pay and then you have to wait seconds and the blockchain where you have also to wait minutes that is a hard limit and that is making it hard to create app that can get Mass adoption like who want to wait a minute for app to to work and the last one is is the cost and it's like normal people can't afford paying thousand dollars just to make a transaction

and today it is still cost I think to dive five dollars maybe to make a transaction or make us what so that brought me to think about what I can use to build an application that can scale and what I found is uh new company that started recently and its company name is bodybase they stated themselves as the alternative the the central alternative to Firebase and Super Bass I really like the tech and the key feature are

the oil offering libraries SDK to allow Developers played easily data permission so we can create and database with Public Access private access and optionally the developer can with encryption into the data then the learning curve is also very low so if you already tried is almost the same and here are key feature about bodybase is the size limitation you don't have limit on the road and the

columns that you can create database and then it is fast so with spot device it's unlocked a tons of opportunities for developers like we can't get end-to-end encrypted messaging apps exchanges social medias to try body base out I built a MVP it's called identify it's a messaging app it's not as messaging up sorry it's a event app with the organizer can create event and then the attendees can buy the

ticket and do the check-in it's built on the most common Tech stack data we use today like a react typescript Tailwind open source so I can share with you at the end of the workshop [Music] um it should work because it's a gift but anyways so the application enable the organizer to create then the attendees can explore the events on it buy the ticket and when I think they go to the event you take workout to do the checking this is a basic flow of the application and because today we are focusing on the

centralized database we are not going to take a look into a typescript or the UI we are going to focus on how to build with bodybase so today agenda we are going to start with how to create the schema in the database and then how to read write authenticate the user and create access control to bodybase so let's start with the setup body device provides serverless libraries the client enables you to build with any JavaScript framework or vanilla but if you choose to use react

they also provide provide hooks to make it more handy to create on top of it so the database use a new language Bodyline social we need to learn a new language to use for device well they call it body lung and it sounds like a new language in reality is something that we already know so if you can check this code you can see it's basically a typescript with some added directives so if you know how to write touch clip you also know how to write body language

and to get started you don't need to install anything or an extension on Visual Studio code they provide this Studio that you can go to the Explorer and then on create collections and it will open their web editor where you can state coding and defining schema so also in the studio you can also take a look into the data that you have already installed now let's take a look into the collection so the collection show in the following way we have the field column then the field type pretty straightforward

and then we have the Constructor The Constructor is used to create a new row in the database so if in the schema we Define in the following way what we do to call this Constructor in the front end is by calling the create method then we have the functions we use the function to update a row that is created so we can change the data and to call it we use the call method then we have the directive if we we want to add a third or filtering feature to our application

and here what we're defining is we are enabling the Sorting of by population and searching by name and it works on the front-end Side by calling the well method now let's take a look into how to read data and what's a feature Body by is provide for reading first we need to create the polybus instance we need the default name space then the full name space work like the token ID or circuit key that we can get

from Super Bass or Firebase any traditional database we can get this default name space from the studio that we saw before so by going to the setting section here we can find our default namespace then if we we need to add a real-time feature like we want to notify the user of a new event when it's added to the database we can use that on Snapshot method to handle it and what about patination I think the um development is 100 items I'm not sure yet but uh if we want to

get more we can use the next method so as you can see we can get everything with just a few lines of code then for filtering we have the work keyword and also the the one that we saw at the beginning the sort method um we also have the record method when we want to like a create a profile of the user on the user click on event on a list and we want to only fetch the data of one row now let's dive into how to write the

database to create a new items database we use the grade and what it's passing is an array of arguments and the other model matter here which means that if in the Constructor we have the ID in the first place the first argument corresponds to the ID to update the database we use the call method where the first argument is the name of the function that we Define in the schema and the second argument is the array of the arguments so here is how we coded the schema and

how we call it from the front-end side now we also need to authenticate the user and we don't need to like create a model or codes because everything is already done by Body base and what we need to do is just call sanding and it will show upper path to the users if it's a website user they can use metamask and if they don't have a wallet they can just simply use their email now we are going to take a look into the access control

we don't want to anyone to like write an access to any data as we defined before in the previous sample we use the public okay so there's four ways to create access control and the first one that I was saying was the permission one well in the previous sample we use public key which means that anyone can read and write to the database and that is not what we want it is too General we want to create more granular access to the database

and what and we can achieve it by using delegation so in this schema what we can see is we are delegating the grid access to the grader and then to the public key so whoever that is calling the database must match the address with the publicly defined and the user uh if they sound too hard there's another way which is using the context because we authenticated the user every time that the database is called it contains the content with the color

public key so we can't just create the rules inside the function to restrict the access now let's talk about encryption so with the previous method we can't deny the access with encryption we are always showing the assets so anyone can read them on their database but only who has the P can decrypt the content and the way we do we do that is by using the duties Library provided by my body weight it contains two encryption method one is symmetric and another one is asymmetric

in this sample we're assuming how we can do it with the symmetric encryption what we do we need to do first is to create a circle key and we have to start somewhere because then we have to use it to the clip so the way that we do is we use the key and we encrypt the data and we upload the data to the database after that with the key we can decrypt the content so as I said before if you're using react you will have a better developer experience as it provides hooks that

enable you to create the data and write to it more easily so what we have learned so far is there are tons of tools that enable us to uh create a better application more scalable right now the best solution that I found is qualifier so that's why I like it a lot and what we have seen so far can help you to start your journey to build anything that you want and there's some problem I think in this

page it should show you the the link to the giba repository and to dive deeper into the documentation so if you want to uh the link you can hit me up later after the workshop in any case you can reach out to me on Twitter and this is my my name and yeah I think that's it if you have any question I'm here to answer yeah who who are maintaining the service with databases and does it have some incentivization to maintain it

so the data are still on Central database or the centralized database that depends on you what polybias provide is they have those nodes running and they are pluggable those nodes right now are centralized like all of them are done by the core team and they want to decentralize the nodes and then you as a developer you can plug the polar bears to any database that you want so you can go to the centralized way so you use Amazon S3 or you can use firecon for example when you're using filecoin how does you

have to pay for storage so do you pay ahead of time to file coin or does polybase provide like interface for you to pay filecoin uh when I try out uh it was it was on testnet and it was it was completely uh free I don't know how they want to implement on mainnet like I read in the documentation that they want to introduce a token so anytime that you use a bodybase you are going to need to buy the token and use them uh about Falcon if you need to pay also to them I don't know if it is covered by

the token or not that we feel like a that deeper like I'm moving forward when the magnet go public but now they are in direct the magnet is launched and they are testing out thank you do you have any question guys thank you for the representation how does it look from the user's perspective when they're fetching data is that like a signature every time for example you're fetching five tickets does the person have to like take five times to fetch all this and decrypt this data and show it how does it work really from the end user perspective uh so your question is how the end user

or yeah interacts with the what the app with his uh wallet to to see his own data it's encrypted they start to sign every time he wants to read or write into the database so for reading the user don't need to say anything so for reading it will like uh what like a web 2 application uh that user wouldn't notice any website stuff uh what's the user who's been differently is writing when you have to write you must sign a message and the user don't need to pay anything it's just a signing that will be the user experience

thank you thank you thank you how fast does it operate according to comparing to web 2 databases how fast it compared to traditional database databases or or so for reading I think it matched the translation databases it's 100 milliseconds very fast and for writing it's a bit slower because it's I think around one second and also the user need to send the message how do you answer Access Control

Access Control yeah so the access control is defined by the directive Ampersand delegate with public call on the schema so if a field is defined with the directive call for example every anyone can have access but then if you add the delegate only the public key that you defined in the interval can't access to it so let's say that someone call it the public is already stored in

industrial then he can read and draw and react sample could maybe could be a Blog where the owner has several writers the owner wants each also to edit their own boss so and it also it will be include a public key and when they sign into the application they can only uh so that a dedicated elective edit their own post sorry

can you explain a better your database is talked where you start your data on blockchain where is the data store yeah the data installed is up to up to you bodybase run Android node well they create the proof with ticket stock and then it is validated installed whatever you define so it could be the centralized solution like uh filecoin or it can be centralized solution like a Amazon S3 any other question

um how does the user decrypt the data when you want to access his data uh how the user decrypt the data [Music] so with encryption [Music] we use the UT Library which provides a way to create a secret key and basically the user must store somewhere like the product key from the phone from your wallet [Music] um but the user when they store something encrypted the usage will still have the key so

when the user face the data then click the data from the database the user used the same key to decrypt it other question or it's okay apparently yes thank you very much okay [Applause] [Music] [Applause] thank you