From 3171489dc8e42d80b1e93f098b6546175ef37b2d Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Mon, 13 Nov 2023 19:03:29 -0500 Subject: [PATCH] feat: add extra params to key spec For use with input doc generation helper Signed-off-by: Daniel Bluhm --- did_peer_4/input_doc.py | 10 +++--- tests/test_input_doc.py | 69 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/did_peer_4/input_doc.py b/did_peer_4/input_doc.py index fb90679..cc8d422 100644 --- a/did_peer_4/input_doc.py +++ b/did_peer_4/input_doc.py @@ -24,7 +24,9 @@ class KeySpec: """Key specification.""" multikey: str - relationships: Sequence[Relationship] + relationships: Optional[Sequence[Relationship]] = None + ident: Optional[str] = None + type: Optional[str] = None def input_doc_from_keys_and_services( @@ -41,12 +43,12 @@ def input_doc_from_keys_and_services( ident = f"#key-{index}" input_doc.setdefault("verificationMethod", []).append( { - "id": ident, - "type": "Multikey", + "id": key.ident or ident, + "type": key.type or "Multikey", "publicKeyMultibase": key.multikey, } ) - for relationship in key.relationships: + for relationship in key.relationships or []: if relationship not in RELATIONSHIPS: raise ValueError(f"Invalid relationship: {relationship}") diff --git a/tests/test_input_doc.py b/tests/test_input_doc.py index e88ff41..2e8b234 100644 --- a/tests/test_input_doc.py +++ b/tests/test_input_doc.py @@ -4,6 +4,7 @@ ED25519_MULTIKEY = "z6MkrCD1csqtgdj8sjrsu8jxcbeyP6m7LiK87NzhfWqio5yr" X25519_MULTIKEY = "z6LSqPZfn9krvgXma2icTMKf2uVcYhKXsudCmPoUzqGYW24U" +ANOTHER_ED25519_MULTIKEY = "z6Mkq4o8kZj1nK5z5Y8K8gY7aV9j8jg6YJzqZ6Dj9j7eY4d2" def test_input_doc_generation(): @@ -62,3 +63,71 @@ def test_input_doc_generation(): } assert validate_input_document(input_doc) assert encode(input_doc) + + +def test_input_doc_generation_extra_params(): + input_doc = input_doc_from_keys_and_services( + [ + KeySpec( + ident="#auth-0", + multikey=ED25519_MULTIKEY, + relationships=["authentication"], + ), + KeySpec( + ident="#agree-0", + multikey=X25519_MULTIKEY, + relationships=["keyAgreement"], + ), + KeySpec( + multikey=ANOTHER_ED25519_MULTIKEY, + ), + ], + [ + { + "id": "#didcommmessaging-0", + "type": "DIDCommMessaging", + "serviceEndpoint": { + "uri": "didcomm:transport/queue", + "accept": ["didcomm/v2"], + }, + } + ], + ) + + assert input_doc == { + "@context": [ + "https://www.w3.org/ns/did/v1", + "https://w3id.org/security/multikey/v1", + ], + "verificationMethod": [ + { + "id": "#auth-0", + "type": "Multikey", + "publicKeyMultibase": ED25519_MULTIKEY, + }, + { + "id": "#agree-0", + "type": "Multikey", + "publicKeyMultibase": X25519_MULTIKEY, + }, + { + "id": "#key-2", + "type": "Multikey", + "publicKeyMultibase": ANOTHER_ED25519_MULTIKEY, + }, + ], + "authentication": ["#key-0"], + "keyAgreement": ["#key-1"], + "service": [ + { + "id": "#didcommmessaging-0", + "type": "DIDCommMessaging", + "serviceEndpoint": { + "uri": "didcomm:transport/queue", + "accept": ["didcomm/v2"], + }, + } + ], + } + assert validate_input_document(input_doc) + assert encode(input_doc)