Skip to content

Commit

Permalink
generate access keys during init command
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-bubu committed Feb 28, 2024
1 parent 8ba34ea commit a154b8b
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions chain-cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/
import { Args } from "@oclif/core";

import * as secp from "@noble/secp256k1";
import * as fs from "fs";
import { writeFile } from "node:fs/promises";
import path from "path";

import BaseCommand from "../../base-command";
Expand All @@ -38,7 +40,7 @@ export default class Init extends BaseCommand<typeof Init> {
const { args } = await this.parse(Init);

try {
this.copyChaincodeTemplate(args.path);
await this.copyChaincodeTemplate(args.path);

// Update the name field in the package.json and the package-lock.json to be `@gala-games/<project-name>`
const fileName = getPathFileName(args.path);
Expand All @@ -61,13 +63,17 @@ export default class Init extends BaseCommand<typeof Init> {
this.error(`Error updating project name in ${projectPath}: ${err}`);
}
});

await this.generateKeys(`${args.path}/keys`);
await this.gitignoreKeys(`${args.path}`);

this.log(`Project template initialized at ${args.path}`);
} catch (error) {
this.error(`Error initializing project template: ${error}`);
}
}

copyChaincodeTemplate(destinationPath: string): void {
async copyChaincodeTemplate(destinationPath: string): Promise<void> {
if (process.platform === "win32") {
const sourceTemplateDir = path.resolve(__dirname, "..", "..", "..", "chaincode-template");
execSync(`xcopy ${sourceTemplateDir} ${destinationPath} /E /I`);
Expand All @@ -76,4 +82,27 @@ export default class Init extends BaseCommand<typeof Init> {
const sourceTemplateDir = path.resolve(require.resolve("."), "../../../chaincode-template");
execSync(`cp -R ${sourceTemplateDir} ${destinationPath}`);
}

async generateKeys(keysPath: string): Promise<void> {
const adminPrivateKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey());
const adminPublicKey = secp.utils.bytesToHex(secp.getPublicKey(adminPrivateKey));

const devPrivateKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey());
const devPublicKey = secp.utils.bytesToHex(secp.getPublicKey(adminPrivateKey));

execSync(`mkdir -p ${keysPath}`);

this.log(`Generating keys to ${keysPath}`);
await writeFile(`${keysPath}/gc-admin-key.pub`, adminPublicKey);
await writeFile(`${keysPath}/gc-admin-key`, adminPrivateKey.toString());
await writeFile(`${keysPath}/gc-dev-key.pub`, devPublicKey);
await writeFile(`${keysPath}/gc-dev-key`, devPrivateKey.toString());
}

private async gitignoreKeys(projectPath: string): Promise<void> {
const gitignorePath = path.resolve(projectPath, ".gitignore");
const keyEntries = ["gc-admin-key", "gc-dev-key", "gc-dev-key.pub"];

keyEntries.forEach((entry) => fs.appendFileSync(gitignorePath, `${entry}\n`));
}
}

0 comments on commit a154b8b

Please sign in to comment.