From 29d7b2057741012811622c30f91cda74cf4906eb Mon Sep 17 00:00:00 2001 From: 0xmad <0xmad@users.noreply.github.com> Date: Thu, 15 Feb 2024 13:02:27 -0600 Subject: [PATCH] feat(cli): add seed param for generate keypair command --- cli/tests/unit/genKeyPair.test.ts | 7 +++++-- cli/tests/unit/genPubkey.test.ts | 2 +- cli/ts/commands/genKeyPair.ts | 14 +++++++++----- cli/ts/index.ts | 3 ++- cli/ts/utils/interfaces.ts | 15 +++++++++++++++ website/versioned_docs/version-v1.x/cli.md | 2 +- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cli/tests/unit/genKeyPair.test.ts b/cli/tests/unit/genKeyPair.test.ts index dbc2dbf253..753521c576 100644 --- a/cli/tests/unit/genKeyPair.test.ts +++ b/cli/tests/unit/genKeyPair.test.ts @@ -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); diff --git a/cli/tests/unit/genPubkey.test.ts b/cli/tests/unit/genPubkey.test.ts index 6ced0877dd..060fe4b51a 100644 --- a/cli/tests/unit/genPubkey.test.ts +++ b/cli/tests/unit/genPubkey.test.ts @@ -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); diff --git a/cli/ts/commands/genKeyPair.ts b/cli/ts/commands/genKeyPair.ts index 0648abe71a..9507d803ac 100644 --- a/cli/ts/commands/genKeyPair.ts +++ b/cli/ts/commands/genKeyPair.ts @@ -1,4 +1,7 @@ -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"; @@ -6,12 +9,13 @@ 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(); diff --git a/cli/ts/index.ts b/cli/ts/index.ts index e71d738c80..d6525a3919 100644 --- a/cli/ts/index.ts +++ b/cli/ts/index.ts @@ -136,10 +136,11 @@ program program .command("genMaciKeyPair") .description("generate a new MACI key pair") + .option("-sp, --seed ", "seed value for keypair", (value) => (value ? BigInt(value) : undefined), undefined) .option("-q, --quiet ", "whether to print values to the console", (value) => value === "true", false) .option("-r, --rpc-provider ", "the rpc provider URL") .action((cmdObj) => { - genKeyPair(cmdObj.quiet); + genKeyPair({ seed: cmdObj.seed, quiet: cmdObj.quiet }); }); program .command("airdrop") diff --git a/cli/ts/utils/interfaces.ts b/cli/ts/utils/interfaces.ts index bcc214deef..b7ddfe0fdb 100644 --- a/cli/ts/utils/interfaces.ts +++ b/cli/ts/utils/interfaces.ts @@ -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 */ diff --git a/website/versioned_docs/version-v1.x/cli.md b/website/versioned_docs/version-v1.x/cli.md index 9515728bff..d1bb0e3e87 100644 --- a/website/versioned_docs/version-v1.x/cli.md +++ b/website/versioned_docs/version-v1.x/cli.md @@ -32,7 +32,7 @@ pnpm run hardhat | `create` | Deploy the contracts | `-v, --vkRegistryAddress `: The vk registry contract address
`-i, --initialVoiceCredits `: The initial voice credits
`-p, --initialVoiceCreditsProxyAddress `: The initial voice credits proxy contract address
`-g, --signupGatekeeperAddress `: The signup gatekeeper contract address
`-ph3, --poseidonT3Address `: The PoseidonT3 contract address
`-ph4, --poseidonT4Address `: The PoseidonT4 contract address
`-ph5, --poseidonT5Address `: The PoseidonT5 contract address
`-ph6, --poseidonT6Address `: The PoseidonT6 contract address
`-q, --quiet`: Whether to print values to the console
`-s, --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
`-vk, --vk-contract `: The VkRegistry contract address
`-s, --state-tree-depth `: The state tree depth
`-i, --int-state-tree-depth `: The intermediate state tree depth
`-m, --msg-tree-depth `: The message tree depth
`-v, --vote-option-tree-depth `: The vote option tree depth
`-b, --msg-batch-depth `: The message batch depth
`-p, --process-messages-zkey `: 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))
`-t, --tally-votes-zkey `: 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))
`-ss, --subsidy-zkey `: 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 `: The private key | -| `genMaciKeyPair` | Generate a new MACI key pair | No options | +| `genMaciKeyPair` | Generate a new MACI key pair | `-sp, --seed seed value for keypair` | | `airdrop` | Airdrop topup credits to the coordinator | `-a, --amount `: The amount of topup
`-x, --contract `: The MACI contract address
`-o, --poll-id `: Poll id
`-t, --token-address `: The token address
`-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 |