in this video you're going to be building the Twitter clone with react native we're going to be adding web3 and blockchain Technology to it using the Phantom wallet and we're going to be using their deep links especially to build out a complete wallet connection and authentication inside the Twitter app you're going to be able to access all the data that is in connection with the user's Phantom wallet that is being connected and you will also see if they disconnect their wallets so with that said let's go ahead and get started alright let's get started with this tutorial and let's implement the Phantom wallet connection with deep links into Twitter let's go ahead and get started what I'm going to do is I'm going to go into my GitHub folder and then inside there I'm going to make the new folder which I will just name YouTube actually apparently I have one so that's nice so I'm going to go into that and then in here what I'm gonna do is now I'm going to run the clever programmer CLI tool all right so this will ask all right so this will ask for my email address and my name and then after that it will just ask for which project I want to build so because this is the Phantom Twitter I'm just going to select that and then hit return now this will go ahead and get the project set up for us in a way that is ready for us to work with and you will know that it's done once it opens up vs code automatically for you for me it actually opened on a different screen but that is pretty much the first step as you can see in order to install node modules and it has everything that I need so let's actually just run it real quick so I'm gonna go into that and let me actually empty out my terminal and what I'm gonna do is I'm gonna say yarn start and that should technically start up my Expo project all right now here I'm going to hit I and that will turn on my simulator powered by xcode because this is a react native project and it pulled up on you know a different screen but here it is and there you go that is the starter app and as you can see it already has some of the functionality in here so we can like undo tweet like a tweet comment stuff like that and then this will be the main button up here that we're going to be working with and then if you want to you can go ahead download um Expo go on your mobile phone on your iPhone and then just scan that QR code and it will pop up right in there I'll be using it later on and I'll show you a couple things because you will need a phone to run this project all right so with that said we're gonna go ahead and get started working with this app all right so first thing first we're going to be adding a couple files and we're going to be importing a few things so let's just go ahead and get started inside of my source components folder what I'm going to do is I'm going to go ahead and create a phantom. js file and I'm going to go ahead and import a couple of things so first off we're gonna need device device event emitter from a react native then we're gonna go with a style sheet and then whoops that became weird then we're going to be importing touch trouble opacity and these are the three things that we're importing from react native next up we're going to be using context later on so let's actually just import use context here and then we're going to be importing the Twitter context which by the way it should be already here yes contact JS so that's not that's nice so we're going to be importing this guy so that's there and we're gonna be importing it from one more folder above maybe not yeah context slash context yes from there and we're going to be importing an asset which is a star asset it's a star icon basically and we're going to be importing it from assets star so that's pretty much the importing in Phantom now next up we're going to be creating a react native component so let's actually just do it this way so that's nice and let me just space it out real quick so in here what we're going to be doing is right away Destructor state from use context yep just like that and then just pass in Twitter context in there so that should be good and let's just go ahead and use the touchable opacity that we imported and let's throw in the star icon that we had so that should in okay so this will be the component that will be triggering all the connection this connection and we will actually be changing whatever inside of app. js because right now it's just the star but we'll be basically changing into that so that's what we're preparing so that will show up later don't look at this in the app just yet so now we just want to test out what our state is so later we'll get there but before we go any further what I want to do is just go ahead and say con State equals one oops one and then here export state just to double check it should technically work but just to see and right now it doesn't work yet because we have to wrap it around we also have to connect the context to the app itself which we haven't done just yet but it will work in a little bit so I'll just leave this there this is ready the test here is ready so now what we're going to do is we're gonna move on to the next step which will include uh this testing so let's move on to creating Phantom containers so that's going to be a file just like this so we're going to say Phantom container dot Js and just like that I'm going to go ahead create a react native component and here we're going to be importing Phantom the file that we just created a minute ago and we're going to be importing Twitter provider and now you already see where I'm going with this so we're gonna be wrapping the Twitter provider around the Phantom components so now if I'm being correct oh we're actually not rendering it out yet so hold on a second now what we're going to be doing is we're going to go into app.
js because now we have to render out the Phantom container and now we're gonna remove the star that right there and we're gonna say Phantom container boom and pass in these props that are in here if you if it didn't import automatically for you then forget to import it up here the Phantom container component so that has to be done and now as you can see in the call in the console the one shows up so that is perfect now I'm going to go ahead and remove it before I forget it to remove state from Context and here in Phantom JS I'll remove that console again as well so at this point we made two components that will be used later on and we will be adding stuff to it but right now we're making good progress let's move on to the next step alright so before we go ahead and create a use effect in our context what we're going to do is we're just going to make sure that we are passing down the props to a fandom container and actually we can get rid of that line where that's not useful anymore and next up what we're gonna do is we actually want to go ahead and catch that catch those prompts so we're going to say props up here as in like that props like that and now what we're gonna do is pass those down to Phantom itself props yes and we also want to be able to catch those right here and oops that was left there and we're going to be catching these later here as well so this is the later going to be useful we will get back to it so Phantom and Phantom container that's it app. js that's it so next up what we're gonna be is inside of here in the context we will be importing use effect apparently that doesn't come with the package itself that's fine we can just add it and we're gonna go ahead and create a state in here which will be a deep link State this is where we're going to be storing the Deep links that we're going to be working with next up we're going to have a use effect which will get started with a self-executing function so what we're going to be doing is we're going to create a check we're going to create a function that is that we will call check deep link and this will be an async function and what it will do is what it will do is it will look for all the deeplings that we're going to be throwing and passing around in the app so we're gonna go ahead and grab the initial link but for that we actually have to import another package so we're going to be importing everything as Expo linking from a project that is called Expo Dash so that's nice and so here we're going to say await Expo linking dot get initial URL like that and that's a function and if that URL exists then we're going to set a save that in our use date and because it's a self-executing function that's how it's gonna that's how we're gonna be closing it off and obviously we don't we just want to let this set deep link run by itself like that so we're going to be passing the await Expo linking get initially let's actually take a look at what we will be getting so I'm just gonna console log it right up here and let's see what this is and let me just put it like that and let's see what we get so basically this is my local address of the app where it's running so that later on it will change but for now that's the link all right and next up we're going to be doing is still inside of this use effect actually let me just get rid of that and what we're going to be doing is we're just going to say we're going to have an event listener that will make sure that every time this deep link changes we'll be able to do certain things so we're just going to add an event listener so Expo linking dot add event listener and we're gonna say URL and if the URL changes we're just going to update that link in our estate and we also don't want to make we also want to make sure that whenever we're done with the app we don't just keep creating all these event listeners oops the weird things are happening we're going to eventually remove it so it doesn't keep doesn't clog up the memory so that's basically this step let's go on to the next one okay now before we move anywhere further what we're going to be doing is inside of phantom we're gonna go ahead and destructure deep link from use context and I'm passing Twitter context so we're just gonna go ahead and grab the Deep link state from the context all right so now what we're going to be doing is we're going to go ahead and actually connect to the Phantom wallet on our phone so that's going to be an exciting one but to get that going we will need a couple of things so first of all we're going to be importing use crypto which is a custom hook that we made for you and you are free to use so here it is nice and is in the utils folder so it comes with the project and we have a couple of encryption algorithms in there so we're going to be importing those things so we're going to import we're gonna destructure in code decode encrypt decrypt and get key pair and actually this is the wrong file I just did these in the Phantom JS this was supposed to be done in contact let me just paste these lines right in here all right we're good and right here I'm gonna go ahead and create a deck key pair you state which is going to start out as null so the initial value is going to be null all right now we're gonna go ahead and create a use effect and all it will do is set the depth key pair to whatever the return value is of get key pair oh the get key pair and we're just gonna run once and that's pretty much it now next up we're going to have the connect function before we actually go on let me just reload this app just to see if everything's yes we're good all right so now what we're going to do is we're finally getting started with the connect function so this is where deep linking will actually make a big big play so this is going to be an async function so we're going to go ahead and create a variable called params because we're going to now construct the Deep link itself or actually the link the link inside of the Phantom app to connect the wallet so we're gonna have a new URL search params thingy and inside there in an object without the P we're gonna go ahead and say dap encryption public key and that'll be whatever the adapt key pair adapt key pairs public key we're gonna go ahead and encode that with a certain certain algorithm we're going to specify that we're gonna be on the devnet and the app URL is https phantom. app and then we're going to go ahead and create a redirect link so what's going to happen is while you're in your Twitter app it's going to drop redirect you to Phantom so it's basically it's gonna it's gonna open Phantom app you will hit connect and then it will throw you right back into Twitter so that's what we're going to be specifying now and that is coming from Expo linking and we're going to go ahead create a URL and we're going to say on connect oops so what that will do is actually get the current URL of the app itself so it will be whatever URL and then slash on connect however this URL part that I'm highlighting right there that is specific to my phone it is different for everybody so we want to make it Dynamic that's why we're using this Library all right so that is our parameters however URL needs a beginning so we're gonna go ahead and construct that so we're gonna say constantly we're out equals and notice how I'm using backticks because we're going to be we're going to be using tablet literals here so we're going to say Phantom dot app slash UL slash V1 slash connect and now the params come into play so we're gonna say params dot two string and that's where the URL ends so this is going to be the Deep link that we're going to be passing into we're already heading to connect all right and now we're going to say Expo linking open URL which will actually go ahead and open the link itself and just pass in url that's pretty much it all right so now let's take a look at this inside of our okay so that itself is not being called just yet as you can see it's unused so we're gonna go ahead and connect it in just a second so what we're going to do is go ahead and say we're gonna be passing this we're going to be exporting it from the provider and we're gonna go into phantom.
js again and now we're gonna go ahead and destructure connect from the context and we're gonna say on press connect all right so now when we hit this little star icon up here we should be redirected over to fandom let me reload just so it rebuilds itself and I'm gonna hit it and there you go now it's taking me over to this Phantom app now here's where the limitation of a simulator comes in it is not able to download the Phantom app from from the App Store so now we cannot actually test it oops who the heck uses Bing anyways so I'm gonna go back to my Expo app this is not the test is not going to go through on this iPhone on this test iPhone so what I'm going to do is I'm going to connect my own iPhone and then show it yeah show you it right there all right so let's take a look at this connect functionality that we just implemented so what I'm going to do is I'm just going to go ahead reload this app and let's see what we got so I'm gonna go hit this connect button there you go there's the URL by the way I'm a console logging a couple things so here's the URL that we're generating and then I just want to double check that the Deep link is working and as you can see in the simulator you cannot run the Phantom map you cannot install it on it so we're gonna need our real phone so here's where this comes to put here's where this guy comes in so I'm going to go ahead let's play that QR code again I think I'll make this a terminal a little bit bigger alright so let's take a look at what do we got so I'm gonna go ahead open that in Expo it takes a second to load it up but there it is so here is our app and so I'm gonna just go hit connect and I'm gonna go connect again and there you go the Deep link has changed now I'm suspecting that later on this might cause error see uh I don't exactly remember but there it is the app is working let's move on to the next step all right so in this next step what I'm gonna do is I will try to get the Phantom wallet Pub uh what is it the wallet address out of all the metadata that you saw it's like a bunch of cryptic stuff I just want to really make sure that we're actually connected so what I'm going to do is I'm going to have a use effect and that use effect first of all it will check that I want to know if a deep link exists so we're just gonna Fail-Safe it here return oh like that yeah so it's not multiple all right now we're gonna go ahead and actually import another thing so I'm gonna go ahead and say import use URL from utils and use URL so this is again another thing that we're just giving to that's fine and then we're gonna go ahead and import this NaCl thing and we're gonna go ahead and import cluster API URL and key from the Solana web3js libraries all right and before we go move on let me actually just create a use State snippet for the Phantom wallet public key and we don't specify the initial value and actually for the depth keeper I'm just going to say get key pair and then whatever that returns that's going to be the value and as you can see don't worry about the error holy cow so here's the Deep link that we're gonna get back I wouldn't get the value of that URL so what I'm going to say is wherever I'm setting the Deep link here Ah that's exactly where I'm gonna put it on second URL so it's going to be URL dot URL I mean my bad there because it's returning an object I just want to make sure I but save that you are a property okay so now that we're not gonna need that actually let me just get rid of all the console logs okay good I don't like all that stuff being stored at all times all right so let's import a couple things so I'm gonna go ahead and destructure params get parameter or I mean params and get param and then check path functions from the use URL hook after I pass in the Deep link so all this does is basically it just gets all these all these things feel free to check out the code but it basically just picks the URL apart into little parts all right now I'm going to go ahead and say now we're gonna we're gonna do based on what oh based on where we are in the app based on the Deep link we're gonna be doing two different or we're going to be doing separate different things so we're gonna say we're just gonna go ahead and check path and if it is the unconnect we're in the unconnect path we're gonna say handle connect and we're not that if we're not there we're just gonna handle this now what are these functions so we're gonna go ahead and create those right now so we're gonna say handle connect right there and we're gonna have a handle disconnect as well so I'm gonna copy that paste it there just so it doesn't freak out completely actually we're not going to need the return clause and we will be running this every time deep link changes so I believe at this point the app should I have no errors yes that should be good all right so let's go ahead for now I'm just gonna put in a console log here that's just gonna say disconnect and then we're gonna change it later on but handle connect this is where we're gonna go ahead and decrypt everything the wallet has sent back again it's like a lot of like cryptic stuff so we're gonna say cons shared secret dab this is gonna be like a variable and that'll be coming from this nacr box before method after with pass in decode get param and then Phantom encryption public key right alongside of the depth key pair not secret key so this is how we're gonna cut back the the wallet address all right and now we're gonna go ahead and decrypt using all that data so we're gonna say get param we're gonna grab the data back and then nons so using these three items we're gonna go ahead and get the connect data and that will actually contain the public key so we're going to go ahead and save that in our Phantom wallet public Key State let me just paste it there actually let's underscore so let's actually just take a look at what this connect data is so right now we're running on the disconnect pad that is completely fine but because of my phone let me grab the QR code and so I'm going to connect and we cannot find get param oh that's because there's a typo all right restart reload nice connect again I think I put the public key the wrong place yeah I don't know what I was thinking there but it's a little bit surely we're fixing all right let's see what do we got so we got back this this big boy big boy object so this contains the public key and if we are if I'm going back into my Phantom wallet you see how it starts with 5wfk whatever and then ends with smtf that's exactly what we're getting so this is the proof that this wallet is actually connected to my app so with that said let's move on to this connection and with that the app is done all right now the last thing that we're going to be doing is we're going to disconnect from the wallet and with that this application is complete so for that we will actually need three new use effects so I'm just gonna go ahead and add them right here one is going to be session the other is going to be is connected and then shared secret so here it is all right now what we're going to be doing is when inside handle connect I'm going to go ahead and just update the set shirt secret state with that and so we're going to be storing it that will be necessary for a disconnection but yeah there it is now actually what I'm going to be doing is I'm just going to change this to that so every time yeah so it's just going to run the handle connect and that's it interesting anyways and what I'm going to be doing is when and actually remove this disconnect or handle disconnect and so right here under set a public Phantom wallet public key I'm gonna say set session and from the same connect data that we get the public key from I'm going to go ahead and save the session in there again that's going to be necessary to end the session itself and the next I'm going to say set is connected to true and yeah as you can see here the initial value is going to be false moving on and now I'm gonna add a new function down here somewhere under connect there's a p for no reason exploding I don't know what happened there I somehow grabbed the P out of there it's whatever all right so I'm going to say disconnect const disconnect okay typing seems okay and this is going to be an async function as well so Aster is doing there nice all right so now what I'm saying now what I'm gonna say is I'm gonna say const payload and I'm going to destruct your session and we're gonna get the we're gonna go ahead and grab grab the non and the encrypted payload make an array out of it and we're going to just say encrypt and then we're going to pass in the session itself so that or the destructured path destructured session which is the payload and the shared secret so again this is the cryptic stuff don't worry about it and now we're gonna say const params and we're gonna say new URL search params like that and we're going to create an object in there let me scroll down a little bit we can have adapt encryption public key and we're going to go ahead and encode the public key I'm going to pass in the nons and encode it and we're going to specify the redirect link which is going to be Expo linking not quick URL and we're gonna pass in the on disconnect whoops so this will make sure that here we're not going to run this functionality on it because the Deep link will change however because this because the path is not going to be connect or on connect we're not going to run this so we're just preventing a bunch of errors with this and we're gonna pass in the payload and that is pretty much our link now we're gonna say set is connected false and we're gonna go ahead and open this link and this is going to be Phantom app ulv1 but instead of connect so you can literally just copy this from there we're going to say disconnect and then pass the params afterwards and that should be it now right here I'm going to also pass the disconnect function and the is connected State and that is because inside phantom.