diff --git a/silkaj/cert.py b/silkaj/cert.py index c0fb56c614e686275bcef8ed38539a0c6b5e33e5..a9dce4912c1ae7c74b0187e71a938f4eece9e77a 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -3,18 +3,18 @@ from tabulate import tabulate from silkaj.auth import auth_method from silkaj.tools import get_publickey_from_seed, message_exit, sign_document_from_seed -from silkaj.network_tools import post_request +from silkaj.network_tools import post_request, HeadBlock from silkaj.license import license_approval from silkaj.wot import is_member,\ get_uid_from_pubkey, get_informations_for_identity -def send_certification(ep, head_block, cli_args): +def send_certification(ep, cli_args): id_to_certify = get_informations_for_identity(ep, cli_args.subsubcmd) main_id_to_certify = id_to_certify["uids"][0] # Display license and ask for confirmation - license_approval(head_block["currency"]) + license_approval(HeadBlock(ep).head_block["currency"]) # Authentication seed = auth_method(cli_args) @@ -35,7 +35,7 @@ def send_certification(ep, head_block, cli_args): # Certification confirmation if not certification_confirmation(issuer_id, issuer_pubkey, id_to_certify, main_id_to_certify): return - cert_doc = generate_certification_document(head_block, issuer_pubkey, id_to_certify, main_id_to_certify) + cert_doc = generate_certification_document(ep, issuer_pubkey, id_to_certify, main_id_to_certify) cert_doc += sign_document_from_seed(cert_doc, seed) + "\n" # Send certification document @@ -53,7 +53,8 @@ def certification_confirmation(issuer_id, issuer_pubkey, id_to_certify, main_id_ return True -def generate_certification_document(head_block, issuer_pubkey, id_to_certify, main_id_to_certify): +def generate_certification_document(ep, issuer_pubkey, id_to_certify, main_id_to_certify): + head_block = HeadBlock(ep).head_block return "Version: 10\n\ Type: Certification\n\ Currency: " + head_block["currency"] + "\n\ diff --git a/silkaj/commands.py b/silkaj/commands.py index 2daa81ac7b79eaff53b6aaf8e11eed254d50a492..67e6a82d32dc8ed2f667e4168052b2e4e669bbab 100644 --- a/silkaj/commands.py +++ b/silkaj/commands.py @@ -6,17 +6,16 @@ from tabulate import tabulate from operator import itemgetter from silkaj.wot import get_uid_from_pubkey -from silkaj.network_tools import discover_peers, get_request, best_node, get_current_block -from silkaj.tools import convert_time, get_currency_symbol, message_exit +from silkaj.network_tools import discover_peers, get_request, best_node, get_current_block, HeadBlock +from silkaj.tools import convert_time, message_exit, CurrencySymbol from silkaj.constants import NO_MATCHING_ID -def currency_info(ep, head_block): - info_type = ["newcomers", "certs", "actives", "leavers", "excluded", "ud", "tx"] - i, info_data = 0, dict() - while (i < len(info_type)): - info_data[info_type[i]] = get_request(ep, "blockchain/with/" + info_type[i])["result"]["blocks"] - i += 1 +def currency_info(ep): + info_data = dict() + for info_type in ["newcomers", "certs", "actives", "leavers", "excluded", "ud", "tx"]: + info_data[info_type] = get_request(ep, "blockchain/with/" + info_type)["result"]["blocks"] + head_block = HeadBlock(ep).head_block system("clear") print("Connected to node:", ep[best_node(ep, False)], ep["port"], "\nCurrent block number:", head_block["number"], @@ -158,7 +157,8 @@ def network_info(ep, discover): print(tabulate(endpoints, headers="keys", tablefmt="orgtbl", stralign="center")) -def list_issuers(ep, head_block, nbr, last): +def list_issuers(ep, nbr, last): + head_block = HeadBlock(ep).head_block current_nbr = head_block["number"] if nbr == 0: nbr = head_block["issuersFrame"] @@ -215,13 +215,14 @@ def list_issuers(ep, head_block, nbr, last): tabulate(sorted_list, headers="keys", tablefmt="orgtbl", floatfmt=".1f", stralign="center"))) -def argos_info(ep, head_block): +def argos_info(ep): info_type = ["newcomers", "certs", "actives", "leavers", "excluded", "ud", "tx"] pretty_names = {'g1': 'Äž1', 'gtest': 'Äžtest'} i, info_data = 0, dict() while (i < len(info_type)): info_data[info_type[i]] = get_request(ep, "blockchain/with/" + info_type[i])["result"]["blocks"] i += 1 + head_block = HeadBlock(ep).head_block pretty = head_block["currency"] if head_block["currency"] in pretty_names: pretty = pretty_names[head_block["currency"]] diff --git a/silkaj/money.py b/silkaj/money.py index 21c67e82bf0194918225812c42a24529e7913f6a..4decd91c329c0479fe8ff2e9258af43a148ff7b3 100644 --- a/silkaj/money.py +++ b/silkaj/money.py @@ -1,10 +1,10 @@ -from silkaj.network_tools import get_request -from silkaj.tools import get_currency_symbol, get_publickey_from_seed +from silkaj.network_tools import get_request, HeadBlock +from silkaj.tools import get_publickey_from_seed, CurrencySymbol from silkaj.auth import auth_method from silkaj.wot import check_public_key -def cmd_amount(ep, cli_args, config, head_block, ud_value, currency_symbol): +def cmd_amount(ep, cli_args): if not cli_args.subsubcmd.startswith("--"): pubkeys = cli_args.subsubcmd.split(":") for pubkey in pubkeys: @@ -13,8 +13,8 @@ def cmd_amount(ep, cli_args, config, head_block, ud_value, currency_symbol): return total = [0, 0] for pubkey in pubkeys: - value = get_amount_from_pubkey(ep, head_block, pubkey) - show_amount_from_pubkey(pubkey, value, ud_value, currency_symbol) + value = get_amount_from_pubkey(ep, pubkey) + show_amount_from_pubkey(ep, pubkey, value) total[0] += value[0] total[1] += value[1] if (len(pubkeys) > 1): @@ -22,7 +22,7 @@ def cmd_amount(ep, cli_args, config, head_block, ud_value, currency_symbol): else: seed = auth_method(cli_args) pubkey = get_publickey_from_seed(seed) - show_amount_from_pubkey(pubkey, get_amount_from_pubkey(ep, head_block, pubkey), ud_value, currency_symbol) + show_amount_from_pubkey(ep, pubkey, get_amount_from_pubkey(ep, pubkey)) def show_amount_from_pubkey(ep, pubkey, value): @@ -50,7 +50,7 @@ def show_amount_from_pubkey(ep, pubkey, value): print("Total Quantitative =", round(totalAmountInput / 100, 2), currency_symbol + "\n") -def get_amount_from_pubkey(ep, head_block, pubkey): +def get_amount_from_pubkey(ep, pubkey): sources = get_request(ep, "tx/sources/" + pubkey)["sources"] listinput = [] @@ -69,7 +69,7 @@ def get_amount_from_pubkey(ep, head_block, pubkey): pendings = history["sending"] + history["receiving"] + history["pending"] # print(pendings) - last_block_number = int(head_block["number"]) + last_block_number = int(HeadBlock(ep).head_block["number"]) # add pending output for pending in pendings: diff --git a/silkaj/network_tools.py b/silkaj/network_tools.py index 490660ab1d2fcd61104e9c7f9f726286a8af5d84..1f25803d52bb7d013649a22fad3ea4ea2681e869 100644 --- a/silkaj/network_tools.py +++ b/silkaj/network_tools.py @@ -160,3 +160,15 @@ def check_port(port): def get_current_block(ep): return get_request(ep, "blockchain/current") + + +class HeadBlock(object): + __instance = None + + def __new__(cls, ep): + if HeadBlock.__instance is None: + HeadBlock.__instance = object.__new__(cls) + return HeadBlock.__instance + + def __init__(self, ep): + self.head_block = get_request(ep, "blockchain/current") diff --git a/silkaj/silkaj.py b/silkaj/silkaj.py index 0779e4de1f5f72ee8c96adb01152c70926eab47f..271148291b25f3c7ba72406c9dff2d9a9d308b10 100644 --- a/silkaj/silkaj.py +++ b/silkaj/silkaj.py @@ -8,7 +8,7 @@ from silkaj.cert import send_certification from silkaj.commands import currency_info, difficulties, set_network_sort_keys,\ network_info, argos_info, list_issuers from silkaj.tools import message_exit -from silkaj.network_tools import get_request, get_current_block +from silkaj.network_tools import get_request from silkaj.wot import received_sent_certifications, id_pubkey_correspondence from silkaj.auth import generate_auth_file from silkaj.license import display_license @@ -102,7 +102,7 @@ def manage_cmd(ep, cli_args): if cli_args.subcmd == "about": about() elif cli_args.subcmd == "info": - currency_info(ep, head_block) + currency_info(ep) elif cli_args.subcmd == "diffi": difficulties(ep) @@ -115,19 +115,19 @@ def manage_cmd(ep, cli_args): network_info(ep, cli_args.contains_switches("discover")) elif cli_args.subcmd == "issuers" and cli_args.subsubcmd and int(cli_args.subsubcmd) >= 0: - list_issuers(ep, head_block, int(cli_args.subsubcmd), cli_args.contains_switches('last')) + list_issuers(ep, int(cli_args.subsubcmd), cli_args.contains_switches('last')) elif cli_args.subcmd == "argos": - argos_info(ep, head_block) + argos_info(ep) elif cli_args.subcmd == "amount": - cmd_amount(ep, cli_args, currency_config, head_block, ud_value, currency_symbol) + cmd_amount(ep, cli_args) elif cli_args.subcmd == "tx" or cli_args.subcmd == "transaction": - send_transaction(ep, cli_args, head_block, ud_value, currency_symbol) + send_transaction(ep, cli_args) elif cli_args.subcmd == "cert": - send_certification(ep, head_block, cli_args) + send_certification(ep, cli_args) elif cli_args.subcmd == "generate_auth_file": generate_auth_file(cli_args) diff --git a/silkaj/tx.py b/silkaj/tx.py index 470d4ed3930a5b440fa5f6efec12cfe2d49c9a70..d4ccce632a2e20eb2f1e5c6b09ccf22f2cf1f6e5 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -4,7 +4,7 @@ from time import sleep import urllib from tabulate import tabulate -from silkaj.network_tools import get_request, post_request +from silkaj.network_tools import get_request, post_request, HeadBlock from silkaj.tools import get_publickey_from_seed, sign_document_from_seed,\ check_public_key, message_exit from silkaj.auth import auth_method @@ -13,7 +13,7 @@ from silkaj.money import get_last_ud_value, get_amount_from_pubkey from silkaj.constants import NO_MATCHING_ID -def send_transaction(ep, cli_args, head_block, ud_value, currency_symbol): +def send_transaction(ep, cli_args): """ Main function """ @@ -22,14 +22,14 @@ def send_transaction(ep, cli_args, head_block, ud_value, currency_symbol): seed = auth_method(cli_args) issuer_pubkey = get_publickey_from_seed(seed) - pubkey_amount = get_amount_from_pubkey(ep, head_block, issuer_pubkey)[0] + pubkey_amount = get_amount_from_pubkey(ep, issuer_pubkey)[0] outputAddresses = output.split(':') check_transaction_values(comment, outputAddresses, outputBackChange, pubkey_amount < amount * len(outputAddresses), issuer_pubkey) if cli_args.contains_switches('yes') or cli_args.contains_switches('y') or \ input(tabulate(transaction_confirmation(ep, issuer_pubkey, amount, ud, outputAddresses, comment), tablefmt="fancy_grid") + "\nDo you confirm sending this transaction? [yes/no]: ") == "yes": - generate_and_send_transaction(ep, head_block, seed, issuer_pubkey, amount, outputAddresses, comment, allSources, outputBackChange) + generate_and_send_transaction(ep, seed, issuer_pubkey, amount, outputAddresses, comment, allSources, outputBackChange) def cmd_transaction(cli_args, ud): @@ -92,10 +92,10 @@ def transaction_confirmation(ep, issuer_pubkey, amount, ud, outputAddresses, com return tx -def generate_and_send_transaction(ep, head_block, seed, issuers, AmountTransfered, outputAddresses, Comment="", all_input=False, OutputbackChange=None): +def generate_and_send_transaction(ep, seed, issuers, AmountTransfered, outputAddresses, Comment="", all_input=False, OutputbackChange=None): while True: - listinput_and_amount = get_list_input_for_transaction(ep, head_block, issuers, AmountTransfered * len(outputAddresses), all_input) + listinput_and_amount = get_list_input_for_transaction(ep, issuers, AmountTransfered * len(outputAddresses), all_input) intermediatetransaction = listinput_and_amount[2] if intermediatetransaction: @@ -104,7 +104,7 @@ def generate_and_send_transaction(ep, head_block, seed, issuers, AmountTransfere print(" - From: " + issuers) print(" - To: " + issuers) print(" - Amount: " + str(totalAmountInput / 100)) - transaction = generate_transaction_document(ep, head_block, issuers, totalAmountInput, listinput_and_amount, outputAddresses, "Change operation") + transaction = generate_transaction_document(ep, issuers, totalAmountInput, listinput_and_amount, outputAddresses, "Change operation") transaction += sign_document_from_seed(transaction, seed) + "\n" post_request(ep, "tx/process", "transaction=" + urllib.parse.quote_plus(transaction)) print("Change Transaction successfully sent.") @@ -119,7 +119,7 @@ def generate_and_send_transaction(ep, head_block, seed, issuers, AmountTransfere print(" - Amount: " + str(listinput_and_amount[1] / 100)) else: print(" - Amount: " + str(AmountTransfered / 100 * len(outputAddresses))) - transaction = generate_transaction_document(ep, head_block, issuers, AmountTransfered, listinput_and_amount, outputAddresses, Comment, OutputbackChange) + transaction = generate_transaction_document(ep, issuers, AmountTransfered, listinput_and_amount, outputAddresses, Comment, OutputbackChange) transaction += sign_document_from_seed(transaction, seed) + "\n" post_request(ep, "tx/process", "transaction=" + urllib.parse.quote_plus(transaction)) @@ -127,13 +127,14 @@ def generate_and_send_transaction(ep, head_block, seed, issuers, AmountTransfere break -def generate_transaction_document(ep, head_block, issuers, AmountTransfered, listinput_and_amount, outputAddresses, Comment="", OutputbackChange=None): +def generate_transaction_document(ep, issuers, AmountTransfered, listinput_and_amount, outputAddresses, Comment="", OutputbackChange=None): totalAmountTransfered = AmountTransfered * len(outputAddresses) listinput = listinput_and_amount[0] totalAmountInput = listinput_and_amount[1] + head_block = HeadBlock(ep).head_block currency_name = head_block["currency"] blockstamp_current = str(head_block["number"]) + "-" + str(head_block["hash"]) curentUnitBase = head_block["unitbase"] @@ -195,7 +196,7 @@ def generate_transaction_document(ep, head_block, issuers, AmountTransfered, lis return transaction_document -def get_list_input_for_transaction(ep, head_block, pubkey, TXamount, allinput=False): +def get_list_input_for_transaction(ep, pubkey, TXamount, allinput=False): # real source in blockchain sources = get_request(ep, "tx/sources/" + pubkey)["sources"] if sources is None: @@ -210,7 +211,7 @@ def get_list_input_for_transaction(ep, head_block, pubkey, TXamount, allinput=Fa history = get_request(ep, "tx/history/" + pubkey + "/pending")["history"] pendings = history["sending"] + history["receiving"] + history["pending"] - last_block_number = int(head_block["number"]) + last_block_number = int(HeadBlock(ep).head_block["number"]) # add pending output for pending in pendings: