new "addInvoice()"

This commit is contained in:
Daniel Lugo 2021-01-13 17:34:38 -04:00
parent d6cdba2276
commit 427c896f42
3 changed files with 89 additions and 58 deletions

View file

@ -1709,49 +1709,12 @@ module.exports = async (
})
// addinvoice
app.post('/api/lnd/addinvoice', (req, res) => {
const { lightning } = LightningServices.services
const invoiceRequest = { memo: req.body.memo, private: true }
if (req.body.value) {
invoiceRequest.value = req.body.value
}
if (req.body.expiry) {
invoiceRequest.expiry = req.body.expiry
}
lightning.addInvoice(invoiceRequest, async (err, newInvoice) => {
if (err) {
logger.debug('AddInvoice Error:', err)
const health = await checkHealth()
if (health.LNDStatus.success) {
res.status(400).json({
field: 'addInvoice',
errorMessage: sanitizeLNDError(err.message)
})
} else {
res.status(500)
res.json({ errorMessage: 'LND is down' })
}
return err
}
logger.debug('AddInvoice:', newInvoice)
if (req.body.value) {
logger.debug('AddInvoice liquidity check:')
lightning.listChannels({ active_only: true }, async (err, response) => {
if (err) {
logger.debug('ListChannels Error:', err)
const health = await checkHealth()
if (health.LNDStatus.success) {
res.status(400).json({
field: 'listChannels',
errorMessage: sanitizeLNDError(err.message)
})
} else {
res.status(500)
res.json({ errorMessage: 'LND is down' })
}
}
logger.debug('ListChannels:', response)
const channelsList = response.channels
app.post('/api/lnd/addinvoice', async (req, res) => {
const { expiry, value, memo } = req.body
const addInvoiceRes = await LV2.addInvoice(value, memo, true, expiry)
if (value) {
const channelsList = await LV2.listChannels({ active_only: true })
let remoteBalance = Big(0)
channelsList.forEach(element => {
const remB = Big(element.remote_balance)
@ -1759,14 +1722,19 @@ module.exports = async (
remoteBalance = remB
}
})
newInvoice.liquidityCheck = remoteBalance > req.body.value
addInvoiceRes.liquidityCheck = remoteBalance > value
//newInvoice.remoteBalance = remoteBalance
res.json(newInvoice)
})
} else {
res.json(newInvoice)
}
try {
return res.json(addInvoiceRes)
} catch (e) {
console.log(e)
return res.status(500).json({
errorMessage: e.message
})
}
})
// signmessage

View file

@ -166,3 +166,31 @@ export interface PendingChannelsRes {
*/
waiting_close_channels: Common.WaitingCloseChannel[]
}
/**
* https://github.com/lightningnetwork/lnd/blob/daf7c8a85420fc67fffa18fa5f7d08c2040946e4/lnrpc/rpc.proto#L2948
*/
export interface AddInvoiceRes {
/**
*
*/
r_hash: Common.Bytes
/**
* A bare-bones invoice for a payment within the Lightning Network. With the
* details of the invoice, the sender has all the data necessary to send a
* payment to the recipient.
*/
payment_request: string
/**
* The "add" index of this invoice. Each newly created invoice will increment
* this index making it monotonically increasing. Callers to the
* SubscribeInvoices call can use this to instantly get notified of all added
* invoices with an add_index greater than this one.
*/
add_index: string
/**
* The payment address of the generated invoice. This value should be used in
* all payments for this invoice as we require it for end to end security.
*/
payment_addr: Common.Bytes
}

View file

@ -537,6 +537,40 @@ const pendingChannels = () =>
})
})
/**
* @typedef {import('./types').AddInvoiceRes} AddInvoiceRes
*/
/**
* https://api.lightning.community/#addinvoice
* @param {number} value
* @param {string=} memo
* @param {boolean=} confidential Alias for `private`.
* @param {number=} expiry
* @returns {Promise<AddInvoiceRes>}
*/
const addInvoice = (value, memo = '', confidential = true, expiry = 180) =>
Common.makePromise((res, rej) => {
const { lightning } = lightningServices.getServices()
lightning.addInvoice(
{
value,
memo,
private: confidential,
expiry
},
(err, resp) => {
if (err) {
rej(new Error(err.message))
} else {
// Needs cast because typescript refuses to assign Record<string, any>
// to an actual object :shrugs
res(/** @type {AddInvoiceRes} */ (resp))
}
}
)
})
module.exports = {
sendPaymentV2Keysend,
sendPaymentV2Invoice,
@ -547,5 +581,6 @@ module.exports = {
listChannels,
getChanInfo,
listPeers,
pendingChannels
pendingChannels,
addInvoice
}