@@ -4,6 +4,7 @@ import { toMsat } from './lib/exchange-rate'
44const debug = require ( 'debug' ) ( 'lightning-charge' )
55 , status = inv => inv . pay_index ? 'paid' : inv . expires_at > now ( ) ? 'unpaid' : 'expired'
66 , format = inv => ( { ...inv , status : status ( inv ) , msatoshi : ( inv . msatoshi || null ) , metadata : JSON . parse ( inv . metadata ) } )
7+ , formatLnurlpay = lnurlpay => ( { ...lnurlpay , metadata : JSON . parse ( lnurlpay . metadata ) } )
78 , now = _ => Date . now ( ) / 1000 | 0
89
910// @XXX invoices that accept any amount are stored as msatoshi='' (empty string)
@@ -16,19 +17,21 @@ const defaultDesc = process.env.INVOICE_DESC_DEFAULT || 'Lightning Charge Invoic
1617
1718module . exports = ( db , ln ) => {
1819 const newInvoice = async props => {
19- const { currency, amount, expiry, description , metadata, webhook } = props
20+ const { currency, amount, expiry, metadata, webhook, lnurlpay_endpoint } = props
2021
2122 const id = nanoid ( )
2223 , msatoshi = props . msatoshi ? '' + props . msatoshi : currency ? await toMsat ( currency , amount ) : ''
23- , desc = props . description ? '' + props . description : defaultDesc
24- , lninv = await ln . invoice ( msatoshi || 'any' , id , desc , expiry )
24+ , desc = props . descriptionHash || ( props . description ? '' + props . description : defaultDesc )
25+ , method = props . descriptionHash ? 'invoicewithdescriptionhash' : 'invoice'
26+ , lninv = await ln . call ( method , [ msatoshi || 'any' , id , desc , expiry ] )
2527
2628 const invoice = {
2729 id, msatoshi, description : desc
2830 , quoted_currency : currency , quoted_amount : amount
2931 , rhash : lninv . payment_hash , payreq : lninv . bolt11
3032 , expires_at : lninv . expires_at , created_at : now ( )
3133 , metadata : JSON . stringify ( metadata || null )
34+ , lnurlpay_endpoint
3235 }
3336
3437 debug ( 'saving invoice:' , invoice )
@@ -50,6 +53,66 @@ module.exports = (db, ln) => {
5053 await db ( 'invoice' ) . where ( { id } ) . del ( )
5154 }
5255
56+ const listLnurlPayEndpoints = _ =>
57+ db ( 'lnurlpay_endpoint' )
58+ . then ( rows => rows . map ( formatLnurlpay ) )
59+
60+ const listInvoicesByLnurlPayEndpoint = lnurlpayId =>
61+ db ( 'invoice' )
62+ . where ( { lnurlpay_endpoint : lnurlpayId } )
63+ . then ( rows => rows . map ( format ) )
64+
65+ const getLnurlPayEndpoint = async id => {
66+ let lnurlpay = await db ( 'lnurlpay_endpoint' ) . where ( { id } ) . first ( )
67+ return formatLnurlpay ( lnurlpay )
68+ }
69+
70+ const setLnurlPayEndpoint = async ( id , props ) => {
71+ let lnurlpay
72+ if ( id ) {
73+ lnurlpay = await db ( 'lnurlpay_endpoint' ) . where ( { id } ) . first ( )
74+ lnurlpay = { ...lnurlpay , ...props }
75+ } else
76+ lnurlpay = { ...props , id : nanoid ( ) }
77+
78+ if ( typeof props . metadata != 'undefined' ) {
79+ let metadata = JSON . stringify ( props . metadata || { } )
80+ if ( metadata [ 0 ] != '{' )
81+ metadata = '{}'
82+
83+ lnurlpay . metadata = metadata
84+ }
85+
86+ if ( props . amount ) {
87+ lnurlpay . min = props . amount
88+ lnurlpay . max = props . amount
89+ } else if ( props . min <= props . max ) {
90+ lnurlpay . min = props . min
91+ lnurlpay . max = props . max
92+ } else if ( props . min > props . max ) {
93+ // silently correct a user error
94+ lnurlpay . min = props . max
95+ lnurlpay . max = props . min
96+ }
97+
98+ if ( lnurlpay . min && ! lnurlpay . max )
99+ lnurlpay . max = lnurlpay . min
100+
101+ if ( lnurlpay . max && ! lnurlpay . min )
102+ lnurlpay . min = lnurlpay . max
103+
104+ await db ( 'lnurlpay_endpoint' )
105+ . insert ( lnurlpay )
106+ . onConflict ( 'id' )
107+ . merge ( )
108+
109+ return formatLnurlpay ( lnurlpay )
110+ }
111+
112+ const delLnurlPayEndpoint = async id => {
113+ await db ( 'lnurlpay_endpoint' ) . where ( { id } ) . del ( )
114+ }
115+
53116 const markPaid = ( id , pay_index , paid_at , msatoshi_received ) =>
54117 db ( 'invoice' ) . where ( { id, pay_index : null } )
55118 . update ( { pay_index, paid_at, msatoshi_received } )
@@ -85,7 +148,8 @@ module.exports = (db, ln) => {
85148 : { requested_at : now ( ) , success : false , resp_error : err } )
86149
87150 return { newInvoice, listInvoices, fetchInvoice, delInvoice
151+ , listInvoicesByLnurlPayEndpoint, listLnurlPayEndpoints
152+ , getLnurlPayEndpoint, setLnurlPayEndpoint, delLnurlPayEndpoint
88153 , getLastPaid, markPaid, delExpired
89154 , addHook, getHooks, logHook }
90155}
91-
0 commit comments