fixies
This commit is contained in:
parent
8b3f09473b
commit
c877c806b4
3 changed files with 69 additions and 56 deletions
|
|
@ -69,3 +69,5 @@ exports.COORDINATES = 'coordinates'
|
||||||
exports.COORDINATE_INDEX = 'coordinateIndex'
|
exports.COORDINATE_INDEX = 'coordinateIndex'
|
||||||
|
|
||||||
exports.TMP_CHAIN_COORDINATE = 'tmpChainCoordinate'
|
exports.TMP_CHAIN_COORDINATE = 'tmpChainCoordinate'
|
||||||
|
|
||||||
|
exports.DATE_COORDINATE_INDEX = 'dateCoordinateIndex'
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
const Crypto = require('crypto')
|
const Crypto = require('crypto')
|
||||||
const { Utils: CommonUtils } = require('shock-common')
|
const { Utils: CommonUtils } = require('shock-common')
|
||||||
const getGunUser = () => require('../gunDB/Mediator').getUser()
|
const getGunUser = () => require('../gunDB/Mediator').getUser()
|
||||||
const SEA = require('../gunDB/Mediator').mySEA
|
|
||||||
const Key = require('../gunDB/contact-api/key')
|
const Key = require('../gunDB/contact-api/key')
|
||||||
/**
|
/**
|
||||||
* @typedef {import('../gunDB/contact-api/SimpleGUN').ISEA} ISEA
|
* @typedef {import('../gunDB/contact-api/SimpleGUN').ISEA} ISEA
|
||||||
|
|
@ -104,10 +103,57 @@ const checkOrderInfo = order => {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {CoordinateOrder} orderInfo
|
||||||
|
* @param {string} coordinateSHA256
|
||||||
|
*/
|
||||||
|
const dateIndexCreateCb = (orderInfo, coordinateSHA256) => {
|
||||||
|
//if (this.memIndex) { need bind to use this here
|
||||||
|
//update date memIndex
|
||||||
|
//}
|
||||||
|
const date = new Date(orderInfo.timestamp || 0)
|
||||||
|
//use UTC for consistency?
|
||||||
|
const year = date.getUTCFullYear().toString()
|
||||||
|
const month = date.getUTCMonth().toString()
|
||||||
|
|
||||||
|
getGunUser()
|
||||||
|
.get(Key.DATE_COORDINATE_INDEX)
|
||||||
|
.get(year)
|
||||||
|
.get(month)
|
||||||
|
.set(coordinateSHA256)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if not provided, assume current month and year
|
||||||
|
* @param {number|null} year
|
||||||
|
* @param {number|null} month
|
||||||
|
*/
|
||||||
|
const getMonthCoordinates = async (year = null, month = null) => {
|
||||||
|
const now = Date.now()
|
||||||
|
//@ts-expect-error
|
||||||
|
const stringYear = year !== null ? year.toString() : now.getUTCFullYear().toString()
|
||||||
|
//@ts-expect-error
|
||||||
|
const stringMonth = month !== null ? month.toString() : now.getUTCMonth().toString()
|
||||||
|
|
||||||
|
const data = await new Promise(res => {
|
||||||
|
getGunUser()
|
||||||
|
.get(Key.DATE_COORDINATE_INDEX)
|
||||||
|
.get(stringYear)
|
||||||
|
.get(stringMonth)
|
||||||
|
.load(res)
|
||||||
|
})
|
||||||
|
const coordinatesArray = Object
|
||||||
|
.values(data)
|
||||||
|
.filter(coordinateSHA256 => typeof coordinateSHA256 === 'string')
|
||||||
|
|
||||||
|
return coordinatesArray
|
||||||
|
}
|
||||||
|
|
||||||
class SchemaManager {
|
class SchemaManager {
|
||||||
constructor({ memIndex = false }) {//config flag?
|
constructor(opts = { memIndex: false }) {//config flag?
|
||||||
this.memIndex = memIndex
|
this.memIndex = opts.memIndex
|
||||||
this.orderCreateIndexCallbacks.push(this.dateIndexCreateCb) //create more Cbs and put them here for more indexes callbacks
|
this.orderCreateIndexCallbacks.push(dateIndexCreateCb) //create more Cbs and put them here for more indexes callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
dateIndexName = 'dateIndex'
|
dateIndexName = 'dateIndex'
|
||||||
|
|
@ -133,7 +179,6 @@ class SchemaManager {
|
||||||
* @param {CoordinateOrder} orderInfo
|
* @param {CoordinateOrder} orderInfo
|
||||||
*/
|
*/
|
||||||
async AddOrder(orderInfo) {
|
async AddOrder(orderInfo) {
|
||||||
|
|
||||||
const checkErr = checkOrderInfo(orderInfo)
|
const checkErr = checkOrderInfo(orderInfo)
|
||||||
if (checkErr) {
|
if (checkErr) {
|
||||||
throw new Error(checkErr)
|
throw new Error(checkErr)
|
||||||
|
|
@ -160,6 +205,7 @@ class SchemaManager {
|
||||||
}
|
}
|
||||||
const orderString = JSON.stringify(filteredOrder)
|
const orderString = JSON.stringify(filteredOrder)
|
||||||
const mySecret = require('../gunDB/Mediator').getMySecret()
|
const mySecret = require('../gunDB/Mediator').getMySecret()
|
||||||
|
const SEA = require('../gunDB/Mediator').mySEA
|
||||||
const encryptedOrderString = await SEA.encrypt(orderString, mySecret)
|
const encryptedOrderString = await SEA.encrypt(orderString, mySecret)
|
||||||
const coordinatePub = filteredOrder.inbound ? filteredOrder.toLndPub : filteredOrder.fromLndPub
|
const coordinatePub = filteredOrder.inbound ? filteredOrder.toLndPub : filteredOrder.fromLndPub
|
||||||
const coordinate = `${coordinatePub}__${filteredOrder.coordinateIndex}__${filteredOrder.coordinateHash}`
|
const coordinate = `${coordinatePub}__${filteredOrder.coordinateIndex}__${filteredOrder.coordinateHash}`
|
||||||
|
|
@ -187,54 +233,7 @@ class SchemaManager {
|
||||||
this.orderCreateIndexCallbacks.forEach(cb => cb(filteredOrder, coordinateSHA256))
|
this.orderCreateIndexCallbacks.forEach(cb => cb(filteredOrder, coordinateSHA256))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {CoordinateOrder} orderInfo
|
|
||||||
* @param {string} coordinateSHA256
|
|
||||||
*/
|
|
||||||
dateIndexCreateCb(orderInfo, coordinateSHA256) {
|
|
||||||
if (this.memIndex) {
|
|
||||||
//update date memIndex
|
|
||||||
}
|
|
||||||
const date = new Date(orderInfo.timestamp || 0)
|
|
||||||
//use UTC for consistency?
|
|
||||||
const year = date.getUTCFullYear().toString()
|
|
||||||
const month = date.getUTCMonth().toString()
|
|
||||||
|
|
||||||
getGunUser()
|
|
||||||
.get(Key.COORDINATE_INDEX)
|
|
||||||
.get(this.dateIndexName)
|
|
||||||
.get(year)
|
|
||||||
.get(month)
|
|
||||||
.set(coordinateSHA256)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* if not provided, assume current month and year
|
|
||||||
* @param {number|null} year
|
|
||||||
* @param {number|null} month
|
|
||||||
*/
|
|
||||||
async getMonthCoordinates(year = null, month = null) {
|
|
||||||
const now = Date.now()
|
|
||||||
//@ts-expect-error
|
|
||||||
const stringYear = year !== null ? year.toString() : now.getUTCFullYear().toString()
|
|
||||||
//@ts-expect-error
|
|
||||||
const stringMonth = month !== null ? month.toString() : now.getUTCMonth().toString()
|
|
||||||
|
|
||||||
const data = await new Promise(res => {
|
|
||||||
getGunUser()
|
|
||||||
.get(Key.COORDINATE_INDEX)
|
|
||||||
.get(this.dateIndexName)
|
|
||||||
.get(stringYear)
|
|
||||||
.get(stringMonth)
|
|
||||||
.load(res)
|
|
||||||
})
|
|
||||||
const coordinatesArray = Object
|
|
||||||
.values(data)
|
|
||||||
.filter(coordinateSHA256 => typeof coordinateSHA256 === 'string')
|
|
||||||
|
|
||||||
return coordinatesArray
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* if not provided, assume current month and year
|
* if not provided, assume current month and year
|
||||||
|
|
@ -243,17 +242,15 @@ class SchemaManager {
|
||||||
* @returns {Promise<CoordinateOrder[]>} from newer to older
|
* @returns {Promise<CoordinateOrder[]>} from newer to older
|
||||||
*/
|
*/
|
||||||
async getMonthOrders(year = null, month = null) {
|
async getMonthOrders(year = null, month = null) {
|
||||||
const now = Date.now()
|
const now = new Date()
|
||||||
//@ts-expect-error
|
|
||||||
const intYear = year !== null ? year : now.getUTCFullYear()
|
const intYear = year !== null ? year : now.getUTCFullYear()
|
||||||
//@ts-expect-error
|
|
||||||
const intMonth = month !== null ? month : now.getUTCMonth()
|
const intMonth = month !== null ? month : now.getUTCMonth()
|
||||||
|
|
||||||
let coordinates = null
|
let coordinates = null
|
||||||
if (this.memIndex) {
|
if (this.memIndex) {
|
||||||
//get coordinates from this.memDateIndex
|
//get coordinates from this.memDateIndex
|
||||||
} else {
|
} else {
|
||||||
coordinates = await this.getMonthCoordinates(intYear, intMonth)
|
coordinates = await getMonthCoordinates(intYear, intMonth)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @type {CoordinateOrder[]}
|
* @type {CoordinateOrder[]}
|
||||||
|
|
@ -271,6 +268,7 @@ class SchemaManager {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const mySecret = require('../gunDB/Mediator').getMySecret()
|
const mySecret = require('../gunDB/Mediator').getMySecret()
|
||||||
|
const SEA = require('../gunDB/Mediator').mySEA
|
||||||
const decryptedString = await SEA.decrypt(encryptedOrderString, mySecret)
|
const decryptedString = await SEA.decrypt(encryptedOrderString, mySecret)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -317,6 +315,7 @@ class SchemaManager {
|
||||||
}
|
}
|
||||||
const orderString = JSON.stringify(filteredOrder)
|
const orderString = JSON.stringify(filteredOrder)
|
||||||
const mySecret = require('../gunDB/Mediator').getMySecret()
|
const mySecret = require('../gunDB/Mediator').getMySecret()
|
||||||
|
const SEA = require('../gunDB/Mediator').mySEA
|
||||||
const encryptedOrderString = await SEA.encrypt(orderString, mySecret)
|
const encryptedOrderString = await SEA.encrypt(orderString, mySecret)
|
||||||
|
|
||||||
const addressSHA256 = Crypto.createHash('SHA256')
|
const addressSHA256 = Crypto.createHash('SHA256')
|
||||||
|
|
@ -364,6 +363,7 @@ class SchemaManager {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
const mySecret = require('../gunDB/Mediator').getMySecret()
|
const mySecret = require('../gunDB/Mediator').getMySecret()
|
||||||
|
const SEA = require('../gunDB/Mediator').mySEA
|
||||||
const decryptedString = await SEA.decrypt(maybeData, mySecret)
|
const decryptedString = await SEA.decrypt(maybeData, mySecret)
|
||||||
if (typeof decryptedString !== 'string' || decryptedString === '') {
|
if (typeof decryptedString !== 'string' || decryptedString === '') {
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
|
|
@ -134,6 +134,17 @@ module.exports = (
|
||||||
stream.on('data', data => {
|
stream.on('data', data => {
|
||||||
logger.info('[SOCKET] New invoice data:', data)
|
logger.info('[SOCKET] New invoice data:', data)
|
||||||
emitEncryptedEvent({ eventName: 'invoice:new', data, socket })
|
emitEncryptedEvent({ eventName: 'invoice:new', data, socket })
|
||||||
|
if (!data.settled) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
SchemaManager.AddOrder({
|
||||||
|
type: 'invoice',
|
||||||
|
amount: parseInt(data.amt_paid_sat, 10),
|
||||||
|
coordinateHash: data.r_hash.toString('hex'),
|
||||||
|
coordinateIndex: parseInt(data.add_index, 10),
|
||||||
|
inbound: true,
|
||||||
|
toLndPub: data.payment_addr
|
||||||
|
})
|
||||||
})
|
})
|
||||||
stream.on('end', () => {
|
stream.on('end', () => {
|
||||||
logger.info('New invoice stream ended, starting a new one...')
|
logger.info('New invoice stream ended, starting a new one...')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue