Repository: hyperledger/aries-cloudagent-python

Issue:
incorrect did:key multicodec value
If `0xed` is prepended to the key data before base58 encoding, this results in an incorrect multicodec code.

Multicodec is designed to allow codes to be variable-length integers, using the [unsigned varint](https://github.com/multiformats/unsigned-varint) format. When the Ed25519 multicodec code is encoded as a varint, it turns into `0xed01`, this is the value that should be prepended before base58 encoding.


Discussion:

Resolving patch:
diff --git a/aries_cloudagent/messaging/decorators/attach_decorator.py b/aries_cloudagent/messaging/decorators/attach_decorator.py
--- a/aries_cloudagent/messaging/decorators/attach_decorator.py
+++ b/aries_cloudagent/messaging/decorators/attach_decorator.py
@@ -22,6 +22,8 @@
     set_urlsafe_b64,
     str_to_b64,
     unpad,
+    naked_to_did_key,
+    did_key_to_naked,
 )
 from ..models.base import BaseModel, BaseModelError, BaseModelSchema
 from ..valid import (
@@ -33,9 +35,6 @@
     UUIDFour,
 )
 
-MULTIBASE_B58_BTC = "z"
-MULTICODEC_ED25519_PUB = b"\xed"
-
 
 class AttachDecoratorDataJWSHeader(BaseModel):
     """Attach decorator data JWS header."""
@@ -198,19 +197,17 @@ def validate_single_xor_multi_sig(self, data: Mapping, **kwargs):
 def did_key(verkey: str) -> str:
     """Qualify verkey into DID key if need be."""
 
-    if verkey.startswith(f"did:key:{MULTIBASE_B58_BTC}"):
+    if verkey.startswith("did:key:"):
         return verkey
 
-    return f"did:key:{MULTIBASE_B58_BTC}" + bytes_to_b58(
-        MULTICODEC_ED25519_PUB + b58_to_bytes(verkey)
-    )
+    return naked_to_did_key(verkey)
 
 
 def raw_key(verkey: str) -> str:
     """Strip qualified key to raw key if need be."""
 
-    if verkey.startswith(f"did:key:{MULTIBASE_B58_BTC}"):
-        return bytes_to_b58(b58_to_bytes(verkey[9:])[1:])
+    if verkey.startswith("did:key:"):
+        return did_key_to_naked(verkey)
 
     return verkey
 
diff --git a/aries_cloudagent/messaging/jsonld/credential.py b/aries_cloudagent/messaging/jsonld/credential.py
--- a/aries_cloudagent/messaging/jsonld/credential.py
+++ b/aries_cloudagent/messaging/jsonld/credential.py
@@ -3,29 +3,24 @@
 import json
 
 from ...wallet.util import (
-    b58_to_bytes,
     b64_to_bytes,
     b64_to_str,
-    bytes_to_b58,
     bytes_to_b64,
     str_to_b64,
+    naked_to_did_key,
 )
 
 from .create_verify_data import create_verify_data
 from .error import BadJWSHeaderError
 
-MULTIBASE_B58_BTC = "z"
-MULTICODEC_ED25519_PUB = b"\xed"
-
 
 def did_key(verkey: str) -> str:
     """Qualify verkey into DID key if need be."""
 
     return (
         verkey
-        if verkey.startswith(f"did:key:{MULTIBASE_B58_BTC}")
-        else f"did:key:{MULTIBASE_B58_BTC}"
-        + bytes_to_b58(MULTICODEC_ED25519_PUB + b58_to_bytes(verkey))
+        if verkey.startswith("did:key:")
+        else naked_to_did_key(verkey)
     )
 
 


Test patch:
diff --git a/aries_cloudagent/messaging/jsonld/tests/test_credential.py b/aries_cloudagent/messaging/jsonld/tests/test_credential.py
--- a/aries_cloudagent/messaging/jsonld/tests/test_credential.py
+++ b/aries_cloudagent/messaging/jsonld/tests/test_credential.py
@@ -10,7 +10,7 @@
 class TestCredential(AsyncTestCase):
     async def test_did_key(self):
         did_key = test_module.did_key(TEST_VERKEY)
-        assert did_key.startswith(f"did:key:{test_module.MULTIBASE_B58_BTC}")
+        assert did_key.startswith("did:key:z")
         assert did_key == test_module.did_key(did_key)
 
     async def test_verify_jws_header(self):