From 98dd904fa579cf7706f56e178dd64d7c5633620a Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 10 Apr 2024 10:52:07 -0700 Subject: [PATCH] feat(/ipfs/service): GET endpoint to retrieve File Pin service used by this app --- src/adapters/ipfs-files/index.js | 93 --------------------- src/adapters/ipfs/ipfs-coord.js | 1 + src/controllers/rest-api/ipfs/controller.js | 28 +++++++ src/controllers/rest-api/ipfs/index.js | 1 + 4 files changed, 30 insertions(+), 93 deletions(-) diff --git a/src/adapters/ipfs-files/index.js b/src/adapters/ipfs-files/index.js index 543b03b..bc1e92a 100644 --- a/src/adapters/ipfs-files/index.js +++ b/src/adapters/ipfs-files/index.js @@ -176,99 +176,6 @@ class IpfsFilesAdapter { } } - // Read an entry from the P2WDB, given an entry hash. - async getEntryByHash (hash) { - try { - // Input validation. - if (!hash || typeof hash !== 'string') { - throw new Error('getEntry() input hash must be a string.') - } - - // Throw an error if this IPFS node has not yet made a connection to a - // wallet service provider. - const selectedProvider = - this.ipfs.ipfsCoordAdapter.state.selectedP2wdbProvider - if (!selectedProvider) { - throw new Error('No P2WDB Service provider available yet.') - } - - const rpcData = { - endpoint: 'getByHash', - hash - } - - // Generate a UUID for the call. - const rpcId = this.uid() - - // Generate a JSON RPC command. - const cmd = this.jsonrpc.request(rpcId, 'p2wdb', rpcData) - const cmdStr = JSON.stringify(cmd) - // console.log('cmdStr: ', cmdStr) - - // Send the RPC command to selected wallet service. - const thisNode = this.ipfs.ipfsCoordAdapter.ipfsCoord.thisNode - await this.ipfs.ipfsCoordAdapter.ipfsCoord.useCases.peer.sendPrivateMessage( - selectedProvider, - cmdStr, - thisNode - ) - - // Wait for data to come back from the wallet service. - const data = await this.waitForRPCResponse(rpcId) - - return data - } catch (err) { - console.error('Error in getEntryByHash()') - throw err - } - } - - async writeEntry (writeObj) { - try { - const { txid, signature, message, data } = writeObj - - // Throw an error if this IPFS node has not yet made a connection to a - // wallet service provider. - const selectedProvider = - this.ipfs.ipfsCoordAdapter.state.selectedP2wdbProvider - if (!selectedProvider) { - throw new Error('No P2WDB Service provider available yet.') - } - - const rpcData = { - endpoint: 'write', - txid, - signature, - message, - data - } - - // Generate a UUID for the call. - const rpcId = this.uid() - - // Generate a JSON RPC command. - const cmd = this.jsonrpc.request(rpcId, 'p2wdb', rpcData) - const cmdStr = JSON.stringify(cmd) - // console.log('cmdStr: ', cmdStr) - - // Send the RPC command to selected wallet service. - const thisNode = this.ipfs.ipfsCoordAdapter.ipfsCoord.thisNode - await this.ipfs.ipfsCoordAdapter.ipfsCoord.useCases.peer.sendPrivateMessage( - selectedProvider, - cmdStr, - thisNode - ) - - // Wait for data to come back from the wallet service. - const result = await this.waitForRPCResponse(rpcId) - - return result - } catch (err) { - console.error('Error in adapters/p2wdb/writeEntry()') - throw err - } - } - // Returns a promise that resolves to data when the RPC response is recieved. async waitForRPCResponse (rpcId) { try { diff --git a/src/adapters/ipfs/ipfs-coord.js b/src/adapters/ipfs/ipfs-coord.js index e07b025..86ae74d 100644 --- a/src/adapters/ipfs/ipfs-coord.js +++ b/src/adapters/ipfs/ipfs-coord.js @@ -373,6 +373,7 @@ class IpfsCoordAdapter { thisPeer === _this.config.preferredIpfsFileProvider ) { _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/ipfs/controller.js b/src/controllers/rest-api/ipfs/controller.js index 22e6618..8ceb542 100644 --- a/src/controllers/rest-api/ipfs/controller.js +++ b/src/controllers/rest-api/ipfs/controller.js @@ -36,6 +36,7 @@ class IpfsRESTControllerLib { this.connect = this.connect.bind(this) this.getThisNode = this.getThisNode.bind(this) this.viewFile = this.viewFile.bind(this) + this.getService = this.getService.bind(this) } /** @@ -167,6 +168,33 @@ class IpfsRESTControllerLib { } } + /** + * @api {get} /ipfs/service Get the IPFS ID for the File Pin service + * @apiPermission public + * @apiName GetService + * @apiGroup REST BCH + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X GET localhost:5020/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 + + ctx.body = { + success: true, + selectedIpfsFileProvider + } + } 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/ipfs/index.js b/src/controllers/rest-api/ipfs/index.js index 842dacc..6f0c3ab 100644 --- a/src/controllers/rest-api/ipfs/index.js +++ b/src/controllers/rest-api/ipfs/index.js @@ -57,6 +57,7 @@ class IpfsRouter { this.router.post('/connect', this.ipfsRESTController.connect) this.router.get('/node', this.ipfsRESTController.getThisNode) this.router.get('/view/:cid', this.ipfsRESTController.viewFile) + this.router.get('/service', this.ipfsRESTController.getService) // Attach the Controller routes to the Koa app. app.use(this.router.routes())