Files
dchain/desktop/src/auth/Welcome.tsx
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

143 lines
4.5 KiB
TypeScript

// Welcome — shown when no key is loaded.
//
// Three options, matching mobile parity:
// * Create — generate a new Ed25519 + X25519 keypair.
// * Import — load node.json file (dialog).
// * Pair — pair with an existing phone/desktop (QR-less, 6-digit code
// + device key, symmetrical with mobile's /auth/pair flow).
//
// v2.2.0-alpha4 wires the first two functionally and stubs Pair with a
// button that routes to a placeholder — the pairing poll loop shared
// with mobile comes in alpha5.
import React, { useState } from 'react';
import { useStore } from '@/lib/store';
import { saveKeyFile } from '@/lib/storage';
import { generateKeyFile } from '@/lib/crypto';
import type { KeyFile } from '@/lib/types';
import { Pair } from './Pair';
export function Welcome(): React.ReactElement {
const setKeyFile = useStore(s => s.setKeyFile);
const [busy, setBusy] = useState(false);
const [err, setErr] = useState<string | null>(null);
const [screen, setScreen] = useState<'welcome' | 'pair'>('welcome');
if (screen === 'pair') return <Pair onBack={() => setScreen('welcome')} />;
const onCreate = async () => {
setBusy(true); setErr(null);
try {
const kf = generateKeyFile();
await saveKeyFile(kf);
setKeyFile(kf);
} catch (e) {
setErr(String(e));
} finally {
setBusy(false);
}
};
const onImport = async () => {
setBusy(true); setErr(null);
try {
const file = await window.dchain.dialog.openFile({
title: 'Select node.json',
filters: [{ name: 'JSON', extensions: ['json'] }],
properties: ['openFile'],
});
if (!file) return;
const contents = await window.dchain.fs.readText(file);
const parsed = JSON.parse(contents) as KeyFile;
if (!parsed.pub_key || !parsed.priv_key) {
throw new Error('file doesn\'t look like a key file');
}
await saveKeyFile(parsed);
setKeyFile(parsed);
} catch (e) {
setErr(e instanceof Error ? e.message : String(e));
} finally {
setBusy(false);
}
};
const onPair = () => {
setErr(null);
setScreen('pair');
};
return (
<div style={{
height: '100%', display: 'flex',
alignItems: 'center', justifyContent: 'center',
padding: 40, background: '#000', color: '#fff',
}}>
<div style={{ maxWidth: 400, width: '100%', textAlign: 'center' }}>
<div style={{
width: 80, height: 80, borderRadius: 22,
background: '#1d9bf0', margin: '0 auto',
display: 'flex', alignItems: 'center', justifyContent: 'center',
fontSize: 36, fontWeight: 800,
}}>
D
</div>
<h1 style={{ fontSize: 30, fontWeight: 800, letterSpacing: -0.5, margin: '16px 0 6px' }}>
DChain
</h1>
<p style={{ color: '#8b8b8b', fontSize: 14, margin: 0, lineHeight: 1.5 }}>
Decentralised messenger + social feed. Your keys stay on this device.
</p>
<div style={{ display: 'flex', flexDirection: 'column', gap: 10, marginTop: 32 }}>
<PrimaryBtn label="Create account" onClick={onCreate} disabled={busy} />
<SecondaryBtn label="Import key file" onClick={onImport} disabled={busy} />
<SecondaryBtn label="Pair with another device" onClick={onPair} disabled={busy} />
</div>
{err && (
<div style={{
marginTop: 20, padding: 10, borderRadius: 10,
background: '#2a1414', color: '#ff9b9b', fontSize: 12,
textAlign: 'left',
}}>
{err}
</div>
)}
</div>
</div>
);
}
function PrimaryBtn({ label, onClick, disabled }: {
label: string; onClick: () => void; disabled?: boolean;
}) {
return (
<button
onClick={onClick}
disabled={disabled}
style={{
height: 46, borderRadius: 999, border: 'none',
background: '#1d9bf0', color: '#fff', fontSize: 14, fontWeight: 700,
cursor: disabled ? 'default' : 'pointer', opacity: disabled ? 0.6 : 1,
}}
>{label}</button>
);
}
function SecondaryBtn({ label, onClick, disabled }: {
label: string; onClick: () => void; disabled?: boolean;
}) {
return (
<button
onClick={onClick}
disabled={disabled}
style={{
height: 46, borderRadius: 999,
background: '#0a0a0a', color: '#fff', fontSize: 14, fontWeight: 700,
border: '1px solid #1f1f1f',
cursor: disabled ? 'default' : 'pointer', opacity: disabled ? 0.6 : 1,
}}
>{label}</button>
);
}