diff --git a/silkaj/auth.py b/silkaj/auth.py index 65f8496c440adfed7e9d6682246c8b1a02cedecd..74b16f83063d47d436a975d6983b7387583f251b 100644 --- a/silkaj/auth.py +++ b/silkaj/auth.py @@ -23,8 +23,7 @@ from pathlib import Path from duniterpy.key import SigningKey from duniterpy.key.scrypt_params import ScryptParams -# had to import display_pubkey_and_checksum from wot to avoid loop dependency. -from silkaj.wot import display_pubkey_and_checksum +from silkaj.tui import display_pubkey_and_checksum from silkaj.tools import message_exit from silkaj.constants import PUBKEY_PATTERN diff --git a/silkaj/cert.py b/silkaj/cert.py index 4c7f0ec3d732ce09ac36a81d4c70d4fc11a12421..01cc2bcf794424e174853e62b96c22b322992404 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -31,6 +31,7 @@ from silkaj.license import license_approval from silkaj import wot, tui from silkaj.constants import SUCCESS_EXIT_STATUS, DATE, ALL from silkaj.crypto_tools import is_pubkey_and_check +from silkaj import wot_tools as wt @click.command("cert", help="Send certification") @@ -92,7 +93,7 @@ async def send_certification(ctx, uid_pubkey_to_certify): async def pre_checks(client, issuer_pubkey, pubkey_to_certify): # Check whether current user is member - issuer = await wot.is_member(issuer_pubkey) + issuer = await wt.is_member(issuer_pubkey) if not issuer: message_exit("Current identity is not member.") diff --git a/silkaj/commands.py b/silkaj/commands.py index 8a47ee492737e7f6e3e8d6caaec3763b56ebbe21..2fcb797d04624b4932f26914bc8512d2ef16e67e 100644 --- a/silkaj/commands.py +++ b/silkaj/commands.py @@ -29,7 +29,7 @@ from _socket import gaierror from duniterpy.api import bma from silkaj.tools import coroutine -from silkaj.wot import identity_of +from silkaj.wot_tools import identity_of from silkaj.network_tools import ( best_endpoint_address, EndPoint, diff --git a/silkaj/money.py b/silkaj/money.py index 15f051aa265d51ea41436614fee85781facf0dc2..a4596771dba4bff0104c42c63f7bba5720dfc9cf 100644 --- a/silkaj/money.py +++ b/silkaj/money.py @@ -18,13 +18,11 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. from click import command, argument, pass_context, echo from tabulate import tabulate +from silkaj import wot_tools as wt from silkaj.network_tools import ClientInstance from silkaj.blockchain_tools import HeadBlock from silkaj.tools import CurrencySymbol, message_exit, coroutine from silkaj.auth import auth_method, has_auth_method - -# had to import wot to prevent loop dependency. No use here. -from silkaj import wot from silkaj.crypto_tools import ( is_pubkey_and_check, check_pubkey_format, @@ -91,7 +89,7 @@ async def show_amount_from_pubkey(label, inputs_balance): # if `pubkey` is a pubkey, get pubkey:checksum and uid if label != "Total": - member = await wot.is_member(label) + member = await wt.is_member(label) label = display_pubkey_and_checksum(label) # display balance table display = list() diff --git a/silkaj/tui.py b/silkaj/tui.py index 2b09d00f7261902c4598d22b87253b86863f99a6..87463bd2c0fc51888de8dc3ef2d64a41e9b759cd 100644 --- a/silkaj/tui.py +++ b/silkaj/tui.py @@ -19,7 +19,7 @@ import sys import click from datetime import datetime -from silkaj import wot, network_tools, constants +from silkaj import network_tools, constants, wot_tools from silkaj import crypto_tools as ct @@ -45,7 +45,7 @@ async def display_pubkey(tx, message, pubkey): Displays a pubkey and the eventually associated id. """ tx.append([message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]) - id = await wot.is_member(pubkey) + id = await wot_tools.is_member(pubkey) if id: tx.append([message + " (id)", id["uid"]]) @@ -67,19 +67,3 @@ async def send_doc_confirmation(document_name): client = network_tools.ClientInstance().client await client.close() sys.exit(constants.SUCCESS_EXIT_STATUS) - - -def convert_time(timestamp, kind): - ts = int(timestamp) - date = "%Y-%m-%d" - hour = "%H:%M" - second = ":%S" - if kind == "all": - pattern = date + " " + hour + second - elif kind == "date": - pattern = date - elif kind == "hour": - pattern = hour - if ts >= 3600: - pattern += second - return datetime.fromtimestamp(ts).strftime(pattern) diff --git a/silkaj/tx_history.py b/silkaj/tx_history.py index a083f88e08c89cf1c8c2e68272235f854fafca7c..f005605301403f78855f9ddefe2a7f8d75456c7a 100644 --- a/silkaj/tx_history.py +++ b/silkaj/tx_history.py @@ -24,6 +24,7 @@ from time import time from duniterpy.api.bma.tx import history from duniterpy.documents.transaction import Transaction +from silkaj import wot_tools as wt from silkaj.network_tools import ClientInstance from silkaj.tools import coroutine from silkaj.tui import display_pubkey_and_checksum @@ -62,7 +63,7 @@ async def transaction_history(pubkey, uids, full_pubkey): async def generate_header(pubkey, currency_symbol, ud_value): try: - idty = await wot.identity_of(pubkey) + idty = await wt.identity_of(pubkey) except: idty = dict([("uid", "")]) balance = await get_amount_from_pubkey(pubkey) @@ -149,7 +150,7 @@ async def parse_received_tx( for received_tx in received_txs: for issuer in received_tx.issuers: issuers.append(issuer) - identities = await wot.identities_from_pubkeys(issuers, uids) + identities = await wt.identities_from_pubkeys(issuers, uids) for received_tx in received_txs: tx_list = list() tx_list.append(pendulum.from_timestamp(received_tx.time).format(ALL)) @@ -181,7 +182,7 @@ async def parse_sent_tx(sent_txs_table, sent_txs, pubkey, ud_value, uids, full_p if output_available(output.condition, ne, pubkey): pubkeys.append(output.condition.left.pubkey) - identities = await wot.identities_from_pubkeys(pubkeys, uids) + identities = await wt.identities_from_pubkeys(pubkeys, uids) for sent_tx in sent_txs: tx_list = list() tx_list.append(pendulum.from_timestamp(sent_tx.time).format(ALL)) diff --git a/silkaj/wot.py b/silkaj/wot.py index 878c76f0493a5aeb7942cca5d7aac6dccad1ae0c..f88a6f5233a9d35e5cf5f42f505e882e9b2e8f61 100644 --- a/silkaj/wot.py +++ b/silkaj/wot.py @@ -20,16 +20,16 @@ import pendulum from time import time from tabulate import tabulate from collections import OrderedDict -from asyncio import sleep from duniterpy.api.bma import wot, blockchain from duniterpy.api.errors import DuniterError +from silkaj import wot_tools as wt from silkaj.network_tools import ClientInstance from silkaj.crypto_tools import is_pubkey_and_check -from silkaj.tools import message_exit, coroutine +from silkaj.tools import coroutine from silkaj.tui import display_pubkey_and_checksum from silkaj.blockchain_tools import BlockchainParams -from silkaj.constants import ASYNC_SLEEP, DATE +from silkaj.constants import DATE def get_sent_certifications(signed, time_first_block, params): @@ -124,7 +124,7 @@ async def membership_status(certifications, pubkey, req): len(certifications["received"]) - params["sigQty"] ] ) - member = await is_member(pubkey) + member = await wt.is_member(pubkey) if member: member = True print("member:", member) @@ -168,7 +168,7 @@ async def id_pubkey_correspondence(uid_pubkey): uid_pubkey = checked_pubkey client = ClientInstance().client - lookups = await wot_lookup(uid_pubkey) + lookups = await wt.wot_lookup(uid_pubkey) await client.close() content = f"Public keys or user id found matching '{uid_pubkey}':\n" @@ -186,7 +186,7 @@ async def choose_identity(pubkey_uid): If there is one uid, returns it If there is multiple uids, prompt a selector """ - lookups = await wot_lookup(pubkey_uid) + lookups = await wt.wot_lookup(pubkey_uid) # Generate table containing the choices identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []} @@ -223,62 +223,3 @@ async def choose_identity(pubkey_uid): lookups[pubkey_index]["pubkey"], lookups[pubkey_index]["signed"], ) - - -async def identity_of(pubkey_uid): - """ - Only works for members - Not able to get corresponding uid from a non-member identity - Able to know if an identity is member or not - """ - client = ClientInstance().client - try: - return await client(wot.identity_of, pubkey_uid) - except ValueError as e: - pass - - -async def is_member(pubkey_uid): - """ - Check identity is member - If member, return corresponding identity, else: False - """ - try: - return await identity_of(pubkey_uid) - except: - return False - - -async def wot_lookup(identifier): - """ - :identifier: identity or pubkey in part or whole - Return received and sent certifications lists of matching identities - if one identity found - """ - client = ClientInstance().client - try: - results = await client(wot.lookup, identifier) - return results["results"] - except DuniterError as e: - message_exit(e.message) - except ValueError as e: - pass - - -async def identities_from_pubkeys(pubkeys, uids): - """ - Make list of pubkeys unique, and remove empty strings - Request identities - """ - if not uids: - return list() - - uniq_pubkeys = list(filter(None, set(pubkeys))) - identities = list() - for pubkey in uniq_pubkeys: - try: - identities.append(await identity_of(pubkey)) - except Exception as e: - pass - await sleep(ASYNC_SLEEP) - return identities diff --git a/silkaj/wot_tools.py b/silkaj/wot_tools.py new file mode 100644 index 0000000000000000000000000000000000000000..829b105fb3cfc7ca548a00eb6ceeeb3c39ee00c9 --- /dev/null +++ b/silkaj/wot_tools.py @@ -0,0 +1,83 @@ +""" +Copyright 2016-2021 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/>. +""" + +from asyncio import sleep +from duniterpy.api.bma import wot +from duniterpy.api.errors import DuniterError + +from silkaj.network_tools import ClientInstance +from silkaj.tools import message_exit +from silkaj.constants import ASYNC_SLEEP + + +async def identity_of(pubkey_uid): + """ + Only works for members + Not able to get corresponding uid from a non-member identity + Able to know if an identity is member or not + """ + client = ClientInstance().client + try: + return await client(wot.identity_of, pubkey_uid) + except ValueError as e: + pass + + +async def is_member(pubkey_uid): + """ + Check identity is member + If member, return corresponding identity, else: False + """ + try: + return await identity_of(pubkey_uid) + except: + return False + + +async def wot_lookup(identifier): + """ + :identifier: identity or pubkey in part or whole + Return received and sent certifications lists of matching identities + if one identity found + """ + client = ClientInstance().client + try: + results = await client(wot.lookup, identifier) + return results["results"] + except DuniterError as e: + message_exit(e.message) + except ValueError as e: + pass + + +async def identities_from_pubkeys(pubkeys, uids): + """ + Make list of pubkeys unique, and remove empty strings + Request identities + """ + if not uids: + return list() + + uniq_pubkeys = list(filter(None, set(pubkeys))) + identities = list() + for pubkey in uniq_pubkeys: + try: + identities.append(await identity_of(pubkey)) + except Exception as e: + pass + await sleep(ASYNC_SLEEP) + return identities diff --git a/tests/test_tui.py b/tests/test_tui.py index ca39bd96a7d9f2e15f7a67cf4acb2dd045c5bb6c..3fd413d132c2cad988246eeb61a170fad52103d7 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -18,6 +18,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import pytest from silkaj.tui import display_pubkey, display_amount, display_pubkey_and_checksum from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE +from silkaj import wot_tools from patched.wot import patched_is_member from patched.test_constants import mock_ud_value @@ -55,7 +56,7 @@ def test_display_amount(message, amount, currency_symbol): ) @pytest.mark.asyncio async def test_display_pubkey(message, pubkey, id, monkeypatch): - monkeypatch.setattr("silkaj.wot.is_member", patched_is_member) + monkeypatch.setattr(wot_tools, "is_member", patched_is_member) expected = [[message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]] if id: diff --git a/tests/test_tx_history.py b/tests/test_tx_history.py index 362ff6a5fd5fe6353aed356bcf52d27ff5d5bd58..ace734042b788421cbb0f7a1dd243f8dac66dd91 100644 --- a/tests/test_tx_history.py +++ b/tests/test_tx_history.py @@ -16,7 +16,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. """ import pytest -from silkaj import tx_history, wot +from silkaj import tx_history, wot, wot_tools from silkaj.constants import ( G1_DEFAULT_ENDPOINT, SHORT_PUBKEY_SIZE, @@ -48,7 +48,9 @@ async def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): # uid is at least one char : "<uid> - <pubkey>" adds min 4 chars to <pubkey> return pubkey + 4 - monkeypatch.setattr(wot, "identities_from_pubkeys", patched_identities_from_pubkeys) + monkeypatch.setattr( + wot_tools, "identities_from_pubkeys", patched_identities_from_pubkeys + ) client = "whatever" ud_value = 10.07 diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 0eee16e5ae95938fe1df9f82503c7d8099de21f7..62fc092c722e053c2e2b974be884759d036a73fe 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -18,7 +18,16 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import sys import pytest -from silkaj import tx, wot, money, tools, blockchain_tools, auth, network_tools +from silkaj import ( + tx, + wot, + wot_tools, + money, + tools, + blockchain_tools, + auth, + network_tools, +) from silkaj.tui import display_pubkey, display_amount from silkaj.constants import ( G1_SYMBOL, @@ -128,7 +137,7 @@ async def test_gen_confirmation_table( monkeypatch, ): # patched functions - monkeypatch.setattr(wot, "is_member", patched_is_member) + monkeypatch.setattr(wot_tools, "is_member", patched_is_member) monkeypatch.setattr(money.UDValue, "get_ud_value", patched_ud_value) monkeypatch.setattr(tools.CurrencySymbol, "get_symbol", patched_currency_symbol) diff --git a/tests/test_wot.py b/tests/test_wot.py index 08a1b265e73c9ff32454fec9f02cbce44b3ab5aa..3b4299ed1b3d1893b53afa9f07bc9c9b7d9cd75d 100644 --- a/tests/test_wot.py +++ b/tests/test_wot.py @@ -19,7 +19,7 @@ import pytest import click from silkaj import wot - +from silkaj import wot_tools pubkey_titi_tata = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88" pubkey_toto_tutu = "totoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88" @@ -148,7 +148,7 @@ def patched_prompt_tutu(message): async def test_choose_identity( selected_uid, pubkey, patched_prompt, patched_lookup, capsys, monkeypatch ): - monkeypatch.setattr(wot, "wot_lookup", patched_lookup) + monkeypatch.setattr(wot_tools, "wot_lookup", patched_lookup) monkeypatch.setattr(click, "prompt", patched_prompt) identity_card, get_pubkey, signed = await wot.choose_identity(pubkey) expected_pubkey = pubkey.split(":")[0]