trustwallet / wallet-connect-kotlin

95 stars 73 forks source link

WalletConnect

WalletConnect Kotlin SDK, implements 1.0.0 websocket based protocol.

Demo

Features

Installation

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the following line to your app's build.gradle:

dependencies {
    implementation "com.github.TrustWallet:wallet-connect-kotlin:$wc_version"
}

Usage

parse session from scanned QR code:

val peerMeta = WCPeerMeta(name = "App name", url = "https://website.com")
val string = "wc:..."
val session = WCSession.from(string) ?: throw InvalidSessionError // invalid session
// handle session
wcClient.connect(wcSession, peerMeta)

configure and handle incoming message:

val wcClient = WCClient(GsonBuilder(), okHttpClient)

wcClient.onDisconnect = { _, _ -> 
    onDisconnect() 
}

wcClient.onSessionRequest = { _, peer -> 
    // ask for user consent
}

wcClient.onDisconnect = { _, _ -> 
    // handle disconnect
}
wcClient.onFailure = { t -> 
    // handle failure
}
wcClient.onGetAccounts = { id -> 
    // handle get_accounts
}

wcClient.onEthSign = { id, message -> 
    // handle eth_sign, personal_sign, eth_signTypedData
}
wcClient.onEthSignTransaction = { id, transaction -> 
    // handle eth_signTransaction
}
wcClient.onEthSendTransaction = { id, transaction -> 
    // handle eth_sendTransaction
}

wcClient.onSignTransaction = { id, transaction -> 
    // handle bnb_sign
}

approve session

wcClient.approveSession(listOf(address), chainId)

approve request

wcClient.approveRequest(id, signResult) // hex formatted sign

disconnect

if (wcClient.session != null) {
    wcClient.killSession()
} else {
    wcClient.disconnect()
}

License

WalletConnect is available under the MIT license. See the LICENSE file for more info.