Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link mode: fix double redirect #1442

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading