def test_self_payment(self): privkey = os.urandom(32) addr = privtoaddr(privkey) ecc = ECCx(privkey) ecc.verify = mock.Mock() msg = mock.Mock() msg.task_to_compute.provider_ethereum_address = encode_hex(addr) res = helpers.process_report_computed_task_no_time_check( msg, ecc, ) self.assertIsInstance(res, message.tasks.RejectReportComputedTask)
def test_fixed_sign_verify(self): # pylint: disable=too-many-locals public_key = "0xcdf2fa12bef915b85d94a9f210f2e432542f249b8225736d923fb0"\ "7ac7ce38fa29dd060f1ea49c75881b6222d26db1c8b0dd1ad4e934" \ "263cc00ed03f9a781444" private_key = "0x1aab847dd0aa9c3993fea3c858775c183a588ac328e5deb9ceeee"\ "3b4ac6ef078" ek = self._create_keysauth() ek.public_key = decode_hex(public_key) ek._private_key = decode_hex(private_key) ek.key_id = encode_hex(ek.public_key)[2:] ek.ecc = ECCx(ek._private_key) msg = message.tasks.WantToComputeTask( node_name='node_name', task_id='task_id', perf_index=2200, price=5 * 10**18, max_resource_size=250000000, max_memory_size=300000000, num_cores=4, ) dumped_l = msg.serialize(sign_as=ek._private_key, encrypt_func=lambda x: x) loaded_l = message.Message.deserialize(dumped_l, decrypt_func=lambda x: x) self.assertEqual(msg.get_short_hash(), loaded_l.get_short_hash()) self.assertTrue(ek.verify(msg.sig, msg.get_short_hash(), public_key))
def test_en_decrypt_shared_mac_data_fail(self): bob = ECCx(None) ciphertext = self.ecies.encrypt( data=b'test', raw_pubkey=bob.raw_pubkey, shared_mac_data=b'shared mac data') with self.assertRaises(exceptions.DecryptionError): self.ecies.decrypt( data=ciphertext, raw_privkey=bob.raw_privkey, shared_mac_data=b'wrong')
def test_en_decrypt_shared_mac_data(self): bob = ECCx(None) text = b'test' shared_mac_data = b'shared mac data' ciphertext = self.ecies.encrypt( data=text, raw_pubkey=bob.raw_pubkey, shared_mac_data=shared_mac_data) decrypted = self.ecies.decrypt( data=ciphertext, raw_privkey=bob.raw_privkey, shared_mac_data=shared_mac_data) self.assertEqual(decrypted, text)
def verify_message_payment_address( report_computed_task: message.tasks.ReportComputedTask, ecc: cryptography.ECCx) -> bool: # Prevent self payments. This check deserve its own reject_reason but also # it belongs ealier in the flow rather then here. decoded_provider_ethereum_address = decode_hex( report_computed_task.task_to_compute.provider_ethereum_address) if privtoaddr(ecc.get_privkey()) == decoded_provider_ethereum_address: logger.warning('Prevented self payment: %r', report_computed_task) return False # Prevent payments to zero address. Same as above. if decoded_provider_ethereum_address == b'\x00' * 20: logger.warning( 'Prevented payment to zero address: %r', report_computed_task, ) return False return True
def _generate_keys() -> ECCx: return ECCx(None)
import socket from golem_messages.cryptography import ECCx import assertpy import mock import pytest from signing_service.constants import SIGNING_SERVICE_DEFAULT_RECONNECT_ATTEMPTS from signing_service.exceptions import SigningServiceMaximumReconnectionAttemptsExceeded from signing_service.signing_service import SigningService from signing_service.utils import ConsoleNotifier TEST_ETHEREUM_PRIVATE_KEY = '47a286230c8b3a1c3fa0282f6a65d1d57ffe5147dafaef7cd110d24ed51b462e' concent_ecc_keys = ECCx(None) (CONCENT_PRIVATE_KEY, CONCENT_PUBLIC_KEY ) = concent_ecc_keys.raw_privkey, concent_ecc_keys.raw_pubkey signing_service_ecc_keys = ECCx(None) (SIGNING_SERVICE_PRIVATE_KEY, SIGNING_SERVICE_PUBLIC_KEY ) = signing_service_ecc_keys.raw_privkey, signing_service_ecc_keys.raw_pubkey class TestSigningServiceRun: @pytest.fixture(autouse=True) def setUp(self, unused_tcp_port_factory): self.host = '127.0.0.1' self.port = unused_tcp_port_factory() self.initial_reconnect_delay = 2 self.parameters = [ self.host,
from middleman_protocol.constants import PayloadType from middleman_protocol.exceptions import FrameInvalidMiddlemanProtocolError from middleman_protocol.exceptions import PayloadTypeInvalidMiddlemanProtocolError from middleman_protocol.exceptions import SignatureInvalidMiddlemanProtocolError from middleman_protocol.message import AbstractFrame from middleman_protocol.message import AuthenticationChallengeFrame from middleman_protocol.message import AuthenticationResponseFrame from middleman_protocol.message import ErrorFrame from middleman_protocol.message import GolemMessageFrame from middleman_protocol.message import HeartbeatFrame from middleman_protocol.registry import create_middleman_protocol_message from middleman_protocol.registry import PAYLOAD_TYPE_TO_MIDDLEMAN_MESSAGE_CLASS from middleman_protocol.stream import send_over_stream from middleman_protocol.stream import unescape_stream concent_ecc_keys = ECCx(None) (CONCENT_PRIVATE_KEY, CONCENT_PUBLIC_KEY ) = concent_ecc_keys.raw_privkey, concent_ecc_keys.raw_pubkey class TestMessageMiddlemanProtocol: request_id = 99 @pytest.mark.parametrize( ('expected_middleman_message_type', 'payload_type', 'payload'), [ (GolemMessageFrame, PayloadType.GOLEM_MESSAGE, Ping()), (ErrorFrame, PayloadType.ERROR, (111, 'error_message')), (AuthenticationChallengeFrame, PayloadType.AUTHENTICATION_CHALLENGE, b'random_bytes'), (AuthenticationResponseFrame, PayloadType.AUTHENTICATION_RESPONSE,
def test_privtopub(self): priv = mk_privkey('test') pub = privtopub(priv) pub2 = ECCx(raw_privkey=priv).raw_pubkey assert pub == pub2
def get_ecc(secret=''): return ECCx(raw_privkey=mk_privkey(secret))