Skip to content
Snippets Groups Projects
Commit 90acd157 authored by matograine's avatar matograine
Browse files

[mod] #344 remove loop dependency

  * create silkaj.wot_tools module
  * update imports
  * modify related tests
parent 0057ce00
No related branches found
No related tags found
No related merge requests found
Pipeline #11950 passed
...@@ -23,8 +23,7 @@ from pathlib import Path ...@@ -23,8 +23,7 @@ from pathlib import Path
from duniterpy.key import SigningKey from duniterpy.key import SigningKey
from duniterpy.key.scrypt_params import ScryptParams from duniterpy.key.scrypt_params import ScryptParams
# had to import display_pubkey_and_checksum from wot to avoid loop dependency. from silkaj.tui import display_pubkey_and_checksum
from silkaj.wot import display_pubkey_and_checksum
from silkaj.tools import message_exit from silkaj.tools import message_exit
from silkaj.constants import PUBKEY_PATTERN from silkaj.constants import PUBKEY_PATTERN
......
...@@ -31,6 +31,7 @@ from silkaj.license import license_approval ...@@ -31,6 +31,7 @@ from silkaj.license import license_approval
from silkaj import wot, tui from silkaj import wot, tui
from silkaj.constants import SUCCESS_EXIT_STATUS, DATE, ALL from silkaj.constants import SUCCESS_EXIT_STATUS, DATE, ALL
from silkaj.crypto_tools import is_pubkey_and_check from silkaj.crypto_tools import is_pubkey_and_check
from silkaj import wot_tools as wt
@click.command("cert", help="Send certification") @click.command("cert", help="Send certification")
...@@ -92,7 +93,7 @@ async def send_certification(ctx, uid_pubkey_to_certify): ...@@ -92,7 +93,7 @@ async def send_certification(ctx, uid_pubkey_to_certify):
async def pre_checks(client, issuer_pubkey, pubkey_to_certify): async def pre_checks(client, issuer_pubkey, pubkey_to_certify):
# Check whether current user is member # Check whether current user is member
issuer = await wot.is_member(issuer_pubkey) issuer = await wt.is_member(issuer_pubkey)
if not issuer: if not issuer:
message_exit("Current identity is not member.") message_exit("Current identity is not member.")
......
...@@ -29,7 +29,7 @@ from _socket import gaierror ...@@ -29,7 +29,7 @@ from _socket import gaierror
from duniterpy.api import bma from duniterpy.api import bma
from silkaj.tools import coroutine from silkaj.tools import coroutine
from silkaj.wot import identity_of from silkaj.wot_tools import identity_of
from silkaj.network_tools import ( from silkaj.network_tools import (
best_endpoint_address, best_endpoint_address,
EndPoint, EndPoint,
......
...@@ -18,13 +18,11 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. ...@@ -18,13 +18,11 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from click import command, argument, pass_context, echo from click import command, argument, pass_context, echo
from tabulate import tabulate from tabulate import tabulate
from silkaj import wot_tools as wt
from silkaj.network_tools import ClientInstance from silkaj.network_tools import ClientInstance
from silkaj.blockchain_tools import HeadBlock from silkaj.blockchain_tools import HeadBlock
from silkaj.tools import CurrencySymbol, message_exit, coroutine from silkaj.tools import CurrencySymbol, message_exit, coroutine
from silkaj.auth import auth_method, has_auth_method 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 ( from silkaj.crypto_tools import (
is_pubkey_and_check, is_pubkey_and_check,
check_pubkey_format, check_pubkey_format,
...@@ -91,7 +89,7 @@ async def show_amount_from_pubkey(label, inputs_balance): ...@@ -91,7 +89,7 @@ async def show_amount_from_pubkey(label, inputs_balance):
# if `pubkey` is a pubkey, get pubkey:checksum and uid # if `pubkey` is a pubkey, get pubkey:checksum and uid
if label != "Total": if label != "Total":
member = await wot.is_member(label) member = await wt.is_member(label)
label = display_pubkey_and_checksum(label) label = display_pubkey_and_checksum(label)
# display balance table # display balance table
display = list() display = list()
......
...@@ -19,7 +19,7 @@ import sys ...@@ -19,7 +19,7 @@ import sys
import click import click
from datetime import datetime 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 from silkaj import crypto_tools as ct
...@@ -45,7 +45,7 @@ async def display_pubkey(tx, message, pubkey): ...@@ -45,7 +45,7 @@ async def display_pubkey(tx, message, pubkey):
Displays a pubkey and the eventually associated id. Displays a pubkey and the eventually associated id.
""" """
tx.append([message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]) 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: if id:
tx.append([message + " (id)", id["uid"]]) tx.append([message + " (id)", id["uid"]])
...@@ -67,19 +67,3 @@ async def send_doc_confirmation(document_name): ...@@ -67,19 +67,3 @@ async def send_doc_confirmation(document_name):
client = network_tools.ClientInstance().client client = network_tools.ClientInstance().client
await client.close() await client.close()
sys.exit(constants.SUCCESS_EXIT_STATUS) 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)
...@@ -24,6 +24,7 @@ from time import time ...@@ -24,6 +24,7 @@ from time import time
from duniterpy.api.bma.tx import history from duniterpy.api.bma.tx import history
from duniterpy.documents.transaction import Transaction from duniterpy.documents.transaction import Transaction
from silkaj import wot_tools as wt
from silkaj.network_tools import ClientInstance from silkaj.network_tools import ClientInstance
from silkaj.tools import coroutine from silkaj.tools import coroutine
from silkaj.tui import display_pubkey_and_checksum from silkaj.tui import display_pubkey_and_checksum
...@@ -62,7 +63,7 @@ async def transaction_history(pubkey, uids, full_pubkey): ...@@ -62,7 +63,7 @@ async def transaction_history(pubkey, uids, full_pubkey):
async def generate_header(pubkey, currency_symbol, ud_value): async def generate_header(pubkey, currency_symbol, ud_value):
try: try:
idty = await wot.identity_of(pubkey) idty = await wt.identity_of(pubkey)
except: except:
idty = dict([("uid", "")]) idty = dict([("uid", "")])
balance = await get_amount_from_pubkey(pubkey) balance = await get_amount_from_pubkey(pubkey)
...@@ -149,7 +150,7 @@ async def parse_received_tx( ...@@ -149,7 +150,7 @@ async def parse_received_tx(
for received_tx in received_txs: for received_tx in received_txs:
for issuer in received_tx.issuers: for issuer in received_tx.issuers:
issuers.append(issuer) 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: for received_tx in received_txs:
tx_list = list() tx_list = list()
tx_list.append(pendulum.from_timestamp(received_tx.time).format(ALL)) 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 ...@@ -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): if output_available(output.condition, ne, pubkey):
pubkeys.append(output.condition.left.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: for sent_tx in sent_txs:
tx_list = list() tx_list = list()
tx_list.append(pendulum.from_timestamp(sent_tx.time).format(ALL)) tx_list.append(pendulum.from_timestamp(sent_tx.time).format(ALL))
......
...@@ -20,16 +20,16 @@ import pendulum ...@@ -20,16 +20,16 @@ import pendulum
from time import time from time import time
from tabulate import tabulate from tabulate import tabulate
from collections import OrderedDict from collections import OrderedDict
from asyncio import sleep
from duniterpy.api.bma import wot, blockchain from duniterpy.api.bma import wot, blockchain
from duniterpy.api.errors import DuniterError from duniterpy.api.errors import DuniterError
from silkaj import wot_tools as wt
from silkaj.network_tools import ClientInstance from silkaj.network_tools import ClientInstance
from silkaj.crypto_tools import is_pubkey_and_check 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.tui import display_pubkey_and_checksum
from silkaj.blockchain_tools import BlockchainParams 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): def get_sent_certifications(signed, time_first_block, params):
...@@ -124,7 +124,7 @@ async def membership_status(certifications, pubkey, req): ...@@ -124,7 +124,7 @@ async def membership_status(certifications, pubkey, req):
len(certifications["received"]) - params["sigQty"] len(certifications["received"]) - params["sigQty"]
] ]
) )
member = await is_member(pubkey) member = await wt.is_member(pubkey)
if member: if member:
member = True member = True
print("member:", member) print("member:", member)
...@@ -168,7 +168,7 @@ async def id_pubkey_correspondence(uid_pubkey): ...@@ -168,7 +168,7 @@ async def id_pubkey_correspondence(uid_pubkey):
uid_pubkey = checked_pubkey uid_pubkey = checked_pubkey
client = ClientInstance().client client = ClientInstance().client
lookups = await wot_lookup(uid_pubkey) lookups = await wt.wot_lookup(uid_pubkey)
await client.close() await client.close()
content = f"Public keys or user id found matching '{uid_pubkey}':\n" content = f"Public keys or user id found matching '{uid_pubkey}':\n"
...@@ -186,7 +186,7 @@ async def choose_identity(pubkey_uid): ...@@ -186,7 +186,7 @@ async def choose_identity(pubkey_uid):
If there is one uid, returns it If there is one uid, returns it
If there is multiple uids, prompt a selector 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 # Generate table containing the choices
identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []} identities_choices = {"id": [], "uid": [], "pubkey": [], "timestamp": []}
...@@ -223,62 +223,3 @@ async def choose_identity(pubkey_uid): ...@@ -223,62 +223,3 @@ async def choose_identity(pubkey_uid):
lookups[pubkey_index]["pubkey"], lookups[pubkey_index]["pubkey"],
lookups[pubkey_index]["signed"], 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
"""
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
...@@ -18,6 +18,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. ...@@ -18,6 +18,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
import pytest import pytest
from silkaj.tui import display_pubkey, display_amount, display_pubkey_and_checksum from silkaj.tui import display_pubkey, display_amount, display_pubkey_and_checksum
from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE from silkaj.constants import G1_SYMBOL, SHORT_PUBKEY_SIZE
from silkaj import wot_tools
from patched.wot import patched_is_member from patched.wot import patched_is_member
from patched.test_constants import mock_ud_value from patched.test_constants import mock_ud_value
...@@ -55,7 +56,7 @@ def test_display_amount(message, amount, currency_symbol): ...@@ -55,7 +56,7 @@ def test_display_amount(message, amount, currency_symbol):
) )
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_display_pubkey(message, pubkey, id, monkeypatch): 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)]] expected = [[message + " (pubkey:checksum)", display_pubkey_and_checksum(pubkey)]]
if id: if id:
......
...@@ -16,7 +16,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. ...@@ -16,7 +16,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
""" """
import pytest import pytest
from silkaj import tx_history, wot from silkaj import tx_history, wot, wot_tools
from silkaj.constants import ( from silkaj.constants import (
G1_DEFAULT_ENDPOINT, G1_DEFAULT_ENDPOINT,
SHORT_PUBKEY_SIZE, SHORT_PUBKEY_SIZE,
...@@ -48,7 +48,9 @@ async def test_tx_history_generate_table_and_pubkey_uid_display(monkeypatch): ...@@ -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> # uid is at least one char : "<uid> - <pubkey>" adds min 4 chars to <pubkey>
return pubkey + 4 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" client = "whatever"
ud_value = 10.07 ud_value = 10.07
......
...@@ -18,7 +18,16 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. ...@@ -18,7 +18,16 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
import sys import sys
import pytest 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.tui import display_pubkey, display_amount
from silkaj.constants import ( from silkaj.constants import (
G1_SYMBOL, G1_SYMBOL,
...@@ -128,7 +137,7 @@ async def test_gen_confirmation_table( ...@@ -128,7 +137,7 @@ async def test_gen_confirmation_table(
monkeypatch, monkeypatch,
): ):
# patched functions # 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(money.UDValue, "get_ud_value", patched_ud_value)
monkeypatch.setattr(tools.CurrencySymbol, "get_symbol", patched_currency_symbol) monkeypatch.setattr(tools.CurrencySymbol, "get_symbol", patched_currency_symbol)
......
...@@ -19,7 +19,7 @@ import pytest ...@@ -19,7 +19,7 @@ import pytest
import click import click
from silkaj import wot from silkaj import wot
from silkaj import wot_tools
pubkey_titi_tata = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88" pubkey_titi_tata = "B4RoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88"
pubkey_toto_tutu = "totoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88" pubkey_toto_tutu = "totoF48cTxzmsQDB3UjodKdZ2cVymKSKzgiPVRoMeA88"
...@@ -148,7 +148,7 @@ def patched_prompt_tutu(message): ...@@ -148,7 +148,7 @@ def patched_prompt_tutu(message):
async def test_choose_identity( async def test_choose_identity(
selected_uid, pubkey, patched_prompt, patched_lookup, capsys, monkeypatch 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) monkeypatch.setattr(click, "prompt", patched_prompt)
identity_card, get_pubkey, signed = await wot.choose_identity(pubkey) identity_card, get_pubkey, signed = await wot.choose_identity(pubkey)
expected_pubkey = pubkey.split(":")[0] expected_pubkey = pubkey.split(":")[0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment