Generate Credential


The generate_message_with_addresses_and_fields function. This function takes a Credential payload and returns a Value<Testnet3> wrapped in Rust's Result type.

Function Definition

pub(crate) fn generate_message_with_addresses_and_fields(payload: Credential) -> Result<Value<Testnet3>, anyhow::Error>;


  • payload: An instance of a Credential struct containing various fields like issuer, subject, date of birth (dob), nationality, and expiry.


A Result containing:

  • A Value<Testnet3> object, or

  • An error of type anyhow::Error if the operation fails.

Insertion to Map

The function employs an IndexMap with an initial capacity of 3 to store key-value pairs. The function insert_to_map is called multiple times to insert:

  • issuer and subject as addresses.

  • dob, nationality, and expiry as fields.

These are all inserted as Plaintext types, and they are derived from the Credential payload.


Generate Credential
/// Creates a credential combining blockchain addresses and other fields 
/// based on the given credential payload.
/// # Parameters
/// - `payload`: A `Credential` object containing fields like issuer, 
/// subject, and date of birth (dob).
/// # Returns
/// Returns a `Result` containing a `Value<Testnet3>` if successful, or an /// error message otherwise.
fn create_credential_with_addresses_and_fields(payload: Credential) -> Result<Value<Testnet3>, String> {
    // Initialize an IndexMap with a capacity of 3 to store key-value pairs.
    let mut map = IndexMap::with_capacity(3);
    // Inner function to insert key-value pairs into the IndexMap.
    fn insert_to_map(
        map: &mut IndexMap<Identifier<Testnet3>, Plaintext<Testnet3>>, 
        key: &str, 
        value: Plaintext<Testnet3>
    ) -> Result<(), String> {
        // Convert the key from a string to an Identifier.
        let id = Identifier::from_str(key)
            .map_err(|_| format!("Can't convert {} to Identifier", key))?;
        // Insert the Identifier and associated Plaintext into the map.
        map.insert(id, value);
    // Insert the issuer's address into the map.
    insert_to_map(&mut map, "issuer",             
    // Insert the subject's address into the map.
    insert_to_map(&mut map, "subject", 
    // Insert the date of birth (dob) into the map.
    insert_to_map(&mut map, "dob", 
    // Return the populated IndexMap wrapped in a Plaintext structure as a 
    // successful result.
    Ok(Value::Plaintext(Plaintext::Struct(map, Default::default())))

Last updated