File size: 3,171 Bytes
e61e751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Federation Node β€” NodeRecord Builder
======================================
Contract: C-FED-NODE-001 v0.1.1

Generates the NodeRecord (Β§4), capability advertisement (Β§6),
conformance declaration (Β§12.3), and trust posture (Β§7).
"""
from __future__ import annotations

from datetime import datetime, timezone


# ─── Constants ───

NODE_VERSION = "0.2.0"
CONTRACT_VERSION = "C-FED-NODE-001 v0.1.1"
PROTOCOL_VERSION = "1.0.0"

# Accepted message classes (Phase 1A)
ACCEPTED_MESSAGE_CLASSES = [
    "HANDSHAKE_REQUEST",
    "HANDSHAKE_RESPONSE",
    "THOUGHT",
    "PROPOSAL",
    "ARTIFACT_TRANSFER",
    "HEARTBEAT",
    "REFUSAL",
    "STATUS_UPDATE",
]

MAX_PAYLOAD_BYTES = 5_242_880  # 5 MB
INBOX_QUOTA = 1000


# ─── Builders ───

def build_capabilities() -> dict:
    """Build the capabilities advertisement per C-FED-NODE-001 Β§6."""
    return {
        "protocol_versions": [PROTOCOL_VERSION],
        "accepted_message_classes": ACCEPTED_MESSAGE_CLASSES,
        "accepted_content_types": ["text/markdown", "application/json"],
        "max_payload_bytes": MAX_PAYLOAD_BYTES,
        "supports_witnessing": True,
        "supports_delegation": False,  # Phase 3
        "supports_registry": False,    # Phase 2
        "rate_limits": {
            "envelopes_per_minute": 30,
            "envelopes_per_hour": 500,
        },
        "inbox_quota": INBOX_QUOTA,
    }


def build_conformance() -> dict:
    """Build conformance declaration per C-FED-NODE-001 Β§12.3."""
    return {
        "contract": "C-FED-NODE-001",
        "version": "0.1.1",
        "level": "minimum",
        "extensions": ["witnessing"],
    }


def build_trust_posture(default_action: str, blocked_behavior: str) -> dict:
    """Build the public trust posture per C-FED-NODE-001 Β§4."""
    return {
        "default_action": default_action,
        "accepts_registry_known": True,
        "accepts_delegation": False,  # Phase 3
        "blocked_behavior": blocked_behavior,
        "trust_graph_public": False,
    }


def build_node_record(
    *,
    node_identity,
    alias: str,
    owner: str,
    relay_url: str,
    roles: list[str],
    default_action: str,
    blocked_behavior: str,
    boot_time: str,
) -> dict:
    """Build the full NodeRecord per C-FED-NODE-001 Β§4."""
    return {
        "identity": {
            "glyph_seal": str(node_identity),
            "public_key": None,  # Phase 3: Ed25519
            "alias": alias,
            "owner_display_name": owner or None,
        },
        "endpoints": {
            "relay_url": relay_url or None,
            "registry_url": None,  # Phase 2
            "capabilities_url": f"{relay_url}/capabilities" if relay_url else None,
        },
        "capabilities": build_capabilities(),
        "roles": roles,
        "trust_posture": build_trust_posture(default_action, blocked_behavior),
        "metadata": {
            "node_version": NODE_VERSION,
            "contract_version": CONTRACT_VERSION,
            "protocol_version": PROTOCOL_VERSION,
            "registered_at": boot_time,
            "last_seen": datetime.now(timezone.utc).isoformat(),
        },
    }