Skip to content

Commit

Permalink
feat: add multikey <-> key helpers for askar
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Nov 10, 2023
1 parent 7919be4 commit 38211a7
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions didcomm_messaging/crypto/askar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ def __init__(self, key: Key, kid: str):
"""Initialize a new AskarKey instance."""
self.key = key
self._kid = kid
self._multikey = self.key_to_multikey(key)

codec = self.alg_to_codec.get(self.key.algorithm)
@classmethod
def key_to_multikey(cls, key: Key) -> str:
"""Get a multikey from an Askar Key instance."""
codec = cls.alg_to_codec.get(key.algorithm)
if not codec:
raise ValueError("Unsupported key type")

self._multikey = multibase.encode(
multicodec.wrap(multicodec.multicodec(codec), self.key.get_public_bytes()),
return multibase.encode(
multicodec.wrap(multicodec.multicodec(codec), key.get_public_bytes()),
"base58btc",
)

@classmethod
def _multikey_to_key(cls, multikey: str) -> Key:
def multikey_to_key(cls, multikey: str) -> Key:
"""Convert a multibase-encoded key to an Askar Key instance."""
decoded = multibase.decode(multikey)
codec, key = multicodec.unwrap(decoded)
Expand Down

0 comments on commit 38211a7

Please sign in to comment.