Skip to content

Commit

Permalink
fix: more efficient kid derivation
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Jun 6, 2024
1 parent 2a136b7 commit 839fbfa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions didcomm_messaging/crypto/backend/askar.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def multikey(self) -> str:
"""Get the key in multibase format."""
return self._multikey

@property
def key_bytes(self) -> bytes:
"""Get the bytes of the key."""
return self.key.get_public_bytes()


class AskarSecretKey(SecretKey):
"""Secret key implementation for Askar."""
Expand Down
12 changes: 12 additions & 0 deletions didcomm_messaging/crypto/backend/authlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ def multikey(self) -> str:
"""Return the key in multikey format."""
return self._multikey

@property
def key_bytes(self) -> bytes:
"""Get the bytes of the key."""
jwk = self.key.as_dict(is_private=False)
codec = self.kty_crv_to_codec.get((jwk["kty"], jwk.get("crv")))

if not codec:
raise ValueError("Unsupported key type")

key_bytes = b64url.decode(jwk["x"])
return key_bytes

@classmethod
def key_to_multikey(cls, key: AsymmetricKey) -> str:
"""Convert an Authlib key to a multikey."""
Expand Down
5 changes: 5 additions & 0 deletions didcomm_messaging/crypto/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def kid(self) -> str:
def multikey(self) -> str:
"""Get the key in multikey format."""

@property
@abstractmethod
def key_bytes(self) -> bytes:
"""Get the bytes of the key."""


class SecretKey(ABC):
"""Secret Key Type."""
Expand Down
26 changes: 9 additions & 17 deletions didcomm_messaging/legacy/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from didcomm_messaging.crypto import P, S, SecretsManager
from didcomm_messaging.legacy.base import LegacyCryptoService
from didcomm_messaging.legacy.packaging import LegacyPackagingService
from didcomm_messaging.multiformats import multibase, multicodec
from didcomm_messaging.resolver import DIDResolver


Expand Down Expand Up @@ -59,13 +58,6 @@ class Target:
class LegacyDIDCommMessagingService(Generic[P, S]):
"""Main entrypoint for DIDComm Messaging."""

def multikey_to_kid(self, multikey: str) -> str:
"""Return a kid from a multikey."""
codec, data = multicodec.unwrap(multibase.decode(multikey))
if codec != multicodec.multicodec("ed25519-pub"):
raise LegacyDIDCommMessagingError("DIDComm v1 requires ed25519 keys")
return base58.b58encode(data).decode()

async def did_to_target(
self, crypto: LegacyCryptoService[P, S], resolver: DIDResolver, did: str
) -> Target:
Expand All @@ -81,19 +73,19 @@ async def did_to_target(
target = services[0]

recipient_keys = [
self.multikey_to_kid(
base58.b58encode(
crypto.verification_method_to_public_key(
doc.dereference_as(VerificationMethod, recip)
).multikey
)
).key_bytes
).decode()
for recip in target.recipient_keys
]
routing_keys = [
self.multikey_to_kid(
base58.b58encode(
crypto.verification_method_to_public_key(
doc.dereference_as(VerificationMethod, routing_key)
).multikey
)
).key_bytes
).decode()
for routing_key in target.routing_keys
]
endpoint = target.service_endpoint
Expand Down Expand Up @@ -121,11 +113,11 @@ async def from_did_to_kid(
target = services[0]

recipient_keys = [
self.multikey_to_kid(
base58.b58encode(
crypto.verification_method_to_public_key(
doc.dereference_as(VerificationMethod, recip)
).multikey
)
).key_bytes
).decode()
for recip in target.recipient_keys
]
return recipient_keys[0]
Expand Down
5 changes: 5 additions & 0 deletions didcomm_messaging/legacy/nacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def multikey(self) -> str:
multicodec.wrap("ed25519-pub", base58.b58decode(self.key)), "base58btc"
)

@property
def key_bytes(self) -> bytes:
"""Get the bytes of the key."""
return self.value


class NaclLegacyCryptoService(LegacyCryptoService[EdPublicKey, KeyPair]):
"""Legacy crypto service using pynacl."""
Expand Down

0 comments on commit 839fbfa

Please sign in to comment.