Skip to content

Commit

Permalink
create-bls-key complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Metroxe committed Sep 12, 2023
1 parent 384b2a5 commit 92757f9
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 61 deletions.
3 changes: 3 additions & 0 deletions apps/cli/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ["../../.eslintrc.js"]
};
13 changes: 9 additions & 4 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
"bin": "dist/index.js",
"scripts": {
"clean": "rimraf dist && rimraf tsconfig.tsbuildinfo",
"build": "tsc && pkg ."
},
"lint": "eslint . --ext .ts,.tsx",
"build": "tsc && pkg .",
"compile": "tsc",
"start": "node dist/index.js"
},
"keywords": [],
"author": "",
"license": "UNLICENSED",
"dependencies": {
"@xai-vanguard-node/core": "workspace:*"
"@xai-vanguard-node/core": "^1.0.0",
"vorpal": "^1.12.0"
},
"devDependencies": {
"@types/vorpal": "^1.12.2",
"pkg": "^5.8.1"
},
},
"pkg": {
"targets": [
"node18-linux-x64",
Expand Down
23 changes: 23 additions & 0 deletions apps/cli/src/commands/create-bls-key-pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as Vorpal from "vorpal";
import { createBlsKeyPair as coreCreateBlsKeyPair } from "@xai-vanguard-node/core";

export function createBlsKeyPair(cli: Vorpal) {
cli
.command('create-bls-key-pair', 'Creates a BLS key pair. If a secret key is provided, it will return the corresponding public key. If no secret key is provided, it will generate a new key pair and return both the secret and public keys. Note: The secret key will be lost when the application closes, so please secure it safely.')
.action(async function (this: Vorpal.CommandInstance) {
const secretKeyPrompt: Vorpal.PromptObject = {
type: 'password',
name: 'secretKey',
message: 'Enter your secret key (optional):',
mask: '*',
optional: true
};
const {secretKey} = await this.prompt(secretKeyPrompt);
const { secretKeyHex, publicKeyHex } = await coreCreateBlsKeyPair(secretKey);
if (secretKey) {
this.log(`Public Key: ${publicKeyHex}`);
} else {
this.log(`Secret Key: ${secretKeyHex}\nPublic Key: ${publicKeyHex}\nPlease secure your secret key safely. It will be lost when the application closes.`);
}
});
}
14 changes: 9 additions & 5 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {createBlsKeyPair} from "@xai-vanguard-node/core";
import * as Vorpal from 'vorpal';
import { createBlsKeyPair } from './commands/create-bls-key-pair';

const cli = new Vorpal();

console.log("Stealing all your info and sending to the Shadow Government");
// entrypoints to each of the commands
createBlsKeyPair(cli);

setInterval(() => {
console.log("Lol why did you run this, idiot!");
}, 1000)
cli
.delimiter('vanguard-node$')
.show()
.log('\nType "help" to display a list of actions.');
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "",
"license": "UNLICENSED",
"dependencies": {
"@chainsafe/bls": "^7.1.2"
"@noble/curves": "^1.2.0"
},
"devDependencies": {
"@types/node": "^20.6.0"
Expand Down
18 changes: 8 additions & 10 deletions packages/core/src/utils/createBlsKeyPair.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import bls from "@chainsafe/bls";
import { bls12_381 as bls } from '@noble/curves/bls12-381';

/**
* Creates a BLS Key Pair and returns the secret and public key in hexadecimal format.
* @param secretKeyString - An optional string representing the secret key. If not provided, a new secret key is generated.
* @returns An object containing the secret key and public key in hexadecimal format.
* @example
* const { secretKeyHex, publicKeyHex } = await createBlsKeyPair();
* const { secretKeyHex, publicKeyHex } = await createBlsKeyPair('yourSecretKeyString');
*/
export async function createBlsKeyPair(): Promise<{ secretKeyHex: string, publicKeyHex: string }> {
const secretKey = bls.SecretKey.fromKeygen();
const publicKey = secretKey.toPublicKey();

const secretKeyBytes = secretKey.toBytes();
const publicKeyBytes = publicKey.toBytes();

const secretKeyHex = Buffer.from(secretKeyBytes).toString('hex');
const publicKeyHex = Buffer.from(publicKeyBytes).toString('hex');
export async function createBlsKeyPair(secretKeyString?: string): Promise<{ secretKeyHex: string, publicKeyHex: string }> {
const secretKey = secretKeyString ? Buffer.from(secretKeyString, 'hex') : bls.utils.randomPrivateKey();
const secretKeyHex = Buffer.from(secretKey).toString('hex');
const publicKey = bls.getPublicKey(secretKey);
const publicKeyHex = Buffer.from(publicKey).toString('hex');

return { secretKeyHex, publicKeyHex };
}
Loading

0 comments on commit 92757f9

Please sign in to comment.