diff --git a/src/main/java/org/hyperledger/aries/api/credentials/CredentialAttributes.java b/src/main/java/org/hyperledger/aries/api/credentials/CredentialAttributes.java index 6923adc..946594e 100644 --- a/src/main/java/org/hyperledger/aries/api/credentials/CredentialAttributes.java +++ b/src/main/java/org/hyperledger/aries/api/credentials/CredentialAttributes.java @@ -32,12 +32,6 @@ public class CredentialAttributes { private String name; private String value; - public CredentialAttributes(String name, String value) { - super(); - this.name = name; - this.value = value; - } - public static List from(@NonNull T instance) { List result = new ArrayList<>(); Field[] fields = instance.getClass().getDeclaredFields(); @@ -60,7 +54,8 @@ public static List from(@NonNull T instance) { } catch (IllegalAccessException | IllegalArgumentException e) { log.error("Could not get value of field: {}", fieldName, e); } - result.add(new CredentialAttributes(fieldName, fieldValue)); + + result.add(CredentialAttributes.builder().name(fieldName).value(fieldValue).build()); } } } @@ -72,13 +67,13 @@ public static List from(@NonNull T instance) { public static List from(@NonNull Map values) { List result = new ArrayList<>(); // TODO check if complex object - values.forEach( (k,v) -> result.add(new CredentialAttributes(k, v.toString()))); + values.forEach( (k,v) -> result.add(CredentialAttributes.builder().name(k).value(v.toString()).build())); return result; } public static List fromMap(@NonNull Map values) { List result = new ArrayList<>(); - values.forEach( (k,v) -> result.add(new CredentialAttributes(k, v))); + values.forEach( (k,v) -> result.add(CredentialAttributes.builder().name(k).value(v).build())); return result; } diff --git a/src/main/java/org/hyperledger/aries/api/issue_credential_v1/CredentialFreeOfferHelper.java b/src/main/java/org/hyperledger/aries/api/issue_credential_v1/CredentialFreeOfferHelper.java index 51e16c2..be39d69 100644 --- a/src/main/java/org/hyperledger/aries/api/issue_credential_v1/CredentialFreeOfferHelper.java +++ b/src/main/java/org/hyperledger/aries/api/issue_credential_v1/CredentialFreeOfferHelper.java @@ -22,6 +22,7 @@ import org.hyperledger.aries.api.out_of_band.BaseOOBInvitationHelper; import java.io.IOException; +import java.util.List; import java.util.Map; /** @@ -51,23 +52,19 @@ public CredentialFreeOfferHelper(AriesClient acaPy) { public CredentialFreeOffer buildV1Indy( @NonNull String credentialDefinitionId, @NonNull Map document) { - CredentialFreeOffer.CredentialFreeOfferBuilder r = CredentialFreeOffer.builder(); - try{ - // issue-credential/create in conjunction with oob invitation attachment - // step 1 - create credential offer - V1CredentialFreeOfferRequest create = V1CredentialFreeOfferRequest.builder() - .autoIssue(Boolean.TRUE) - .autoRemove(Boolean.TRUE) - .credDefId(credentialDefinitionId) - .credentialPreview(new CredentialPreview(CredentialAttributes.fromMap(document))) - .build(); - V1CredentialExchange ex = acaPy.issueCredentialCreateOffer(create).orElseThrow(); - // step 2 - create out-of-band invitation with attached credential offer - setAndBuildInvitation(r, ex); - } catch (IOException e) { - throw new AriesNetworkException(NETWORK_ERROR); - } - return r.build(); + return getCredentialFreeOffer(credentialDefinitionId, CredentialAttributes.fromMap(document)); + } + + /** + * Build for v1/indy + * @param credentialDefinitionId credential definition id + * @param document list of credential attributes + * @return {@link CredentialFreeOffer} + */ + public CredentialFreeOffer buildV1Indy( + @NonNull String credentialDefinitionId, + @NonNull List document) { + return getCredentialFreeOffer(credentialDefinitionId, document); } /** @@ -79,28 +76,19 @@ public CredentialFreeOffer buildV1Indy( public CredentialFreeOffer buildV2Indy( @NonNull String credentialDefinitionId, @NonNull Map document) { - CredentialFreeOffer.CredentialFreeOfferBuilder r = CredentialFreeOffer.builder(); - try { - V2CredentialExchangeFree create = V2CredentialExchangeFree.builder() - .autoIssue(Boolean.TRUE) - .autoRemove(Boolean.TRUE) - .filter(V2CredentialExchangeFree.V20CredFilter.builder() - .indy(V20CredFilterIndy.builder() - .credDefId(credentialDefinitionId) - .build()) - .build()) - .credentialPreview(V2CredentialExchangeFree.V2CredentialPreview.builder() - .attributes(CredentialAttributes.fromMap(document)) - .build()) - .build(); - V1CredentialExchange ex = acaPy.issueCredentialV2CreateOffer(create) - .map(V2ToV1IndyCredentialConverter::toV1Offer) - .orElseThrow(); - setAndBuildInvitation(r, ex); - } catch (IOException e) { - throw new AriesNetworkException(NETWORK_ERROR); - } - return r.build(); + return getCredentialFreeOfferV2(credentialDefinitionId, CredentialAttributes.fromMap(document)); + } + + /** + * Build for v2/indy + * @param credentialDefinitionId credential definition id + * @param document list of credential attributes + * @return {@link CredentialFreeOffer} + */ + public CredentialFreeOffer buildV2Indy( + @NonNull String credentialDefinitionId, + @NonNull List document) { + return getCredentialFreeOfferV2(credentialDefinitionId, document); } /** @@ -110,6 +98,7 @@ public CredentialFreeOffer buildV2Indy( */ public CredentialFreeOffer buildDif(@NonNull V2CredentialExchangeFree.LDProofVCDetail vc) { CredentialFreeOffer.CredentialFreeOfferBuilder r = CredentialFreeOffer.builder(); + try { V2CredentialExchangeFree create = V2CredentialExchangeFree.builder() .autoIssue(Boolean.TRUE) @@ -126,6 +115,52 @@ public CredentialFreeOffer buildDif(@NonNull V2CredentialExchangeFree.LDProofVCD return r.build(); } + private CredentialFreeOffer getCredentialFreeOffer(@NonNull String credentialDefinitionId, @NonNull List document) { + CredentialFreeOffer.CredentialFreeOfferBuilder r = CredentialFreeOffer.builder(); + + try{ + // issue-credential/create in conjunction with oob invitation attachment + // step 1 - create credential offer + V1CredentialFreeOfferRequest create = V1CredentialFreeOfferRequest.builder() + .autoIssue(Boolean.TRUE) + .autoRemove(Boolean.TRUE) + .credDefId(credentialDefinitionId) + .credentialPreview(new CredentialPreview(document)) + .build(); + V1CredentialExchange ex = acaPy.issueCredentialCreateOffer(create).orElseThrow(); + // step 2 - create out-of-band invitation with attached credential offer + setAndBuildInvitation(r, ex); + } catch (IOException e) { + throw new AriesNetworkException(NETWORK_ERROR); + } + return r.build(); + } + + private CredentialFreeOffer getCredentialFreeOfferV2(@NonNull String credentialDefinitionId, @NonNull List document) { + CredentialFreeOffer.CredentialFreeOfferBuilder r = CredentialFreeOffer.builder(); + try { + V2CredentialExchangeFree create = V2CredentialExchangeFree.builder() + .autoIssue(Boolean.TRUE) + .autoRemove(Boolean.TRUE) + .filter(V2CredentialExchangeFree.V20CredFilter.builder() + .indy(V20CredFilterIndy.builder() + .credDefId(credentialDefinitionId) + .build()) + .build()) + .credentialPreview(V2CredentialExchangeFree.V2CredentialPreview.builder() + .attributes(document) + .build()) + .build(); + V1CredentialExchange ex = acaPy.issueCredentialV2CreateOffer(create) + .map(V2ToV1IndyCredentialConverter::toV1Offer) + .orElseThrow(); + setAndBuildInvitation(r, ex); + } catch (IOException e) { + throw new AriesNetworkException(NETWORK_ERROR); + } + return r.build(); + } + private void setAndBuildInvitation( @NonNull CredentialFreeOffer.CredentialFreeOfferBuilder r, @NonNull BaseCredExRecord ex) diff --git a/src/main/java/org/hyperledger/aries/api/issue_credential_v1/V1CredentialExchange.java b/src/main/java/org/hyperledger/aries/api/issue_credential_v1/V1CredentialExchange.java index 212fa43..b4fb76c 100644 --- a/src/main/java/org/hyperledger/aries/api/issue_credential_v1/V1CredentialExchange.java +++ b/src/main/java/org/hyperledger/aries/api/issue_credential_v1/V1CredentialExchange.java @@ -104,12 +104,19 @@ public static final class CredentialOfferDict { } public Optional> findAttributesInCredentialOfferDict() { - Optional> result = Optional.empty(); + Map attributesMap = findAttributesInCredentialOfferDictList().orElse(List.of()) + .stream() + .collect(Collectors.toMap(CredentialAttributes::getName, CredentialAttributes::getValue)); + + return Optional.of(attributesMap); + } + + public Optional> findAttributesInCredentialOfferDictList() { + Optional> result = Optional.empty(); if (credentialOfferDict != null && credentialOfferDict.credentialPreview != null) { List attributes = credentialOfferDict.getCredentialPreview().getAttributes(); if (attributes != null) { - return Optional.of(attributes.stream() - .collect(Collectors.toMap(CredentialAttributes::getName, CredentialAttributes::getValue))); + return Optional.of(attributes); } } return result;