Merge pull request #298 from shocknet/basic-coordinates
Basic coordinates
This commit is contained in:
commit
a339d1058c
7 changed files with 231 additions and 124 deletions
|
|
@ -51,7 +51,7 @@
|
|||
"request-promise": "^4.2.6",
|
||||
"response-time": "^2.3.2",
|
||||
"shelljs": "^0.8.2",
|
||||
"shock-common": "29.1.0",
|
||||
"shock-common": "30.0.0",
|
||||
"socket.io": "2.1.1",
|
||||
"text-encoding": "^0.7.0",
|
||||
"tingodb": "^0.6.1",
|
||||
|
|
|
|||
63
services/coordinates.js
Normal file
63
services/coordinates.js
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
* @format
|
||||
*/
|
||||
|
||||
const Common = require('shock-common')
|
||||
const mapValues = require('lodash/mapValues')
|
||||
const pickBy = require('lodash/pickBy')
|
||||
const Bluebird = require('bluebird')
|
||||
const Logger = require('winston')
|
||||
const Key = require('../services/gunDB/contact-api/key')
|
||||
|
||||
const { getUser, getMySecret, mySEA } = require('./gunDB/Mediator')
|
||||
|
||||
/**
|
||||
* @param {string} coordID
|
||||
* @param {Common.Coordinate} data
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export const writeCoordinate = async (coordID, data) => {
|
||||
if (coordID !== data.id) {
|
||||
throw new Error('CoordID must be equal to data.id')
|
||||
}
|
||||
|
||||
try {
|
||||
/**
|
||||
* Because there are optional properties, typescript can also allow them
|
||||
* to be specified but with a value of `undefined`. Filter out these.
|
||||
* @type {Record<string, number|boolean|string>}
|
||||
*/
|
||||
const sanitizedData = pickBy(data, v => typeof v !== 'undefined')
|
||||
|
||||
const encData = await Bluebird.props(
|
||||
mapValues(sanitizedData, v => {
|
||||
return mySEA.encrypt(v, getMySecret())
|
||||
})
|
||||
)
|
||||
|
||||
getUser()
|
||||
.get(Key.COORDINATES)
|
||||
.get(coordID)
|
||||
.put(encData, ack => {
|
||||
if (ack.err && typeof ack.err !== 'number') {
|
||||
Logger.info(
|
||||
`Error writting corrdinate, coordinate id: ${coordID}, data: ${JSON.stringify(
|
||||
data,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
)
|
||||
Logger.error(ack.err)
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
Logger.info(
|
||||
`Error writing coordinate, coordinate id: ${coordID}, data: ${JSON.stringify(
|
||||
data,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
)
|
||||
Logger.error(e.message)
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,8 @@ const { ErrorCode } = Constants
|
|||
|
||||
const {
|
||||
sendPaymentV2Invoice,
|
||||
decodePayReq
|
||||
decodePayReq,
|
||||
myLNDPub
|
||||
} = require('../../../utils/lightningServices/v2')
|
||||
|
||||
/**
|
||||
|
|
@ -21,6 +22,7 @@ const {
|
|||
const Getters = require('./getters')
|
||||
const Key = require('./key')
|
||||
const Utils = require('./utils')
|
||||
const { writeCoordinate } = require('../../coordinates')
|
||||
|
||||
/**
|
||||
* @typedef {import('./SimpleGUN').GUNNode} GUNNode
|
||||
|
|
@ -1074,6 +1076,26 @@ const sendSpontaneousPayment = async (
|
|||
payment_request: orderResponse.response
|
||||
})
|
||||
|
||||
await writeCoordinate(payment.payment_hash, {
|
||||
id: payment.payment_hash,
|
||||
type: (() => {
|
||||
if (opts.type === 'post') {
|
||||
return 'tip'
|
||||
} else if (opts.type === 'user') {
|
||||
return 'spontaneousPayment'
|
||||
}
|
||||
// ensures we handle all possible types
|
||||
/** @type {never} */
|
||||
const assertNever = opts.type
|
||||
|
||||
return assertNever && opts.type // please TS
|
||||
})(),
|
||||
amount: Number(payment.value_sat),
|
||||
inbound: false,
|
||||
timestamp: Date.now(),
|
||||
toLndPub: await myLNDPub()
|
||||
})
|
||||
|
||||
return payment
|
||||
} catch (e) {
|
||||
logger.error('Error inside sendPayment()')
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
* @format
|
||||
*/
|
||||
// @ts-check
|
||||
const { performance } = require('perf_hooks')
|
||||
const logger = require('winston')
|
||||
const isFinite = require('lodash/isFinite')
|
||||
const isNumber = require('lodash/isNumber')
|
||||
|
|
@ -14,7 +13,11 @@ const {
|
|||
} = Common
|
||||
|
||||
const LightningServices = require('../../../../utils/lightningServices')
|
||||
|
||||
const {
|
||||
addInvoice,
|
||||
myLNDPub
|
||||
} = require('../../../../utils/lightningServices/v2')
|
||||
const { writeCoordinate } = require('../../../coordinates')
|
||||
const Key = require('../key')
|
||||
const Utils = require('../utils')
|
||||
|
||||
|
|
@ -56,28 +59,6 @@ const ordersProcessed = new Set()
|
|||
|
||||
let currentOrderAddr = ''
|
||||
|
||||
/**
|
||||
* @param {InvoiceRequest} invoiceReq
|
||||
* @returns {Promise<InvoiceResponse>}
|
||||
*/
|
||||
const _addInvoice = invoiceReq =>
|
||||
new Promise((resolve, rej) => {
|
||||
const {
|
||||
services: { lightning }
|
||||
} = LightningServices
|
||||
|
||||
lightning.addInvoice(invoiceReq, (
|
||||
/** @type {any} */ error,
|
||||
/** @type {InvoiceResponse} */ response
|
||||
) => {
|
||||
if (error) {
|
||||
rej(error)
|
||||
} else {
|
||||
resolve(response)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {string} addr
|
||||
* @param {ISEA} SEA
|
||||
|
|
@ -104,8 +85,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
return
|
||||
}
|
||||
|
||||
const listenerStartTime = performance.now()
|
||||
|
||||
ordersProcessed.add(orderID)
|
||||
|
||||
logger.info(
|
||||
|
|
@ -114,8 +93,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
)} -- addr: ${addr}`
|
||||
)
|
||||
|
||||
const orderAnswerStartTime = performance.now()
|
||||
|
||||
const alreadyAnswered = await getUser()
|
||||
.get(Key.ORDER_TO_RESPONSE)
|
||||
.get(orderID)
|
||||
|
|
@ -126,12 +103,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
return
|
||||
}
|
||||
|
||||
const orderAnswerEndTime = performance.now() - orderAnswerStartTime
|
||||
|
||||
logger.info(`[PERF] Order Already Answered: ${orderAnswerEndTime}ms`)
|
||||
|
||||
const decryptStartTime = performance.now()
|
||||
|
||||
const senderEpub = await Utils.pubToEpub(order.from)
|
||||
const secret = await SEA.secret(senderEpub, getUser()._.sea)
|
||||
|
||||
|
|
@ -140,10 +111,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
SEA.decrypt(order.memo, secret)
|
||||
])
|
||||
|
||||
const decryptEndTime = performance.now() - decryptStartTime
|
||||
|
||||
logger.info(`[PERF] Decrypt invoice info: ${decryptEndTime}ms`)
|
||||
|
||||
const amount = Number(decryptedAmount)
|
||||
|
||||
if (!isNumber(amount)) {
|
||||
|
|
@ -175,26 +142,19 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
`onOrders() -> Will now create an invoice : ${JSON.stringify(invoiceReq)}`
|
||||
)
|
||||
|
||||
const invoiceStartTime = performance.now()
|
||||
|
||||
const invoice = await _addInvoice(invoiceReq)
|
||||
|
||||
const invoiceEndTime = performance.now() - invoiceStartTime
|
||||
|
||||
logger.info(`[PERF] LND Invoice created in ${invoiceEndTime}ms`)
|
||||
const invoice = await addInvoice(
|
||||
invoiceReq.value,
|
||||
invoiceReq.memo,
|
||||
true,
|
||||
invoiceReq.expiry
|
||||
)
|
||||
|
||||
logger.info(
|
||||
'onOrders() -> Successfully created the invoice, will now encrypt it'
|
||||
)
|
||||
|
||||
const invoiceEncryptStartTime = performance.now()
|
||||
|
||||
const encInvoice = await SEA.encrypt(invoice.payment_request, secret)
|
||||
|
||||
const invoiceEncryptEndTime = performance.now() - invoiceEncryptStartTime
|
||||
|
||||
logger.info(`[PERF] Invoice encrypted in ${invoiceEncryptEndTime}ms`)
|
||||
|
||||
logger.info(
|
||||
`onOrders() -> Will now place the encrypted invoice in order to response usergraph: ${addr}`
|
||||
)
|
||||
|
|
@ -205,9 +165,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
type: 'invoice'
|
||||
}
|
||||
|
||||
const invoicePutStartTime = performance.now()
|
||||
|
||||
await new Promise((res, rej) => {
|
||||
await /** @type {Promise<void>} */ (new Promise((res, rej) => {
|
||||
getUser()
|
||||
.get(Key.ORDER_TO_RESPONSE)
|
||||
.get(orderID)
|
||||
|
|
@ -223,9 +181,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
res()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const invoicePutEndTime = performance.now() - invoicePutStartTime
|
||||
}))
|
||||
|
||||
// invoices should be settled right away so we can rely on this single
|
||||
// subscription instead of life-long all invoices subscription
|
||||
|
|
@ -234,24 +190,46 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
if (!Common.isPopulatedString(postID)) {
|
||||
throw new TypeError(`postID not a a populated string`)
|
||||
}
|
||||
|
||||
const { r_hash } = invoice
|
||||
}
|
||||
|
||||
// A post tip order lifecycle is short enough that we can do it like this.
|
||||
const stream = LightningServices.invoices.subscribeSingleInvoice({
|
||||
r_hash
|
||||
r_hash: invoice.r_hash
|
||||
})
|
||||
|
||||
/** @type {Common.Coordinate} */
|
||||
const coord = {
|
||||
amount,
|
||||
id: invoice.r_hash.toString(),
|
||||
inbound: true,
|
||||
timestamp: Date.now(),
|
||||
type: 'invoice',
|
||||
invoiceMemo: memo,
|
||||
fromGunPub: order.from,
|
||||
toGunPub: getUser()._.sea.pub,
|
||||
toLndPub: await myLNDPub()
|
||||
}
|
||||
|
||||
if (order.targetType === 'post') {
|
||||
coord.type = 'tip'
|
||||
} else {
|
||||
coord.type = 'spontaneousPayment'
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Common.InvoiceWhenListed} invoice
|
||||
*/
|
||||
const onData = invoice => {
|
||||
if (invoice.settled) {
|
||||
if (order.targetType === 'post') {
|
||||
getUser()
|
||||
.get('postToTipCount')
|
||||
.get(postID)
|
||||
// CAST: Checked above.
|
||||
.get(/** @type {string} */ (order.postID))
|
||||
.set(null) // each item in the set is a tip
|
||||
}
|
||||
|
||||
writeCoordinate(invoice.r_hash.toString(), coord)
|
||||
stream.off()
|
||||
}
|
||||
}
|
||||
|
|
@ -259,21 +237,14 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
|||
stream.on('data', onData)
|
||||
|
||||
stream.on('status', (/** @type {any} */ status) => {
|
||||
logger.info(`Post tip, post: ${postID}, invoice status:`, status)
|
||||
logger.info(`Post tip, post: ${order.postID}, invoice status:`, status)
|
||||
})
|
||||
stream.on('end', () => {
|
||||
logger.warn(`Post tip, post: ${postID}, invoice stream ended`)
|
||||
logger.warn(`Post tip, post: ${order.postID}, invoice stream ended`)
|
||||
})
|
||||
stream.on('error', (/** @type {any} */ e) => {
|
||||
logger.warn(`Post tip, post: ${postID}, error:`, e)
|
||||
logger.warn(`Post tip, post: ${order.postID}, error:`, e)
|
||||
})
|
||||
}
|
||||
|
||||
logger.info(`[PERF] Added invoice to GunDB in ${invoicePutEndTime}ms`)
|
||||
|
||||
const listenerEndTime = performance.now() - listenerStartTime
|
||||
|
||||
logger.info(`[PERF] Invoice generation completed in ${listenerEndTime}ms`)
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
`error inside onOrders, orderAddr: ${addr}, orderID: ${orderID}, order: ${JSON.stringify(
|
||||
|
|
|
|||
|
|
@ -62,3 +62,5 @@ exports.TOTAL_TIPS = 'totalTips'
|
|||
exports.PROFILE_BINARY = 'profileBinary'
|
||||
|
||||
exports.POSTS_NEW = 'posts'
|
||||
|
||||
exports.COORDINATES = 'coordinates'
|
||||
|
|
|
|||
|
|
@ -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,50 @@ const isValidSendPaymentInvoiceParams = sendPaymentInvoiceParams => {
|
|||
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 = () =>
|
||||
Common.makePromise((res, rej) => {
|
||||
const { lightning } = lightningServices.getServices()
|
||||
|
||||
lightning.getInfo({}, (err, data) => {
|
||||
if (err) {
|
||||
rej(new Error(err.message))
|
||||
} else {
|
||||
res(data.identity_pubkey)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* aklssjdklasd
|
||||
* @param {SendPaymentV2Request} sendPaymentRequest
|
||||
* @returns {Promise<PaymentV2>}
|
||||
*/
|
||||
const sendPaymentV2 = sendPaymentRequest => {
|
||||
const sendPaymentV2 = async sendPaymentRequest => {
|
||||
const {
|
||||
services: { router }
|
||||
} = lightningServices
|
||||
|
|
@ -229,7 +269,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 +311,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 +450,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
|
||||
* @returns {Promise<string>}
|
||||
|
|
@ -582,5 +630,6 @@ module.exports = {
|
|||
getChanInfo,
|
||||
listPeers,
|
||||
pendingChannels,
|
||||
addInvoice
|
||||
addInvoice,
|
||||
myLNDPub
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6282,10 +6282,10 @@ shellwords@^0.1.1:
|
|||
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
|
||||
integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==
|
||||
|
||||
shock-common@29.1.0:
|
||||
version "29.1.0"
|
||||
resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-29.1.0.tgz#3b6d8613fb7c73b8b76c98293a14ec168a9dc888"
|
||||
integrity sha512-O2tK+TShF3ioAdP4K33MB5QUDTmMqzz+pZe/HnSbi9q1DyX/zQ2Uluzol1NDE/6Z2SSnVFA7/2vJKGaCEdMKoQ==
|
||||
shock-common@30.0.0:
|
||||
version "30.0.0"
|
||||
resolved "https://registry.yarnpkg.com/shock-common/-/shock-common-30.0.0.tgz#86ee39d076fe48adf551265c55e012ab3201c8e9"
|
||||
integrity sha512-GgXCNOyk/iu0FBbYcfqOy7obX4RdEn3Q4y8R970CQVk7PvxYt0nZeQwKWe49esKb2B3JL1LOF/yJ7jg7tGze3w==
|
||||
dependencies:
|
||||
immer "^6.0.6"
|
||||
lodash "^4.17.19"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue