From 415d829b918d0cebaac7a27d4a5b013ba701b7f8 Mon Sep 17 00:00:00 2001 From: matograine <tom.ngr@zaclys.net> Date: Thu, 24 Sep 2020 18:47:09 +0200 Subject: [PATCH] [mod] #301 : create function display_pubkey_and_checksum() and related tests --- silkaj/constants.py | 1 + silkaj/tui.py | 12 ++++++++++++ tests/test_tui.py | 21 +++++++++++++++++++-- tests/test_unit_tx.py | 6 +++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/silkaj/constants.py b/silkaj/constants.py index 207999fa..c790a12d 100644 --- a/silkaj/constants.py +++ b/silkaj/constants.py @@ -29,3 +29,4 @@ BMA_MAX_BLOCKS_CHUNK_SIZE = 5000 PUBKEY_PATTERN = "[1-9A-HJ-NP-Za-km-z]{43,44}" MINIMAL_TX_AMOUNT = 0.01 CENT_MULT_TO_UNIT = 100 +SHORT_PUBKEY_SIZE = 8 diff --git a/silkaj/tui.py b/silkaj/tui.py index 9cb35a69..955a2232 100644 --- a/silkaj/tui.py +++ b/silkaj/tui.py @@ -18,6 +18,8 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. from datetime import datetime from silkaj import wot +from silkaj import crypto_tools as ct +from silkaj.constants import SHORT_PUBKEY_SIZE def display_amount(tx, message, amount, ud_value, currency_symbol): @@ -47,6 +49,16 @@ async def display_pubkey(tx, message, pubkey): tx.append([message + " (id)", id["uid"]]) +def display_pubkey_and_checksum(pubkey, short=False, length=SHORT_PUBKEY_SIZE): + """ + Returns "<pubkey>:<checksum>" in full form. + returns `length` first chars of pubkey and checksum in short form. + `length` defaults to SHORT_PUBKEY_SIZE. + """ + short_pubkey = pubkey[:length] + "…" if short else pubkey + return short_pubkey + ":" + ct.gen_checksum(pubkey) + + def convert_time(timestamp, kind): ts = int(timestamp) date = "%Y-%m-%d" diff --git a/tests/test_tui.py b/tests/test_tui.py index 888cf4be..b66b8255 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -16,8 +16,8 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. """ import pytest -from silkaj.tui import display_pubkey, display_amount -from silkaj.constants import G1_SYMBOL +from silkaj.tui import display_pubkey, display_amount, display_pubkey_and_checksum +from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE import patched @@ -63,3 +63,20 @@ async def test_display_pubkey(message, pubkey, id, monkeypatch): tx = list() await display_pubkey(tx, message, pubkey) assert tx == expected + + +# display_pubkey_and_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( + pubkey, short=True + ) + assert pubkey[:14] + "…:" + checksum == display_pubkey_and_checksum( + pubkey, short=True, length=14 + ) diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 568e1431..ba0e09f8 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -26,7 +26,11 @@ from silkaj.tx import ( ) from silkaj.tui import display_pubkey, display_amount from silkaj.money import UDValue -from silkaj.constants import G1_SYMBOL, CENT_MULT_TO_UNIT, MINIMAL_TX_AMOUNT +from silkaj.constants import ( + G1_SYMBOL, + CENT_MULT_TO_UNIT, + MINIMAL_TX_AMOUNT, +) from duniterpy.documents.transaction import ( InputSource, Transaction, -- GitLab