From 4b222a2043ff2abc0edffe553700d5f1fe9519f7 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 15 Nov 2026 21:30:02 -0700 Subject: BIP-353 records move to our own DNS: Knot on cs, served from ovh2. "just learned cloudflare may have a quota on how many names we can register on dns so is that going to foil our plans to give everyone a bip353 payment address?" … "yeah build it on cs don't worry about the fallback just get it working with our knot dns" (Adam, 2026-11-16). coinos.io's Cloudflare zone (Pro) was at 3,038 of its 3,500 records with one TXT per name and about 35 names claimed a day. user._bitcoin-payment.coinos.io and user._bitcoin-payment.staging.coinos.io get delegated (NS + DS in Cloudflare) to Knot DNS, so there's no record quota. The `dns` compose service on cs is the hidden primary: it signs both zones (ECDSA P-256, NSEC3 without iterations, the KSK fixed because its DS sits in Cloudflare) and listens on cs's Tailscale address only, because the house router forwards nothing to cs. ovh2 runs the public secondary (dns/secondary.conf, dns1.coinos.io), notified and transferred over Tailscale with a TSIG key, so names keep resolving while cs is down. The registrar sends RFC 2136 UPDATEs signed with TSIG hmac-sha256 over TCP (names/dns-update.js) for domains with `knot` in its config. It replaces a name's TXT RRset whole, removes it on release and on account deletion (forgetAccount now hands back the name), and deletes a name's leftover Cloudflare record as it rewrites it. names/dns-migrate.js does the one-shot moves: backfill, check, cf-cleanup. dns/README.md is the runbook. Tested against Knot 3.4.11: updates land signed, a 700-byte URI spans several TXT strings, a wrong key gets NOTAUTH and an out-of-zone name gets NOTZONE. bun test names/ passes (21, with the new dns-update tests) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6SwkivduFDDkMmnXjZ --- names/dns-update.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 names/dns-update.js (limited to 'names/dns-update.js') diff --git a/names/dns-update.js b/names/dns-update.js new file mode 100644 index 0000000..021caf5 --- /dev/null +++ b/names/dns-update.js @@ -0,0 +1,90 @@ +// BIP-353 records on our own DNS server (Knot, dns/), not Cloudflare. +// Cloudflare caps a zone at 3,500 records (Pro) and coinos.io reached 3,038 +// with one TXT per name (2026-11-16), so `user._bitcoin-payment.` is +// delegated to Knot, which signs it (DNSSEC) and takes changes as RFC 2136 +// UPDATE messages authenticated with TSIG (RFC 8945, hmac-sha256), over TCP +// (an ark + lno URI doesn't fit a 512-byte UDP message). +import { createHmac, randomInt } from 'node:crypto'; +import { connect } from 'node:net'; + +const TYPE = { TXT: 16, SOA: 6, ANY: 255, TSIG: 250 }; +const CLASS = { IN: 1, NONE: 254, ANY: 255 }; + +function nameWire(name) { + const parts = String(name).replace(/\.$/, '').split('.').filter(Boolean); + const out = []; + for (const p of parts) { + const b = Buffer.from(p.toLowerCase(), 'ascii'); + if (!b.length || b.length > 63) throw new Error(`bad DNS label in ${name}`); + out.push(Buffer.from([b.length]), b); + } + out.push(Buffer.from([0])); + return Buffer.concat(out); +} +const u16 = (n) => { const b = Buffer.alloc(2); b.writeUInt16BE(n); return b; }; +const u32 = (n) => { const b = Buffer.alloc(4); b.writeUInt32BE(n >>> 0); return b; }; +const u48 = (n) => { const b = Buffer.alloc(6); b.writeUIntBE(n, 0, 6); return b; }; + +// TXT rdata: the value as ≤255-byte character-strings, in order (BIP-353 +// readers concatenate them) +export function txtRdata(value) { + const bytes = Buffer.from(String(value), 'utf8'); + const out = []; + for (let i = 0; i < bytes.length || i === 0; i += 255) { + const c = bytes.subarray(i, i + 255); + out.push(Buffer.from([c.length]), c); + if (!bytes.length) break; + } + return Buffer.concat(out); +} +const rr = (name, type, cls, ttl, rdata = Buffer.alloc(0)) => + Buffer.concat([nameWire(name), u16(type), u16(cls), u32(ttl), u16(rdata.length), rdata]); + +// ops: [{ name, value? }] — every op replaces the name's TXT records with +// `value`, or removes them when value is null +export function buildUpdate(zone, ops, ttl, key, { id = randomInt(0, 65536), now = Math.floor(Date.now() / 1000) } = {}) { + const updates = []; + for (const op of ops) { + updates.push(rr(op.name, TYPE.TXT, CLASS.ANY, 0)); // delete the RRset + if (op.value != null) updates.push(rr(op.name, TYPE.TXT, CLASS.IN, ttl, txtRdata(op.value))); + } + const header = (arcount) => Buffer.concat([u16(id), u16(5 << 11), u16(1), u16(0), u16(updates.length), u16(arcount)]); + const zoneSec = Buffer.concat([nameWire(zone), u16(TYPE.SOA), u16(CLASS.IN)]); + const body = Buffer.concat([zoneSec, ...updates]); + const unsigned = Buffer.concat([header(0), body]); + // TSIG: MAC over the message, then the TSIG variables (RFC 8945 §4.3.3) + const keyName = nameWire(key.name), alg = nameWire('hmac-sha256'); + const fudge = 300; + const vars = Buffer.concat([keyName, u16(CLASS.ANY), u32(0), alg, u48(now), u16(fudge), u16(0), u16(0)]); + const mac = createHmac('sha256', Buffer.from(key.secret, 'base64')).update(unsigned).update(vars).digest(); + const tsigRdata = Buffer.concat([alg, u48(now), u16(fudge), u16(mac.length), mac, u16(id), u16(0), u16(0)]); + const tsig = Buffer.concat([keyName, u16(TYPE.TSIG), u16(CLASS.ANY), u32(0), u16(tsigRdata.length), tsigRdata]); + return { id, msg: Buffer.concat([header(1), body, tsig]) }; +} + +const RCODES = ['NOERROR', 'FORMERR', 'SERVFAIL', 'NXDOMAIN', 'NOTIMP', 'REFUSED', 'YXDOMAIN', 'YXRRSET', 'NXRRSET', 'NOTAUTH', 'NOTZONE']; + +function exchange(host, port, msg, timeoutMs) { + return new Promise((resolve, reject) => { + const sock = connect({ host, port }); + let buf = Buffer.alloc(0); + const done = (err, v) => { clearTimeout(timer); sock.destroy(); err ? reject(err) : resolve(v); }; + const timer = setTimeout(() => done(new Error(`dns update to ${host}:${port} timed out`)), timeoutMs); + sock.on('connect', () => sock.write(Buffer.concat([u16(msg.length), msg]))); + sock.on('data', (d) => { + buf = Buffer.concat([buf, d]); + if (buf.length >= 2 && buf.length >= 2 + buf.readUInt16BE(0)) done(null, buf.subarray(2, 2 + buf.readUInt16BE(0))); + }); + sock.on('error', (e) => done(e)); + }); +} + +// Send one UPDATE; throws unless the server answers NOERROR. +export async function dnsUpdate(cfg, ops, ttl) { + if (!ops.length) return; + const { id, msg } = buildUpdate(cfg.zone, ops, ttl, cfg.key); + const res = await exchange(cfg.server || 'dns', cfg.port || 53, msg, cfg.timeoutMs || 10000); + if (res.length < 12 || res.readUInt16BE(0) !== id) throw new Error('dns update: bad reply'); + const rcode = res.readUInt16BE(2) & 0xf; + if (rcode) throw new Error(`dns update refused: ${RCODES[rcode] || rcode}`); +} -- cgit v1.2.3