Skip to content

Commit

Permalink
Merge pull request #503 from WalletConnect/develop
Browse files Browse the repository at this point in the history
Core 1.4.0, Sign. 2.2.0, Auth 1.2.0, Chat 1.0.0-alpha03
  • Loading branch information
Talhaali00 authored Nov 23, 2022
2 parents 3a72014 + 242cb7b commit 4d719e0
Show file tree
Hide file tree
Showing 78 changed files with 956 additions and 724 deletions.
4 changes: 3 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ WalletConnect v2 protocols for Android applications.
| 1.1.0 | 2.0.0 | 1.0.0 | |
| 1.2.0 | | | 1.0.0-alpha01 |
| 1.3.0 | 2.1.0 | 1.1.0 | 1.0.0-alpha02 |
| 1.4.0 | 2.2.0 | 1.2.0 | 1.0.0-alpha03 |



## License
WalletConnect v2 is released under the Apache 2.0 license. [See LICENSE](https://github.com/WalletConnect/WalletConnectKotlinV2/blob/feature/develop/LICENSE) for details.
WalletConnect v2 is released under the Apache 2.0 license. [See LICENSE](/LICENSE) for details.
1 change: 1 addition & 0 deletions androidCore/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ root/build.gradle.kts:
allprojects {
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions androidCore/impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {

project.apply {
extra[KEY_PUBLISH_ARTIFACT_ID] = "android-core-impl"
extra[KEY_PUBLISH_VERSION] = "1.3.0"
extra[KEY_PUBLISH_VERSION] = "1.4.0"
extra[KEY_SDK_NAME] = "Android Core Impl"
}

Expand Down Expand Up @@ -51,7 +51,7 @@ sqldelight {

dependencies {
debugApi(project(":androidCore:sdk"))
releaseApi("com.walletconnect:android-core:1.3.0")
releaseApi("com.walletconnect:android-core:1.4.0")

bouncyCastle()
coroutines()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,24 @@ internal class BouncyCastleKeyManagementRepository(private val keyChain: KeyStor
keyChain.setKey(tag, key)
}

@Throws(MissingKeyException::class)
override fun getPublicKey(tag: String): PublicKey {
val key = keyChain.getKey(tag) ?: throw MissingKeyException("No SymmetricKey for tag: $tag")
val key = keyChain.getKey(tag) ?: throw MissingKeyException("No PublicKey for tag: $tag")
return PublicKey(key)
}

@Throws(MissingKeyException::class)
override fun getSymmetricKey(tag: String): SymmetricKey {
val key = keyChain.getKey(tag) ?: throw MissingKeyException("No PublicKey for tag: $tag")
val key = keyChain.getKey(tag) ?: throw MissingKeyException("No SymmetricKey for tag: $tag")
return SymmetricKey(key)
}

override fun getKeyAgreement(topic: Topic): Pair<PublicKey, PublicKey> {
@Throws(MissingKeyException::class)
override fun getSelfPublicFromKeyAgreement(topic: Topic): PublicKey {
val tag = "$KEY_AGREEMENT_CONTEXT${topic.value}"
val (selfPublic, peerPublic) = keyChain.getKeys(tag)
val (selfPublic, _) = keyChain.getKeys(tag) ?: throw MissingKeyException("No key pair for tag: $tag")

return Pair(PublicKey(selfPublic), PublicKey(peerPublic))
return PublicKey(selfPublic)
}

override fun setKeyAgreement(topic: Topic, self: PublicKey, peer: PublicKey) {
Expand Down Expand Up @@ -83,8 +86,9 @@ internal class BouncyCastleKeyManagementRepository(private val keyChain: KeyStor

override fun getTopicFromKey(key: Key): Topic = Topic(sha256(key.keyAsHex))

@Throws(MissingKeyException::class)
override fun removeKeys(tag: String) {
val (publicKey, _) = keyChain.getKeys(tag)
val (publicKey, _) = keyChain.getKeys(tag) ?: throw MissingKeyException("No key pair for tag: $tag")
with(keyChain) {
deleteKeys(publicKey.lowercase())
deleteKeys(tag)
Expand All @@ -95,8 +99,9 @@ internal class BouncyCastleKeyManagementRepository(private val keyChain: KeyStor
keyChain.setKeys(publicKey.keyAsHex, publicKey, privateKey)
}

internal fun getKeyPair(wcKey: Key): Pair<PublicKey, PrivateKey> {
val (publicKeyHex, privateKeyHex) = keyChain.getKeys(wcKey.keyAsHex)
@Throws(MissingKeyException::class)
internal fun getKeyPair(key: Key): Pair<PublicKey, PrivateKey> {
val (publicKeyHex, privateKeyHex) = keyChain.getKeys(key.keyAsHex) ?: throw MissingKeyException("No key pair for tag: ${key.keyAsHex}")

return Pair(PublicKey(publicKeyHex), PrivateKey(privateKeyHex))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import com.walletconnect.android.impl.json_rpc.data.JsonRpcSerializer
import com.walletconnect.android.impl.json_rpc.domain.JsonRpcInteractor
import com.walletconnect.android.internal.common.SerializableJsonRpc
import com.walletconnect.android.internal.common.model.JsonRpcInteractorInterface
import com.walletconnect.android.pairing.PairingJsonRpcMethod
import com.walletconnect.android.pairing.PairingParams
import com.walletconnect.android.pairing.model.PairingJsonRpcMethod
import com.walletconnect.android.pairing.model.PairingParams
import com.walletconnect.utils.addDeserializerEntry
import com.walletconnect.utils.addSerializerEntry
import org.koin.core.qualifier.named
import org.koin.dsl.module
import kotlin.reflect.KClass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ internal class JsonRpcInteractor(
}

val requestJson = serializer.serialize(payload) ?: return onFailure(IllegalStateException("JsonRpcInteractor: Unknown result params"))

if (jsonRpcHistory.setRequest(payload.id, topic, payload.method, requestJson)) {

val encryptedRequest = chaChaPolyCodec.encrypt(topic, requestJson, envelopeType, participants)

relay.publish(topic.value, encryptedRequest, params.toRelay()) { result ->
Expand Down Expand Up @@ -122,11 +122,15 @@ internal class JsonRpcInteractor(
irnParams: IrnParams,
envelopeType: EnvelopeType,
participants: Participants?,
onFailure: (Throwable) -> Unit
) {
val result = JsonRpcResponse.JsonRpcResult(id = request.id, result = clientParams)

publishJsonRpcResponse(request.topic, irnParams, result, envelopeType = envelopeType, participants = participants,
onFailure = { error -> Logger.error("Cannot send the response, error: $error") })
onFailure = { error ->
Logger.error("Cannot send the response, error: $error")
onFailure(error)
})
}

override fun respondWithSuccess(
Expand All @@ -139,7 +143,7 @@ internal class JsonRpcInteractor(

try {
publishJsonRpcResponse(request.topic, irnParams, result, envelopeType = envelopeType, participants = participants,
onFailure = { error -> Logger.error("Cannot send the response, error: $error") })
onFailure = { error -> handleError("Cannot send the response, error: $error") })
} catch (e: Exception) {
handleError(e.message ?: String.Empty)
}
Expand All @@ -159,7 +163,7 @@ internal class JsonRpcInteractor(
try {
publishJsonRpcResponse(request.topic, irnParams, jsonRpcError, envelopeType = envelopeType, participants = participants,
onFailure = { failure ->
Logger.error("Cannot respond with error: $failure")
handleError("Cannot respond with error: $failure")
onFailure(failure)
})
} catch (e: Exception) {
Expand Down Expand Up @@ -266,7 +270,7 @@ internal class JsonRpcInteractor(
}

private fun handleError(errorMessage: String) {
Logger.error(errorMessage)
Logger.error("JsonRpcInteractor error: $errorMessage")
scope.launch {
_internalErrors.emit(InternalError(errorMessage))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.walletconnect.android.internal.common.model.Expiry
import com.walletconnect.android.internal.common.model.Pairing
import com.walletconnect.android.internal.common.model.Redirect
import com.walletconnect.android.internal.common.storage.PairingStorageRepositoryInterface
import com.walletconnect.android.pairing.ACTIVE_PAIRING
import com.walletconnect.android.pairing.model.ACTIVE_PAIRING
import com.walletconnect.foundation.common.model.Topic

class PairingStorageRepository(private val pairingQueries: PairingQueries) : PairingStorageRepositoryInterface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.walletconnect.android.impl

import com.walletconnect.android.internal.common.storage.KeyStore
import com.walletconnect.android.impl.data.repository.BouncyCastleKeyManagementRepository
import com.walletconnect.android.internal.common.storage.KeyStore
import com.walletconnect.foundation.common.model.PrivateKey
import com.walletconnect.foundation.common.model.PublicKey
import com.walletconnect.foundation.common.model.Topic
Expand Down Expand Up @@ -63,8 +63,8 @@ internal class BouncyCastleCryptoRepositoryTest {
@Test
fun `SetKeyPair sets the concatenated keys to storage`() {
assertNotNull(sut.getKeyPair(publicKey))
assertEquals(publicKey.keyAsHex, keyChain.getKeys(publicKey.keyAsHex).first)
assertEquals(privateKey.keyAsHex, keyChain.getKeys(publicKey.keyAsHex).second)
assertEquals(publicKey.keyAsHex, keyChain.getKeys(publicKey.keyAsHex)?.first)
assertEquals(privateKey.keyAsHex, keyChain.getKeys(publicKey.keyAsHex)?.second)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ internal class RelayerInteractorTest {
val result = JsonRpcResponse.JsonRpcResult(request.id, result = params)
val irnParams = IrnParams(Tags.SESSION_PING, Ttl(300))
mockRelayPublishSuccess()
sut.respondWithParams(request, params, irnParams)
sut.respondWithParams(request, params, irnParams) {}
verify { sut.publishJsonRpcResponse(topic = topicVO, response = result, params = irnParams, onSuccess = any(), onFailure = any()) }
}

Expand Down Expand Up @@ -214,7 +214,7 @@ internal class RelayerInteractorTest {
@Test
fun `InitializationErrorsFlow emits value only on OnConnectionFailed`() = runBlockingTest {
every { relay.wsConnectionFailedFlow } returns flowOf(
object: WalletConnectException("Test") {}
object : WalletConnectException("Test") {}
)

val job = sut.wsConnectionFailedFlow.onEach { walletConnectException ->
Expand Down
2 changes: 1 addition & 1 deletion androidCore/sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ android {

dependencies {
debugImplementation(project(":androidCore:impl"))
releaseImplementation("com.walletconnect:android-core:1.1.0")
releaseImplementation("com.walletconnect:android-core:1.4.0")

implementation("androidx.core:core-ktx:1.8.0")
implementation("androidx.appcompat:appcompat:1.5.0")
Expand Down
4 changes: 2 additions & 2 deletions androidCore/sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

project.apply {
extra[KEY_PUBLISH_ARTIFACT_ID] = "android-core"
extra[KEY_PUBLISH_VERSION] = "1.3.0"
extra[KEY_PUBLISH_VERSION] = "1.4.0"
extra[KEY_SDK_NAME] = "Android Core"
}

Expand All @@ -18,7 +18,7 @@ android {
minSdk = MIN_SDK
targetSdk = TARGET_SDK

buildConfigField(type = "String", name = "sdkVersion", value = "\"${requireNotNull(extra.get(KEY_PUBLISH_VERSION))}\"")
buildConfigField(type = "String", name = "SDK_VERSION", value = "\"${requireNotNull(extra.get(KEY_PUBLISH_VERSION))}\"")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
Expand Down
14 changes: 11 additions & 3 deletions androidCore/sdk/src/main/kotlin/com/walletconnect/android/Core.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.walletconnect.android

import com.walletconnect.android.internal.common.model.AppMetaData
import com.walletconnect.android.internal.common.model.AppMetaDataType
import com.walletconnect.android.internal.common.model.Expiry
import com.walletconnect.foundation.common.model.Topic


object Core {
sealed interface Listeners {
Expand Down Expand Up @@ -33,13 +33,21 @@ object Core {
val uri: String,
val isActive: Boolean,
val registeredMethods: String
): Model()
) : Model()
}

sealed class Params {

data class Ping(val topic: String) : Params()

data class Pair(val uri: String) : Params()

data class Disconnect(val topic: String) : Params()

data class Activate(val topic: String) : Params()

data class UpdateExpiry(val topic: String, val expiry: Expiry) : Params()

data class UpdateMetadata(val topic: String, val metadata: Model.AppMetaData, val metaDataType: AppMetaDataType) : Params()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,47 @@ package com.walletconnect.android

import android.app.Application
import com.walletconnect.android.internal.common.wcKoinApp
import com.walletconnect.android.pairing.PairingClient
import com.walletconnect.android.pairing.PairingInterface
import com.walletconnect.android.pairing.client.PairingInterface
import com.walletconnect.android.pairing.client.PairingProtocol
import com.walletconnect.android.pairing.engine.domain.PairingEngine
import com.walletconnect.android.pairing.handler.PairingController
import com.walletconnect.android.pairing.handler.PairingControllerInterface
import com.walletconnect.android.relay.ConnectionType
import com.walletconnect.android.relay.RelayClient
import com.walletconnect.android.relay.RelayConnectionInterface
import org.koin.dsl.module

object CoreClient {
val Pairing: PairingInterface = PairingClient
val Pairing: PairingInterface = PairingProtocol
var Relay: RelayConnectionInterface = RelayClient

interface CoreDelegate : PairingInterface.Delegate

fun initialize(metaData: Core.Model.AppMetaData, relayServerUrl: String, connectionType: ConnectionType, application: Application, relay: RelayConnectionInterface? = null) {
fun initialize(
metaData: Core.Model.AppMetaData,
relayServerUrl: String,
connectionType: ConnectionType,
application: Application,
relay: RelayConnectionInterface? = null
) {
if (relay != null) {
Relay = relay
} else {
RelayClient.initialize(relayServerUrl, connectionType, application)
}
PairingClient.initialize(metaData)
wcKoinApp.modules(
module {
single { PairingEngine() }
single { Pairing }
single<PairingControllerInterface> { PairingController }
single { Relay }
}
)
PairingProtocol.initialize(metaData)
PairingController.initialize()
}

fun setDelegate(delegate: CoreDelegate) {
PairingClient.setDelegate(delegate)
PairingProtocol.setDelegate(delegate)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
package com.walletconnect.android.internal.common

import android.util.Base64
import com.walletconnect.android.internal.common.exception.CannotFindKeyPairException
import com.walletconnect.android.internal.common.storage.KeyStore
import com.walletconnect.foundation.common.model.PrivateKey
import com.walletconnect.foundation.common.model.PublicKey
import com.walletconnect.foundation.crypto.data.repository.BaseJwtRepository

internal class JwtRepositoryAndroid(private val keyChain: KeyStore): BaseJwtRepository() {
internal class JwtRepositoryAndroid(private val keyChain: KeyStore) : BaseJwtRepository() {

override fun setKeyPair(key: String, privateKey: PrivateKey, publicKey: PublicKey) {
keyChain.setKeys(KEY_DID_KEYPAIR, privateKey, publicKey)
Expand All @@ -21,6 +22,7 @@ internal class JwtRepositoryAndroid(private val keyChain: KeyStore): BaseJwtRepo
override fun getKeyPair(): Pair<String, String> {
return if (doesKeyPairExist()) {
val (privateKey, publicKey) = keyChain.getKeys(KEY_DID_KEYPAIR)
?: throw CannotFindKeyPairException("No key pair for given tag: $KEY_DID_KEYPAIR")
publicKey to privateKey
} else {
getDIDFromNewKeyPair()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import com.walletconnect.foundation.common.model.Topic
interface KeyManagementRepository {
fun setKey(key: Key, tag: String)
fun removeKeys(tag: String)

fun getPublicKey(tag: String): PublicKey
fun getSymmetricKey(tag: String): SymmetricKey

fun generateKeyPair(): PublicKey
fun setKeyAgreement(topic: Topic, self: PublicKey, peer: PublicKey)
fun getKeyAgreement(topic: Topic): Pair<PublicKey, PublicKey>
fun getSelfPublicFromKeyAgreement(topic: Topic): PublicKey

fun generateAndStoreSymmetricKey(topic: Topic): SymmetricKey
fun generateSymmetricKeyFromKeyAgreement(self: PublicKey, peer: PublicKey): SymmetricKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class CannotFindSequenceForTopic(override val message: String?) : WalletConnectE

class InvalidProjectIdException(override val message: String?) : WalletConnectException(message)
class ProjectIdDoesNotExistException(override val message: String?) : WalletConnectException(message)
class NoRelayConnectionException(override val message: String?) : WalletConnectException(message)
class NoRelayConnectionException(override val message: String?) : WalletConnectException(message)
class CannotFindKeyPairException(override val message: String?) : WalletConnectException(message)
Loading

0 comments on commit 4d719e0

Please sign in to comment.