From 6254126f2301c629f7b98cf26d95f1f6b4c74c91 Mon Sep 17 00:00:00 2001 From: Marty Bostick Date: Fri, 9 Oct 2026 14:11:01 +0000 Subject: [PATCH] CTB Gate: click-to-verify button service for the Crypto Team Build Discord Co-Authored-By: Claude Fable 5.1 --- Dockerfile | 7 +++++ README.md | 3 +++ server.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 server.js diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c134a6b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM node:20-alpine +WORKDIR /app +COPY server.js ./ +ENV NODE_ENV=production +EXPOSE 3000 +HEALTHCHECK --interval=30s --timeout=5s CMD wget -qO- http://127.0.0.1:3000/health || exit 1 +CMD ["node", "server.js"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..17442e1 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# CTB Gate +Click-to-verify button service for the Crypto Team Build Discord. See the header of `server.js` for the env it needs. +Interactions Endpoint URL (Developer Portal, the CTB Gate application): `https:///interactions`. diff --git a/server.js b/server.js new file mode 100644 index 0000000..ad05a34 --- /dev/null +++ b/server.js @@ -0,0 +1,75 @@ +// CTB Gate — the click-to-verify button for the Crypto Team Build Discord (2026-10-09). +// Discord POSTs every button click to /interactions (signed with the app's Ed25519 public key). +// "verify" click -> the member gets the Verified role (unlocks #general-chat and #support) and an +// ephemeral confirmation. Admin-only "post-button" endpoint publishes the welcome button message. +// Env: DISCORD_PUBLIC_KEY, DISCORD_BOT_TOKEN (the CTB Gate bot, Manage Roles only), GUILD_ID, +// VERIFIED_ROLE_ID, WELCOME_CHANNEL_ID, DISCORD_WEBHOOK_OPS (log line per verification), +// MIN_ACCOUNT_AGE_HOURS (default 24; younger accounts are told to come back), ADMIN_KEY, PORT. +'use strict'; +const http = require('node:http'); +const crypto = require('node:crypto'); +const E = k => String(process.env[k] || '').trim(); +const API = 'https://discord.com/api/v10'; +const PORT = Number(E('PORT') || 3000); +const MIN_AGE_MS = Number(E('MIN_ACCOUNT_AGE_HOURS') || 24) * 3600000; + +function pubKey() { // raw 32-byte Ed25519 key -> SPKI DER + const raw = Buffer.from(E('DISCORD_PUBLIC_KEY'), 'hex'); + return crypto.createPublicKey({ key: Buffer.concat([Buffer.from('302a300506032b6570032100', 'hex'), raw]), format: 'der', type: 'spki' }); +} +function verified(req, body) { + try { + const sig = Buffer.from(req.headers['x-signature-ed25519'] || '', 'hex'), ts = String(req.headers['x-signature-timestamp'] || ''); + return sig.length === 64 && crypto.verify(null, Buffer.concat([Buffer.from(ts), body]), pubKey(), sig); + } catch (e) { return false; } +} +async function discord(method, path, payload) { + const r = await fetch(API + path, { method, headers: { Authorization: 'Bot ' + E('DISCORD_BOT_TOKEN'), 'Content-Type': 'application/json', 'X-Audit-Log-Reason': 'CTB Gate' }, body: payload ? JSON.stringify(payload) : undefined }); + return { ok: r.ok, status: r.status, body: await r.text().catch(() => '') }; +} +function opsLog(text) { + const url = E('DISCORD_WEBHOOK_OPS'); if (!url.startsWith('https://discord.com/api/webhooks/')) return; + fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content: text, allowed_mentions: { parse: [] }, flags: 4 }) }).catch(() => {}); +} +const snowflakeTime = id => Number((BigInt(id) >> 22n) + 1420070400000n); +const json = (res, code, obj) => { res.writeHead(code, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(obj)); }; +const ephemeral = content => ({ type: 4, data: { content, flags: 64 } }); + +async function onInteraction(i) { + if (i.type === 1) return { type: 1 }; // PING + if (i.type !== 3 || !i.data || i.data.custom_id !== 'ctb-verify') return ephemeral('Nothing to do.'); + const m = i.member, u = m && m.user; if (!u) return ephemeral('Use this button inside the server.'); + if (String(i.guild_id) !== E('GUILD_ID')) return ephemeral('Wrong server.'); + const role = E('VERIFIED_ROLE_ID'); + if ((m.roles || []).includes(role)) return ephemeral('✅ You are already verified. Head to <#' + E('GENERAL_CHANNEL_ID') + '>!'); + const age = Date.now() - snowflakeTime(u.id); + if (age < MIN_AGE_MS) { opsLog('\u{1F6D1} verify refused (account ' + Math.round(age / 3600000) + ' h old): ' + u.username + ' (' + u.id + ')'); return ephemeral('⏳ Your Discord account is very new. Come back in a day and tap the button again.'); } + const r = await discord('PUT', '/guilds/' + i.guild_id + '/members/' + u.id + '/roles/' + role); + if (!r.ok) { console.error('role add failed', r.status, r.body.slice(0, 200)); opsLog('⚠️ verify FAILED for ' + u.username + ' (' + u.id + '): http ' + r.status); return ephemeral('Something went wrong on our side. Ask in <#' + E('SUPPORT_CHANNEL_ID') + '> and an admin will verify you.'); } + opsLog('✅ verified: ' + u.username + ' (' + u.id + ') · account ' + Math.round(age / 86400000) + ' d old'); + return ephemeral('✅ Welcome in, ' + (u.global_name || u.username) + '! <#' + E('GENERAL_CHANNEL_ID') + '> and <#' + E('SUPPORT_CHANNEL_ID') + '> are open to you now. Earning happens in the CTB Rewards app: https://t.me/CTBRewards_Bot/app'); +} + +const BUTTON_MSG = { + content: '**New here? Tap the button to unlock the chat.**\nThis keeps spam bots out. It takes one click and you only do it once.', + components: [{ type: 1, components: [{ type: 2, style: 3, label: 'Verify me', emoji: { name: '✅' }, custom_id: 'ctb-verify' }] }], + allowed_mentions: { parse: [] }, +}; +http.createServer((req, res) => { + let chunks = []; req.on('data', c => chunks.push(c)); req.on('end', async () => { + const body = Buffer.concat(chunks); + try { + if (req.method === 'GET' && req.url === '/health') return json(res, 200, { ok: true, configured: !!(E('DISCORD_PUBLIC_KEY') && E('DISCORD_BOT_TOKEN') && E('VERIFIED_ROLE_ID')) }); + if (req.method === 'POST' && req.url === '/interactions') { + if (!verified(req, body)) return json(res, 401, { error: 'bad signature' }); + return json(res, 200, await onInteraction(JSON.parse(body.toString('utf8') || '{}'))); + } + if (req.method === 'POST' && req.url === '/post-button') { // admin: publish (or re-publish) the welcome button + if (!E('ADMIN_KEY') || req.headers['x-admin-key'] !== E('ADMIN_KEY')) return json(res, 403, { error: 'forbidden' }); + const r = await discord('POST', '/channels/' + E('WELCOME_CHANNEL_ID') + '/messages', BUTTON_MSG); + return json(res, r.ok ? 200 : 502, { ok: r.ok, status: r.status, body: r.body.slice(0, 300) }); + } + json(res, 404, { error: 'not found' }); + } catch (e) { console.error(e); json(res, 500, { error: 'server error' }); } + }); +}).listen(PORT, () => console.log('ctb-gate listening on ' + PORT));