Skip to content

Commit 42c8bbc

Browse files
rmeissnerligi
authored andcommitted
Added sample dapp with WC server (WalletConnect#12)
1 parent 8c598cc commit 42c8bbc

File tree

14 files changed

+585
-120
lines changed

14 files changed

+585
-120
lines changed

build.gradle

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
buildscript {
22
ext {
33
versions = [
4-
kotlin: "1.3.31",
5-
moshi : '1.8.0',
6-
okhttp: '3.11.0',
4+
kotlin : "1.3.31",
5+
moshi : '1.8.0',
6+
okhttp : '3.11.0',
7+
8+
'minSdk' : 14,
9+
'compileSdk' : 28,
10+
'buildTools' : '28.0.3',
11+
'androidPlugin' : '3.4.0',
12+
13+
androidx_app_compat : '1.0.2',
14+
androidx_card_view : '1.0.0',
15+
androidx_constraint_layout: '1.1.3',
16+
androidx_lifecycle : '2.0.0',
17+
androidx_test_ext : '1.1.0',
18+
androidx_recycler_view : '1.0.0',
19+
androidx_room : '2.0.0',
20+
21+
multidex : '2.0.1',
22+
]
23+
24+
ext.deps = [
25+
android: [
26+
'runtime' : 'com.google.android:android:4.1.1.4',
27+
'gradlePlugin': "com.android.tools.build:gradle:${versions.androidPlugin}",
28+
]
729
]
830
}
931

1032
repositories {
1133
jcenter()
34+
/*
35+
google()
36+
maven {
37+
url uri('../../repo')
38+
}
39+
*/
1240
}
1341

1442
dependencies {
1543
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
1644
classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0'
45+
// How can we move this to the sample folder
46+
//classpath deps.android.gradlePlugin
1747
}
1848
}
1949

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: "kotlin"
2+
apply plugin: 'maven'
23

34
repositories {
45
jcenter()

lib/src/main/kotlin/org/walletconnect/Session.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package org.walletconnect
22

33
import java.net.URLDecoder
4+
import java.net.URLEncoder
45

56
interface Session {
67
fun init()
8+
/**
9+
* Send client info to the bridge and wait for a client to connect
10+
*/
11+
fun offer()
712
fun approve(accounts: List<String>, chainId: Long)
813
fun reject()
914
fun update(accounts: List<String>, chainId: Long)
@@ -14,9 +19,11 @@ interface Session {
1419

1520
fun approveRequest(id: Long, response: Any)
1621
fun rejectRequest(id: Long, errorCode: Long, errorMsg: String)
22+
fun performMethodCall(call: MethodCall, callback: ((Session.MethodCall.Response) -> Unit)? = null)
1723

18-
fun addCallback(cb: Session.Callback)
19-
fun removeCallback(cb: Session.Callback)
24+
fun addCallback(cb: Callback)
25+
fun removeCallback(cb: Callback)
26+
fun clearCallbacks()
2027

2128
data class Config(
2229
val handshakeTopic: String,
@@ -25,6 +32,8 @@ interface Session {
2532
val protocol: String = "wc",
2633
val version: Int = 1
2734
) {
35+
fun toWCUri(): String =
36+
"wc:$handshakeTopic@$version?bridge=${URLEncoder.encode(bridge, "UTF-8")}&key=$key"
2837
companion object {
2938
fun fromWCUri(uri: String): Config {
3039
val protocolSeparator = uri.indexOf(':')
@@ -82,8 +91,8 @@ interface Session {
8291
interface Builder {
8392
fun build(
8493
url: String,
85-
statusHandler: (Session.Transport.Status) -> Unit,
86-
messageHandler: (Session.Transport.Message) -> Unit
94+
statusHandler: (Status) -> Unit,
95+
messageHandler: (Message) -> Unit
8796
): Transport
8897
}
8998

lib/src/main/kotlin/org/walletconnect/impls/MoshiPayloadAdapter.kt

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.bouncycastle.crypto.params.KeyParameter
1313
import org.bouncycastle.crypto.params.ParametersWithIV
1414
import org.walletconnect.Session
1515
import org.walletconnect.nullOnThrow
16-
import org.walletconnect.types.intoMap
16+
import org.walletconnect.types.*
1717
import org.walleth.khex.hexToByteArray
1818
import org.walleth.khex.toNoPrefixHexString
1919
import java.security.SecureRandom
@@ -113,28 +113,12 @@ class MoshiPayloadAdapter(moshi: Moshi) : Session.PayloadAdapter {
113113
} ?: throw IllegalArgumentException("Invalid json")
114114
}
115115

116-
private fun Map<String, *>.getId(): Long =
117-
(this["id"] as? Double)?.toLong() ?: throw IllegalArgumentException("id missing")
118-
119-
private fun Map<String, *>.toSessionRequest(): Session.MethodCall.SessionRequest {
120-
val params = this["params"] as? List<*> ?: throw IllegalArgumentException("params missing")
121-
val data = params.firstOrNull() as? Map<*, *> ?: throw IllegalArgumentException("Invalid params")
122-
123-
return Session.MethodCall.SessionRequest(
124-
getId(),
125-
data.extractPeerData()
126-
)
127-
}
128-
129116
private fun Map<String, *>.toSessionUpdate(): Session.MethodCall.SessionUpdate {
130117
val params = this["params"] as? List<*> ?: throw IllegalArgumentException("params missing")
131-
val data = params.firstOrNull() as? Map<*, *> ?: throw IllegalArgumentException("Invalid params")
132-
val approved = data["approved"] as? Boolean ?: throw IllegalArgumentException("approved missing")
133-
val chainId = data["chainId"] as? Long
134-
val accounts = nullOnThrow { (data["accounts"] as? List<*>)?.toStringList() }
118+
val data = params.firstOrNull() as? Map<String, *> ?: throw IllegalArgumentException("Invalid params")
135119
return Session.MethodCall.SessionUpdate(
136120
getId(),
137-
Session.SessionParams(approved, chainId, accounts, nullOnThrow { data.extractPeerData() })
121+
data.extractSessionParams()
138122
)
139123
}
140124

@@ -176,31 +160,6 @@ class MoshiPayloadAdapter(moshi: Moshi) : Session.PayloadAdapter {
176160
)
177161
}
178162

179-
private fun Map<*, *>.extractError(): Session.Error {
180-
val code = (this["code"] as? Double)?.toLong()
181-
val message = this["message"] as? String
182-
return Session.Error(code ?: 0, message ?: "Unknown error")
183-
}
184-
185-
private fun Map<*, *>.extractPeerData(): Session.PeerData {
186-
val peerId = this["peerId"] as? String ?: throw IllegalArgumentException("peerId missing")
187-
val peerMeta = this["peerMeta"] as? Map<*, *>
188-
return Session.PeerData(peerId, peerMeta.extractPeerMeta())
189-
}
190-
191-
private fun Map<*, *>?.extractPeerMeta(): Session.PeerMeta {
192-
val description = this?.get("description") as? String
193-
val url = this?.get("url") as? String
194-
val name = this?.get("name") as? String
195-
val icons = nullOnThrow { (this?.get("icons") as? List<*>)?.toStringList() }
196-
return Session.PeerMeta(url, name, description, icons)
197-
}
198-
199-
private fun List<*>.toStringList(): List<String> =
200-
this.map {
201-
(it as? String) ?: throw IllegalArgumentException("List contains non-String values")
202-
}
203-
204163
/**
205164
* Convert INTO request bytes
206165
*/

0 commit comments

Comments
 (0)