Skip to content

Commit

Permalink
Added chainId support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobilekosmos committed Nov 3, 2021
1 parent 785087d commit b7e4667
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/src/main/kotlin/org/walletconnect/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ interface Session {
data class Response(val id: Long, val result: Any?, val error: Error? = null) : MethodCall(id)
}

data class PeerData(val id: String, val meta: PeerMeta?)
data class PeerData(val id: String, val meta: PeerMeta?, val chainId: Long? = null)
data class PeerMeta(
val url: String? = null,
val name: String? = null,
Expand Down
17 changes: 17 additions & 0 deletions lib/src/main/kotlin/org/walletconnect/impls/MoshiPayloadAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ class MoshiPayloadAdapter(moshi: Moshi) : Session.PayloadAdapter {
}
).toByteArray()

// Only used to verify JSON is correct.
// private fun Session.MethodCall.toBytes(): ByteArray {
// val json = mapAdapter.toJson(
// when (this) {
// is Session.MethodCall.SessionRequest -> this.toMap()
// is Session.MethodCall.Response -> this.toMap()
// is Session.MethodCall.SessionUpdate -> this.toMap()
// is Session.MethodCall.SendTransaction -> this.toMap()
// is Session.MethodCall.PersonalSignMessage -> this.toMap()
// is Session.MethodCall.SignMessage -> this.toMap()
// is Session.MethodCall.SignTypedDataMessage -> this.toMap()
// is Session.MethodCall.Custom -> this.toMap()
// }
// )
// return json.toByteArray()
// }

private fun Session.MethodCall.SessionRequest.toMap() =
jsonRpc(id, "wc_sessionRequest", peer.intoMap())

Expand Down
32 changes: 28 additions & 4 deletions lib/src/main/kotlin/org/walletconnect/impls/WCSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class WCSession(
private val sessionStore: WCSessionStore,
transportBuilder: Session.Transport.Builder,
clientMeta: Session.PeerMeta,
clientId: String? = null
clientId: String? = null,
_chainId: Long? = null
) : Session {

private val keyLock = Any()
Expand Down Expand Up @@ -46,15 +47,15 @@ class WCSession(
clientData = sessionStore.load(config.handshakeTopic)?.let {
currentKey = it.currentKey
approvedAccounts = it.approvedAccounts
chainId = it.chainId
this.chainId = it.chainId
handshakeId = it.handshakeId
peerId = it.peerData?.id
peerMeta = it.peerData?.meta
if (clientId != null && clientId != it.clientData.id)
throw IllegalArgumentException("Provided clientId is different from stored clientId")
it.clientData
} ?: run {
Session.PeerData(clientId ?: UUID.randomUUID().toString(), clientMeta)
Session.PeerData(clientId ?: UUID.randomUUID().toString(), clientMeta, _chainId)
}
storeSession()
}
Expand Down Expand Up @@ -114,6 +115,28 @@ class WCSession(
handshakeId = requestId
}
}
// Only used for testing purposes.
// override fun offer() {
// if (transport.connect()) {
// val requestId = createCallId()
// val params = mutableMapOf<String, Any?>()
// params["peerId"] = this.clientData.id
// this.clientData.meta?.intoMap(params)
// params["chainId"] = 2
//
// send(Session.MethodCall.Custom(requestId,"wc_sessionRequest", listOf(params)), topic = config.handshakeTopic, callback = { resp ->
// (resp.result as? Map<String, *>)?.extractSessionParams()?.let { params ->
// peerId = params.peerData?.id
// peerMeta = params.peerData?.meta
// approvedAccounts = params.accounts
// chainId = params.chainId
// storeSession()
// propagateToCallbacks { onStatus(if (params.approved) Session.Status.Approved else Session.Status.Closed) }
// }
// })
// handshakeId = requestId
// }
// }

override fun approve(accounts: List<String>, chainId: Long) {
val handshakeId = handshakeId ?: return
Expand Down Expand Up @@ -195,6 +218,7 @@ class WCSession(
handshakeId = data.id
peerId = data.peer.id
peerMeta = data.peer.meta
chainId = data.peer.chainId
storeSession()
}
is Session.MethodCall.SessionUpdate -> {
Expand Down Expand Up @@ -255,7 +279,7 @@ class WCSession(
WCSessionStore.State(
config,
clientData,
peerId?.let { Session.PeerData(it, peerMeta) },
peerId?.let { Session.PeerData(it, peerMeta, chainId) },
handshakeId,
currentKey,
approvedAccounts,
Expand Down
66 changes: 34 additions & 32 deletions lib/src/main/kotlin/org/walletconnect/types/TypeMapConversion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import org.walletconnect.Session
import org.walletconnect.nullOnThrow

fun Session.PeerMeta.intoMap(params: MutableMap<String, Any?> = mutableMapOf()) =
params.also {
params["peerMeta"] =
mutableMapOf<String, Any>(
"description" to (description ?: ""),
"url" to (url ?: ""),
"name" to (name ?: "")
).apply {
icons?.let { put("icons", it) }
}
}
params.also {
params["peerMeta"] =
mutableMapOf<String, Any>(
"description" to (description ?: ""),
"url" to (url ?: ""),
"name" to (name ?: "")
).apply {
icons?.let { put("icons", it) }
}
}

fun Map<*, *>?.extractPeerMeta(): Session.PeerMeta {
val description = this?.get("description") as? String
Expand All @@ -25,24 +25,26 @@ fun Map<*, *>?.extractPeerMeta(): Session.PeerMeta {


fun Session.PeerData.intoMap(params: MutableMap<String, Any?> = mutableMapOf()) =
params.also {
params["peerId"] = this.id
this.meta?.intoMap(params)
}
params.also {
params["peerId"] = this.id
this.meta?.intoMap(params)
params["chainId"] = this.chainId
}

fun Map<*, *>.extractPeerData(): Session.PeerData {
val peerId = this["peerId"] as? String ?: throw IllegalArgumentException("peerId missing")
val peerMeta = this["peerMeta"] as? Map<*, *>
return Session.PeerData(peerId, peerMeta.extractPeerMeta())
val chainId = this["chainId"] as? Long ?: throw IllegalArgumentException("chainId missing")
return Session.PeerData(peerId, peerMeta.extractPeerMeta(), chainId)
}

fun Session.SessionParams.intoMap(params: MutableMap<String, Any?> = mutableMapOf()) =
params.also {
it["approved"] = approved
it["chainId"] = chainId
it["accounts"] = accounts
this.peerData?.intoMap(params)
}
params.also {
it["approved"] = approved
it["chainId"] = chainId
it["accounts"] = accounts
this.peerData?.intoMap(params)
}

fun Map<String, *>.extractSessionParams(): Session.SessionParams {
val approved = this["approved"] as? Boolean ?: throw IllegalArgumentException("approved missing")
Expand All @@ -55,19 +57,19 @@ fun Map<String, *>.extractSessionParams(): Session.SessionParams {
fun Map<String, *>.toSessionRequest(): Session.MethodCall.SessionRequest {
val params = this["params"] as? List<*> ?: throw IllegalArgumentException("params missing")
val data = params.firstOrNull() as? Map<*, *>
?: throw IllegalArgumentException("Invalid params")
?: throw IllegalArgumentException("Invalid params")

return Session.MethodCall.SessionRequest(
getId(),
data.extractPeerData()
getId(),
data.extractPeerData()
)
}

fun Session.Error.intoMap(params: MutableMap<String, Any?> = mutableMapOf()) =
params.also {
it["code"] = code
it["message"] = message
}
params.also {
it["code"] = code
it["message"] = message
}

fun Map<*, *>.extractError(): Session.Error {
val code = (this["code"] as? Double)?.toLong()
Expand All @@ -76,10 +78,10 @@ fun Map<*, *>.extractError(): Session.Error {
}

fun Map<String, *>.getId(): Long =
(this["id"] as? Double)?.toLong() ?: throw IllegalArgumentException("id missing")
(this["id"] as? Double)?.toLong() ?: throw IllegalArgumentException("id missing")


fun List<*>.toStringList(): List<String> =
this.map {
(it as? String) ?: throw IllegalArgumentException("List contains non-String values-en")
}
this.map {
(it as? String) ?: throw IllegalArgumentException("List contains non-String values-en")
}
2 changes: 1 addition & 1 deletion sample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dependencies {

implementation "com.squareup.okhttp3:okhttp:$versions.okhttp"

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'

// We need to specify this extra because the kotlin plugin adds an older version leading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class ExampleApplication : Application() {
name = "WalletConnect Example App",
description = "WalletConnect Example App",
icons = listOf()
)
)
),
_chainId = null)
session.offer()
}
}
Expand Down

0 comments on commit b7e4667

Please sign in to comment.