spont payment fix, generate orderAddress by socket

This commit is contained in:
hatim boufnichel 2020-08-20 18:08:02 +02:00
parent 3d59d22323
commit c67a9d7790
3 changed files with 130 additions and 39 deletions

View file

@ -509,6 +509,7 @@ class Mediator {
Action.GENERATE_NEW_HANDSHAKE_NODE, Action.GENERATE_NEW_HANDSHAKE_NODE,
this.generateHandshakeNode this.generateHandshakeNode
) )
this.socket.on('GENERATE_ORDER_ADDRESS', this.generateOrderAddress)
this.socket.on(Action.SEND_HANDSHAKE_REQUEST, this.sendHandshakeRequest) this.socket.on(Action.SEND_HANDSHAKE_REQUEST, this.sendHandshakeRequest)
this.socket.on( this.socket.on(
Action.SEND_HANDSHAKE_REQUEST_WITH_INITIAL_MSG, Action.SEND_HANDSHAKE_REQUEST_WITH_INITIAL_MSG,
@ -745,6 +746,32 @@ class Mediator {
} }
} }
/**
* @param {Readonly<{ token: string }>} body
*/
generateOrderAddress = async body => {
try {
const { token } = body
await throwOnInvalidToken(token)
await API.Actions.generateOrderAddress(user)
this.socket.emit('GENERATE_ORDER_ADDRESS', {
ok: true,
msg: null,
origBody: body
})
} catch (err) {
logger.info(err)
this.socket.emit('GENERATE_ORDER_ADDRESS', {
ok: false,
msg: err.message,
origBody: body
})
}
}
/** /**
* @param {Readonly<{ recipientPublicKey: string , token: string }>} body * @param {Readonly<{ recipientPublicKey: string , token: string }>} body
*/ */
@ -861,15 +888,20 @@ class Mediator {
} }
/** /**
* @param {Readonly<{ uuid: string, recipientPub: string, amount: number, memo: string, token: string }>} reqBody * @param {Readonly<{ uuid: string, recipientPub: string, amount: number, memo: string, token: string, feeLimit:number }>} reqBody
*/ */
sendPayment = async reqBody => { sendPayment = async reqBody => {
try { try {
const { recipientPub, amount, memo, token } = reqBody const { recipientPub, amount, memo, feeLimit, token } = reqBody
await throwOnInvalidToken(token) await throwOnInvalidToken(token)
const preimage = await API.Actions.sendPayment(recipientPub, amount, memo) const preimage = await API.Actions.sendPayment(
recipientPub,
amount,
memo,
feeLimit
)
this.socket.emit(Action.SEND_PAYMENT, { this.socket.emit(Action.SEND_PAYMENT, {
ok: true, ok: true,

View file

@ -902,11 +902,21 @@ const sendHRWithInitialMsg = async (
* @param {string} to * @param {string} to
* @param {number} amount * @param {number} amount
* @param {string} memo * @param {string} memo
* @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) => { const sendPayment = async (
to,
amount,
memo,
feeLimit,
maxParts = 3,
timeoutSeconds = 5
) => {
try { try {
const SEA = require('../Mediator').mySEA const SEA = require('../Mediator').mySEA
const getUser = () => require('../Mediator').getUser() const getUser = () => require('../Mediator').getUser()
@ -1037,7 +1047,7 @@ const sendPayment = async (to, amount, memo) => {
} }
const { const {
services: { lightning } services: { router }
} = LightningServices } = LightningServices
/** /**
@ -1047,50 +1057,46 @@ const sendPayment = async (to, amount, memo) => {
/** /**
* Partial * Partial
* https://api.lightning.community/#grpc-response-sendresponse-2 * https://api.lightning.community/#sendpaymentv2
* @typedef {object} SendResponse * @typedef {object} SendResponse
* @prop {string|null} payment_error * @prop {string} failure_reason
* @prop {any[]|null} payment_route
* @prop {string} payment_preimage * @prop {string} payment_preimage
*/ */
logger.info('Will now send payment through lightning') logger.info('Will now send payment through lightning')
const sendPaymentSyncArgs = { const sendPaymentV2Args = {
/** @type {string} */ /** @type {string} */
payment_request: orderResponse.response payment_request: orderResponse.response,
max_parts: maxParts,
timeout_seconds: timeoutSeconds,
no_inflight_updates: true,
fee_limit_sat: feeLimit
} }
/** @type {string} */ const preimage = await new Promise((res, rej) => {
const preimage = await new Promise((resolve, rej) => { const sentPaymentStream = router.sendPaymentV2(sendPaymentV2Args)
lightning.sendPaymentSync(sendPaymentSyncArgs, ( /**
/** @type {SendErr=} */ err, * @param {SendResponse} response
/** @type {SendResponse} */ res */
) => { const dataCB = response => {
if (err) { logger.info('SendPayment Data:', response)
rej(new Error(err.details)) if (response.failure_reason !== 'FAILURE_REASON_NONE') {
} else if (res) { rej(new Error(response.failure_reason))
if (res.payment_error) {
rej(
new Error(
`sendPaymentSync error response: ${JSON.stringify(res)}`
)
)
} else if (!res.payment_route || !res.payment_preimage) {
rej(
new Error(
`sendPaymentSync no payment route response or preimage: ${JSON.stringify(
res
)}`
)
)
} else { } else {
resolve(res.payment_preimage) res(response.payment_preimage)
} }
} else {
rej(new Error('no error or response received from sendPaymentSync'))
} }
}) sentPaymentStream.on('data', dataCB)
/**
*
* @param {SendErr} err
*/
const errCB = err => {
logger.error('SendPayment Error:', err)
rej(err.details)
}
sentPaymentStream.on('error', errCB)
}) })
if (Utils.successfulHandshakeAlreadyExists(to)) { if (Utils.successfulHandshakeAlreadyExists(to)) {

View file

@ -1488,7 +1488,13 @@ module.exports = async (
const sentPayment = router.sendPaymentV2(paymentRequest) const sentPayment = router.sendPaymentV2(paymentRequest)
sentPayment.on('data', response => { sentPayment.on('data', response => {
logger.info('SendPayment Data:', response) logger.info('SendPayment Data:', response)
if (response.failure_reason !== 'FAILURE_REASON_NONE') {
res.status(500).json({
errorMessage: response.failure_reason
})
} else {
res.json(response) res.json(response)
}
}) })
sentPayment.on('status', status => { sentPayment.on('status', status => {
@ -1993,6 +1999,53 @@ module.exports = async (
}) })
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
app.post(`/api/gun/sendpayment`, async (req, res) => {
const {
recipientPub,
amount,
memo,
maxParts,
timeoutSeconds,
feeLimit,
sessionUuid
} = req.body
logger.info('handling spont pay')
if (!feeLimit) {
logger.error(
'please provide a "feeLimit" to the send spont payment request'
)
return res.status(500).json({
errorMessage:
'please provide a "feeLimit" to the send spont payment request'
})
}
if (!recipientPub || !amount) {
logger.info(
'please provide a "recipientPub" and "amount" to the send spont payment request'
)
return res.status(500).json({
errorMessage:
'please provide a "recipientPub" and "amount" to the send spont payment request'
})
}
try {
const preimage = await GunActions.sendPayment(
recipientPub,
amount,
memo,
feeLimit,
maxParts,
timeoutSeconds
)
res.json({ preimage, sessionUuid })
} catch (err) {
logger.info('spont pay err:', err)
return res.status(500).json({
errorMessage: err.message
})
}
})
app.get(`/api/gun/wall/:publicKey?`, async (req, res) => { app.get(`/api/gun/wall/:publicKey?`, async (req, res) => {
try { try {
const { page } = req.query const { page } = req.query