From 302da845aff920b49e420f6aa839b3b0ce72c4eb Mon Sep 17 00:00:00 2001 From: SunnyAjin Date: Wed, 15 Feb 2023 05:56:33 +0000 Subject: [PATCH] chaincode metadata changes added Signed-off-by: SunnyAjin --- app/platform/fabric/Proxy.ts | 27 +++++++++++++++++--- app/platform/fabric/gateway/FabricGateway.ts | 11 ++++++++ app/rest/platformroutes.ts | 16 ++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/platform/fabric/Proxy.ts b/app/platform/fabric/Proxy.ts index 65505a931..7b9a86d60 100644 --- a/app/platform/fabric/Proxy.ts +++ b/app/platform/fabric/Proxy.ts @@ -139,11 +139,11 @@ export class Proxy { } } // Sometime 'peers_by_org' property is not included in discover result - if(typeof node.ledger_height_low === 'undefined') + if (typeof node.ledger_height_low === 'undefined') node.ledger_height_low = '-'; - if(typeof node.ledger_height_high === 'undefined') + if (typeof node.ledger_height_high === 'undefined') node.ledger_height_high = '-'; - if(typeof node.ledger_height_unsigned === 'undefined') + if (typeof node.ledger_height_unsigned === 'undefined') node.ledger_height_unsigned = '-'; peers.push(node); } else if (node.peer_type === 'ORDERER') { @@ -391,4 +391,25 @@ export class Proxy { ); } } + + /** + * + * @param {*} contract_name + * @returns + * @memberof Proxy + */ + async getContractMetadata(network_id, contract_name) { + const client = this.platform.getClient(network_id); + const channel_name = Object.keys(client.fabricGateway.config.channels)[0]; + let metadata; + try { + metadata = await client.fabricGateway.queryContractMetadata(channel_name, contract_name); + } catch (e) { + logger.debug('getContractMetadata >> ', e); + } if (metadata) { + return metadata; + } + logger.error('response_payloads is null'); + return 'response_payloads is null'; + } } diff --git a/app/platform/fabric/gateway/FabricGateway.ts b/app/platform/fabric/gateway/FabricGateway.ts index 49251c0b7..f108280a7 100644 --- a/app/platform/fabric/gateway/FabricGateway.ts +++ b/app/platform/fabric/gateway/FabricGateway.ts @@ -488,4 +488,15 @@ export class FabricGateway { return null; } + + async queryContractMetadata(channel_name, contract_name) { + const network = await this.gateway.getNetwork(channel_name); + // Get the contract from the network.        + const contract = network.getContract(contract_name); + // Get the contract metadata from the network. + const result = await contract.evaluateTransaction('org.hyperledger.fabric:GetMetadata'); + const metadata = JSON.parse(result.toString('utf8')); + logger.debug('queryContractMetadata', metadata) + return metadata; + } } diff --git a/app/rest/platformroutes.ts b/app/rest/platformroutes.ts index 7bb540f2f..22444bf6b 100644 --- a/app/rest/platformroutes.ts +++ b/app/rest/platformroutes.ts @@ -180,4 +180,20 @@ export async function platformroutes( }); }); }); + + /** + * Return channel metadata + * GET /metadata + * curl -i 'http://:/metadata/' + */ + router.get('/metadata/:chaincode', (req, res) => { + const chaincode = req.params.chaincode; + if (chaincode) { + proxy.getContractMetadata(req.network, chaincode).then((data: any) => { + res.send({ status: 200, data: data }); + }); + } else { + return requtil.invalidRequest(req, res); + } + }); } // End platformroutes()