Building a Decentralized Android App: Part I (WalletConnect)

The future is decentralized and your Android App can be too: a 101 tour to build your first Android DApp.

Bruno Correia
Pink Wall
6 min readFeb 1, 2022

--

Photo by Alina Grubnyak on Unsplash

Disclaimer: I’m not here to convince you that Web3.0, Blockchain, Crypto, NFTs, or other fancy buzzwords are important and will be part of our future. Primarily, because opinions are just that, opinions, and also because I surely don’t know how the future will be. However, there’s something undeniable happening right now, and the internet is changing, our world is changing. And although the future is uncertain, concepts like decentralization, ownership, security, and privacy were here before and are here to stay. And that's why we, at Pink Room, started learning more about Decentralized Apps (DApps) and how to build them.

What is a DApp?

A DApp is any application that exists and runs on a peer-to-peer (P2P) network. This means that these applications don’t have a central authority that controls the data or the communication (like a server), but instead, any peer of the network can communicate directly with other peers.

Although DApps can be developed in many different ways, currently, they’re often associated with technologies that use blockchain and support Smart Contracts like Ethereum. Actually, Ethereum is currently the most used technology and the first supporting Smart Contracts. And that’s why we will focus on it.

What do we need to build an Android DApp?

This was the first question that we asked ourselves before starting to develop one. So, there are 2 essential things that we, as developers, need:

  1. A way to connect our DApp to a Wallet.
  2. A way to communicate with Smart Contracts. (we will talk about this in the next blog post).

Connect DApp with Wallet

Why do we need to connect our DApp with a Wallet? Because Wallets are literally your key to communicate with blockchains. For those who are not familiar with this, picture a normal App. Typically, an App needs credentials to be able to communicate with an API that gives you the ability to perform some actions in the platform that you are using. With DApps, these credentials are private keys that are normally kept inside a Wallet and correspond to unique addresses in the blockchain. These keys are used to sign transactions that will be stored in the blockchain. So, how can we do this? WalletConnect protocol is the answer and we will delve into it right away.

WalletConnect

Integrating WalletConnect is the first step for building your Android DApp. But, let me first give you a brief explanation of how it works.

It is important to mention that WalletConnect has two different protocols: v1.0 and v2.0. My goal is not to explain the differences between them, you can read more about it here, and if you want to understand better the protocol I recommend you read the WalletConnect documentation.

To put it simply, the WalletConnect protocol relies on 3 main components:

WalletConnect Architecture
  1. DApp: what we are building.
  2. Wallet: which contains the account private key.
  3. Relay/Bridge*: a WebSocket server used to relay messages between the DApp and Wallet.

*if you read the WalletConnect documentation, you will notice that in the protocol v1.0, this component is called Bridge because, at the time, it was seen as a bridge between a DApp and a Wallet. In v2.0, this name was changed to Relay to better represent what this component does.

In order to connect your DApp with a Wallet, you firstly start with an out-of-band communication. You can see this as a handshake between your DApp and Wallet that contains some information like the version of the protocol in use, the topic that will be used for communication, and a key used to secure this communication cryptographically. The way you do this in Android is by sending an Intent that will open the Wallet and share all this information.

After that, all the communication will be done through the Relay server. Your DApp and Wallet will publish messages to the subscribed topic and act accordingly to the protocol to connect, disconnect, and perform transactions.

Although this looks simple, implementing the DApp logic to comply with the WalletConnect protocol is a tedious task and implies writing a lot of code. That’s why we built WalletConnectKit. WalletConnectKit is, currently, the simplest way to integrate WalletConnect into your DApp. And we will use it to kick off the development of your first DApp.

Important Note: If you don’t have an Ethereum account yet, this is the right time to do it. We recommend that you create one with Metamask. Besides being one of the most used Wallets, it also implements the WalletConnect protocol. After creating your account, you can change the Metamask to the Ropsten Test Network. This is very important so you can play around with fake Ether and avoid losing your assets. To get your first fake ETH, you can go to a Ropsten Faucet and drop your account address.

Installation and Setup

Start by creating an Android project and add the WalletConnectKit dependency: implementation 'dev.pinkroom:walletconnectkit:<version>'.

Then, create a configuration that will be used to share the information of your DApp with the Wallet.

After that, you just need to create an instance of the WalletConnectKit.

This instance gives you access to all the methods needed to manage your connection and perform transactions.

Connect Button

Although you can manage your connection with the WalletConnectKit instance, the easiest way to do it is by using the WalletConnectButton. The WalletConnectButton is a custom ImageButton containing a default theme that can be overridden and added to your screen.

After adding the WalletConnectButton, start it with the WalletConnectKit instance and you’re done. As simple as that.

Transactions

Now that you are connected with the Wallet, it’s time to start performing transactions. With WalletConnectKit, you do this by calling the performTransaction method. This method is a suspend function which means you need to call it inside a coroutine.

When you call this method, your Wallet will open and you should be prompted to accept or reject the transaction with the passed value to the given address. Then, just handle the success and failure states.

You can see an example of a DApp integrating the WalletConnectKit here.

Note: Currently, the WalletConnectKit only supports the v1.0 protocol of WalletConnect since it is the protocol that most Wallets implement. As soon as Wallets implement the v2.0 protocol and the WalletConnect library for Kotlin is stable, we will support the v2.0 protocol.

Conclusion

Although right now, there’s a lack of good documentation and tools that we can use to build native mobile DApps, we have already started to see some progress and efforts for better development support.

Nevertheless, we are still in the beginning and even though things can change a lot in the development of DApps, I truly believe that WalletConnect protocol will be the future standard to connect DApps and Wallets. It’s already implemented in the most used Wallets and it’s being used by big companies like Twitter.

And that’s all folks! In the next blog post, we will show you how your DApp can communicate with a Smart Contract. Let us know if you have any questions or ideas to improve WalletConnectKit. Contributions are welcome!

If you want to know more about this topic and other topics related to mobile development and web3, follow PinkRoomDev and bffcorreia on Twitter.

Happy coding! 🚀

--

--