def setUp(self) -> None:

        opts = gateway_helpers.get_gateway_opts(8000, include_default_eth_args=True)

        self.node = MockGatewayNode(opts)

        self.connection = MagicMock(spec=AbstractGatewayBlockchainConnection)
        self.connection.node = self.node
        self.connection.is_active = MagicMock(return_value=True)
        self.connection.network_num = 5
        self.connection.format_connection_desc = "127.0.0.1:12345 - B"
        self.node.set_known_total_difficulty = MagicMock()
        self.node.node_conn = self.connection
        self.node.message_converter = EthNormalMessageConverter()

        self.cleanup_service = EthNormalBlockCleanupService(
            self.node, NETWORK_NUM
        )
        self.node.block_cleanup_service = self.cleanup_service
        self.node.block_processing_service = EthBlockProcessingService(
            self.node
        )
        self.node.block_processing_service.queue_block_for_processing = MagicMock()
        self.node.block_queuing_service = EthBlockQueuingService(self.node)

        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111)
        )
        dummy_public_key = crypto_utils.private_to_public_key(dummy_private_key)
        self.sut = EthNodeConnectionProtocol(
            self.connection, True, dummy_private_key, dummy_public_key
        )
        self.sut._waiting_checkpoint_headers_request = False
    def setUp(self):
        self.node = MockGatewayNode(
            gateway_helpers.get_gateway_opts(8000,
                                             include_default_btc_args=True,
                                             include_default_eth_args=True))

        self.relay_connection = AbstractRelayConnection(
            MockSocketConnection(node=self.node,
                                 ip_address="127.0.0.1",
                                 port=12345), self.node)
        self.blockchain_connection = EthBaseConnection(
            MockSocketConnection(node=self.node,
                                 ip_address="127.0.0.1",
                                 port=12345), self.node)
        self.node.message_converter = converter_factory.create_eth_message_converter(
            self.node.opts)

        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        dummy_public_key = crypto_utils.private_to_public_key(
            dummy_private_key)
        self.blockchain_connection_protocol = EthNodeConnectionProtocol(
            self.blockchain_connection, True, dummy_private_key,
            dummy_public_key)
        self.blockchain_connection.network_num = 0
        self.blockchain_connection_protocol.publish_transaction = MagicMock()

        self.relay_connection.state = ConnectionState.INITIALIZED
        gateway_transaction_stats_service.set_node(self.node)
示例#3
0
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(
            8000,
            include_default_eth_args=True,
            track_detailed_sent_messages=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()

        self.node = MockGatewayNode(opts)
        self.node.block_processing_service = MagicMock()

        self.connection = MagicMock()
        gateway_helpers.add_blockchain_peer(self.node, self.connection)
        self.connection.node = self.node
        self.connection.peer_ip = LOCALHOST
        self.connection.peer_port = 8001
        self.connection.network_num = 2
        self.connection.endpoint = IpEndpoint(self.connection.peer_ip,
                                              self.connection.peer_port)
        self.node.blockchain_peers.add(
            BlockchainPeerInfo(self.connection.peer_ip,
                               self.connection.peer_port))
        gateway_bdn_performance_stats_service.set_node(self.node)

        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        dummy_public_key = crypto_utils.private_to_public_key(
            dummy_private_key)
        self.sut = EthNodeConnectionProtocol(self.connection, True,
                                             dummy_private_key,
                                             dummy_public_key)
    def setup_ciphers(self, private_key1=None, private_key2=None):
        if private_key1 is None:
            private_key1 = crypto_utils.make_private_key(
                helpers.generate_bytearray(111))
        if private_key2 is None:
            private_key2 = crypto_utils.make_private_key(
                helpers.generate_bytearray(111))

        public_key1 = crypto_utils.private_to_public_key(private_key1)
        public_key2 = crypto_utils.private_to_public_key(private_key2)

        cipher1 = RLPxCipher(True, private_key1, public_key2)
        cipher2 = RLPxCipher(False, private_key2, public_key1)

        self.assertFalse(cipher1.is_ready())
        self.assertFalse(cipher2.is_ready())

        auth_msg = cipher1.create_auth_message()
        self.assertEqual(len(auth_msg), eth_common_constants.AUTH_MSG_LEN)
        self.assertTrue(auth_msg)

        enc_auth_msg = cipher1.encrypt_auth_message(auth_msg)
        self.assertEqual(len(enc_auth_msg),
                         eth_common_constants.ENC_AUTH_MSG_LEN)

        decrypted_auth_msg, size = cipher2.decrypt_auth_message(enc_auth_msg)
        self.assertEqual(len(decrypted_auth_msg),
                         eth_common_constants.AUTH_MSG_LEN)
        self.assertEqual(size, eth_common_constants.ENC_AUTH_MSG_LEN)

        cipher2.parse_auth_message(decrypted_auth_msg)

        auth_ack_message = cipher2.create_auth_ack_message()
        enc_auth_ack_message = cipher2.encrypt_auth_ack_message(
            auth_ack_message)

        cipher1.decrypt_auth_ack_message(enc_auth_ack_message)

        cipher1.setup_cipher()
        cipher2.setup_cipher()

        self.assertTrue(cipher1.is_ready())
        self.assertTrue(cipher2.is_ready())

        return cipher1, cipher2
示例#5
0
    def test_get_ecdh_key(self):
        private_key1 = crypto_utils.make_private_key(helpers.generate_bytearray(111))
        public_key1 = crypto_utils.private_to_public_key(private_key1)

        private_key2 = crypto_utils.make_private_key(helpers.generate_bytearray(222))
        public_key2 = crypto_utils.private_to_public_key(private_key2)

        private_key3 = crypto_utils.make_private_key(helpers.generate_bytearray(333))

        eccx1 = ECCx(raw_private_key=private_key1)
        eccx2 = ECCx(raw_private_key=private_key2)
        eccx3 = ECCx(raw_private_key=private_key3)

        key1 = eccx1.get_ecdh_key(public_key2)
        key2 = eccx2.get_ecdh_key(public_key1)
        key3 = eccx3.get_ecdh_key(public_key1)

        self.assertEqual(key1, key2)
        self.assertNotEqual(key1, key3)
示例#6
0
    def test_sign_and_verify_signature__valid_signature(self):
        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        public_key = crypto_utils.private_to_public_key(dummy_private_key)

        # generate random bytes
        msg = helpers.generate_bytearray(222)
        msg_hash = eth_common_utils.keccak_hash(msg)

        signature = crypto_utils.sign(msg_hash, dummy_private_key)

        self.assertTrue(
            crypto_utils.verify_signature(public_key, signature, msg_hash))
示例#7
0
    def test_recover_public_key(self):
        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        public_key = crypto_utils.private_to_public_key(dummy_private_key)

        msg = helpers.generate_bytearray(222)
        msg_hash = eth_common_utils.keccak_hash(msg)

        signature = crypto_utils.sign(msg_hash, dummy_private_key)

        recovered_pub_key = crypto_utils.recover_public_key(
            msg_hash, signature)

        self.assertEqual(recovered_pub_key, public_key)
示例#8
0
    def test_hello_eth_message(self):
        dummy_private_key = convert.hex_to_bytes(
            "294549f8629f0eeb2b8e01aca491f701f5386a9662403b485c4efe7d447dfba3")
        dummy_public_key = crypto_utils.private_to_public_key(
            dummy_private_key)

        self._test_msg_serialization(
            HelloEthProtocolMessage,
            False,
            eth_common_constants.P2P_PROTOCOL_VERSION,
            eth_common_constants.BX_ETH_CLIENT_NAME,
            eth_common_constants.CAPABILITIES,
            30303,  # random port value
            dummy_public_key)
    def setUp(self):
        self.local_node_fileno = 1
        self.local_node_fileno_2 = 2
        self.remote_node_fileno = 3
        self.local_blockchain_ip = "127.0.0.1"
        self.local_blockchain_port = 30303
        self.local_blockchain_port_2 = 30302

        eth_node_private_key = crypto_utils.make_private_key(helpers.generate_bytearray(111))
        self.gateway_node = spies.make_spy_node(
            EthGatewayNode, 8000, include_default_eth_args=True, blockchain_protocol="Ethereum",
            blockchain_address=(self.local_blockchain_ip, self.local_blockchain_port),
            blockchain_peers="enode://d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396@127.0.0.1:30302",
            pub_key="d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396")
        gateway_node_private_key = convert.hex_to_bytes(self.gateway_node.opts.private_key)
        eth_node_public_key = crypto_utils.private_to_public_key(eth_node_private_key)

        self.eth_node_cipher, gateway_cipher = self.setup_ciphers(eth_node_private_key, gateway_node_private_key)

        self.gateway_node._node_public_key = eth_node_public_key
        self.gateway_node._remote_public_key = eth_node_public_key

        self.eth_node_connection = spies.make_spy_connection(EthNodeConnection, self.local_node_fileno,
                                                             self.local_blockchain_port, self.gateway_node)

        self.eth_node_connection_2 = spies.make_spy_connection(EthNodeConnection, self.local_node_fileno_2,
                                                               self.local_blockchain_port_2, self.gateway_node)
        self.eth_remote_node_connection = spies.make_spy_connection(EthRemoteConnection, self.remote_node_fileno, 8003,
                                                                    self.gateway_node)

        self.eth_node_connection._rlpx_cipher = gateway_cipher
        self.eth_node_connection_2._rlpx_cipher = gateway_cipher
        self.eth_node_connection.message_factory = EthProtocolMessageFactory(gateway_cipher)
        self.eth_node_connection_2.message_factory = EthProtocolMessageFactory(gateway_cipher)
        self.eth_remote_node_connection._rlpx_cipher = gateway_cipher
        self.eth_remote_node_connection.message_factory = EthProtocolMessageFactory(gateway_cipher)
        self.gateway_node.remote_node_conn = self.eth_remote_node_connection

        self.gateway_node.connection_pool.add(
            self.local_node_fileno, LOCALHOST, self.local_blockchain_port, self.eth_node_connection
        )
        self.gateway_node.connection_pool.add(
            self.local_node_fileno_2, LOCALHOST, self.local_blockchain_port_2, self.eth_node_connection_2
        )
        self.gateway_node.connection_pool.add(self.remote_node_fileno, LOCALHOST, 8003, self.eth_remote_node_connection)
    def setUp(self):
        opts = gateway_helpers.get_gateway_opts(8000, include_default_eth_args=True,
                                                                  track_detailed_sent_messages=True)
        if opts.use_extensions:
            helpers.set_extensions_parallelism()

        self.node = MockGatewayNode(opts)
        self.node.block_processing_service = MagicMock()

        self.connection = MagicMock()
        self.connection.node = self.node
        self.connection.peer_ip = LOCALHOST
        self.connection.peer_port = 8001
        self.connection.network_num = 2

        dummy_private_key = crypto_utils.make_private_key(helpers.generate_bytearray(111))
        dummy_public_key = crypto_utils.private_to_public_key(dummy_private_key)
        self.sut = EthBaseConnectionProtocol(self.connection, True, dummy_private_key, dummy_public_key)
示例#11
0
文件: eccx.py 项目: mxito3/bxgateway
    def _init_ecc(self, raw_public_key, raw_private_key):
        if raw_private_key:
            assert not raw_public_key
            raw_public_key = crypto_utils.private_to_public_key(
                raw_private_key)
        if raw_public_key:
            assert len(raw_public_key) == eth_common_constants.PUBLIC_KEY_LEN
            _, pubkey_x, pubkey_y, _ = self._decode_pubkey(raw_public_key)
        else:
            pubkey_x, pubkey_y = None, None

        while True:
            pyelliptic.ECC.__init__(self,
                                    pubkey_x=pubkey_x,
                                    pubkey_y=pubkey_y,
                                    raw_privkey=raw_private_key,
                                    curve=eth_common_constants.ECIES_CURVE)

            # when raw_private_key is generated by pyelliptic it sometimes has 31 bytes so we try again!
            if self.get_raw_private_key() and len(self.get_raw_private_key(
            )) != eth_common_constants.PRIVATE_KEY_LEN:
                continue
            try:
                if self.get_raw_private_key():
                    bitcoin.get_privkey_format(
                        self.get_raw_private_key())  # failed for some keys
                valid_private_key = True
            except AssertionError:
                valid_private_key = False
            if len(self.get_raw_public_key(
            )) == eth_common_constants.PUBLIC_KEY_LEN and valid_private_key:
                break
            elif raw_private_key or raw_public_key:
                raise Exception("invalid private or public key")

        assert len(
            self.get_raw_public_key()) == eth_common_constants.PUBLIC_KEY_LEN
示例#12
0
 def get_public_key(self) -> bytes:
     return crypto_utils.private_to_public_key(self.get_private_key())
示例#13
0
 def _get_dummy_public_key(self):
     dummy_private_key = crypto_utils.make_private_key(
         helpers.generate_bytearray(111))
     return crypto_utils.private_to_public_key(dummy_private_key)
示例#14
0
 def test_get_raw_public_key(self):
     public_key = self._eccx.get_raw_public_key()
     expected_public_key = crypto_utils.private_to_public_key(self._private_key)
     self.assertEqual(public_key, expected_public_key)
 def get_node_public_key(self, _ip, _port):
     return crypto_utils.private_to_public_key(self.get_private_key())
示例#16
0
    def setUp(self):
        self.node = MockGatewayNode(gateway_helpers.get_gateway_opts(
            8000, include_default_eth_args=True, use_extensions=True),
                                    block_queueing_cls=MagicMock())
        self.node.message_converter = converter_factory.create_eth_message_converter(
            self.node.opts)
        self.node.block_processing_service = BlockProcessingService(self.node)

        is_handshake_initiator = True
        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        dummy_public_key = crypto_utils.private_to_public_key(
            dummy_private_key)
        rlpx_cipher = RLPxCipher(is_handshake_initiator, dummy_private_key,
                                 dummy_public_key)

        node_ssl_service = MockNodeSSLService(EthGatewayNode.NODE_TYPE,
                                              MagicMock())
        local_ip = "127.0.0.1"
        eth_port = 30303
        eth_opts = gateway_helpers.get_gateway_opts(
            1234,
            include_default_eth_args=True,
            blockchain_address=(local_ip, eth_port),
            pub_key=convert.bytes_to_hex(dummy_public_key))
        self.eth_node = EthGatewayNode(eth_opts, node_ssl_service)
        self.blockchain_connection = EthNodeConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=30303), self.node)
        self.blockchain_connection.on_connection_established()
        self.node.connection_pool.add(19, local_ip, eth_port,
                                      self.blockchain_connection)

        self.blockchain_connection_1 = EthNodeConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=333), self.node)
        self.blockchain_connection_1.on_connection_established()
        self.node.mock_add_blockchain_peer(self.blockchain_connection_1)
        self.node_1_endpoint = IpEndpoint(local_ip, 333)
        self.node.connection_pool.add(20, self.node_1_endpoint.ip_address,
                                      self.node_1_endpoint.port,
                                      self.blockchain_connection_1)

        self.blockchain_connection_2 = EthNodeConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=444), self.node)
        self.blockchain_connection_2.on_connection_established()
        self.node.mock_add_blockchain_peer(self.blockchain_connection_2)
        self.node_2_endpoint = IpEndpoint(local_ip, 444)
        self.node.connection_pool.add(21, self.node_2_endpoint.ip_address,
                                      self.node_2_endpoint.port,
                                      self.blockchain_connection_2)

        self.blockchain_connection_1.network_num = 0

        self.tx_blockchain_connection_protocol = EthNodeConnectionProtocol(
            self.blockchain_connection_1, is_handshake_initiator, rlpx_cipher)
        self.block_blockchain_connection_protocol = EthNodeConnectionProtocol(
            self.blockchain_connection_1, is_handshake_initiator, rlpx_cipher)
        self.tx_blockchain_connection_protocol.publish_transaction = MagicMock(
        )
        self.block_blockchain_connection_protocol.publish_transaction = MagicMock(
        )

        self.tx_blockchain_connection_protocol_2 = EthNodeConnectionProtocol(
            self.blockchain_connection_2, is_handshake_initiator, rlpx_cipher)
        self.block_blockchain_connection_protocol_2 = EthNodeConnectionProtocol(
            self.blockchain_connection_2, is_handshake_initiator, rlpx_cipher)
        self.tx_blockchain_connection_protocol_2.publish_transaction = MagicMock(
        )
        self.block_blockchain_connection_protocol_2.publish_transaction = MagicMock(
        )

        self.relay_connection = AbstractRelayConnection(
            MockSocketConnection(1,
                                 node=self.node,
                                 ip_address=local_ip,
                                 port=12345), self.node)
        self.relay_connection.state = ConnectionState.INITIALIZED

        gateway_bdn_performance_stats_service.set_node(self.node)
        self.node.account_id = "12345"