Skip to content

Commit

Permalink
Add a script to generate consensus keys
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Sep 12, 2023
1 parent 5e49b7a commit 7923c86
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"import-state:dev": "DEBUG=vulcanize:* ts-node src/cli/import-state.ts",
"inspect-cid": "DEBUG=vulcanize:* ts-node src/cli/inspect-cid.ts",
"index-block": "DEBUG=vulcanize:* ts-node src/cli/index-block.ts",
"peer": "DEBUG='vulcanize:*, laconic:*' node --enable-source-maps dist/cli/peer.js"
"peer": "DEBUG='vulcanize:*, laconic:*' node --enable-source-maps dist/cli/peer.js",
"gen-consensus-keys": "DEBUG=laconic:* node --enable-source-maps dist/cli/gen-consensus-keys.js"
},
"repository": {
"type": "git",
Expand Down
56 changes: 56 additions & 0 deletions src/cli/gen-consensus-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright 2023 Vulcanize, Inc.
//

import crypto from 'crypto';
import debug from 'debug';
import fs from 'fs';
import path from 'path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs';

const log = debug('laconic:gen-consensus-keys');

interface Arguments {
file: string;
}

async function main (): Promise<void> {
const node = crypto.createECDH('secp256k1');
node.generateKeys('hex');
const obj = {
publicKey: node.getPublicKey('hex', 'compressed'),
privateKey: node.getPrivateKey('hex')
};

const argv: Arguments = _getArgv();
if (argv.file) {
const exportFilePath = path.resolve(argv.file);
const exportFileDir = path.dirname(exportFilePath);

if (!fs.existsSync(exportFileDir)) {
fs.mkdirSync(exportFileDir, { recursive: true });
}

fs.writeFileSync(exportFilePath, JSON.stringify(obj, null, 2));
log(`Key pair exported to file ${exportFilePath}`);
} else {
log(obj);
}
}

function _getArgv (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).options({
file: {
type: 'string',
alias: 'f',
describe: 'Peer Id export file path (json)'
}
}).argv;
}

main().catch(err => {
log(err);
});

0 comments on commit 7923c86

Please sign in to comment.