From bbaf1dc8fd260f56f260b688021c9daf34563652 Mon Sep 17 00:00:00 2001 From: Dmitri Tsumak Date: Mon, 24 Apr 2023 17:25:57 +0300 Subject: [PATCH] Add batch sign (#30) * Add batch sign * Fix eth2.0-deposit-cli checksum * Fix formatting * Fix flake8 --- cli/sign.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/cli/sign.py b/cli/sign.py index 6bd4a7b..c80a8f8 100644 --- a/cli/sign.py +++ b/cli/sign.py @@ -8,6 +8,20 @@ @click.command() +@click.option( + "--payloads-file", + required=False, + help="The path to the file containing signing payloads. Defaults to ./payloads.json", + default="./payloads.json", + type=click.Path(exists=True, file_okay=True, dir_okay=False), +) +@click.option( + "--output-file", + required=False, + help="The file to save signatures to. Defaults to ./output.json.", + default="./output.json", + type=click.Path(exists=False, file_okay=True, dir_okay=False), +) @click.option( "--horcrux-file", prompt="Enter the path to the horcrux keystore", @@ -22,7 +36,9 @@ for your password as otherwise it will appear in your shell history.)""", prompt="Enter the horcrux password used during your horcrux creation", ) -def sign(horcrux_file: str, horcrux_password: str) -> None: +def sign( + payloads_file: str, output_file: str, horcrux_file: str, horcrux_password: str +) -> None: """Unlocks the keystore and signs the data.""" horcrux_file = horcrux_file.strip() if not os.path.exists(horcrux_file): @@ -32,17 +48,23 @@ def sign(horcrux_file: str, horcrux_password: str) -> None: with open(horcrux_file, "r") as key_file: keystore = HorcruxPbkdf2Keystore.create_from_json(json.load(key_file)) - signing_data = click.prompt( - text="Enter hexadecimal encoded data to sign", type=click.STRING - ).strip() - if signing_data.startswith("0x"): - signing_data = signing_data[2:] + click.echo(f"Loading payloads from {payloads_file}...") + with open(payloads_file, "r") as f: + payloads = json.load(f) # decrypt and sign data private_key = int.from_bytes(keystore.decrypt(horcrux_password), "big") - signature = bls_pop.Sign(private_key, bytes.fromhex(signing_data)) - click.echo(f"Signature: {click.style(f'0x{signature.hex()}', fg='green')}") + click.echo("Signing payloads...") + signatures = {} + for index, payload in payloads.items(): + signature = bls_pop.Sign(private_key, bytes.fromhex(payload)) + signatures[index] = signature.hex() + + with open(output_file, "w") as f: + json.dump(signatures, f) + + click.echo(f"Signatures saved to {click.style(output_file, fg='green')}...") click.echo(f"Horcrux index: {click.style(f'{keystore.index}', fg='green')}") click.echo( f"""Next steps: