use basic coordinates

This commit is contained in:
Daniel Lugo 2021-01-18 15:43:12 -04:00
parent f19f04a97c
commit d67b6ef674

View file

@ -6,6 +6,8 @@ const logger = require('winston')
const Common = require('shock-common') const Common = require('shock-common')
const Ramda = require('ramda') const Ramda = require('ramda')
const { writeCoordinate } = require('../../services/coordinates')
const lightningServices = require('./lightning-services') const lightningServices = require('./lightning-services')
/** /**
* @typedef {import('./types').PaymentV2} PaymentV2 * @typedef {import('./types').PaymentV2} PaymentV2
@ -213,12 +215,39 @@ const isValidSendPaymentInvoiceParams = sendPaymentInvoiceParams => {
return true return true
} }
/**
* @param {string} payReq
* @returns {Promise<Common.Schema.InvoiceWhenDecoded>}
*/
const decodePayReq = payReq =>
Common.Utils.makePromise((res, rej) => {
lightningServices.lightning.decodePayReq(
{ pay_req: payReq },
/**
* @param {{ message: any; }} err
* @param {any} paymentRequest
*/
(err, paymentRequest) => {
if (err) {
rej(new Error(err.message))
} else {
res(paymentRequest)
}
}
)
})
/**
* @returns {Promise<string>}
*/
const myLNDPub = () => Promise.resolve('afjsjkhasdjkhajksd')
/** /**
* aklssjdklasd * aklssjdklasd
* @param {SendPaymentV2Request} sendPaymentRequest * @param {SendPaymentV2Request} sendPaymentRequest
* @returns {Promise<PaymentV2>} * @returns {Promise<PaymentV2>}
*/ */
const sendPaymentV2 = sendPaymentRequest => { const sendPaymentV2 = async sendPaymentRequest => {
const { const {
services: { router } services: { router }
} = lightningServices } = lightningServices
@ -229,7 +258,10 @@ const sendPaymentV2 = sendPaymentRequest => {
) )
} }
return new Promise((res, rej) => { /**
* @type {import("./types").PaymentV2}
*/
const paymentV2 = await Common.makePromise((res, rej) => {
const stream = router.sendPaymentV2(sendPaymentRequest) const stream = router.sendPaymentV2(sendPaymentRequest)
stream.on( stream.on(
@ -268,6 +300,33 @@ const sendPaymentV2 = sendPaymentRequest => {
} }
) )
}) })
/** @type {Common.Coordinate} */
const coord = {
amount: Number(paymentV2.value_sat),
id: paymentV2.payment_hash,
inbound: false,
timestamp: Date.now(),
toLndPub: await myLNDPub(),
fromLndPub: undefined,
invoiceMemo: undefined,
type: 'payment'
}
if (sendPaymentRequest.payment_request) {
const invoice = await decodePayReq(sendPaymentRequest.payment_request)
coord.invoiceMemo = invoice.description
coord.toLndPub = invoice.destination
}
if (sendPaymentRequest.dest) {
coord.toLndPub = sendPaymentRequest.dest.toString('base64')
}
await writeCoordinate(paymentV2.payment_hash, coord)
return paymentV2
} }
/** /**
@ -380,28 +439,6 @@ const listPayments = req => {
}) })
} }
/**
* @param {string} payReq
* @returns {Promise<Common.Schema.InvoiceWhenDecoded>}
*/
const decodePayReq = payReq =>
Common.Utils.makePromise((res, rej) => {
lightningServices.lightning.decodePayReq(
{ pay_req: payReq },
/**
* @param {{ message: any; }} err
* @param {any} paymentRequest
*/
(err, paymentRequest) => {
if (err) {
rej(new Error(err.message))
} else {
res(paymentRequest)
}
}
)
})
/** /**
* @param {0|1} type * @param {0|1} type
* @returns {Promise<string>} * @returns {Promise<string>}
@ -582,5 +619,6 @@ module.exports = {
getChanInfo, getChanInfo,
listPeers, listPeers,
pendingChannels, pendingChannels,
addInvoice addInvoice,
myLNDPub
} }