All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperledger.aries.api.credentials.Credential Maven / Gradle / Ivy

There is a newer version: 0.10.0
Show newest version
/*
 * 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.credentials;

import com.google.gson.annotations.SerializedName;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.aries.config.CredDefId;
import org.hyperledger.aries.pojo.PojoProcessor;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import java.util.Map;

@Slf4j
@Data @NoArgsConstructor @AllArgsConstructor @Builder
public class Credential {

    private Map attrs;

    @SerializedName(value = CredDefId.CREDENTIAL_DEFINITION_ID,
            alternate = {CredDefId.CRED_DEF_ID, CredDefId.CREDENTIALDEFINITIONID})
    private String credentialDefinitionId;

    private String credRevId;

    private String referent;

    private String revRegId;

    private String schemaId;

    /**
     * Maps the credential attributes into an instance of the provided class type.
     * @param  The class type
     * @param type The class type
     * @return Instantiated type with all matching properties set
     */
    public  T to(@NonNull Class type) {
        T result = PojoProcessor.getInstance(type);

        List fields = PojoProcessor.fields(type);
        AccessController.doPrivileged((PrivilegedAction) () -> {
            for(Field field: fields) {
                String fieldName = PojoProcessor.fieldName(field);
                String fieldValue = attrs.get(fieldName);
                try {
                    field.setAccessible(true);
                    field.set(result, fieldValue);
                } catch (IllegalAccessException | IllegalArgumentException e) {
                    log.error("Could not set value of field: {} to: {}", fieldName, fieldValue, e);
                }
            }
            return null; // nothing to return
        });
        return result;
    }

    @Data @NoArgsConstructor @AllArgsConstructor
    public static final class CredentialRevokedResult {
        private Boolean revoked;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy