From 71a6de03b32b9edfb433bd7c1ce647573e174f39 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 10 Apr 2024 11:05:46 -0700 Subject: [PATCH] feat(/bch/service): GET endpoint to get ipfs-bch-wallet-service consumed by this app --- src/adapters/ipfs/ipfs-coord.js | 9 ++++++- src/controllers/rest-api/bch/controller.js | 27 +++++++++++++++++++++ src/controllers/rest-api/bch/index.js | 5 ++++ src/controllers/rest-api/ipfs/controller.js | 14 +++++------ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/adapters/ipfs/ipfs-coord.js b/src/adapters/ipfs/ipfs-coord.js index 86ae74d..2074c70 100644 --- a/src/adapters/ipfs/ipfs-coord.js +++ b/src/adapters/ipfs/ipfs-coord.js @@ -286,6 +286,10 @@ class IpfsCoordAdapter { _this.config.preferredProvider && thisPeer === _this.config.preferredProvider ) { + if(_this.state.selectedServiceProvider !== thisPeer) { + console.log(`---->BCH wallet service switched to preferred peer: ${thisPeer}`) + } + _this.state.selectedServiceProvider = thisPeer } @@ -372,8 +376,11 @@ class IpfsCoordAdapter { _this.config.preferredIpfsFileProvider && thisPeer === _this.config.preferredIpfsFileProvider ) { + if(_this.state.selectedIpfsFileProvider !== thisPeer) { + console.log(`---->IPFS File service switched to preferred peer: ${thisPeer}`) + } + _this.state.selectedIpfsFileProvider = thisPeer - console.log(`---->IPFS File service switched to preferred peer: ${thisPeer}`) } // console.log('selectedServiceProvider: ', _this.state.selectedServiceProvider) diff --git a/src/controllers/rest-api/bch/controller.js b/src/controllers/rest-api/bch/controller.js index ebccfa0..0e00412 100644 --- a/src/controllers/rest-api/bch/controller.js +++ b/src/controllers/rest-api/bch/controller.js @@ -723,6 +723,33 @@ class BchRESTControllerLib { } } + /** + * @api {get} /bch/service Get the IPFS ID for the Wallet service + * @apiPermission public + * @apiName getService + * @apiGroup REST BCH + * @apiDescription Get the IPFS ID for the ipfs-bch-wallet-service node that + * this app is using to retrieve blockchain data from. + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X GET localhost:5015/bch/service + * + */ + async getService (ctx) { + try { + const selectedServiceProvider = this.adapters.ipfs.ipfsCoordAdapter.state.selectedServiceProvider + + ctx.body = { + success: true, + selectedServiceProvider + } + } catch (err) { + // wlogger.error('Error in ipfs/controller.js/viewFile(): ', err) + console.log('Error in ipfs/controller.js/getService(): ', err) + this.handleError(ctx, err) + } + } + // DRY error handler handleError (ctx, err) { // If an HTTP status is specified by the buisiness logic, use that. diff --git a/src/controllers/rest-api/bch/index.js b/src/controllers/rest-api/bch/index.js index 4a80b40..4958087 100644 --- a/src/controllers/rest-api/bch/index.js +++ b/src/controllers/rest-api/bch/index.js @@ -63,6 +63,7 @@ class BchRouter { this.router.post('/utxoIsValid', this.utxoIsValid) this.router.post('/getTokenData', this.getTokenData) this.router.post('/getTokenData2', this.getTokenData2) + this.router.get('/service', this.getService) // Attach the Controller routes to the Koa app. app.use(this.router.routes()) @@ -116,6 +117,10 @@ class BchRouter { async getTokenData2 (ctx, next) { await _this.bchRESTController.getTokenData2(ctx, next) } + + async getService (ctx, next) { + await _this.bchRESTController.getService(ctx, next) + } } // module.exports = BchRouter diff --git a/src/controllers/rest-api/ipfs/controller.js b/src/controllers/rest-api/ipfs/controller.js index 8ceb542..0aede4e 100644 --- a/src/controllers/rest-api/ipfs/controller.js +++ b/src/controllers/rest-api/ipfs/controller.js @@ -43,7 +43,7 @@ class IpfsRESTControllerLib { * @api {get} /ipfs Get status on IPFS infrastructure * @apiPermission public * @apiName GetIpfsStatus - * @apiGroup REST BCH + * @apiGroup REST IPFS * * @apiExample Example usage: * curl -H "Content-Type: application/json" -X GET localhost:5001/ipfs @@ -110,7 +110,7 @@ class IpfsRESTControllerLib { * @api {get} /ipfs/node Get a copy of the thisNode object from helia-coord * @apiPermission public * @apiName GetThisNode - * @apiGroup REST BCH + * @apiGroup REST IPFS * * @apiExample Example usage: * curl -H "Content-Type: application/json" -X GET localhost:5001/ipfs/node @@ -132,7 +132,7 @@ class IpfsRESTControllerLib { * @api {get} /ipfs/view/:cid Retrieve and display a file via its IPFS CID * @apiPermission public * @apiName GetCidView - * @apiGroup REST BCH + * @apiGroup REST IPFS * * @apiExample Example usage: * curl -H "Content-Type: application/json" -X GET localhost:5001/ipfs/view/bafkreieaqtdhfywyddomswogynzymukosqqgqo7lkt5lch2zwfnc55m6om @@ -172,14 +172,14 @@ class IpfsRESTControllerLib { * @api {get} /ipfs/service Get the IPFS ID for the File Pin service * @apiPermission public * @apiName GetService - * @apiGroup REST BCH + * @apiGroup REST IPFS + * @apiDescription Get the IPFS ID for the ipfs-file-pin-service node that + * this app is using to retrieve file data from. * * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X GET localhost:5020/ipfs/service + * curl -H "Content-Type: application/json" -X GET localhost:5015/ipfs/service * */ - // Reports the ipfs-file-pin-service node that this app is using to retrieve - // file information. async getService (ctx) { try { const selectedIpfsFileProvider = this.adapters.ipfs.ipfsCoordAdapter.state.selectedIpfsFileProvider