---
# System prepended metadata

title: 'Forward Example 1: Two mediators as two routing keys'

---

# Forward Example 1: Two mediators as two routing keys

#### Sender
1. Alice encrypts a message to Bob, so we have `encrypted_payload_bob`.
2. Alice resolves Bob's DID DOC and gets the following DIDDocServiceEndpoint:
```
{
    "id": "did:example:bob#didcomm-1",
    "type": "DIDCommMessaging",
    "serviceEndpoint": "http://example.com/path",
    "accept": [
       "didcomm/v2",
       "didcomm/aip2;env=rfc587"
     ],
     "routingKeys": ["did:example:somemediator1#somekey", "did:example:somemediator2#somekey"]
}
```
3. Alice wraps `encrypted_payload_bob` into a Forward message for Mediator 2:
- Build `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` 
- Anoncrypt the Forward message to `did:example:somemediator2#somekey` . Result - `encrypted_forward_mediator2`.

4. Alice wraps `encrypted_forward_mediator2` into a Forward message for Mediator 1:
- Build `Forward(next="did:example:somemediator2", attachments.body.json=encrypted_forward_mediator2`
- Anoncrypt the Forward message to `did:example:somemediator1#somekey`. Result - `encrypted_forward_mediator1`.

5. Alice sends `encrypted_forward_mediator1` to `http://example.com/path`.

#### Mediator 1
1. Mediator1 calls `unpack` to `encrypted_forward_mediator1` and gets `Forward(next="did:example:somemediator2", attachments.body.json=encrypted_forward_mediator2` as a plaintext message.
2. Mediator1 sends `encrypted_forward_mediator2` to Mediator2 via some pre-configured endpoint (out of scope for DID Comm library).

#### Mediator 2
1. Mediator2 calls `unpack` to `encrypted_forward_mediator2` and gets `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` as a plaintext message.
2. Mediator2 sends `encrypted_payload_bob` to Bob via some pre-configured endpoint (out of scope for DID Comm library).

#### Bob
1. Bob calls `unpack` on `encrypted_payload_bob`


# Forward Example 2: Two mediators with alternative endpoints

#### Sender
1. Alice encrypts a message to Bob, so we have `encrypted_payload_bob`.
2. Alice resolves Bob's DID DOC and gets the following DIDDocServiceEndpoint:
```
{
    "id": "did:example:bob#didcomm-1",
    "type": "DIDCommMessaging",
    "serviceEndpoint": "did:example:somemediator1",
     "routingKeys": ["did:example:somemediator2#somekey"]
}
```
3. Alice wraps `encrypted_payload_bob` into a Forward message for Mediator 2:
- Build `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` 
- Anoncrypt the Forward message to `did:example:somemediator2#somekey`. Result - `encrypted_forward_mediator2`.


4. Alice wraps `encrypted_forward_mediator2` into a Forward message for Mediator 1:
- Build `Forward(next="did:example:somemediator2", attachments.body.json=encrypted_forward_mediator2`
- Resolve `did:example:somemediator1` DID DOC and finds `keyAgreement` keys. 
- Anoncrypt the Forward message for every found `keyAgreement` (multiplex encryption). Result - `encrypted_forward_mediator1`.

5. Alice resolves `did:example:somemediator1` DID DOC and finds the following DIDDocServiceEndpoint:
```
{
    "id": "did:example:somemediator1#didcomm-1",
    "type": "DIDCommMessaging",
    "serviceEndpoint": "http://example-mediator1.com/path",
}
```

6. Alice sends `encrypted_forward_mediator1` to `http://example-mediator1.com/path`.

#### Mediator 1
1. Mediator1 calls `unpack` to `encrypted_forward_mediator1` and gets `Forward(next="did:example:somemediator2", attachments.body.json=encrypted_forward_mediator2` as a plaintext message.
2. Mediator1 sends `encrypted_forward_mediator2` to Mediator2 via some pre-configured endpoint (out of scope for DID Comm library).

#### Mediator 2
1. Mediator2 calls `unpack` to `encrypted_forward_mediator2` and gets `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` as a plaintext message.
2. Mediator2 sends `encrypted_payload_bob` to Bob via some pre-configured endpoint (out of scope for DID Comm library).

#### Bob
1. Bob calls `unpack` on `encrypted_payload_bob`


# Forward Example 3: Re-wrapping for a final recipient

#### Sender
1. Alice encrypts a message to Bob, so we have `encrypted_payload_bob`.
2. Alice resolves Bob's DID DOC and gets the following DIDDocServiceEndpoint:
```
{
    "id": "did:example:bob#didcomm-1",
    "type": "DIDCommMessaging",
    "serviceEndpoint": "http://example.com/path",
    "routingKeys": ["did:example:somemediator#somekey"]
}
```
3. Alice wraps `encrypted_payload_bob` into a Forward message for Mediator:
- Build `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` 
- Anoncrypt the Forward message to `did:example:somemediator#somekey`. Result - `encrypted_forward_mediator`.

4. Alice sends `encrypted_forward_mediator` to `http://example.com/path`.

#### Mediator 
1. Mediator calls `unpack` to `encrypted_forward_mediator` and gets `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` as a plaintext message.
2. Mediator re-wraps `encrypted_payload_bob` into a new Forward message for Bob:
- Build `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` 
- Anoncrypt the Forward message to `did:example:bob`. Result - `encrypted_forward_bob`.
3. Mediator sends `encrypted_forward_bob` to Bob via some pre-configured endpoint (out of scope for DID Comm library).

#### Bob
1. Bob calls `unpack` on `encrypted_forward_bob`. Unpack does the following:
 - unpacks `encrypted_forward_bob` and gets `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)`
 - unpacks `encrypted_payload_bob` 


# Forward Example 4: Mediator unknown by the sender

#### Sender
1. Alice encrypts a message to Bob, so we have `encrypted_payload_bob`.
2. Alice resolves Bob's DID DOC and gets the following DIDDocServiceEndpoint:
```
{
    "id": "did:example:bob#didcomm-1",
    "type": "DIDCommMessaging",
    "serviceEndpoint": "http://example.com/path",
     "routingKeys": ["did:example:somemediator1#somekey"]
}
```
3. Alice wraps `encrypted_payload_bob` into a Forward message for Mediator:
- Build `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` 
- Anoncrypt the Forward message to `did:example:somemediator1#somekey`. Result - `encrypted_forward_mediator1`.

4. Alice sends `encrypted_forward_mediator1` to `http://example.com/path`.

#### Mediator 1
1. Mediator1 calls `unpack` to `encrypted_forward_mediator1` and gets `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` as a plaintext message.


2. Mediator1 knows (out-of-scope) that the following should be used to transfer the message to Bob:
- `"serviceEndpoint": "http://example-mediator2.com/path"`
- ``"routingKeys": ["did:example:somemediator2#somekey"]``

3. Mediator1 wraps `encrypted_payload_bob` into a Forward message for Mediator2:
- Build `Forward(next="did:example:somemediator2", attachments.body.json=encrypted_payload_bob)` 
- Anoncrypt the Forward message to `did:example:somemediator2#somekey`. Result - `encrypted_forward_mediator2`.

4. Mediator1 sends `encrypted_forward_mediator2` to `http://example-mediator2.com/path`.

#### Mediator 2
1. Mediator2 calls `unpack` to `encrypted_forward_mediator2` and gets `Forward(next="did:example:bob", attachments.body.json=encrypted_payload_bob)` as a plaintext message.
2. Mediator2 sends `encrypted_payload_bob` to Bob via some pre-configured endpoint (out of scope for DID Comm library).

#### Bob
1. Bob calls `unpack` on `encrypted_payload_bob`