Giter Site home page Giter Site logo

walletconnectkit-android's Introduction

WalletConnectKit

Maven Central Status

WalletConnectKit is the Swiss Army toolkit for WalletConnect! It will allow you to connect your DApp with an Ethereum Wallet in a few minutes and start performing transactions right away.

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


InstallationSetupConnect ButtonTransactionsAdvanced


Demo

Installation

implementation 'dev.pinkroom:walletconnectkit:<latest_version>'

Setup

First, you need to create a config:

val config = WalletConnectKitConfig(
    context = this,
    bridgeUrl = "wss://bridge.aktionariat.com:8887",
    appUrl = "walletconnectkit.com",
    appName = "WalletConnectKit",
    appDescription = "WalletConnectKit is the Swiss Army toolkit for WalletConnect!"
)

Note: The bridge url provided above is a deployed version of this repo by its owner. Feel free to use it or use your own bridge server.

Then, build the WalletConnectKit instance:

val walletConnectKit = WalletConnectKit.Builder(config).build()

And you are ready to go! 🚀

Connect Button

Add the WalletConnectButton to your layout:

<dev.pinkroom.walletconnectkit.WalletConnectButton
    android:id="@+id/walletConnectButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Start the button with the previously created WalletConnectKit instance. When the account is successfully connected, you will receive the account address.

walletConnectButton.start(walletConnectKit) { address ->
    println("You are connected with account: $address")
}

Or:

walletConnectButton.start(walletConnectKit, ::onConnected, ::onDisconnected)
private fun onConnected(address: String) {
    println("You are connected with account: $address")
}

private fun onDisconnected() {
    println("Account disconnected!")
}

Note: WalletConnectButton is an ImageButton with a default theme that can be overridden by you!

If you want to be informed about the status of the WalletConnect session, you can set a Session.Callback before calling the start method.

class MyActivity : AppCompatActivity(), Session.Callback {

    override fun onMethodCall(call: Session.MethodCall) {
        // Handle onMethodCall
    }

    override fun onStatus(status: Session.Status) {
        // Handle session status
    }
}
walletConnectButton.sessionCallback = this

Transactions

In order to perform a transaction you just need to call the performTransaction method. This method is a suspend function so, you need to call it inside a coroutine.

lifecycleScope.launch {
    runCatching { walletConnectKit.performTransaction(toAddress, value) }
        .onSuccess { /* Handle onSuccess */ }
        .onFailure { /* Handle onFailure */ }
}

Note: If you want to perform a transaction through a smart contract function, you need to pass the encoded function data of the smart contract to the data parameter of the performTransaction function.

Sign

If you only want to sign a message, you can use the personalSign method.

lifecycleScope.launch {
    runCatching { walletConnectKit.personalSign(message) }
        .onSuccess { /* Handle onSuccess */ }
        .onFailure { /* Handle onFailure */ }
}

Advanced

If you don't want to use the WalletConnectKitButton and want to create your own implementation, you can still use the WalletConnectKit to manage the connection between your DApp and Wallet.

Below are the most relevant methods provided by the WalletConnectKit that you need to care about:

Method Description
createSession(callback: Session.Callback) Creates a session and stores it locally. After calling this method you should receive a `Session.Status.Connected` in the passed callback. This is where you should call the `requestHandshake` method (see below).
removeSession() Removes the current session and cleans everything related to it.
loadSession(callback: Session.Callback) Loads the session that is stored locally.
isSessionStored A flag that tells you if there is any session stored locally.
session Returns the current session or null otherwise.
address Returns the approved account address or null otherwise.
requestHandshake() Starts an intent that performs the handshake between your DApp and a Wallet.
openWallet() Starts an intent that opens a Wallet.

License

Copyright 2021 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.

walletconnectkit-android's People

Contributors

bffcorreia avatar crysis21 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.