Quick Start
The goal of this tutorial is to show the minimal approach to connect Wallet Connect (and Stacks) with your app.
We'll take you through the following steps:
Create React app
Create the Wallet Connect client
Establish a Wallet Connect Session
Make wallet requests via Wallet Connect
Create React app
Create a React project
npx create-react-app webapp cd webapp yarn startClear the contents of
src/App.css- remove everything, leave empty file.Remove everything "unnecessary" from
src/App.js, the end result should look like this:import './App.css'; function App() { return ( <div>foo</div> ); } export default App;
Create the Wallet Connect client
Install WalletConnect dependencies
Setup Wallet Connect client
We want to store the Client "globally" and set it up as soon as possible, so:
Open
src/App.jsand import dependencies:Add local state variable to
Appcomponent like this:Now use effect hook to initialize the Client:
Establish a Wallet Connect Session
We have the Client ready, now we need to connect our app to our wallet - establish a Session.
We do that by providing selected Network's chainID to Wallet Connect client.
ChainID is in a form of a string containing network name and a number, ex.
foo:123. The Stacks chain ID is defined on ChainAgnostic.In case of Stacks, the chain IDs you're interested in are:
In case of Bitcoin, the chain IDs are as follows:
Add it to src/App.js after the imports:
Create local state that will hold our selected Chain and our Session:
Add empty connection handler:
Handle our Networks (chainIDs) in UI:
Replace the
returnof our App component like this:Make it questionably prettier, replace
src/App.csswith:Handle connection, edit the
handleConnectfunction:Reset any previously set chains with:
Prior to connecting our app with the wallet, we need to know a couple of things:
what are the supported methods?
what are the supported events?
what are the supported chains?
Chains are simple - it's either Stacks or
Bip122for Bitcoin, so we simply pass the selected chain (full chainID). Events and methods are wallet dependent.Our dummy wallet doesn't support events and supports only 4 example methods for Stacks and 1 method for Bitcoin:
stacks_signMessage - for signing a message with wallet users private key
stacks_stxTransfer - simple STX transfer
stacks_contractCall - contract call (with post conditions)
stacks_contractDeploy - contract deploy
bitcoin_btcTransfer - simple BTC transfer
See API reference for complete list.
We pass these method names as array and keep
eventsempty - They must match what the wallet supports.In our case it looks like this:
and for Bitcoin chain, it looks like this:
We can use the returned uri directly, but it's easier (preferred) to use Wallet Connect QR Code modal to establish connection:
Import the QRCodeModal:
And handle it in handleConnect:
Wait for the wallet to approve your app, save the session, chain, close the modal:
The full handleConnect should look like this:
Make some wallet calls via Wallet Connect
At this point, when you click connect - you should be presented with Wallet Connect QR Code modal.
If you scan the QR code with Xverse wallet, you will see a prompt to approve the connection request.
If you scan the QR code with the Xverse wallet, it'll show the list of methods we've passed. Approve it and you'll see the connect buttons go away - indicating there's nothing to connect to anymore.
Show available methods depending on the selected chain, add this to
src/App.jsrender method:Add the missing handlers:
We'll cover each handler individually building up complexity.
You can read more about available Stacks transactions here.
A little theory:
Wallet Connect acts as RPC layer between our App and the Wallet.
The Wallet defines RPC protocol (methods, events, etc.) and performs all of the operations.
The App is just a client and simply sends some requests.
So - everything we do is RPC-like request that goes through "RPC client" like this:
The
requestitself is heavily dependent on what Wallet requires from us!We'll also need an
addressand we can get it from thesessionlike this:Another thing is Wallet Connect as a communication layer has some limitations we have to deal with - it uses JSON serialization internally. Because of this,
ClarityValuesthat utilizebigintwill not work out of the box.For that reason we need a hack to allow
bigintto be serialized as JSON. Add this to yoursrc/App.js(after imports):If you use TypeScript, you can do it like this:
Add some prerequisites:
Import Stacks dependencies:
Set up results box, add this to our
src/App.js:Result state in the beginning:
And render part:
Sign Message:
The idea is that we send a
messageto the wallet and it sends back a cryptographicsignature(signs ourmessagewith aprivate key).Edit
handleSignMessagefunction and set it's contents to:Connect your wallet and test it.
The API strives to be as similar to the native Stacks API as possible. So looking into Stacks Connect should give you a good idea on how to create any other calls yourself.
Structured Message Signing: In order to sign ClarityValue message we use
signMessageHashRsvmethod from'@stacks/transactions'in the Xverse wallet EdithandleStructuredMessageand set it's content to:Contract Deploy:
Take Contract Deploy for example - there's not much difference! You just specify "RPC method" to call and pass some parameters et voilà!
Transfer STX:
Can you can see the pattern?
Contract Call:
Calling a contract method is slightly more elaborate, but it's only due to the fact we want to provide Stacks
PostConditions.Luckily, thanks to the
BigIntdefined hacks above - we can use Stacks libraries to build the transaction as we normally would.In our case we want to
transfersome dummyExampleCointokens.First we figure out contract related details, like the contract's name and account address. We also need the token's name - it's hidden in the contract's code (you can see it through explorer).
Then we build the
PostConditionsarray - we want it totransfer exactly 1000 tokens(our "order amount"). If you want the transaction to be sponsored, you can pass a sponsored flag in the parameters.The last thing is to simply combine it all and pass as
request.
That's it - you can now:
connect to your Stacks wallet through Wallet Connect
sign a message
sign a structured message
transfer STX
deploy a contract
call a contract
send a BTC transfer transaction
Last updated