import { createHmac, randomBytes, timingSafeEqual } from "node:crypto"; // RFC 3986: only A-Z a-z 0-9 - . _ ~ stay as they are. const enc = (s) => encodeURIComponent(s).replace(/[!'()*]/g, (c) => "%" + c.charCodeAt(0).toString(16).toUpperCase()); export function signLaunchUrl(secret, { placement, userId, subIds = {} }, ts = Math.floor(Date.now() / 1000), nonce = randomBytes(16).toString("hex")) { const keys = Object.keys(subIds).sort(); const sub = keys.map((k) => `${enc(k)}=${enc(subIds[k])}`).join("&"); const message = ["krin-launch-v1", placement, userId, String(ts), nonce, sub].join("\n"); const sig = createHmac("sha256", secret).update(message).digest("hex"); const query = new URLSearchParams({ placement, user_id: userId, ts: String(ts), nonce, sig }); for (const k of keys) query.append(`sub_ids[${k}]`, subIds[k]); return { url: `https://wall.krin.io/w?${query}`, sig }; } const same = (a, b) => a.length === b.length && timingSafeEqual(Buffer.from(a), Buffer.from(b)); // POST: header "t=,v1=" (two v1 values during a secret rotation). // rawBody: the exact bytes KRIN sent. secrets: [current] or [new, old] while you rotate. export function verifyPostbackPost(rawBody, header, secrets, now = Math.floor(Date.now() / 1000)) { let t = null; const sigs = []; for (const part of header.split(",")) { const [k, v = ""] = part.trim().split("=", 2); if (k === "t") t = v; else if (k === "v1") sigs.push(v); } if (!/^\d+$/.test(t ?? "") || Math.abs(now - Number(t)) > 300) return false; return secrets.some((secret) => { const expected = createHmac("sha256", secret).update(`${t}.`).update(rawBody).digest("hex"); return sigs.some((v) => same(v, expected)); }); } // GET: the decoded query values of the signed macros (krin-pb-v2; test is "0" or "1"). // A true result with q.test === "1" is a test postback: never credit it. export function verifyPostbackGet(q, secrets, now = Math.floor(Date.now() / 1000)) { if (!/^\d+$/.test(q.ts ?? "") || Math.abs(now - Number(q.ts)) > 300) return false; if (q.test !== "0" && q.test !== "1") return false; const message = ["krin-pb-v2", q.ts, q.conversion_id, q.kind, q.user_id, q.reward, q.payout_micros, q.test].join("\n"); return secrets.some((secret) => same(String(q.sig ?? ""), createHmac("sha256", secret).update(message).digest("hex"))); } // Advertiser S2S: sign the conversion you send to t.krin.io (postbacks.md 4.1, signature v2). // The signature covers the request exactly as sent: method, host, path and the query without sig. export function signInbound(secret, { offerKey, clickId, event, txnId = "" }, ts = Math.floor(Date.now() / 1000)) { const host = "t.krin.io"; const path = `/pb/${encodeURIComponent(offerKey)}`; const params = new URLSearchParams({ click_id: clickId, event }); if (txnId) params.set("txn_id", txnId); params.set("ts", String(ts)); const query = params.toString(); const sig = createHmac("sha256", secret).update(["krin-in-v2", "GET", host, path, query].join("\n")).digest("hex"); return { url: `https://${host}${path}?${query}&sig=${sig}`, sig }; }