Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
Signed-off-by: Chinmayi D S <[email protected]>
  • Loading branch information
urgetolearn authored Dec 13, 2024
2 parents 5f29dae + f434ad2 commit ec39b85
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 35 deletions.
62 changes: 28 additions & 34 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ const {
connectToFabric,
decodeBlock,
} = require("./src/blockReader/blockQueries");

const DelveDebugAdapterDescriptorFactory = require("./src/debugAdapter/DelveDebugAdapterDescriptorFactory");

const { invokeChaincode } = require('./src/invokechaincode/invoke.js')
let loadedConnectionProfile = null;

const fabricsamples = require("./src/fabricsamples");
Expand Down Expand Up @@ -292,28 +294,20 @@ function activate(context) {
);

context.subscriptions.push(disposableExtractFunctions);

function isGoChaincodeFile(filePath) {
return filePath.toLowerCase().endsWith(".go");
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;
const regex = /func\s*\((\w+)\s+\*SmartContract\)\s*(\w+)\s*\((.*?)\)\s*(\w*)/g;
let match;

while ((match = regex.exec(code)) !== null) {
const functionName = match[2];
const params = match[3];
functionDetails.push({ name: functionName, params });
}

return functionDetails;
}

function filterIntAndStringFunctions(functions) {
return functions
function filterIntAndStringFunctions(functions) {
return functions
.filter((func) => /int|string/.test(func.params))
.map((func) => `${func.name}(${func.params})`);
}
Expand All @@ -324,6 +318,7 @@ function activate(context) {
context.workspaceState.update("storedFunctions", storedFunctions);
}


function showStoredFunctions(context, outputChannel) {
const storedFunctions = context.workspaceState.get("storedFunctions", []);

Expand Down Expand Up @@ -403,25 +398,7 @@ function activate(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 All @@ -440,6 +417,23 @@ function activate(context) {
treeViewProviderFabric.setActiveNetwork(fabricItem);
}


context.subscriptions.push(
vscode.commands.registerCommand(
"fabric-network.switchNetwork",
async (treeItem) => {
const descItem = treeViewProviderDesc.getNetworkByLabel(treeItem.label);
const fabricItem = treeViewProviderFabric.getNetworkByLabel(
treeItem.label
);

if (descItem) {
treeViewProviderDesc.setActiveNetwork(descItem);
}
if (fabricItem) {
treeViewProviderFabric.setActiveNetwork(fabricItem);
}

const activeNetwork = fabricItem || descItem;
if (!activeNetwork) {
vscode.window.showErrorMessage(
Expand Down
11 changes: 10 additions & 1 deletion package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,13 @@
]
},
"dependencies": {

"@hyperledger/fabric-protos": "^0.2.2",
"@vscode/debugadapter": "^1.68.0",
"dotenv": "^16.4.5",

"@hyperledger/fabric-gateway": "^1.7.0",

"fabric-ca-client": "^2.2.20",
"fabric-network": "^2.2.20",
"fabric-protos": "^2.2.20",
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 ec39b85

Please sign in to comment.