Skip to content

Commit

Permalink
feat(cli): add seed param for generate keypair command
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Feb 15, 2024
1 parent e2e1031 commit 29d7b20
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
7 changes: 5 additions & 2 deletions cli/tests/unit/genKeyPair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { genKeyPair } from "../../ts";

describe("genMaciKeypair CLI subcommand", () => {
it("genMaciKeypair should output a random private key and public key", () => {
const keypair1 = genKeyPair(true);
const keypair2 = genKeyPair(true);
const keypair1 = genKeyPair({ quiet: true });
const keypair2 = genKeyPair({
seed: 2801240355254658294358555831749515294960721159561923188361445519585440177746075496738285268291285781469276476510188450870897435498492786799730508035869388059n,
quiet: true,
});

// Invoking the same command twice should result in different private keys
expect(keypair1.privateKey).not.to.eq(keypair2.privateKey);
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/unit/genPubkey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { genKeyPair, genMaciPubKey } from "../../ts";

describe("genMaciPubkey CLI subcommand", () => {
it("should output a valid public key", () => {
const keypair = genKeyPair(true);
const keypair = genKeyPair({ quiet: true });
const pubKey = genMaciPubKey(keypair.privateKey, true);

expect(pubKey).to.eq(keypair.publicKey);
Expand Down
14 changes: 9 additions & 5 deletions cli/ts/commands/genKeyPair.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Keypair } from "maci-domainobjs";
import { SNARK_FIELD_SIZE } from "maci-crypto";
import { Keypair, PrivKey } from "maci-domainobjs";

import type { IGenKeypairArgs } from "../utils/interfaces";

import { banner } from "../utils/banner";
import { logGreen, success } from "../utils/theme";

/**
* Generate a new Maci Key Pair
* and print it to the screen
* @param quiet - whether to log the output
* @param {IGenKeypairArgs} args - keypair generation params
* @returns - keypair
*/
export const genKeyPair = (quiet = true): { publicKey: string; privateKey: string } => {
export const genKeyPair = ({ seed, quiet = true }: IGenKeypairArgs): { publicKey: string; privateKey: string } => {
banner(quiet);
// create the new rando keypair
const keypair = new Keypair();
// create the new random keypair if there is no seed value
const keypair = new Keypair(seed ? new PrivKey(seed % SNARK_FIELD_SIZE) : undefined);

// serialize both private and public keys
const serializedPubKey = keypair.pubKey.serialize();
Expand Down
3 changes: 2 additions & 1 deletion cli/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ program
program
.command("genMaciKeyPair")
.description("generate a new MACI key pair")
.option("-sp, --seed <seed>", "seed value for keypair", (value) => (value ? BigInt(value) : undefined), undefined)
.option("-q, --quiet <quiet>", "whether to print values to the console", (value) => value === "true", false)
.option("-r, --rpc-provider <provider>", "the rpc provider URL")
.action((cmdObj) => {
genKeyPair(cmdObj.quiet);
genKeyPair({ seed: cmdObj.seed, quiet: cmdObj.quiet });
});
program
.command("airdrop")
Expand Down
15 changes: 15 additions & 0 deletions cli/ts/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,21 @@ export interface VerifyArgs {
quiet?: boolean;
}

/**
* Interface for the arguments for generate keypair command
*/
export interface IGenKeypairArgs {
/**
* Seed value for keypair
*/
seed?: bigint;

/**
* Whether to log the output
*/
quiet?: boolean;
}

/**
* Interface for the arguments to the FundWallet command
*/
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-v1.x/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pnpm run hardhat
| `create` | Deploy the contracts | `-v, --vkRegistryAddress <vkRegistryAddress>`: The vk registry contract address <br/> `-i, --initialVoiceCredits <initialVoiceCredits>`: The initial voice credits <br/> `-p, --initialVoiceCreditsProxyAddress <initialVoiceCreditsProxyAddress>`: The initial voice credits proxy contract address <br/> `-g, --signupGatekeeperAddress <signupGatekeeperAddress>`: The signup gatekeeper contract address <br/> `-ph3, --poseidonT3Address <poseidonT3Address>`: The PoseidonT3 contract address <br/> `-ph4, --poseidonT4Address <poseidonT4Address>`: The PoseidonT4 contract address <br/> `-ph5, --poseidonT5Address <poseidonT5Address>`: The PoseidonT5 contract address <br/> `-ph6, --poseidonT6Address <poseidonT6Address>`: The PoseidonT6 contract address <br/> `-q, --quiet`: Whether to print values to the console <br/> `-s, --stateTreeDepth <stateTreeDepth>`: The state tree depth |
| `checkVerifyingKeys` | Check that the verifying keys in the contract match the local ones | `-q, --quiet`: Whether to print values to the console <br/> `-vk, --vk-contract <vkContract>`: The VkRegistry contract address <br/> `-s, --state-tree-depth <stateTreeDepth>`: The state tree depth <br/> `-i, --int-state-tree-depth <intStateTreeDepth>`: The intermediate state tree depth <br/> `-m, --msg-tree-depth <messageTreeDepth>`: The message tree depth <br/> `-v, --vote-option-tree-depth <voteOptionTreeDepth>`: The vote option tree depth <br/> `-b, --msg-batch-depth <messageBatchDepth>`: The message batch depth <br/> `-p, --process-messages-zkey <processMessagesZkeyPath>`: The process messages zkey path (see different options to use specific circuits [Trusted setup](https://maci.pse.dev/docs/trusted-setup) or [Testing](https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)) <br/> `-t, --tally-votes-zkey <tallyVotesZkeyPath>`: The tally votes zkey path (see different options to use specific circuits [Trusted setup](https://maci.pse.dev/docs/trusted-setup) or [Testing](https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)) <br /> `-ss, --subsidy-zkey <subsidyZkeyPath>`: The subsidy zkey path (see different options to use specific circuits [Trusted setup](https://maci.pse.dev/docs/trusted-setup) or [Testing](https://maci.pse.dev/docs/testing/#pre-compiled-artifacts-for-testing)) |
| `genMaciPubKey` | Generate a new MACI public key | `-sk, --privkey <privkey>`: The private key |
| `genMaciKeyPair` | Generate a new MACI key pair | No options |
| `genMaciKeyPair` | Generate a new MACI key pair | `-sp, --seed <seed> seed value for keypair` |
| `airdrop` | Airdrop topup credits to the coordinator | `-a, --amount <amount>`: The amount of topup <br/> `-x, --contract <contract>`: The MACI contract address <br/> `-o, --poll-id <pollId>`: Poll id <br/> `-t, --token-address <tokenAddress>`: The token address <br/> `-q, --quiet`: Whether to print values to the console |
| `deployVkRegistry` | Deploy a new verification key registry contract | `-q, --quiet`: Whether to print values to the console |
| `show` | Show the deployed contract addresses | No options |
Expand Down

0 comments on commit 29d7b20

Please sign in to comment.