|
| 1 | +"""Test forwarding.""" |
| 2 | + |
| 3 | +from aries_askar import Key, KeyAlg, Store |
| 4 | +import base58 |
| 5 | +import did_peer_4 |
| 6 | +from did_peer_4.input_doc import KeySpec, input_doc_from_keys_and_services |
| 7 | +import pytest |
| 8 | +from didcomm_messaging.crypto.backend.askar import AskarKey |
| 9 | +from didcomm_messaging.v1.crypto.nacl import EdPublicKey, InMemSecretsManager |
| 10 | +from didcomm_messaging.v1.messaging import V1DIDCommMessaging |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_nacl_forward( |
| 15 | + alice: V1DIDCommMessaging, |
| 16 | + nacl_secrets: InMemSecretsManager, |
| 17 | +): |
| 18 | + """Test nacl crypto forwarding.""" |
| 19 | + local_key = EdPublicKey(nacl_secrets.create().verkey) |
| 20 | + routing_key = EdPublicKey(nacl_secrets.create().verkey) |
| 21 | + doc = input_doc_from_keys_and_services( |
| 22 | + [KeySpec(local_key.multikey, relationships=["authentication"])], |
| 23 | + [ |
| 24 | + { |
| 25 | + "id": "#didcomm", |
| 26 | + "type": "did-communication", |
| 27 | + "recipientKeys": ["#key-0"], |
| 28 | + "routingKeys": [f"did:key:{routing_key.multikey}#{routing_key.multikey}"], |
| 29 | + "serviceEndpoint": "https://example.com", |
| 30 | + } |
| 31 | + ], |
| 32 | + ) |
| 33 | + did = did_peer_4.encode(doc) |
| 34 | + msg = b"hello world" |
| 35 | + packed = await alice.pack(msg, did, did) |
| 36 | + unpacked = await alice.unpack(packed.message) |
| 37 | + forward = unpacked.message |
| 38 | + assert forward["@type"] == "https://didcomm.org/routing/1.0/forward" |
| 39 | + unwrapped = await alice.unpack(forward["msg"]) |
| 40 | + assert unwrapped.unpacked == msg |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.asyncio |
| 44 | +async def test_askar_forward( |
| 45 | + bob: V1DIDCommMessaging, |
| 46 | + store: Store, |
| 47 | +): |
| 48 | + """Test nacl crypto forwarding.""" |
| 49 | + key = Key.generate(KeyAlg.ED25519) |
| 50 | + kid = base58.b58encode(key.get_public_bytes()).decode() |
| 51 | + async with store.session() as session: |
| 52 | + await session.insert_key(kid, key) |
| 53 | + local_key = AskarKey(key, kid) |
| 54 | + |
| 55 | + key = Key.generate(KeyAlg.ED25519) |
| 56 | + kid = base58.b58encode(key.get_public_bytes()).decode() |
| 57 | + async with store.session() as session: |
| 58 | + await session.insert_key(kid, key) |
| 59 | + routing_key = AskarKey(key, kid) |
| 60 | + |
| 61 | + doc = input_doc_from_keys_and_services( |
| 62 | + [KeySpec(local_key.multikey, relationships=["authentication"])], |
| 63 | + [ |
| 64 | + { |
| 65 | + "id": "#didcomm", |
| 66 | + "type": "did-communication", |
| 67 | + "recipientKeys": ["#key-0"], |
| 68 | + "routingKeys": [f"did:key:{routing_key.multikey}#{routing_key.multikey}"], |
| 69 | + "serviceEndpoint": "https://example.com", |
| 70 | + } |
| 71 | + ], |
| 72 | + ) |
| 73 | + did = did_peer_4.encode(doc) |
| 74 | + msg = b"hello world" |
| 75 | + packed = await bob.pack(msg, did, did) |
| 76 | + unpacked = await bob.unpack(packed.message) |
| 77 | + forward = unpacked.message |
| 78 | + assert forward["@type"] == "https://didcomm.org/routing/1.0/forward" |
| 79 | + unwrapped = await bob.unpack(forward["msg"]) |
| 80 | + assert unwrapped.unpacked == msg |
0 commit comments