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 3 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 @@ -24,20 +24,14 @@
import java.util.stream.Collectors;

@Slf4j
@Data @NoArgsConstructor @AllArgsConstructor @Builder
@Data @Builder
schlagtim marked this conversation as resolved.
Show resolved Hide resolved
public class CredentialAttributes {

@SerializedName(value = "mime-type")
private String mimeType;
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
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,8 @@
import org.hyperledger.aries.api.out_of_band.BaseOOBInvitationHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -50,7 +52,7 @@ public CredentialFreeOfferHelper(AriesClient acaPy) {
*/
public CredentialFreeOffer buildV1Indy(
@NonNull String credentialDefinitionId,
@NonNull Map<String, String> document) {
@NonNull List<CredentialAttributes> document) {
schlagtim marked this conversation as resolved.
Show resolved Hide resolved
CredentialFreeOffer.CredentialFreeOfferBuilder r = CredentialFreeOffer.builder();
try{
// issue-credential/create in conjunction with oob invitation attachment
Expand All @@ -59,7 +61,7 @@ public CredentialFreeOffer buildV1Indy(
.autoIssue(Boolean.TRUE)
.autoRemove(Boolean.TRUE)
.credDefId(credentialDefinitionId)
.credentialPreview(new CredentialPreview(CredentialAttributes.fromMap(document)))
.credentialPreview(new CredentialPreview(document))
.build();
V1CredentialExchange ex = acaPy.issueCredentialCreateOffer(create).orElseThrow();
// step 2 - create out-of-band invitation with attached credential offer
Expand All @@ -78,7 +80,7 @@ public CredentialFreeOffer buildV1Indy(
*/
public CredentialFreeOffer buildV2Indy(
@NonNull String credentialDefinitionId,
@NonNull Map<String, String> document) {
@NonNull List<CredentialAttributes> document) {
CredentialFreeOffer.CredentialFreeOfferBuilder r = CredentialFreeOffer.builder();
try {
V2CredentialExchangeFree create = V2CredentialExchangeFree.builder()
Expand All @@ -90,7 +92,7 @@ public CredentialFreeOffer buildV2Indy(
.build())
.build())
.credentialPreview(V2CredentialExchangeFree.V2CredentialPreview.builder()
.attributes(CredentialAttributes.fromMap(document))
.attributes(document)
.build())
.build();
V1CredentialExchange ex = acaPy.issueCredentialV2CreateOffer(create)
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void testSimpleV2ToV1() {
Assertions.assertTrue(credential.isPresent());
Assertions.assertNotNull(credential.get().getAttrs());
Assertions.assertEquals(2, credential.get().getAttrs().size());
Assertions.assertEquals("222", credential.get().getAttrs().get("iban"));
Assertions.assertEquals("1111", credential.get().getAttrs().get("bic"));
Assertions.assertEquals("222", credential.get().getAttrs().stream().filter(attr -> attr.getName().equals("iban")).findFirst().get().getValue());
Assertions.assertEquals("1111", credential.get().getAttrs().stream().filter(attr -> attr.getName().equals("bic")).findFirst().get().getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void testGetPresentProofRecordsCredentials() throws Exception {

Assertions.assertTrue(credentials.isPresent());
Assertions.assertEquals(2, credentials.get().size());
Assertions.assertEquals("bpa", credentials.get().get(0).getCredentialInfo().getAttrs().get("name"));
Assertions.assertEquals("bpa", credentials.get().get(0).getCredentialInfo().getAttrs().stream().filter(attr -> attr.getName().equals("name")).findFirst().get().getValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.aries.IntegrationTestBase;
import org.hyperledger.aries.api.credentials.Credential;
import org.hyperledger.aries.api.credentials.CredentialAttributes;
import org.hyperledger.aries.api.exception.AriesException;
import org.hyperledger.aries.config.GsonConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -27,7 +28,7 @@ public class PresentProofProposalTest extends IntegrationTestBase {
@Test
void testBuildPresentationProposal() {
Credential cred = new Credential();
cred.setAttrs(Map.of("street", "teststreet"));
cred.setAttrs(List.of(new CredentialAttributes("street", "teststreet", null)));
cred.setCredentialDefinitionId("WgWxqztrNooG92RXvxSTWv:3:CL:20:tag");
cred.setReferent("referent");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.aries.IntegrationTestBase;
import org.hyperledger.aries.api.credentials.Credential;
import org.hyperledger.aries.api.credentials.CredentialAttributes;
import org.hyperledger.aries.api.exception.AriesException;
import org.hyperledger.aries.api.present_proof.PresentProofProposalBuilder;
import org.hyperledger.aries.config.GsonConfig;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -26,7 +27,7 @@ public class PresentProofProposalV2Test extends IntegrationTestBase {
@Test
void testBuildPresentationProposalV2() {
Credential cred = new Credential();
cred.setAttrs(Map.of("street", "teststreet"));
cred.setAttrs(List.of(new CredentialAttributes("street", "teststreet", null)));
cred.setCredentialDefinitionId("WgWxqztrNooG92RXvxSTWv:3:CL:20:tag");
cred.setReferent("referent");

Expand Down