From 87ff1808a2f4d7b378283d24f269ab64a79a7fb2 Mon Sep 17 00:00:00 2001 From: nothingbutlucas <69118979+nothingbutlucas@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:04:26 -0300 Subject: [PATCH] refactor(main): Reduce complexity of main function This will make it easier for us to maintain and understand the main function. Also deleting duplicated code Signed-off-by: nothingbutlucas <69118979+nothingbutlucas@users.noreply.github.com> --- RNS/Utilities/rnid.py | 56 +++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/RNS/Utilities/rnid.py b/RNS/Utilities/rnid.py index 71cb0ee..02c5eef 100644 --- a/RNS/Utilities/rnid.py +++ b/RNS/Utilities/rnid.py @@ -58,6 +58,27 @@ def spin(until=None, msg="", timeout=None): else: return True + +def get_keys(args: argparse.Namespace, identity) -> None: + """Get public or/and private keys""" + + if args.base64: + RNS.log("Public Key : " + base64.urlsafe_b64encode(identity.get_public_key()).decode("utf-8")) + elif args.base32: + RNS.log("Public Key : " + base64.b32encode(identity.get_public_key()).decode("utf-8")) + else: + RNS.log("Public Key : " + RNS.hexrep(identity.get_public_key(), delimit=False)) + if identity.prv: + if args.print_private: + if args.base64: + RNS.log("Private Key : " + base64.urlsafe_b64encode(identity.get_private_key()).decode("utf-8")) + elif args.base32: + RNS.log("Private Key : " + base64.b32encode(identity.get_private_key()).decode("utf-8")) + else: + RNS.log("Private Key : " + RNS.hexrep(identity.get_private_key(), delimit=False)) + else: + RNS.log("Private Key : Hidden") + def main(): try: parser = argparse.ArgumentParser(description="Reticulum Identity & Encryption Utility") @@ -131,22 +152,8 @@ def main(): exit(42) RNS.log("Identity imported") - if args.base64: - RNS.log("Public Key : "+base64.urlsafe_b64encode(identity.get_public_key()).decode("utf-8")) - elif args.base32: - RNS.log("Public Key : "+base64.b32encode(identity.get_public_key()).decode("utf-8")) - else: - RNS.log("Public Key : "+RNS.hexrep(identity.get_public_key(), delimit=False)) - if identity.prv: - if args.print_private: - if args.base64: - RNS.log("Private Key : "+base64.urlsafe_b64encode(identity.get_private_key()).decode("utf-8")) - elif args.base32: - RNS.log("Private Key : "+base64.b32encode(identity.get_private_key()).decode("utf-8")) - else: - RNS.log("Private Key : "+RNS.hexrep(identity.get_private_key(), delimit=False)) - else: - RNS.log("Private Key : Hidden") + + get_keys(args, identity) if args.write: try: @@ -298,22 +305,7 @@ def main(): exit(0) if args.print_identity: - if args.base64: - RNS.log("Public Key : "+base64.urlsafe_b64encode(identity.get_public_key()).decode("utf-8")) - elif args.base32: - RNS.log("Public Key : "+base64.b32encode(identity.get_public_key()).decode("utf-8")) - else: - RNS.log("Public Key : "+RNS.hexrep(identity.get_public_key(), delimit=False)) - if identity.prv: - if args.print_private: - if args.base64: - RNS.log("Private Key : "+base64.urlsafe_b64encode(identity.get_private_key()).decode("utf-8")) - elif args.base32: - RNS.log("Private Key : "+base64.b32encode(identity.get_private_key()).decode("utf-8")) - else: - RNS.log("Private Key : "+RNS.hexrep(identity.get_private_key(), delimit=False)) - else: - RNS.log("Private Key : Hidden") + get_keys(args, identity) exit(0) if args.export: