52 lines
1.1 KiB
JavaScript
52 lines
1.1 KiB
JavaScript
|
|
import { bech32 } from 'bech32'
|
|
import UTF8 from 'utf-8'
|
|
|
|
function getDomain(url) {
|
|
return url
|
|
.split('://')[1]
|
|
.split('/')[0]
|
|
.split('@')
|
|
.slice(-1)[0]
|
|
.split(':')[0]
|
|
}
|
|
|
|
export function decodeLnurl(lnurl) {
|
|
let obj = bech32.decode(lnurl,20000)
|
|
if (obj.prefix!="lnurl")
|
|
throw new Error("bad prefix")
|
|
return UTF8.getStringFromBytes(bech32.fromWords(obj.words))
|
|
}
|
|
|
|
export async function payStep1(url) {
|
|
const r = await fetch(url)
|
|
if (r.status >= 300) {
|
|
throw new Error(await r.text())
|
|
}
|
|
let res = await r.json()
|
|
if (res.status == "ERROR")
|
|
return res;
|
|
if (res.tag != "payRequest") {
|
|
throw new Error(`LNURL type ${r.tag}, expected: payRequest`)
|
|
}
|
|
try {
|
|
res.decodedMetadata = JSON.parse(res.metadata)
|
|
} catch (err) {
|
|
res.decodedMetadata = []
|
|
}
|
|
return res
|
|
}
|
|
|
|
export async function payStep2(url, res, msat, comment) {
|
|
let cb = res.callback;
|
|
let params = new URLSearchParams();
|
|
params.set("amount",msat);
|
|
cb = cb+(cb.includes("?")?"&":"?")+params.toString()
|
|
const r = await fetch(cb)
|
|
if (r.status >= 300) {
|
|
throw new Error(await r.text())
|
|
}
|
|
let res2 = await r.json()
|
|
return res2
|
|
}
|