|
| 1 | +/* eslint-disable camelcase */ |
| 2 | + |
| 3 | +import { payViaPaymentRequest, parsePaymentRequest } from 'ln-service' |
| 4 | +import { estimateRouteFee } from '@/api/lnd' |
| 5 | + |
| 6 | +import { payViaBolt12PaymentRequest, parseBolt12Request, estimateBolt12RouteFee } from '@/lib/lndk' |
| 7 | + |
| 8 | +export function isBolt11 (request) { |
| 9 | + return request.startsWith('lnbc') || request.startsWith('lntb') || request.startsWith('lntbs') || request.startsWith('lnbcrt') |
| 10 | +} |
| 11 | + |
| 12 | +export function parseBolt11 ({ request }) { |
| 13 | + if (!isBolt11(request)) { |
| 14 | + throw new Error('Not a bolt11 invoice') |
| 15 | + } |
| 16 | + return parsePaymentRequest({ request }) |
| 17 | +} |
| 18 | + |
| 19 | +export function payBolt11 ({ lnd, request, max_fee, ...args }) { |
| 20 | + if (!isBolt11(request)) { |
| 21 | + throw new Error('Not a bolt11 invoice') |
| 22 | + } |
| 23 | + return payViaPaymentRequest({ |
| 24 | + lnd, |
| 25 | + request, |
| 26 | + max_fee, |
| 27 | + ...args |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +export function isBolt12Offer (invoice) { |
| 32 | + return invoice.startsWith('lno1') |
| 33 | +} |
| 34 | + |
| 35 | +export function isBolt12Invoice (invoice) { |
| 36 | + console.log('isBolt12Invoice', invoice) |
| 37 | + console.trace() |
| 38 | + return invoice.startsWith('lni1') |
| 39 | +} |
| 40 | + |
| 41 | +export async function payBolt12 ({ lnd, request: invoice, max_fee }) { |
| 42 | + if (!isBolt12Invoice(invoice)) { |
| 43 | + throw new Error('Not a bolt12 invoice') |
| 44 | + } |
| 45 | + if (!invoice) throw new Error('No invoice in bolt12, please use prefetchBolt12Invoice') |
| 46 | + return await payViaBolt12PaymentRequest({ lnd, request: invoice, max_fee }) |
| 47 | +} |
| 48 | + |
| 49 | +export function parseBolt12 ({ lnd, request: invoice }) { |
| 50 | + if (!isBolt12Invoice(invoice)) { |
| 51 | + throw new Error('Not a bolt12 request') |
| 52 | + } |
| 53 | + return parseBolt12Request({ lnd, request: invoice }) |
| 54 | +} |
| 55 | + |
| 56 | +export async function payInvoice ({ lnd, request: invoice, max_fee, ...args }) { |
| 57 | + if (isBolt12Invoice(invoice)) { |
| 58 | + return await payBolt12({ lnd, request: invoice, max_fee, ...args }) |
| 59 | + } else { |
| 60 | + return await payBolt11({ lnd, request: invoice, max_fee, ...args }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export async function parseInvoice ({ lnd, request }) { |
| 65 | + if (isBolt12Invoice(request)) { |
| 66 | + return await parseBolt12({ lnd, request }) |
| 67 | + } else { |
| 68 | + return await parseBolt11({ request }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export async function estimateFees ({ lnd, destination, tokens, mtokens, request, timeout }) { |
| 73 | + if (isBolt12Invoice(request)) { |
| 74 | + return await estimateBolt12RouteFee({ lnd, destination, tokens, mtokens, request, timeout }) |
| 75 | + } else { |
| 76 | + return await estimateRouteFee({ lnd, destination, tokens, request, mtokens, timeout }) |
| 77 | + } |
| 78 | +} |
0 commit comments