diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index 293b0337..1a9d4cf1 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -906,17 +906,30 @@ const sendHRWithInitialMsg = async ( await sendMessage(recipientPublicKey, initialMsg, user, SEA) } +/** + * @typedef {object} SpontPaymentOptions + * @prop {Common.Schema.OrderTargetType} type + * @prop {string=} postID + */ + /** * Returns the preimage corresponding to the payment. * @param {string} to * @param {number} amount * @param {string} memo * @param {number} feeLimit + * @param {SpontPaymentOptions} opts * @throws {Error} If no response in less than 20 seconds from the recipient, or * lightning cannot find a route for the payment. * @returns {Promise} The payment's preimage. */ -const sendSpontaneousPayment = async (to, amount, memo, feeLimit) => { +const sendSpontaneousPayment = async ( + to, + amount, + memo, + feeLimit, + opts = { type: 'user' } +) => { try { const SEA = require('../Mediator').mySEA const getUser = () => require('../Mediator').getUser() @@ -937,7 +950,11 @@ const sendSpontaneousPayment = async (to, amount, memo, feeLimit) => { from: getUser()._.sea.pub, memo: memo || 'no memo', timestamp: Date.now(), - targetType: 'user' + targetType: opts.type + } + + if (opts.type === 'post') { + order.postID = opts.postID } logger.info(JSON.stringify(order)) diff --git a/src/routes.js b/src/routes.js index 5e184349..62d93272 100644 --- a/src/routes.js +++ b/src/routes.js @@ -1267,12 +1267,12 @@ module.exports = async ( app.post('/api/lnd/unifiedTrx', async (req, res) => { try { - const { type, amt, to, memo, feeLimit } = req.body + const { type, amt, to, memo, feeLimit, postID } = req.body - if (type !== 'spont') { + if (type !== 'spont' && type !== 'post') { return res.status(415).json({ field: 'type', - errorMessage: `Only 'spont' payments supported via this endpoint for now.` + errorMessage: `Only 'spont' and 'post' payments supported via this endpoint for now.` }) } @@ -1306,9 +1306,19 @@ module.exports = async ( }) } - return res - .status(200) - .json(await GunActions.sendSpontaneousPayment(to, amt, memo, feeLimit)) + if (type === 'post' && typeof postID !== 'string') { + return res.status(400).json({ + field: 'postID', + errorMessage: `Send postID` + }) + } + + return res.status(200).json( + await GunActions.sendSpontaneousPayment(to, amt, memo, feeLimit, { + type, + postID + }) + ) } catch (e) { return res.status(500).json({ errorMessage: e.message