Skip to content

Commit

Permalink
finishing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianIOHK committed Apr 9, 2024
1 parent 6373053 commit 4490d1f
Show file tree
Hide file tree
Showing 6 changed files with 345 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface JWTPayload {
*/
@Serializable
data class JWTVerifiableCredential @JvmOverloads constructor(
@SerialName("@context")
val context: Array<String> = arrayOf(),
val type: Array<String> = arrayOf(),
val credentialSchema: VerifiableCredentialTypeContainer? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class PolluxImpl(
@Throws(PolluxError.InvalidCredentialError::class)
fun parseVerifiableCredential(data: String): Credential {
return try {
JWTCredential.fromJsonString(data)
JWTCredential.fromJwtString(data)
} catch (e: Exception) {
try {
Json.decodeFromString<W3CCredential>(data)
Expand All @@ -126,7 +126,7 @@ class PolluxImpl(
): Credential {
return when (type) {
CredentialType.JWT -> {
JWTCredential.fromJsonString(jsonData)
JWTCredential.fromJwtString(jsonData)
}

CredentialType.ANONCREDS_ISSUE -> {
Expand Down Expand Up @@ -176,7 +176,7 @@ class PolluxImpl(
): Credential {
return when (restorationIdentifier) {
"jwt+credential" -> {
JWTCredential.fromJsonString(credentialData.decodeToString())
JWTCredential.fromJwtString(credentialData.decodeToString())
}

"anon+credential" -> {
Expand Down Expand Up @@ -479,7 +479,7 @@ class PolluxImpl(
* @param privateKey The PrivateKey to parse.
* @return The parsed ECPrivateKey.
*/
private fun parsePrivateKey(privateKey: PrivateKey): ECPrivateKey {
internal fun parsePrivateKey(privateKey: PrivateKey): ECPrivateKey {
val curveName = KMMEllipticCurve.SECP256k1.value
val sp = ECNamedCurveTable.getParameterSpec(curveName)
val params: ECParameterSpec = ECNamedCurveSpec(sp.name, sp.curve, sp.g, sp.n, sp.h)
Expand Down Expand Up @@ -546,7 +546,7 @@ class PolluxImpl(
* @param challenge The challenge value for the JWT.
* @return The signed JWT as a string.
*/
private fun signClaimsProofPresentationJWT(
internal fun signClaimsProofPresentationJWT(
subjectDID: DID,
privateKey: ECPrivateKey,
credential: Credential,
Expand Down Expand Up @@ -742,7 +742,7 @@ class PolluxImpl(
val holderJws =
descriptorMap.getValue(descriptorItem.path)
?: throw Exception("Could not find ${descriptorItem.path} value") // TODO: Custom error
val presentation = JWTCredential.fromJsonString(holderJws as String)
val presentation = JWTCredential.fromJwtString(holderJws as String)
presentation.verifiablePresentation?.let { verifiablePresentation ->

// TODO: Should type be CredentialType.JWT.type?
Expand All @@ -763,7 +763,7 @@ class PolluxImpl(

val vcJws = verifiablePresentation.verifiableCredential.firstOrNull()
?: throw Exception("VC credential not found") // TODO: Custom error
val jwtCredential = JWTCredential.fromJsonString(vcJws)
val jwtCredential = JWTCredential.fromJwtString(vcJws)
val didDocIssuer = castor.resolveDID(jwtCredential.issuer)
val authenticationMethodIssuer =
didDocIssuer.coreProperties.find { it::class == DIDDocument.Authentication::class }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.iohk.atala.prism.walletsdk.pollux.models

import io.iohk.atala.prism.apollo.base64.base64UrlDecoded
import io.iohk.atala.prism.didcomm.didpeer.core.toJsonElement
import io.iohk.atala.prism.walletsdk.domain.VC
import io.iohk.atala.prism.walletsdk.domain.models.Claim
import io.iohk.atala.prism.walletsdk.domain.models.ClaimType
Expand All @@ -13,7 +14,12 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

@Serializable
/**
Expand All @@ -35,16 +41,12 @@ data class JWTCredential(
override val exp: Long?,
override val jti: String?,
override val aud: String?,
override val originalJWTString: String?
) : Credential, JWTPayload {
// private var jwtString: String = data


override val originalJWTString: String?,
@SerialName("vp")
override var verifiablePresentation: JWTVerifiablePresentation? = null

override var verifiablePresentation: JWTVerifiablePresentation? = null,
@SerialName(VC)
override var verifiableCredential: JWTVerifiableCredential? = null
) : Credential, JWTPayload {

@Transient
override val issuer: String = iss
Expand Down Expand Up @@ -178,7 +180,7 @@ data class JWTCredential(

companion object {
@JvmStatic
fun fromJsonString(jwtString: String): JWTCredential {
fun fromJwtString(jwtString: String): JWTCredential {
val jwtParts = jwtString.split(".")
require(jwtParts.size == 3) { "Invalid JWT string" }
val credentialString = jwtParts[1]
Expand All @@ -188,7 +190,37 @@ data class JWTCredential(
ignoreUnknownKeys = true
explicitNulls = false
}
return json.decodeFromString<JWTCredential>(jsonString)

val jsonObject = Json.decodeFromString<JsonElement>(jsonString).jsonObject
return json.decodeFromJsonElement(jsonObject.plus("id" to jsonString).toJsonElement())

// val iss = jsonObject["iss"]?.jsonPrimitive?.content ?: throw Exception() //TODO: Custom exception
// val sub = jsonObject["sub"]?.jsonPrimitive?.content
// val nbf = jsonObject["nbf"]?.jsonPrimitive?.content?.toLong()
// val exp = jsonObject["exp"]?.jsonPrimitive?.content?.toLong()
// val aud = jsonObject["aud"]?.jsonPrimitive?.content
// val vp = if (jsonObject.containsKey("vp")) {
// json.decodeFromJsonElement<JWTVerifiablePresentation>(jsonObject["vp"]!!)
// } else {
// null
// }
// val vc = if (jsonObject.containsKey("vc")) {
// json.decodeFromJsonElement<JWTVerifiableCredential>(jsonObject["vc"]!!)
// } else {
// null
// }
// return JWTCredential(
// id = jsonString,
// iss = iss,
// sub = sub,
// nbf = nbf,
// exp = exp,
// jti = jsonString,
// aud = aud,
// originalJWTString = jsonString,
// verifiableCredential = vc,
// verifiablePresentation = vp
// )
}
}
}
Loading

0 comments on commit 4490d1f

Please sign in to comment.