diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/W3CVCHolder.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/W3CVCHolder.java new file mode 100644 index 0000000..e8b986b --- /dev/null +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/W3CVCHolder.java @@ -0,0 +1,39 @@ +package org.oneedtech.inspect.vc; + +import java.util.List; + +import org.oneedtech.inspect.vc.jsonld.JsonLDObjectUtils; + +import com.danubetech.verifiablecredentials.VerifiableCredential; + +import foundation.identity.jsonld.ConfigurableDocumentLoader; +import info.weboftrust.ldsignatures.LdProof; + +/** + * Holder for W3C's Verifiable Credential + */ +public class W3CVCHolder { + private VerifiableCredential credential; + + public W3CVCHolder(VerifiableCredential credential) { + this.credential = credential; + ConfigurableDocumentLoader documentLoader = new ConfigurableDocumentLoader(); + documentLoader.setEnableHttp(true); + documentLoader.setEnableHttps(true); + credential.setDocumentLoader(documentLoader); + } + + /** + * Get the list of proofs in the credential. + * {@link VerifiableCredential} contains the method getLdProof(), but only works with one proof. This methods + * returns a list of all proofs defined in the credential. + * @return proofs defined in the credential + */ + public List getLdProofs() { + return JsonLDObjectUtils.getListFromJsonLDObject(LdProof.class, credential); + } + + public VerifiableCredential getCredential() { + return credential; + } +} diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/JsonLDObjectUtils.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/JsonLDObjectUtils.java new file mode 100644 index 0000000..398ccc2 --- /dev/null +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/jsonld/JsonLDObjectUtils.java @@ -0,0 +1,45 @@ +package org.oneedtech.inspect.vc.jsonld; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import foundation.identity.jsonld.JsonLDObject; + +public class JsonLDObjectUtils { + @SuppressWarnings("unchecked") + public static List getListFromJsonLDObject(Class cl, JsonLDObject jsonLdObject) { + String term = JsonLDObject.getDefaultJsonLDPredicate(cl); + List> jsonObjects = jsonLdGetJsonObjectList(jsonLdObject.getJsonObject(), term); + if (jsonObjects == null) return null; + try { + Method method = cl.getMethod("fromMap", Map.class); + return jsonObjects.stream().map(jsonObject -> { + try { + return (C) method.invoke(null, jsonObject); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new Error(e); + } + }).collect(Collectors.toList()); + } catch (NoSuchMethodException | SecurityException e) { + throw new Error(e); + } + } + + @SuppressWarnings("unchecked") + public static List> jsonLdGetJsonObjectList(Map jsonObject, String term) { + Object entry = jsonObject.get(term); + if (entry == null) return null; + + if (entry instanceof Map) { + return Collections.singletonList((Map) entry); + } else if (entry instanceof List && ((List) entry).stream().allMatch(e -> e instanceof Map)) { + return (List>) (List>) entry; + } else { + throw new IllegalArgumentException("Cannot get json object '" + term + "' from " + jsonObject); + } + } +}