Mobile Wallet Building Workshop

ETHBarcelona Talks · July 2024 · 23:25

Vanes Mercado, a developer advocate at WalletConnect, leads a workshop on building mobile wallets with the web3 wallet SDK. It begins with WalletConnect as a messaging layer and standard for connecting wallets and decentralized applications, then covers pairing, session proposals and namespaces, and walks through the project structure and code in a repository that participants can follow along with.

Speakers

Topics

From ETHBarcelona Talks; community: ETH Barcelona.

Transcript

hello hello good afternoon everyone congrats you made it to the last day of the finals and um yeah thanks for coming in and checking out my workshop today so my name is vaness I'm a developer Advocate at wallet connect so today what we're going to be going over is how to use a web through wallet STK but before we get kind of jump in um let's talk a little bit about what wallet connect is wallet connect is a messaging layer and it's a standard to connect wallets and dabs together web3 wallet is the SD that I'll be demoing today and it's for wallets to connect to DS so it's a super easy tool for wallet developers if you're looking

to get started all right so before we jump in code I kind of want to have an idea if anyone really understands how wallets and dabs connect together by show of hands okay a couple like two or three um so let me explain for the broad audience for those who like the formal definitions you can go ahead and read the screen this is directly from our glossery but I like to explain this a little little differently you can think of when a dab and a wallet want to connect think of it as there's two friends and they want to play a computer game and the first thing they need to do is they need to be connected onto the same network and once they're on once

they're on this network then they can actually begin playing games and so this is how we want to think about pairing and sessions pairing is the initial connection to the network that they're both connected on and then anytime a Dap wants to send a proposal wants to send a request to send or sign a transaction this is where the session gets initiated and the first thing that you're going to see is we have this object called the session proposal and the session proposal gets initiated by the D and it sends this object to the wallet and basically what it's asking is hey here are a list of events a list of methods and a list of chains that I want

to connect to and then the wallet gets to decide whether or not they want to accept or reject that connection these are probably one of the more important things you want to look at when you're thinking about session proposals and it's these objects called name spaces you'll notice that there's a name space and an optional name space and to give you kind of an idea of how they work let's pretend that I have a Dap and I want whatever wallet I connect to they always have to be connected with eth mainnet and so we'll pass that through the required and then I'm also passing in a an array of the methods that I also want to have access to and I'll do the same thing

with chains as you can see EIP pull in one as well as the events that I also want to be using and let's say that I want to have an optional chain that maybe I want to connect to corly and so I want to I don't want to not allow a wallet to connect to my dab if they don't support poorly and so this is why I would send it in the optional versus the required because if a wallet doesn't accept everything in the required the connection just won't work and so I've started a a demo wallet repo so so if you kind of want to follow along you can scan that QR code and we're going to just kind of go through

the different file structure so that you can um we'll just walk through all the different functions because there's a lot of code and I don't want to have everyone try to start coding live with me but you'll have the repo ready and you can also run it locally as well so I'll give everyone a sec to get that QR code what we're going to be looking at is this project structure um we're going to be focusing on these four files all the other ones we'll touch briefly on what they are but basically they're just a bunch of libraries and utils that we'll

use to make our dab development or excuse me our wallet development a lot faster cool so let's jump into theod all right so we're going to kind of start with um we can just kind of take a look at the libraries here there's really nothing crazy happening um yeah here's all the libraries and so we already have these all hardcoded a bunch of different chains that you'll have available that a little larger too so let's jump into utils this is

where we're going to basically kind of begin your project here so we'll be um importing here some of the different um types and core as well from the wallet connect to library and what we want to do here is we want to create our our wallet first and so we'll do that by calling create or restore EIP 155 wallet if you're curious on what this does you can jump in the code and kind of see how that works we're going to pass in this project ID um I did hardcode it in but you probably shouldn't do this although it's already going to live on the front end so um there are other ways that you can make sure you hide this in a react native

application and so we first begin with initializing bu wallet we're going to pass in that core instance that we just created as well as some metadata hard to read oh make it larger oh yes more now it's okay thank you perfect yeah yeah yeah no problem all right so okay we've initialized our web through wallet STK and so now we just want to initialize it and so we're just kind of using some JavaScript um to get this started and this is kind of really all will be using wallet connect UTS 4 and the main stuff really happens here in app.

TSX so I've kind of divided the Imports in different sections so we can kind of follow along what's going on here and the first couple of imports are going to be the react hooks react native components styling texting coding library and Expo uh component registration we're using this um component called Barcode Scanner so that we can take pictures of the QR code um for these we are going to be importing some wallet connects types utils and a compatibility layer to use react native we'll then be using a custom wallet connect initialization hook and utils which these were the ones we just

created um in that last file and then lastly we'll also be importing a library and also some custom models that we'll go through in a bit I don't want to go through all the use State stuff because I feel like that kind of gets boring and repetitive I rather just go into the functions and kind of see how this works so the first thing we're going to want to do so we think back um earlier I talked about this pairing in the session that happens and so whenever the you see the QR code the wallet connect QR code and your phone scans it it creates a pair and so once this pair is created then we still need to receive that session proposal the wallet needs to receive that session proposal and so we

can either accept or reject that so let's take a look at what the function for handle accept looks like and we got a lot of stuff going on so so we'll break it down firstly we kind of want to make sure that there's a current proposal available to us and what we're doing here is we're essentially pulling out the name spaces um from this proposal so if you remember we have that required and that optional name space so we're going to first create an object um outside and then we're going to Loop through the required as well as the optional name spaces and so we're going to grab the accounts the chains as well as the events and

methods so that's what we're doing up here so we're just going through the required Nam space object we'll minimize that and we're doing the same thing with the optional name spaces down here is going to be the approved name spaces so I mentioned that the wallet actually has the wallet will always have a list of what chains they're going to accept because they're not going to just accept everything um and this is a u build tool that we have at wallet connect so that it helps wallets figure out how to actually pass these name spaces so that this like connection doesn't break and so what I'm passing in here is my wallet I only want my wallet to

accept eth meet so I've done that here on line 96 and the methods that I want to also allow will be e send transaction and personal sign I'll do the same thing with events chain changed accounts changed and then for the accounts um I'll be grabbing the account from the wallet and we do that up here so when we go through that proposal we'll also be receiving the wallet address when we get that as well and the same thing happens with optional name spaces we've got the um push the chain in the account and we computate it together

all right so once we have the approved name spaces then we're going to call we through wallet we're going to call approved session so we call this function and we pass in two things we're going to pass in the ID and the ID we're getting from The Proposal so we pull it out from like our current proposal up here and then we're setting name spaces to approved name spaces because we want to make sure that we're using that util and then we're going to be setting some things for for the UI so we'll want to get rid of the model um we'll want to set the proposal to undefined and then because it's successful we'll set that to tr let's look at what happens when we're

going to reject this connection so we'll take the current proposal we grab the ID and then we call the reject session function on web 3 wallet also pass in the ID but this this time we're passing in this reason and it's user rejected methods and this get S error is a UIL that we're importing up here cool so once we've got handle reject we've got handle accept um we're going to want to take it figure out like what are we how are we passing in that URI that we're scanning and so we can take a look at handle bar code scanned

and we can see where this is getting called um down here in this component so whenever we scan it we're going to grab the URI and then we're going to call this pair function let's take a look at what this pair function does what pair is going to do it's going to create a um it's going to call Web three wallet pair and it's going to then create that function that I mentioned when the QR code gets scanned by the wallet and then once we have that pairing session we can then actually add sessions to it oh sorry one thing I did miss when

you get that session proposal um well I guess not nothing really we have to cover sorry so from here if having some issues with my [Music] simulator but in the meantime I can kind of show you what this should look like so let's

so I'll pause it for a moment here um so when we this UI like what this project already has um when you run it you're going to initially see your e address and we got this by creating the web fre wallet and then we're going to click on scan QR code and this is that component that will open up your camera and so we'll scan really any wallet connect QR code you can scan and this is once you scan it creates that pairing and then once it creates that pairing it then creates this session proposal and this is what we're seeing here on this model here so you'll see that it's requesting the chains which is main the methods that I want to be getting permissions to as well as the

events and an accept or a cancel button and so we're going to hit accept and you'll sit on on the dab side but on the dab side it'll get connected and we can also then um yeah once we're connected we'll be able to disconnect and that is let's see cool all right so we kind of can jump through this UI as well um let me actually just open this up on simulator does somebody know the command

this is my first time doing react native so so does anyone know the command to get these MPS Expo start so that's for to get it on my phone but do you know what which one it is you can do it oh I can still do that one yeah and then once it thanks all right while that's floating we can take a look at uh the other models here so let's take a look at that modal pairing and let's see what we're getting um actually let's go back to app really quick um because we're going to be passing down some functions so we

should probably take a look at these first um we're going to be passing down handle accept which is what we went over handle reject modal visible set modal visible and the current proposal so having that information um we can see what's going on here so we're just grabbing everything from current proposal that we passed down and we're going to just grab the name URL methods events chains and icon and then we're going to just pass it through so that you can see that modal that comes up and you get the option to either accept or reject

okay still loading saying opening project okay uh yeah and we've got our styling down there and let's take a look at the sign model um for an example while we have this loading um I'm going to show you what this looks like on a web wallet but our example is on a react native wallet but you'll kind of get the gist of how this works all right we're getting

somewhere nothing like live demos right guys WI guess we can go back to the code while that's loading what I've kind of demonstrated

so far in a way is um connecting your wallet to your dab which is we do that by going through that session proposal and accepting or rejecting that now when you want to do something more than just connect like sign a transaction or send a transaction what we want to do is we want to then send a session request and this is what we'll be using the sign model for let's look at appjs and kind of see what are the functions that are getting down uh pass down to sign modal we have sign modal visible this is just a prop um we're setting it we're passing in

request event data and then request session Okay cool so when we pass a request event this is very similar to rec session proposal but what's going to change here is that it's actually going to be letting you know what method is actually going to be used and so we'll just basically grab all that information similar to what we did with the pairing model with the current proposal this time we're using this request event and so we'll take all those cons and then we'll be passing them in into our functions either on improve or on

reject and so what we do on on approve it's kind of pretty simple we take the response um and the response is going to be the request event and we're calling this approved EIP 155 requests and this is something that we're also sorry something that we're also going to be importing from our utils so let's take a look at what that's doing all right lots happening here again similar to the other ones we're always going to be grabbing those prams and ID because we grab the name spaces from pams and ID is something we

generally pass to our function um when they're when we're approving or when we're rejecting a session and so a lot of conss here but basically these are going to be all the different methods that are available to you and depending on which one we're going to be getting um we'll then grab the signature by calling wallet do doign transaction and then we'll pass in that sign transaction which is coming from the request earlier that we passed in so it's that s request all right hopefully that will load

eventually but let's go back to the code and let's see that's kind of all I got everyone I was supposed to show you a demo but um we're kind of having some internet issues here and nothing's really loading for me but if you're able to clone that repo and if you run it locally uh let me show you how you can also get a project ID in the meantime this is what it looks like once it's loaded so if you go into projects

you'll have the ability to just create a new project you can call this e Barcelona okay let's just pretend it was created um the project information will be in

here and what's really cool with this is that you get access to a lot of different metrics and so we're actually currently building out our UI and adding a lot of other features to our Cloud platform so if you have any suggestions or things that you'd like to see definitely let us know um because we are in the planning portion of it and so yeah all the user feedback is very welcome um that's kind of all about I've got everyone sorry you couldn't do the full like are there any questions cool well glad to give your time back to you it's a Friday so have a

great rest of your afternoon everyone thank [Applause] you