Skip to content

chaincode operations #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,192 @@ function activate(context) {
`Debugging terminated: ${session.name}`
);
})
context.subscriptions.push(
vscode.commands.registerCommand("chaincode.Packagechaincode", async() => {
try {
vscode.window.showInformationMessage('Packaging chaincode');

const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active editor with chaincode file.');
return;
}
const chaincodePath = path.dirname(editor.document.fileName);

const packager = new BasePackager();
const packageBuffer = await packager.package(chaincodePath, 'go');
const outputPath = path.join(chaincodePath, 'chaincode.tar.gz');
fs.writeFileSync(outputPath, packageBuffer);

vscode.window.showInformationMessage(`Chaincode packaged successfully at: ${outputPath}`);
} catch (error) {
vscode.window.showErrorMessage(`Error packaging chaincode: ${error.message}`);
}
})
);

context.subscriptions.push(
vscode.commands.registerCommand("chaincode.Installchaincode", async() => {
try {
vscode.window.showInformationMessage('Installing chaincode');

const ccpPath = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectMany: false,
filters: { JSON: ['json'] },
openLabel: 'Select Connection Profile',
});

if (!ccpPath || ccpPath.length === 0) {
vscode.window.showErrorMessage('No connection profile selected.');
return;
}

const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
const walletPath = path.join(__dirname, '..', 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);

const gateway = new Gateway();
await gateway.connect(ccp, {
wallet,
identity: 'admin',
discovery: { enabled: true, asLocalhost: true },
});

const client = gateway.getClient();
const peers = Object.keys(ccp.peers || {});

const chaincodePackagePath = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectMany: false,
filters: { TAR: ['gz'] },
openLabel: 'Select Chaincode Package',
});

if (!chaincodePackagePath || chaincodePackagePath.length === 0) {
vscode.window.showErrorMessage('No chaincode package selected.');
return;
}

const packageBuffer = fs.readFileSync(chaincodePackagePath[0].fsPath);

for (const peer of peers) {
const installRequest = {
targets: [client.getPeer(peer)],
chaincodePackage: packageBuffer,
};

const response = await client.installChaincode(installRequest);
if (response && response[0]?.response?.status === 200) {
vscode.window.showInformationMessage(`Chaincode installed on peer: ${peer}`);
} else {
vscode.window.showErrorMessage(`Failed to install chaincode on peer: ${peer}`);
}
}

gateway.disconnect();
} catch (error) {
vscode.window.showErrorMessage(`Error installing chaincode: ${error.message}`);
}
})
);

context.subscriptions.push(
vscode.commands.registerCommand("chaincode.Approvechaincode", async() => {
try {
vscode.window.showInformationMessage('Approving chaincode definition...');

const ccpPath = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectMany: false,
filters: { JSON: ['json'] },
openLabel: 'Select Connection Profile',
});

if (!ccpPath || ccpPath.length === 0) {
vscode.window.showErrorMessage('No connection profile selected.');
return;
}

const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
const walletPath = path.join(__dirname, '..', 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);

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('lscc');

const approveRequest = {
chaincodeName: 'mychaincode',
chaincodeVersion: '1.0',
sequence: 1,
chaincodePackageId: '<PACKAGE_ID>', // Replace with actual Package ID from install
};

await contract.submitTransaction('approveChaincodeDefinitionForMyOrg', JSON.stringify(approveRequest));

vscode.window.showInformationMessage('Chaincode definition approved successfully.');
gateway.disconnect();
} catch (error) {
vscode.window.showErrorMessage(`Error approving chaincode: ${error.message}`);
}

})
)

context.subscriptions.push(
vscode.commands.registerCommand("chaincode.Commitchaincode", async() => {
try {
vscode.window.showInformationMessage('Committing chaincode definition...');

const ccpPath = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectMany: false,
filters: { JSON: ['json'] },
openLabel: 'Select Connection Profile',
});

if (!ccpPath || ccpPath.length === 0) {
vscode.window.showErrorMessage('No connection profile selected.');
return;
}

const ccp = JSON.parse(fs.readFileSync(ccpPath[0].fsPath, 'utf8'));
const walletPath = path.join(__dirname, '..', 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);

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('lscc');

const commitRequest = {
chaincodeName: 'mychaincode',
chaincodeVersion: '1.0',
sequence: 1,
endorsementPolicy: '', // Add your endorsement policy if needed
};

await contract.submitTransaction('commitChaincodeDefinition', JSON.stringify(commitRequest));

vscode.window.showInformationMessage('Chaincode definition committed successfully.');
gateway.disconnect();
} catch (error) {
vscode.window.showErrorMessage(`Error committing chaincode: ${error.message}`);
}
})
)
);

async function generateWallet(context, connectionProfileName) {
Expand Down Expand Up @@ -972,6 +1158,18 @@ function extractWalletDetails(walletData) {
return null;
}

function packageChaincode(chaincodePath) {
class chaincodePackager extends BasePackager {
async package(directoryPath) {
const files = fs.readdirSync(directoryPath).map((file) => path.join(directoryPath, file));
return this.generateTarGz(files);
}
}

const packager = new chaincodePackager();
return await packager.package(chaincodePath);
}

function deactivate() {}

module.exports = {
Expand Down
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@
"command": "wallets.generateWallet",
"title": "Generate Wallet",
"icon": "media/wallet.png"
},
{
"command":"chaincode.Packagechaincode",
"title": "Package chaincode",
"icon": "$(packagecc)"
},
{
"command": "chaincode.Installchaincode",
"title" : "Install chaincode",
"icon": "$(installcc)"
},
{
"command": "chaincode.Approvechaincode",
"title": "Approve chaincode",
"icon": "$(approvecc)"
},
{
"command": "chaincode.Commitchaincode",
"title": "Commit chaincode",
"icon": "$(commitcc)"
}
]
},
Expand Down
Loading