aboutsummaryrefslogtreecommitdiffstats
path: root/names/dns-update.js
diff options
context:
space:
mode:
Diffstat (limited to 'names/dns-update.js')
-rw-r--r--names/dns-update.js90
1 files changed, 90 insertions, 0 deletions
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.<domain>` 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}`);
+}