-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to generate consensus keys
- Loading branch information
1 parent
5e49b7a
commit 7923c86
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |