diff --git a/silkaj/constants.py b/silkaj/constants.py index 207999faad08e0d168aabcf8aa6b831143b38994..c790a12daa0f082b4aebf34a420ecd4dff2c9cad 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 9cb35a69a9585bf649f5021b1ad204205b1df527..955a223234ab1b38c774da37061bd20902e67f59 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 888cf4be7662edcee4353c556d75291b7950c4f0..b66b825553e7e89a9f68dd6cc438220e11717d7a 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 568e1431ab8046db6b0e523a19506f147ee8ac47..ba0e09f813b2cfbeabe680042b4d8580fae5e7b3 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,