Adding 'dataType' to P2WDB data, to distinguish between 'offer' and 'counter-offer' data

This commit is contained in:
Chris Troutner
2022-03-22 06:46:03 -07:00
parent 90d1e68dae
commit ceafb2820d
5 changed files with 145 additions and 2 deletions
+2 -2
View File
@@ -94,13 +94,13 @@ class Server {
try { try {
try { try {
// Delete an old webhook if it exists. // Delete an old webhook if it exists.
await webhookLib.deleteWebhook(`http://localhost:${config.port}/offer`) await webhookLib.deleteWebhook(`http://localhost:${config.port}/p2wdb`)
} catch (err) { } catch (err) {
/* exit quietly */ /* exit quietly */
// console.log('err deleting webhook: ', err) // console.log('err deleting webhook: ', err)
} }
await webhookLib.createWebhook(`http://localhost:${config.port}/offer`) await webhookLib.createWebhook(`http://localhost:${config.port}/p2wdb`)
console.log('Webhook created') console.log('Webhook created')
} catch (error) { } catch (error) {
console.log('Webhook cant be created') console.log('Webhook cant be created')
+4
View File
@@ -14,6 +14,7 @@ const LogsRESTController = require('./logs')
const EntryRouter = require('./entry') const EntryRouter = require('./entry')
const OfferRouter = require('./offer') const OfferRouter = require('./offer')
const OrderRouter = require('./order') const OrderRouter = require('./order')
const P2WDBRouter = require('./p2wdb')
class RESTControllers { class RESTControllers {
constructor (localConfig = {}) { constructor (localConfig = {}) {
@@ -65,6 +66,9 @@ class RESTControllers {
const orderRouter = new OrderRouter(dependencies) const orderRouter = new OrderRouter(dependencies)
orderRouter.attach(app) orderRouter.attach(app)
const p2wdbRouter = new P2WDBRouter(dependencies)
p2wdbRouter.attach(app)
} }
} }
@@ -0,0 +1,74 @@
/*
REST API Controller library for the /p2wdb route
This route handles incoming data from the P2WDB webhook, and routes the data
to the proper handler.
*/
// const { wlogger } = require('../../../adapters/wlogger')
let _this
class P2WDBRESTControllerLib {
constructor (localConfig = {}) {
// Dependency Injection.
this.adapters = localConfig.adapters
if (!this.adapters) {
throw new Error(
'Instance of Adapters library required when instantiating /p2wdb REST Controller.'
)
}
this.useCases = localConfig.useCases
if (!this.useCases) {
throw new Error(
'Instance of Use Cases library required when instantiating /p2wdb REST Controller.'
)
}
// Encapsulate dependencies
// this.OrderModel = this.adapters.localdb.Order
// this.userUseCases = this.useCases.user
_this = this
}
// No api-doc documentation because this wont be a public endpoint
async routeWebhook (ctx) {
try {
console.log('p2wdb REST API handler: body: ', ctx.request.body)
// const orderObj = ctx.request.body.order
//
// const hash = await _this.useCases.order.createOrder(orderObj)
//
// ctx.body = { hash }
ctx.body = {
success: true
}
} catch (err) {
// console.log(`err.message: ${err.message}`)
// console.log('err: ', err)
// ctx.throw(422, err.message)
_this.handleError(ctx, err)
}
}
// DRY error handler
handleError (ctx, err) {
console.log('err', err)
// If an HTTP status is specified by the buisiness logic, use that.
if (err.status) {
if (err.message) {
ctx.throw(err.status, err.message)
} else {
ctx.throw(err.status)
}
} else {
// By default use a 422 error if the HTTP status is not specified.
ctx.throw(422, err.message)
}
}
}
module.exports = P2WDBRESTControllerLib
+62
View File
@@ -0,0 +1,62 @@
/*
REST API library for /p2wdb route.
This route handles incoming data from the P2WDB webhook, and routes the data
to the proper handler.
*/
// Public npm libraries.
const Router = require('koa-router')
// Local libraries.
const P2WDBRESTControllerLib = require('./controller')
let _this
class P2WDBRouter {
constructor (localConfig = {}) {
// Dependency Injection.
this.adapters = localConfig.adapters
if (!this.adapters) {
throw new Error(
'Instance of Adapters library required when instantiating /p2wdb REST Controller.'
)
}
this.useCases = localConfig.useCases
if (!this.useCases) {
throw new Error(
'Instance of Use Cases library required when instantiating /p2wdb REST Controller.'
)
}
const dependencies = {
adapters: this.adapters,
useCases: this.useCases
}
// Encapsulate dependencies.
this.p2wdbRESTController = new P2WDBRESTControllerLib(dependencies)
// Instantiate the router and set the base route.
const baseUrl = '/p2wdb'
this.router = new Router({ prefix: baseUrl })
_this = this
}
attach (app) {
if (!app) {
throw new Error(
'Must pass app object when attached REST API controllers.'
)
}
// Define the routes and attach the controller.
this.router.post('/', _this.p2wdbRESTController.routeWebhook)
// Attach the Controller routes to the Koa app.
app.use(_this.router.routes())
app.use(_this.router.allowedMethods())
}
}
module.exports = P2WDBRouter
+3
View File
@@ -52,6 +52,9 @@ class OrderLib {
orderEntity.utxoTxid = utxoInfo.txid orderEntity.utxoTxid = utxoInfo.txid
orderEntity.utxoVout = utxoInfo.vout orderEntity.utxoVout = utxoInfo.vout
// Add P2WDB specific flag for signaling that this is a new offer.
orderEntity.dataType = 'offer'
// Add order to P2WDB. // Add order to P2WDB.
const p2wdbObj = { const p2wdbObj = {
wif: this.adapters.wallet.bchWallet.walletInfo.privateKey, wif: this.adapters.wallet.bchWallet.walletInfo.privateKey,