Skip to content

Commit

Permalink
Merge pull request #17 from Indicio-tech/test/additional-askar-tests
Browse files Browse the repository at this point in the history
feat: additional askar tests
  • Loading branch information
dbluhm authored Nov 16, 2023
2 parents 92886a4 + 4ad9c2b commit c93619c
Showing 1 changed file with 97 additions and 56 deletions.
153 changes: 97 additions & 56 deletions tests/crypto/test_askar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from aries_askar import Key, KeyAlg
from aries_askar import Key, KeyAlg, Store
from pydid import VerificationMethod
import pytest

from aries_askar.bindings import generate_raw_key
from didcomm_messaging.crypto.backend.askar import (
AskarCryptoService,
AskarKey,
AskarSecretKey,
AskarSecretsManager,
)


Expand All @@ -22,7 +24,7 @@ def crypto():

@pytest.mark.asyncio
async def test_1pu_round_trip(crypto: AskarCryptoService):
"""Authcrypt."""
"""Test ECDH-1PU (Authcrypt) round trip."""
alg = KeyAlg.X25519
alice_sk = Key.generate(alg)
alice_pk = Key.from_jwk(alice_sk.get_jwk_public())
Expand Down Expand Up @@ -58,68 +60,107 @@ async def test_es_round_trip(crypto: AskarCryptoService):
assert plaintext == MESSAGE


# to test store/secrets manager stuff
# store = await Store.open("sqlite::memory:")
# async with store.session as session():
# await session.insert_key("kid", alice_sk)
@pytest.mark.asyncio
async def test_askar_store():
alg = KeyAlg.X25519
alice_sk = Key.generate(alg)
alice_key = AskarKey(alice_sk, ALICE_KID)

raw_key = generate_raw_key()

store = await Store.provision("sqlite://:memory:", "raw", raw_key, recreate=True)
askar_secrets_manager = AskarSecretsManager(store)

async with askar_secrets_manager.store.session() as session:
await session.insert_key(alice_key.kid, alice_key.key)

retrieved_key = await askar_secrets_manager.get_secret_by_kid(alice_key.kid)

assert retrieved_key.kid == alice_key.kid # type:ignore
assert (
retrieved_key.key.get_public_bytes() # type:ignore
== alice_key.key.get_public_bytes()
)


@pytest.mark.parametrize(
["vm", "expected"],
[
[
{
"id": "#6LSqPZfn",
"type": "X25519KeyAgreementKey2020",
"publicKeyMultibase": "z6LSqPZfn9krvgXma2icTMKf2uVcYhKXsudCmPoUzqGYW24U",
"controller": "did:example:1",
},
"did:example:1#6LSqPZfn",
],
[
{
"id": "#6MkrCD1c",
"type": "Ed25519VerificationKey2020",
"publicKeyMultibase": "z6MkrCD1csqtgdj8sjrsu8jxcbeyP6m7LiK87NzhfWqio5yr",
"controller": "did:example:2",
},
"did:example:2#6MkrCD1c",
],
[
{
"id": "#1",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG",
"controller": "did:example:3",
},
"did:example:3#1",
],
[
{
"id": "#1",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "3dtu2WWtd5ELwRTJEPzmEJUYEp8Qq36N2QA24g9tFXK9",
"controller": "did:peer:4zQmRMVzDUXhV64pfw3vFaDvyExjzW9oBXCF2n4zYCaHQFAT:zMx3zwMnDECV3GiFs8nmHr38TziMEEkcgFBEDH5PXQ8hxnMrwNfB9wTwskpJMjggeg8NF1jeDSK5772op2zLLdy8TGFCEiYQxpUvvku8qSCZx5Q8V9Li9mDp6WEqGabXLQ9GTinmyQHQyJ6TcfbHaTtJUFjHS962LFPdUwv3aDK673Pci2doTyHVTAsw4m5eToS2dKbtix9f7HNxwvixnbQucWNAWVAF6HTxFYRYmrRPDmeE8n7V1fXFkY7yvR6BWxKiWwHd8Vb1TbBBRStf5niRM2dUAyjJorTstPWSfG2pN5DsRF81NUd7Aif4EhNAQEJCTuAHxQ3rCnNkb9Pf7YTTxbt1t25YgDMioDi4uFhYcnTbHj7D74yNPC2Cfk6WasU69hMxj7Wxro58vtkA6hvDWGtnDyX4PzntBp3fn62R25HW2jadsZMiJpm5ufpYSktEFEHX6gGeF4KPgyU8b2hhyS3FKL4DULYLB6d6CZqUpwrJesGfDtFjfG1btbdmjd6Lm7FCbL3fU9E3AJWEmnFkg16vARiQ1CrzeS9SyNtybKCk4",
},
"did:peer:4zQmRMVzDUXhV64pfw3vFaDvyExjzW9oBXCF2n4zYCaHQFAT:zMx3zwMnDECV3GiFs8nmHr38TziMEEkcgFBEDH5PXQ8hxnMrwNfB9wTwskpJMjggeg8NF1jeDSK5772op2zLLdy8TGFCEiYQxpUvvku8qSCZx5Q8V9Li9mDp6WEqGabXLQ9GTinmyQHQyJ6TcfbHaTtJUFjHS962LFPdUwv3aDK673Pci2doTyHVTAsw4m5eToS2dKbtix9f7HNxwvixnbQucWNAWVAF6HTxFYRYmrRPDmeE8n7V1fXFkY7yvR6BWxKiWwHd8Vb1TbBBRStf5niRM2dUAyjJorTstPWSfG2pN5DsRF81NUd7Aif4EhNAQEJCTuAHxQ3rCnNkb9Pf7YTTxbt1t25YgDMioDi4uFhYcnTbHj7D74yNPC2Cfk6WasU69hMxj7Wxro58vtkA6hvDWGtnDyX4PzntBp3fn62R25HW2jadsZMiJpm5ufpYSktEFEHX6gGeF4KPgyU8b2hhyS3FKL4DULYLB6d6CZqUpwrJesGfDtFjfG1btbdmjd6Lm7FCbL3fU9E3AJWEmnFkg16vARiQ1CrzeS9SyNtybKCk4#1",
],
[
{
"id": "#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN",
"type": "Ed25519VerificationKey2018",
"publicKeyBase58": "DK7uJiq9PnPnj7AmNZqVBFoLuwTjT1hFPrk6LSjZ2JRz",
"controller": "did:example:4",
},
"did:example:4#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN",
],
],
)
@pytest.mark.asyncio
async def test_key_from_verification_method(vm, expected):
vm = VerificationMethod.deserialize(vm)

key = AskarCryptoService.verification_method_to_public_key(vm)
print(key.kid)
assert type(key) == AskarKey
assert key.kid == expected


# TODO @pytest.mark.parametrize for the different dicts? See below for more examples
@pytest.mark.xfail
@pytest.mark.asyncio
async def test_key_from_verification_method():
async def test_key_from_verification_method_unsupported():
# Fails to deserialize, don't know what we can do about that (or if it's even valuable to have this test here)
vm = VerificationMethod.deserialize(
{
"id": "#6LSqPZfn",
"type": "X25519KeyAgreementKey2020",
"publicKeyMultibase": "z6LSqPZfn9krvgXma2icTMKf2uVcYhKXsudCmPoUzqGYW24U",
"controller": "did:example:123",
# Here's a case that we probably don't support right now:
"type": "Ed25519VerificationKey2018",
"publicKeyJwk": {
"kty": "OKP",
"crv": "Ed25519",
"x": "UTBElpNSZB8dS_R9rzWnWB-ozdtL7Sz96RQZhwnzur8",
},
"id": "#z6MkjvBkt8ETnxXGBFPSGgYKb43q7oNHLX8BiYSPcXVG6gY6",
}
)

key = AskarCryptoService.verification_method_to_public_key(vm)
print(key.kid)
assert type(key) == AskarKey
assert key.kid.startswith("did:example:123")


# Verification Methods
# {
# "id": "#6LSqPZfn",
# "type": "X25519KeyAgreementKey2020",
# "publicKeyMultibase": "z6LSqPZfn9krvgXma2icTMKf2uVcYhKXsudCmPoUzqGYW24U"
# },
# {
# "id": "#6MkrCD1c",
# "type": "Ed25519VerificationKey2020",
# "publicKeyMultibase": "z6MkrCD1csqtgdj8sjrsu8jxcbeyP6m7LiK87NzhfWqio5yr"
# }

# {
# "id": "#1",
# "type": "Ed25519VerificationKey2018",
# "publicKeyBase58": "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG"
# }

# {
# "id": "#1",
# "type": "Ed25519VerificationKey2018",
# "publicKeyBase58": "3dtu2WWtd5ELwRTJEPzmEJUYEp8Qq36N2QA24g9tFXK9",
# "controller": "did:peer:4zQmRMVzDUXhV64pfw3vFaDvyExjzW9oBXCF2n4zYCaHQFAT:zMx3zwMnDECV3GiFs8nmHr38TziMEEkcgFBEDH5PXQ8hxnMrwNfB9wTwskpJMjggeg8NF1jeDSK5772op2zLLdy8TGFCEiYQxpUvvku8qSCZx5Q8V9Li9mDp6WEqGabXLQ9GTinmyQHQyJ6TcfbHaTtJUFjHS962LFPdUwv3aDK673Pci2doTyHVTAsw4m5eToS2dKbtix9f7HNxwvixnbQucWNAWVAF6HTxFYRYmrRPDmeE8n7V1fXFkY7yvR6BWxKiWwHd8Vb1TbBBRStf5niRM2dUAyjJorTstPWSfG2pN5DsRF81NUd7Aif4EhNAQEJCTuAHxQ3rCnNkb9Pf7YTTxbt1t25YgDMioDi4uFhYcnTbHj7D74yNPC2Cfk6WasU69hMxj7Wxro58vtkA6hvDWGtnDyX4PzntBp3fn62R25HW2jadsZMiJpm5ufpYSktEFEHX6gGeF4KPgyU8b2hhyS3FKL4DULYLB6d6CZqUpwrJesGfDtFjfG1btbdmjd6Lm7FCbL3fU9E3AJWEmnFkg16vARiQ1CrzeS9SyNtybKCk4"
# }

# {
# "id": "#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN",
# "type": "Ed25519VerificationKey2018",
# "publicKeyBase58": "DK7uJiq9PnPnj7AmNZqVBFoLuwTjT1hFPrk6LSjZ2JRz"
# }

# Here's a case that we probably don't support right now:
# {
# "type": "Ed25519VerificationKey2018",
# "publicKeyJwk": {
# "kty": "OKP",
# "crv": "Ed25519",
# "x": "UTBElpNSZB8dS_R9rzWnWB-ozdtL7Sz96RQZhwnzur8"
# },
# "id": "#z6MkjvBkt8ETnxXGBFPSGgYKb43q7oNHLX8BiYSPcXVG6gY6"
# }
# assert key.kid == expected

0 comments on commit c93619c

Please sign in to comment.