I built File Transfer, a tool that sends files directly between two browsers. One screen shows a 9-digit code (for example 482-915-736), the other side types it in, and the two devices connect peer-to-peer over WebRTC. Files never touch a server.
- Web version (nothing to install): https://rentabextension-oss.github.io/rentab/ft/?utm_source=devto
- Chrome extension (side panel): https://chromewebstore.google.com/detail/lffomfhccijamechladimcfcpijpjnne?utm_source=devto
In this post I want to show the part I found most interesting: how to connect two browsers safely with only a short code, without running a signaling server.
The big picture
To open a WebRTC connection, the two sides first have to exchange an offer and an answer (SDP) somehow. Usually you run your own signaling server for that. Here I use public Nostr relays and public MQTT brokers as a "bulletin board", and everything posted there is encrypted.
Device A (shows code) Public relays (Nostr / MQTT) Device B (types code)
| -- p1: CPace share Ya ----------------> | |
| | <-- p2: Yb + encrypted offer
| -- p3: encrypted answer ---------------> | |
| ======== direct WebRTC (DTLS) connection, files go P2P ======== |
The relays only ever see a few short encrypted messages. File contents never pass through them.
What's inside the 9-digit code
482-915-736 has two parts:
| Part | Role | Leaves the device? |
|---|---|---|
482 (first 3 digits) |
picks a "mailbox" on the relays | yes (not secret) |
915-736 (last 6 digits) |
the CPace password | never |
The first 3 digits are hashed before being used as a topic name:
const topicFor = async (np: string) =>
[...(await sha('p2pft-nameplate|' + np))]
.slice(0, 20)
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
On Nostr, messages are ephemeral events (kind 25050, a range relays don't store) tagged with #t; on MQTT they are published to p2pft/<topic>.
Why a PAKE (CPace) and not "encrypt with the code"
The simplest idea is "derive a key from the 6 digits and encrypt the offer with it". That's dangerous: 6 digits are only a million combinations, so anyone watching the relay could grab the ciphertext and brute-force it offline.
So I use CPace, a PAKE, on ristretto255:
- both sides run Diffie-Hellman with a generator derived from the password
- they only end up with the same key if the passwords match
- an eavesdropper can't test passwords offline from what they saw
If someone types a wrong code, the keys don't match, decryption fails, and the host throws that code away (a bad message). So each code allows exactly one guess. Codes also rotate every 10 minutes.
After connecting, both screens show four words derived from both DTLS fingerprints (for example clock · ferry · hazel · spider). If anything had been swapped in the middle, the words would differ.
Making both sides land on the same relays
Public relays are often down, so the app connects to several from a long list at once. But when each side picked randomly, they sometimes had no relay in common and never met (about 1 in 6 times).
The fix: a shuffle seeded by the topic. Same code → same order → same relays on both sides.
function shuffle<T>(a: T[], seed: string): T[] {
let h = 2166136261 >>> 0; // FNV-1a
for (let i = 0; i < seed.length; i++) h = Math.imul(h ^ seed.charCodeAt(i), 16777619) >>> 0;
const rnd = () => { h ^= h << 13; h >>>= 0; h ^= h >>> 17; h ^= h << 5; h >>>= 0; return h; };
const r = [...a];
for (let i = r.length - 1; i > 0; i--) {
const j = rnd() % (i + 1);
[r[i], r[j]] = [r[j], r[i]];
}
return r;
}
Nostr and MQTT run in parallel, and either one is enough. If no relay gets through, the app falls back to a mode where the two people copy a long code to each other by hand.
Shrinking the SDP
A raw SDP is several KB. What's really needed is the DTLS fingerprint, the ICE ufrag/pwd and a few candidate addresses, so only those are packed into a compact binary form. New candidate types (for example relay candidates from the user's own TURN server) reuse the existing format so older versions stay compatible.
The transfer itself
- files are split into 1 MB blocks, each checked with SHA-256
- the receiver writes to OPFS from a worker, so memory stays flat
- after a drop, the transfer resumes from the first missing block
- the send buffer is paced with
bufferedAmountLowThreshold - on high-latency links one DataChannel stalls, so up to 8 extra
RTCPeerConnections are added, and kept only if they actually help - on-the-fly zstd compression (WebAssembly), with the level re-tuned every 2 seconds to match the line speed
When it doesn't work
There's no TURN server in the design, so some networks can't connect. From anonymous stats (no IP addresses are collected) the three main causes are:
- symmetric NAT on both sides: neither can accept an incoming connection
- Wi-Fi client isolation: same network, but devices aren't allowed to talk to each other
- CGNAT: the ISP shares one public address between many customers
The app runs a quick network check before connecting and explains the cause when it can. Settings also let you add your own TURN server, and when a phone and a computer are next to each other, the app suggests connecting the computer to the phone's hotspot.
Takeaways
- public Nostr / MQTT can work as a bulletin board, so WebRTC can connect without your own signaling server
- if you use a short code, don't turn it into a key directly: use a PAKE (CPace)
- with public relays, pick them deterministically so both sides choose the same ones
The web version works without installing anything. I'd love to hear whether it connects on your network and what speed you get.
Top comments (0)