Repository: hyperledger/aries-cloudagent-python

Issue:
New AATH Connection Issues between ACA-Py and AF-Go and Findy Agent
New errors were introduced in the AATH test run from last night for ACA-Py and AFGo and ACA-Py and Findy Agent, likely caused by Aries Cloud Agent Python PR #2409 -- refactoring of the DIDComm DID handling.  Newly failing tests were found by going from https://aries-interop.info following the links into Allure and seeing the tests that passed last run, failed on this one.

@dbluhm -- I hope this gives you all you need.

ACA-Py - AFGo Logs from the run are below, and I think they have what you need, but if not, the logs were produced using these steps:

- Clone the https://github.com/hyperledger/aries-agent-test-harness
- Build: `./manage build -a acapy-main -a afgo-interop`
- Run the services: `./manage service start von-network; ./manage service start indy-tails; ./manage service start uniresolver; ./manage service start orb`
- Run Failing Test: `./manage run -d acapy-main -b afgo-interop -t AIP20 -t RFC0023 -t T001-RFC0023 -t AcceptanceTest`
- Stop the services: `./manage service stop von-network; ./manage service stop indy-tails; ./manage service stop uniresolver; ./manage service stop orb`
- Logs are in .logs for ACA-Py (Acme) and AF-Go (Bob)

[acapy-afgo-logs.zip](https://github.com/hyperledger/aries-cloudagent-python/files/12445550/acapy-afgo-logs.zip)

ACA-Py - Findy Agent Logs from the run are below, and I think they have what you need, but if not, the logs were produced using these steps:

- Build: `./manage build -a acapy-main -a findy`
- Run the services: `./manage service start von-network; ./manage service start indy-tails; ./manage service start uniresolver; ./manage service start orb`
- Run Failing Test: `./manage run -d acapy-main -b findy -t AIP10 -t RFC0160 -t T001-RFC0160 -t MobileTest -t AcceptanceTest`
- Stop the services: `./manage service stop von-network; ./manage service stop indy-tails; ./manage service stop uniresolver; ./manage service stop orb`
- Logs are in .logs for ACA-Py (Acme) and Findy (Bob)

[acapy-findy-logs.zip](https://github.com/hyperledger/aries-cloudagent-python/files/12445574/acapy-findy-logs.zip)




Discussion:
# Extracted details

## ACA-Py <-> AFGo

```mermaid
sequenceDiagram
participant acapy
participant afgo

afgo -->> acapy: out-of-band/1.0/invitation
acapy ->> afgo: didexchange/1.0/request
afgo -x acapy: didexchange/1.0/response
```

ACA-Py Received Response:

```json
{
  "@type": "https://didcomm.org/didexchange/1.0/response",
  "@id": "5e0b3da8-1901-46a3-bd5a-c3b577a15477",
  "~thread": {
    "thid": "d4bb53fa-573c-433d-aaec-75c146c9744f",
    "pthid": "9fffcde5-8edf-4f30-9b27-96fe519780bf"
  },
  "did": "1H6d1WS29Bcfr7Bb9tZxA",
  "did_doc~attach": {
    "mime-type": "application/json",
    "lastmod_time": "0001-01-01T00:00:00Z",
    "data": {
      "base64": "eyJAY29udGV4dCI6WyJodHRwczovL3czaWQub3JnL2RpZC92MSJdLCJpZCI6ImRpZDpzb3Y6MUg2ZDFXUzI5QmNmcjdCYjl0WnhBIiwidmVyaWZpY2F0aW9uTWV0aG9kIjpbeyJjb250cm9sbGVyIjoiZGlkOnNvdjoxSDZkMVdTMjlCY2ZyN0JiOXRaeEEiLCJpZCI6IiNrZXktMSIsInB1YmxpY0tleUJhc2U1OCI6ImdNeGRWTXlGOFJha2FwdHhGc0R6SEF4ZlE1aUVLWmtDRVFjU3dzRGJTV2YiLCJ0eXBlIjoiRWQyNTUxOVZlcmlmaWNhdGlvbktleTIwMTgifSx7ImNvbnRyb2xsZXIiOiJkaWQ6c292OjFINmQxV1MyOUJjZnI3QmI5dFp4QSIsImlkIjoiI2tleS0yIiwicHVibGljS2V5QmFzZTU4IjoicWdYVTFiSlY4b2JIREtLSzEyZGZRVDlmaXR2Qkt2YmUxQlpVNkd3Q1pucCIsInR5cGUiOiJYMjU1MTlLZXlBZ3JlZW1lbnRLZXkyMDE5In1dLCJzZXJ2aWNlIjpbeyJpZCI6IjBmNTU1YTgwLWM5NTAtNGIyYy1iOGVjLTYzMmM3MmZmZDc4MCIsInByaW9yaXR5IjowLCJyZWNpcGllbnRLZXlzIjpbImdNeGRWTXlGOFJha2FwdHhGc0R6SEF4ZlE1aUVLWmtDRVFjU3dzRGJTV2YiXSwicm91dGluZ0tleXMiOltdLCJzZXJ2aWNlRW5kcG9pbnQiOiJodHRwOi8vMTcyLjE3LjAuMTo5MDMxIiwidHlwZSI6IkluZHlBZ2VudCJ9XSwiYXV0aGVudGljYXRpb24iOlt7InB1YmxpY0tleSI6IiNrZXktMSJ9XSwiYXNzZXJ0aW9uTWV0aG9kIjpbeyJwdWJsaWNLZXkiOiIja2V5LTEifV0sImtleUFncmVlbWVudCI6W3sicHVibGljS2V5IjoiI2tleS0yIn1dLCJjcmVhdGVkIjoiMjAyMy0wOC0yNlQxNDoxNzo0Mi4wMDU1NjM2NTlaIiwidXBkYXRlZCI6IjIwMjMtMDgtMjZUMTQ6MTc6NDIuMDA1NTYzNjU5WiJ9",
      "jws": {
        "header": {
          "kid": "did:key:z6MktR4HLoWtKmxpjB8FMJigVoxf7ScqSA1nnTQHbr6urL82"
        },
        "signature": "gsczKP6XXuHBmQBlUJVPmQCMn4ejSGEg0kWuC1ZAjh7cQXmnvhQrZIDWCRaqW2e80HEmiGReVnvSgoGVXMOPDg",
        "protected": "eyJqd2siOnsia3R5IjoiT0tQIiwia2lkIjoiZGlkOmtleTp6Nk1rdFI0SExvV3RLbXhwakI4Rk1KaWdWb3hmN1NjcVNBMW5uVFFIYnI2dXJMODIiLCJjcnYiOiJFZDI1NTE5IiwieCI6InozTW5yTmppeTZJT1pZTE9zTEN6QWpCRll1QXhHZS1LdTBvTG55YWlUWXMifSwiYWxnIjoiRWREU0EifQ"
      }
    }
  }
}
```

Extracted DID Document:
```json
{
  "@context": [
    "https://w3id.org/did/v1"
  ],
  "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
  "verificationMethod": [
    {
      "controller": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
      "id": "#key-1",
      "publicKeyBase58": "gMxdVMyF8RakaptxFsDzHAxfQ5iEKZkCEQcSwsDbSWf",
      "type": "Ed25519VerificationKey2018"
    },
    {
      "controller": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
      "id": "#key-2",
      "publicKeyBase58": "qgXU1bJV8obHDKKK12dfQT9fitvBKvbe1BZU6GwCZnp",
      "type": "X25519KeyAgreementKey2019"
    }
  ],
  "service": [
    {
      "id": "0f555a80-c950-4b2c-b8ec-632c72ffd780",
      "priority": 0,
      "recipientKeys": [
        "gMxdVMyF8RakaptxFsDzHAxfQ5iEKZkCEQcSwsDbSWf"
      ],
      "routingKeys": [],
      "serviceEndpoint": "http://172.17.0.1:9031",
      "type": "IndyAgent"
    }
  ],
  "authentication": [
    {
      "publicKey": "#key-1"
    }
  ],
  "assertionMethod": [
    {
      "publicKey": "#key-1"
    }
  ],
  "keyAgreement": [
    {
      "publicKey": "#key-2"
    }
  ],
  "created": "2023-08-26T14:17:42.005563659Z",
  "updated": "2023-08-26T14:17:42.005563659Z"
}
```

Stored did document:
```json
{
  "@context": "https://w3id.org/did/v1",
  "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
  "publicKey": [
    {
      "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA#cSwsDbSW",
      "type": "Ed25519VerificationKey2018",
      "controller": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
      "publicKeyBase58": "gMxdVMyF8RakaptxFsDzHAxfQ5iEKZkCEQcSwsDbSWf"
    }
  ],
  "authentication": [],
  "service": [
    {
      "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA;0f555a80-c950-4b2c-b8ec-632c72ffd780",
      "type": "IndyAgent",
      "priority": 0,
      "recipientKeys": [
        "gMxdVMyF8RakaptxFsDzHAxfQ5iEKZkCEQcSwsDbSWf"
      ],
      "serviceEndpoint": "http://172.17.0.1:9031"
    }
  ]
}
```

Relevant error logs:
```python
2023-08-26 14:17:42,082 aries_cloudagent.connections.base_manager ERROR Failed to resolve service details while determining connection targets; skipping service
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/aries_cloudagent/resolver/did_resolver.py", line 142, in dereference
    return document.dereference(parsed)
  File "/usr/local/lib/python3.9/site-packages/pydid/doc/doc.py", line 145, in dereference
    raise IDNotFoundError("ID {} not found in document".format(reference))
pydid.doc.doc.IDNotFoundError: ID did:sov:1H6d1WS29Bcfr7Bb9tZxA#1 not found in document

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/aries_cloudagent/connections/base_manager.py", line 368, in resolve_connection_targets
    recips, routing = await self.verification_methods_for_service(
  File "/usr/local/lib/python3.9/site-packages/aries_cloudagent/connections/base_manager.py", line 296, in verification_methods_for_service
    recipient_keys: List[VerificationMethod] = [
  File "/usr/local/lib/python3.9/site-packages/aries_cloudagent/connections/base_manager.py", line 297, in <listcomp>
    await resolver.dereference_verification_method(
  File "/usr/local/lib/python3.9/site-packages/aries_cloudagent/resolver/did_resolver.py", line 156, in dereference_verification_method
    dereferenced = await self.dereference(profile, did_url, document=document)
  File "/usr/local/lib/python3.9/site-packages/aries_cloudagent/resolver/did_resolver.py", line 144, in dereference
    raise ResolverError(
aries_cloudagent.resolver.base.ResolverError: Failed to dereference DID URL: ID did:sov:1H6d1WS29Bcfr7Bb9tZxA#1 not found in document
```

Resolving patch:
diff --git a/aries_cloudagent/resolver/default/legacy_peer.py b/aries_cloudagent/resolver/default/legacy_peer.py
--- a/aries_cloudagent/resolver/default/legacy_peer.py
+++ b/aries_cloudagent/resolver/default/legacy_peer.py
@@ -120,17 +120,34 @@ def didcomm_services_use_updated_conventions(value: dict) -> dict:
                     service["type"] = "did-communication"
                     if ";" in service["id"]:
                         service["id"] = value["id"] + "#didcomm"
+                    if "#" not in service["id"]:
+                        service["id"] += "#didcomm"
+                    if "priority" in service and service["priority"] is None:
+                        service.pop("priority")
         return value
 
     @staticmethod
+    def recip_base58_to_ref(vms: List[dict], recip: str) -> str:
+        """Convert base58 public key to ref."""
+        for vm in vms:
+            if "publicKeyBase58" in vm and vm["publicKeyBase58"] == recip:
+                return vm["id"]
+        return recip
+
+    @classmethod
     def didcomm_services_recip_keys_are_refs_routing_keys_are_did_key(
+        cls,
         value: dict,
     ) -> dict:
         """Update DIDComm service recips to use refs and routingKeys to use did:key."""
+        vms = value.get("verificationMethod", [])
         if "service" in value:
             for service in value["service"]:
                 if "type" in service and service["type"] == "did-communication":
-                    service["recipientKeys"] = [f"{value['id']}#1"]
+                    service["recipientKeys"] = [
+                        cls.recip_base58_to_ref(vms, recip)
+                        for recip in service.get("recipientKeys", [])
+                    ]
                 if "routingKeys" in service:
                     service["routingKeys"] = [
                         DIDKey.from_public_key_b58(key, ED25519).key_id


Test patch:
diff --git a/aries_cloudagent/resolver/default/tests/test_legacy_peer.py b/aries_cloudagent/resolver/default/tests/test_legacy_peer.py
--- a/aries_cloudagent/resolver/default/tests/test_legacy_peer.py
+++ b/aries_cloudagent/resolver/default/tests/test_legacy_peer.py
@@ -1,6 +1,7 @@
 """Test LegacyPeerDIDResolver."""
 
 from asynctest import mock as async_mock
+import pydid
 import pytest
 
 from .. import legacy_peer as test_module
@@ -122,182 +123,298 @@ async def test_resolve_x_not_found(
             result = await resolver.resolve(profile, TEST_DID0)
             assert result == doc
 
-    def test_corrections_examples(self):
-        input_doc = {
-            "@context": "https://w3id.org/did/v1",
-            "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
-            "publicKey": [
+    @pytest.mark.parametrize(
+        ("input_doc", "expected"),
+        [
+            (  # Examples
                 {
-                    "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1",
-                    "type": "Ed25519VerificationKey2018",
-                    "controller": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
-                    "publicKeyBase58": "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG",
-                }
-            ],
-            "authentication": [
-                {
-                    "type": "Ed25519SignatureAuthentication2018",
-                    "publicKey": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1",
-                }
-            ],
-            "service": [
-                {
-                    "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ;indy",
-                    "type": "IndyAgent",
-                    "priority": 0,
-                    "recipientKeys": ["AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG"],
-                    "routingKeys": ["9NnKFUZoYcCqYC2PcaXH3cnaGsoRfyGgyEHbvbLJYh8j"],
-                    "serviceEndpoint": "http://bob:3000",
-                }
-            ],
-        }
-        expected = {
-            "@context": "https://w3id.org/did/v1",
-            "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
-            "verificationMethod": [
-                {
-                    "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1",
-                    "type": "Ed25519VerificationKey2018",
-                    "controller": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
-                    "publicKeyBase58": "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG",
-                }
-            ],
-            "authentication": ["did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1"],
-            "service": [
-                {
-                    "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#didcomm",
-                    "type": "did-communication",
-                    "priority": 0,
-                    "recipientKeys": ["did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1"],
-                    "routingKeys": [
-                        "did:key:z6Mknq3MqipEt9hJegs6J9V7tiLa6T5H5rX3fFCXksJKTuv7#z6Mknq3MqipEt9hJegs6J9V7tiLa6T5H5rX3fFCXksJKTuv7"
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
+                    "publicKey": [
+                        {
+                            "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
+                            "publicKeyBase58": "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG",
+                        }
                     ],
-                    "serviceEndpoint": "http://bob:3000",
-                }
-            ],
-        }
-        actual = test_module.LegacyDocCorrections.apply(input_doc)
-        assert actual == expected
-        assert expected == test_module.LegacyDocCorrections.apply(expected)
-
-    def test_corrections_bifold(self):
-        input_doc = {
-            "@context": "https://w3id.org/did/v1",
-            "publicKey": [
-                {
-                    "id": "PkWfCgY4SSAYeSoaWx3RFP#1",
-                    "controller": "PkWfCgY4SSAYeSoaWx3RFP",
-                    "type": "Ed25519VerificationKey2018",
-                    "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
-                }
-            ],
-            "service": [
-                {
-                    "id": "PkWfCgY4SSAYeSoaWx3RFP#IndyAgentService",
-                    "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
-                    "type": "IndyAgent",
-                    "priority": 0,
-                    "recipientKeys": ["DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K"],
-                    "routingKeys": ["cK7fwfjpakMuv8QKVv2y6qouZddVw4TxZNQPUs2fFTd"],
-                }
-            ],
-            "authentication": [
+                    "authentication": [
+                        {
+                            "type": "Ed25519SignatureAuthentication2018",
+                            "publicKey": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1",
+                        }
+                    ],
+                    "service": [
+                        {
+                            "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ;indy",
+                            "type": "IndyAgent",
+                            "priority": 0,
+                            "recipientKeys": [
+                                "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG"
+                            ],
+                            "routingKeys": [
+                                "9NnKFUZoYcCqYC2PcaXH3cnaGsoRfyGgyEHbvbLJYh8j"
+                            ],
+                            "serviceEndpoint": "http://bob:3000",
+                        }
+                    ],
+                },
                 {
-                    "publicKey": "PkWfCgY4SSAYeSoaWx3RFP#1",
-                    "type": "Ed25519SignatureAuthentication2018",
-                }
-            ],
-            "id": "PkWfCgY4SSAYeSoaWx3RFP",
-        }
-        expected = {
-            "@context": "https://w3id.org/did/v1",
-            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
-            "verificationMethod": [
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
+                    "verificationMethod": [
+                        {
+                            "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ",
+                            "publicKeyBase58": "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG",
+                        }
+                    ],
+                    "authentication": ["did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1"],
+                    "service": [
+                        {
+                            "id": "did:sov:JNKL9kJxQi5pNCfA8QBXdJ#didcomm",
+                            "type": "did-communication",
+                            "priority": 0,
+                            "recipientKeys": ["did:sov:JNKL9kJxQi5pNCfA8QBXdJ#1"],
+                            "routingKeys": [
+                                "did:key:z6Mknq3MqipEt9hJegs6J9V7tiLa6T5H5rX3fFCXksJKTuv7#z6Mknq3MqipEt9hJegs6J9V7tiLa6T5H5rX3fFCXksJKTuv7"
+                            ],
+                            "serviceEndpoint": "http://bob:3000",
+                        }
+                    ],
+                },
+            ),
+            (  # Stored findy doc
                 {
-                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
-                    "type": "Ed25519VerificationKey2018",
-                    "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
-                    "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
-                }
-            ],
-            "authentication": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
-            "service": [
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:5qXMeLdyWEQhieFUBNw5ux",
+                    "publicKey": [
+                        {
+                            "id": "did:sov:5qXMeLdyWEQhieFUBNw5ux#1",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:5qXMeLdyWEQhieFUBNw5ux",
+                            "publicKeyBase58": "3dtu2WWtd5ELwRTJEPzmEJUYEp8Qq36N2QA24g9tFXK9",
+                        }
+                    ],
+                    "authentication": [
+                        {
+                            "type": "Ed25519SignatureAuthentication2018",
+                            "publicKey": "did:sov:5qXMeLdyWEQhieFUBNw5ux#1",
+                        }
+                    ],
+                    "service": [
+                        {
+                            "id": "did:sov:5qXMeLdyWEQhieFUBNw5ux",
+                            "type": "IndyAgent",
+                            "priority": None,
+                            "recipientKeys": [
+                                "3dtu2WWtd5ELwRTJEPzmEJUYEp8Qq36N2QA24g9tFXK9"
+                            ],
+                            "serviceEndpoint": "http://172.17.0.1:9031/a2a/5b6dyY6PndLaCnWxZbeEYW/5b6dyY6PndLaCnWxZbeEYW/2f6aae0c-6b04-40ff-a25e-faecaea39f83",
+                        }
+                    ],
+                },
                 {
-                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#IndyAgentService",
-                    "type": "did-communication",
-                    "priority": 0,
-                    "recipientKeys": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
-                    "routingKeys": [
-                        "did:key:z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1#z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1"
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:5qXMeLdyWEQhieFUBNw5ux",
+                    "verificationMethod": [
+                        {
+                            "id": "did:sov:5qXMeLdyWEQhieFUBNw5ux#1",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:5qXMeLdyWEQhieFUBNw5ux",
+                            "publicKeyBase58": "3dtu2WWtd5ELwRTJEPzmEJUYEp8Qq36N2QA24g9tFXK9",
+                        }
                     ],
-                    "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
-                }
-            ],
-        }
-        actual = test_module.LegacyDocCorrections.apply(input_doc)
-        assert actual == expected
-        assert expected == test_module.LegacyDocCorrections.apply(expected)
-
-    def test_corrections_stored(self):
-        input_doc = {
-            "@context": "https://w3id.org/did/v1",
-            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
-            "publicKey": [
+                    "authentication": ["did:sov:5qXMeLdyWEQhieFUBNw5ux#1"],
+                    "service": [
+                        {
+                            "id": "did:sov:5qXMeLdyWEQhieFUBNw5ux#didcomm",
+                            "type": "did-communication",
+                            "recipientKeys": ["did:sov:5qXMeLdyWEQhieFUBNw5ux#1"],
+                            "serviceEndpoint": "http://172.17.0.1:9031/a2a/5b6dyY6PndLaCnWxZbeEYW/5b6dyY6PndLaCnWxZbeEYW/2f6aae0c-6b04-40ff-a25e-faecaea39f83",
+                        }
+                    ],
+                },
+            ),
+            (  # Stored afgo doc
                 {
-                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
-                    "type": "Ed25519VerificationKey2018",
-                    "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
-                    "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
+                    "publicKey": [
+                        {
+                            "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA#cSwsDbSW",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
+                            "publicKeyBase58": "gMxdVMyF8RakaptxFsDzHAxfQ5iEKZkCEQcSwsDbSWf",
+                        }
+                    ],
+                    "authentication": [],
+                    "service": [
+                        {
+                            "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA;0f555a80-c950-4b2c-b8ec-632c72ffd780",
+                            "type": "IndyAgent",
+                            "priority": 0,
+                            "recipientKeys": [
+                                "gMxdVMyF8RakaptxFsDzHAxfQ5iEKZkCEQcSwsDbSWf"
+                            ],
+                            "serviceEndpoint": "http://172.17.0.1:9031",
+                        }
+                    ],
                 },
                 {
-                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#QPUs2fFT",
-                    "type": "Ed25519VerificationKey2018",
-                    "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
-                    "publicKeyBase58": "cK7fwfjpakMuv8QKVv2y6qouZddVw4TxZNQPUs2fFTd",
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
+                    "verificationMethod": [
+                        {
+                            "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA#cSwsDbSW",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:1H6d1WS29Bcfr7Bb9tZxA",
+                            "publicKeyBase58": "gMxdVMyF8RakaptxFsDzHAxfQ5iEKZkCEQcSwsDbSWf",
+                        }
+                    ],
+                    "authentication": [],
+                    "service": [
+                        {
+                            "id": "did:sov:1H6d1WS29Bcfr7Bb9tZxA#didcomm",
+                            "type": "did-communication",
+                            "priority": 0,
+                            "recipientKeys": ["did:sov:1H6d1WS29Bcfr7Bb9tZxA#cSwsDbSW"],
+                            "serviceEndpoint": "http://172.17.0.1:9031",
+                        }
+                    ],
                 },
-            ],
-            "authentication": [
+            ),
+            (  # Stored doc with routing keys
                 {
-                    "type": "Ed25519SignatureAuthentication2018",
-                    "publicKey": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
-                }
-            ],
-            "service": [
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
+                    "publicKey": [
+                        {
+                            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
+                            "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
+                        },
+                        {
+                            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#QPUs2fFT",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
+                            "publicKeyBase58": "cK7fwfjpakMuv8QKVv2y6qouZddVw4TxZNQPUs2fFTd",
+                        },
+                    ],
+                    "authentication": [
+                        {
+                            "type": "Ed25519SignatureAuthentication2018",
+                            "publicKey": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
+                        }
+                    ],
+                    "service": [
+                        {
+                            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP;PkWfCgY4SSAYeSoaWx3RFP#IndyAgentService",
+                            "type": "IndyAgent",
+                            "priority": 0,
+                            "recipientKeys": [
+                                "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K"
+                            ],
+                            "routingKeys": [
+                                "cK7fwfjpakMuv8QKVv2y6qouZddVw4TxZNQPUs2fFTd"
+                            ],
+                            "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
+                        }
+                    ],
+                },
                 {
-                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP;PkWfCgY4SSAYeSoaWx3RFP#IndyAgentService",
-                    "type": "IndyAgent",
-                    "priority": 0,
-                    "recipientKeys": ["DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K"],
-                    "routingKeys": ["cK7fwfjpakMuv8QKVv2y6qouZddVw4TxZNQPUs2fFTd"],
-                    "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
-                }
-            ],
-        }
-        expected = {
-            "@context": "https://w3id.org/did/v1",
-            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
-            "verificationMethod": [
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
+                    "verificationMethod": [
+                        {
+                            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
+                            "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
+                        }
+                    ],
+                    "authentication": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
+                    "service": [
+                        {
+                            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#didcomm",
+                            "type": "did-communication",
+                            "priority": 0,
+                            "recipientKeys": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
+                            "routingKeys": [
+                                "did:key:z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1#z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1"
+                            ],
+                            "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
+                        }
+                    ],
+                },
+            ),
+            (  # Stored bifold doc
                 {
-                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
-                    "type": "Ed25519VerificationKey2018",
-                    "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
-                    "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
-                }
-            ],
-            "authentication": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
-            "service": [
+                    "@context": "https://w3id.org/did/v1",
+                    "publicKey": [
+                        {
+                            "id": "PkWfCgY4SSAYeSoaWx3RFP#1",
+                            "controller": "PkWfCgY4SSAYeSoaWx3RFP",
+                            "type": "Ed25519VerificationKey2018",
+                            "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
+                        }
+                    ],
+                    "service": [
+                        {
+                            "id": "PkWfCgY4SSAYeSoaWx3RFP#IndyAgentService",
+                            "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
+                            "type": "IndyAgent",
+                            "priority": 0,
+                            "recipientKeys": [
+                                "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K"
+                            ],
+                            "routingKeys": [
+                                "cK7fwfjpakMuv8QKVv2y6qouZddVw4TxZNQPUs2fFTd"
+                            ],
+                        }
+                    ],
+                    "authentication": [
+                        {
+                            "publicKey": "PkWfCgY4SSAYeSoaWx3RFP#1",
+                            "type": "Ed25519SignatureAuthentication2018",
+                        }
+                    ],
+                    "id": "PkWfCgY4SSAYeSoaWx3RFP",
+                },
                 {
-                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#didcomm",
-                    "type": "did-communication",
-                    "priority": 0,
-                    "recipientKeys": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
-                    "routingKeys": [
-                        "did:key:z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1#z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1"
+                    "@context": "https://w3id.org/did/v1",
+                    "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
+                    "verificationMethod": [
+                        {
+                            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#1",
+                            "type": "Ed25519VerificationKey2018",
+                            "controller": "did:sov:PkWfCgY4SSAYeSoaWx3RFP",
+                            "publicKeyBase58": "DQBMbzLmAK5iyiuPnqNvfkx3CQ2iTJ2sKz98utvdET4K",
+                        }
                     ],
-                    "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
-                }
-            ],
-        }
+                    "authentication": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
+                    "service": [
+                        {
+                            "id": "did:sov:PkWfCgY4SSAYeSoaWx3RFP#IndyAgentService",
+                            "type": "did-communication",
+                            "priority": 0,
+                            "recipientKeys": ["did:sov:PkWfCgY4SSAYeSoaWx3RFP#1"],
+                            "routingKeys": [
+                                "did:key:z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1#z6Mkf4aAGBvBA8Eq2Qy714sspCPoj8uUupJpeaHLDkq3aUF1"
+                            ],
+                            "serviceEndpoint": "https://aries-mediator-agent.vonx.io",
+                        }
+                    ],
+                },
+            ),
+        ],
+    )
+    def test_corrections(self, input_doc: dict, expected: dict):
         actual = test_module.LegacyDocCorrections.apply(input_doc)
         assert actual == expected
         assert expected == test_module.LegacyDocCorrections.apply(expected)
+        doc = pydid.deserialize_document(actual)
+        assert doc.service
+        assert isinstance(doc.service[0], pydid.DIDCommService)