From fc0c33fdb831ad567a176192e4b070f58ea71921 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sat, 13 Mar 2021 13:25:13 +0100
Subject: [PATCH] [mod] cert: Split pre-checks and docs gen into f()

---
 silkaj/cert.py | 97 +++++++++++++++++++++++++++++---------------------
 1 file changed, 56 insertions(+), 41 deletions(-)

diff --git a/silkaj/cert.py b/silkaj/cert.py
index c581c593..53cd5e8b 100644
--- a/silkaj/cert.py
+++ b/silkaj/cert.py
@@ -51,32 +51,8 @@ async def send_certification(ctx, 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")
+    issuer = await pre_checks(client, issuer_pubkey, pubkey_to_certify)
 
     # Display license and ask for confirmation
     head = await HeadBlock().head_block
@@ -88,22 +64,12 @@ async def send_certification(ctx, uid_pubkey_to_certify):
         ctx, 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="",
+    certification = docs_generation(
+        currency,
+        pubkey_to_certify,
+        idty_to_certify,
+        issuer_pubkey,
+        head,
     )
 
     if ctx.obj["DISPLAY_DOCUMENT"]:
@@ -124,6 +90,35 @@ async def send_certification(ctx, uid_pubkey_to_certify):
     await client.close()
 
 
+async def pre_checks(client, issuer_pubkey, pubkey_to_certify):
+    # Check whether current user is member
+    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")
+    return issuer
+
+
 async def certification_confirmation(
     ctx, issuer, issuer_pubkey, pubkey_to_certify, idty_to_certify
 ):
@@ -159,3 +154,23 @@ async def confirmation():
         client = ClientInstance().client
         await client.close()
         sys.exit(SUCCESS_EXIT_STATUS)
+
+
+def docs_generation(currency, pubkey_to_certify, idty_to_certify, issuer_pubkey, head):
+    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"],
+    )
+
+    return Certification(
+        version=10,
+        currency=currency,
+        pubkey_from=issuer_pubkey,
+        identity=identity,
+        timestamp=BlockUID(head["number"], head["hash"]),
+        signature="",
+    )
-- 
GitLab