Correct typings and parameters for spontaneous payments

This commit is contained in:
Daniel Lugo 2022-01-29 15:41:40 -05:00
parent cecf6a9d2e
commit fffe1313fd

View file

@ -306,8 +306,10 @@ const setCurrentStreamInfo = (encryptedCurrentStreamInfo, user) =>
/** /**
* @typedef {object} SpontaneousPaymentOptions * @typedef {object} SpontaneousPaymentOptions
* @prop {Common.Schema.OrderTargetType} type * @prop {Common.Schema.OrderTargetType=} type
* @prop {string=} ackInfo * @prop {string=} ackInfo
* @prop {number=} maxParts
* @prop {number=} timeoutSeconds
*/ */
/** /**
* @typedef {object} OrderRes * @typedef {object} OrderRes
@ -330,7 +332,7 @@ const sendSpontaneousPayment = async (
amount, amount,
memo, memo,
feeLimit, feeLimit,
opts = { type: 'spontaneousPayment' } { ackInfo, maxParts, timeoutSeconds, type = 'spontaneousPayment' }
) => { ) => {
try { try {
const SEA = require('../Mediator').mySEA const SEA = require('../Mediator').mySEA
@ -338,12 +340,12 @@ const sendSpontaneousPayment = async (
const myPub = getUser()._.sea.pub const myPub = getUser()._.sea.pub
if ( if (
to === myPub && to === myPub &&
opts.type === 'torrentSeed' && type === 'torrentSeed' &&
opts.ackInfo && ackInfo &&
!isNaN(parseInt(opts.ackInfo, 10)) !isNaN(parseInt(ackInfo, 10))
) { ) {
//user requested a seed to themselves //user requested a seed to themselves
const numberOfTokens = Number(opts.ackInfo) || 1 const numberOfTokens = Number(ackInfo) || 1
const seedInfo = selfContentToken() const seedInfo = selfContentToken()
if (!seedInfo) { if (!seedInfo) {
throw new Error('torrentSeed service not available') throw new Error('torrentSeed service not available')
@ -375,8 +377,8 @@ const sendSpontaneousPayment = async (
from: getUser()._.sea.pub, from: getUser()._.sea.pub,
memo: memo || 'no memo', memo: memo || 'no memo',
timestamp: Date.now(), timestamp: Date.now(),
targetType: opts.type, targetType: type,
ackInfo: opts.ackInfo ackInfo
} }
logger.info(JSON.stringify(order)) logger.info(JSON.stringify(order))
@ -496,18 +498,20 @@ const sendSpontaneousPayment = async (
const payment = await sendPaymentV2Invoice({ const payment = await sendPaymentV2Invoice({
feeLimit, feeLimit,
payment_request: orderResponse.response payment_request: orderResponse.response,
max_parts: maxParts,
timeoutSeconds
}) })
const myLndPub = LNDHealthManager.lndPub const myLndPub = LNDHealthManager.lndPub
if ( if (
(opts.type !== 'contentReveal' && (type !== 'contentReveal' &&
opts.type !== 'torrentSeed' && type !== 'torrentSeed' &&
opts.type !== 'service' && type !== 'service' &&
opts.type !== 'product') || type !== 'product') ||
!orderResponse.ackNode !orderResponse.ackNode
) { ) {
SchemaManager.AddOrder({ SchemaManager.AddOrder({
type: opts.type, type,
amount: parseInt(payment.value_sat, 10), amount: parseInt(payment.value_sat, 10),
coordinateHash: payment.payment_hash, coordinateHash: payment.payment_hash,
coordinateIndex: parseInt(payment.payment_index, 10), coordinateIndex: parseInt(payment.payment_index, 10),
@ -580,7 +584,7 @@ const sendSpontaneousPayment = async (
throw new Error(`expected orderAck response, got: ${orderAck.type}`) throw new Error(`expected orderAck response, got: ${orderAck.type}`)
} }
SchemaManager.AddOrder({ SchemaManager.AddOrder({
type: opts.type, type,
amount: parseInt(payment.value_sat, 10), amount: parseInt(payment.value_sat, 10),
coordinateHash: payment.payment_hash, coordinateHash: payment.payment_hash,
coordinateIndex: parseInt(payment.payment_index, 10), coordinateIndex: parseInt(payment.payment_index, 10),
@ -606,12 +610,24 @@ const sendSpontaneousPayment = async (
* @param {number} amount * @param {number} amount
* @param {string} memo * @param {string} memo
* @param {number} feeLimit * @param {number} feeLimit
* @param {number=} maxParts
* @param {number=} timeoutSeconds
* @throws {Error} If no response in less than 20 seconds from the recipient, or * @throws {Error} If no response in less than 20 seconds from the recipient, or
* lightning cannot find a route for the payment. * lightning cannot find a route for the payment.
* @returns {Promise<string>} The payment's preimage. * @returns {Promise<string>} The payment's preimage.
*/ */
const sendPayment = async (to, amount, memo, feeLimit) => { const sendPayment = async (
const res = await sendSpontaneousPayment(to, amount, memo, feeLimit) to,
amount,
memo,
feeLimit,
maxParts,
timeoutSeconds
) => {
const res = await sendSpontaneousPayment(to, amount, memo, feeLimit, {
maxParts,
timeoutSeconds
})
if (!res.payment) { if (!res.payment) {
throw new Error('invalid payment params') //only if it's a torrentSeed request to self throw new Error('invalid payment params') //only if it's a torrentSeed request to self
} }