From 04ecedc277de244833ce5300593b7e312609c404 Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Thu, 8 Jul 2021 13:50:14 +0200
Subject: [PATCH] [enh] #173: Implement Transaction.sign() and
 Transaction.check_signature()

---
 duniterpy/documents/transaction.py  | 24 ++++++++++++++++++------
 tests/documents/test_transaction.py | 24 ++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/duniterpy/documents/transaction.py b/duniterpy/documents/transaction.py
index fb565bd0..ff0a45ea 100644
--- a/duniterpy/documents/transaction.py
+++ b/duniterpy/documents/transaction.py
@@ -929,11 +929,17 @@ Currency: {1}
         return signed_raw
 
     def sign(self, key: SigningKey) -> None:
-        raise Exception("sign() is not implemented, use multi_sign()")
+        """
+        Add a signature to the document from key
+
+        :param key: SigningKey instance
+        :return:
+        """
+        self.multi_sign(key)
 
     def multi_sign(self, keys: Union[SigningKey, List[SigningKey]]) -> None:
         """
-        Sign the current document with multiple keys
+        Add signature(s) to the document from one key or a list of multiple keys
 
         :param keys: Libnacl key or list of them
         """
@@ -945,12 +951,18 @@ Currency: {1}
             logging.debug("Signature : \n%s", signature.decode("ascii"))
             self.signatures.append(signature.decode("ascii"))
 
-    def check_signature(self, pubkey: str):
-        raise Exception("check_signature() is not implemented, use check_signatures()")
+    def check_signature(self, pubkey: str) -> bool:
+        """
+        Check if document is signed by pubkey
+
+        :param pubkey: Base58 pubkey
+        :return:
+        """
+        return self.check_signatures(pubkey)
 
-    def check_signatures(self, pubkeys: Union[str, List[str]]):
+    def check_signatures(self, pubkeys: Union[str, List[str]]) -> bool:
         """
-        Check if the signatures matches the pubkeys
+        Check if the signatures matches a public key or a list of public keys
 
         :param pubkeys: Base58 public key or list of them
 
diff --git a/tests/documents/test_transaction.py b/tests/documents/test_transaction.py
index b1baa85e..58be4321 100644
--- a/tests/documents/test_transaction.py
+++ b/tests/documents/test_transaction.py
@@ -485,6 +485,30 @@ class TestTransaction(unittest.TestCase):
         unlock2 = Unlock.from_inline("0:SIG(0)")
         self.assertEqual(unlock1, unlock2)
 
+    def test_check_signature(self):
+        # create 3 wallets
+        signing_key = SigningKey.from_credentials("1", "1")
+
+        issuer = signing_key.pubkey
+
+        transaction = Transaction(
+            block_id=BlockID(
+                8979, "000041DF0CCA173F09B5FBA48F619D4BC934F12ADF1D0B798639EB2149C4A8CC"
+            ),
+            locktime=0,
+            issuers=[issuer],
+            inputs=[InputSource.from_inline(input_source_str)],
+            unlocks=[Unlock(index=0, parameters=[SIGParameter(0)])],
+            outputs=[OutputSource.from_inline(output_source_str)],
+            comment="",
+            currency=G1_TEST_CURRENCY_CODENAME,
+        )
+
+        # multi-signature on the transaction
+        transaction.sign(signing_key)
+
+        self.assertTrue(transaction.check_signature(issuer))
+
     def test_check_signatures(self):
         # create 3 wallets
         signing_key_01 = SigningKey.from_credentials("1", "1")
-- 
GitLab