Repository navigation
Conversation
…nst a real relay's note
…ral Nostr relays A publish counts when min(2, relays) relays answer OK true, a read when at least one relay ends with EOSE; every event read is checked and duplicates across relays are dropped. Relays are asked at once, each with a connect and an answer timeout, so a dead or silent relay costs one timeout, not one per relay. TLS is our rustls with the ring provider passed explicitly and the platform verifier; frames are capped at 1 MiB. tokio-tungstenite 0.28 rather than 0.30: 0.30 brings a second RustCrypto generation (digest 0.11, sha1 0.11) and rand 0.10 into the tree, 0.28 adds four lockfile entries. Tests run against a fake NIP-01 relay on 127.0.0.1 that can accept, refuse, stay silent or serve forged events.
…nly so much Every event a relay sends is now checked against the read's own filter (kinds, authors, ids, since, until, one-letter tags) and its signature as it arrives, so a relay cannot slip in events of another kind, author or tag. A frame is at most 128 KiB and one relay's answer to one read at most 4 MiB; past that the rest is not read. Before, a count of 1,000 events of up to 1 MiB each bounded nothing useful. Found by an automated security review of the previous commit.
Offers are addressable Nostr events (kind 37491) and trade messages NIP-44-encrypted regular events (kind 7491), both signed by the relay key that signs their content; the content is the SignedOffer or Envelope a namehold-relay received before. Readers take an offer only from its maker's key and a message only from its sender's, so a copied offer cannot be listed, renewed or hidden by anyone else. A withdrawal replaces the offer and asks relays to delete it; it hides the offer on relays that ignore deletions too. The job and the commands now reach the meeting point through one trait, MeetingPoint, whose writes carry the signing key; the watchtower is its own trait. The meeting point is a custom relay when one is set, otherwise the Nostr relays of the network's settings, which default on mainnet and testnet to six public relays that the live check found taking and serving both kinds. Exchange settings get a Nostr relays field (wss:// only, no logins, confirmed in the secure window) and the custom relay becomes optional.
… move the inbox cursor The inbox reads back 900 s before its cursor, so the same events came again on the next steps, and the job handled a message once per read: a declined take, for one, spent another pooled hash each time. The stored cursor now carries the ids of the events in the window the next read covers (nostr:<time>:<id>,…, at most 500), so each is handed over once, after a restart too. The cursor's time now moves only with a message that was handed over: before, any event addressed to the key by its p tag, which anyone can publish with any date, moved it forward and could push a late real message out of the window. Both found by an automated security review of the transport commit.
…AY.md, SECURITY.md, the changelog and QA
… ids out of its cursor The cursor kept the ids of every event in the window the next read covers, newest first, at most 500. Anyone can address events to a key, dated up to 15 minutes ahead, so 500 of them evicted the ids of real messages already handed over, and those were handed over again on a later read. The cursor now keeps only the ids of messages handed over, this read or before, ranked by a time no later than now; junk never enters it. Found by an automated security review of the previous commit. A test also checks that a relay dropping the connection mid-answer ends the call with that relay counted as failed.
…ne the job already announced in this process
…en behind a generic error The client passed on the socket's own error, "IO error: Connection refused (os error 61)", and the page's error mapping turns any message containing "connection refused" into "Couldn't reach the configured endpoint". A publish refused because a Nostr relay was down therefore lost its whole explanation: how many relays took the event and which did not. The client now says "refused the connection (is it running?)"; a test of the mapping checks the count and each relay reach the page.
… Nostr relays included The publication window showed the custom relay's URL under "Relay"; with the Nostr relays as the meeting point that URL is empty, so the user confirmed a publication without seeing where it went. The row is now "Published on": the custom relay's URL, or "Nostr relays" and their list.
scripts/nostr-relay.sh starts one to three strfry relays in Docker on 127.0.0.1:7777 and up (image pinned by digest, data in the git-ignored .regtest/nostr), with the image's config changed to listen inside the container, which its default of 127.0.0.1 does not allow through a published port. scripts/nostr-req.mjs reads events from a relay with one REQ and prints them; it never sends an event. Checked on 2026-11-12: three relays started and live_exchange_nostr against them took, served, replaced and deleted offers and took a burst of 20 writes on each.
…r-relay procedure A new scenario file covers what the Nostr transport changes: the Nostr relays in Exchange settings and their secure window (E41), an offer's events from publication through renewal to withdrawal (E42), encrypted and addressed trade messages (E43), each message handled once across a restart (E44), relays going down and the two-relay quorum (E45), a custom relay in place of Nostr (E46), a whole trade over Nostr (E40, moved from trades.md), and the check of the public default relays before a release (E47, only with the maintainer's agreement). The procedure nostr-relay starts the local relays and shows how to tie an offer in the run's database to its events. The index, run order and stand requirements name them; E10 expects the publication window's new "Published on" row.
Running E41–E45 on the stand found three mistakes in the cases themselves. E43's check for readable message bodies counted the "regtest" in each event's own tags (2 hits for 0 readable bodies); it now reads only the content field. E45 expected the first offer to stay Live with two relays down; its next heartbeat needs two relays as well, so both offers pause, with the quorum error stored. The database helpers read with -readonly: an immutable=1 read misses the WAL and showed a stale setting.
The exchange step read the inboxes and renewed the offers before it stepped the trades, so every claim and refund waited for the meeting point. Over Nostr each call waits for the slowest relay, up to 25 seconds when one of the six defaults never answers, once per live offer and once per open trade: with a few offers and trades the claims of that profile, and the steps of the profiles after it, came minutes late, and the step decided with the time taken before all that waiting. The step now runs the trades first, then the meeting point, and steps again only when the meeting point handed messages over, so terms and lock announcements still move their trade in the same tick. The meeting point gets at most 90 seconds a tick (the call under way finishes); the inboxes are read in turn, starting each minute further along, so a slow relay cannot leave one trade's inbox unread for ever.
…hanged nothing Every step of the background sync rewrote every open trade, and every write moves the trade's version. The Commit stores only if the trade is still the version it read before the secure window opened, so a window open across a step (every 60 s in the daemon, and on the app's own sync) was refused with "nothing was sent: this trade was confirmed already, or changed while the window was open". Found on the regtest stand: the Buyer's Commit failed on three tries in a row while the trade's version moved twice a minute with nothing new on either chain. A step now writes a trade only when it changed its status, holdings, error, end time or finished flag. A Commit window open across a real change is still refused, as before.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The exchange needs a place where offers are listed and where the two sides of a trade talk. Until now that was a
namehold-relaysomeone had to run, and nobody runs a public one, so a fresh install could not trade. With this change offers and trade messages go through public Nostr relays that independent people run: the exchange works out of the box and the project operates no server for it. Anamehold-relaysomeone does run can still be entered in Exchange settings and is used instead; the watchtower stays anamehold-relayfeature. Spec:docs/specs/2026-11-12-exchange-nostr-transport.md.How it works
Offers are addressable events (kind 37491,
d= offer id) whose content is the existingSignedOffer; each heartbeat publishes a new version with a 5-minute NIP-40 expiration. A withdrawal replaces the offer and asks relays to delete it (NIP-09).Trade messages are regular events (kind 7491) whose content is the existing
Envelope, encrypted to the recipient with NIP-44 v2.Both are signed by the relay key that signs their content, and readers take an offer only from its maker's key and a message only from its sender's. A relay or a third party can fail to carry them, never forge, renew or withdraw someone else's.
The inbox reads back 15 minutes before its cursor, and hands each message to the trade step once: the stored cursor carries the ids of messages handed over, never of junk, so a re-read or a restart does not repeat them.
The client is a small NIP-01 implementation over
tokio-tungstenite0.28 with our rustls (ring) and the platform verifier. A publish counts when two relays take the event; a read when one answers. Every event read is checked against the read's own filter and its signature; a frame is at most 128 KiB, one relay's answer at most 4 MiB.Settings: each network has a list of Nostr relays (
wss://only, no logins, confirmed in the secure window). The default on mainnet and testnet is six public relays; regtest and simnet have none.The job and the commands reach the meeting point through one trait,
MeetingPoint, whose writes carry the signing key; the watchtower is its own trait.The offer's secure window names where it is published ("Published on": the custom relay, or "Nostr relays" and their list); before, over Nostr, it showed an empty relay.
A relay that refused the connection is named in the refusal ("refused the connection (is it running?)"): the page's error mapping turned "Connection refused" into a generic sentence that hid how many relays took the event.
Claims and refunds no longer wait on the meeting point. The exchange step read the inboxes and renewed the offers before it stepped the trades; over Nostr each call waits for the slowest relay (up to 25 s when one of the six defaults never answers), so a profile's claims could come minutes late. The step now runs the trades first, then the meeting point within 90 s a tick (the inboxes in turn), and steps again only when messages arrived.
A trade's Commit is no longer refused by a sync that changed nothing. Every step rewrote every open trade, moving its version, and the Commit stores only if the version did not move while its secure window was open, so a window open across a sync was refused with "nothing was sent: … changed while the window was open". This came with the exchange (feat(exchange): trade HNS and BTC by atomic swap (beta) #161); a step now writes a trade only when it changed something.
QA
New cases in
docs/qa/scenarios/exchange/nostr.md, written so another agent can run them on the regtest stand: the Nostr relays in settings and their secure window (E41), an offer's events from publication to withdrawal (E42), encrypted and addressed trade messages (E43), each message handled once across a restart (E44), relays going down and the quorum (E45), a custom relay in place of Nostr (E46), a whole trade over Nostr (E40), and the public defaults before a release (E47, only with the maintainer's agreement). The proceduredocs/qa/procedures/nostr-relay.mdstarts local relays withscripts/nostr-relay.sh(strfry in Docker, pinned by digest) and reads them withscripts/nostr-req.mjs(read-only). The stand was checked: three local relays took, served, replaced and deleted offers and took a burst of writes. The cases were then run in the app on the regtest stand (build 76721eb), with three local relays:You receiveHNS at its address; the Seller locked exactly 5 HNS and its BTC balance grew by the 58460 sats its window showed. The relay's log took no offer and no message during the trade, only the two watchtower jobs.The first run found no bug in the app; a recheck of the money paths (below) found the Commit refusal fixed above. It corrected three mistakes in the cases themselves (a check that matched the
regtesttag, a wrong expectation about the first offer under a 2-relay outage, and database reads that missed the WAL).A recheck on the build with both fixes, over the local Nostr relays: E20 ✅ the Seller never confirms; no BTC refund while the Bitcoin median time is 10 minutes short of the refund time, then the refund 22 s after it passes, to the address the Buyer's window showed, and a second such refund 27 s after its time. E18 ✅ with the app closed the daemon read the Buyer's announcement, then sent both claims after the Seller's Commit (the same secret, the amounts and addresses of the windows), and both trades completed.
wss://from the daemon ✅ withwss://relay.primal.netas the only relay and the app closed, the daemon published the offer over TLS within a minute. Every Commit on this build stored on its first press. Ten full test runs under a watchdog: none hung.Checked
cargo test --lib: 4402 passed (earlier, 4401 four runs in a row); the exchange tests passed 25 runs in a row. Frontend: 2123 tests;pnpm build, clippy, fmt,cargo auditand prettier are clean.live_exchange_nostr(gated byHNS_IT_NOSTR=1) published short-lived test events from throwaway keys to candidate relays. The defaults are the six that took and served both kinds, replaced and listed offers, and took a burst of writes: relay.primal.net, offchain.pub, nostr.bitcoiner.social, relay.nostr.net, nostr.oxtr.dev, relay.mostro.network. relay.damus.io (HTTP 503), nos.lol and nostr.mom (no answer to these kinds) failed on two runs each and are not defaults.Not checked
live_exchange_e2e); they need the local regtest chain rolled back before they run again.