Skip to content

Commit

Permalink
add sd jwt to credential pack
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <ryan.tate@spruceid.com>
  • Loading branch information
Ryanmtate committed Oct 16, 2024
1 parent a7aa2f5 commit ce4f899
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
25 changes: 25 additions & 0 deletions MobileSdk/src/main/java/com/spruceid/mobile/sdk/Credential.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,6 +98,30 @@ fun JsonVc.credentialClaimsFiltered(claimNames: List<String>): 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<String>): 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<String>): Any {
try {
val firstKey = path.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -44,6 +45,14 @@ class CredentialPack {
return credentials
}

/**
* Add a SD-JWT to the CredentialPack.
*/
fun addSdJwt(sdJwt: SdJwt): List<ParsedCredential> {
credentials.add(ParsedCredential.newSdJwt(sdJwt))
return credentials
}

/**
* Find claims from all credentials in this CredentialPack.
*/
Expand All @@ -54,17 +63,20 @@ 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)
} else if (jwtVc != null) {
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"
}
Expand Down

0 comments on commit ce4f899

Please sign in to comment.