sicpa-dlab / peer-did-python

Peer DID method implementation in Python
Apache License 2.0
11 stars 10 forks source link

Service from create_peer_did_numalgo_2 serde to UnknownService #60

Open Jsyro opened 1 year ago

Jsyro commented 1 year ago

What

After providing this dictionary as the service parameter to create_peer_did_numalgo_2(), the DIDDocument that is returned has an object of type Service.

                service = {
                    "type": "DIDCommMessaging",
                    "serviceEndpoint": self.profile.settings.get("default_endpoint"),
                    "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"],
                }

After that returned DIDDocument is serialized and deserialized, the Service object becomes a UnknownService object, when it should become a DIDCommService object.

Why

This is needed to create a did:peer:2 did and the corresponding did_doc in way that can be encoded into secure messages (like acapy).

Success Criteria

the type string should result in the appropriate pydid.service Class to be instantiated.

Jsyro commented 1 year ago

Here is a sample script demonstrating my issue. I'm looking into making an appropriate change that will resolve it.

from peerdid.dids import resolve_peer_did, DIDDocument, create_peer_did_numalgo_2
from peerdid.keys import Ed25519VerificationKey2018, X25519KeyAgreementKey, Ed25519VerificationKey
from pydid import DIDCommService

from pprint import pprint
from json import loads

#starting params
verkey = "jBQAjABKakepe5Ay8qTYp1RWN5eaWnKjfepi3fXiXUL"
service = {
    "type": "did-communication",
    "serviceEndpoint": "http://host.docker.internal:9060",
    "accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"],
}

#use lib to create peer_did
enc_keys = [X25519KeyAgreementKey.from_base58(verkey)]
sign_keys = [Ed25519VerificationKey.from_base58(verkey)]
peer_did = create_peer_did_numalgo_2(enc_keys, sign_keys, service)

#resolve did and serde
did_doc = resolve_peer_did(peer_did)
dd_str = did_doc.to_json()
deser_did_doc = DIDDocument.from_json(dd_str)

#use pydid to explicitly create DIDCommService
vm = Ed25519VerificationKey2018.make(
    id="#resv",
    controller=peer_did,
    public_key_base58=verkey,
)

dc_service = DIDCommService.make(
    id="#ress",
    service_endpoint="http://host.docker.internal:9060",
    recipient_keys=["#resv"]
)

#resolve document and serde
dd = DIDDocument.make(id=peer_did,verification_method=[vm],service=[dc_service])
dd2_str = dd.to_json()
deser_dd2 =DIDDocument.from_json(dd2_str)

#print library+serde
print("")
print("create_peer_did_numalgo_2")
pprint(type(did_doc.service[0]))
pprint(type(deser_did_doc.service[0]))

#print DIDDocument+serde
print("")
print("DIDDocument.make")
pprint(type(dd.service[0]))
pprint(type(deser_dd2.service[0]))

#isolate json differences after serialization
print("")
print("json service string")
print(loads(dd_str)["service"])
print("json DIDCommService string")
print(loads(dd2_str)["service"])

OUTPUT


create_peer_did_numalgo_2
<class 'pydid.service.Service'>
<class 'pydid.service.UnknownService'>

DIDDocument.make
<class 'pydid.service.DIDCommService'>
<class 'pydid.service.DIDCommService'>

json service string
[{'id': '#did-communication-0', 'type': 'did-communication', 'serviceEndpoint': 'http://host.docker.internal:9060', 'accept': ['didcomm/v2', 'didcomm/aip2;env=rfc587']}]
json DIDCommService string
[{'id': '#ress', 'type': 'did-communication', 'serviceEndpoint': 'http://host.docker.internal:9060', 'recipientKeys': ['#resv'], 'routingKeys': [], 'priority': 0}]
Jsyro commented 1 year ago

Updating pydid to the current HEAD installed as pydid-0.3.9a0 only changes the output slightly.

create_peer_did_numalgo_2
<class 'pydid.service.Service'>
<class 'pydid.service.UnknownService'>

DIDDocument.make
<class 'pydid.service.DIDCommV1Service'>
<class 'pydid.service.DIDCommV1Service'>

json service string
[{'id': '#did-communication-0', 'type': 'did-communication', 'serviceEndpoint': 'http://host.docker.internal:9060', 'accept': ['didcomm/v2', 'didcomm/aip2;env=rfc587']}]
json DIDCommService string
[{'id': '#ress', 'type': 'did-communication', 'serviceEndpoint': 'http://host.docker.internal:9060', 'recipientKeys': ['#resv'], 'routingKeys': [], 'priority': 0}]
Jsyro commented 1 year ago
service = {
    "type": "did-communication",
    "serviceEndpoint": "http://host.docker.internal:9060",
    "recipient_keys":[]
}

By providing an empty recipient_keys list, the object is created as a Service but after serde, becomes as DIDCommV1Service object. that is apparently the critical value that was causing it to not being loaded to the right service subclass!

Success!

create_peer_did_numalgo_2
<class 'pydid.service.Service'>
<class 'pydid.service.DIDCommV1Service'>

DIDDocument.make
<class 'pydid.service.DIDCommV1Service'>
<class 'pydid.service.DIDCommV1Service'>

json service string
[{'id': '#did-communication-0', 'type': 'did-communication', 'serviceEndpoint': 'http://host.docker.internal:9060', 'recipient_keys': []}]
json DIDCommService string
[{'id': '#ress', 'type': 'did-communication', 'serviceEndpoint': 'http://host.docker.internal:9060', 'recipientKeys': ['#resv'], 'routingKeys': [], 'priority': 0}]