1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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();
}
|