diff --git a/silkaj/cert.py b/silkaj/cert.py index bcd6a34fd3484314342a065f46f9487207785f14..12e37b2793092060efabf2c78a2307e74293053d 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -5,19 +5,14 @@ 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 get_current_block, post_request from silkaj.license import license_approval -from silkaj.constants import NO_MATCHING_ID -from silkaj.wot import is_member, get_pubkey_from_id, get_pubkeys_from_id,\ - get_uid_from_pubkey +from silkaj.wot import is_member,\ + get_uid_from_pubkey, get_informations_for_identity def send_certification(ep, cli_args): current_blk = get_current_block(ep) - certified_uid = cli_args.subsubcmd - certified_pubkey = get_pubkey_from_id(ep, certified_uid) - - # Check that the id is present on the network - if (certified_pubkey is NO_MATCHING_ID): - message_exit(NO_MATCHING_ID) + 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(current_blk["currency"]) @@ -31,20 +26,17 @@ def send_certification(ep, cli_args): if not is_member(ep, issuer_pubkey, issuer_id): message_exit("Current identity is not member.") - # Check whether issuer and certified identities are different - if issuer_pubkey == certified_pubkey: + if issuer_pubkey == id_to_certify["pubkey"]: message_exit("You can’t certify yourself!") - # Check if this certification is already present on the network - id_lookup = get_pubkeys_from_id(ep, certified_uid)[0] - for certifiers in id_lookup["uids"][0]["others"]: - if certifiers["pubkey"] == issuer_pubkey: + for certifier in main_id_to_certify["others"]: + if certifier["pubkey"] == issuer_pubkey: message_exit("Identity already certified by " + issuer_id) # Certification confirmation - if not certification_confirmation(issuer_id, issuer_pubkey, certified_uid, certified_pubkey): + if not certification_confirmation(issuer_id, issuer_pubkey, id_to_certify, main_id_to_certify): return - cert_doc = generate_certification_document(id_lookup, current_blk, issuer_pubkey, certified_uid) + cert_doc = generate_certification_document(current_blk, issuer_pubkey, id_to_certify, main_id_to_certify) cert_doc += sign_document_from_seed(cert_doc, seed) + "\n" # Send certification document @@ -52,23 +44,23 @@ def send_certification(ep, cli_args): print("Certification successfully sent.") -def certification_confirmation(issuer_id, issuer_pubkey, certified_uid, certified_pubkey): +def certification_confirmation(issuer_id, issuer_pubkey, id_to_certify, main_id_to_certify): cert = list() cert.append(["Cert", "From", "–>", "To"]) - cert.append(["ID", issuer_id, "–>", certified_uid]) - cert.append(["Pubkey", issuer_pubkey, "–>", certified_pubkey]) + cert.append(["ID", issuer_id, "–>", main_id_to_certify["uid"]]) + cert.append(["Pubkey", issuer_pubkey, "–>", id_to_certify["pubkey"]]) if input(tabulate(cert, tablefmt="fancy_grid") + "\nDo you confirm sending this certification? [yes/no]: ") == "yes": return True -def generate_certification_document(id_lookup, current_blk, issuer_pubkey, certified_uid): +def generate_certification_document(current_blk, issuer_pubkey, id_to_certify, main_id_to_certify): return "Version: 10\n\ Type: Certification\n\ Currency: " + current_blk["currency"] + "\n\ Issuer: " + issuer_pubkey + "\n\ -IdtyIssuer: " + id_lookup["pubkey"] + "\n\ -IdtyUniqueID: " + certified_uid + "\n\ -IdtyTimestamp: " + id_lookup["uids"][0]["meta"]["timestamp"] + "\n\ -IdtySignature: " + id_lookup["uids"][0]["self"] + "\n\ +IdtyIssuer: " + id_to_certify["pubkey"] + "\n\ +IdtyUniqueID: " + main_id_to_certify["uid"] + "\n\ +IdtyTimestamp: " + main_id_to_certify["meta"]["timestamp"] + "\n\ +IdtySignature: " + main_id_to_certify["self"] + "\n\ CertTimestamp: " + str(current_blk["number"]) + "-" + current_blk["hash"] + "\n" diff --git a/silkaj/wot.py b/silkaj/wot.py index 40712cf79bd6f1aa991c1edc47113013875eb0c7..3dceb56db26f925dda83b6a890c6778534f1d26e 100644 --- a/silkaj/wot.py +++ b/silkaj/wot.py @@ -20,21 +20,13 @@ def get_sent_certifications(certs, time_first_block, params): def received_sent_certifications(ep, id): """ - check id exist - many identities could exist - retrieve the one searched + get searched id get id of received and sent certifications display on a chart the result with the numbers """ params = get_request(ep, "blockchain/parameters") time_first_block = get_request(ep, "blockchain/block/1")["time"] - if get_pubkeys_from_id(ep, id) == NO_MATCHING_ID: - message_exit(NO_MATCHING_ID) - certs_req = get_request(ep, "wot/lookup/" + id)["results"] - for certs_id in certs_req: - if certs_id['uids'][0]['uid'].lower() == id.lower(): - id_certs = certs_id - break + id_certs = get_informations_for_identity(ep, id) certifications = OrderedDict() system("clear") for certs in id_certs["uids"]: @@ -87,7 +79,7 @@ def id_pubkey_correspondence(ep, id_pubkey): if check_public_key(id_pubkey, False): print("{} public key corresponds to identity: {}".format(id_pubkey, get_uid_from_pubkey(ep, id_pubkey))) else: - pubkeys = get_pubkeys_from_id(ep, id_pubkey) + pubkeys = get_informations_for_identities(ep, id_pubkey) if pubkeys == NO_MATCHING_ID: print(NO_MATCHING_ID) else: @@ -100,6 +92,21 @@ def id_pubkey_correspondence(ep, id_pubkey): print("") +def get_informations_for_identity(ep, id): + """ + Check that the id is present on the network + many identities could match + return the one searched + """ + certs_req = get_informations_for_identities(ep, id) + if certs_req == NO_MATCHING_ID: + message_exit(NO_MATCHING_ID) + for certs_id in certs_req: + if certs_id['uids'][0]['uid'].lower() == id.lower(): + return certs_id + message_exit(NO_MATCHING_ID) + + def get_uid_from_pubkey(ep, pubkey): try: results = get_request(ep, "wot/lookup/" + pubkey) @@ -112,9 +119,14 @@ def get_uid_from_pubkey(ep, pubkey): i += 1 -def get_pubkeys_from_id(ep, uid): +def get_informations_for_identities(ep, identifier): + """ + :identifier: identity or pubkey in part or whole + Return received and sent certifications lists of matching identities + if one identity found + """ try: - results = get_request(ep, "wot/lookup/" + uid) + results = get_request(ep, "wot/lookup/" + identifier) except: return NO_MATCHING_ID return results["results"]