diff options
Diffstat (limited to 'lib/lnurl-payload.ts')
| -rw-r--r-- | lib/lnurl-payload.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/lnurl-payload.ts b/lib/lnurl-payload.ts new file mode 100644 index 00000000..6248d213 --- /dev/null +++ b/lib/lnurl-payload.ts @@ -0,0 +1,60 @@ +import { bech32 } from "bech32"; + +const invalid = (): never => { throw new Error("Invalid LNURL response"); }; +const object = (v: any) => v !== null && typeof v === "object" && !Array.isArray(v); +const amount = (v: any, min: number) => Number.isSafeInteger(v) && v >= min; +const callback = (v: any) => { + if (typeof v !== "string") return invalid(); + const u = new URL(v); + if (!["http:", "https:"].includes(u.protocol) || u.username || u.password) invalid(); + return v; +}; + +// Return only protocol fields: never relay an arbitrary upstream JSON object. +export function lnurlPayload(v: any): any { + if (!object(v)) return invalid(); + if (v.status === "ERROR" && typeof v.reason === "string") + return { status: "ERROR", reason: "LNURL service returned an error" }; + if (v.status === "OK" && !v.tag && !v.pr) return { status: "OK" }; + if (v.tag === "payRequest") { + if (!amount(v.minSendable, 1) || !amount(v.maxSendable, v.minSendable) || typeof v.metadata !== "string") invalid(); + let metadata: any; + try { metadata = JSON.parse(v.metadata); } catch { return invalid(); } + if (!Array.isArray(metadata) || !metadata.every((e: any) => Array.isArray(e) && e.length >= 2 && typeof e[0] === "string") || + metadata.filter((e: any) => e[0] === "text/plain" && typeof e[1] === "string").length !== 1) invalid(); + const out: any = { tag: v.tag, callback: callback(v.callback), minSendable: v.minSendable, maxSendable: v.maxSendable, metadata: v.metadata }; + if (amount(v.commentAllowed, 0)) out.commentAllowed = v.commentAllowed; + if (v.allowsNostr === true && typeof v.nostrPubkey === "string" && /^[a-f0-9]{64}$/i.test(v.nostrPubkey)) { + out.allowsNostr = true; out.nostrPubkey = v.nostrPubkey; + } + return out; + } + if (v.tag === "withdrawRequest") { + if (typeof v.k1 !== "string" || !v.k1 || typeof v.defaultDescription !== "string" || + !amount(v.minWithdrawable, 0) || !amount(v.maxWithdrawable, v.minWithdrawable)) invalid(); + return { tag: v.tag, callback: callback(v.callback), k1: v.k1, defaultDescription: v.defaultDescription, + minWithdrawable: v.minWithdrawable, maxWithdrawable: v.maxWithdrawable }; + } + if (typeof v.pr === "string") { + let decoded: any; + try { decoded = bech32.decode(v.pr, 20000); } catch { return invalid(); } + if (!/^ln(bc|tb|bcrt|sb)[0-9]*[munp]?$/.test(decoded.prefix) || decoded.words.length < 111 || + !Array.isArray(v.routes) || v.routes.length !== 0) invalid(); + const out: any = { pr: v.pr, routes: [] }; + if (v.disposable === false || v.disposable === true) out.disposable = v.disposable; + if (typeof v.verify === "string") out.verify = callback(v.verify); + const a = v.successAction; + if (object(a)) { + if (a.tag === "message" && typeof a.message === "string" && a.message.length <= 144) + out.successAction = { tag: a.tag, message: a.message }; + else if (a.tag === "url" && typeof a.description === "string" && a.description.length <= 144) + out.successAction = { tag: a.tag, description: a.description, url: callback(a.url) }; + else if (a.tag === "aes" && typeof a.description === "string" && a.description.length <= 144 && + typeof a.ciphertext === "string" && a.ciphertext.length <= 4096 && typeof a.iv === "string" && /^[A-Za-z0-9+/]{22}==$/.test(a.iv)) + out.successAction = { tag: a.tag, description: a.description, ciphertext: a.ciphertext, iv: a.iv }; + else invalid(); + } + return out; + } + return invalid(); +} |
