diff --git a/silkaj/blockchain/blocks.py b/silkaj/blockchain/blocks.py index 563fb33d317938f1fa98092d7534c145fbd20f6b..96ce7e0fea0a03302d2d8aecbc16579685cfd4fb 100644 --- a/silkaj/blockchain/blocks.py +++ b/silkaj/blockchain/blocks.py @@ -26,7 +26,7 @@ from silkaj import tui from silkaj.blockchain.tools import get_head_block from silkaj.constants import ALL from silkaj.network import client_instance -from silkaj.wot_tools import identity_of +from silkaj.wot.tools import identity_of @command("blocks", help="Display blocks: default: 0 for current window size") diff --git a/silkaj/idty_tools.py b/silkaj/idty_tools.py index 1d1f265b8b040d7194e6f97d6537da86a56d0ae6..63fb823892f136d68c1252046da95094b7df3499 100644 --- a/silkaj/idty_tools.py +++ b/silkaj/idty_tools.py @@ -24,10 +24,10 @@ from duniterpy.api import bma from duniterpy.documents import BlockID, Identity, Revocation from texttable import Texttable -from silkaj import wot_tools as wt from silkaj.constants import ALL from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum +from silkaj.wot.tools import wot_lookup def display_identity(idty: Identity) -> Texttable: @@ -60,8 +60,8 @@ def check_many_identities(document: Union[Identity, Revocation]) -> bool: idty = document if doc_type == "Identity" else document.identity try: - results_pubkey = wt.wot_lookup(idty.pubkey) - results_uid = wt.wot_lookup(idty.uid) + results_pubkey = wot_lookup(idty.pubkey) + results_uid = wot_lookup(idty.uid) except urllib.error.HTTPError: sys.exit( f"{error_no_identical_id}\nuid: {idty.uid}\npubkey: \ diff --git a/silkaj/money/balance.py b/silkaj/money/balance.py index 8fff6ecfd0d8a3397be3f6f64b7eff1ab76f4e0a..9483cb759235b189a26ca4c806ccac668a01042e 100644 --- a/silkaj/money/balance.py +++ b/silkaj/money/balance.py @@ -19,12 +19,12 @@ from typing import List from click import Context, argument, command, echo, pass_context from silkaj import tui -from silkaj import wot_tools as wt from silkaj.auth import auth_method, has_auth_method from silkaj.blockchain.tools import get_head_block from silkaj.money import tools as m_tools from silkaj.public_key import gen_pubkey_checksum, is_pubkey_and_check from silkaj.tools import get_currency_symbol +from silkaj.wot import tools as wt @command("balance", help="Get wallet balance") diff --git a/silkaj/money/history.py b/silkaj/money/history.py index 98a8f892c93a9b1d78135a9d8dc23e8ff30ca6ae..005b3873b21743c463ab0205967a000030381f5d 100644 --- a/silkaj/money/history.py +++ b/silkaj/money/history.py @@ -24,7 +24,6 @@ from duniterpy.documents.transaction import OutputSource, Transaction from duniterpy.grammars.output import Condition from pendulum import from_timestamp, now -from silkaj import wot_tools as wt from silkaj.constants import ALL, ALL_DIGITAL from silkaj.money.tools import ( amount_in_current_base, @@ -39,6 +38,7 @@ from silkaj.public_key import ( ) from silkaj.tools import get_currency_symbol from silkaj.tui import Table +from silkaj.wot import tools as wt @command("history", help="Display transaction history") diff --git a/silkaj/money/tools.py b/silkaj/money/tools.py index 2d5032e7faf3c62c063cb9a38a780d107253da35..ba83be3f227baa69fc4b84277e403bce60fa9b7c 100644 --- a/silkaj/money/tools.py +++ b/silkaj/money/tools.py @@ -19,9 +19,9 @@ from typing import List, Tuple, Union from duniterpy.api import bma from duniterpy.documents.transaction import InputSource, OutputSource -from silkaj import wot_tools from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum +from silkaj.wot import tools as wt def display_amount( @@ -45,7 +45,7 @@ def display_pubkey(tx: List, message: str, pubkey: str) -> None: Displays a pubkey and the eventually associated identity """ tx.append([f"{message} (pubkey:checksum)", gen_pubkey_checksum(pubkey)]) - idty = wot_tools.is_member(pubkey) + idty = wt.is_member(pubkey) if idty: tx.append([f"{message} (id)", idty["uid"]]) diff --git a/silkaj/wot/certification.py b/silkaj/wot/certification.py index 8b12b0510bef8fc2d33bfb5926b428e73e250596..4cfa31bfa8f40dcfd7ea200f2e7a2287d890f2df 100644 --- a/silkaj/wot/certification.py +++ b/silkaj/wot/certification.py @@ -24,7 +24,6 @@ from duniterpy.key import SigningKey from pendulum import from_timestamp, now from silkaj import tui -from silkaj import wot_tools as wt from silkaj.auth import auth_method from silkaj.blockchain import tools as bc_tools from silkaj.constants import ALL, DATE @@ -85,7 +84,7 @@ def send_certification(ctx: click.Context, uid_pubkey_to_certify: str) -> None: def pre_checks(client: Client, issuer_pubkey: str, pubkey_to_certify: str) -> Dict: # Check whether current user is member - issuer = wt.is_member(issuer_pubkey) + issuer = wot_tools.is_member(issuer_pubkey) if not issuer: sys.exit("Current identity is not member.") diff --git a/silkaj/wot/lookup.py b/silkaj/wot/lookup.py index 88c3b0b8a0e9a9b4855431f5dadcb245e0f68fc0..8ed02536d5174ffda4800278c4572e39f1679f7b 100644 --- a/silkaj/wot/lookup.py +++ b/silkaj/wot/lookup.py @@ -17,9 +17,9 @@ import urllib import click -from silkaj import wot_tools as wt from silkaj.network import exit_on_http_error from silkaj.public_key import gen_pubkey_checksum, is_pubkey_and_check +from silkaj.wot import tools as wt @click.command("lookup", help="User identifier and public key lookup") diff --git a/silkaj/wot/tools.py b/silkaj/wot/tools.py index 2830208b72f07432f172801139f3e88a7b3d53c6..566fe2649810e89e433fb05560296bdab1adef53 100644 --- a/silkaj/wot/tools.py +++ b/silkaj/wot/tools.py @@ -14,16 +14,66 @@ # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import urllib -from typing import Dict, List, Tuple +from typing import Dict, List, Optional, Tuple +from urllib.error import HTTPError import click +from duniterpy.api.bma import wot -from silkaj import wot_tools as wt -from silkaj.network import exit_on_http_error +from silkaj.network import client_instance, exit_on_http_error from silkaj.public_key import gen_pubkey_checksum from silkaj.tui import Table +def identity_of(pubkey_uid: str) -> Dict: + """ + 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 = client_instance() + return client(wot.identity_of, pubkey_uid) + + +def is_member(pubkey_uid: str) -> Optional[Dict]: + """ + Check identity is member + If member, return corresponding identity, else: False + """ + try: + return identity_of(pubkey_uid) + except HTTPError: + return None + + +def wot_lookup(identifier: str) -> List: + """ + :identifier: identity or pubkey in part or whole + Return received and sent certifications lists of matching identities + if one identity found + """ + client = client_instance() + return (client(wot.lookup, identifier))["results"] + + +def identities_from_pubkeys(pubkeys: List[str], uids: bool) -> List: + """ + Make list of pubkeys unique, and remove empty strings + Request identities + """ + if not uids: + return [] + + uniq_pubkeys = list(filter(None, set(pubkeys))) + identities = [] + for pubkey in uniq_pubkeys: + try: + identities.append(identity_of(pubkey)) + except HTTPError: + pass + return identities + + def choose_identity(pubkey_uid: str) -> Tuple[Dict, str, List]: """ Get lookup from a pubkey or an uid @@ -33,7 +83,7 @@ def choose_identity(pubkey_uid: str) -> Tuple[Dict, str, List]: """ try: - lookups = wt.wot_lookup(pubkey_uid) + lookups = wot_lookup(pubkey_uid) except urllib.error.HTTPError as e: exit_on_http_error(e, 404, f"No identity found for {pubkey_uid}") diff --git a/silkaj/wot/wot.py b/silkaj/wot/wot.py index 635bdc436c231ad6ac0002e00ca8f26e2ca2a423..887dca2c1632a2d5518654c39bed9d0a55ea7227 100644 --- a/silkaj/wot/wot.py +++ b/silkaj/wot/wot.py @@ -20,13 +20,12 @@ import click from duniterpy.api.bma import blockchain, wot from pendulum import from_timestamp, now -from silkaj import wot_tools as wt from silkaj.blockchain.tools import get_blockchain_parameters from silkaj.constants import DATE from silkaj.network import client_instance from silkaj.public_key import gen_pubkey_checksum, is_pubkey_and_check from silkaj.tui import Table -from silkaj.wot import tools as w_tools +from silkaj.wot import tools as wt def get_sent_certifications( @@ -65,7 +64,7 @@ def received_sent_certifications(uid_pubkey: str) -> None: if checked_pubkey: uid_pubkey = str(checked_pubkey) - identity, pubkey, signed = w_tools.choose_identity(uid_pubkey) + identity, pubkey, signed = wt.choose_identity(uid_pubkey) certifications = OrderedDict() # type: OrderedDict params = get_blockchain_parameters() diff --git a/silkaj/wot_tools.py b/silkaj/wot_tools.py deleted file mode 100644 index b233f9fffba94166b744ed19c2c04e8c8454696a..0000000000000000000000000000000000000000 --- a/silkaj/wot_tools.py +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright 2016-2022 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 typing import Dict, List, Optional -from urllib.error import HTTPError - -from duniterpy.api.bma import wot - -from silkaj.network import client_instance - - -def identity_of(pubkey_uid: str) -> Dict: - """ - 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 = client_instance() - return client(wot.identity_of, pubkey_uid) - - -def is_member(pubkey_uid: str) -> Optional[Dict]: - """ - Check identity is member - If member, return corresponding identity, else: False - """ - try: - return identity_of(pubkey_uid) - except HTTPError: - return None - - -def wot_lookup(identifier: str) -> List: - """ - :identifier: identity or pubkey in part or whole - Return received and sent certifications lists of matching identities - if one identity found - """ - client = client_instance() - return (client(wot.lookup, identifier))["results"] - - -def identities_from_pubkeys(pubkeys: List[str], uids: bool) -> List: - """ - Make list of pubkeys unique, and remove empty strings - Request identities - """ - if not uids: - return [] - - uniq_pubkeys = list(filter(None, set(pubkeys))) - identities = [] - for pubkey in uniq_pubkeys: - try: - identities.append(identity_of(pubkey)) - except HTTPError: - pass - return identities diff --git a/tests/money/test_history.py b/tests/money/test_history.py index cd18e22dbc7ed33b4dde9c4d789b58b2efac20d6..7b49aa312795259c6cf0baa55929b63157514f30 100644 --- a/tests/money/test_history.py +++ b/tests/money/test_history.py @@ -15,10 +15,10 @@ import pytest -from silkaj import wot_tools from silkaj.constants import PUBKEY_MAX_LENGTH, PUBKEY_MIN_LENGTH, SHORT_PUBKEY_SIZE from silkaj.money import history from silkaj.public_key import CHECKSUM_SIZE +from silkaj.wot import tools as wt from tests.patched.blockchain_tools import currency from tests.patched.tx_history import patched_get_transactions_history from tests.patched.wot import patched_identities_from_pubkeys @@ -41,9 +41,7 @@ def test_history_generate_txs_list_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_tools, "identities_from_pubkeys", patched_identities_from_pubkeys - ) + monkeypatch.setattr(wt, "identities_from_pubkeys", patched_identities_from_pubkeys) client = "whatever" ud_value = 10.07 diff --git a/tests/money/test_tools.py b/tests/money/test_tools.py index 756a953e5183e5e4687ff1b8b45cf1433f797202..d282d6ef3ab825b82f18c0364d9245079cd75191 100644 --- a/tests/money/test_tools.py +++ b/tests/money/test_tools.py @@ -17,11 +17,11 @@ import duniterpy.api.bma.tx as bma_tx import pytest from click.testing import CliRunner -from silkaj import wot_tools from silkaj.cli import cli from silkaj.constants import FAILURE_EXIT_STATUS, G1_SYMBOL from silkaj.money import tools as m_tools from silkaj.public_key import gen_pubkey_checksum +from silkaj.wot import tools from tests.patched.test_constants import mock_ud_value from tests.patched.wot import patched_is_member @@ -52,7 +52,7 @@ def test_display_amount(message, amount, currency_symbol): ], ) def test_display_pubkey(message, pubkey, uid, monkeypatch): - monkeypatch.setattr(wot_tools, "is_member", patched_is_member) + monkeypatch.setattr(tools, "is_member", patched_is_member) expected = [[f"{message} (pubkey:checksum)", gen_pubkey_checksum(pubkey)]] if uid: diff --git a/tests/money/test_transfer.py b/tests/money/test_transfer.py index 57d9316244ff49b6540c5f9868ac7f2ed1b9ec67..d5243b4fe54fd036578d63147ace0b271061dc11 100644 --- a/tests/money/test_transfer.py +++ b/tests/money/test_transfer.py @@ -26,12 +26,13 @@ from duniterpy.documents.transaction import ( Unlock, ) -from silkaj import auth, network, tools, wot_tools +from silkaj import auth, network, tools from silkaj.blockchain import tools as bc_tools from silkaj.cli import cli from silkaj.constants import CENT_MULT_TO_UNIT, G1_SYMBOL from silkaj.money import tools as m_tools from silkaj.money import transfer +from silkaj.wot import tools as wt from tests.patched.auth import patched_auth_method from tests.patched.blockchain_tools import fake_block_id, patched_get_head_block from tests.patched.money import Counter, patched_get_sources, patched_get_ud_value @@ -111,7 +112,7 @@ def test_gen_confirmation_table( monkeypatch, ): # patched functions - monkeypatch.setattr(wot_tools, "is_member", patched_is_member) + monkeypatch.setattr(wt, "is_member", patched_is_member) monkeypatch.setattr(m_tools, "get_ud_value", patched_get_ud_value) monkeypatch.setattr(tools, "get_currency_symbol", patched_get_currency_symbol) diff --git a/tests/wot/test_tools.py b/tests/wot/test_tools.py index 2e0aa4eae4695577320808ae0a4abf87f6a7decd..f10b37f972f3b438f1224c25cfdc54bc70866bc9 100644 --- a/tests/wot/test_tools.py +++ b/tests/wot/test_tools.py @@ -16,7 +16,6 @@ import click import pytest -from silkaj import wot_tools from silkaj.wot import tools as w_tools pubkey_titi_tata = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88" @@ -145,7 +144,7 @@ def patched_prompt_tutu(message): def test_choose_identity( selected_uid, pubkey, patched_prompt, patched_lookup, capsys, monkeypatch ): - monkeypatch.setattr(wot_tools, "wot_lookup", patched_lookup) + monkeypatch.setattr(w_tools, "wot_lookup", patched_lookup) monkeypatch.setattr(click, "prompt", patched_prompt) # pylint: disable=unused-variable idty_card, get_pubkey, signed = w_tools.choose_identity(pubkey)