Source code for fleetmesh_core.services.discover

"""discover-features/2.0 on the wire: a peer asks what this node speaks, and
the answer is the descriptor's c tags said back in DIDComm's shape.

The protocol is DIDComm's (https://didcomm.org/discover-features/2.0),
reused unchanged: `queries` carries [{feature-type, match}], `disclose`
carries [{feature-type, id}]. What fleetmesh fixes is the source of the
answer. The descriptor is the binding declaration, so a disclose that
names a protocol the descriptor does not is a lie this node will not tell
and a peer may report; `fleetmesh_core/peering/peering.Node.disclose` is the pure answer and
this module is the envelope around it.

Only feature-type `protocol` is answered. A query for another type gets an
empty disclosure, which is what DIDComm says a node that does not have the
feature says.
"""
from __future__ import annotations

from typing import Any, Mapping, Optional, TypedDict

PIURI: str = 'https://didcomm.org/discover-features/2.0'
QUERIES: str = PIURI + '/queries'
DISCLOSE: str = PIURI + '/disclose'

# The DIDComm plaintext envelopes this module builds. `from` is a keyword, so
# the shapes use TypedDict's functional form.
QueryItem = TypedDict('QueryItem', {'feature-type': str, 'match': str})
DiscloseItem = TypedDict('DiscloseItem', {'feature-type': str, 'id': str})
QueriesBody = TypedDict('QueriesBody', {'queries': list[QueryItem]})
DiscloseBody = TypedDict('DiscloseBody', {'disclosures': list[DiscloseItem]})

Queries = TypedDict('Queries', {'id': str, 'type': str, 'from': str, 'to': list[str],
                                'body': QueriesBody})
Disclose = TypedDict('Disclose', {'id': str, 'type': str, 'from': str, 'to': list[str], 'thid': str,
                                  'body': DiscloseBody})


[docs]def queries(from_did: str, to_did: str, matches: list[str], msg_id: str) -> Queries: """A queries message asking about the protocols that match each entry; a match ending in * is a prefix, anything else must equal the id.""" return { 'id': msg_id, 'type': QUERIES, 'from': from_did, 'to': [to_did], 'body': {'queries': [{'feature-type': 'protocol', 'match': m} for m in matches]}, }
from ..wire.interfaces import DIDCommMessage
[docs]def is_queries(msg: Queries | DIDCommMessage | Mapping[str, Any]) -> bool: return msg.get('type') == QUERIES and isinstance(msg.get('body'), dict)
[docs]def query_list(msg: Queries | DIDCommMessage | Mapping[str, Any]) -> list[dict[str, str]]: """The queries a message carries, malformed entries dropped. An empty list means `everything`, which is DIDComm's reading of no filter.""" out: list[dict[str, str]] = [] body = msg.get('body') if not isinstance(body, dict): return out for q in body.get('queries', []): if isinstance(q, dict) and isinstance(q.get('match'), str): out.append({'feature-type': str(q.get('feature-type', 'protocol')), 'match': q['match']}) return out
[docs]def disclose(from_did: str, to_did: str, thid: str, disclosures: DiscloseBody | Mapping[str, Any], msg_id: Optional[str] = None) -> Disclose: """The reply: `disclosures` is what fleetmesh_core/peering/peering.Node.disclose returned, threaded to the queries that asked.""" return { 'id': msg_id or ('disclose-' + thid), 'type': DISCLOSE, 'from': from_did, 'to': [to_did], 'thid': thid, 'body': {'disclosures': list(disclosures.get('disclosures', []))}, }
[docs]def disclosed_ids(msg: Disclose | DIDCommMessage | Mapping[str, Any]) -> list[str]: """The protocol identifiers a disclose names, in order.""" body = msg.get('body') if msg.get('type') != DISCLOSE or not isinstance(body, dict): return [] return [d['id'] for d in body.get('disclosures', []) if isinstance(d, dict) and d.get('feature-type', 'protocol') == 'protocol' and isinstance(d.get('id'), str)]