From bc723fd0c2eacfa9a0ef6a2cf15f0a47de1829dd Mon Sep 17 00:00:00 2001 From: kubel Date: Wed, 21 Aug 2024 11:26:07 +0200 Subject: [PATCH 1/2] Use Toast instead of the alert dialog --- .../domain/relay/RelayJsonRpcInteractor.kt | 1 + .../web3/modal/engine/Web3ModalEngine.kt | 2 ++ .../ui/components/internal/Web3ModalComponent.kt | 8 +++++++- .../com/walletconnect/sample/modal/common/Route.kt | 6 ------ .../com/walletconnect/sample/modal/ui/LabScreen.kt | 14 ++++++++------ 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt index 8210cdeace..f95b5314f7 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt @@ -365,6 +365,7 @@ internal class RelayJsonRpcInteractor( storePushRequestsIfEnabled(relayRequest, topic) Subscription(decryptMessage(topic, relayRequest), relayRequest.message, topic, relayRequest.publishedAt, relayRequest.attestation) }.collect { subscription -> + println("kobe: Message: ${subscription.decryptedMessage}") if (subscription.decryptedMessage.isNotEmpty()) { try { manageSubscriptions(subscription) diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt index 45364bc90f..d749cc3c5f 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt @@ -290,6 +290,7 @@ internal class Web3ModalEngine( signature = (response.result as Sign.Model.JsonRpcResponse.JsonRpcResult).result ) siweRequestIdWithMessage = null + println("kobe: response reault: $siweResponse") delegate.onSIWEAuthenticationResponse(siweResponse) } else if (response.result is Sign.Model.JsonRpcResponse.JsonRpcError) { val siweResponse = Modal.Model.SIWEAuthenticateResponse.Error( @@ -298,6 +299,7 @@ internal class Web3ModalEngine( code = (response.result as Sign.Model.JsonRpcResponse.JsonRpcError).code ) siweRequestIdWithMessage = null + println("kobe: response error: $siweResponse") delegate.onSIWEAuthenticationResponse(siweResponse) } } else { diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt index d246a3886e..9bcbc18d05 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt @@ -2,6 +2,7 @@ package com.walletconnect.web3.modal.ui.components.internal +import android.annotation.SuppressLint import androidx.compose.animation.AnimatedContent import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.animation.core.tween @@ -46,6 +47,7 @@ fun Web3ModalComponent( ) } +@SuppressLint("RestrictedApi") @Composable internal fun Web3ModalComponent( modifier: Modifier = Modifier, @@ -62,9 +64,13 @@ internal fun Web3ModalComponent( .wcEventModels .onEach { event -> when (event) { - is Modal.Model.SIWEAuthenticateResponse.Result, is Modal.Model.SessionAuthenticateResponse.Result -> closeModal() + is Modal.Model.SIWEAuthenticateResponse.Result, is Modal.Model.SessionAuthenticateResponse.Result -> { + println("kobe: close modal: ${navController.currentBackStack.value}") + closeModal() + } is Modal.Model.ApprovedSession -> { if (Web3Modal.authPayloadParams != null) { + println("kobe: approved") navController.navigate(Route.SIWE_FALLBACK.path) } else { closeModal() diff --git a/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/common/Route.kt b/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/common/Route.kt index 7a3b7124e8..f07a7384fa 100644 --- a/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/common/Route.kt +++ b/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/common/Route.kt @@ -1,7 +1,5 @@ package com.walletconnect.sample.modal.common -import androidx.navigation.NavController - const val messageArg = "messageArg" sealed class Route(val path: String) { @@ -10,8 +8,4 @@ sealed class Route(val path: String) { object Lab : Route("Lab") object AlertDialog : Route("Alert") -} - -fun NavController.openAlertDialog(message: String) { - navigate(Route.AlertDialog.path + "/$message") } \ No newline at end of file diff --git a/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/ui/LabScreen.kt b/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/ui/LabScreen.kt index ae09d5c5f1..366bcb1b79 100644 --- a/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/ui/LabScreen.kt +++ b/sample/modal/src/main/kotlin/com/walletconnect/sample/modal/ui/LabScreen.kt @@ -19,7 +19,6 @@ import com.walletconnect.sample.common.getEthSignTypedData import com.walletconnect.sample.common.getPersonalSignBody import com.walletconnect.sample.common.ui.commons.BlueButton import com.walletconnect.sample.modal.ModalSampleDelegate -import com.walletconnect.sample.modal.common.openAlertDialog import com.walletconnect.web3.modal.client.Modal import com.walletconnect.web3.modal.client.Web3Modal import com.walletconnect.web3.modal.client.models.request.Request @@ -44,17 +43,20 @@ fun LabScreen( LaunchedEffect(Unit) { ModalSampleDelegate.wcEventModels.collect { event -> - when(event) { + when (event) { is Modal.Model.SessionRequestResponse -> { - when(event.result) { + when (event.result) { is Modal.Model.JsonRpcResponse.JsonRpcError -> { val error = event.result as Modal.Model.JsonRpcResponse.JsonRpcError - navController.openAlertDialog("Error Message: ${error.message}\n Error Code: ${error.code}") + Toast.makeText(context, "Error Message: ${error.message}\n Error Code: ${error.code}", Toast.LENGTH_SHORT).show() } - is Modal.Model.JsonRpcResponse.JsonRpcResult -> navController.openAlertDialog((event.result as Modal.Model.JsonRpcResponse.JsonRpcResult).result) + + is Modal.Model.JsonRpcResponse.JsonRpcResult -> Toast.makeText(context, (event.result as Modal.Model.JsonRpcResponse.JsonRpcResult).result, Toast.LENGTH_SHORT).show() } } - is Modal.Model.Error -> { navController.openAlertDialog(event.throwable.localizedMessage ?: "Something went wrong") } + + is Modal.Model.Error -> Toast.makeText(context, event.throwable.localizedMessage ?: "Something went wrong", Toast.LENGTH_SHORT).show() + else -> Unit } } From de8d03f3bee20fedc4da81e61d3f3d12336088d7 Mon Sep 17 00:00:00 2001 From: kubel Date: Wed, 21 Aug 2024 12:08:28 +0200 Subject: [PATCH 2/2] Event Adapter fix for parsing a number in data params --- .../domain/relay/RelayJsonRpcInteractor.kt | 1 - .../web3/modal/engine/Web3ModalEngine.kt | 2 -- .../ui/components/internal/Web3ModalComponent.kt | 6 +----- .../common/adapters/SessionEventVOJsonAdapter.kt | 14 +++++++++++++- .../sign/adapters/SessionEventVOJsonAdapterTest.kt | 7 +++++++ 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt index f95b5314f7..8210cdeace 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/internal/common/json_rpc/domain/relay/RelayJsonRpcInteractor.kt @@ -365,7 +365,6 @@ internal class RelayJsonRpcInteractor( storePushRequestsIfEnabled(relayRequest, topic) Subscription(decryptMessage(topic, relayRequest), relayRequest.message, topic, relayRequest.publishedAt, relayRequest.attestation) }.collect { subscription -> - println("kobe: Message: ${subscription.decryptedMessage}") if (subscription.decryptedMessage.isNotEmpty()) { try { manageSubscriptions(subscription) diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt index d749cc3c5f..45364bc90f 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/engine/Web3ModalEngine.kt @@ -290,7 +290,6 @@ internal class Web3ModalEngine( signature = (response.result as Sign.Model.JsonRpcResponse.JsonRpcResult).result ) siweRequestIdWithMessage = null - println("kobe: response reault: $siweResponse") delegate.onSIWEAuthenticationResponse(siweResponse) } else if (response.result is Sign.Model.JsonRpcResponse.JsonRpcError) { val siweResponse = Modal.Model.SIWEAuthenticateResponse.Error( @@ -299,7 +298,6 @@ internal class Web3ModalEngine( code = (response.result as Sign.Model.JsonRpcResponse.JsonRpcError).code ) siweRequestIdWithMessage = null - println("kobe: response error: $siweResponse") delegate.onSIWEAuthenticationResponse(siweResponse) } } else { diff --git a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt index 9bcbc18d05..5b2bcdd4b7 100644 --- a/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt +++ b/product/web3modal/src/main/kotlin/com/walletconnect/web3/modal/ui/components/internal/Web3ModalComponent.kt @@ -64,13 +64,9 @@ internal fun Web3ModalComponent( .wcEventModels .onEach { event -> when (event) { - is Modal.Model.SIWEAuthenticateResponse.Result, is Modal.Model.SessionAuthenticateResponse.Result -> { - println("kobe: close modal: ${navController.currentBackStack.value}") - closeModal() - } + is Modal.Model.SIWEAuthenticateResponse.Result, is Modal.Model.SessionAuthenticateResponse.Result -> closeModal() is Modal.Model.ApprovedSession -> { if (Web3Modal.authPayloadParams != null) { - println("kobe: approved") navController.navigate(Route.SIWE_FALLBACK.path) } else { closeModal() diff --git a/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionEventVOJsonAdapter.kt b/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionEventVOJsonAdapter.kt index d376af1723..92b28d0993 100644 --- a/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionEventVOJsonAdapter.kt +++ b/protocol/sign/src/main/kotlin/com/walletconnect/sign/common/adapters/SessionEventVOJsonAdapter.kt @@ -30,11 +30,23 @@ internal class SessionEventVOJsonAdapter(moshi: Moshi) : JsonAdapter { // Moshi does not handle malformed JSON where there is a missing key for an array or object val dataAny = anyAdapter.fromJson(reader) ?: throw Util.unexpectedNull("data", "data", reader) + data = if (dataAny is List<*>) { upsertArray(JSONArray(), dataAny).toString() - } else { + } else if (dataAny is Map<*, *>) { val paramsMap = dataAny as Map<*, *> upsertObject(JSONObject(), paramsMap).toString() + } else { + if (dataAny is Number) { + val castedNumber = if (dataAny.toDouble() % 1 == 0.0) { + dataAny.toLong() + } else { + dataAny.toDouble() + } + castedNumber.toString() + } else { + dataAny.toString() + } } } diff --git a/protocol/sign/src/test/kotlin/com/walletconnect/sign/adapters/SessionEventVOJsonAdapterTest.kt b/protocol/sign/src/test/kotlin/com/walletconnect/sign/adapters/SessionEventVOJsonAdapterTest.kt index cead8093d4..583b6dd946 100644 --- a/protocol/sign/src/test/kotlin/com/walletconnect/sign/adapters/SessionEventVOJsonAdapterTest.kt +++ b/protocol/sign/src/test/kotlin/com/walletconnect/sign/adapters/SessionEventVOJsonAdapterTest.kt @@ -11,6 +11,7 @@ import junit.framework.TestCase import org.json.JSONArray import org.junit.Test import kotlin.reflect.jvm.jvmName +import kotlin.test.assertEquals class SessionEventVOJsonAdapterTest { private val moshi: Moshi = Moshi.Builder() @@ -60,4 +61,10 @@ class SessionEventVOJsonAdapterTest { iterateJsonArrays(expectedParamsJsonArray, actualParamsJsonArray) } + + @Test + fun testParsingNumber() { + data = """1""".trimIndent() + assertEquals(data, serializedData) + } } \ No newline at end of file