From ceafb2820dc759ab5d7684dc4a98cc4d3035e2b8 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 22 Mar 2022 06:46:03 -0700 Subject: [PATCH] Adding 'dataType' to P2WDB data, to distinguish between 'offer' and 'counter-offer' data --- bin/server.js | 4 +- src/controllers/rest-api/index.js | 4 ++ src/controllers/rest-api/p2wdb/controller.js | 74 ++++++++++++++++++++ src/controllers/rest-api/p2wdb/index.js | 62 ++++++++++++++++ src/use-cases/order.js | 3 + 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/controllers/rest-api/p2wdb/controller.js create mode 100644 src/controllers/rest-api/p2wdb/index.js diff --git a/bin/server.js b/bin/server.js index c58ae53..554376a 100644 --- a/bin/server.js +++ b/bin/server.js @@ -94,13 +94,13 @@ class Server { try { try { // 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) { /* exit quietly */ // 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') } catch (error) { console.log('Webhook cant be created') diff --git a/src/controllers/rest-api/index.js b/src/controllers/rest-api/index.js index 1724bdd..a5d5849 100644 --- a/src/controllers/rest-api/index.js +++ b/src/controllers/rest-api/index.js @@ -14,6 +14,7 @@ const LogsRESTController = require('./logs') const EntryRouter = require('./entry') const OfferRouter = require('./offer') const OrderRouter = require('./order') +const P2WDBRouter = require('./p2wdb') class RESTControllers { constructor (localConfig = {}) { @@ -65,6 +66,9 @@ class RESTControllers { const orderRouter = new OrderRouter(dependencies) orderRouter.attach(app) + + const p2wdbRouter = new P2WDBRouter(dependencies) + p2wdbRouter.attach(app) } } diff --git a/src/controllers/rest-api/p2wdb/controller.js b/src/controllers/rest-api/p2wdb/controller.js new file mode 100644 index 0000000..e9440ca --- /dev/null +++ b/src/controllers/rest-api/p2wdb/controller.js @@ -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 diff --git a/src/controllers/rest-api/p2wdb/index.js b/src/controllers/rest-api/p2wdb/index.js new file mode 100644 index 0000000..a105404 --- /dev/null +++ b/src/controllers/rest-api/p2wdb/index.js @@ -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 diff --git a/src/use-cases/order.js b/src/use-cases/order.js index a5dc392..d1fab88 100644 --- a/src/use-cases/order.js +++ b/src/use-cases/order.js @@ -52,6 +52,9 @@ class OrderLib { orderEntity.utxoTxid = utxoInfo.txid orderEntity.utxoVout = utxoInfo.vout + // Add P2WDB specific flag for signaling that this is a new offer. + orderEntity.dataType = 'offer' + // Add order to P2WDB. const p2wdbObj = { wif: this.adapters.wallet.bchWallet.walletInfo.privateKey,