Skip to content

Commit 5f590ed

Browse files
chaincode changes
1 parent 92c439f commit 5f590ed

File tree

2 files changed

+223
-1
lines changed

2 files changed

+223
-1
lines changed

extension.js

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ let loadedConnectionProfile = null;
3030

3131
const fabricsamples = require("./src/fabricsamples");
3232

33+
const { BasePackager } = require('fabric-client/lib/packager');
34+
3335
function activate(context) {
3436
const fabricDebuggerPath = "C:\\Users\\chinm\\fabric-debugger";
3537

@@ -1132,6 +1134,193 @@ function activate(context) {
11321134
}
11331135
}
11341136
}
1137+
1138+
context.subscriptions.push(
1139+
vscode.commands.registerCommand("chaincode.Packagechaincode", async() => {
1140+
try {
1141+
vscode.window.showInformationMessage('Packaging chaincode');
1142+
1143+
const editor = vscode.window.activeTextEditor;
1144+
if (!editor) {
1145+
vscode.window.showErrorMessage('No active editor with chaincode file.');
1146+
return;
1147+
}
1148+
const chaincodePath = path.dirname(editor.document.fileName);
1149+
1150+
const packager = new BasePackager();
1151+
const packageBuffer = await packager.package(chaincodePath, 'go');
1152+
const outputPath = path.join(chaincodePath, 'chaincode.tar.gz');
1153+
fs.writeFileSync(outputPath, packageBuffer);
1154+
1155+
vscode.window.showInformationMessage(`Chaincode packaged successfully at: ${outputPath}`);
1156+
} catch (error) {
1157+
vscode.window.showErrorMessage(`Error packaging chaincode: ${error.message}`);
1158+
}
1159+
})
1160+
);
1161+
1162+
context.subscriptions.push(
1163+
vscode.commands.registerCommand("chaincode.Installchaincode", async() => {
1164+
try {
1165+
vscode.window.showInformationMessage('Installing chaincode');
1166+
1167+
const ccpPath = await vscode.window.showOpenDialog({
1168+
canSelectFiles: true,
1169+
canSelectMany: false,
1170+
filters: { JSON: ['json'] },
1171+
openLabel: 'Select Connection Profile',
1172+
});
1173+
1174+
if (!ccpPath || ccpPath.length === 0) {
1175+
vscode.window.showErrorMessage('No connection profile selected.');
1176+
return;
1177+
}
1178+
1179+
const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
1180+
const walletPath = path.join(__dirname, '..', 'wallet');
1181+
const wallet = await Wallets.newFileSystemWallet(walletPath);
1182+
1183+
const gateway = new Gateway();
1184+
await gateway.connect(ccp, {
1185+
wallet,
1186+
identity: 'admin',
1187+
discovery: { enabled: true, asLocalhost: true },
1188+
});
1189+
1190+
const client = gateway.getClient();
1191+
const peers = Object.keys(ccp.peers || {});
1192+
1193+
const chaincodePackagePath = await vscode.window.showOpenDialog({
1194+
canSelectFiles: true,
1195+
canSelectMany: false,
1196+
filters: { TAR: ['gz'] },
1197+
openLabel: 'Select Chaincode Package',
1198+
});
1199+
1200+
if (!chaincodePackagePath || chaincodePackagePath.length === 0) {
1201+
vscode.window.showErrorMessage('No chaincode package selected.');
1202+
return;
1203+
}
1204+
1205+
const packageBuffer = fs.readFileSync(chaincodePackagePath[0].fsPath);
1206+
1207+
for (const peer of peers) {
1208+
const installRequest = {
1209+
targets: [client.getPeer(peer)],
1210+
chaincodePackage: packageBuffer,
1211+
};
1212+
1213+
const response = await client.installChaincode(installRequest);
1214+
if (response && response[0]?.response?.status === 200) {
1215+
vscode.window.showInformationMessage(`Chaincode installed on peer: ${peer}`);
1216+
} else {
1217+
vscode.window.showErrorMessage(`Failed to install chaincode on peer: ${peer}`);
1218+
}
1219+
}
1220+
1221+
gateway.disconnect();
1222+
} catch (error) {
1223+
vscode.window.showErrorMessage(`Error installing chaincode: ${error.message}`);
1224+
}
1225+
})
1226+
);
1227+
1228+
context.subscriptions.push(
1229+
vscode.commands.registerCommand("chaincode.Approvechaincode", async() => {
1230+
try {
1231+
vscode.window.showInformationMessage('Approving chaincode definition...');
1232+
1233+
const ccpPath = await vscode.window.showOpenDialog({
1234+
canSelectFiles: true,
1235+
canSelectMany: false,
1236+
filters: { JSON: ['json'] },
1237+
openLabel: 'Select Connection Profile',
1238+
});
1239+
1240+
if (!ccpPath || ccpPath.length === 0) {
1241+
vscode.window.showErrorMessage('No connection profile selected.');
1242+
return;
1243+
}
1244+
1245+
const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
1246+
const walletPath = path.join(__dirname, '..', 'wallet');
1247+
const wallet = await Wallets.newFileSystemWallet(walletPath);
1248+
1249+
const gateway = new Gateway();
1250+
await gateway.connect(ccp, {
1251+
wallet,
1252+
identity: 'admin',
1253+
discovery: { enabled: true, asLocalhost: true },
1254+
});
1255+
1256+
const network = await gateway.getNetwork('mychannel');
1257+
const contract = network.getContract('lscc');
1258+
1259+
const approveRequest = {
1260+
chaincodeName: 'mychaincode',
1261+
chaincodeVersion: '1.0',
1262+
sequence: 1,
1263+
chaincodePackageId: '<PACKAGE_ID>', // Replace with actual Package ID from install
1264+
};
1265+
1266+
await contract.submitTransaction('approveChaincodeDefinitionForMyOrg', JSON.stringify(approveRequest));
1267+
1268+
vscode.window.showInformationMessage('Chaincode definition approved successfully.');
1269+
gateway.disconnect();
1270+
} catch (error) {
1271+
vscode.window.showErrorMessage(`Error approving chaincode: ${error.message}`);
1272+
}
1273+
1274+
})
1275+
)
1276+
1277+
context.subscriptions.push(
1278+
vscode.commands.registerCommand("chaincode.Commitchaincode", async() => {
1279+
try {
1280+
vscode.window.showInformationMessage('Committing chaincode definition...');
1281+
1282+
const ccpPath = await vscode.window.showOpenDialog({
1283+
canSelectFiles: true,
1284+
canSelectMany: false,
1285+
filters: { JSON: ['json'] },
1286+
openLabel: 'Select Connection Profile',
1287+
});
1288+
1289+
if (!ccpPath || ccpPath.length === 0) {
1290+
vscode.window.showErrorMessage('No connection profile selected.');
1291+
return;
1292+
}
1293+
1294+
const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
1295+
const walletPath = path.join(__dirname, '..', 'wallet');
1296+
const wallet = await Wallets.newFileSystemWallet(walletPath);
1297+
1298+
const gateway = new Gateway();
1299+
await gateway.connect(ccp, {
1300+
wallet,
1301+
identity: 'admin',
1302+
discovery: { enabled: true, asLocalhost: true },
1303+
});
1304+
1305+
const network = await gateway.getNetwork('mychannel');
1306+
const contract = network.getContract('lscc');
1307+
1308+
const commitRequest = {
1309+
chaincodeName: 'mychaincode',
1310+
chaincodeVersion: '1.0',
1311+
sequence: 1,
1312+
endorsementPolicy: '', // Add your endorsement policy if needed
1313+
};
1314+
1315+
await contract.submitTransaction('commitChaincodeDefinition', JSON.stringify(commitRequest));
1316+
1317+
vscode.window.showInformationMessage('Chaincode definition committed successfully.');
1318+
gateway.disconnect();
1319+
} catch (error) {
1320+
vscode.window.showErrorMessage(`Error committing chaincode: ${error.message}`);
1321+
}
1322+
})
1323+
)
11351324
};
11361325

11371326
function extractNetworkDetails(profile) {
@@ -1242,6 +1431,18 @@ function extractWalletDetails(walletData) {
12421431
return null;
12431432
}
12441433

1434+
function packageChaincode(chaincodePath) {
1435+
class chaincodePackager extends BasePackager {
1436+
async package(directoryPath) {
1437+
const files = fs.readdirSync(directoryPath).map((file) => path.join(directoryPath, file));
1438+
return this.generateTarGz(files);
1439+
}
1440+
}
1441+
1442+
const packager = new chaincodePackager();
1443+
return await packager.package(chaincodePath);
1444+
}
1445+
12451446
function deactivate() {}
12461447

12471448
module.exports = {

package.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,26 @@
259259
"title": "Start Delve Debugger",
260260
"category": "Debug",
261261
"icon": "media/debug.png"
262+
},
263+
{
264+
"command":"chaincode.Packagechaincode",
265+
"title": "Package chaincode",
266+
"icon": "$(packagecc)"
267+
},
268+
{
269+
"command": "chaincode.Installchaincode",
270+
"title" : "Install chaincode",
271+
"icon": "$(installcc)"
272+
},
273+
{
274+
"command": "chaincode.Approvechaincode",
275+
"title": "Approve chaincode",
276+
"icon": "$(approvecc)"
277+
},
278+
{
279+
"command": "chaincode.Commitchaincode",
280+
"title": "Commit chaincode",
281+
"icon": "$(commitcc)"
262282
}
263283
]
264284
},
@@ -275,7 +295,8 @@
275295
"fabric-protos": "^2.2.20",
276296
"protobufjs": "^7.4.0",
277297
"js-yaml": "^4.1.0",
278-
"simple-git": "^3.27.0"
298+
"simple-git": "^3.27.0",
299+
"fabric-client": "2.2.0"
279300
},
280301
"publish": {
281302
"registry": "https://npm.pkg.github.com"

0 commit comments

Comments
 (0)