def test_build_and_dereference(): builder = DIDDocumentBuilder("did:example:123") builder.verification_method.add(Ed25519VerificationKey2018, public_key_base58="test", ident="1") doc = builder.build() assert doc.dereference("#1")
def test_programmatic_construction(): builder = DIDDocumentBuilder("did:example:123") assert builder.context == ["https://www.w3.org/ns/did/v1"] vmethod1 = builder.verification_method.add(Ed25519VerificationKey2018, public_key_base58="1234") builder.authentication.reference(vmethod1.id) builder.authentication.embed(Ed25519VerificationKey2018, public_key_base58="abcd") builder.service.add(type_="example", service_endpoint="https://example.com") assert builder.build().serialize() == DOC6
def test_programmatic_construction(): ed25519 = VerificationSuite("Ed25519VerificationKey2018", "publicKeyBase58") builder = DIDDocumentBuilder("did:example:123") assert builder.context == ["https://www.w3.org/ns/did/v1"] with builder.verification_methods.defaults(suite=ed25519) as vmethods: vmethod1 = vmethods.add("1234") with builder.authentication.defaults(suite=ed25519) as auth: auth.reference(vmethod1.id) auth.embed("abcd") with builder.services.defaults() as services: services.add(type_="example", endpoint="https://example.com") assert builder.build().serialize() == DOC6
def test_key_rotation_from_doc(): doc = DIDDocument.deserialize(DOC6) vmethod0 = doc.dereference("did:example:123#key-0") builder = DIDDocumentBuilder.from_doc(doc) builder.verification_method.remove(vmethod0) builder.authentication.remove(vmethod0.id) assert builder.build().serialize() == { "@context": ["https://www.w3.org/ns/did/v1"], "id": "did:example:123", "authentication": [ { "id": "did:example:123#auth-0", "type": "Ed25519VerificationKey2018", "controller": "did:example:123", "publicKeyBase58": "abcd", }, ], "service": [{ "id": "did:example:123#service-0", "type": "example", "serviceEndpoint": "https://example.com", }], }
def test_builder_from_doc(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) with builder.verification_methods.defaults() as vmethods: vmethods.add(suite=VerificationSuite("Example", "publicKeyExample"), material="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2
def test_programmatic_construction_didcomm(): builder = DIDDocumentBuilder("did:example:123") with builder.verification_methods.defaults( suite=VerificationSuite("Example", "publicKeyBase58")) as vmethods: key = vmethods.add("1234") route = vmethods.add("abcd") with builder.services.defaults() as services: services = cast(ServiceBuilder, services) services.add_didcomm(endpoint="https://example.com", recipient_keys=[key], routing_keys=[route]) assert builder.build().serialize() == { "@context": "https://www.w3.org/ns/did/v1", "id": "did:example:123", "verificationMethod": [ { "id": "did:example:123#keys-0", "type": "Example", "controller": "did:example:123", "publicKeyBase58": "1234", }, { "id": "did:example:123#keys-1", "type": "Example", "controller": "did:example:123", "publicKeyBase58": "abcd", }, ], "service": [{ "id": "did:example:123#service-0", "type": "did-communication", "serviceEndpoint": "https://example.com", "recipientKeys": ["did:example:123#keys-0"], "routingKeys": ["did:example:123#keys-1"], "priority": 0, }], }
def test_programmatic_construction_didcomm(): builder = DIDDocumentBuilder("did:example:123") key = builder.verification_method.add(ExampleVerificationMethod, public_key_example="1234") route = builder.verification_method.add(ExampleVerificationMethod, public_key_example="abcd") builder.service.add_didcomm( service_endpoint="https://example.com", recipient_keys=[key], routing_keys=[route], ) assert builder.build().serialize() == { "@context": ["https://www.w3.org/ns/did/v1"], "id": "did:example:123", "verificationMethod": [ { "id": "did:example:123#key-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "1234", }, { "id": "did:example:123#key-1", "type": "Example", "controller": "did:example:123", "publicKeyExample": "abcd", }, ], "service": [{ "id": "did:example:123#service-0", "type": "did-communication", "serviceEndpoint": "https://example.com", "recipientKeys": ["did:example:123#key-0"], "routingKeys": ["did:example:123#key-1"], "priority": 0, }], }
def test_builder_from_doc_remove(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) vmethod = builder.verification_method.add(ExampleVerificationMethod, public_key_example="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2 builder.verification_method.remove(vmethod) assert len(builder.build().serialize()["verificationMethod"]) == 1 service = builder.service.add("example", "http://example.com", "ident") assert len(builder.build().serialize()["service"]) == 2 builder.service.remove(service) assert len(builder.build().serialize()["service"]) == 1 assertion = builder.assertion_method.add( ExampleVerificationMethod, ident="123", public_key_example="1234", ) assert len(builder.build().serialize()["assertionMethod"]) == 1 builder.assertion_method.remove(assertion) assert "assertionMethod" not in builder.build().serialize()
def test_builder_from_doc_remove(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) with builder.verification_methods.defaults() as vmethods: vmethod = vmethods.add(suite=VerificationSuite("Example", "publicKeyExample"), material="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2 builder.verification_methods.remove(vmethod) assert len(builder.build().serialize()["verificationMethod"]) == 1 service = builder.services.add("example", "http://example.com", "ident") assert len(builder.build().serialize()["service"]) == 2 builder.services.remove(service) assert len(builder.build().serialize()["service"]) == 1 assertion = builder.assertion_method.add( ident="123", suite=VerificationSuite("Example", "publicKeyExample"), material="1234", ) assert len(builder.build().serialize()["assertionMethod"]) == 1 builder.assertion_method.remove(assertion) assert "assertionMethod" not in builder.build().serialize()
def test_builder_from_doc(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) builder.verification_method.add(ExampleVerificationMethod, public_key_example="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2
def test_relationship_builder_ref_x(): builder = DIDDocumentBuilder("did:example:123") with pytest.raises(ValueError): builder.authentication.reference("123")
def test_all_relationship_builders(): builder = DIDDocumentBuilder("did:example:123") vmethod = builder.verification_method.add(Ed25519VerificationKey2018, public_key_base58="12345") builder.authentication.reference(vmethod.id) builder.authentication.embed(ExampleVerificationMethod, public_key_example="auth") builder.assertion_method.reference(vmethod.id) builder.assertion_method.embed(ExampleVerificationMethod, public_key_example="assert") builder.key_agreement.reference(vmethod.id) builder.key_agreement.embed(ExampleVerificationMethod, public_key_example="key_agreement") builder.capability_invocation.reference(vmethod.id) builder.capability_invocation.embed( ExampleVerificationMethod, public_key_example="capability_invocation") builder.capability_delegation.reference(vmethod.id) builder.capability_delegation.embed( ExampleVerificationMethod, public_key_example="capability_delegation") assert builder.build().serialize() == { "@context": ["https://www.w3.org/ns/did/v1"], "id": "did:example:123", "verificationMethod": [ { "id": "did:example:123#key-0", "type": "Ed25519VerificationKey2018", "controller": "did:example:123", "publicKeyBase58": "12345", }, ], "authentication": [ "did:example:123#key-0", { "id": "did:example:123#auth-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "auth", }, ], "assertionMethod": [ "did:example:123#key-0", { "id": "did:example:123#assert-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "assert", }, ], "keyAgreement": [ "did:example:123#key-0", { "id": "did:example:123#key-agreement-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "key_agreement", }, ], "capabilityInvocation": [ "did:example:123#key-0", { "id": "did:example:123#capability-invocation-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "capability_invocation", }, ], "capabilityDelegation": [ "did:example:123#key-0", { "id": "did:example:123#capability-delegation-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "capability_delegation", }, ], }
def test_vmethod_builder_x_no_ident(): builder = DIDDocumentBuilder("did:example:123") with pytest.raises(ValueError): builder.authentication.embed("1234", suite=VerificationSuite( "Example", "publicKeyExample"))
def test_all_relationship_builders(): builder = DIDDocumentBuilder("did:example:123") with builder.verification_methods.defaults() as vmethods: vmethods = cast(VerificationMethodBuilder, vmethods) vmethod = vmethods.add( suite=VerificationSuite("Ed25519VerificationKey2018", "publicKeyBase58"), material="12345", ) with builder.authentication.defaults() as auth: auth.reference(vmethod.id) auth.embed(suite=VerificationSuite("Example", "publicKeyExample"), material="auth") with builder.assertion_method.defaults() as assertion: assertion.reference(vmethod.id) assertion.embed(suite=VerificationSuite("Example", "publicKeyExample"), material="assert") with builder.key_agreement.defaults() as key_agreement: key_agreement.reference(vmethod.id) key_agreement.embed( suite=VerificationSuite("Example", "publicKeyExample"), material="key_agreement", ) with builder.capability_invocation.defaults() as capability_invocation: capability_invocation.reference(vmethod.id) capability_invocation.embed( suite=VerificationSuite("Example", "publicKeyExample"), material="capability_invocation", ) with builder.capability_delegation.defaults() as capability_delegation: capability_delegation.reference(vmethod.id) capability_delegation.embed( suite=VerificationSuite("Example", "publicKeyExample"), material="capability_delegation", ) assert builder.build().serialize() == { "@context": "https://www.w3.org/ns/did/v1", "id": "did:example:123", "verificationMethod": [ { "id": "did:example:123#keys-0", "type": "Ed25519VerificationKey2018", "controller": "did:example:123", "publicKeyBase58": "12345", }, ], "authentication": [ "did:example:123#keys-0", { "id": "did:example:123#auth-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "auth", }, ], "assertionMethod": [ "did:example:123#keys-0", { "id": "did:example:123#assert-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "assert", }, ], "keyAgreement": [ "did:example:123#keys-0", { "id": "did:example:123#key-agreement-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "key_agreement", }, ], "capabilityInvocation": [ "did:example:123#keys-0", { "id": "did:example:123#capability-invocation-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "capability_invocation", }, ], "capabilityDelegation": [ "did:example:123#keys-0", { "id": "did:example:123#capability-delegation-0", "type": "Example", "controller": "did:example:123", "publicKeyExample": "capability_delegation", }, ], }
def test_programmatic_construction_x_no_suite(): builder = DIDDocumentBuilder("did:example:123") with pytest.raises(ValueError): with builder.verification_methods.defaults() as vmethods: vmethods.add("1234")