Skip to content

Commit f434ad2

Browse files
authored
Merge pull request #23 from urgetolearn/main
added connection-profile to invoke the chaincode
2 parents 5e01130 + eddb0e8 commit f434ad2

File tree

5 files changed

+134
-15
lines changed

5 files changed

+134
-15
lines changed

extension.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
getLatestBlockNumber,
1919
connectToFabric,
2020
} = require("./src/blockReader/blockQueries");
21+
const { invokeChaincode } = require('./src/invokechaincode/invoke.js');
2122

2223
let loadedConnectionProfile = null;
2324

@@ -266,7 +267,9 @@ function activate(context) {
266267
function isGoChaincodeFile(filePath) {
267268
return filePath.toLowerCase().endsWith('.go');
268269
}
269-
270+
function isJavaChaincodeFile(filePath) {
271+
return filePath.toLowerCase().endsWith('.java');
272+
}
270273
function extractGoFunctions(code) {
271274
const functionDetails = [];
272275
const regex = /func\s*\((\w+)\s+\*SmartContract\)\s*(\w+)\s*\((.*?)\)\s*(\w*)/g;
@@ -280,6 +283,19 @@ function activate(context) {
280283

281284
return functionDetails;
282285
}
286+
function extractJavaFunctions(code) {
287+
const functionDetails = [];
288+
const regex = /public\s+.*\s+(\w+)\s*\((.*?)\)/g;
289+
let match;
290+
291+
while ((match = regex.exec(code)) !== null) {
292+
const functionName = match[1];
293+
const params = match[2];
294+
functionDetails.push({ name: functionName, params });
295+
}
296+
297+
return functionDetails;
298+
}
283299

284300
function filterIntAndStringFunctions(functions) {
285301
return functions.filter(func => /int|string/.test(func.params)).map(func => `${func.name}(${func.params})`);
@@ -343,26 +359,15 @@ function activate(context) {
343359
outputChannel.appendLine(`Function: ${functionName}`);
344360
outputChannel.appendLine(`Arguments: ${finalArgs}`);
345361

346-
vscode.window.showInformationMessage(`Arguments captured. Press "Invoke" to execute the command.`, "Invoke").then(selection => {
362+
vscode.window.showInformationMessage(`Arguments captured. Press "Invoke" to execute the command.`, "Invoke").then(async selection => {
347363
if (selection === "Invoke") {
348-
invokeCommand(functionName, argumentValues);
364+
await invokeChaincode(functionName, argumentValues, context);
349365
}
350366
});
367+
351368
}
352369

353370

354-
async function invokeCommand(functionName, argumentValues) {
355-
outputChannel.appendLine(`Invoking function ${functionName} with arguments: ${argumentValues.join(', ')}`);
356-
357-
try {
358-
359-
outputChannel.appendLine(`Simulated invocation of ${functionName}(${argumentValues.join(', ')})`);
360-
} catch (error) {
361-
outputChannel.appendLine(`Error during invocation: ${error.message}`);
362-
}
363-
364-
outputChannel.show();
365-
}
366371
context.subscriptions.push(
367372
vscode.commands.registerCommand(
368373
"fabric-network.switchNetwork",

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
},
202202
"dependencies": {
203203
"@hyperledger/fabric-gateway": "^1.7.0",
204+
"fabric-ca-client": "^2.2.20",
204205
"fabric-network": "^2.2.20",
205206
"js-yaml": "^4.1.0",
206207
"simple-git": "^3.27.0"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "fabric-network",
3+
"version": "1.0.0",
4+
"client": {
5+
"organization": "Org1",
6+
"connection": {
7+
"timeout": {
8+
"peer": "300"
9+
}
10+
}
11+
},
12+
"channels": {
13+
"mychannel": {
14+
"orderers": [
15+
{
16+
"url": "grpcs://localhost:7050"
17+
}
18+
],
19+
"peers": {
20+
"peer0.org1.example.com": {
21+
"url": "grpcs://localhost:7051"
22+
}
23+
}
24+
}
25+
},
26+
"organizations": {
27+
"Org1": {
28+
"mspid": "Org1MSP",
29+
"peerNames": ["peer0.org1.example.com"],
30+
"certificateAuthorities": ["ca.org1.example.com"]
31+
}
32+
},
33+
"orderers": [
34+
{
35+
"url": "grpcs://localhost:7050"
36+
}
37+
],
38+
"peers": {
39+
"peer0.org1.example.com": {
40+
"url": "grpcs://localhost:7051"
41+
}
42+
}
43+
}

src/invokechaincode/invoke.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { Gateway, Wallets } = require('fabric-network');
2+
const FabricCAServices = require('fabric-ca-client');
3+
const path = require('path');
4+
const fs = require('fs');
5+
const { log } = require('console');
6+
7+
async function invokeChaincode(functionName, args, context) {
8+
try {
9+
10+
const ccpPath = path.resolve(__dirname, 'connection-profile.json');
11+
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
12+
const wallet = await Wallets.newFileSystemWallet(path.join(process.cwd(), 'wallet'));
13+
const gateway = new Gateway();
14+
await gateway.connect(ccp, {
15+
wallet,
16+
identity: 'admin',
17+
discovery: { enabled: true, asLocalhost: true }
18+
});
19+
20+
21+
const network = await gateway.getNetwork('mychannel');
22+
const contract = network.getContract('mychaincode');
23+
24+
const result = await contract.submitTransaction('CreateAsset', 'asset1', '100');
25+
console.log(`Chaincode invoked successfully. Result: ${result.toString()}`);
26+
27+
28+
await gateway.disconnect();
29+
30+
return result.toString();
31+
} catch (error) {
32+
console.error(`Failed to invoke chaincode: ${error.message}`);
33+
throw new Error(`Failed to invoke chaincode: ${error.message}`);
34+
}
35+
}
36+
37+
38+
invokeChaincode();

0 commit comments

Comments
 (0)