use new wrapper

This commit is contained in:
Daniel Lugo 2020-08-21 13:50:15 -04:00
parent 7543a7eca9
commit a72dd72280

View file

@ -29,6 +29,10 @@ const {
const GunActions = require('../services/gunDB/contact-api/actions') const GunActions = require('../services/gunDB/contact-api/actions')
const GunGetters = require('../services/gunDB/contact-api/getters') const GunGetters = require('../services/gunDB/contact-api/getters')
const GunKey = require('../services/gunDB/contact-api/key') const GunKey = require('../services/gunDB/contact-api/key')
const {
sendPaymentV2Keysend,
sendPaymentV2Invoice
} = require('../utils/lightningServices/v2')
const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10 const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10
const SESSION_ID = uuid() const SESSION_ID = uuid()
@ -1442,92 +1446,46 @@ module.exports = async (
}) })
// sendpayment // sendpayment
app.post('/api/lnd/sendpayment', (req, res) => { app.post('/api/lnd/sendpayment', async (req, res) => {
const { router } = LightningServices.services
// this is the recommended value from lightning labs // this is the recommended value from lightning labs
let paymentRequest = {}
const { keysend, maxParts = 3, timeoutSeconds = 5, feeLimit } = req.body const { keysend, maxParts = 3, timeoutSeconds = 5, feeLimit } = req.body
if (!feeLimit) { if (!feeLimit) {
return res.status(500).json({ return res.status(400).json({
errorMessage: 'please provide a "feeLimit" to the send payment request' errorMessage: 'please provide a "feeLimit" to the send payment request'
}) })
} }
if (keysend) { if (keysend) {
const { dest, amt, finalCltvDelta = 40 } = req.body const { dest, amt, finalCltvDelta = 40 } = req.body
if (!dest || !amt) { if (!dest || !amt) {
return res.status(500).json({ return res.status(400).json({
errorMessage: 'please provide "dest" and "amt" for keysend payments' errorMessage: 'please provide "dest" and "amt" for keysend payments'
}) })
} }
const preimage = Crypto.randomBytes(32)
const r_hash = Crypto.createHash('sha256') const payment = await sendPaymentV2Keysend({
.update(preimage)
.digest()
//https://github.com/lightningnetwork/lnd/blob/master/record/experimental.go#L5:2
//might break in future updates
const KeySendType = 5482373484
//https://api.lightning.community/#featurebit
const TLV_ONION_REQ = 8
paymentRequest = {
dest: Buffer.from(dest, 'hex'),
amt, amt,
final_cltv_delta: finalCltvDelta, dest,
dest_features: [TLV_ONION_REQ], feeLimit,
dest_custom_records: { finalCltvDelta,
[KeySendType]: preimage maxParts,
}, timeoutSeconds
payment_hash: r_hash, })
max_parts: maxParts,
timeout_seconds: timeoutSeconds,
no_inflight_updates: true,
fee_limit_sat: feeLimit
}
} else {
const { payreq } = req.body
paymentRequest = { return res.status(200).json(payment)
payment_request: payreq,
max_parts: maxParts,
timeout_seconds: timeoutSeconds,
no_inflight_updates: true,
fee_limit_sat: feeLimit
}
if (req.body.amt) {
paymentRequest.amt = req.body.amt
}
} }
const { payreq } = req.body
logger.info('Sending payment', paymentRequest) const payment = await sendPaymentV2Invoice({
const sentPayment = router.sendPaymentV2(paymentRequest) feeLimit,
sentPayment.on('data', response => { payment_request: payreq,
logger.info('SendPayment Data:', response) amt: req.body.amt,
if (response.failure_reason !== 'FAILURE_REASON_NONE') { max_parts: maxParts,
res.status(500).json({ timeoutSeconds
errorMessage: response.failure_reason
})
} else {
res.json(response)
}
}) })
sentPayment.on('status', status => { return res.status(200).json(payment)
logger.info('SendPayment Status:', status)
})
sentPayment.on('error', async err => {
logger.error('SendPayment Error:', err)
const health = await checkHealth()
if (health.LNDStatus.success) {
res.status(500).json({
errorMessage: sanitizeLNDError(err.details)
})
} else {
res.status(500)
res.json({ errorMessage: 'LND is down' })
}
})
//sentPayment.on('end', () => {})
}) })
app.post('/api/lnd/trackpayment', (req, res) => { app.post('/api/lnd/trackpayment', (req, res) => {