aboutsummaryrefslogtreecommitdiffstats
path: root/names/server.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-12-01 11:52:18 -0700
committerAdam Soltys <[email protected]>2026-12-01 11:52:18 -0700
commit24738a235e87e7e17ee8a721609e0cbb6613218e (patch)
tree94a027344d44591b5cf2623cff2690dd61dba925 /names/server.js
parent4b568b59a28d6fc466671b92ecd68fc45d1c075e (diff)
downloadcoinosv3-server-24738a235e87e7e17ee8a721609e0cbb6613218e.tar.gz
coinosv3-server-24738a235e87e7e17ee8a721609e0cbb6613218e.zip
names: a free Santa hat for everyone through December. "Let's add a free Santa hat to the hat shop for everyone. Available only through the month of December." (Adam, 2026-12-01). POST /hats/free (NIP-98) grants and wears a free seasonal hat while its season is on; /hats/<pk> lists the hats free right now (free: ['santa']). December runs from the moment it is December 1st anywhere (UTC+14) to the last moment it is December 31st anywhere (UTC-12), every year. A hat taken stays in the rack after the season; out of season /hats/free refuses it. The operator grant accepts it too.
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar
Diffstat (limited to 'names/server.js')
-rw-r--r--names/server.js39
1 files changed, 37 insertions, 2 deletions
diff --git a/names/server.js b/names/server.js
index 553377e..0a8a9f1 100644
--- a/names/server.js
+++ b/names/server.js
@@ -747,6 +747,20 @@ const hatNetOf = (v) => (v === 'mutinynet' ? 'mutinynet' : 'mainnet');
const hatStore = (net) => (net === 'mutinynet' ? (state.hatsMut ||= {}) : (state.hats ||= {}));
const hatLn = (net) => (net === 'mutinynet' ? lnMut : ln);
+// Free seasonal hats: anyone may take one while its season is on, and keeps
+// it afterwards. The Santa hat is December's — from the first moment it is
+// December 1st anywhere on Earth (UTC+14) to the last moment it is still
+// December 31st anywhere (UTC−12), so every time zone gets the whole month.
+const HAT_FREE = {
+ santa: (now) => {
+ const y = new Date(now - 13 * 86400_000).getUTCFullYear(); // early January belongs to last December
+ const from = Date.UTC(y, 11, 1) - 14 * 3600_000;
+ const to = Date.UTC(y + 1, 0, 1) + 12 * 3600_000;
+ return now >= from && now < to;
+ },
+};
+const freeHatsNow = (now = Date.now()) => Object.keys(HAT_FREE).filter((id) => HAT_FREE[id](now));
+
function hatRec(pk, net = 'mainnet') {
const r = hatStore(net)[pk];
const owned = r ? [...(r.owned || [])] : [];
@@ -1745,7 +1759,28 @@ Bun.serve({
// client shows what the server would actually charge).
const hm = url.pathname.match(/^\/hats\/([0-9a-f]{64})$/);
if (hm && req.method === 'GET') {
- return json({ ...hatRec(hm[1], hatNetOf(url.searchParams.get('net'))), prices: HAT_PRICES });
+ return json({ ...hatRec(hm[1], hatNetOf(url.searchParams.get('net'))), prices: HAT_PRICES, free: freeHatsNow() });
+ }
+
+ // A free seasonal hat, taken while its season is on: straight onto the
+ // head, like a purchase. NIP-98 so it lands on the taker's own key.
+ if (url.pathname === '/hats/free' && req.method === 'POST') {
+ if (!rateOk(ip)) return json({ error: 'rate limited' }, 429);
+ const bodyText = await req.text();
+ const a = await checkNip98(req, url, bodyText);
+ if (a.error) return json({ error: a.error }, 401);
+ let body;
+ try { body = JSON.parse(bodyText); } catch { return json({ error: 'bad body' }, 400); }
+ const hat = String(body.hat || '');
+ const net = hatNetOf(body.net);
+ if (!(hat in HAT_FREE)) return json({ error: 'no such hat' }, 400);
+ if (!HAT_FREE[hat](Date.now())) return json({ error: 'that hat is out of season' }, 400);
+ if (!hatRec(a.pubkey, net).owned.includes(hat)) {
+ grantHat(a.pubkey, hat, net);
+ persist();
+ log(`hat taken: ${hat} (${net}) by ${a.pubkey.slice(0, 12)}`);
+ }
+ return json(hatRec(a.pubkey, net));
}
// Operator gift: drop a hat into someone's rack (x-admin-token). Takes
@@ -1764,7 +1799,7 @@ Bun.serve({
if (!/^[0-9a-f]{64}$/.test(pk)) return json({ error: 'bad pubkey' }, 400);
const hat = String(b.hat || '');
// giftable but never sold: the beta hard hat, and the knight's helm
- if (!(hat in HAT_PRICES) && !['construction', 'knight'].includes(hat)) return json({ error: 'no such hat' }, 400);
+ if (!(hat in HAT_PRICES) && !(hat in HAT_FREE) && !['construction', 'knight'].includes(hat)) return json({ error: 'no such hat' }, 400);
const net = hatNetOf(b.net);
// the operator may say the gift goes straight on the head (a hat made
// for this one person); by default it only crowns a bare one