Skip to content

Commit

Permalink
Fetch key when there's intention to pair
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Aug 6, 2024
1 parent 1eca932 commit 321f24b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class CoreProtocol(private val koinApp: KoinApplication = wcKoinApp) : CoreInter
)
}

Verify.initialize()
Pairing.initialize()
PairingController.initialize()
Verify.initialize()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ internal class PairingEngine(
private var jsonRpcRequestsJob: Job? = null
private val setOfRegisteredMethods: MutableSet<String> = mutableSetOf()
private val _isPairingStateFlow: MutableStateFlow<Boolean> = MutableStateFlow(false)

private val _deletedPairingFlow: MutableSharedFlow<Pairing> = MutableSharedFlow()
val deletedPairingFlow: SharedFlow<Pairing> = _deletedPairingFlow.asSharedFlow()
private val _inactivePairingTopicFlow: MutableSharedFlow<Pair<Topic, MutableList<String>>> = MutableSharedFlow()
val inactivePairingTopicFlow: SharedFlow<Pair<Topic, MutableList<String>>> = _inactivePairingTopicFlow.asSharedFlow()
val internalErrorFlow = MutableSharedFlow<SDKError>()
private val _checkVerifyKeyFlow: MutableSharedFlow<Unit> = MutableSharedFlow()
val checkVerifyKeyFlow: SharedFlow<Unit> = _checkVerifyKeyFlow.shareIn(scope, SharingStarted.Lazily, 1)

private val _engineEvent: MutableSharedFlow<EngineDO> = MutableSharedFlow()
val engineEvent: SharedFlow<EngineDO> =
merge(_engineEvent, _deletedPairingFlow.map { pairing -> EngineDO.PairingExpire(pairing) }, _isPairingStateFlow.map { EngineDO.PairingState(it) })
.shareIn(scope, SharingStarted.Lazily, 0)

private val _inactivePairingTopicFlow: MutableSharedFlow<Pair<Topic, MutableList<String>>> = MutableSharedFlow()
val inactivePairingTopicFlow: SharedFlow<Pair<Topic, MutableList<String>>> = _inactivePairingTopicFlow.asSharedFlow()

val internalErrorFlow = MutableSharedFlow<SDKError>()

// TODO: emission of events can be missed since they are emitted potentially before there's a subscriber and the event gets missed by protocols
init {
Expand Down Expand Up @@ -160,6 +160,7 @@ internal class PairingEngine(
}

fun pair(uri: String, onSuccess: () -> Unit, onFailure: (Throwable) -> Unit) {
scope.launch { _checkVerifyKeyFlow.emit(Unit) }
val trace: MutableList<String> = mutableListOf()
trace.add(Trace.Pairing.PAIRING_STARTED).also { logger.log("Pairing started") }
val walletConnectUri: WalletConnectUri = Validator.validateWCUri(uri) ?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class PairingController(private val koinApp: KoinApplication = wcKoinAp
override val deletedPairingFlow: SharedFlow<Pairing> by lazy { pairingEngine.deletedPairingFlow }
override val findWrongMethodsFlow: Flow<SDKError> by lazy { merge(pairingEngine.internalErrorFlow, pairingEngine.jsonRpcErrorFlow) }
override val inactivePairingFlow: SharedFlow<Pair<Topic, MutableList<String>>> by lazy { pairingEngine.inactivePairingTopicFlow }
override val checkVerifyKeyFlow: SharedFlow<Unit> by lazy { pairingEngine.checkVerifyKeyFlow }

override fun initialize() {
pairingEngine = koinApp.koin.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface PairingControllerInterface {
val deletedPairingFlow: SharedFlow<Pairing>
val findWrongMethodsFlow: Flow<SDKError>
val inactivePairingFlow: SharedFlow<Pair<Topic, MutableList<String>>>
val checkVerifyKeyFlow: SharedFlow<Unit>

fun initialize()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.walletconnect.android.verify.client

import com.walletconnect.android.internal.common.di.verifyModule
import com.walletconnect.android.internal.common.wcKoinApp
import com.walletconnect.android.pairing.handler.PairingControllerInterface
import com.walletconnect.android.verify.domain.VerifyRepository
import com.walletconnect.android.verify.domain.VerifyResult
import kotlinx.coroutines.CoroutineScope
Expand All @@ -15,12 +16,15 @@ internal class VerifyClient(
private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
) : VerifyInterface {
private val verifyRepository by lazy { koinApp.koin.get<VerifyRepository>() }
private val pairingController: PairingControllerInterface by lazy { koinApp.koin.get() }

override fun initialize() {
koinApp.modules(verifyModule())

scope.launch {
verifyRepository.getVerifyPublicKey().onFailure { throwable -> println("Error fetching a key: ${throwable.message}") }
pairingController.checkVerifyKeyFlow.collect {
verifyRepository.getVerifyPublicKey().onFailure { throwable -> println("Error fetching a key: ${throwable.message}") }
}
}
}

Expand Down

0 comments on commit 321f24b

Please sign in to comment.