From ce4f899d694ae88bd24a28360364dc41f289c68c Mon Sep 17 00:00:00 2001 From: Ryan Tate Date: Wed, 16 Oct 2024 00:53:35 -0400 Subject: [PATCH] add sd jwt to credential pack Signed-off-by: Ryan Tate --- .../com/spruceid/mobile/sdk/Credential.kt | 25 +++++++++++++++++++ .../com/spruceid/mobile/sdk/CredentialPack.kt | 14 ++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/MobileSdk/src/main/java/com/spruceid/mobile/sdk/Credential.kt b/MobileSdk/src/main/java/com/spruceid/mobile/sdk/Credential.kt index f22db64..5b329b9 100644 --- a/MobileSdk/src/main/java/com/spruceid/mobile/sdk/Credential.kt +++ b/MobileSdk/src/main/java/com/spruceid/mobile/sdk/Credential.kt @@ -3,6 +3,7 @@ package com.spruceid.mobile.sdk import com.spruceid.mobile.sdk.rs.JsonVc import com.spruceid.mobile.sdk.rs.JwtVc import com.spruceid.mobile.sdk.rs.Mdoc +import com.spruceid.mobile.sdk.rs.SdJwt import org.json.JSONException import org.json.JSONObject import org.json.JSONTokener @@ -97,6 +98,30 @@ fun JsonVc.credentialClaimsFiltered(claimNames: List): JSONObject { return new } +/** + * Access the SD-JWT credential. + */ +fun SdJwt.credentialClaims(): JSONObject { + try { + return JSONObject(this.decodeRevealJsonString()) + } catch (e: Error) { + print("failed to decode SD-JWT data from UTF-8-encoded JSON") + return JSONObject() + } +} + +/** + * Access the specified claims from the SD-JWT credential. + */ +fun SdJwt.credentialClaimsFiltered(claimNames: List): JSONObject { + val old = this.credentialClaims() + val new = JSONObject() + for (name in claimNames) { + new.put(name, keyPathFinder(old, name.split(".").toMutableList())) + } + return new +} + private fun keyPathFinder(json: Any, path: MutableList): Any { try { val firstKey = path.first() diff --git a/MobileSdk/src/main/java/com/spruceid/mobile/sdk/CredentialPack.kt b/MobileSdk/src/main/java/com/spruceid/mobile/sdk/CredentialPack.kt index acb52e2..e7d5d0c 100644 --- a/MobileSdk/src/main/java/com/spruceid/mobile/sdk/CredentialPack.kt +++ b/MobileSdk/src/main/java/com/spruceid/mobile/sdk/CredentialPack.kt @@ -4,6 +4,7 @@ import com.spruceid.mobile.sdk.rs.JsonVc import com.spruceid.mobile.sdk.rs.JwtVc import com.spruceid.mobile.sdk.rs.Mdoc import com.spruceid.mobile.sdk.rs.ParsedCredential +import com.spruceid.mobile.sdk.rs.SdJwt import org.json.JSONObject /** @@ -44,6 +45,14 @@ class CredentialPack { return credentials } + /** + * Add a SD-JWT to the CredentialPack. + */ + fun addSdJwt(sdJwt: SdJwt): List { + credentials.add(ParsedCredential.newSdJwt(sdJwt)) + return credentials + } + /** * Find claims from all credentials in this CredentialPack. */ @@ -54,6 +63,7 @@ class CredentialPack { val mdoc = credential.asMsoMdoc() val jwtVc = credential.asJwtVc() val jsonVc = credential.asJsonVc() + val sdJwt = credential.asSdJwt() if (mdoc != null) { claims = mdoc.jsonEncodedDetailsFiltered(claimNames) @@ -61,10 +71,12 @@ class CredentialPack { claims = jwtVc.credentialClaimsFiltered(claimNames) } else if (jsonVc != null) { claims = jsonVc.credentialClaimsFiltered(claimNames) + } else if (sdJwt != null) { + claims = sdJwt.credentialClaimsFiltered(claimNames) } else { var type: String try { - type = credential.intoGenericForm().type + type = credential.intoGenericForm().type() } catch (e: Error) { type = "unknown" }