<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[File Transfer Blog]]></title><description><![CDATA[File Transfer Blog]]></description><link>https://filetransfer-p2p.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 20 Nov 2026 16:23:28 GMT</lastBuildDate><atom:link href="https://filetransfer-p2p.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[WebRTC file transfer with a 9-digit code and no signaling server of my own]]></title><description><![CDATA[How to connect two browsers over WebRTC with a short code: CPace (PAKE) over public Nostr and MQTT relays, no signaling server of your own.]]></description><link>https://filetransfer-p2p.hashnode.dev/webrtc-file-transfer-with-a-9-digit-code-and-no-signaling-server-of-my-own</link><guid isPermaLink="true">https://filetransfer-p2p.hashnode.dev/webrtc-file-transfer-with-a-9-digit-code-and-no-signaling-server-of-my-own</guid><category><![CDATA[WebRTC]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Security]]></category><category><![CDATA[p2p]]></category><dc:creator><![CDATA[File Transfer]]></dc:creator><pubDate>Thu, 19 Nov 2026 11:30:25 GMT</pubDate><content:encoded><![CDATA[<p>I built <strong>File Transfer</strong>, a tool that sends files directly between two browsers. One screen shows a 9-digit code (for example <code>482-915-736</code>), the other side types it in, and the two devices connect peer-to-peer over WebRTC. Files never touch a server.</p>
<ul>
<li>Web version (nothing to install): <a href="https://rentabextension-oss.github.io/rentab/ft/?utm_source=hashnode">https://rentabextension-oss.github.io/rentab/ft/?utm_source=hashnode</a></li>
<li>Chrome extension (side panel): <a href="https://chromewebstore.google.com/detail/lffomfhccijamechladimcfcpijpjnne?utm_source=hashnode">https://chromewebstore.google.com/detail/lffomfhccijamechladimcfcpijpjnne?utm_source=hashnode</a></li>
</ul>
<p>In this post I want to show the part I found most interesting: <strong>how to connect two browsers safely with only a short code, without running a signaling server.</strong></p>
<h2>The big picture</h2>
<p>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 <strong>public Nostr relays and public MQTT brokers</strong> as a "bulletin board", and everything posted there is encrypted.</p>
<pre><code class="language-plaintext">Device A (shows code)        Public relays (Nostr / MQTT)        Device B (types code)
        | -- p1: CPace share Ya ----------------&gt; |                       |
        |                                         | &lt;-- p2: Yb + encrypted offer
        | -- p3: encrypted answer ---------------&gt; |                       |
        | ======== direct WebRTC (DTLS) connection, files go P2P ======== |
</code></pre>
<p>The relays only ever see a few short encrypted messages. File contents never pass through them.</p>
<h2>What's inside the 9-digit code</h2>
<p><code>482-915-736</code> has two parts:</p>
<table>
<thead>
<tr>
<th>Part</th>
<th>Role</th>
<th>Leaves the device?</th>
</tr>
</thead>
<tbody><tr>
<td><code>482</code> (first 3 digits)</td>
<td>picks a "mailbox" on the relays</td>
<td>yes (not secret)</td>
</tr>
<tr>
<td><code>915-736</code> (last 6 digits)</td>
<td>the CPace password</td>
<td>never</td>
</tr>
</tbody></table>
<p>The first 3 digits are hashed before being used as a topic name:</p>
<pre><code class="language-ts">const topicFor = async (np: string) =&gt;
  [...(await sha('p2pft-nameplate|' + np))]
    .slice(0, 20)
    .map((b) =&gt; b.toString(16).padStart(2, '0'))
    .join('');
</code></pre>
<p>On Nostr, messages are ephemeral events (kind 25050, a range relays don't store) tagged with <code>#t</code>; on MQTT they are published to <code>p2pft/&lt;topic&gt;</code>.</p>
<h2>Why a PAKE (CPace) and not "encrypt with the code"</h2>
<p>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.</p>
<p>So I use <strong>CPace</strong>, a PAKE, on ristretto255:</p>
<ul>
<li>both sides run Diffie-Hellman with a generator derived from the password</li>
<li>they only end up with the same key if the passwords match</li>
<li>an eavesdropper can't test passwords offline from what they saw</li>
</ul>
<p>If someone types a wrong code, the keys don't match, decryption fails, and the host throws that code away (a <code>bad</code> message). So <strong>each code allows exactly one guess</strong>. Codes also rotate every 10 minutes.</p>
<p>After connecting, both screens show four words derived from both DTLS fingerprints (for example <code>clock · ferry · hazel · spider</code>). If anything had been swapped in the middle, the words would differ.</p>
<h2>Making both sides land on the same relays</h2>
<p>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).</p>
<p>The fix: <strong>a shuffle seeded by the topic</strong>. Same code → same order → same relays on both sides.</p>
<pre><code class="language-ts">function shuffle&lt;T&gt;(a: T[], seed: string): T[] {
  let h = 2166136261 &gt;&gt;&gt; 0; // FNV-1a
  for (let i = 0; i &lt; seed.length; i++) h = Math.imul(h ^ seed.charCodeAt(i), 16777619) &gt;&gt;&gt; 0;
  const rnd = () =&gt; { h ^= h &lt;&lt; 13; h &gt;&gt;&gt;= 0; h ^= h &gt;&gt;&gt; 17; h ^= h &lt;&lt; 5; h &gt;&gt;&gt;= 0; return h; };
  const r = [...a];
  for (let i = r.length - 1; i &gt; 0; i--) {
    const j = rnd() % (i + 1);
    [r[i], r[j]] = [r[j], r[i]];
  }
  return r;
}
</code></pre>
<p>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.</p>
<h2>Shrinking the SDP</h2>
<p>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.</p>
<h2>The transfer itself</h2>
<ul>
<li>files are split into 1 MB blocks, each checked with SHA-256</li>
<li>the receiver writes to OPFS from a worker, so memory stays flat</li>
<li>after a drop, the transfer resumes from the first missing block</li>
<li>the send buffer is paced with <code>bufferedAmountLowThreshold</code></li>
<li>on high-latency links one DataChannel stalls, so up to 8 extra <code>RTCPeerConnection</code>s are added, and kept only if they actually help</li>
<li>on-the-fly zstd compression (WebAssembly), with the level re-tuned every 2 seconds to match the line speed</li>
</ul>
<h2>When it doesn't work</h2>
<p>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:</p>
<ol>
<li><strong>symmetric NAT on both sides</strong>: neither can accept an incoming connection</li>
<li><strong>Wi-Fi client isolation</strong>: same network, but devices aren't allowed to talk to each other</li>
<li><strong>CGNAT</strong>: the ISP shares one public address between many customers</li>
</ol>
<p>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.</p>
<h2>Takeaways</h2>
<ul>
<li>public Nostr / MQTT can work as a bulletin board, so WebRTC can connect without your own signaling server</li>
<li>if you use a short code, don't turn it into a key directly: use a PAKE (CPace)</li>
<li>with public relays, pick them deterministically so both sides choose the same ones</li>
</ul>
<p>The web version works without installing anything. I'd love to hear whether it connects on your network and what speed you get.</p>
<ul>
<li>Web: <a href="https://rentabextension-oss.github.io/rentab/ft/?utm_source=hashnode">https://rentabextension-oss.github.io/rentab/ft/?utm_source=hashnode</a></li>
<li>Chrome extension: <a href="https://chromewebstore.google.com/detail/lffomfhccijamechladimcfcpijpjnne?utm_source=hashnode">https://chromewebstore.google.com/detail/lffomfhccijamechladimcfcpijpjnne?utm_source=hashnode</a></li>
</ul>
]]></content:encoded></item></channel></rss>