import pytest from tests.helpers.models import ClientCryptograpy from tests.helpers.resources import WIF, MESSAGE_IN_BYTES @pytest.mark.crypto class TestSignAndVerify: def test_sign_rfc6979_success(self, client_cryptography: ClientCryptograpy): private_key = client_cryptography.key_extension.get_private_key_from_wif(WIF) signature = client_cryptography.signer.sign_rfc6979(private_key, MESSAGE_IN_BYTES) assert len(signature) == 64, f"Incorrect len of signature, Expected: 64, Actual: {len(signature)}" assert isinstance(signature, bytes), f"Not correct type of signature, Expected bytes, Actual: {type(signature)}" def test_sign_success(self, client_cryptography: ClientCryptograpy): private_key = client_cryptography.key_extension.get_private_key_from_wif(WIF) signature = client_cryptography.signer.sign(private_key, MESSAGE_IN_BYTES) assert len(signature) == 64, f"Incorrect len of signature, Expected: 64, Actual: {len(signature)}" assert isinstance(signature, bytes), f"Not correct type of signature, Expected bytes, Actual: {type(signature)}" @pytest.mark.parametrize("message", [MESSAGE_IN_BYTES, b""]) def test_verify_rfc6979_success(self, client_cryptography: ClientCryptograpy, message: bytes): private_key = client_cryptography.key_extension.get_private_key_from_wif(WIF) public_key = client_cryptography.key_extension.get_public_key(private_key) signature = client_cryptography.signer.sign_rfc6979(private_key, message) assert client_cryptography.verifier.verify_rfc6979(public_key, message, signature) is True @pytest.mark.parametrize("message", [MESSAGE_IN_BYTES, b""]) def test_verify_success(self, client_cryptography: ClientCryptograpy, message: bytes): private_key = client_cryptography.key_extension.get_private_key_from_wif(WIF) public_key = client_cryptography.key_extension.get_public_key(private_key) signature = client_cryptography.signer.sign(private_key, message) assert client_cryptography.verifier.verify(public_key, message, signature) is True # = = = = = = = = = = = = = = = = = = = = = NEGATIVE cases = = = = = = = = = = = = = = = = = = = = = def test_verify_empty_public_key(self, client_cryptography: ClientCryptograpy): with pytest.raises(ValueError, match="Incorrect public key"): client_cryptography.verifier.verify(b'', b"Test message", b"signature") def test_verify_invalid_signature(self, client_cryptography: ClientCryptograpy): invalid_signature = b"invalid_signature" private_key = client_cryptography.key_extension.get_private_key_from_wif(WIF) public_key = client_cryptography.key_extension.get_public_key(private_key) assert not client_cryptography.verifier.verify(public_key, MESSAGE_IN_BYTES, invalid_signature), "Verified invalid signature" def test_sign_empty_key(self, client_cryptography: ClientCryptograpy): with pytest.raises(ValueError, match="Incorrect private key"): client_cryptography.signer.sign(b'', MESSAGE_IN_BYTES)