async def membership_status(certifications, pubkey, req): params = await BlockchainParams().params if len(certifications["received"]) >= params["sigQty"]: print( "Membership expiration due to certification expirations: " + certifications["received_expire"][ len(certifications["received"]) - params["sigQty"] ] ) member = await is_member(pubkey) if member: member = True print("member:", member) if req["revoked"]: print( "revoked:", req["revoked"], "\nrevoked on:", convert_time(req["revoked_on"], "date") + "\n", ) if not member and req["wasMember"]: print("expired:", req["expired"], "\nwasMember:", req["wasMember"]) elif member: print( "Membership document expiration: " + convert_time(time() + req["membershipExpiresIn"], "date") ) print("Sentry:", req["isSentry"]) print("outdistanced:", req["outdistanced"])
async def argos_info(): head_block = await HeadBlock().head_block print(await CurrencySymbol().symbol, "|") print("---") ep = EndPoint().ep endpoint_address = ep[best_endpoint_address(ep, False)] if ep["port"] == "443": href = "href=https://%s/" % (endpoint_address) else: href = "href=http://%s:%s/" % (endpoint_address, ep["port"]) print( "Connected to node:", ep[best_endpoint_address(ep, False)], ep["port"], "|", href, "\nCurrent block number:", head_block["number"], "\nCurrency name:", await CurrencySymbol().symbol, "\nNumber of members:", head_block["membersCount"], "\nMinimal Proof-of-Work:", head_block["powMin"], "\nCurrent time:", convert_time(head_block["time"], "all"), "\nMedian time:", convert_time(head_block["medianTime"], "all"), "\nDifference time:", convert_time(head_block["time"] - head_block["medianTime"], "hour"), ) client = ClientInstance().client await client.close()
async def certification_confirmation(issuer, issuer_pubkey, pubkey_to_certify, idty_to_certify): cert = list() cert.append( ["Cert", "Issuer", "–>", "Recipient: Published: #block-hash date"]) client = ClientInstance().client idty_timestamp = idty_to_certify["meta"]["timestamp"] block_uid_idty = block_uid(idty_timestamp) block = await client(bma.blockchain.block, block_uid_idty.number) block_uid_date = (": #" + idty_timestamp[:15] + "… " + convert_time(block["time"], "all")) cert.append( ["ID", issuer["uid"], "–>", idty_to_certify["uid"] + block_uid_date]) cert.append([ "Pubkey", display_pubkey_and_checksum(issuer_pubkey), "–>", display_pubkey_and_checksum(pubkey_to_certify), ]) params = await BlockchainParams().params cert_begins = convert_time(time(), "date") cert_ends = convert_time(time() + params["sigValidity"], "date") cert.append(["Valid", cert_begins, "—>", cert_ends]) echo(tabulate(cert, tablefmt="fancy_grid")) if not confirm("Do you confirm sending this certification?"): await client.close() sys.exit(SUCCESS_EXIT_STATUS)
async def display_diffi(current, diffi): levels = [ OrderedDict((i, d[i]) for i in ("uid", "level")) for d in diffi["levels"] ] diffi["levels"] = levels issuers = 0 sorted_diffi = sorted(diffi["levels"], key=itemgetter("level"), reverse=True) for d in diffi["levels"]: if d["level"] / 2 < current["powMin"]: issuers += 1 d["match"] = match_pattern(d["level"])[0][:20] d["Π diffi"] = power(match_pattern(d["level"])[1]) d["Σ diffi"] = d.pop("level") system("cls||clear") print("Current block: n°{0}, generated on the {1}\n\ Generation of next block n°{2} possible by at least {3}/{4} members\n\ Common Proof-of-Work difficulty level: {5}, hash starting with `{6}`\n{7}". format( current["number"], convert_time(current["time"], "all"), diffi["block"], issuers, len(diffi["levels"]), current["powMin"], match_pattern(int(current["powMin"]))[0], tabulate(sorted_diffi, headers="keys", tablefmt="orgtbl", stralign="center"), ))
async def parse_received_tx(received_txs_table, received_txs, pubkey, ud_value, uids): """ Extract issuers’ pubkeys Get identities from pubkeys Convert time into human format Assign identities Get amounts and assign amounts and amounts_ud Append comment """ issuers = list() for received_tx in received_txs: for issuer in received_tx.issuers: issuers.append(issuer) identities = await identities_from_pubkeys(issuers, uids) for received_tx in received_txs: tx_list = list() tx_list.append(convert_time(received_tx.time, "all")) tx_list.append(str()) for i, issuer in enumerate(received_tx.issuers): tx_list[1] += prefix(None, None, i) + assign_idty_from_pubkey( issuer, identities ) amounts = tx_amount(received_tx, pubkey, received_func)[0] tx_list.append(amounts / 100) tx_list.append(amounts / ud_value) tx_list.append(received_tx.comment) received_txs_table.append(tx_list)
async def currency_info(): head_block = await HeadBlock().head_block ep = EndPoint().ep print( "Connected to node:", ep[best_endpoint_address(ep, False)], ep["port"], "\nCurrent block number:", head_block["number"], "\nCurrency name:", await CurrencySymbol().symbol, "\nNumber of members:", head_block["membersCount"], "\nMinimal Proof-of-Work:", head_block["powMin"], "\nCurrent time:", convert_time(head_block["time"], "all"), "\nMedian time:", convert_time(head_block["medianTime"], "all"), "\nDifference time:", convert_time(head_block["time"] - head_block["medianTime"], "hour"), ) client = ClientInstance().client await client.close()
async def generate_header(pubkey, currency_symbol, ud_value): try: idty = await identity_of(pubkey) except: idty = dict([("uid", "")]) balance = await get_amount_from_pubkey(pubkey) return "Transactions history from: {uid} {pubkey}\n\ Current balance: {balance} {currency}, {balance_ud} UD {currency} on the {date}\n\ ".format( uid=idty["uid"], pubkey=display_pubkey_and_checksum(pubkey), currency=currency_symbol, balance=balance[1] / 100, balance_ud=round(balance[1] / ud_value, 2), date=convert_time(time(), "all"), )
async def parse_sent_tx(sent_txs_table, sent_txs, pubkey, ud_value, uids): """ Extract recipients’ pubkeys from outputs Get identities from pubkeys Convert time into human format Store "Total" and total amounts according to the number of outputs If not output back return: Assign amounts, amounts_ud, identities, and comment """ pubkeys = list() for sent_tx in sent_txs: outputs = tx_amount(sent_tx, pubkey, sent_func)[1] for output in outputs: if output_available(output.condition, ne, pubkey): pubkeys.append(output.condition.left.pubkey) identities = await identities_from_pubkeys(pubkeys, uids) for sent_tx in sent_txs: tx_list = list() tx_list.append(convert_time(sent_tx.time, "all")) total_amount, outputs = tx_amount(sent_tx, pubkey, sent_func) if len(outputs) > 1: tx_list.append("Total") amounts = str(total_amount / 100) amounts_ud = str(round(total_amount / ud_value, 2)) else: tx_list.append(str()) amounts = str() amounts_ud = str() for i, output in enumerate(outputs): if output_available(output.condition, ne, pubkey): amounts += prefix(None, outputs, i) + str( neg(amount_in_current_base(output)) / 100 ) amounts_ud += prefix(None, outputs, i) + str( round(neg(amount_in_current_base(output)) / ud_value, 2) ) tx_list[1] += prefix(tx_list[1], outputs, 0) + assign_idty_from_pubkey( output.condition.left.pubkey, identities ) tx_list.append(amounts) tx_list.append(amounts_ud) tx_list.append(sent_tx.comment) sent_txs_table.append(tx_list)
def expiration_date_from_block_id(block_id, time_first_block, params): return convert_time( date_approximation(block_id, time_first_block, params["avgGenTime"]) + params["sigValidity"], "date", )
async def list_blocks(number, detailed): head_block = await HeadBlock().head_block current_nbr = head_block["number"] if number == 0: number = head_block["issuersFrame"] client = ClientInstance().client blocks = await client(bma.blockchain.blocks, number, current_nbr - number + 1) issuers = list() issuers_dict = dict() for block in blocks: issuer = OrderedDict() issuer["pubkey"] = block["issuer"] if detailed or number <= 30: issuer["block"] = block["number"] issuer["gentime"] = convert_time(block["time"], "all") issuer["mediantime"] = convert_time(block["medianTime"], "all") issuer["hash"] = block["hash"][:10] issuer["powMin"] = block["powMin"] issuers_dict[issuer["pubkey"]] = issuer issuers.append(issuer) for pubkey in issuers_dict.keys(): issuer = issuers_dict[pubkey] idty = await identity_of(issuer["pubkey"]) for issuer2 in issuers: if (issuer2.get("pubkey") is not None and issuer.get("pubkey") is not None and issuer2["pubkey"] == issuer["pubkey"]): issuer2["uid"] = idty["uid"] issuer2.pop("pubkey") await sleep(ASYNC_SLEEP) await client.close() print( "Last {0} blocks from n°{1} to n°{2}".format(number, current_nbr - number + 1, current_nbr), end=" ", ) if detailed or number <= 30: sorted_list = sorted(issuers, key=itemgetter("block"), reverse=True) print("\n" + tabulate( sorted_list, headers="keys", tablefmt="orgtbl", stralign="center")) else: list_issued = list() for issuer in issuers: found = False for issued in list_issued: if issued.get( "uid") is not None and issued["uid"] == issuer["uid"]: issued["blocks"] += 1 found = True break if not found: issued = OrderedDict() issued["uid"] = issuer["uid"] issued["blocks"] = 1 list_issued.append(issued) for issued in list_issued: issued["percent"] = issued["blocks"] / number * 100 sorted_list = sorted(list_issued, key=itemgetter("blocks"), reverse=True) print("from {0} issuers\n{1}".format( len(list_issued), tabulate( sorted_list, headers="keys", tablefmt="orgtbl", floatfmt=".1f", stralign="center", ), ))
async def send_certification(uid_pubkey_to_certify): client = ClientInstance().client checked_pubkey = is_pubkey_and_check(uid_pubkey_to_certify) if checked_pubkey: uid_pubkey_to_certify = checked_pubkey idty_to_certify, pubkey_to_certify, send_certs = await wot.choose_identity( uid_pubkey_to_certify) # Authentication key = auth_method() # Check whether current user is member issuer_pubkey = key.pubkey issuer = await wot.is_member(issuer_pubkey) if not issuer: message_exit("Current identity is not member.") if issuer_pubkey == pubkey_to_certify: message_exit("You can’t certify yourself!") # Check if the certification can be renewed req = await client(bma.wot.requirements, pubkey_to_certify) req = req["identities"][0] for cert in req["certifications"]: if cert["from"] == issuer_pubkey: params = await BlockchainParams().params # Ğ1: 0<–>2y - 2y + 2m # ĞT: 0<–>4.8m - 4.8m + 12.5d renewable = cert["expiresIn"] - params["sigValidity"] + params[ "sigReplay"] if renewable > 0: renewable_date = convert_time(time() + renewable, "date") message_exit("Certification renewable the " + renewable_date) # Check if the certification is already in the pending certifications for pending_cert in req["pendingCerts"]: if pending_cert["from"] == issuer_pubkey: message_exit("Certification is currently been processed") # Display license and ask for confirmation head = await HeadBlock().head_block currency = head["currency"] license_approval(currency) # Certification confirmation await certification_confirmation(issuer, issuer_pubkey, pubkey_to_certify, idty_to_certify) identity = Identity( version=10, currency=currency, pubkey=pubkey_to_certify, uid=idty_to_certify["uid"], ts=block_uid(idty_to_certify["meta"]["timestamp"]), signature=idty_to_certify["self"], ) certification = Certification( version=10, currency=currency, pubkey_from=issuer_pubkey, identity=identity, timestamp=BlockUID(head["number"], head["hash"]), signature="", ) # Sign document certification.sign([key]) # Send certification document response = await client(bma.wot.certify, certification.signed_raw()) if response.status == 200: print("Certification successfully sent.") else: print("Error while publishing certification: {0}".format( await response.text())) await client.close()