Skip to content

Commit 4ee9fea

Browse files
authored
Merge pull request #28 from varshapichandi30/main
chaincode operations
2 parents 3471ed0 + d2cf788 commit 4ee9fea

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

extension.js

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,192 @@ function activate(context) {
728728
`Debugging terminated: ${session.name}`
729729
);
730730
})
731+
context.subscriptions.push(
732+
vscode.commands.registerCommand("chaincode.Packagechaincode", async() => {
733+
try {
734+
vscode.window.showInformationMessage('Packaging chaincode');
735+
736+
const editor = vscode.window.activeTextEditor;
737+
if (!editor) {
738+
vscode.window.showErrorMessage('No active editor with chaincode file.');
739+
return;
740+
}
741+
const chaincodePath = path.dirname(editor.document.fileName);
742+
743+
const packager = new BasePackager();
744+
const packageBuffer = await packager.package(chaincodePath, 'go');
745+
const outputPath = path.join(chaincodePath, 'chaincode.tar.gz');
746+
fs.writeFileSync(outputPath, packageBuffer);
747+
748+
vscode.window.showInformationMessage(`Chaincode packaged successfully at: ${outputPath}`);
749+
} catch (error) {
750+
vscode.window.showErrorMessage(`Error packaging chaincode: ${error.message}`);
751+
}
752+
})
753+
);
754+
755+
context.subscriptions.push(
756+
vscode.commands.registerCommand("chaincode.Installchaincode", async() => {
757+
try {
758+
vscode.window.showInformationMessage('Installing chaincode');
759+
760+
const ccpPath = await vscode.window.showOpenDialog({
761+
canSelectFiles: true,
762+
canSelectMany: false,
763+
filters: { JSON: ['json'] },
764+
openLabel: 'Select Connection Profile',
765+
});
766+
767+
if (!ccpPath || ccpPath.length === 0) {
768+
vscode.window.showErrorMessage('No connection profile selected.');
769+
return;
770+
}
771+
772+
const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
773+
const walletPath = path.join(__dirname, '..', 'wallet');
774+
const wallet = await Wallets.newFileSystemWallet(walletPath);
775+
776+
const gateway = new Gateway();
777+
await gateway.connect(ccp, {
778+
wallet,
779+
identity: 'admin',
780+
discovery: { enabled: true, asLocalhost: true },
781+
});
782+
783+
const client = gateway.getClient();
784+
const peers = Object.keys(ccp.peers || {});
785+
786+
const chaincodePackagePath = await vscode.window.showOpenDialog({
787+
canSelectFiles: true,
788+
canSelectMany: false,
789+
filters: { TAR: ['gz'] },
790+
openLabel: 'Select Chaincode Package',
791+
});
792+
793+
if (!chaincodePackagePath || chaincodePackagePath.length === 0) {
794+
vscode.window.showErrorMessage('No chaincode package selected.');
795+
return;
796+
}
797+
798+
const packageBuffer = fs.readFileSync(chaincodePackagePath[0].fsPath);
799+
800+
for (const peer of peers) {
801+
const installRequest = {
802+
targets: [client.getPeer(peer)],
803+
chaincodePackage: packageBuffer,
804+
};
805+
806+
const response = await client.installChaincode(installRequest);
807+
if (response && response[0]?.response?.status === 200) {
808+
vscode.window.showInformationMessage(`Chaincode installed on peer: ${peer}`);
809+
} else {
810+
vscode.window.showErrorMessage(`Failed to install chaincode on peer: ${peer}`);
811+
}
812+
}
813+
814+
gateway.disconnect();
815+
} catch (error) {
816+
vscode.window.showErrorMessage(`Error installing chaincode: ${error.message}`);
817+
}
818+
})
819+
);
820+
821+
context.subscriptions.push(
822+
vscode.commands.registerCommand("chaincode.Approvechaincode", async() => {
823+
try {
824+
vscode.window.showInformationMessage('Approving chaincode definition...');
825+
826+
const ccpPath = await vscode.window.showOpenDialog({
827+
canSelectFiles: true,
828+
canSelectMany: false,
829+
filters: { JSON: ['json'] },
830+
openLabel: 'Select Connection Profile',
831+
});
832+
833+
if (!ccpPath || ccpPath.length === 0) {
834+
vscode.window.showErrorMessage('No connection profile selected.');
835+
return;
836+
}
837+
838+
const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
839+
const walletPath = path.join(__dirname, '..', 'wallet');
840+
const wallet = await Wallets.newFileSystemWallet(walletPath);
841+
842+
const gateway = new Gateway();
843+
await gateway.connect(ccp, {
844+
wallet,
845+
identity: 'admin',
846+
discovery: { enabled: true, asLocalhost: true },
847+
});
848+
849+
const network = await gateway.getNetwork('mychannel');
850+
const contract = network.getContract('lscc');
851+
852+
const approveRequest = {
853+
chaincodeName: 'mychaincode',
854+
chaincodeVersion: '1.0',
855+
sequence: 1,
856+
chaincodePackageId: '<PACKAGE_ID>', // Replace with actual Package ID from install
857+
};
858+
859+
await contract.submitTransaction('approveChaincodeDefinitionForMyOrg', JSON.stringify(approveRequest));
860+
861+
vscode.window.showInformationMessage('Chaincode definition approved successfully.');
862+
gateway.disconnect();
863+
} catch (error) {
864+
vscode.window.showErrorMessage(`Error approving chaincode: ${error.message}`);
865+
}
866+
867+
})
868+
)
869+
870+
context.subscriptions.push(
871+
vscode.commands.registerCommand("chaincode.Commitchaincode", async() => {
872+
try {
873+
vscode.window.showInformationMessage('Committing chaincode definition...');
874+
875+
const ccpPath = await vscode.window.showOpenDialog({
876+
canSelectFiles: true,
877+
canSelectMany: false,
878+
filters: { JSON: ['json'] },
879+
openLabel: 'Select Connection Profile',
880+
});
881+
882+
if (!ccpPath || ccpPath.length === 0) {
883+
vscode.window.showErrorMessage('No connection profile selected.');
884+
return;
885+
}
886+
887+
const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
888+
const walletPath = path.join(__dirname, '..', 'wallet');
889+
const wallet = await Wallets.newFileSystemWallet(walletPath);
890+
891+
const gateway = new Gateway();
892+
await gateway.connect(ccp, {
893+
wallet,
894+
identity: 'admin',
895+
discovery: { enabled: true, asLocalhost: true },
896+
});
897+
898+
const network = await gateway.getNetwork('mychannel');
899+
const contract = network.getContract('lscc');
900+
901+
const commitRequest = {
902+
chaincodeName: 'mychaincode',
903+
chaincodeVersion: '1.0',
904+
sequence: 1,
905+
endorsementPolicy: '', // Add your endorsement policy if needed
906+
};
907+
908+
await contract.submitTransaction('commitChaincodeDefinition', JSON.stringify(commitRequest));
909+
910+
vscode.window.showInformationMessage('Chaincode definition committed successfully.');
911+
gateway.disconnect();
912+
} catch (error) {
913+
vscode.window.showErrorMessage(`Error committing chaincode: ${error.message}`);
914+
}
915+
})
916+
)
731917
);
732918

733919
async function generateWallet(context, connectionProfileName) {
@@ -972,6 +1158,18 @@ function extractWalletDetails(walletData) {
9721158
return null;
9731159
}
9741160

1161+
function packageChaincode(chaincodePath) {
1162+
class chaincodePackager extends BasePackager {
1163+
async package(directoryPath) {
1164+
const files = fs.readdirSync(directoryPath).map((file) => path.join(directoryPath, file));
1165+
return this.generateTarGz(files);
1166+
}
1167+
}
1168+
1169+
const packager = new chaincodePackager();
1170+
return await packager.package(chaincodePath);
1171+
}
1172+
9751173
function deactivate() {}
9761174

9771175
module.exports = {

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@
240240
"command": "wallets.generateWallet",
241241
"title": "Generate Wallet",
242242
"icon": "media/wallet.png"
243+
},
244+
{
245+
"command":"chaincode.Packagechaincode",
246+
"title": "Package chaincode",
247+
"icon": "$(packagecc)"
248+
},
249+
{
250+
"command": "chaincode.Installchaincode",
251+
"title" : "Install chaincode",
252+
"icon": "$(installcc)"
253+
},
254+
{
255+
"command": "chaincode.Approvechaincode",
256+
"title": "Approve chaincode",
257+
"icon": "$(approvecc)"
258+
},
259+
{
260+
"command": "chaincode.Commitchaincode",
261+
"title": "Commit chaincode",
262+
"icon": "$(commitcc)"
243263
}
244264
]
245265
},

0 commit comments

Comments
 (0)