From 1eca932e94fe78cfa233bea1487b0437676c0955 Mon Sep 17 00:00:00 2001 From: kubel Date: Tue, 6 Aug 2024 09:17:26 +0200 Subject: [PATCH] Lock fetching pubKey --- .../android/verify/client/VerifyClient.kt | 12 ++- .../android/verify/domain/VerifyRepository.kt | 76 ++++++++++--------- .../sample/wallet/ui/Web3WalletActivity.kt | 2 +- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/core/android/src/main/kotlin/com/walletconnect/android/verify/client/VerifyClient.kt b/core/android/src/main/kotlin/com/walletconnect/android/verify/client/VerifyClient.kt index 8f84c549f..ff727db03 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/verify/client/VerifyClient.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/verify/client/VerifyClient.kt @@ -25,11 +25,19 @@ internal class VerifyClient( } override fun resolve(attestationId: String, metadataUrl: String, onSuccess: (VerifyResult) -> Unit, onError: (Throwable) -> Unit) { - verifyRepository.resolve(attestationId, metadataUrl, onSuccess, onError) + try { + verifyRepository.resolve(attestationId, metadataUrl, onSuccess, onError) + } catch (e: Exception) { + onError(e) + } } override fun resolveV2(attestation: String, metadataUrl: String, onSuccess: (VerifyResult) -> Unit, onError: (Throwable) -> Unit) { - verifyRepository.resolveV2(attestation, metadataUrl, onSuccess, onError) + try { + verifyRepository.resolveV2(attestation, metadataUrl, onSuccess, onError) + } catch (e: Exception) { + onError(e) + } } override fun register(attestationId: String, onSuccess: () -> Unit, onError: (Throwable) -> Unit) { diff --git a/core/android/src/main/kotlin/com/walletconnect/android/verify/domain/VerifyRepository.kt b/core/android/src/main/kotlin/com/walletconnect/android/verify/domain/VerifyRepository.kt index 5e3a1ce4b..e4715c28b 100644 --- a/core/android/src/main/kotlin/com/walletconnect/android/verify/domain/VerifyRepository.kt +++ b/core/android/src/main/kotlin/com/walletconnect/android/verify/domain/VerifyRepository.kt @@ -11,6 +11,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import kotlinx.coroutines.supervisorScope +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock internal class VerifyRepository( private val verifyService: VerifyService, @@ -19,55 +21,59 @@ internal class VerifyRepository( private val verifyPublicKeyStorageRepository: VerifyPublicKeyStorageRepository, private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) ) { - suspend fun getVerifyPublicKey(): Result = runCatching { - val (localPublicKey, expiresAt) = verifyPublicKeyStorageRepository.getPublicKey() - val publicKey = if (!isLocalKeyValid(localPublicKey, expiresAt)) { - fetchAndCacheKey() - } else { - localPublicKey!! + private val mutex = Mutex() + suspend fun getVerifyPublicKey(): Result = mutex.withLock { + runCatching { + val (localPublicKey, expiresAt) = verifyPublicKeyStorageRepository.getPublicKey() + val publicKey = if (!isLocalKeyValid(localPublicKey, expiresAt)) { + fetchAndCacheKey() + } else { + localPublicKey!! + } + publicKey } - publicKey } fun resolveV2(attestationJWT: String, metadataUrl: String, onSuccess: (VerifyResult) -> Unit, onError: (Throwable) -> Unit) { scope.launch { supervisorScope { - getVerifyPublicKey() - .fold( - onSuccess = { key -> - if (jwtRepository.verifyJWT(attestationJWT, key.hexToBytes())) { - try { + getVerifyPublicKey().fold( + onSuccess = { key -> + if (jwtRepository.verifyJWT(attestationJWT, key.hexToBytes())) { + try { + val claims = moshi.adapter(VerifyClaims::class.java).fromJson(jwtRepository.decodeClaimsJWT(attestationJWT)) + if (claims == null) { + onError(IllegalArgumentException("Error while decoding JWT claims")) + return@supervisorScope + } + + onSuccess(VerifyResult(getValidation(claims, metadataUrl), claims.isScam, claims.origin)) + } catch (e: Exception) { + onError(e) + } + } else { + try { + val newKey = fetchAndCacheKey() + if (jwtRepository.verifyJWT(attestationJWT, newKey.hexToBytes())) { val claims = moshi.adapter(VerifyClaims::class.java).fromJson(jwtRepository.decodeClaimsJWT(attestationJWT)) if (claims == null) { onError(IllegalArgumentException("Error while decoding JWT claims")) - return@fold + return@supervisorScope } onSuccess(VerifyResult(getValidation(claims, metadataUrl), claims.isScam, claims.origin)) - } catch (e: Exception) { - onError(e) - } - } else { - try { - val newKey = fetchAndCacheKey() - if (jwtRepository.verifyJWT(attestationJWT, newKey.hexToBytes())) { - val claims = moshi.adapter(VerifyClaims::class.java).fromJson(jwtRepository.decodeClaimsJWT(attestationJWT)) - if (claims == null) { - onError(IllegalArgumentException("Error while decoding JWT claims")) - return@fold - } - - onSuccess(VerifyResult(getValidation(claims, metadataUrl), claims.isScam, claims.origin)) - } else { - onError(IllegalArgumentException("Error while verifying JWT")) - } - } catch (e: Exception) { - onError(e) + } else { + onError(IllegalArgumentException("Error while verifying JWT")) } + } catch (e: Exception) { + onError(e) } - }, - onFailure = { throwable -> onError(throwable) } - ) + } + }, + onFailure = { error -> + onError(error) + } + ) } } } diff --git a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/Web3WalletActivity.kt b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/Web3WalletActivity.kt index 2289a4cc9..e70780460 100644 --- a/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/Web3WalletActivity.kt +++ b/sample/wallet/src/main/kotlin/com/walletconnect/sample/wallet/ui/Web3WalletActivity.kt @@ -162,7 +162,7 @@ class Web3WalletActivity : AppCompatActivity() { private suspend fun navigateWhenReady(navigate: () -> Unit) { if (!::navController.isInitialized) { - delay(200) + delay(300) navigate() } else { navigate()