Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Credential attributes #66

Merged
merged 8 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> List<CredentialAttributes> from(@NonNull T instance) {
List<CredentialAttributes> result = new ArrayList<>();
Field[] fields = instance.getClass().getDeclaredFields();
Expand All @@ -60,7 +54,8 @@ public static <T> List<CredentialAttributes> from(@NonNull T instance) {
} catch (IllegalAccessException | IllegalArgumentException e) {
log.error("Could not get value of field: {}", fieldName, e);
}
result.add(new CredentialAttributes(fieldName, fieldValue));

schlagtim marked this conversation as resolved.
Show resolved Hide resolved
result.add(CredentialAttributes.builder().name(fieldName).value(fieldValue).build());
}
}
}
Expand All @@ -72,13 +67,13 @@ public static <T> List<CredentialAttributes> from(@NonNull T instance) {
public static List<CredentialAttributes> from(@NonNull Map<String, Object> values) {
List<CredentialAttributes> 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<CredentialAttributes> fromMap(@NonNull Map<String, String> values) {
List<CredentialAttributes> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -51,23 +52,19 @@ public CredentialFreeOfferHelper(AriesClient acaPy) {
public CredentialFreeOffer buildV1Indy(
@NonNull String credentialDefinitionId,
@NonNull Map<String, String> 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<CredentialAttributes> document) {
schlagtim marked this conversation as resolved.
Show resolved Hide resolved
return getCredentialFreeOffer(credentialDefinitionId, document);
}

/**
Expand All @@ -79,28 +76,19 @@ public CredentialFreeOffer buildV1Indy(
public CredentialFreeOffer buildV2Indy(
@NonNull String credentialDefinitionId,
@NonNull Map<String, String> 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<CredentialAttributes> document) {
return getCredentialFreeOfferV2(credentialDefinitionId, document);
}

/**
Expand All @@ -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)
Expand All @@ -126,6 +115,52 @@ public CredentialFreeOffer buildDif(@NonNull V2CredentialExchangeFree.LDProofVCD
return r.build();
}

private CredentialFreeOffer getCredentialFreeOffer(@NonNull String credentialDefinitionId, @NonNull List<CredentialAttributes> 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<CredentialAttributes> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,19 @@ public static final class CredentialOfferDict {
}

public Optional<Map<String, String>> findAttributesInCredentialOfferDict() {
Optional<Map<String, String>> result = Optional.empty();
Map<String, String> attributesMap = findAttributesInCredentialOfferDictList().orElse(List.of())
.stream()
.collect(Collectors.toMap(CredentialAttributes::getName, CredentialAttributes::getValue));

return Optional.of(attributesMap);
}

public Optional<List<CredentialAttributes>> findAttributesInCredentialOfferDictList() {
Optional<List<CredentialAttributes>> result = Optional.empty();
if (credentialOfferDict != null && credentialOfferDict.credentialPreview != null) {
List<CredentialAttributes> 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;
Expand Down