From 0228d596b750ec4e31a24aa56252322940fe0c5f Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Mon, 14 Jun 2021 23:49:18 +0200
Subject: [PATCH] [enh] #95 add Identity.from_lookup_response() to simplify
 examples

---
 duniterpy/documents/identity.py  | 44 ++++++++++++++++++++++++++++++++
 examples/save_revoke_document.py | 32 +++++------------------
 examples/send_certification.py   | 35 +++++++------------------
 3 files changed, 59 insertions(+), 52 deletions(-)

diff --git a/duniterpy/documents/identity.py b/duniterpy/documents/identity.py
index d952a527..30f5af37 100644
--- a/duniterpy/documents/identity.py
+++ b/duniterpy/documents/identity.py
@@ -257,3 +257,47 @@ Timestamp: {timestamp}
         identity.signatures = [signature]
 
         return identity
+
+    @classmethod
+    def from_bma_lookup_response(
+        cls: Type[IdentityType],
+        version: int,
+        currency: str,
+        pubkey: str,
+        lookup_response: dict,
+    ) -> IdentityType:
+        """
+        Return Identity instance from bma.lookup request response
+
+        :param pubkey:
+        :param version: Document version
+        :param currency: Currency codename
+        :param pubkey: Requested identity pubkey
+        :param lookup_response: Lookup request response
+        :return:
+        """
+        identity = None
+        # parse results
+        for result in lookup_response["results"]:
+            if result["pubkey"] == pubkey:
+                uids = result["uids"]
+                uid_data = uids[0]
+                # capture data
+                timestamp = BlockUID.from_str(uid_data["meta"]["timestamp"])
+                uid = uid_data["uid"]  # type: str
+                signature = uid_data["self"]  # type: str
+
+        # return self-certification document
+        identity = cls(
+            version=version,
+            currency=currency,
+            pubkey=pubkey,
+            uid=uid,
+            timestamp=timestamp,
+        )
+        identity.signatures = [signature]
+
+        if identity is None:
+            raise Exception("Identity pubkey not found")
+
+        return identity
diff --git a/examples/save_revoke_document.py b/examples/save_revoke_document.py
index 9c4596ad..f7545004 100644
--- a/examples/save_revoke_document.py
+++ b/examples/save_revoke_document.py
@@ -19,7 +19,7 @@ from typing import Optional
 
 from duniterpy.api import bma
 from duniterpy.api.client import Client
-from duniterpy.documents import BlockUID, Identity, Revocation
+from duniterpy.documents import Identity, Revocation
 from duniterpy.key import SigningKey
 
 if "XDG_CONFIG_HOME" in os.environ:
@@ -65,31 +65,11 @@ def get_identity_document(
     :rtype: Identity
     """
     # Here we request for the path wot/lookup/pubkey
-    lookup_data = client(bma.wot.lookup, pubkey)
-    identity = None
-
-    # parse results
-    for result in lookup_data["results"]:
-        if result["pubkey"] == pubkey:
-            uids = result["uids"]
-            uid_data = uids[0]
-            # capture data
-            timestamp = BlockUID.from_str(uid_data["meta"]["timestamp"])
-            uid = uid_data["uid"]  # type: str
-            signature = uid_data["self"]  # type: str
-
-            # return self-certification document
-            identity = Identity(
-                version=10,
-                currency=current_block["currency"],
-                pubkey=pubkey,
-                uid=uid,
-                timestamp=timestamp,
-            )
-            identity.signatures = [signature]
-            break
-
-    return identity
+    lookup_response = client(bma.wot.lookup, pubkey)
+
+    return Identity.from_bma_lookup_response(
+        PROTOCOL_VERSION, current_block["currency"], pubkey, lookup_response
+    )
 
 
 def get_signed_raw_revocation_document(
diff --git a/examples/send_certification.py b/examples/send_certification.py
index a14db4c9..81d19ec1 100644
--- a/examples/send_certification.py
+++ b/examples/send_certification.py
@@ -29,6 +29,9 @@ from duniterpy.key import SigningKey
 # Here we use the secure BASIC_MERKLED_API (BMAS)
 BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
 
+# Current protocol version
+PROTOCOL_VERSION = 10
+
 
 ################################################
 
@@ -46,31 +49,11 @@ def get_identity_document(
     :rtype: Identity
     """
     # Here we request for the path wot/lookup/pubkey
-    lookup_data = client(bma.wot.lookup, pubkey)
-    identity = None
-
-    # parse results
-    for result in lookup_data["results"]:
-        if result["pubkey"] == pubkey:
-            uids = result["uids"]
-            uid_data = uids[0]
-            # capture data
-            timestamp = BlockUID.from_str(uid_data["meta"]["timestamp"])
-            uid = uid_data["uid"]  # type: str
-            signature = uid_data["self"]  # type: str
-
-            # return self-certification document
-            identity = Identity(
-                version=10,
-                currency=current_block["currency"],
-                pubkey=pubkey,
-                uid=uid,
-                timestamp=timestamp,
-            )
-            identity.signatures = [signature]
-            break
-
-    return identity
+    lookup_response = client(bma.wot.lookup, pubkey)
+
+    return Identity.from_bma_lookup_response(
+        PROTOCOL_VERSION, current_block["currency"], pubkey, lookup_response
+    )
 
 
 def get_certification_document(
@@ -117,7 +100,7 @@ def send_certification():
     pubkey_from = key.pubkey
 
     # prompt entry
-    pubkey_to = input("Enter certified pubkey: ")
+    pubkey_to = input("Enter pubkey to certify: ")
 
     # capture current block to get version and currency and blockstamp
     current_block = client(bma.blockchain.current)
-- 
GitLab