Skip to content

Commit

Permalink
Fix double redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Jul 15, 2024
1 parent 67444c1 commit b5e7b97
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")!!
}
}

Expand All @@ -47,15 +47,26 @@ 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")
}

@get:JvmSynthetic
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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -233,6 +213,10 @@ internal class SignEngine(
internalErrorsJob = collectInternalErrors()
}

handleRelayRequestsAndResponses()
}

private fun handleRelayRequestsAndResponses() {
jsonRpcInteractor.wssConnectionState
.filterIsInstance<WSSConnectionState.Connected>()
.onEach {
Expand All @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ internal class RejectSessionAuthenticateUseCase(
Participants(senderPublicKey, receiverPublicKey),
EnvelopeType.ONE
)
onSuccess()
} catch (e: Exception) {
onFailure(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ internal class RespondSessionRequestUseCase(
try {
removePendingSessionRequestAndEmit(jsonRpcResponse.id)
linkModeJsonRpcInteractor.triggerResponse(Topic(topic), jsonRpcResponse, session.peerAppLink)
onSuccess()
} catch (e: Exception) {
onFailure(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("", "", "", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b5e7b97

Please sign in to comment.