Skip to content

Commit

Permalink
Merge pull request #23 from urgetolearn/main
Browse files Browse the repository at this point in the history
added connection-profile to invoke the chaincode
  • Loading branch information
technomad01 authored Dec 13, 2024
2 parents 5e01130 + eddb0e8 commit f434ad2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 15 deletions.
35 changes: 20 additions & 15 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
getLatestBlockNumber,
connectToFabric,
} = require("./src/blockReader/blockQueries");
const { invokeChaincode } = require('./src/invokechaincode/invoke.js');

let loadedConnectionProfile = null;

Expand Down Expand Up @@ -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;
Expand All @@ -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})`);
Expand Down Expand Up @@ -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",
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 43 additions & 0 deletions src/invokechaincode/connection-profile.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
38 changes: 38 additions & 0 deletions src/invokechaincode/invoke.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit f434ad2

Please sign in to comment.