KWalletConnect

KWalletConnect is a Kotlin Multiplatform library for WalletConnect protocol v1.
Setup
Add the dependency in your common module's commonMain sourceSet
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.joesteven:kwalletconnect:$last_version")
}
}
}
}
Usage
WCConnectionStore
WCConnectionStore is an interface that you need to implement to store the connection data.
You can use any storage you want(SqlDelight database/File System...), but you need to implement the interface.
WCClient
1.create a WCClient instance, better make it a singleton
val wcClient = WCClient(store = YourWCConnectionStore())
2.create a WCSessionConfig to get the WalletConnect uri
val config = WCSessionConfig(bridge = "https://bridge.walletconnect.org")
val uri = config.uri
3.connection
connect will return Result<WCConnection>, you can use this connection to send and receive messages.
val result = wcClient.connect(
config = config,
clientMeta = WCPeerMeta(
name = "Your Client name",
description = "Your client description",
icons = listOf("Your client logo url"),
url = "Your client website"
)
)
wcClient.disconnect(connectionId)
4.send a message
wcClient.request(
connectionId = it.id,
method = "personal_sign",
params = Json.encodeToJsonElement(
listOf("0x48656c6c6f2c207765623321",
it.accounts.first(),
)
).jsonArray
)
You can also get specified connection or current connections by wcClient.getConnection(connectionId) or wcClient.connections
WCServer
1.create a WCServer instance, better make it a singleton
val wcServer = WCServer(store = YourWCConnectionStore())
2.pair with a WalletConnect uri
wcServer.pair(
uri = uri,
clientMeta = YourClientMeta
).onSuccess {
}.onFailure {
}
pairRequest.approve(
account = listOf("0x1234567890...."),
chainId = 1
)
pairRequest.reject()
wcServer.disconnect(connectionId)
3.handle client request and send response
wcServer.request.collect{
}
wcServer.response(
connectionId = request.connectionId,
requestId = request.method.requestId,
response = response
)
wcServer.errorResponse(
connectionId = request.connectionId,
requestId = request.method.requestId,
errorCode = -32000,
errorMessage = "error message here"
)
Proguard
-keep class com.mimao.kmp.walletconnect.entity.** { *; }
LICENSE