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:

  1. Create React app

  2. Create the Wallet Connect client

  3. Establish a Wallet Connect Session

  4. Make wallet requests via Wallet Connect

Create React app

  1. Create a React project

    npx create-react-app webapp
    cd webapp
    yarn start
  2. Clear the contents of src/App.css - remove everything, leave empty file.

  3. 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

  1. Install WalletConnect dependencies

  2. Setup Wallet Connect client

    We want to store the Client "globally" and set it up as soon as possible, so:

    Open src/App.js and import dependencies:

    Add local state variable to App component 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.

  1. 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:

  1. Create local state that will hold our selected Chain and our Session:

  2. Add empty connection handler:

  3. Handle our Networks (chainIDs) in UI:

    Replace the return of our App component like this:

  4. Make it questionably prettier, replace src/App.css with:

  5. Handle connection, edit the handleConnect function:

    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 Bip122 for 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 events empty - 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.

  1. Show available methods depending on the selected chain, add this to src/App.js render method:

  2. Add the missing handlers:

    We'll cover each handler individually building up complexity.

    You can read more about available Stacks transactions here.

  3. 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 request itself is heavily dependent on what Wallet requires from us!

    We'll also need an address and we can get it from the session like 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, ClarityValues that utilize bigint will not work out of the box.

    For that reason we need a hack to allow bigint to be serialized as JSON. Add this to your src/App.js (after imports):

    If you use TypeScript, you can do it like this:

  4. 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:

  5. Sign Message:

    The idea is that we send a message to the wallet and it sends back a cryptographic signature (signs our message with a private key).

    Edit handleSignMessage function 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.

  6. Structured Message Signing: In order to sign ClarityValue message we use signMessageHashRsv method from '@stacks/transactions' in the Xverse wallet Edit handleStructuredMessage and set it's content to:

  7. 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à!

  8. Transfer STX:

    Can you can see the pattern?

  9. 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 BigInt defined hacks above - we can use Stacks libraries to build the transaction as we normally would.

    In our case we want to transfer some dummy ExampleCoin tokens.

    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 PostConditions array - we want it to transfer 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