Skip to content

Commit

Permalink
Lock fetching pubKey
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Aug 6, 2024
1 parent d6e395d commit 1eca932
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,55 +21,59 @@ internal class VerifyRepository(
private val verifyPublicKeyStorageRepository: VerifyPublicKeyStorageRepository,
private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
) {
suspend fun getVerifyPublicKey(): Result<String> = runCatching {
val (localPublicKey, expiresAt) = verifyPublicKeyStorageRepository.getPublicKey()
val publicKey = if (!isLocalKeyValid(localPublicKey, expiresAt)) {
fetchAndCacheKey()
} else {
localPublicKey!!
private val mutex = Mutex()
suspend fun getVerifyPublicKey(): Result<String> = 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)
}
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Web3WalletActivity : AppCompatActivity() {

private suspend fun navigateWhenReady(navigate: () -> Unit) {
if (!::navController.isInitialized) {
delay(200)
delay(300)
navigate()
} else {
navigate()
Expand Down

0 comments on commit 1eca932

Please sign in to comment.