diff options
Diffstat (limited to 'names/server.js')
| -rw-r--r-- | names/server.js | 39 |
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 |
