From 7923c86b081af0b657dc4f6c916c44f6a461a379 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 12 Sep 2023 19:52:10 +0530 Subject: [PATCH] Add a script to generate consensus keys --- package.json | 3 +- src/cli/gen-consensus-keys.ts | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/cli/gen-consensus-keys.ts diff --git a/package.json b/package.json index 92e198b..7e705a6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cli/gen-consensus-keys.ts b/src/cli/gen-consensus-keys.ts new file mode 100644 index 0000000..4271eb0 --- /dev/null +++ b/src/cli/gen-consensus-keys.ts @@ -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 { + 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); +});