Building a native multi-chain wallet on top of the chromium stack - Anirudha Bose (Brave Browser)

ETHMilan 2023 · 20 Dec 2023 · 19:15

Anirudha Bose, a staff engineer at Brave, shares how the team built Brave Wallet natively inside the Chromium-based browser instead of as a JavaScript extension. The talk covers the C++ architecture, a wallet written without web3.js or ethers.js, browser-level permissions for window.ethereum, the rule of two for untrusted input, sandboxed frames for NFTs and hardware wallets, and built-in IPFS support.

Speakers

Topics

From ETHMilan 2023, hosted by ETH Milan.

Transcript

good morning everyone uh thank you for coming to my talk uh for those who are watching a recording it's uh 10:00 a.m. and 10:00 a.m. is pretty early in bare market so thank you uh my name is Ani uh I'm a Staff engineer at Brave quick show of hands how many use Brave okay pretty much everyone uh so uh I work on three initiatives at Brave and that uh that's most notably a crypto wallet we call it Brave wallet which is uh part of the browser and I'm going to share our

experience building this product and how we manag to teach a beast like Chrome chromium to speak uh web 3 so for those who uh do not know about Brave wallet just open your browser and there would be a wallet icon in the toolbar if you click on it it should show you Brave wallet if you have set it up uh pretty standard and we are it's part of the browser and we're shipping this um uh in uh in the browser itself so any device that has Brave browser

installed has Brave wallet so we are make making it available to some 50 or 60 million users uh of Brave and it's native uh and by that I mean it's it's not a JavaScript wallet uh it's it's um most of uh crypto funds fortunately or unfortunately are locked in uh extension wallets written in JavaScript and that's not a good idea for a number of reasons uh um but before I delve into that I want to quickly uh explain you what it

means to be a native wallet this is going to be the most technical slide in the whole presentation so just bear with me so I want to explain our the browser's architecture um it's a browser that's built on top of the chromium stack um which means there's a main browser process that does a lot of fancy stuff and uh there are renderer processes which are sandboxed and they run something called blink blink is a renderer engine uh it's pretty popular if most browsers use this engine maybe apart apart from

Firefox so uh Brave core is uh kind of sitting uh in the middle of all this orchestrating stuff uh Brave wallet is just part of this Brave core so it is able to compute stuff like talk to the rpcs uh build a build your balances create transactions and able to um pass it on to the renderer which runs blink so that's uh written in C++ and those are C++ objects what we then do is wrap those objects in uh into a V8 object V8 is the JavaScript engine in

Brave and we make it available to the client uh the front end so um in Brave browser uh the the wallet that you saw in the previous slide that was react and it was using uh accessing JavaScript objects but behind the scenes it is actually a a C++ object and if you call any function that's running C++ not JavaScript so that's an illusion uh works for us okay why are extension wallets problematic because they're heavy if if you have a JavaScript based

uh extension wallet you're probably running tons and like hundreds of these npm packages bloating your uh your entire software so you don't know if they have security issues and well you also inherit not just security issues but also design flaws uh architectural constraints and so on so they are heavy and we also have a diversity problem so there is A diversity problem in ethereum which is that uh some 83% of ethereum nodes run

G uh actually I think the slide is outdated because it says 8% open ethereum and open ethereum was uh like it it's not maintained anymore as of last year so the problem is a lot worse now uh with execution clients consensus clients it's okay because there's a lot of uh um there's a lot of effort put behind ensuring diversity but it's still still not that great the the most used client is prism my case is that there is a similar problem in wallets as well a vast majority of extension wallets or rather all extension wallets they use the same

set of JavaScript dependencies and that's ether's JS web3js and so so on um it it's good because it makes development easy but you also repeat the same mistakes over and over again so what is it that we do we implemented uh the core functionality of the wallet from scratch um completely free of web3js and ethers JS these are great libraries by the way um but uh we didn't want to or rather we uh it is more of a security principle not to use not to rely on third party npm packages when you don't have

to um to my knowledge that makes us uh probably the only op Source C++ client web 3 client uh that's also actively maintained you can say open ethereum is another one but it's not maintained anymore and it sits uh right in inside the browser score so it's deeply integrated into our stack it's crossplatform so if you can compile Brave so it could be your uh Raspberry Pi or your Mac OS anything Windows uh you would have access to the client so we do not rely on architecture or platform specific

features and finally um it is uh lighter on CPU and memory footprint compared to an extension wallet if you if you know how that works um they use uh an extra browser process uh to run all the time okay I want to talk about permissions because this is one area where I think uh uh we do something cool and I want to talk about it so in your regular browsing experience just open Google Maps it's going to ask

you for location permission you go to a travel website it's going to ask you for notifications and on uh Zoom it's going to open your camera and the browser has certain uh security features that prevents these websites from accessing your camera if you have not granted the permission that's great so we used it so just like your location camera microphone some websites even access motion sensors I don't know why but you have these uh a lot of these permissions notifications we created one for ethereum so if you block access to

ethereum for a particular site it cannot uh it cannot um do anything with window window. ethereum and this is enforced at the browser level not by JavaScript which is which is good um just like any other permissions um we can also make it temporal so when you grant when you connect a Dap Grant permission uh to access window.

ethereum uh interact with the wallet you can also say clear this permission in uh yeah once I close the site or in 24 hours and so on this is great because um um daps can still sometimes access like know your address and stuff like that maybe create uh false uh message signing requests if you don't want them to do that once you have closed the website you have a way and this is my personal favorite uh there's uh permission isolation imagine you're on Unis Swap and uh you're

connected to okay uh optimism and on another dap Let's uh say curve or 1 in you can connect to another Network they are talking to the same wallet but they're using different networks uh it's it's not that fancy but uh it's a useful feature you want to you don't want to switch Network on one dab and have it reflected on another dab and this isolation is guaranteed by the browser again not through um not in a phony way through JavaScript uh what about untrusted

content so pretty much anything coming from the outside world so that would include images HTML content XML uh responses from third party apis they are all untrusted content they can um we don't want them to impact your wallet or crash your browser and so on so we we have a solution for that uh at Brave so what we do at Brave is we follow something that we call a rule of two um rule of two says that uh it's it's just a security hygiene that's it the rule says you can pick no more than two of

three things one untrustworthy input so that would be uh the NFD uh image that you are rendering on your screen U or third party API responses two unsafe uh language implementation so anything you you write in C++ or C that would be that's considered unsafe so what's safe safe rust is safe uh so whenever we talk to apis um it can even be RPC uh uh like nodes gu node we make sure that the response is sanitized in Rust and it's something that is uh uh

valid Jen most of these Json responses are not really valid because numbers in most blockchains are pretty large and they are not standard um numbers uh in JavaScript and third uh we don't want to do things that require high privilege um I you can do that but not uh all of them together high privilege is something when you do it in the main browser process so we don't want uh some piece of uh cryptography to uh crash your browser for example and if you have a coincidence of

these three evil things uh Doom we don't do this so that's our rule of two it's a general rule for uh that you can also apply to other softwares we have another interesting piece of technology known as untrusted frames so whenever we um show an nft for example this nft nfts could have any media content it could be arbitrary we make sure that it's rendered within what we call an untrusted frame it's it's basically a tiny sandbox that limits the kind of things that can

be done within that frame so uh for uh security purpose you can have a malicious SVG that's doing something we also do that for connecting with Hardware wallets so um Hardware wallets actually use um vender specific communication libraries um so maybe a JavaScript npn package and they can be compromised we don't want uh Brave users worldwide to be impacted by a vendor library of a hardware wallet so what do we do what we do is we make sure that the communication with the device

with the hardware wallet happens within an untrusted frame so this Library that's normally used uh uh to talk over USB or Bluetooth we make sure that it has the protections of this unrusted Frame uh some other features uh some of my little personal favorites things outside wallets but you would find them sprinkled around in the user experience of browsing the reason I want to talk about it is that um stuff like this like when you visit an ipfs

URL uh for users it feels as if it's a regular website uh apart from some fancy logos but it it feels native uh you don't have to know that it's a web3 website ipfs hosted also works with ipns uh names uh did you know that you could run an ipfs node uh in Brave so you could let Brave spin up and manage a local ipfs node and then you can do some interesting stuff with it like um importing files uh pinning content to your local node pinning your nfts so

on it's not that um I don't know if you can see the numbers but it's not that heavy it's pretty light for those who who have used Brave a lot you may have seen those uh uh purple ipfs buttons in the URL and what what this is about is if there is an ipfs hosted version available for this URL uh what we do is we show this button and then you can uh view it in a Gateway the cool thing is if you have a local node running it's going to open it in your Local Host so this image the one at

the bottom that's being served through the Local Host that's the ipfs uh filecoin website pretty cool so uh I want to wrap up here uh and uh talk a little bit about uh what we are doing and why we are doing and how we are doing so um a browser a browser is something that every internet user has it's the most sophisticated uh piece of software that you have um on your device right now you use it every day and we have good distribution we have we reach a lot of users and we want all of

them each of them to have the option to use crypto in a native way maybe you don't even know that you're using web3 things uh that's how we want to build our product we uh uh have an expertise building browsers um so we want to leverage that we want to leverage our browser to offer experiences is that um feel a bit more native and uh don't get in the way uh and uh a lot of those things may not be possible with uh extension wallets and finally uh we uh every product that we build uh we try to add some character and um make it

maximally private uh even if it means it's going to hamstring you and uh we want it to reflect uh The Wider culture we have at Brave and uh one of that one of them is um Open Source by the way most uh or a lot of crypto wallets out there are not open source um and I'm not just talking about wallets that do not use permissive licenses but they just don't have uh a public publicly viewable source code Robin OD wallet so yeah um that's

it thank you all for listening and yeah see you around

Related talks