The SDK that simplifies the integration of the WalletConnect v2 Sign protocol for Android apps.
Installation • Setup • Connect Button • Session and Accounts • Methods • Receive Events • Advanced
wckit_demo.mp4
Add Jitpack to your repositories list:
maven { url "https://jitpack.io" }
Then, add the WalletConnectKit
dependency:
implementation 'dev.pinkroom.walletconnectkit:sign-dapp:<latest_version>'
First, you need to create a config:
val config = WalletConnectKitConfig(projectId = "{PROJECT-ID}")
Note You can obtain your Project ID from the following link.
Next, build the WalletConnectKit
instance:
val walletConnectKit = WalletConnectKit.builder(this).config(config).build()
And you are ready to go! 🚀
The simplest way to connect a DApp to a Wallet is to use the WalletConnectKitButton
. This is a
composable component that you can use to connect existing pairings or new sessions. Using Jetpack
Compose you only need to do this:
WalletConnectKitButton(walletConnectKit)
Even tho this component is not fully customisable yet, you can check the accepted parameters to change the button style or simply copy paste the component code and write your own implementation.
Also, by default, the WalletConnectKitButton
is configured to use the Ethereum chain with some
default methods and events. For more information on how to change this, please refer to the
advanced section.
Sessions
represent the connections established between a DApp and a Wallet. Accounts
refer to
the public addresses associated with each session. Since the sessions and accounts can change or
be updated at any time they are exposed through a Kotlin Flow
.
Because we use Flows, make sure you get or collect them inside a coroutine:
scope.launch {
val currentActiveSessions = walletConnectKit.activeSessions.lastOrNull()
// or if you want to always have the active sessions up to date
walletConnectKit.activeSessions.collect { sessions ->
// Handle the sessions here! Each session contains a list of accounts.
sessions.forEach { it.accounts }
}
}
In compose, you can simply collect them as:
val activeSessions by walletConnectKit.activeSessions.collectAsStateWithLifecycle(initialValue = emptyList())
Important note: by default, the SDK automatically stores the active account that you use to
perform method calls and operations. If you have access to multiple accounts and want to override
the active account just set the activeAccount
variable:
walletConnectKit.activeAccount = /* Account */
Within this SDK, we have integrated the send_transaction
and personal_sign
methods specifically
for Ethereum.
All the method calls are suspend functions
and return a Result
so you can easily handle the
success and failure states.
To initiate a transaction, simply call the performEthSendTransaction
method.
walletConnectKit.performEthSendTransaction(
toAddress = toAddress,
value = value,
).onSuccess { /* Handle the success state */ }
.onFailure { /* Handle the failure state */ }
If you solely require signing a message, you can use the performEthPersonalSign
method.
walletConnectKit.performEthPersonalSign(message)
.onSuccess { /* Handle the success state */ }
.onFailure { /* Handle the failure state */ }
Any other RPC method that you want to call, simply use the performCustomMethodCall
function. This
function receives the method name that you want to call and the params. You can send any class
containing the params or a String representing the params in a JSON format.
To receive updates on the status of the WalletConnect session, you can access a property
called events
which is a Flow
. This allows you to collect the events and handle them
accordingly.
walletConnectKit.events.collect { event ->
// Handle the event based on its type.
// Add your own logic here
// ...
}
To use a different method/chain when connecting with the WalletConnectKitButton
, you can make the
following adjustments.
Use another chain already defined here:
WalletConnectKitButton(
walletConnectKit = walletConnectKit,
chains = listOf(Optimism),
)
Changing methods or events for a defined chain:
WalletConnectKitButton(
walletConnectKit = walletConnectKit,
chains = listOf(
Ethereum.copy(
methods = listOf(
EthMethod.ETH_SIGN,
EthMethod.ETH_SIGN_TRANSACTION,
),
events = listOf(
EthEvent.connect,
EthEvent.disconnect,
),
)
)
)
Setting up a new chain:
val Cosmos = Chain(
name = "Cosmos",
namespace = "cosmos",
reference = "cosmoshub-4",
methods = listOf("chainChanged", "accountsChanged"),
events = listOf("cosmos_signDirect", "cosmos_signAmino"),
)
WalletConnectKitButton(
walletConnectKit = walletConnectKit,
chains = listOf(Cosmos),
)
If you don't want to use the WalletConnectKitButton
you just need to call the connect function to
connect with a Wallet.
walletConnectKit.connect(
chains = chains,
optionalChains = optionalChains,
onSuccess = { /* Handle onSuccess */ },
onError = { /* Handle onSuccess */ },
)
To obtain the list of pairings currently available simple get them:
walletConnectKit.pairings
If you have an existing pairing, you can reuse it and connect to that specific wallet:
walletConnectKit.connectExistingPairing(
chains = chains,
optionalChains = optionalChains,
pairing = selectedPairing,
onSuccess = { /* Handle onSuccess */ },
onError = { /* Handle onSuccess */ },
)
Note: Even when using an existing pair, you still need to pass the chains and optional chains.
Copyright 2023 Pink Room, Lda
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.