File size: 5,960 Bytes
e61e751
8511813
 
e61e751
 
8511813
e61e751
 
 
 
8511813
e61e751
 
 
 
8511813
e61e751
 
 
 
 
8511813
 
 
e61e751
8511813
 
e61e751
8511813
e61e751
 
 
 
 
 
 
 
 
 
 
 
 
8511813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e61e751
8511813
 
e61e751
8511813
 
 
 
 
e61e751
 
8511813
 
 
 
 
e61e751
8511813
 
 
 
 
e61e751
8511813
e61e751
 
 
 
 
 
 
 
 
 
 
 
8511813
 
 
e61e751
8511813
 
 
 
 
 
 
 
 
 
 
 
 
e61e751
8511813
 
 
4a035f6
8511813
 
4a035f6
8511813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a035f6
8511813
 
 
4a035f6
 
8511813
 
 
e61e751
8511813
 
 
 
e61e751
8511813
e61e751
 
 
8511813
e61e751
8511813
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Federation Node — Envelope Store (Phase 2 Vault)
================================================
Contract: C-FED-NODE-001 v0.1.1

Persistent SQLite envelope storage backed by Cloudflare D1.
"""
from __future__ import annotations

import uuid
import json
from datetime import datetime, timezone
from typing import Optional

from models import DeliveryStatus
import d1_client

class EnvelopeStore:
    def __init__(self, max_envelopes: int = 1000):
        self._max = max_envelopes

    async def get_count(self) -> int:
        rs = await d1_client.execute_sql("SELECT COUNT(*) as c FROM envelopes")
        return rs[0]["c"] if rs else 0

    async def is_full(self) -> bool:
        return await self.get_count() >= self._max

    async def accept(
        self,
        *,
        sender_seal: str,
        sender_origin: str,
        message_class: str,
        payload: dict,
        delivery: dict,
        trust_tier: str,
        initial_status: DeliveryStatus = DeliveryStatus.QUEUED,
    ) -> dict:
        now = datetime.now(timezone.utc).isoformat()
        envelope_id = str(uuid.uuid4())

        await d1_client.execute_sql(
            '''INSERT INTO envelopes 
               (envelope_id, protocol_version, message_class, sender_seal, recipient_seal, payload_body, status, received_at)
               VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
            [
                envelope_id, 
                "1.0.0", 
                message_class, 
                sender_seal, 
                "NODE", 
                json.dumps(payload), 
                initial_status.value, 
                now
            ]
        )

        history = [
            {"status": DeliveryStatus.RECEIVED.value, "at": now},
            {"status": DeliveryStatus.VALIDATED.value, "at": now},
        ]
        
        await d1_client.execute_sql(
            "INSERT INTO status_history (envelope_id, status, transitioned_at) VALUES (?, ?, ?), (?, ?, ?)",
            [envelope_id, DeliveryStatus.RECEIVED.value, now, envelope_id, DeliveryStatus.VALIDATED.value, now]
        )

        if initial_status == DeliveryStatus.QUARANTINED:
            history.append({"status": DeliveryStatus.QUARANTINED.value, "at": now})
            await d1_client.execute_sql(
                "INSERT INTO status_history (envelope_id, status, transitioned_at) VALUES (?, ?, ?)",
                [envelope_id, DeliveryStatus.QUARANTINED.value, now]
            )
        else:
            history.append({"status": DeliveryStatus.QUEUED.value, "at": now})
            await d1_client.execute_sql(
                "INSERT INTO status_history (envelope_id, status, transitioned_at) VALUES (?, ?, ?)",
                [envelope_id, DeliveryStatus.QUEUED.value, now]
            )

        return {
            "envelope_id": envelope_id,
            "status": initial_status,
            "sender_seal": sender_seal,
            "sender_origin": sender_origin,
            "message_class": message_class,
            "payload": payload,
            "delivery": delivery,
            "trust_tier": trust_tier,
            "received_at": now,
            "status_history": history,
        }

    async def get_envelope(self, envelope_id: str) -> Optional[dict]:
        rs = await d1_client.execute_sql("SELECT * FROM envelopes WHERE envelope_id = ?", [envelope_id])
        if not rs:
            return None
        record = rs[0]
        try:
            payload = json.loads(record["payload_body"])
        except:
            payload = {"body": record["payload_body"]}
        return {
            "envelope_id": record["envelope_id"],
            "status": record["status"],
            "sender_seal": record["sender_seal"],
            "message_class": record["message_class"],
            "received_at": record["received_at"],
            "payload": payload,
        }

    async def list_envelopes(self, limit: int = 20, status_filter: Optional[str] = None) -> dict:
        total_rs = await d1_client.execute_sql("SELECT COUNT(*) as c FROM envelopes")
        total = total_rs[0]["c"] if total_rs else 0

        query = "SELECT * FROM envelopes"
        params = []
        if status_filter:
            query += " WHERE status = ?"
            params.append(status_filter)
        query += " ORDER BY received_at DESC LIMIT ?"
        params.append(limit)

        rs = await d1_client.execute_sql(query, params)
        envelopes = []
        for r in rs:
            try:
                payload = json.loads(r["payload_body"])
                preview = str(payload.get("body", ""))[:120]
            except:
                preview = str(r["payload_body"])[:120]
                
            envelopes.append({
                "envelope_id": r["envelope_id"],
                "status": r["status"],
                "sender_origin": "Federation", 
                "sender_seal": r["sender_seal"],
                "message_class": r["message_class"],
                "received_at": r["received_at"],
                "payload_preview": preview,
            })
            
        return {
            "total": total,
            "showing": len(envelopes),
            "envelopes": envelopes
        }

    async def get_status(self, envelope_id: str) -> Optional[dict]:
        rs = await d1_client.execute_sql("SELECT * FROM envelopes WHERE envelope_id = ?", [envelope_id])
        if not rs:
            return None
        record = rs[0]
        
        hist_rs = await d1_client.execute_sql("SELECT status, transitioned_at as at FROM status_history WHERE envelope_id = ? ORDER BY id ASC", [envelope_id])
        
        return {
            "envelope_id": record["envelope_id"],
            "status": record["status"],
            "sender_seal": record["sender_seal"],
            "message_class": record["message_class"],
            "trust_tier": "UNKNOWN", 
            "received_at": record["received_at"],
            "status_history": hist_rs,
        }