Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def test_init_mapping():
    vmethod = VerificationMethod(
        id_=DIDUrl("did:example:123", fragment="keys-1"),
        suite=VerificationSuite("TestType", "publicKeyBase58"),
        controller=DID("did:example:123"),
        material="12345",
    )
    assert isinstance(vmethod.id, DIDUrl)
    assert str(vmethod.id) == "did:example:123#keys-1"
    assert isinstance(vmethod.controller, DID)
    assert str(vmethod.controller) == "did:example:123"
Ejemplo n.º 3
0
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()
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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,
        }],
    }
Ejemplo n.º 6
0
def test_suite_derive(values, prop):
    assert VerificationSuite.derive(
        "type", **values).verification_material_prop == prop
Ejemplo n.º 7
0
def test_suite_derive_x(values):
    with pytest.raises(InvalidVerificationMaterial):
        VerificationSuite.derive("type", **values)
Ejemplo n.º 8
0
        {
            "one": 2,
            "three": 4
        },
    ],
)
def test_suite_derive_x(values):
    with pytest.raises(InvalidVerificationMaterial):
        VerificationSuite.derive("type", **values)


@pytest.mark.parametrize(
    "lhs, rhs",
    [
        (
            VerificationSuite("type", "publiceyPem"),
            VerificationSuite("type", "publiceyPem"),
        ),
        (
            VerificationSuite("type", "publicKeyBase58"),
            VerificationSuite("type", "publicKeyBase58"),
        ),
    ],
)
def test_suite_eq(lhs, rhs):
    assert lhs == rhs


@pytest.mark.parametrize(
    "lhs, rhs",
    [
Ejemplo n.º 9
0
def test_vmethod_builder_x_no_ident():
    builder = DIDDocumentBuilder("did:example:123")
    with pytest.raises(ValueError):
        builder.authentication.embed("1234",
                                     suite=VerificationSuite(
                                         "Example", "publicKeyExample"))
Ejemplo n.º 10
0
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",
            },
        ],
    }