Skip to content
Snippets Groups Projects
Commit 04ecedc2 authored by Vincent Texier's avatar Vincent Texier
Browse files

[enh] #173: Implement Transaction.sign() and Transaction.check_signature()

parent 89bf76f2
No related branches found
No related tags found
2 merge requests!157v1.0.0rc0: merge dev into master,!156Release v1.0.0rc0
...@@ -929,11 +929,17 @@ Currency: {1} ...@@ -929,11 +929,17 @@ Currency: {1}
return signed_raw return signed_raw
def sign(self, key: SigningKey) -> None: 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: 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 :param keys: Libnacl key or list of them
""" """
...@@ -945,12 +951,18 @@ Currency: {1} ...@@ -945,12 +951,18 @@ Currency: {1}
logging.debug("Signature : \n%s", signature.decode("ascii")) logging.debug("Signature : \n%s", signature.decode("ascii"))
self.signatures.append(signature.decode("ascii")) self.signatures.append(signature.decode("ascii"))
def check_signature(self, pubkey: str): def check_signature(self, pubkey: str) -> bool:
raise Exception("check_signature() is not implemented, use check_signatures()") """
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 :param pubkeys: Base58 public key or list of them
......
...@@ -485,6 +485,30 @@ class TestTransaction(unittest.TestCase): ...@@ -485,6 +485,30 @@ class TestTransaction(unittest.TestCase):
unlock2 = Unlock.from_inline("0:SIG(0)") unlock2 = Unlock.from_inline("0:SIG(0)")
self.assertEqual(unlock1, unlock2) 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): def test_check_signatures(self):
# create 3 wallets # create 3 wallets
signing_key_01 = SigningKey.from_credentials("1", "1") signing_key_01 = SigningKey.from_credentials("1", "1")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment