aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/transport/quic_server.py
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/transport/quic_server.py')
-rw-r--r--packages/meshbay-node/src/meshbay_node/transport/quic_server.py49
1 files changed, 8 insertions, 41 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/transport/quic_server.py b/packages/meshbay-node/src/meshbay_node/transport/quic_server.py
index 30c7daf..8a32538 100644
--- a/packages/meshbay-node/src/meshbay_node/transport/quic_server.py
+++ b/packages/meshbay-node/src/meshbay_node/transport/quic_server.py
@@ -26,7 +26,6 @@ import subprocess
from pathlib import Path
from typing import Any, Callable
-import blake3
import jwt
import msgpack
from aioquic.asyncio import QuicConnectionProtocol, serve
@@ -47,13 +46,10 @@ from meshbay_common.handshake import (
quic_binding,
verify_proof,
)
-from meshbay_common.crypto import (
- sign_chunk,
- pk_to_b64,
-)
-from meshbay_common.webcrypto import chunk_key_aes as derive_chunk_key, encrypt_chunk_aes as encrypt_chunk
-from meshbay_common.protocol import MNP
+from meshbay_common.crypto import pk_to_b64
+from meshbay_common.protocol import MNP, file_chunk_wire
from meshbay_node.indexer import GroupIndex
+from meshbay_node.transport.wire import index_sync_message
log = logging.getLogger(__name__)
@@ -390,12 +386,7 @@ class _MNPServerProtocol(QuicConnectionProtocol):
def _do_index_sync_sync(self, stream_id: int) -> None:
ctx = self._group_ctx()
- wire = ctx["index"].serialize()
- self._send(stream_id, {
- "type": MNP.INDEX_SYNC,
- "v": MNP_VERSION,
- "index_b64": base64.b64encode(wire).decode(),
- })
+ self._send(stream_id, index_sync_message(ctx["index"], ctx.get("roots")))
def _do_file_request_sync(self, stream_id: int, msg: dict) -> None:
"""Serve file chunk synchronously (blocking I/O — acceptable for test sizes)."""
@@ -414,12 +405,7 @@ class _MNPServerProtocol(QuicConnectionProtocol):
file_hash = bytes.fromhex(entry.id)
chunk_data = _read_and_encrypt(
- self._ctx["sk_node"],
- ctx["gek"],
- file_path,
- chunk_index,
- file_hash,
- )
+ ctx["gek"], file_path, chunk_index, file_hash, entry.id)
self._send(stream_id, chunk_data)
async def _do_stream_segment(self, stream_id: int, msg: dict) -> None:
@@ -520,36 +506,17 @@ class _MNPServerProtocol(QuicConnectionProtocol):
def _read_and_encrypt(
- sk_node: Ed25519PrivateKey,
gek: bytes,
file_path: Path,
chunk_index: int,
file_hash: bytes,
+ file_id: str = "",
) -> dict:
- """Read and encrypt one chunk (blocking — runs in executor)."""
+ """Read one chunk off disk and encrypt it, in the one shape every transport uses."""
with open(file_path, "rb") as f:
f.seek(chunk_index * CHUNK_SIZE)
plaintext = f.read(CHUNK_SIZE)
-
- pt_hash = blake3.blake3(plaintext).digest()
- ckey = derive_chunk_key(gek, file_hash, chunk_index)
- nonce, ct = encrypt_chunk(ckey, plaintext)
- ct_hash = blake3.blake3(ct).digest()
- sig = sign_chunk(sk_node, chunk_index, nonce, ct_hash)
-
- return {
- "type": MNP.FILE_CHUNK,
- "v": MNP_VERSION,
- "chunk_index": chunk_index,
- "plaintext_size": len(plaintext),
- "nonce_b64": base64.b64encode(nonce).decode(),
- "ct_b64": base64.b64encode(ct).decode(),
- "ct_hash_b64": base64.b64encode(ct_hash).decode(),
- "pt_hash_b64": base64.b64encode(pt_hash).decode(),
- "sig_b64": base64.b64encode(sig).decode(),
- "pk_node_b64": pk_to_b64(sk_node.public_key()),
- "file_hash_b64": base64.b64encode(file_hash).decode(),
- }
+ return file_chunk_wire(gek, plaintext, chunk_index, file_hash, file_id)
def _extract_segment(file_path: Path, start_time: float, duration: float) -> bytes | None: