Skip to content

Commit

Permalink
Merge pull request #1442 from WalletConnect/link_mode/fix_double_redi…
Browse files Browse the repository at this point in the history
…rect

Link mode: fix double redirect
  • Loading branch information
jakubuid authored Jul 15, 2024
2 parents 67444c1 + 17372f8 commit 26d731c
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 31 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 @@ -19,7 +19,7 @@ CREATE TABLE MetaData(

insertOrAbortMetaData:
INSERT OR ABORT INTO MetaData(sequence_topic, name, description, url, icons, native, type, app_link, link_mode)
VALUES (?, ?, ?, ?, ?, ?,?,?, ?);
VALUES (?,?,?,?,?,?,?,?,?);

updateMetaData:
UPDATE MetaData
Expand Down
Binary file modified core/android/src/main/sqldelight/databases/10.db
Binary file not shown.
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
2 changes: 1 addition & 1 deletion sample/modal/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<data
android:host="web3modal-laboratory-git-chore-kotlin-assetlinks-walletconnect1.vercel.app"
android:scheme="https"
android:pathPrefix="/dapp"/>
android:pathPrefix="/lab"/>
</intent-filter>

<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ModalSampleApp : Application() {
icons = listOf("https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png?alt=media"),
redirect = "kotlin-modal-wc://request",
linkMode = true,
appLink = "https://web3modal-laboratory-git-chore-kotlin-assetlinks-walletconnect1.vercel.app/dapp"
appLink = "https://web3modal-laboratory-git-chore-kotlin-assetlinks-walletconnect1.vercel.app/lab"
)

CoreClient.initialize(
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 26d731c

Please sign in to comment.