diff --git a/silkaj/checksum.py b/silkaj/checksum.py new file mode 100644 index 0000000000000000000000000000000000000000..d0a6806cbc10752e5772ac3e657268e6c4daea86 --- /dev/null +++ b/silkaj/checksum.py @@ -0,0 +1,56 @@ +""" +Copyright 2016-2020 Maël Azimi <m.a@moul.re> + +Silkaj is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Silkaj is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Silkaj. If not, see <https://www.gnu.org/licenses/>. +""" + +import re +import click + +from silkaj.auth import auth_method, has_auth_method +from silkaj.crypto_tools import ( + gen_checksum, + PUBKEY_DELIMITED_PATTERN, + PUBKEY_CHECKSUM_PATTERN, +) +from silkaj.tools import message_exit +from silkaj.tui import display_pubkey_and_checksum + + +MESSAGE = "You should specify a pubkey or an authentication method" + + +@click.command( + "checksum", + help="Generate checksum out of a passed pubkey or an authentication method.\ + Can also check if the checksum is valid", +) +@click.argument("pubkey_checksum", nargs=-1) +def checksum_command(pubkey_checksum): + if has_auth_method(): + key = auth_method() + click.echo(display_pubkey_and_checksum(key.pubkey)) + else: + if not pubkey_checksum: + message_exit(MESSAGE) + elif re.search(re.compile(PUBKEY_DELIMITED_PATTERN), pubkey_checksum[0]): + click.echo(display_pubkey_and_checksum(pubkey_checksum[0])) + elif re.search(re.compile(PUBKEY_CHECKSUM_PATTERN), pubkey_checksum[0]): + pubkey, checksum = pubkey_checksum[0].split(":") + if checksum == gen_checksum(pubkey): + click.echo("The checksum is valid") + else: + click.echo("The checksum is invalid") + else: + message_exit("Error: Wrong public key format") diff --git a/silkaj/cli.py b/silkaj/cli.py index 942f9d8c8dd727113cd9c27349588d5e1bc9eebe..544550ddc9af7ae20ca8add014fa49891b10121b 100644 --- a/silkaj/cli.py +++ b/silkaj/cli.py @@ -23,6 +23,7 @@ from silkaj.tx import send_transaction from silkaj.tx_history import transaction_history from silkaj.money import cmd_amount from silkaj.cert import send_certification +from silkaj.checksum import checksum_command from silkaj.commands import ( currency_info, difficulties, @@ -104,6 +105,7 @@ cli.add_command(generate_auth_file) cli.add_command(cmd_amount) cli.add_command(list_blocks) cli.add_command(send_certification) +cli.add_command(checksum_command) cli.add_command(difficulties) cli.add_command(transaction_history) cli.add_command(id_pubkey_correspondence) diff --git a/tests/test_checksum.py b/tests/test_checksum.py new file mode 100644 index 0000000000000000000000000000000000000000..a7a5a475e4473e3b555399ecb6be0ac055e8af3f --- /dev/null +++ b/tests/test_checksum.py @@ -0,0 +1,50 @@ +""" +Copyright 2016-2020 Maël Azimi <m.a@moul.re> + +Silkaj is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Silkaj is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Silkaj. If not, see <https://www.gnu.org/licenses/>. +""" + +import pytest +from click.testing import CliRunner + +from silkaj.cli import cli +from silkaj.checksum import MESSAGE + + +pubkey = "3rp7ahDGeXqffBQTnENiXEFXYS7BRjYmS33NbgfCuDc8" +checksum = "DFQ" +pubkey_checksum = pubkey + ":" + checksum +pubkey_seedhex_authfile = ( + "3bc6f2484e441e40562155235cdbd8ce04c25e7df35bf5f87c067bf239db8511" +) + + +@pytest.mark.parametrize( + "command, excepted_output", + [ + (["checksum", pubkey_checksum], "The checksum is valid"), + (["checksum", pubkey + ":vAK"], "The checksum is invalid"), + (["checksum", pubkey], pubkey_checksum), + (["checksum", "uid"], "Error: Wrong public key format"), + (["checksum"], MESSAGE), + (["--auth-file", "checksum"], pubkey_checksum), + (["--auth-file", "checksum", "pubkey"], pubkey_checksum), + ], +) +def test_checksum_command(command, excepted_output): + with CliRunner().isolated_filesystem(): + with open("authfile", "w") as f: + f.write(pubkey_seedhex_authfile) + result = CliRunner().invoke(cli, args=command) + assert result.output == excepted_output + "\n"