# WASM and Rust: Building Blocks for Blockchain by Dawn Song, Nikhil Sharma (Devcon5)

- Channel: [Devcon](https://streameth.org/devcon)
- Date: 2020-10-09
- Duration: 36:32
- Topics: blockchain, smart contracts, distributed ledger, ethereum, Science & Technology
- Watch: https://streameth.org/watch/yt-lDuS4rUyVsk
- YouTube: https://www.youtube.com/watch?v=lDuS4rUyVsk

## Description

Computing on-chain is an exciting prospect for high-integrity algorithms, trusted agents, finance and more. The convergence on WebAssembly provides a base layer to get there, but what does it look like in practice? This workshop will look at how a library layer can get us towards code that can run both on and off chain, and will delve into WASM and the other work at the runtime interface layer for interacting with the chain.

## Transcript

[Applause] started Oasis too and so I've been working there for about a year now this is my first Def Con and I'm super excited yeah let's talk about what we do today so I wanted to give like a short reply a 10-minute just feel about what awaits us and why what we're doing is right and then we'll jump straight into we're gonna show you how to start thank you where I'm going to show you how to build on bases they have some this is a workshop and so to download our developer tools so there's going to be a few things so a little bit of white lace is actually important so data pictures have been super popular over the last decade or more and that's because a lot of and either conventionally ordered by accident they often this so my show ends how many people here have or you do so entirely Austrians so how this be always watching help fix issues with an epoxy so we help you build decentralized applications with 30 90 guarantees privacy infantry now that's it so so the major issues that are there with Anderson left home are first of all I need to trust whatever and you also need to trust the applications that are built uncomfortable together at these two they have to trust we do all sorts of undesirable data linkages and that is Oasis fixes this by using the blockchain we have our code layer one solution that we're hoping is available and this is combined with secure hardware and other confidentially confidentiality preserving environment so the notes that executed we'll get more into vitamin but together these things enable general-purpose computation including but not limited to privacy preserving analytics and so I'm sure everybody you're just quite familiar with guarantees that smart concept to you but just or a the street properties that give you are my integrity high visibility and also immutability but these things don't give you privacy things on the blocks you are and that all is solved by ASUS with secured on this so what is a secure on play it's a black box that's run on an untrusted machine so in this case a blockchain so at a high level what happens is the untrusted machines the Mafia will allocate a section of their memory that they no longer as a remote user of their machine whatever their configuration is to be able to successfully run a program without actually seeing and receiving once that happens you can send encrypted data into this black box which again that untrusted node or knows by a Kita's provided provisioned by our decentralized manager and and the results are reinforced but out so only you read them during this entire process there was never a point in which any of the nodes running so yeah so right now what we use is Intel SGX and that comes with Senator Publix mainly being itself the private area there's a bunch of other various security vulnerabilities and we also hope to support the Keystone which is a research project that works super simple basis that's going to be open source that is going to be more and it has a bunch of other benefits such as they haven't been reallocation one of the performance issues yes is has to do with memory limitations around actually setting up these on there so how would you actually interact with these great programs that can be consumed within a secure Enclave in a reasonable one so that gets into velocity which is the webassembly system enters so doesn't understand huazi we first need to understand that wasn't so awesome is love assembly it's an environment agnostic language that's used on mostly in your browser and it is agnostic because the way it interacts in the apartment is strictly through 80 eyes and on the web where we already have wet me because that's pretty straightforward but in general environments dirty until now it definitely exists any standardized so wasn't wasn't as portable as a chip so while she fixes this it's a project about my and what it is it's a this call like API where in a generic form can interesting and we use this within secure on to so yeah so the Oasis blockchain along with many other people are how adopting one and so yeah let's dig into a little bit more what does so to compare it to the EDM EBM has some properties that make it really nice and portable so this deterministic impossible platform and box and its general purpose what assembly is all of these things as well but it was not so yeah so this is called interface it's you a bunch of things interact with environment variable system yeah capability-based enough for a model it's where you only have to implement exactly densa cells that you want for your loved one and yeah so we feel that was the end of opportunity this magical window there in particular for running well chain computation could you know we need to support a few different means for storage which you can think of a key value store you need to be able to read and write files for our pcs in our system similarly together mean write files and also variables randomness and for and so logy provides all these so how does the Asus let you do all of this in a super abstract high level well we have an oasis SDK which I'm going to show you during the workshop and so it comes with a bunch of things it comes with a command-line interface which is similar to you in trouble and you lose you build and I'll deploy your projects on oasis we have a gateway that lets you deploy to the oasis network and you also have some rust libraries that can be used to help and coming soon we have a few additional development kits that I'm pretty excited about in particular the data privacy API and the open finance etiquette so - that kid is going to let you build apps and the data privacy API is going to be a way by which you as a user and share your data with malevolent consume and receive results exposing your data to those parties that might writing abilities and again that all ties back to secure hardware okay so this is an overview of how the network actually works so you're going to be able to initialize build and run programs with and deploy our work through the Oasis client which is very similar to and once you deploy to your client transactions will be sent through either a three gateway or a waste estate way that we're working on she's actually already open source with this kind of get get it to a stable state right now and so yeah so would you execute plans out through the Gateway they just sent to the oasis network where our runtime will consume your welcome binary any that's nice okay so let's go ahead and actually get to the workshops so yeah I'm about it's allowing things will work okay [Music] I can get this [Music] maybe one of accidents all right so to actually create a contract on an oasis super simple I just type the oysters in it and then what I want to build today we're going to build a calculator oh so now I have a calculator initialized and so what does that calculate a revolution so we need two parts there's two sides to this we need a service so we need a service where I can define my actual contract and then an app from where I can actually interact with that service once it's deployed and execute our pcs and all that so what the service is is standard Russel service rust is our language of choice apparently and so all of the code is going to live inside of this main dot RS file for those of you that are bowling so it may not arrest you'll see that a bunch of code is already going to be initialized for you and this for the most part is going to look exactly like a normal service there's two things that you have to add as you're implementing there is both a you need to define its fate anyways tubes modify state and so state definition happens inside up here needed by and a constructor for this calculator we're not actually going to have any state but if you wanted to this is where you do it you just so to actually define ways to interact with a state and do stuff that happens within this implementation so you'll notice that there's two things there's a constructor that's empty you can add instructor variables here there's also a default RPC call that we find all it does is it reads in your your address and we're going to replace this and it could actually function like a calculator so the first function the only deployment is what I'm going to add and want to do add two floats together call them a and B and their sum is going to be a float as well so to return across the last line return a plus B all right so keeping it simple it's going to do a few more of these subtract multiply and it back Thanks so to subtract oh so that was pretty simple now we have for our pcs to find and [Music] being functional and smart let us give the poor basic operations and so you might notice that there's also some code down here this is how you need customer service so we have a library that lets you actually your service so what it does is it creates a account for you and then now you can use that accounts to create one of these context objects which again if you're familiar with solidity context does exactly the same in context of Zender context our value yeah so they're now using that context we initialized it later all right and then now the only test we have is not result the same Club let's actually make some tests so in rust and you do that with this assert so we have one two five and we want the result of s and similarly we can have same kind of stuff or subtract multiply yeah so you also need to provide my taxes and actually it now we should have a fully functioning going to tests for this service that we bring that is going to affect like validate how do we actually go ahead and build this so let's go ahead and check out and hopefully so while that's going ahead in Thailand I'm going to talk to you a bit about how how exactly what we've built here the first from a standard service and there's a human so of course there's that there's that context I hooked up so every method that's just so all the information was centered on you oh my the other difference is this derive orientation and that handles message parsing or persisting things to voices platforms for it to endless like loading and storing your state so the final difference is this main method which has a service macro and so running Oasis bill that tells us that stuff is so that tells running away stability that's running inside of you okay so seems like this profile there are a few warnings for those those are because we don't have to use that anymore now that we have that you can brought those tests of yours by running yes so the tests are built because I didn't change the values go ahead and that 10-5 15 so now when I test it there we go there we go so that's past everything is nice so now that we have this working service that's ready to deploy how do we actually deploy it an application so let's go to this app directory now and you'll see this other cloud structure you can build out whatever after while ago here in here we have a couple examples on our website but for actually testing that deploying and making dreadful work everything you want to do is inside this when we have a service yes go ahead and open that up it's going to have a basic default chest so here I'm going to cheat a bit and take some replace it with some stuff so whatever based here it is just our by default it connects to our production developer gateway he waits his gateway here and this changed his two points to a web to me gateway and that way we can actually see transaction logs inside of our terminal so how do you actually configure the stuff first you have a despite one since the spider demonic you also want to specify the order to wear that local change running so I guess that was a good time to actually show you how to run that local so going back to the terminal again super simple as you expect always change should get that up and running for you and so now you'll see that the Sun pretty much it's not the same as the ganache feel like it just creates a bunch of accounts for you and now it's listening for transactions that are common again state so going back here now we've connected to that chain and using that you wanna create a wallet so this is exactly the same as in canoes - yes wallet and with that wallet you create a instance of a local web 3 gateway what that gateways gonna do initially and Simon's actions innocence with that wallet before deploying a calculator since now you have to provision it so oh my guess I forgot much of that so when you've compiled your service with the way to decide this target directory a calculator not Wasson file is generated and so we're just reading than that my code and passing it to your deploy function and so that should now deploy your calculator and create a confidence and with the way when you initialize your repository with the weight is in it then also generated this basic test of deploy so just make sure that after you deploy whatever services and not null actually insurance so that's not all we want we also want to be able to test for company their contract positive growth so let's well let's do that I test so how do we [Music] since first and then I'd like and then some [Music] the same thing or subtract multiply change what's happening so there we go so now we have full running and now let's actually test that we'll take the second side I'm sorry any questions so far oh yeah yeah so it like it's based on that it's very vital sorry spot yeah so in this so what this is worth 1 p.m. so within 50 secure on things are actually with me to entertain like which he wasn't about this [Music] turned out so once the results then write to standard out yeah hopefully for some deal so I wanted to now that I've explained a bit about how this happens I wanted to take a moment to try and show a another thing that we're trying to do so what this is doing is it's going to run the same computation wrote in rust except this one is going to be and it's the your sequences so the way so that this is one of this is to show how general-purpose huasi compete and how would actually make was he work to write a smart contract in basically in language so what's happening here is I've written a super basic function with the same stuff that we were just talking about but in C++ and so in the main method I'm doing exactly what I showed you in generated it leads from standardin and it deep oh it's once before and then whatever it commits it checks what method is being called it calls the appropriate method out of here and once you've actually figured out what methods be involved it calls that happen with the arguments are provided through standard it after if you do that again it works to standard out and so C and C++ the languages that compile to blossom and SDKs to equality and so that's exactly what so the exact steps show you so first we dialed using a velocity SDK which is probably a little less you gave for compiling to quasi compliant awesome second thing that we did was now file this file the way to wases expects was about speed it needs to have the idol which is the equivalent of in TV API it expects that to be baked into this one so this second step here we convert our base calculator lots of fun consume a service employable to us then how does that work well so that's super easy the rust rust has a library or modifying and so you take your traditional wasn't file those built and you can essentially craft on a ideal that I do we're going to build to a little list made all of these types of insulin but once that's done you now have a calculator doctor service and this is something that can actually run inside so to integration this last file which should look exactly like that Union test that we discussed for the rest of alternator where we configure instead of a yeah so now going back here so that hopefully showcases like how simple it is to write it any language that can be converted to blossom and a form so that's actually what we're hoping to do we have our tooling and stuff too mainly and easily with rust and Oasis was actually backwards compatible with solidity and Piper as well so this demo for C++ today we're going to actually at some point and aperture extended to support really anything we want to have yeah so I guess like the vision here is that right now it feels like a lot of things are pushing locked in to replace existing but the idea here is that existing able to remain largely the same a with minimal modification it should now be able to be so yeah so where is the asus now has at least a few SDKs such as that tooling that i we contradict our dev net is live and we are now along with that it'll be a bug bounty and so people receive prizes and soon in the next auditioner also or people that are interested in that so this competition is going to be totally open if you want more information like goes to our total college they work all summer and so for people that are able to also contribute as well see how nobody exciting so yeah this doings here for our website it so yeah so people have to need a lot of time to think help you finish building [Applause]
