
org.hyperledger.aries.api.present_proof.PresentProofRequestHelper Maven / Gradle / Ivy
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/acapy-java-client
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.aries.api.present_proof;
import lombok.NonNull;
import org.hyperledger.aries.pojo.PojoProcessor;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Helper class that provides some convenience methods to build a {@link PresentProofRequest}
* For more complex usecases use the builder methods directly in the class.
*/
@SuppressWarnings("unused")
public class PresentProofRequestHelper {
/**
* Appends the restrictions for each field in the attribute list
*
* {@code
* "name":"proof_req_2",
* "version":"0.1",
* "requested_attributes": {
* "attr1_referent": {
* "name":"attr1",
* "restrictions": [
* {
* "schema_id": "123:2:mySchema:1.0"
* }, {}]
* },
* "attr2_referent": {
* "name":"attr2",
* "restrictions": [
* {
* "schema_id": "123:2:mySchema:1.0"
* }, {}]
* }
* }
* "requested_predicates": {}
* }
* @param connectionId The connection id
* @param attributes List of schema attributes
* @param restrictions list of {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForEachAttribute(
String connectionId,
@NonNull List attributes,
@NonNull List restrictions) {
Map attr = new LinkedHashMap<>();
attributes.forEach(name -> attr.put(name + "_referent",
PresentProofRequest.ProofRequest.ProofRequestedAttributes
.builder()
.name(name)
.restrictions(restrictions.stream().map(PresentProofRequest.ProofRequest.
ProofRestrictions::toJsonObject).collect(Collectors.toList()))
.build()));
return PresentProofRequest
.builder()
.proofRequest(PresentProofRequest.ProofRequest
.builder()
.requestedAttributes(attr)
.build())
.connectionId(connectionId)
.build();
}
/**
* Appends the restrictions for each attribute within the list
* @param connectionId The connection id
* @param attributes List of schema attributes
* @param restrictions {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForEachAttribute(
String connectionId,
@NonNull List attributes,
@Nullable PresentProofRequest.ProofRequest.ProofRestrictions restrictions) {
return buildForEachAttribute(connectionId, attributes, restrictions != null ? List.of(restrictions) : List.of());
}
/**
* Appends the restrictions for each field of the Pojo class
* @param connectionId The connection id
* @param attributes Pojo class that represents the schema and its attributes
* @param restrictions {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @param Class type
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForEachAttribute(
String connectionId,
@NonNull Class attributes,
@Nullable List restrictions) {
return buildForEachAttribute(connectionId, PojoProcessor.fieldNames(attributes),
restrictions != null ? restrictions : List.of());
}
/**
* Appends the restrictions for each field of the Pojo class
* @param connectionId The connection id
* @param attributes Pojo class that represents the schema and its attributes
* @param restrictions {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @param Class type
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForEachAttribute(
String connectionId,
@NonNull Class attributes,
@Nullable PresentProofRequest.ProofRequest.ProofRestrictions restrictions) {
return buildForEachAttribute(connectionId, PojoProcessor.fieldNames(attributes), restrictions);
}
/**
* Appends the restrictions to the attribute list
* @param connectionId The connection id
* @param attributes List of schema attributes
* @param restrictions list of {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForAllAttributes(
String connectionId,
@NonNull List attributes,
@NonNull List restrictions) {
return buildForAllAttributes(connectionId, attributes, restrictions, null);
}
/**
* Appends the restrictions to the attribute list
*
* {@code
* "name":"proof_req_2",
* "version":"0.1",
* "requested_attributes": {
* "attribute_group_0": {
* "names":["attr1", "attr2"],
* "restrictions": [{
* "schema_id": "123:2:mySchema:1.0",
* "issuer_did": "issuer1"
* },
* {
* "schema_id": "123:2:otherSchema:1.0",
* "issuer_did": "issuer2"
* }]
* }]
* }
* "requested_predicates": {}
* }
* @param connectionId The connection id
* @param attributes List of schema attributes
* @param restrictions list of {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @param nonRevoked {@link PresentProofRequest.ProofRequest.ProofNonRevoked}
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForAllAttributes(
String connectionId,
@NonNull List attributes,
@NonNull List restrictions,
PresentProofRequest.ProofRequest.ProofNonRevoked nonRevoked) {
PresentProofRequest.ProofRequest.ProofRequestedAttributes attr = buildAttributeForAll(
attributes, restrictions, nonRevoked);
return buildProofRequest(connectionId, Map.of("attribute_group_0", attr));
}
/**
* Appends the restrictions to the attribute list
* @param connectionId The connection id
* @param attributes pojo class that will be converted into a list of schema attributes
* @param restrictions list of {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForAllAttributes(
String connectionId,
@NonNull Class attributes,
@NonNull List restrictions) {
return buildForAllAttributes(connectionId, PojoProcessor.fieldNames(attributes), restrictions);
}
/**
* Appends the restrictions to the attribute list
* @param connectionId The connection id
* @param attributes List of schema attributes
* @param restrictions list of {@link PresentProofRequest.ProofRequest.ProofRestrictions}
* @param nonRevoked {@link PresentProofRequest.ProofRequest.ProofNonRevoked}
* @return {@link PresentProofRequest}
*/
public static PresentProofRequest buildForAllAttributes(
String connectionId,
@NonNull Class attributes,
@NonNull List restrictions,
PresentProofRequest.ProofRequest.ProofNonRevoked nonRevoked) {
return buildForAllAttributes(connectionId, PojoProcessor.fieldNames(attributes), restrictions, nonRevoked);
}
public static PresentProofRequest.ProofRequest.ProofRequestedAttributes buildAttributeForAll(
@NotNull List attributes,
@NotNull List restrictions,
PresentProofRequest.ProofRequest.ProofNonRevoked nonRevoked) {
return PresentProofRequest.ProofRequest.ProofRequestedAttributes
.builder()
.names(attributes)
.nonRevoked(nonRevoked)
.restrictions(restrictions.size() > 0
? restrictions.stream().map(PresentProofRequest.ProofRequest.
ProofRestrictions::toJsonObject).collect(Collectors.toList())
: List.of(PresentProofRequest.ProofRequest.ProofRestrictions
.builder().build().toJsonObject()))
.build();
}
public static PresentProofRequest buildProofRequest(
String connectionId,
@NonNull Map attrs) {
return PresentProofRequest
.builder()
.proofRequest(PresentProofRequest.ProofRequest
.builder()
.requestedAttributes(attrs)
.build())
.connectionId(connectionId)
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy