diff --git a/core/android/src/main/kotlin/com/walletconnect/android/utils/Extensions.kt b/core/android/src/main/kotlin/com/walletconnect/android/utils/Extensions.kt index 6a31649601..d054f9d10b 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/utils/Extensions.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/utils/Extensions.kt @@ -37,7 +37,7 @@ internal fun String.isValidRelayServerUrl(): Boolean { @JvmSynthetic internal fun String.projectId(): String { return Uri.parse(this)!!.let { relayUrl -> - relayUrl.getQueryParameter("projectId")!! + relayUrl.getQueryParameter("projectId")!! } } @@ -47,10 +47,13 @@ internal val Throwable.toWalletConnectException: WalletConnectException when { this.message?.contains(HttpURLConnection.HTTP_UNAUTHORIZED.toString()) == true -> UnableToConnectToWebsocketException("${this.message}. It's possible that JWT has expired. Try initializing the CoreClient again.") + this.message?.contains(HttpURLConnection.HTTP_NOT_FOUND.toString()) == true -> ProjectIdDoesNotExistException(this.message) + this.message?.contains(HttpURLConnection.HTTP_FORBIDDEN.toString()) == true -> InvalidProjectIdException(this.message) + else -> GenericException("Error while connecting, please check your Internet connection or contact support: $this") } @@ -58,4 +61,12 @@ internal val Throwable.toWalletConnectException: WalletConnectException val Int.Companion.DefaultId get() = -1 -fun AppMetaData?.toClient() = Core.Model.AppMetaData(this?.name ?: String.Empty, this?.description ?: String.Empty, this?.url ?: String.Empty, this?.icons ?: emptyList(), this?.redirect?.native) +fun AppMetaData?.toClient() = Core.Model.AppMetaData( + name = this?.name ?: String.Empty, + description = this?.description ?: String.Empty, + url = this?.url ?: String.Empty, + icons = this?.icons ?: emptyList(), + redirect = this?.redirect?.native, + appLink = this?.redirect?.universal, + linkMode = this?.redirect?.linkMode ?: false +) diff --git a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/domain/SignEngine.kt b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/domain/SignEngine.kt index 6c3212a7df..c56b9b4071 100644 --- a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/domain/SignEngine.kt +++ b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/domain/SignEngine.kt @@ -202,28 +202,8 @@ internal class SignEngine( } fun setup() { - //todo: clean up - if (envelopeRequestsJob == null) { - envelopeRequestsJob = linkModeJsonRpcInteractor.clientSyncJsonRpc - .filter { request -> request.params is SignParams } - .onEach { request -> - when (val requestParams = request.params) { - is SignParams.SessionAuthenticateParams -> onAuthenticateSessionUseCase(request, requestParams) - is SignParams.SessionRequestParams -> onSessionRequestUseCase(request, requestParams) - } - }.launchIn(scope) - } - - if (envelopeResponsesJob == null) { - envelopeResponsesJob = linkModeJsonRpcInteractor.peerResponse - .filter { request -> request.params is SignParams } - .onEach { response -> - when (val params = response.params) { - is SignParams.SessionAuthenticateParams -> onSessionAuthenticateResponseUseCase(response, params) - is SignParams.SessionRequestParams -> onSessionRequestResponseUseCase(response, params) - } - }.launchIn(scope) - } + handleLinkModeRequests() + handleLinkModeResponses() if (signEventsJob == null) { signEventsJob = collectSignEvents() @@ -233,6 +213,10 @@ internal class SignEngine( internalErrorsJob = collectInternalErrors() } + handleRelayRequestsAndResponses() + } + + private fun handleRelayRequestsAndResponses() { jsonRpcInteractor.wssConnectionState .filterIsInstance() .onEach { @@ -253,6 +237,32 @@ internal class SignEngine( }.launchIn(scope) } + private fun handleLinkModeResponses() { + if (envelopeResponsesJob == null) { + envelopeResponsesJob = linkModeJsonRpcInteractor.peerResponse + .filter { request -> request.params is SignParams } + .onEach { response -> + when (val params = response.params) { + is SignParams.SessionAuthenticateParams -> onSessionAuthenticateResponseUseCase(response, params) + is SignParams.SessionRequestParams -> onSessionRequestResponseUseCase(response, params) + } + }.launchIn(scope) + } + } + + private fun handleLinkModeRequests() { + if (envelopeRequestsJob == null) { + envelopeRequestsJob = linkModeJsonRpcInteractor.clientSyncJsonRpc + .filter { request -> request.params is SignParams } + .onEach { request -> + when (val requestParams = request.params) { + is SignParams.SessionAuthenticateParams -> onAuthenticateSessionUseCase(request, requestParams) + is SignParams.SessionRequestParams -> onSessionRequestUseCase(request, requestParams) + } + }.launchIn(scope) + } + } + private fun collectJsonRpcRequests(): Job = jsonRpcInteractor.clientSyncJsonRpc .filter { request -> request.params is SignParams } diff --git a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/ApproveSessionAuthenticateUseCase.kt b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/ApproveSessionAuthenticateUseCase.kt index a1ce5fb2a1..9714f018bc 100644 --- a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/ApproveSessionAuthenticateUseCase.kt +++ b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/ApproveSessionAuthenticateUseCase.kt @@ -162,7 +162,6 @@ internal class ApproveSessionAuthenticateUseCase( if (jsonRpcHistoryEntry.transportType == TransportType.LINK_MODE && receiverMetadata.redirect?.linkMode == true) { if (receiverMetadata.redirect?.universal.isNullOrEmpty()) return@supervisorScope onFailure(IllegalStateException("App link is missing")) try { - onSuccess() linkModeJsonRpcInteractor.triggerResponse( responseTopic, response, diff --git a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RejectSessionAuthenticateUseCase.kt b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RejectSessionAuthenticateUseCase.kt index 334f3766f2..ffb5f67403 100644 --- a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RejectSessionAuthenticateUseCase.kt +++ b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RejectSessionAuthenticateUseCase.kt @@ -69,7 +69,6 @@ internal class RejectSessionAuthenticateUseCase( Participants(senderPublicKey, receiverPublicKey), EnvelopeType.ONE ) - onSuccess() } catch (e: Exception) { onFailure(e) } diff --git a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RespondSessionRequestUseCase.kt b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RespondSessionRequestUseCase.kt index 8f77a8b882..b52ed48fc2 100644 --- a/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RespondSessionRequestUseCase.kt +++ b/protocol/sign/src/main/kotlin/com/walletconnect/sign/engine/use_case/calls/RespondSessionRequestUseCase.kt @@ -73,7 +73,6 @@ internal class RespondSessionRequestUseCase( try { removePendingSessionRequestAndEmit(jsonRpcResponse.id) linkModeJsonRpcInteractor.triggerResponse(Topic(topic), jsonRpcResponse, session.peerAppLink) - onSuccess() } catch (e: Exception) { onFailure(e) } diff --git a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/domain/WCDelegate.kt b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/domain/WCDelegate.kt index 4e4d029072..b4f2f76818 100644 --- a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/domain/WCDelegate.kt +++ b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/domain/WCDelegate.kt @@ -75,7 +75,6 @@ object WCDelegate : Web3Wallet.WalletDelegate, CoreClient.CoreDelegate { override val onSessionAuthenticate: (Wallet.Model.SessionAuthenticate, Wallet.Model.VerifyContext) -> Unit get() = { sessionAuthenticate, verifyContext -> - sessionAuthenticateEvent = Pair(sessionAuthenticate, verifyContext) scope.launch { diff --git a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/common/peer/PeerUI.kt b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/common/peer/PeerUI.kt index a87be0ff1f..3c008c4d6b 100644 --- a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/common/peer/PeerUI.kt +++ b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/common/peer/PeerUI.kt @@ -13,6 +13,7 @@ data class PeerUI( val peerName: String, val peerUri: String, val peerDescription: String, + val linkMode: Boolean = false ) { companion object { val Empty = PeerUI("", "", "", "") diff --git a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateRoute.kt b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateRoute.kt index 8c83454d6e..5996a920c6 100644 --- a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateRoute.kt +++ b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateRoute.kt @@ -139,6 +139,11 @@ private fun SessionAuthenticateDialog( onCancel = { isCancelLoading = true + if (authenticateRequestUI.peerUI.linkMode) { + navController.popBackStack(route = Route.Connections.path, inclusive = false) + connectionsViewModel.refreshConnections() + } + try { sessionAuthenticateViewModel.reject( onSuccess = { redirect -> @@ -165,6 +170,11 @@ private fun SessionAuthenticateDialog( }, onConfirm = { isConfirmLoading = true + if (authenticateRequestUI.peerUI.linkMode) { + navController.popBackStack(route = Route.Connections.path, inclusive = false) + connectionsViewModel.refreshConnections() + } + try { sessionAuthenticateViewModel.approve( onSuccess = { redirect -> diff --git a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateViewModel.kt b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateViewModel.kt index 80de73426f..7760f5e960 100644 --- a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateViewModel.kt +++ b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_authenticate/SessionAuthenticateViewModel.kt @@ -118,6 +118,7 @@ class SessionAuthenticateViewModel : ViewModel() { peerName = "Kotlin Wallet", peerUri = "https://walletconnect.com/", peerDescription = "The communications protocol for web3.", + linkMode = sessionAuthenticate.participant.metadata?.linkMode ?: false ), messages = messages, peerContextUI = authContext.toPeerUI() diff --git a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestRoute.kt b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestRoute.kt index 5a2f55be9b..91326db6bd 100644 --- a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestRoute.kt +++ b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestRoute.kt @@ -85,6 +85,9 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode allowButtonColor, onConfirm = { isConfirmLoading = true + if (sessionRequestUI.peerUI.linkMode) { + navController.popBackStack(route = Route.Connections.path, inclusive = false) + } try { sessionRequestViewModel.approve( onSuccess = { uri -> @@ -111,6 +114,9 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode }, onCancel = { isCancelLoading = true + if (sessionRequestUI.peerUI.linkMode) { + navController.popBackStack(route = Route.Connections.path, inclusive = false) + } try { sessionRequestViewModel.reject( onSuccess = { uri -> diff --git a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestViewModel.kt b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestViewModel.kt index 2c2b31027c..6884bef294 100644 --- a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestViewModel.kt +++ b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/routes/dialog_routes/session_request/SessionRequestViewModel.kt @@ -139,6 +139,7 @@ class SessionRequestViewModel : ViewModel() { peerIcon = sessionRequest.peerMetaData?.icons?.firstOrNull() ?: "", peerUri = sessionRequest.peerMetaData?.url ?: "", peerDescription = sessionRequest.peerMetaData?.description ?: "", + linkMode = sessionRequest.peerMetaData?.linkMode ?: false ), topic = sessionRequest.topic, requestId = sessionRequest.request.id,