# Affinidi Messaging

> A secure, private and trusted messaging framework based on DIDComm Messaging protocol.

[
        Apache 2.0 License 
    ](https://github.com/affinidi/affinidi-tdk-rs/blob/main/LICENSE)

    [
        Contribution Guidelines 
    ](https://github.com/affinidi/affinidi-tdk-rs/blob/main/CONTRIBUTING.md)

    [
        Repository 
    ](#ZgotmplZ)

Affinidi Messaging is a communication system built on the DIDComm v2.1 protocol. It ensures complete privacy and control of user data and identity during interactions with individuals, businesses, and AI agents.

Affinidi Messaging offers libraries and tools for implementing the DIDComm Messaging protocol, which builds upon the decentralized architecture of the Decentralised Identifier (DID) standard. This framework includes packages designed to enable secure and private messaging, as well as capabilities for discovering and establishing connections with individuals, businesses, or AI agents.

## Core Concepts

- 
Decentralised Identifier (DID) - A globally unique identifier that enables
secure interactions. The DID is the cornerstone of Self-Sovereign Identity (SSI),
a concept that aims to put individuals or entities in control of their digital
identities.

- 
DID Document - A DID is a URI (Uniform Resource Identifier) that resolves
into a DID Document that contains information such as cryptographic public keys,
authentication methods, and service endpoints. It allows others to verify
signatures, authenticate interactions, and validate data cryptographically.

- 
Envelope Encryption - A cryptographic technique that uses multiple layers of
encryption to protect the data. A Data Encryption Key (DEK) encrypts the data,
and then the Key Encryption Key (KEK) encrypts the DEK. This layered approach
enhances security by protecting the data and the key to access it.

- 
Mediator - A service that handles and routes messages sent between
participants (e.g., users, organisations, another mediator, or even AI agents).

- 
DIDComm Message - usually called DIDComm Encrypted Message is a JSON Web
Message (JWM), a lightweight, secure, and standardised format for structured
communication using JSON. It represents headers, message types, routing metadata,
and payloads designed to enable secure and interoperable communication across
different systems.

## Requirements

- 
Rust (1.85.0) 2024 Edition

- 
Redis 8.0

## Overall Crate Structure

Affinidi Messaging consists of different crates, each providing various libraries
and tools that form the solution. Each crate has embedded sub-crates that provide
different capabilities to support and run the main crate.

### Affinidi Messaging

A messaging server built on DIDComm protocol that facilitates confidential and
secure exchanges between senders and recipients.

### Affinidi DID Resolver

A high-performance service that resolves and caches the DID document for a certain
period, for faster resolution of DID documents and processing of messages.

### Affinidi Meeting Place

Affinidi Meeting Place provides a safe and secure method for discovering and
connecting with others using decentralised identifiers (DIDs) and the DIDComm
Messaging protocol.

### Affinidi TDK

The Affinidi Trust Development Kit (TDK) provides common elements for developing
privacy-preserving services using decentralised identity technologies.

## Get Started

To deploy the DIDComm Mediator from the open-sourced version, follow the steps below:

### 1. Prerequisites

Before you begin, install the following applications.

- 
Install Rust (1.95) on your machine if you haven’t installed it yet using [this guide](https://www.rust-lang.org/tools/install).

- 
Install Docker on your machine if you haven’t installed it yet using [this guide](https://docs.docker.com/desktop/). We will need this to run Redis instance for the mediator to store messages temporarily and mediator configurations.

### 2. Clone the Mediator Repository

- Clone the [GitHub repo](https://github.com/affinidi/affinidi-tdk-rs) on your local.

```bash
git clone git@github.com:affinidi/affinidi-tdk-rs.git
```

- Navigate to the crates/affinidi-messaging folder.

```bash
cd affinidi-tdk-rs/crates/affinidi-messaging
```

### 3. Set up the Mediator Instance

- Run the Redis Docker container using the command below from your terminal:

```bash
docker run --name=redis-local --publish=6380:6380 --hostname=redis --restart=on-failure --detach redis:latest
```

The latest supported version of Redis is version 8.0.

- Run setup_environment to configure the mediator with all the required information to run locally.

```bash
cargo run --bin setup_environment
```

Output from setup_environment

Select Local Mediator Configuration and follow the on-screen instructions. Upon successful setup, it will generate the following information:

- Mediator DID and secrets.

- Administration DID and secrets.

- SSL Certificates for local development/testing.

- Optionally, different users with their DIDs for testing.

Important Note

To the question “Use existing Mediator DID?” during setup, answer “no”. This will generate new DID.

- After completing the setup, navigate to the affinidi-messaging-mediator directory and run the following command to start the mediator:

```bash
cd affinidi-messaging-mediator && cargo run
```

- To verify that the mediator is running, open http://localhost:7037/mediator/v1/.well-known/did.json in your web browser. This page should display your local mediator’s DID document.

### 4. Test the Mediator Connection

The repository provides examples that you can run to test whether the setup was successful. To do this, follow the steps below:

- Open another terminal tab and go to the crates/affinidi-messaging folder.

```bash
cd affinidi-tdk-rs/crates/affinidi-messaging
```

- From the terminal, run the following example.

```bash
cargo run --example mediator_ping
```

The above example will execute a code that authenticates to the mediator and send a ping message. If successful, it sends a pong message back.

### Enable Logging

To enable logging to see more information when running the example and to learn more about how it works, set the following environment variables from your terminal.

```bash
export RUST_LOG=none,affinidi_messaging_helpers=debug,affinidi_messaging_sdk=info
```

## What’s Next

  [Explore Meeting Place, an implementation of Affinidi Messaging](/products/affinidi-elements/affinidi-messaging/meeting-place.md)

  [Integrate Affinidi Messaging into your applications](/products/affinidi-elements/affinidi-messaging/integration-guides.md)
