Skip to content

Commit

Permalink
Fix handling session requests in sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Jan 19, 2024
1 parent 196661c commit 12c98b9
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ internal class RespondSessionRequestUseCase(
logger.log("Sending session request response on topic: $topic, id: ${jsonRpcResponse.id}")
jsonRpcInteractor.publishJsonRpcResponse(topic = Topic(topic), params = irnParams, response = jsonRpcResponse,
onSuccess = {
onSuccess()
logger.log("Session request response sent successfully on topic: $topic, id: ${jsonRpcResponse.id}")
scope.launch {
supervisorScope {
removePendingSessionRequestAndEmit(jsonRpcResponse.id)
}
}
onSuccess()
},
onFailure = { error ->
logger.error("Sending session response error: $error, id: ${jsonRpcResponse.id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ internal class SessionRequestUseCase(
}

val nowInSeconds = TimeUnit.SECONDS.convert(Date().time, TimeUnit.SECONDS)
println("kobe: Sending expiry: ${request.expiry}")
if (!CoreValidator.isExpiryWithinBounds(request.expiry)) {
logger.error("Sending session request error: expiry not within bounds")
return@supervisorScope onFailure(InvalidExpiryException())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ internal class OnSessionRequestUseCase(
logger.log("Session request received on topic: ${request.topic}")

try {
println("kobe: Request expiry: ${params.request.expiryTimestamp}")
params.request.expiryTimestamp?.let {
if (Expiry(it).isExpired()) {
logger.error("Session request received failure on topic: ${request.topic} - request expired")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object WCDelegate : Web3Wallet.WalletDelegate, CoreClient.CoreDelegate {
var authRequestEvent: Pair<Wallet.Model.AuthRequest, Wallet.Model.VerifyContext>? = null
var sessionProposalEvent: Pair<Wallet.Model.SessionProposal, Wallet.Model.VerifyContext>? = null
var sessionRequestEvent: Pair<Wallet.Model.SessionRequest, Wallet.Model.VerifyContext>? = null
var currentId: Long? = null

init {
CoreClient.setDelegate(this)
Expand Down Expand Up @@ -71,13 +72,12 @@ object WCDelegate : Web3Wallet.WalletDelegate, CoreClient.CoreDelegate {
}

override fun onSessionRequest(sessionRequest: Wallet.Model.SessionRequest, verifyContext: Wallet.Model.VerifyContext) {
if (currentId != sessionRequest.request.id) {
sessionRequestEvent = Pair(sessionRequest, verifyContext)

println("kobe: YES onSessionRequest: ${sessionRequest.request.id}")

sessionRequestEvent = Pair(sessionRequest, verifyContext)

scope.launch {
_walletEvents.emit(sessionRequest)
scope.launch {
_walletEvents.emit(sessionRequest)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,20 @@ class Web3WalletActivity : AppCompatActivity() {
web3walletViewModel: Web3WalletViewModel,
connectionsViewModel: ConnectionsViewModel,
) {
web3walletViewModel.sessionRequestStateFlow
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.onEach {
if (it.arrayOfArgs.isNotEmpty()){
navController.navigate(Route.SessionRequest.path)
}
}
.launchIn(lifecycleScope)

web3walletViewModel.walletEvents
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.onEach { event ->
when (event) {
is SignEvent.SessionProposal -> navController.navigate(Route.SessionProposal.path)
is SignEvent.SessionRequest -> {
println("kobe: navigate to session request")
navController.navigate(Route.SessionRequest.path)
}
is SignEvent.ExpiredRequest -> {
navController.popBackStack(route = Route.Connections.path, inclusive = false)
Toast.makeText(baseContext, "Request expired", Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Web3WalletViewModel : ViewModel() {
private val _timerFlow: MutableStateFlow<String> = MutableStateFlow("0")
val timerFlow = _timerFlow.asStateFlow()

private val _sessionRequestStateFlow: MutableSharedFlow<SignEvent.SessionRequest> = MutableSharedFlow()
val sessionRequestStateFlow = _sessionRequestStateFlow.asSharedFlow()

init {
WCDelegate.coreEvents.onEach { coreEvent ->
_isLoadingFlow.value = (coreEvent as? Core.Model.PairingState)?.isPairingState ?: false
Expand Down Expand Up @@ -85,10 +88,11 @@ class Web3WalletViewModel : ViewModel() {
val chain = wcEvent.chainId
val method = wcEvent.request.method
val arrayOfArgs: ArrayList<String?> = arrayListOf(topic, icon, peerName, requestId, params, chain, method)

println("kobe: wallet request: ${wcEvent.request.id}")

SignEvent.SessionRequest(arrayOfArgs, arrayOfArgs.size)
if (WCDelegate.currentId != WCDelegate.sessionRequestEvent?.first?.request?.id) {
_sessionRequestStateFlow.emit(SignEvent.SessionRequest(arrayOfArgs, arrayOfArgs.size))
} else {
println("wallet request already there: ${wcEvent.request.id}")
}
}

is Wallet.Model.AuthRequest -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.walletconnect.sample.wallet.ui.routes.dialog_routes.session_request

import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -34,6 +35,7 @@ import com.walletconnect.sample.common.sendResponseDeepLink
import com.walletconnect.sample.common.ui.theme.PreviewTheme
import com.walletconnect.sample.common.ui.theme.verified_color
import com.walletconnect.sample.common.ui.themedColor
import com.walletconnect.sample.wallet.domain.WCDelegate.currentId
import com.walletconnect.sample.wallet.ui.common.Buttons
import com.walletconnect.sample.wallet.ui.common.Content
import com.walletconnect.sample.wallet.ui.common.InnerContent
Expand All @@ -42,6 +44,7 @@ import com.walletconnect.sample.wallet.ui.common.blue.BlueLabelRow
import com.walletconnect.sample.wallet.ui.common.peer.Peer
import com.walletconnect.sample.wallet.ui.common.peer.PeerUI
import com.walletconnect.sample.wallet.ui.common.peer.getColor
import com.walletconnect.sample.wallet.ui.routes.Route
import com.walletconnect.sample.wallet.ui.routes.showSnackbar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -57,20 +60,18 @@ fun SessionRequestRoutePreview() {
}


@SuppressLint("RestrictedApi")
@Composable
fun SessionRequestRoute(navController: NavHostController, sessionRequestViewModel: SessionRequestViewModel = viewModel()) {
val sessionRequestUI = sessionRequestViewModel.sessionRequest
val composableScope = rememberCoroutineScope()
val context = LocalContext.current
var isConfirmLoading by remember { mutableStateOf(false) }
var isCancelLoading by remember { mutableStateOf(false) }

when (sessionRequestUI) {
is SessionRequestUI.Content -> {
val allowButtonColor = getColor(sessionRequestUI.peerContextUI)
//pop init dialog
//get one before current and pop?
println("kobe: session request content")
currentId = sessionRequestUI.requestId
SemiTransparentDialog {
Spacer(modifier = Modifier.height(24.dp))
Peer(peerUI = sessionRequestUI.peerUI, "sends a request", sessionRequestUI.peerContextUI)
Expand All @@ -85,14 +86,13 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
sessionRequestViewModel.approve(
onSuccess = { uri ->
isConfirmLoading = false
composableScope.launch(Dispatchers.Main) {
navController.popBackStack(route = Route.Connections.path, inclusive = false)
}
if (uri != null) {
context.sendResponseDeepLink(uri)
}
composableScope.launch(Dispatchers.Main) {
// navController.popBackStack(route = Route.Connections.path, inclusive = false)
//if size == 1 -> navController.popBackStack(route = Route.Connections.path, inclusive = false)
navController.popBackStack()
}

},
onError = { error ->
isConfirmLoading = false
Expand All @@ -109,13 +109,12 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
sessionRequestViewModel.reject(
onSuccess = { uri ->
isCancelLoading = false
composableScope.launch(Dispatchers.Main) {
navController.popBackStack(route = Route.Connections.path, inclusive = false)
}
if (uri != null) {
context.sendResponseDeepLink(uri)
}
composableScope.launch(Dispatchers.Main) {
println("kobe: popping")
navController.popBackStack()
}
},
onError = { error ->
isCancelLoading = false
Expand All @@ -133,7 +132,6 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
}

SessionRequestUI.Initial -> {
println("kobe: session request initial")
SemiTransparentDialog {
Spacer(modifier = Modifier.height(24.dp))
Peer(peerUI = PeerUI.Empty, null)
Expand All @@ -152,13 +150,9 @@ fun SessionRequestRoute(navController: NavHostController, sessionRequestViewMode
isLoadingCancel = isCancelLoading
)
}
}

SessionRequestUI.Nothing -> {

}
}

}

private fun closeAndShowError(navController: NavHostController, message: String?, coroutineScope: CoroutineScope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import com.walletconnect.sample.wallet.ui.common.peer.PeerUI
sealed class SessionRequestUI {
object Initial : SessionRequestUI()

object Nothing : SessionRequestUI()

data class Content(
val peerUI: PeerUI,
val topic: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class SessionRequestViewModel : ViewModel() {

private fun clearSessionRequest() {
WCDelegate.sessionRequestEvent = null
sessionRequest = SessionRequestUI.Nothing
sessionRequest = SessionRequestUI.Initial
}

fun reject(onSuccess: (Uri?) -> Unit = {}, onError: (String) -> Unit = {}) { //callback z popem?
fun reject(onSuccess: (Uri?) -> Unit = {}, onError: (String) -> Unit = {}) {
try {
val sessionRequest = sessionRequest as? SessionRequestUI.Content
if (sessionRequest != null) {
Expand Down

0 comments on commit 12c98b9

Please sign in to comment.