From eddb0e8fa042b7b80b5b2241f6e10ee86f83d71e Mon Sep 17 00:00:00 2001 From: urgetolearn Date: Sun, 8 Dec 2024 23:11:05 +0530 Subject: [PATCH] added connection-profile to invoke the chaincode Signed-off-by: urgetolearn --- extension.js | 35 ++++++++++------- package-lock.json | 32 +++++++++++++++ package.json | 1 + src/invokechaincode/connection-profile.json | 43 +++++++++++++++++++++ src/invokechaincode/invoke.js | 38 ++++++++++++++++++ 5 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 src/invokechaincode/connection-profile.json create mode 100644 src/invokechaincode/invoke.js diff --git a/extension.js b/extension.js index 992a907..5e6e686 100644 --- a/extension.js +++ b/extension.js @@ -18,6 +18,7 @@ const { getLatestBlockNumber, connectToFabric, } = require("./src/blockReader/blockQueries"); +const { invokeChaincode } = require('./src/invokechaincode/invoke.js'); let loadedConnectionProfile = null; @@ -266,7 +267,9 @@ function activate(context) { function isGoChaincodeFile(filePath) { return filePath.toLowerCase().endsWith('.go'); } - + function isJavaChaincodeFile(filePath) { + return filePath.toLowerCase().endsWith('.java'); + } function extractGoFunctions(code) { const functionDetails = []; const regex = /func\s*\((\w+)\s+\*SmartContract\)\s*(\w+)\s*\((.*?)\)\s*(\w*)/g; @@ -280,6 +283,19 @@ function activate(context) { return functionDetails; } + function extractJavaFunctions(code) { + const functionDetails = []; + const regex = /public\s+.*\s+(\w+)\s*\((.*?)\)/g; + let match; + + while ((match = regex.exec(code)) !== null) { + const functionName = match[1]; + const params = match[2]; + functionDetails.push({ name: functionName, params }); + } + + return functionDetails; + } function filterIntAndStringFunctions(functions) { return functions.filter(func => /int|string/.test(func.params)).map(func => `${func.name}(${func.params})`); @@ -343,26 +359,15 @@ function activate(context) { outputChannel.appendLine(`Function: ${functionName}`); outputChannel.appendLine(`Arguments: ${finalArgs}`); - vscode.window.showInformationMessage(`Arguments captured. Press "Invoke" to execute the command.`, "Invoke").then(selection => { + vscode.window.showInformationMessage(`Arguments captured. Press "Invoke" to execute the command.`, "Invoke").then(async selection => { if (selection === "Invoke") { - invokeCommand(functionName, argumentValues); + await invokeChaincode(functionName, argumentValues, context); } }); + } - async function invokeCommand(functionName, argumentValues) { - outputChannel.appendLine(`Invoking function ${functionName} with arguments: ${argumentValues.join(', ')}`); - - try { - - outputChannel.appendLine(`Simulated invocation of ${functionName}(${argumentValues.join(', ')})`); - } catch (error) { - outputChannel.appendLine(`Error during invocation: ${error.message}`); - } - - outputChannel.show(); - } context.subscriptions.push( vscode.commands.registerCommand( "fabric-network.switchNetwork", diff --git a/package-lock.json b/package-lock.json index f489813..85f6520 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@hyperledger/fabric-gateway": "^1.7.0", + "fabric-ca-client": "^2.2.20", "fabric-network": "^2.2.20", "js-yaml": "^4.1.0", "simple-git": "^3.27.0" @@ -456,6 +457,20 @@ "node": "> 0.1.90" } }, + "node_modules/fabric-ca-client": { + "version": "2.2.20", + "resolved": "https://registry.npmjs.org/fabric-ca-client/-/fabric-ca-client-2.2.20.tgz", + "integrity": "sha512-uaVpjPU+Yar2n4sNPJZpRC/jPgeLhw7nbzGl+aInkleg8Zkor4eBfQKpNQQxEORqgqWS5hAHNYu2x0iPEHBmLw==", + "dependencies": { + "fabric-common": "2.2.20", + "jsrsasign": "^10.5.25", + "url": "^0.11.0", + "winston": "^2.4.5" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/fabric-common": { "version": "2.2.20", "resolved": "https://registry.npmjs.org/fabric-common/-/fabric-common-2.2.20.tgz", @@ -886,6 +901,11 @@ "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" }, + "node_modules/punycode": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", + "integrity": "sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ==" + }, "node_modules/qs": { "version": "6.13.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.13.0.tgz", @@ -1005,6 +1025,18 @@ "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.18.2.tgz", "integrity": "sha512-5ruQbENj95yDYJNS3TvcaxPMshV7aizdv/hWYjGIKoANWKjhWNBsr2YEuYZKodQulB1b8l7ILOuDQep3afowQQ==" }, + "node_modules/url": { + "version": "0.11.4", + "resolved": "https://registry.npmjs.org/url/-/url-0.11.4.tgz", + "integrity": "sha512-oCwdVC7mTuWiPyjLUz/COz5TLk6wgp0RCsN+wHZ2Ekneac9w8uuV0njcbbie2ME+Vs+d6duwmYuR3HgQXs1fOg==", + "dependencies": { + "punycode": "^1.4.1", + "qs": "^6.12.3" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/winston": { "version": "2.4.7", "resolved": "https://registry.npmjs.org/winston/-/winston-2.4.7.tgz", diff --git a/package.json b/package.json index 50e239a..e3ef8be 100644 --- a/package.json +++ b/package.json @@ -201,6 +201,7 @@ }, "dependencies": { "@hyperledger/fabric-gateway": "^1.7.0", + "fabric-ca-client": "^2.2.20", "fabric-network": "^2.2.20", "js-yaml": "^4.1.0", "simple-git": "^3.27.0" diff --git a/src/invokechaincode/connection-profile.json b/src/invokechaincode/connection-profile.json new file mode 100644 index 0000000..49c95b9 --- /dev/null +++ b/src/invokechaincode/connection-profile.json @@ -0,0 +1,43 @@ +{ + "name": "fabric-network", + "version": "1.0.0", + "client": { + "organization": "Org1", + "connection": { + "timeout": { + "peer": "300" + } + } + }, + "channels": { + "mychannel": { + "orderers": [ + { + "url": "grpcs://localhost:7050" + } + ], + "peers": { + "peer0.org1.example.com": { + "url": "grpcs://localhost:7051" + } + } + } + }, + "organizations": { + "Org1": { + "mspid": "Org1MSP", + "peerNames": ["peer0.org1.example.com"], + "certificateAuthorities": ["ca.org1.example.com"] + } + }, + "orderers": [ + { + "url": "grpcs://localhost:7050" + } + ], + "peers": { + "peer0.org1.example.com": { + "url": "grpcs://localhost:7051" + } + } +} diff --git a/src/invokechaincode/invoke.js b/src/invokechaincode/invoke.js new file mode 100644 index 0000000..4d493f8 --- /dev/null +++ b/src/invokechaincode/invoke.js @@ -0,0 +1,38 @@ +const { Gateway, Wallets } = require('fabric-network'); +const FabricCAServices = require('fabric-ca-client'); +const path = require('path'); +const fs = require('fs'); +const { log } = require('console'); + +async function invokeChaincode(functionName, args, context) { + try { + + const ccpPath = path.resolve(__dirname, 'connection-profile.json'); + const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8')); + const wallet = await Wallets.newFileSystemWallet(path.join(process.cwd(), 'wallet')); + const gateway = new Gateway(); + await gateway.connect(ccp, { + wallet, + identity: 'admin', + discovery: { enabled: true, asLocalhost: true } + }); + + + const network = await gateway.getNetwork('mychannel'); + const contract = network.getContract('mychaincode'); + + const result = await contract.submitTransaction('CreateAsset', 'asset1', '100'); + console.log(`Chaincode invoked successfully. Result: ${result.toString()}`); + + + await gateway.disconnect(); + + return result.toString(); + } catch (error) { + console.error(`Failed to invoke chaincode: ${error.message}`); + throw new Error(`Failed to invoke chaincode: ${error.message}`); + } +} + + +invokeChaincode();