Files
dchain/client-app/hooks/useMessages.ts
vsecoder ce11a13874 feat: desktop messaging + pairing + cross-client master-pub attribution (v2.2.0-alpha5)
Two coordinated changes:

1. Desktop client gets a functional Messages section and working pairing
flow, putting it at feature parity with mobile for the v2.2.0 line.

2. Server + both clients teach each other to use the sender's master
Ed25519 (not just their X25519) to address conversations, so a peer
writing from a different linked device still rolls into the same chat.
This is the "new API logic" the desktop scaffold was waiting on.

Server (node/api_relay.go, cmd/node/main.go):
  * /relay/inbox items now carry `sender_ed25519_pub` alongside the
    per-device `sender_pub`. Empty string for pre-v2.2.0 senders.
  * WS `inbox` push summary also includes `sender_ed25519_pub`, so the
    client can skip the refetch when the envelope plainly isn't for
    the chat they're watching.
  * Both existing tests pass.

Mobile client:
  * lib/types.ts Envelope grew `sender_ed25519_pub`; fetchInbox normalises
    it (default '') for older nodes.
  * hooks/useGlobalInbox matches contacts by (master Ed25519 OR legacy
    X25519) so an incoming message from a peer's desktop reuses the
    existing chat instead of creating a duplicate placeholder.
  * hooks/useMessages now takes an optional `contactMasterEd25519` and
    exposes a matchesChat() predicate; WS inbox handler uses it too to
    avoid spurious refetches.
  * chats/[id].tsx passes `contact.address` (master) along with x25519.

Desktop client — all new:
  * src/lib/crypto.ts — tweetnacl hex/base64 helpers, generateKeyFile,
    encryptMessage/decryptMessage, signBase64, shortAddr. Same signatures
    as the mobile lib; uses Chromium's window.crypto, no expo-crypto dep.
  * src/lib/tx.ts — buildTransferTx / buildLinkDeviceTx / buildUnlinkDeviceTx
    + submitTx + humanizeTxError, canonical-bytes identical to mobile.
  * src/lib/relay.ts — fetchInbox, sendEnvelope, resolveRecipientKeys
    (multi-device fan-out with legacy identity.x25519 fallback).
  * src/lib/store.ts — zustand state gets messages{}, unread{},
    activeChat.
  * src/lib/storage.ts — per-chat cache via localStorage (500-msg cap).
  * src/hooks/useInboxPoll — 4s polling loop, addresses conversations
    by master Ed25519, bumps unread unless chat is active.
  * src/sections/messages/* — ChatList (sorted tiles, unread badges),
    Conversation (auto-scroll messages + composer + fan-out send,
    Enter-to-send / Shift+Enter for newline), EmptyConversation.
  * src/auth/Pair.tsx — 6-digit code + device key screen, polls inbox
    for a handshake envelope, assembles the KeyFile on arrival.
  * Welcome.tsx: Pair button now actually routes to <Pair>; imports
    generateKeyFile from lib/crypto (was inlined).

docs/ROADMAP.md delta: alpha5 row flipped to done inline. Alpha6
(feed + wallet) and rc1 (contacts + devices UI + profile) still
pending.
2026-04-22 17:43:18 +03:00

183 lines
7.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Subscribe to the relay inbox via WebSocket and decrypt incoming envelopes
* for the active chat. Falls back to 30-second polling whenever the WS is
* not connected — preserves correctness on older nodes or flaky networks.
*
* Flow:
* 1. On mount: one HTTP fetch so we have whatever is already in the inbox
* 2. Subscribe to topic `inbox:<my_x25519>` — the node pushes a summary
* for each fresh envelope as soon as mailbox.Store() succeeds
* 3. On each push, pull the full envelope list (cheap — bounded by
* MailboxPerRecipientCap) and decrypt anything we haven't seen yet
* 4. If WS disconnects for > 15 seconds, start a 30 s HTTP poll until it
* reconnects
*/
import { useEffect, useCallback, useRef } from 'react';
import { fetchInbox } from '@/lib/api';
import { getWSClient } from '@/lib/ws';
import { decryptMessage } from '@/lib/crypto';
import { appendMessage, loadMessages } from '@/lib/storage';
import { useStore } from '@/lib/store';
import { tryParsePostRef } from '@/lib/forwardPost';
const FALLBACK_POLL_INTERVAL = 30_000; // HTTP poll when WS is down
const WS_GRACE_BEFORE_POLLING = 15_000; // don't start polling immediately on disconnect
/**
* useMessages — mounts per-chat inbox consumption. Accepts:
* - contactX25519: the legacy/primary X25519 for the contact.
* - contactMasterEd25519 (optional, v2.2.0+): the contact's master
* identity so we can attribute envelopes from any of their
* linked devices to this conversation.
*
* Matching rule: an envelope belongs to this chat when
* env.sender_ed25519_pub === contactMasterEd25519 (v2.2.0 path)
* OR env.sender_pub === contactX25519 (legacy path)
*/
export function useMessages(contactX25519: string, contactMasterEd25519?: string) {
const keyFile = useStore(s => s.keyFile);
const appendMsg = useStore(s => s.appendMessage);
const matchesChat = useCallback((env: { sender_pub: string; sender_ed25519_pub: string }): boolean => {
if (contactMasterEd25519 && env.sender_ed25519_pub === contactMasterEd25519) return true;
return env.sender_pub === contactX25519;
}, [contactX25519, contactMasterEd25519]);
// Подгружаем кэш сообщений из AsyncStorage при открытии чата.
// Релей держит envelope'ы всего 7 дней, поэтому без чтения кэша
// история старше недели пропадает при каждом рестарте приложения.
// appendMsg в store идемпотентен по id, поэтому безопасно гонять его
// для каждого кэшированного сообщения.
useEffect(() => {
if (!contactX25519) return;
let cancelled = false;
loadMessages(contactX25519).then(cached => {
if (cancelled) return;
for (const m of cached) appendMsg(contactX25519, m);
}).catch(() => { /* cache miss / JSON error — not fatal */ });
return () => { cancelled = true; };
}, [contactX25519, appendMsg]);
const pullAndDecrypt = useCallback(async () => {
if (!keyFile || !contactX25519) return;
try {
const envelopes = await fetchInbox(keyFile.x25519_pub);
for (const env of envelopes) {
// Only process messages that belong to this chat (see matchesChat).
if (!matchesChat(env)) continue;
const text = decryptMessage(
env.ciphertext,
env.nonce,
env.sender_pub,
keyFile.x25519_priv,
);
if (!text) continue;
// Detect forwarded feed posts — plaintext is a tiny JSON
// envelope (see lib/forwardPost.ts). Regular text messages
// stay as-is.
const postRef = tryParsePostRef(text);
const msg = {
id: env.id || `${env.sender_pub}_${env.nonce.slice(0, 16)}`,
from: env.sender_pub,
text: postRef ? '' : text,
timestamp: env.timestamp,
mine: false,
...(postRef && {
postRef: {
postID: postRef.post_id,
author: postRef.author,
excerpt: postRef.excerpt,
hasImage: postRef.has_image,
},
}),
};
appendMsg(contactX25519, msg);
await appendMessage(contactX25519, msg);
}
} catch (e: any) {
// Шумные ошибки (404 = нет mailbox'а, Network request failed =
// нода недоступна) — ожидаемы в dev-среде и при offline-режиме,
// не спамим console. Остальное — логируем.
const msg = String(e?.message ?? e ?? '');
if (/→\s*404\b/.test(msg)) return;
if (/ 404\b/.test(msg)) return;
if (/Network request failed/i.test(msg)) return;
if (/Failed to fetch/i.test(msg)) return;
console.warn('[useMessages] pull error:', e);
}
}, [keyFile, contactX25519, appendMsg]);
// ── Fallback polling state ────────────────────────────────────────────
const pollTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
const disconnectTORef = useRef<ReturnType<typeof setTimeout> | null>(null);
const startPolling = useCallback(() => {
if (pollTimerRef.current) return;
console.log('[useMessages] WS down — starting HTTP poll fallback');
pullAndDecrypt();
pollTimerRef.current = setInterval(pullAndDecrypt, FALLBACK_POLL_INTERVAL);
}, [pullAndDecrypt]);
const stopPolling = useCallback(() => {
if (pollTimerRef.current) {
clearInterval(pollTimerRef.current);
pollTimerRef.current = null;
}
if (disconnectTORef.current) {
clearTimeout(disconnectTORef.current);
disconnectTORef.current = null;
}
}, []);
useEffect(() => {
if (!keyFile || !contactX25519) return;
const ws = getWSClient();
// Initial fetch — populate whatever landed before we mounted.
pullAndDecrypt();
// Subscribe to our x25519 inbox — the node emits on mailbox.Store.
// Topic filter: only envelopes for ME; we then filter by sender inside
// the handler so we only render messages in THIS chat.
const offInbox = ws.subscribe('inbox:' + keyFile.x25519_pub, (frame) => {
if (frame.event !== 'inbox') return;
const d = frame.data as {
sender_pub?: string; sender_ed25519_pub?: string;
} | undefined;
// Optimisation: if the envelope definitely isn't for this chat,
// skip the whole refetch. Multi-device aware — the peer may be
// writing from any of their linked devices (different X25519
// pubs), so we check against their master Ed25519 too.
if (d && !matchesChat({
sender_pub: d.sender_pub ?? '',
sender_ed25519_pub: d.sender_ed25519_pub ?? '',
})) return;
pullAndDecrypt();
});
// Manage fallback polling based on WS connection state.
const offConn = ws.onConnectionChange((ok) => {
if (ok) {
stopPolling();
// Catch up anything we missed while disconnected.
pullAndDecrypt();
} else if (disconnectTORef.current === null) {
disconnectTORef.current = setTimeout(startPolling, WS_GRACE_BEFORE_POLLING);
}
});
ws.connect();
return () => {
offInbox();
offConn();
stopPolling();
};
}, [keyFile, contactX25519, pullAndDecrypt, startPolling, stopPolling]);
}