From 9b9925c739e41386b4c14dc7e3069b39460b5744 Mon Sep 17 00:00:00 2001
From: matograine <tom.ngr@zaclys.net>
Date: Mon, 21 Sep 2020 20:20:36 +0200
Subject: [PATCH] [enh] #301 : make `wot` and `id` commands use
 <pubkey:checksum> format     * display     * parameter * change
 tests/test_wot and test_end_to_end to match new behavior

* change in choose_identity() will affect commands `cert` and `membership` display.
---
 silkaj/wot.py            | 17 ++++++++++++-----
 tests/test_end_to_end.py |  2 +-
 tests/test_wot.py        |  6 +++++-
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/silkaj/wot.py b/silkaj/wot.py
index 25a029d6..ddbe2a16 100644
--- a/silkaj/wot.py
+++ b/silkaj/wot.py
@@ -26,7 +26,7 @@ from duniterpy.api.errors import DuniterError
 from silkaj.network_tools import ClientInstance
 from silkaj.crypto_tools import is_pubkey_and_check
 from silkaj.tools import message_exit, coroutine
-from silkaj.tui import convert_time
+from silkaj.tui import convert_time, display_pubkey_and_checksum
 from silkaj.blockchain_tools import BlockchainParams
 from silkaj.constants import ASYNC_SLEEP
 
@@ -60,6 +60,11 @@ async def received_sent_certifications(uid_pubkey):
     client = ClientInstance().client
     first_block = await client(blockchain.block, 1)
     time_first_block = first_block["time"]
+
+    checked_pubkey = is_pubkey_and_check(uid_pubkey)
+    if checked_pubkey:
+        uid_pubkey = checked_pubkey
+
     identity, pubkey, signed = await choose_identity(uid_pubkey)
     certifications = OrderedDict()
     params = await BlockchainParams().params
@@ -84,7 +89,7 @@ async def received_sent_certifications(uid_pubkey):
     print(
         "{0} ({1}) from block #{2}\nreceived {3} and sent {4}/{5} certifications:\n{6}\n{7}\n".format(
             identity["uid"],
-            pubkey[:5] + "…",
+            display_pubkey_and_checksum(pubkey, True),
             identity["meta"]["timestamp"][:15] + "…",
             len(certifications["received"]),
             nbr_sent_certs,
@@ -167,7 +172,7 @@ async def id_pubkey_correspondence(id_pubkey):
             idty = await identity_of(id_pubkey)
             print(
                 "{} public key corresponds to identity: {}".format(
-                    id_pubkey, idty["uid"]
+                    display_pubkey_and_checksum(id_pubkey), idty["uid"]
                 )
             )
         except:
@@ -177,7 +182,7 @@ async def id_pubkey_correspondence(id_pubkey):
         pubkeys = await wot_lookup(id_pubkey)
         print("Public keys found matching '{}':\n".format(id_pubkey))
         for pubkey in pubkeys:
-            print("→", pubkey["pubkey"], end=" ")
+            print("→", display_pubkey_and_checksum(pubkey["pubkey"]), end=" ")
             try:
                 corresponding_id = await client(wot.identity_of, pubkey["pubkey"])
                 print("↔ " + corresponding_id["uid"])
@@ -200,7 +205,9 @@ async 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(lookup["pubkey"])
+            identities_choices["pubkey"].append(
+                display_pubkey_and_checksum(lookup["pubkey"])
+            )
             identities_choices["uid"].append(identity["uid"])
             identities_choices["timestamp"].append(
                 identity["meta"]["timestamp"][:20] + "…"
diff --git a/tests/test_end_to_end.py b/tests/test_end_to_end.py
index 3c8fa306..b5e34027 100644
--- a/tests/test_end_to_end.py
+++ b/tests/test_end_to_end.py
@@ -31,7 +31,7 @@ def test_wot():
     """tests 'silkaj wot' returns a number of members"""
 
     output = check_output(silkaj + ["wot", "moul"]).decode()
-    assert "moul (GfKER…) from block #0-E3B0C44298FC1…" in output
+    assert "moul (GfKERHnJ…:J1k) from block #0-E3B0C44298FC1…" in output
     assert "received_expire" in output
     assert "received" in output
     assert "sent" in output
diff --git a/tests/test_wot.py b/tests/test_wot.py
index a4789a38..9f05df3f 100644
--- a/tests/test_wot.py
+++ b/tests/test_wot.py
@@ -24,6 +24,8 @@ from silkaj import wot
 pubkey_titi_tata = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88"
 pubkey_toto_tutu = "totoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88"
 
+pubkey_titi_tata_checksum = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88:9iP"
+
 
 def identity_card(uid, timestamp):
     return {
@@ -135,6 +137,7 @@ def patched_prompt_tutu(message):
     "selected_uid, pubkey, patched_prompt, patched_lookup",
     [
         ("titi", pubkey_titi_tata, patched_prompt_titi, patched_lookup_one),
+        ("titi", pubkey_titi_tata_checksum, patched_prompt_titi, patched_lookup_one),
         ("tata", pubkey_titi_tata, patched_prompt_tata, patched_lookup_two),
         ("toto", pubkey_toto_tutu, patched_prompt_toto, patched_lookup_three),
         ("tutu", pubkey_toto_tutu, patched_prompt_tutu, patched_lookup_four),
@@ -148,7 +151,8 @@ async def test_choose_identity(
     monkeypatch.setattr(wot, "wot_lookup", patched_lookup)
     monkeypatch.setattr(click, "prompt", patched_prompt)
     identity_card, get_pubkey, signed = await wot.choose_identity(pubkey)
-    assert pubkey == get_pubkey
+    expected_pubkey = pubkey.split(":")[0]
+    assert expected_pubkey == get_pubkey
     assert selected_uid == identity_card["uid"]
 
     # Check whether the table is not displayed in case of one identity
-- 
GitLab