|
| 1 | +package com.danubetech.verifiablecredentials; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.LinkedHashMap; |
| 6 | + |
| 7 | +import com.danubetech.verifiablecredentials.jwt.JwtVerifiableCredential; |
| 8 | +import com.fasterxml.jackson.core.JsonGenerationException; |
| 9 | +import com.fasterxml.jackson.core.JsonParseException; |
| 10 | +import com.github.jsonldjava.core.JsonLdConsts; |
| 11 | +import com.github.jsonldjava.utils.JsonUtils; |
| 12 | + |
| 13 | +public class VerifiablePresentation { |
| 14 | + |
| 15 | + public static final String JSONLD_CONTEXT_CREDENTIALS = "https://www.w3.org/2018/credentials/v1"; |
| 16 | + public static final String JSONLD_CONTEXT_CREDENTIALS_NO_WWW = "https://w3.org/2018/credentials/v1"; |
| 17 | + public static final String JSONLD_TYPE_VERIFIABLE_PRESENTATION = "VerifiablePresentation"; |
| 18 | + |
| 19 | + public static final String JSONLD_TERM_ID = "id"; |
| 20 | + public static final String JSONLD_TERM_TYPE = "type"; |
| 21 | + |
| 22 | + public static final String JSONLD_TERM_VERIFIABLE_CREDENTIAL = "verifiableCredential"; |
| 23 | + |
| 24 | + private final LinkedHashMap<String, Object> jsonLdObject; |
| 25 | + |
| 26 | + private VerifiablePresentation(LinkedHashMap<String, Object> jsonLdObject, boolean validate) { |
| 27 | + |
| 28 | + this.jsonLdObject = jsonLdObject; |
| 29 | + |
| 30 | + if (validate) this.validate(); |
| 31 | + } |
| 32 | + |
| 33 | + public VerifiablePresentation() { |
| 34 | + |
| 35 | + ArrayList<Object> context = new ArrayList<Object> (); |
| 36 | + context.add(JSONLD_CONTEXT_CREDENTIALS); |
| 37 | + |
| 38 | + ArrayList<String> type = new ArrayList<String> (); |
| 39 | + type.add(JSONLD_TYPE_VERIFIABLE_PRESENTATION); |
| 40 | + |
| 41 | + ArrayList<String> verifiableCredential = new ArrayList<String> (); |
| 42 | + |
| 43 | + this.jsonLdObject = new LinkedHashMap<String, Object> (); |
| 44 | + this.jsonLdObject.put(JsonLdConsts.CONTEXT, context); |
| 45 | + this.jsonLdObject.put(JSONLD_TERM_TYPE, type); |
| 46 | + this.jsonLdObject.put(JSONLD_TERM_VERIFIABLE_CREDENTIAL, verifiableCredential); |
| 47 | + } |
| 48 | + |
| 49 | + public static VerifiablePresentation fromJsonLdObject(LinkedHashMap<String, Object> jsonLdObject, boolean validate) { |
| 50 | + |
| 51 | + return new VerifiablePresentation(jsonLdObject, validate); |
| 52 | + } |
| 53 | + |
| 54 | + public static VerifiablePresentation fromJsonString(String jsonString, boolean validate) throws JsonParseException, IOException { |
| 55 | + |
| 56 | + LinkedHashMap<String, Object> jsonLdObject = (LinkedHashMap<String, Object>) JsonUtils.fromString(jsonString); |
| 57 | + |
| 58 | + return fromJsonLdObject(jsonLdObject, validate); |
| 59 | + } |
| 60 | + |
| 61 | + public static VerifiablePresentation fromJsonString(String jsonString) throws JsonParseException, IOException { |
| 62 | + |
| 63 | + return fromJsonString(jsonString, true); |
| 64 | + } |
| 65 | + |
| 66 | + public static VerifiablePresentation fromVerifiableCredential(VerifiableCredential verifiableCredential, boolean validate) throws JsonGenerationException, IOException { |
| 67 | + |
| 68 | + LinkedHashMap<String, Object> jsonLdObject = new LinkedHashMap<String, Object> (); |
| 69 | + |
| 70 | + ArrayList<Object> contextList = new ArrayList<Object> (); |
| 71 | + contextList.add(JSONLD_CONTEXT_CREDENTIALS); |
| 72 | + |
| 73 | + ArrayList<String> typeList = new ArrayList<String> (); |
| 74 | + typeList.add(JSONLD_TYPE_VERIFIABLE_PRESENTATION); |
| 75 | + |
| 76 | + ArrayList<String> verifiableCredentialList = new ArrayList<String> (); |
| 77 | + verifiableCredentialList.add(verifiableCredential.toJsonString()); |
| 78 | + |
| 79 | + jsonLdObject = new LinkedHashMap<String, Object> (); |
| 80 | + jsonLdObject.put(JsonLdConsts.CONTEXT, contextList); |
| 81 | + jsonLdObject.put(JSONLD_TERM_TYPE, typeList); |
| 82 | + jsonLdObject.put(JSONLD_TERM_VERIFIABLE_CREDENTIAL, verifiableCredentialList); |
| 83 | + |
| 84 | + return fromJsonLdObject(jsonLdObject, validate); |
| 85 | + } |
| 86 | + |
| 87 | + public static VerifiablePresentation fromVerifiableCredential(VerifiableCredential verifiableCredential) throws JsonParseException, IOException { |
| 88 | + |
| 89 | + return fromVerifiableCredential(verifiableCredential, true); |
| 90 | + } |
| 91 | + |
| 92 | + public static VerifiablePresentation fromJwtVerifiableCredential(JwtVerifiableCredential jwtVerifiableCredential, boolean validate) { |
| 93 | + |
| 94 | + LinkedHashMap<String, Object> jsonLdObject = new LinkedHashMap<String, Object> (); |
| 95 | + |
| 96 | + ArrayList<Object> contextList = new ArrayList<Object> (); |
| 97 | + contextList.add(JSONLD_CONTEXT_CREDENTIALS); |
| 98 | + |
| 99 | + ArrayList<String> typeList = new ArrayList<String> (); |
| 100 | + typeList.add(JSONLD_TYPE_VERIFIABLE_PRESENTATION); |
| 101 | + |
| 102 | + ArrayList<String> verifiableCredentialList = new ArrayList<String> (); |
| 103 | + verifiableCredentialList.add(jwtVerifiableCredential.getCompactSerialization()); |
| 104 | + |
| 105 | + jsonLdObject = new LinkedHashMap<String, Object> (); |
| 106 | + jsonLdObject.put(JsonLdConsts.CONTEXT, contextList); |
| 107 | + jsonLdObject.put(JSONLD_TERM_TYPE, typeList); |
| 108 | + jsonLdObject.put(JSONLD_TERM_VERIFIABLE_CREDENTIAL, verifiableCredentialList); |
| 109 | + |
| 110 | + return fromJsonLdObject(jsonLdObject, validate); |
| 111 | + } |
| 112 | + |
| 113 | + public static VerifiablePresentation fromJwtVerifiableCredential(JwtVerifiableCredential jwtVerifiableCredential) { |
| 114 | + |
| 115 | + return fromJwtVerifiableCredential(jwtVerifiableCredential, true); |
| 116 | + } |
| 117 | + |
| 118 | + public LinkedHashMap<String, Object> getJsonLdObject() { |
| 119 | + |
| 120 | + return this.jsonLdObject; |
| 121 | + } |
| 122 | + |
| 123 | + public String toPrettyJsonString() throws JsonGenerationException, IOException { |
| 124 | + |
| 125 | + return JsonUtils.toPrettyString(this.jsonLdObject); |
| 126 | + } |
| 127 | + |
| 128 | + public String toJsonString() throws JsonGenerationException, IOException { |
| 129 | + |
| 130 | + return JsonUtils.toString(this.jsonLdObject); |
| 131 | + } |
| 132 | + |
| 133 | + /* |
| 134 | + * Validation |
| 135 | + */ |
| 136 | + |
| 137 | + public void validate() throws IllegalStateException { |
| 138 | + |
| 139 | + } |
| 140 | +} |
0 commit comments