Repository: hyperledger/aries-cloudagent-python

Issue:
Exception when resolving a DID when multi-ledger support is active
Resolving a did from a ledger that is not the main (production and write set to true) ledger raises the exception below, and a recent PyDID change (#1562) didn't resolve the issue. From the exception, the guess is that the value that is handed over to pydid is empty.

My next bet is that it's related to the ledger resolution in indy.py https://github.com/hyperledger/aries-cloudagent-python/blob/3fe6715a2bb6ac30ecddf52ec5ad5e03493354df/aries_cloudagent/resolver/default/indy.py#L48

Exception:

```
2021-12-16 15:28:09,214 indy.libindy.native.indy.services.ledger INFO 	src/services/ledger/mod.rs:137 | build_get_attrib_request() => Ok("{\"reqId\":1639668489214330900,\"identifier\":\"F6dB7dMVHUQSC64qemnBi7\",\"operation\":{\"type\":\"104\",\"dest\":\"NBUXM1kVv4yNVc6ezpaa94\",\"raw\":\"endpoint\"},\"protocolVersion\":2}")
 =================
 2021-12-16 15:28:09,240 aries_cloudagent.core.dispatcher ERROR Handler error: resolve_did
 Traceback (most recent call last):
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/aries_cloudagent/resolver/routes.py", line 96, in resolve_did
     context.profile, did
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/aries_cloudagent/resolver/did_resolver.py", line 73, in resolve_with_metadata
     resolver, doc = await self._resolve(profile, did)
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/aries_cloudagent/resolver/did_resolver.py", line 54, in _resolve
     did,
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/aries_cloudagent/resolver/base.py", line 146, in resolve
     return await self._resolve(profile, did)
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/aries_cloudagent/resolver/default/indy.py", line 67, in _resolve
     Ed25519VerificationKey2018, ident="key-1", public_key_base58=recipient_key
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/pydid/doc/builder.py", line 49, in add
     vmethod = type_.make(id=self._did.ref(ident), controller=controller, **kwargs)
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/pydid/resource.py", line 106, in make
     return cls(**kwargs)
   File "/home/indy/.pyenv/versions/3.6.13/lib/python3.6/site-packages/pydid/verification_method.py", line 49, in __init__
     super().__init__(**data)
   File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
 pydantic.error_wrappers.ValidationError: 1 validation error for Ed25519VerificationKey2018
 publicKeyBase58
   none is not an allowed value (type=type_error.none.not_allowed)
```


Discussion:
Not able to replicate this:
Used this config
```
- id: localVON
  is_production: false
  is_write: false
  genesis_url: 'http://host.docker.internal:9000/genesis'
- id: bcorvinTest
  is_production: true
  is_write: true
  genesis_url: 'http://test.bcovrin.vonx.io/genesis'
- id: greenlightTest
  is_production: false
  genesis_url: 'http://dev.greenlight.bcovrin.vonx.io/genesis'
```
Startup
```
PORTS="5002 8002" ./scripts/run_docker start --admin 0.0.0.0 5002 --inbound-transport http 0.0.0.0 8002 --outbound-transport http -e http://host.docker.internal:8002 --admin-insecure-mode --public-invites --auto-ping-connection --auto-accept-invites --auto-accept-requests --auto-respond-credential-offer --auto-respond-credential-request --auto-respond-presentation-proposal --auto-verify-presentation --seed 00000000000000000000000000000008 --auto-provision --recreate-wallet --wallet-type indy --wallet-name issuer --wa
llet-key mykey --genesis-transactions-list ./aries_cloudagent/multi_ledger_config.yml
```
On local von network, added a new DID `P9qHs8EYhyZPpKLUReNAWA` from seed `00000000000000000000000000000099` that is not added to other networks. Then executed `/ledger/did-verkey` with DID `P9qHs8EYhyZPpKLUReNAWA` and it successfully resolves it to `localVON`
I think I have found the cause of the problem within `DIDResolver`. I will provide details here soon.

Resolving patch:
diff --git a/aries_cloudagent/ledger/multiple_ledger/base_manager.py b/aries_cloudagent/ledger/multiple_ledger/base_manager.py
--- a/aries_cloudagent/ledger/multiple_ledger/base_manager.py
+++ b/aries_cloudagent/ledger/multiple_ledger/base_manager.py
@@ -6,6 +6,7 @@
 from ...core.error import BaseError
 from ...core.profile import Profile
 from ...ledger.base import BaseLedger
+from ...messaging.valid import IndyDID
 
 
 class MultipleLedgerManagerError(BaseError):
@@ -44,4 +45,7 @@ async def lookup_did_in_configured_ledgers(
 
     def extract_did_from_identifier(self, identifier: str) -> str:
         """Return did from record identifier (REV_REG_ID, CRED_DEF_ID, SCHEMA_ID)."""
-        return identifier.split(":")[0]
+        if bool(IndyDID.PATTERN.match(identifier)):
+            return identifier.split(":")[-1]
+        else:
+            return identifier.split(":")[0]


Test patch:
diff --git a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py
--- a/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py
+++ b/aries_cloudagent/ledger/multiple_ledger/tests/test_indy_manager.py
@@ -428,6 +428,14 @@ def test_extract_did_from_identifier(self):
             )
             == "WgWxqztrNooG92RXvxSTWv"
         )
+        assert (
+            self.manager.extract_did_from_identifier("WgWxqztrNooG92RXvxSTWv")
+            == "WgWxqztrNooG92RXvxSTWv"
+        )
+        assert (
+            self.manager.extract_did_from_identifier("did:sov:WgWxqztrNooG92RXvxSTWv")
+            == "WgWxqztrNooG92RXvxSTWv"
+        )
 
     async def test_get_production_ledgers(self):
         assert len(await self.manager.get_prod_ledgers()) == 2