Skip to content

Commit

Permalink
fix: wrong casting KeyPair instead of PublicKey (#139)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmed Moussa <ahmed.moussa@iohk.io>
Signed-off-by: Cristian G <cristian.castro@iohk.io>
  • Loading branch information
cristianIOHK and hamada147 committed Aug 26, 2024
1 parent 57eeefc commit 4e5ea2c
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.iohk.atala.prism.walletsdk.castor

import io.iohk.atala.prism.walletsdk.apollo.utils.Ed25519KeyPair
import io.iohk.atala.prism.walletsdk.apollo.utils.Ed25519PublicKey
import io.iohk.atala.prism.walletsdk.apollo.utils.Secp256k1KeyPair
import io.iohk.atala.prism.walletsdk.apollo.utils.Secp256k1PublicKey
import io.iohk.atala.prism.walletsdk.castor.resolvers.LongFormPrismDIDResolver
import io.iohk.atala.prism.walletsdk.castor.resolvers.PeerDIDResolver
Expand Down Expand Up @@ -102,7 +100,13 @@ constructor(
override suspend fun resolveDID(did: String): DIDDocument {
logger.debug(
message = "Trying to resolve DID",
metadata = arrayOf(Metadata.MaskedMetadataByLevel(key = "DID", value = did, level = LogLevel.DEBUG))
metadata = arrayOf(
Metadata.MaskedMetadataByLevel(
key = "DID",
value = did,
level = LogLevel.DEBUG
)
)
)
val resolver = CastorShared.getDIDResolver(did, resolvers)
return resolver.resolve(did)
Expand All @@ -120,25 +124,27 @@ constructor(
* @throws [CastorError.InvalidKeyError] if the DID or signature data are invalid.
*/
@Throws(CastorError.InvalidKeyError::class)
override suspend fun verifySignature(did: DID, challenge: ByteArray, signature: ByteArray): Boolean {
override suspend fun verifySignature(
did: DID,
challenge: ByteArray,
signature: ByteArray
): Boolean {
val document = resolveDID(did.toString())
val keyPairs: List<PublicKey> =
val publicKeys: List<PublicKey> =
CastorShared.getKeyPairFromCoreProperties(document.coreProperties)

if (keyPairs.isEmpty()) {
if (publicKeys.isEmpty()) {
throw CastorError.InvalidKeyError("KeyPairs is empty")
}

for (keyPair in keyPairs) {
when (keyPair.getCurve()) {
for (publicKey in publicKeys) {
when (publicKey.getCurve()) {
Curve.SECP256K1.value -> {
val secp = keyPair as Secp256k1KeyPair
return (secp.publicKey as Secp256k1PublicKey).verify(challenge, signature)
return (publicKey as Secp256k1PublicKey).verify(challenge, signature)
}

Curve.ED25519.value -> {
val ed = keyPair as Ed25519KeyPair
return (ed.publicKey as Ed25519PublicKey).verify(challenge, signature)
return (publicKey as Ed25519PublicKey).verify(challenge, signature)
}
}
}
Expand Down

0 comments on commit 4e5ea2c

Please sign in to comment.