Skip to content

feat(chat): add Shroud Chat with instant payment notices over Marmot - #233

Open
theanmolsharma wants to merge 18 commits into
masterfrom
theanmolsharma/marmot-protocol-implementation-research
Open

theanmolsharma wants to merge 18 commits into
masterfrom
theanmolsharma/marmot-protocol-implementation-research

Conversation

@theanmolsharma

Copy link
Copy Markdown
Collaborator

Summary

Shroud Chat: Google Pay-style messaging between contacts, with payments showing up inside the conversation. 1:1 end-to-end encrypted text over Marmot (MLS over Nostr) via marmot-ts, plus an encrypted payment notice that lets the receiver find an incoming silent payment in the mempool within seconds instead of waiting for a block and an indexer scan.

  • Chat. Your SP address is your chat address: the Nostr pubkey is x-only(B_scan + TaggedHash("Shroud/NostrIdentity", B_scan)·G), so every saved contact maps to a chat pubkey with no lookup server. A 1:1 chat is a two-member Marmot group; invites from contacts are joined automatically, anything else waits under Message requests.
  • Payment notice. After a broadcast, Confirm sends each payee on chat the txid, the BIP-352 scan tweak, the raw tx and the note. The receiver verifies the hex by hash (or fetches from Electrum), scans with the sender's tweak, stores the output as a pending coin through the existing de-duplicating addUTXO, and replies with a receipt. The amount shown is always what the receiver's own scan matched.
  • Cards. Sender: Sent → Received by Bob ✓ → Confirmed. Receiver: Verifying… → Received · pending → Confirmed (or Waiting for confirmation…).
  • Payment requests. "Request 21,000 sats"; the payer's Pay button opens send prefilled, after checking the request's address belongs to the sender.
  • UI. Message button beside Pay on the contact screen and a message icon on home contact cards, on-Shroud badge / preview / unread in the contact list, Message requests, a Shroud Chat settings screen (toggle, accepted costs, relay list).

What's in the diff

  • helpers/chat/: nobleSubtle.ts (crypto.subtle shim for Hermes), identity.ts, events.ts, storage.ts (tagged JSON + AES-256-GCM, keys from the chat secret), network.ts, ChatService.ts, PaymentTracker.ts, ChatProvider.tsx, plus an in-memory relay and a DevMenu "Chat self-test".
  • hd-bip352-wallet.ts: computeScanTweak (extracted from the post-broadcast change scan, which now shares it) and detectUnconfirmedPayment.
  • Send flow: Confirm computes the tweak before broadcasting and notifies payees fire-and-forget; SendDetails prefills amount/note from a request and passes the pre-substitution payee list.
  • Electrum.onNewBlock, SettingsProvider chat toggle, device wipe clears chat storage.
  • Dependencies added (exact marmot-ts@0.9.0, applesauce-*, nostr-tools, @noble/ciphers, @noble/curves, @scure/base). package.json scripts are untouched, so the OTA fingerprint is unaffected; no native changes.

Verification

  • npm run tslint and npm run lint pass (0 errors, same 113 warnings as master). npm run unit: 782 pass, 1 fails.
  • The one failure, sendDetails.test.tsx › fills in a payment request opened from home, fails identically on a clean bc5ac6f90 and is unrelated.
  • New tests: identity vector (both directions), shim vs Node WebCrypto for HMAC/HKDF/AES-GCM/X25519/Ed25519, storage, event validation, two ChatServices on an in-memory relay (invite → chat → notice → receipt, duplicate-group race, restart), PaymentTracker, sender tweak and receiver detection (Rust scanner stood in by scanOutputsWithTweak), request-address verification.
  • On devices (iOS simulator + Android emulator, dev builds, real public relays, signet): chat both ways with history surviving relaunches; a real 10,000-sat payment showed Pending on the receiver ~3 s after broadcast (before any block), "Received by Peer ✓" on the sender, and both flipped to Confirmed after the next block with no duplicate UTXO. The payment was sent from a temporary harness that mirrors Confirm's logic, not by tapping through the Confirm screen; the harness is not in the PR.
  • The self-test passes on both platforms (1.3 s iOS, 1.6 s Android, dev Hermes). Release timings are unmeasured.

Notes for review

  • Accepted costs (also stated in the settings screen): the SP address and npub are publicly linkable; the chat key is exactly as sensitive as the scan key; the identity can't change without a new address. testnet4 and signet derive the same keys, so they share a chat identity and storage namespace.
  • Chat does not stop when the app re-locks (AppLock is only an auth gate; the seed stays in memory). It stops on wallet, decoy-bucket or network switch, and when Tor-only is on. It resumes on foreground to re-subscribe and backfill.
  • Default relays are the three public ones (damus, nos.lol, primal); no demo relay is configured (setChatRelays adds one).
  • First message to a new peer took 5–7 s in a dev build (group create + invite); later ones ~1 s.
  • Known gaps: an unconfirmed UTXO from a notice that never confirms (replaced/dropped) stays as a pending coin, same as broadcast placeholders today. Deleting just the wallet leaves its chat data encrypted in AsyncStorage; a device wipe clears it. Someone accepted from Message requests has no contact address, so that conversation is reachable only from that screen.

🤖 Generated with Claude Code

theanmolsharma and others added 18 commits November 5, 2026 19:18
marmot-ts needs WebCrypto, which Hermes lacks. Install a lazy subtle backed by
@noble (digest, HMAC, HKDF, AES-GCM, X25519, Ed25519) only when the runtime has
none, and add the chat dependencies plus the jest ESM allowlist.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Runs key package, group, invite, join, chat, notice and receipt between two
in-memory clients. Exposed as a "Chat self-test" DevMenu action and exercised in
jest with the native subtle removed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The silent payment address is the chat address: pubkey = x-only(B_scan + t·G)
with t a tagged hash of B_scan, so any saved contact maps to a chat pubkey with
no lookup server. Includes the cross-checked test vector.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Chat text, payment notice, receipt and request rumors with strict schema
validation: malformed or foreign events parse to null instead of throwing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
AsyncStorage-backed marmot stores with tagged JSON for Uint8Array and bigint,
AES-256-GCM per value keyed from the chat secret, and a persisted client id.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
NostrNetworkInterface over nostr-tools SimplePool with NIP-42 auth answered by
the chat identity, and one shared default relay list.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
One MarmotClient per wallet: publishes key packages and relay lists, creates
and joins 1:1 groups, auto-accepts invites from contacts, holds the rest as
message requests, converges duplicate groups on the lower pubkey, and sends
text, payment notices, receipts and requests. Integration-tested on an
in-memory relay.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Starts the service for the active wallet once storage is unlocked, restarts it
when the wallet, bucket or network changes, and stays off while chat is
disabled or Tor-only is on.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Inverted thread with text bubbles and payment cards, composer with Pay, and a
message requests list for invites from people who are not contacts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Message button beside Pay, an on-Shroud badge with preview and unread count in
the contact list, a message requests entry, and a Shroud Chat settings screen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Extracts the BIP-352 input maths into computeScanTweak, shared with the
post-broadcast change scan, and adds detectUnconfirmedPayment: it verifies the
notice's hex by hash (or fetches from Electrum), scans with the sender's tweak,
and stores matches as pending coins through the de-duplicating addUTXO.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Receiver checks a notice at once, goes Pending and sends a detected receipt,
retries per block while nothing matches, and confirms on the first
confirmation. Sender follows its own transaction. Both resume after a restart.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Confirm computes the scan tweak before broadcasting, then tells each silent
payment payee who is on Shroud chat, fire and forget. Success shows when it went out.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
useSendToAddress takes an amount, note and chat request id; SendDetails fills
them in and Confirm passes the request id on so the payment notice answers it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Request button asks for an amount and note. The payer's Pay button opens send
prefilled, but only after checking that the request's address belongs to the
sender (pubkeyFromAddress(address) === rumor.pubkey); a paid request shows Paid.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Until the receiver's own scan matches, the sender's amount is shown muted and
never as received. Also tidies lint in the tracker, wallet and request sheet.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Chat keeps encrypted records in AsyncStorage beside the wallet's; a forgot-PIN
wipe now removes them too.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Opens the conversation directly, with an unread count on the icon. Hidden while
chat is off or unavailable.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant