Affinidi Messaging SDK

Quickly implement decentralised communication with Affinidi Messaging using the SDK.

Affinidi Messaging SDK provides a suite of methods to simplify the process of sending and receiving messages between Decentralised Identifiers (DIDs) based on DIDComm Messaging protocol.

Send DIDComm Message

Send a DIDComm packed message to the mediator and if it’s a forward type message, routes the message to the the recipient.

pub async fn send_didcomm_message<T>(
        &mut self,
        message: &str,
        return_response: bool,
    ) -> Result<SendMessageResponse<T>, ATMError>
Sample Code
// Pack the message
let (msg, _) = atm
.pack_encrypted(
    &msg,
    mediator_did,
    Some(sender_did),
    Some(sender_did)
)
.await?;

let response = atm
    .send_didcomm_message::<InboundMessageResponse>(&msg, true)
    .await?;

Fetch Messages from Mediator

Retrieves all the available messages from Mediator addressed to your DID.

pub async fn fetch_messages(
        &mut self,
        options: &FetchOptions,
    ) -> Result<GetMessagesResponse, ATMError>
Sample Code
// Get the messages from ATM
let msgs = atm
    .fetch_messages(&FetchOptions {
        limit: 10,
        start_id: None,
        delete_policy: FetchDeletePolicy::DoNotDelete
    })
    .await?;

DIDComm Trust-Ping

Sends a trust ping to a target to test connectivity and routing.

pub async fn send_ping<'c>(
        &self,
        atm: &'c mut ATM<'_>,
        to_did: &str,
        signed: bool,
        expect_response: bool,
    ) -> Result<TrustPingSent, ATMError>
Sample Code
send_ping("did:example:target#123", true, true).await?;

List Messages

Retrieves the list of messages for the specified DID.

pub async fn list_messages(
        &mut self,
        did: &str,
        folder: Folder,
    ) -> Result<MessageList, ATMError>
Sample Code
list_messages("did:example:target#123", Folder::Inbox).await?;

Delete Messages

Deletes on or more messages from ATM.

pub async fn delete_messages(
        &mut self,
        messages: &DeleteMessageRequest,
    ) -> Result<DeleteMessageResponse, ATMError>
Sample Code
delete_messages(&vec!["message_hash1", "message_hash2", ...]).await?;

Get DIDComm Message

Retrieves one or more messages from ATM

pub async fn get_messages(
        &mut self,
        messages: &GetMessagesRequest,
    ) -> Result<GetMessagesResponse, ATMError>
Sample Code
get_messages(&vec!["message_id".into()], false).await?;

Pack a DIDComm message

Constructs the encrypted message to be sent to ATM.

let message = Message::build()...;
let (message, meta_data) = 
    atm.pack_encrypted(
        &message, 
        Some(from_did), 
        Some(from_did)).await?;
Sample Code
let (msg, _) = atm
    .pack_encrypted(
        &msg,
        to_did,
        Some(my_did),
        Some(my_did)
    )
    .await
    .map_err(|e| ATMError::MsgSendError(format!("Error packing message: {}", e)))?;

let response = atm
    .send_didcomm_message::<InboundMessageResponse>(&msg, true)
        .await?;

Unpack a DIDComm message

Decrypts a retried message from ATM.

pub async fn unpack(
    &mut self, 
    message: &str) 
        -> Result<(Message, UnpackMetadata), ATMError>
Sample Code
let didcomm_message = atm.get_messages(&vec!["msg_id".to_string()], true).await?;

let (message, meta_data) = atm.unpack(&didcomm_message).await?;

Learn more about Affinidi Messaging SDK methods in this repository.