diff --git a/utils/lightningServices/v2.js b/utils/lightningServices/v2.js index b933c63b..db9f8d6b 100644 --- a/utils/lightningServices/v2.js +++ b/utils/lightningServices/v2.js @@ -6,6 +6,8 @@ const logger = require('winston') const Common = require('shock-common') const Ramda = require('ramda') +const { writeCoordinate } = require('../../services/coordinates') + const lightningServices = require('./lightning-services') /** * @typedef {import('./types').PaymentV2} PaymentV2 @@ -213,12 +215,39 @@ const isValidSendPaymentInvoiceParams = sendPaymentInvoiceParams => { return true } +/** + * @param {string} payReq + * @returns {Promise} + */ +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} + */ +const myLNDPub = () => Promise.resolve('afjsjkhasdjkhajksd') + /** * aklssjdklasd * @param {SendPaymentV2Request} sendPaymentRequest * @returns {Promise} */ -const sendPaymentV2 = sendPaymentRequest => { +const sendPaymentV2 = async sendPaymentRequest => { const { services: { router } } = 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) 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} - */ -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 * @returns {Promise} @@ -582,5 +619,6 @@ module.exports = { getChanInfo, listPeers, pendingChannels, - addInvoice + addInvoice, + myLNDPub }