@@ -5,6 +5,7 @@ import java.net.URLEncoder
55
66interface Session {
77 fun init ()
8+
89 /* *
910 * Send client info to the bridge and wait for a client to connect
1011 */
@@ -25,29 +26,46 @@ interface Session {
2526 fun removeCallback (cb : Callback )
2627 fun clearCallbacks ()
2728
29+ data class FullyQualifiedConfig (
30+ val handshakeTopic : String ,
31+ val bridge : String ,
32+ val key : String ,
33+ val protocol : String = " wc" ,
34+ val version : Int = 1
35+ )
36+
2837 data class Config (
29- val handshakeTopic : String ,
30- val bridge : String ,
31- val key : String ,
32- val protocol : String = " wc" ,
33- val version : Int = 1
38+ val handshakeTopic : String ,
39+ val bridge : String? = null ,
40+ val key : String? = null ,
41+ val protocol : String = " wc" ,
42+ val version : Int = 1
3443 ) {
35- fun toWCUri (): String =
36- " wc:$handshakeTopic @$version ?bridge=${URLEncoder .encode(bridge, " UTF-8" )} &key=$key "
44+ fun toWCUri () = " wc:$handshakeTopic @$version ?bridge=${URLEncoder .encode(bridge, " UTF-8" )} &key=$key "
45+
46+ fun isFullyQualifiedConfig () = bridge != null && key != null
47+ fun toFullyQualifiedConfig () = FullyQualifiedConfig (handshakeTopic, bridge!! , key!! , protocol, version)
48+
3749 companion object {
3850 fun fromWCUri (uri : String ): Config {
3951 val protocolSeparator = uri.indexOf(' :' )
4052 val handshakeTopicSeparator = uri.indexOf(' @' , startIndex = protocolSeparator)
4153 val versionSeparator = uri.indexOf(' ?' )
4254 val protocol = uri.substring(0 , protocolSeparator)
4355 val handshakeTopic = uri.substring(protocolSeparator + 1 , handshakeTopicSeparator)
44- val version = Integer .valueOf(uri.substring(handshakeTopicSeparator + 1 , versionSeparator))
45- val params = uri.substring(versionSeparator + 1 ).split(" &" ).associate {
46- it.split(" =" ).let { param -> param.first() to URLDecoder .decode(param[1 ], " UTF-8" ) }
56+
57+ return if (versionSeparator > 0 ) {
58+ val version = Integer .valueOf(uri.substring(handshakeTopicSeparator + 1 , versionSeparator))
59+ val params = uri.substring(versionSeparator + 1 ).split(" &" ).associate {
60+ it.split(" =" ).let { param -> param.first() to URLDecoder .decode(param[1 ], " UTF-8" ) }
61+ }
62+ val bridge = params[" bridge" ] ? : throw IllegalArgumentException (" Missing bridge param in URI" )
63+ val key = params[" key" ] ? : throw IllegalArgumentException (" Missing key param in URI" )
64+ Config (handshakeTopic, bridge, key, protocol, version)
65+ } else {
66+ val version = Integer .valueOf(uri.substring(handshakeTopicSeparator + 1 ))
67+ Config (handshakeTopic, protocol, version = version)
4768 }
48- val bridge = params[" bridge" ] ? : throw IllegalArgumentException (" Missing bridge param in URI" )
49- val key = params[" key" ] ? : throw IllegalArgumentException (" Missing key param in URI" )
50- return Config (handshakeTopic, bridge, key, protocol, version)
5169 }
5270 }
5371 }
@@ -58,14 +76,14 @@ interface Session {
5876 }
5977
6078 sealed class Status {
61- object Connected: Status()
62- object Disconnected: Status()
63- object Approved: Status()
64- object Closed: Status()
65- data class Error (val throwable : Throwable ): Status()
79+ object Connected : Status()
80+ object Disconnected : Status()
81+ object Approved : Status()
82+ object Closed : Status()
83+ data class Error (val throwable : Throwable ) : Status()
6684 }
6785
68- data class TransportError (override val cause : Throwable ): RuntimeException(" Transport exception caused by $cause " , cause)
86+ data class TransportError (override val cause : Throwable ) : RuntimeException(" Transport exception caused by $cause " , cause)
6987
7088 interface PayloadAdapter {
7189 fun parse (payload : String , key : String ): MethodCall
@@ -84,22 +102,22 @@ interface Session {
84102 fun close ()
85103
86104 sealed class Status {
87- object Connected: Status()
88- object Disconnected: Status()
89- data class Error (val throwable : Throwable ): Status()
105+ object Connected : Status()
106+ object Disconnected : Status()
107+ data class Error (val throwable : Throwable ) : Status()
90108 }
91109
92110 data class Message (
93- val topic : String ,
94- val type : String ,
95- val payload : String
111+ val topic : String ,
112+ val type : String ,
113+ val payload : String
96114 )
97115
98116 interface Builder {
99117 fun build (
100- url : String ,
101- statusHandler : (Status ) -> Unit ,
102- messageHandler : (Message ) -> Unit
118+ url : String ,
119+ statusHandler : (Status ) -> Unit ,
120+ messageHandler : (Message ) -> Unit
103121 ): Transport
104122 }
105123
@@ -119,14 +137,14 @@ interface Session {
119137 data class SessionUpdate (val id : Long , val params : SessionParams ) : MethodCall(id)
120138
121139 data class SendTransaction (
122- val id : Long ,
123- val from : String ,
124- val to : String? ,
125- val nonce : String? ,
126- val gasPrice : String? ,
127- val gasLimit : String? ,
128- val value : String ,
129- val data : String
140+ val id : Long ,
141+ val from : String ,
142+ val to : String? ,
143+ val nonce : String? ,
144+ val gasPrice : String? ,
145+ val gasLimit : String? ,
146+ val value : String ,
147+ val data : String
130148 ) : MethodCall(id)
131149
132150 data class SignMessage (val id : Long , val address : String , val message : String ) : MethodCall(id)
@@ -138,10 +156,10 @@ interface Session {
138156
139157 data class PeerData (val id : String , val meta : PeerMeta ? )
140158 data class PeerMeta (
141- val url : String? = null ,
142- val name : String? = null ,
143- val description : String? = null ,
144- val icons : List <String >? = null
159+ val url : String? = null ,
160+ val name : String? = null ,
161+ val description : String? = null ,
162+ val icons : List <String >? = null
145163 )
146164
147165 data class SessionParams (val approved : Boolean , val chainId : Long? , val accounts : List <String >? , val peerData : PeerData ? )
0 commit comments