From 2fba584e02df8f660bdba9f1afc99d76901e51e8 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Sun, 5 Jun 2022 15:32:34 +0200 Subject: [PATCH] Rename tui.display_pubkey_and_checksum to gen_pubkey_checksum (#386) --- silkaj/auth.py | 4 ++-- silkaj/cert.py | 4 ++-- silkaj/checksum.py | 6 +++--- silkaj/idty_tools.py | 10 ++++------ silkaj/membership.py | 2 +- silkaj/money.py | 6 +++--- silkaj/revocation.py | 6 +++--- silkaj/tui.py | 6 ++---- silkaj/tx.py | 8 ++++---- silkaj/tx_history.py | 8 ++++---- silkaj/wot.py | 10 ++++------ tests/test_idty_tools.py | 6 +++--- tests/test_membership.py | 4 ++-- tests/test_money.py | 6 ++---- tests/test_revocation.py | 4 ++-- tests/test_tui.py | 14 +++++++------- 16 files changed, 48 insertions(+), 56 deletions(-) diff --git a/silkaj/auth.py b/silkaj/auth.py index 52b936fc..b9470d9b 100644 --- a/silkaj/auth.py +++ b/silkaj/auth.py @@ -23,7 +23,7 @@ from duniterpy.key import SigningKey from duniterpy.key.scrypt_params import ScryptParams from silkaj.constants import PUBKEY_PATTERN -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum SEED_HEX_PATTERN = "^[0-9a-fA-F]{64}$" PUBSEC_PUBKEY_PATTERN = f"pub: ({PUBKEY_PATTERN})" @@ -57,7 +57,7 @@ def has_auth_method(ctx): def generate_auth_file(file): key = auth_method() authfile = Path(file) - pubkey_cksum = display_pubkey_and_checksum(key.pubkey) + pubkey_cksum = gen_pubkey_checksum(key.pubkey) if authfile.is_file(): message = f"Would you like to erase {file} by an authfile corresponding \n\ to following pubkey `{pubkey_cksum}`?" diff --git a/silkaj/cert.py b/silkaj/cert.py index 82b51859..876c2fc7 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -123,9 +123,9 @@ def certification_confirmation( cert.append( [ "Pubkey", - tui.display_pubkey_and_checksum(issuer_pubkey), + tui.gen_pubkey_checksum(issuer_pubkey), "–>", - tui.display_pubkey_and_checksum(pubkey_to_certify), + tui.gen_pubkey_checksum(pubkey_to_certify), ] ) params = get_blockchain_parameters() diff --git a/silkaj/checksum.py b/silkaj/checksum.py index f74cdd8e..d095e598 100644 --- a/silkaj/checksum.py +++ b/silkaj/checksum.py @@ -24,7 +24,7 @@ from silkaj.crypto_tools import ( PUBKEY_DELIMITED_PATTERN, gen_checksum, ) -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum MESSAGE = "You should specify a pubkey or an authentication method" @@ -38,12 +38,12 @@ MESSAGE = "You should specify a pubkey or an authentication method" def checksum_command(pubkey_checksum): if has_auth_method(): key = auth_method() - click.echo(display_pubkey_and_checksum(key.pubkey)) + click.echo(gen_pubkey_checksum(key.pubkey)) else: if not pubkey_checksum: sys.exit(MESSAGE) elif re.search(re.compile(PUBKEY_DELIMITED_PATTERN), pubkey_checksum[0]): - click.echo(display_pubkey_and_checksum(pubkey_checksum[0])) + click.echo(gen_pubkey_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): diff --git a/silkaj/idty_tools.py b/silkaj/idty_tools.py index dcd2d31d..42c7934c 100644 --- a/silkaj/idty_tools.py +++ b/silkaj/idty_tools.py @@ -27,7 +27,7 @@ from texttable import Texttable from silkaj import wot_tools as wt from silkaj.constants import ALL from silkaj.network_tools import client_instance -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum def display_identity(idty: Identity): @@ -36,7 +36,7 @@ def display_identity(idty: Identity): """ client = client_instance() id_table = list() - id_table.append(["Public key", display_pubkey_and_checksum(idty.pubkey)]) + id_table.append(["Public key", gen_pubkey_checksum(idty.pubkey)]) id_table.append(["User ID", idty.uid]) id_table.append(["Blockstamp", str(idty.block_id)]) creation_block = client(bma.blockchain.block, idty.block_id.number) @@ -65,7 +65,7 @@ def check_many_identities(document: Union[Identity, Revocation]): except urllib.error.HTTPError: sys.exit( f"{error_no_identical_id}\nuid: {idty.uid}\npubkey: \ -{display_pubkey_and_checksum(idty.pubkey)}" +{gen_pubkey_checksum(idty.pubkey)}" ) # get all matching identities @@ -91,9 +91,7 @@ def display_alternate_ids(ids_list: list): table = Texttable(max_width=shutil.get_terminal_size().columns) table.header(labels) for id in ids_list: - table.add_row( - [id.uid, display_pubkey_and_checksum(id.pubkey), str(id.block_id)[:12]] - ) + table.add_row([id.uid, gen_pubkey_checksum(id.pubkey), str(id.block_id)[:12]]) return table diff --git a/silkaj/membership.py b/silkaj/membership.py index fab1f723..c7101a1e 100644 --- a/silkaj/membership.py +++ b/silkaj/membership.py @@ -117,7 +117,7 @@ def display_confirmation_table(identity_uid, pubkey, identity_block_id): table.append(["Pending membership documents will expire", expiration]) table.append(["User Identifier (UID)", identity_uid]) - table.append(["Public Key", tui.display_pubkey_and_checksum(pubkey)]) + table.append(["Public Key", tui.gen_pubkey_checksum(pubkey)]) table.append(["Block Identity", str(identity_block_id)[:45] + "…"]) diff --git a/silkaj/money.py b/silkaj/money.py index 0f0d3f64..6e307125 100644 --- a/silkaj/money.py +++ b/silkaj/money.py @@ -27,7 +27,7 @@ from silkaj.blockchain_tools import get_head_block from silkaj.crypto_tools import is_pubkey_and_check from silkaj.network_tools import client_instance from silkaj.tools import get_currency_symbol -from silkaj.tui import display_amount, display_pubkey_and_checksum +from silkaj.tui import display_amount, gen_pubkey_checksum @command("balance", help="Get wallet balance") @@ -48,7 +48,7 @@ def cmd_amount(ctx, pubkeys): print(f"ERROR: pubkey {inputPubkey} has a wrong format") elif pubkey in pubkeys_list: sys.exit( - f"ERROR: pubkey {display_pubkey_and_checksum(pubkey)} was specified many times" + f"ERROR: pubkey {gen_pubkey_checksum(pubkey)} was specified many times" ) pubkeys_list.append(pubkey) if wrong_pubkeys: @@ -83,7 +83,7 @@ def show_amount_from_pubkey(label, inputs_balance): # if `pubkey` is a pubkey, get pubkey:checksum and uid if label != "Total": member = wt.is_member(label) - label = display_pubkey_and_checksum(label) + label = gen_pubkey_checksum(label) # display balance table display = list() display.append(["Balance of pubkey", label]) diff --git a/silkaj/revocation.py b/silkaj/revocation.py index eb11ee56..9ec1fe3d 100644 --- a/silkaj/revocation.py +++ b/silkaj/revocation.py @@ -46,7 +46,7 @@ def save(ctx: click.core.Context, file: str): currency = get_currency() key = auth.auth_method() - tui.display_pubkey_and_checksum(key.pubkey) + tui.gen_pubkey_checksum(key.pubkey) id = (wot.choose_identity(key.pubkey))[0] rev_doc = create_revocation_doc(id, key.pubkey, currency) rev_doc.sign(key) @@ -79,7 +79,7 @@ def revoke_now(ctx: click.core.Context): warn_before_dry_run_or_display(ctx) key = auth.auth_method() - tui.display_pubkey_and_checksum(key.pubkey) + tui.gen_pubkey_checksum(key.pubkey) id = (wot.choose_identity(key.pubkey))[0] rev_doc = create_revocation_doc(id, key.pubkey, currency) rev_doc.sign(key) @@ -184,7 +184,7 @@ def create_revocation_doc(id, pubkey: str, currency: str): def save_doc(path: str, content: str, pubkey: str): - pubkey_cksum = tui.display_pubkey_and_checksum(pubkey) + pubkey_cksum = tui.gen_pubkey_checksum(pubkey) rev_path = Path(path) # Ask confirmation if the file exists if rev_path.is_file(): diff --git a/silkaj/tui.py b/silkaj/tui.py index c3558251..9afc551d 100644 --- a/silkaj/tui.py +++ b/silkaj/tui.py @@ -40,15 +40,13 @@ def display_pubkey(tx, message, pubkey): """ Displays a pubkey and the eventually associated id. """ - tx.append([f"{message} (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]) + tx.append([f"{message} (pubkey:checksum)", gen_pubkey_checksum(pubkey)]) id = wot_tools.is_member(pubkey) if id: tx.append([f"{message} (id)", id["uid"]]) -def display_pubkey_and_checksum( - pubkey, short=False, length=constants.SHORT_PUBKEY_SIZE -): +def gen_pubkey_checksum(pubkey, short=False, length=constants.SHORT_PUBKEY_SIZE): """ Returns "<pubkey>:<checksum>" in full form. returns `length` first chars of pubkey and checksum in short form. diff --git a/silkaj/tx.py b/silkaj/tx.py index d7dd0588..00f6d813 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -145,7 +145,7 @@ def send_transaction( if allsources: if pubkey_amount[0] <= 0: tools.message_exit( - f"Error: Issuer pubkey {tui.display_pubkey_and_checksum(issuer_pubkey)} is empty. \ + f"Error: Issuer pubkey {tui.gen_pubkey_checksum(issuer_pubkey)} is empty. \ No transaction sent." ) @@ -296,7 +296,7 @@ def check_transaction_values( if ct.check_pubkey_format(outputBackChange): outputBackChange = ct.validate_checksum(outputBackChange) if enough_source: - pubkey = tui.display_pubkey_and_checksum(issuer_pubkey) + pubkey = tui.gen_pubkey_checksum(issuer_pubkey) tools.message_exit( f"{pubkey} pubkey doesn’t have enough money for this transaction." ) @@ -452,7 +452,7 @@ def generate_and_send_transaction( print("Generate Change Transaction") else: print("Generate Transaction:") - print(" - From: " + tui.display_pubkey_and_checksum(issuers)) + print(" - From: " + tui.gen_pubkey_checksum(issuers)) for tx_amount, outputAddress in zip(tx_amounts, outputAddresses): display_sent_tx(outputAddress, tx_amount) print(" - Total: " + str(sum(tx_amounts) / 100)) @@ -472,7 +472,7 @@ def generate_and_send_transaction( def display_sent_tx(outputAddress, amount): print( " - To: ", - tui.display_pubkey_and_checksum(outputAddress), + tui.gen_pubkey_checksum(outputAddress), "\n - Amount: ", amount / 100, ) diff --git a/silkaj/tx_history.py b/silkaj/tx_history.py index 7dca8b00..1b69842e 100644 --- a/silkaj/tx_history.py +++ b/silkaj/tx_history.py @@ -28,7 +28,7 @@ from silkaj.crypto_tools import check_pubkey_format, validate_checksum from silkaj.money import amount_in_current_base, get_amount_from_pubkey, get_ud_value from silkaj.network_tools import client_instance from silkaj.tools import get_currency_symbol -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum @command("history", help="Display transaction history") @@ -63,7 +63,7 @@ def generate_header(pubkey, currency_symbol, ud_value): balance = get_amount_from_pubkey(pubkey) balance_ud = round(balance[1] / ud_value, 2) date = now().format(ALL) - return f'Transactions history from: {idty["uid"]} {display_pubkey_and_checksum(pubkey)}\n\ + return f'Transactions history from: {idty["uid"]} {gen_pubkey_checksum(pubkey)}\n\ Current balance: {balance[1] / 100} {currency_symbol}, {balance_ud} UD {currency_symbol} on {date}\n' @@ -240,10 +240,10 @@ def output_available(condition, comparison, value): def assign_idty_from_pubkey(pubkey, identities, full_pubkey): - idty = display_pubkey_and_checksum(pubkey, short=not full_pubkey) + idty = gen_pubkey_checksum(pubkey, short=not full_pubkey) for identity in identities: if pubkey == identity["pubkey"]: - pubkey_mod = display_pubkey_and_checksum(pubkey, short=not full_pubkey) + pubkey_mod = gen_pubkey_checksum(pubkey, short=not full_pubkey) idty = f'{identity["uid"]} - {pubkey_mod}' return idty diff --git a/silkaj/wot.py b/silkaj/wot.py index 4803c803..49d8fcc1 100644 --- a/silkaj/wot.py +++ b/silkaj/wot.py @@ -26,7 +26,7 @@ from silkaj.blockchain_tools import get_blockchain_parameters from silkaj.constants import DATE from silkaj.crypto_tools import is_pubkey_and_check from silkaj.network_tools import client_instance, exit_on_http_error -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum def get_sent_certifications(signed, time_first_block, params): @@ -89,7 +89,7 @@ def received_sent_certifications(uid_pubkey): table = tabulate( certifications, headers="keys", tablefmt="orgtbl", stralign="right" ) - txt = f'{identity["uid"]} ({display_pubkey_and_checksum(pubkey, True)}) \ + txt = f'{identity["uid"]} ({gen_pubkey_checksum(pubkey, True)}) \ from block #{identity["meta"]["timestamp"][:15]}…\n\ received {len(certifications["received"])} and \ sent {nbr_sent_certs}/{params["sigStock"]} certifications:\n\ @@ -156,7 +156,7 @@ def id_pubkey_correspondence(uid_pubkey): content = f"Public keys or user id found matching '{uid_pubkey}':\n" for lookup in lookups: for identity in lookup["uids"]: - pubkey_checksum = display_pubkey_and_checksum(lookup["pubkey"]) + pubkey_checksum = gen_pubkey_checksum(lookup["pubkey"]) content += f'\n→ {pubkey_checksum} ↔ {identity["uid"]}' click.echo(content) @@ -179,9 +179,7 @@ def choose_identity(pubkey_uid): for pubkey_index, lookup in enumerate(lookups): for uid_index, identity in enumerate(lookup["uids"]): identities_choices["id"].append(str(pubkey_index) + str(uid_index)) - identities_choices["pubkey"].append( - display_pubkey_and_checksum(lookup["pubkey"]) - ) + identities_choices["pubkey"].append(gen_pubkey_checksum(lookup["pubkey"])) identities_choices["uid"].append(identity["uid"]) identities_choices["timestamp"].append( identity["meta"]["timestamp"][:20] + "…" diff --git a/tests/test_idty_tools.py b/tests/test_idty_tools.py index ec2a5ba6..872d8b3a 100644 --- a/tests/test_idty_tools.py +++ b/tests/test_idty_tools.py @@ -32,7 +32,7 @@ from patched.idty_tools import ( ) from silkaj import idty_tools from silkaj.constants import ALL, PUBKEY_PATTERN -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum # used test identities @@ -362,7 +362,7 @@ def test_display_identity(idty, monkeypatch, capsys): None, [ f"Identity document does not match any valid identity.\n\ -uid: {idty1.uid}\npubkey: {display_pubkey_and_checksum(idty1.pubkey)}" +uid: {idty1.uid}\npubkey: {gen_pubkey_checksum(idty1.pubkey)}" ], False, ), @@ -396,7 +396,7 @@ def test_check_many_identities( # identity does not exist if expected == [ f"Identity document does not match any valid identity.\ -\nuid: {idty.uid}\npubkey: {display_pubkey_and_checksum(idty.pubkey)}" +\nuid: {idty.uid}\npubkey: {gen_pubkey_checksum(idty.pubkey)}" ]: with pytest.raises(SystemExit) as pytest_exit: result = idty_tools.check_many_identities(idty) diff --git a/tests/test_membership.py b/tests/test_membership.py index 2643e9dc..72b2dfd0 100644 --- a/tests/test_membership.py +++ b/tests/test_membership.py @@ -39,7 +39,7 @@ from silkaj.blockchain_tools import get_blockchain_parameters from silkaj.cli import cli from silkaj.constants import DATE from silkaj.network_tools import client_instance -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum # Values and patches pubkey = "EA7Dsw39ShZg4SpURsrgMaMqrweJPUFPYHwZA8e92e3D" @@ -158,7 +158,7 @@ def test_display_confirmation_table(patched_wot_requirements, monkeypatch, capsy table.append(["Pending membership documents will expire", expiration]) table.append(["User Identifier (UID)", identity_uid]) - table.append(["Public Key", display_pubkey_and_checksum(pubkey)]) + table.append(["Public Key", gen_pubkey_checksum(pubkey)]) table.append(["Block Identity", str(identity_block_id)[:45] + "…"]) diff --git a/tests/test_money.py b/tests/test_money.py index 0b3a6af9..26774e55 100644 --- a/tests/test_money.py +++ b/tests/test_money.py @@ -21,7 +21,7 @@ from silkaj.constants import FAILURE_EXIT_STATUS from silkaj.money import get_sources # had to import from wot to prevent loop dependencies -from silkaj.wot import display_pubkey_and_checksum +from silkaj.wot import gen_pubkey_checksum def test_get_sources(monkeypatch): @@ -125,9 +125,7 @@ def test_balance_errors(): "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh", ], ) - pubkeyCk = display_pubkey_and_checksum( - "BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh" - ) + pubkeyCk = gen_pubkey_checksum("BFb5yv8z1fowR6Z8mBXTALy5z7gHfMU976WtXhmRsUMh") assert f"ERROR: pubkey {pubkeyCk} was specified many times" in result.output assert result.exit_code == FAILURE_EXIT_STATUS diff --git a/tests/test_revocation.py b/tests/test_revocation.py index 2001318d..956a5d7a 100644 --- a/tests/test_revocation.py +++ b/tests/test_revocation.py @@ -31,7 +31,7 @@ from silkaj import auth, blockchain_tools, idty_tools, revocation, wot from silkaj.cli import cli from silkaj.constants import FAILURE_EXIT_STATUS, SUCCESS_EXIT_STATUS from silkaj.network_tools import client_instance -from silkaj.tui import display_pubkey_and_checksum +from silkaj.tui import gen_pubkey_checksum # Useful function @@ -966,7 +966,7 @@ def test_save_doc(path, rev_1, rev_2, pubkey, capsys, monkeypatch): monkeypatch.setattr(click, "confirm", value=conf_true) revocation.save_doc(path, rev_2.signed_raw(), pubkey) expected_confirm = f"Revocation document file stored into `{path}` \ -for following public key: {display_pubkey_and_checksum(pubkey)}" +for following public key: {gen_pubkey_checksum(pubkey)}" assert expected_confirm in capsys.readouterr().out with open(path) as f: assert f.read() == rev_2.signed_raw() diff --git a/tests/test_tui.py b/tests/test_tui.py index 61861297..e4592ad4 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -19,7 +19,7 @@ from patched.test_constants import mock_ud_value from patched.wot import patched_is_member from silkaj import wot_tools from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE -from silkaj.tui import display_amount, display_pubkey, display_pubkey_and_checksum +from silkaj.tui import display_amount, display_pubkey, gen_pubkey_checksum # display_amount() @@ -50,7 +50,7 @@ def test_display_amount(message, amount, currency_symbol): def test_display_pubkey(message, pubkey, id, monkeypatch): monkeypatch.setattr(wot_tools, "is_member", patched_is_member) - expected = [[f"{message} (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]] + expected = [[f"{message} (pubkey:checksum)", gen_pubkey_checksum(pubkey)]] if id: expected.append([f"{message} (id)", id]) tx = list() @@ -58,18 +58,18 @@ def test_display_pubkey(message, pubkey, id, monkeypatch): assert tx == expected -# display_pubkey_and_checksum +# gen_pubkey_checksum @pytest.mark.parametrize( "pubkey, checksum", [ ("J4c8CARmP9vAFNGtHRuzx14zvxojyRWHW2darguVqjtX", "KAv"), ], ) -def test_display_pubkey_and_checksum(pubkey, checksum): - assert pubkey + ":" + checksum == display_pubkey_and_checksum(pubkey) - assert pubkey[:SHORT_PUBKEY_SIZE] + "…:" + checksum == display_pubkey_and_checksum( +def test_gen_pubkey_checksum(pubkey, checksum): + assert pubkey + ":" + checksum == gen_pubkey_checksum(pubkey) + assert pubkey[:SHORT_PUBKEY_SIZE] + "…:" + checksum == gen_pubkey_checksum( pubkey, short=True ) - assert pubkey[:14] + "…:" + checksum == display_pubkey_and_checksum( + assert pubkey[:14] + "…:" + checksum == gen_pubkey_checksum( pubkey, short=True, length=14 ) -- GitLab