Skip to content

Commit

Permalink
testing jwt and key manage for p256
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <[email protected]>
  • Loading branch information
gmulhearn-anonyome committed Jan 14, 2025
1 parent b3399cf commit c0f5152
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 109 deletions.
2 changes: 1 addition & 1 deletion acapy_agent/wallet/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Meta:
async def resolve_public_key_by_kid_for_verify(
profile: Profile, kid: str
) -> Tuple[str, KeyType]:
"""Resolve public key material from a kid."""
"""Resolve public key verkey (base58 public key) and key type from a kid."""
resolver = profile.inject(DIDResolver)
vmethod: Resource = await resolver.dereference(
profile,
Expand Down
68 changes: 43 additions & 25 deletions acapy_agent/wallet/keys/tests/test_key_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,55 @@

class TestKeyOperations(IsolatedAsyncioTestCase):
seed = "00000000000000000000000000000000"
multikey = "z6MkgKA7yrw5kYSiDuQFcye4bMaJpcfHFry3Bx45pdWh3s8i"
verkey = "2ru5PcgeQzxF7QZYwQgDkG2K13PRqyigVw99zMYg8eML"
alg = "ed25519"
kid = "did:web:example.com#key-01"

ed25519_multikey = "z6MkgKA7yrw5kYSiDuQFcye4bMaJpcfHFry3Bx45pdWh3s8i"
ed25519_verkey = "2ru5PcgeQzxF7QZYwQgDkG2K13PRqyigVw99zMYg8eML"
ed25519_alg = "ed25519"

p256_multikey = "zDnaeSd75MAwSRmem34MfZEzSMjQNcpWLmzkbF8Su49AuA9U2"
p256_verkey = "demmi97mhJ7JQu31git4hQz8a1PD1dETJH9TVKaynNQv"
p256_alg = "p256"

async def asyncSetUp(self) -> None:
self.profile = await create_test_profile()
self.profile.context.injector.bind_instance(KeyTypes, KeyTypes())

async def test_key_creation(self):
async with self.profile.session() as session:
key_info = await MultikeyManager(session=session).create(seed=self.seed)
assert key_info["multikey"] == self.multikey
assert key_info["kid"] is None

key_info = await MultikeyManager(session=session).from_multikey(
multikey=self.multikey
)
assert key_info["multikey"] == self.multikey
assert key_info["kid"] is None

key_info = await MultikeyManager(session=session).update(
multikey=self.multikey, kid=self.kid
)
assert key_info["multikey"] == self.multikey
assert key_info["kid"] == self.kid

key_info = await MultikeyManager(session=session).from_kid(kid=self.kid)
assert key_info["multikey"] == self.multikey
assert key_info["kid"] == self.kid
for i, (alg, expected_multikey) in enumerate(
[
(self.ed25519_alg, self.ed25519_multikey),
(self.p256_alg, self.p256_multikey),
]
):
kid = f"did:web:example.com#key-0{i}"

key_info = await MultikeyManager(session=session).create(
seed=self.seed, alg=alg
)
assert key_info["multikey"] == expected_multikey
assert key_info["kid"] is None

key_info = await MultikeyManager(session=session).from_multikey(
multikey=expected_multikey
)
assert key_info["multikey"] == expected_multikey
assert key_info["kid"] is None

key_info = await MultikeyManager(session=session).update(
multikey=expected_multikey, kid=kid
)
assert key_info["multikey"] == expected_multikey
assert key_info["kid"] == kid

key_info = await MultikeyManager(session=session).from_kid(kid=kid)
assert key_info["multikey"] == expected_multikey
assert key_info["kid"] == kid

async def test_key_transformations(self):
assert multikey_to_verkey(self.multikey) == self.verkey
assert verkey_to_multikey(self.verkey, alg=self.alg) == self.multikey
for alg, multikey, verkey in [
(self.ed25519_alg, self.ed25519_multikey, self.ed25519_verkey),
(self.p256_alg, self.p256_multikey, self.p256_verkey),
]:
assert multikey_to_verkey(multikey) == verkey
assert verkey_to_multikey(verkey, alg=alg) == multikey
63 changes: 62 additions & 1 deletion acapy_agent/wallet/tests/test_did_method.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
from unittest import TestCase

from ..key_type import BLS12381G1, BLS12381G1G2, BLS12381G2, ED25519, X25519, KeyTypes
from ..key_type import (
BLS12381G1,
BLS12381G1G2,
BLS12381G2,
ED25519,
P256,
X25519,
KeyTypes,
)

ED25519_PREFIX_BYTES = b"\xed\x01"
BLS12381G1_PREFIX_BYTES = b"\xea\x01"
BLS12381G1G2_PREFIX_BYTES = b"\xee\x01"
BLS12381G2_PREFIX_BYTES = b"\xeb\x01"
X25519_PREFIX_BYTES = b"\xec\x01"
P256_PREFIX_BYTES = b"\x80\x24"

ED25519_KEY_NAME = "ed25519"
X25519_KEY_NAME = "x25519"
P256_KEY_NAME = "p256"
BLS12381G1_KEY_NAME = "bls12381g1"
BLS12381G2_KEY_NAME = "bls12381g2"
BLS12381G1G2_KEY_NAME = "bls12381g1g2"

ED25519_MULTICODEC_NAME = "ed25519-pub"
X25519_MULTICODEC_NAME = "x25519-pub"
P256_MULTICODEC_NAME = "p256-pub"
BLS12381G1_MULTICODEC_NAME = "bls12_381-g1-pub"
BLS12381G2_MULTICODEC_NAME = "bls12_381-g2-pub"
BLS12381G1G2_MULTICODEC_NAME = "bls12_381-g1g2-pub"

ED25519_JWS_ALG = "EdDSA"
X25519_JWS_ALG = None
P256_JWS_ALG = "ES256"
BLS12381G1_JWS_ALG = None
BLS12381G2_JWS_ALG = None
BLS12381G1G2_JWS_ALG = None


class TestKeyType(TestCase):
def test_from_multicodec_name(self):
key_types = KeyTypes()
assert key_types.from_multicodec_name(ED25519_MULTICODEC_NAME) == ED25519
assert key_types.from_multicodec_name(X25519_MULTICODEC_NAME) == X25519
assert key_types.from_multicodec_name(P256_MULTICODEC_NAME) == P256
assert key_types.from_multicodec_name(BLS12381G1_MULTICODEC_NAME) == BLS12381G1
assert key_types.from_multicodec_name(BLS12381G2_MULTICODEC_NAME) == BLS12381G2
assert (
Expand All @@ -37,6 +56,7 @@ def test_from_key_type(self):
key_types = KeyTypes()
assert key_types.from_key_type(ED25519_KEY_NAME) == ED25519
assert key_types.from_key_type(X25519_KEY_NAME) == X25519
assert key_types.from_key_type(P256_KEY_NAME) == P256
assert key_types.from_key_type(BLS12381G1_KEY_NAME) == BLS12381G1
assert key_types.from_key_type(BLS12381G2_KEY_NAME) == BLS12381G2
assert key_types.from_key_type(BLS12381G1G2_KEY_NAME) == BLS12381G1G2
Expand All @@ -46,6 +66,7 @@ def test_from_multicodec_prefix(self):
key_types = KeyTypes()
assert key_types.from_multicodec_prefix(ED25519_PREFIX_BYTES) == ED25519
assert key_types.from_multicodec_prefix(X25519_PREFIX_BYTES) == X25519
assert key_types.from_multicodec_prefix(P256_PREFIX_BYTES) == P256
assert key_types.from_multicodec_prefix(BLS12381G1_PREFIX_BYTES) == BLS12381G1
assert key_types.from_multicodec_prefix(BLS12381G2_PREFIX_BYTES) == BLS12381G2
assert key_types.from_multicodec_prefix(BLS12381G1G2_PREFIX_BYTES) == BLS12381G1G2
Expand All @@ -65,6 +86,10 @@ def test_from_prefixed_bytes(self):
)
== X25519
)
assert (
key_types.from_prefixed_bytes(b"".join([P256_PREFIX_BYTES, b"random-bytes"]))
== P256
)
assert (
key_types.from_prefixed_bytes(
b"".join([BLS12381G1_PREFIX_BYTES, b"random-bytes"])
Expand Down Expand Up @@ -94,3 +119,39 @@ def test_properties(self):
assert key_type.key_type == ED25519_KEY_NAME
assert key_type.multicodec_name == ED25519_MULTICODEC_NAME
assert key_type.multicodec_prefix == ED25519_PREFIX_BYTES
assert key_type.jws_algorithm == ED25519_JWS_ALG

key_type = X25519

assert key_type.key_type == X25519_KEY_NAME
assert key_type.multicodec_name == X25519_MULTICODEC_NAME
assert key_type.multicodec_prefix == X25519_PREFIX_BYTES
assert key_type.jws_algorithm == X25519_JWS_ALG

key_type = P256

assert key_type.key_type == P256_KEY_NAME
assert key_type.multicodec_name == P256_MULTICODEC_NAME
assert key_type.multicodec_prefix == P256_PREFIX_BYTES
assert key_type.jws_algorithm == P256_JWS_ALG

key_type = BLS12381G1

assert key_type.key_type == BLS12381G1_KEY_NAME
assert key_type.multicodec_name == BLS12381G1_MULTICODEC_NAME
assert key_type.multicodec_prefix == BLS12381G1_PREFIX_BYTES
assert key_type.jws_algorithm == BLS12381G1_JWS_ALG

key_type = BLS12381G2

assert key_type.key_type == BLS12381G2_KEY_NAME
assert key_type.multicodec_name == BLS12381G2_MULTICODEC_NAME
assert key_type.multicodec_prefix == BLS12381G2_PREFIX_BYTES
assert key_type.jws_algorithm == BLS12381G2_JWS_ALG

key_type = BLS12381G1G2

assert key_type.key_type == BLS12381G1G2_KEY_NAME
assert key_type.multicodec_name == BLS12381G1G2_MULTICODEC_NAME
assert key_type.multicodec_prefix == BLS12381G1G2_PREFIX_BYTES
assert key_type.jws_algorithm == BLS12381G1G2_JWS_ALG
Loading

0 comments on commit c0f5152

Please sign in to comment.