Ejemplo n.º 1
0
    async def zil_requestWork(request, pub_key: str, header: str,
                              block_num: str, boundary: str, timeout: str,
                              signature: str) -> bool:
        assert (len(pub_key) == 68 and  # 33 bytes -> "0x" + 66 chars
                len(header) == 66 and  # 32 bytes -> "0x" + 64 chars
                len(block_num) == 18 and  # 8 bytes  -> "0x" + 16 chars
                len(boundary) == 66 and  # 32 bytes -> "0x" + 64 chars
                len(timeout) == 10 and  # 4 bytes  -> "0x" + 8 chars
                len(signature) == 130)  # 64 bytes -> "0x" + 128 chars

        # verify signature
        if not verify_signature(pub_key, signature, pub_key, header, block_num,
                                boundary, timeout):
            logging.warning(f"failed verify signature")
            return False

        block_num = crypto.hex_str_to_int(block_num)
        timeout = crypto.hex_str_to_int(timeout)

        node = zilnode.ZilNode.get_by_pub_key(pub_key=pub_key, authorized=True)
        if not (node and node.authorized):
            logging.warning(f"unauthorized public key: {pub_key}")
            return False

        work = pow.PowWork.new_work(header,
                                    block_num,
                                    boundary,
                                    pub_key=pub_key,
                                    signature=signature,
                                    timeout=timeout)
        # update pow window
        pow.PoWWindow.update_pow_window(work)

        return work is not None
Ejemplo n.º 2
0
    def test_zil_mykey(self):
        key = crypto.ZilKey.load_mykey_txt(path_join("mykey.txt"))
        assert key.address == "967e40168af66f441b73c0146e26069bfc3accc7"

        with pytest.raises(AssertionError):
            crypto.ZilKey(
                "02A349FA10F0E6A614A38D6033588A422357F2C60AF2EEBAE15D06498DF8AF0B05",
                "75889EA1AF5D402B69E61C654C74D8B569E363D2E271E1E6E2B63FDB9B635173"
            )

        new_key = crypto.ZilKey(
            "02A349FA10F0E6A614A38D6033588A422357F2C60AF2EEBAE15D06498DF8AF0B05",
            "75889EA1AF5D402B69E61C654C74D8B569E363D2E271E1E6E2B63FDB9B635174")

        assert key == new_key
        assert key != crypto.ZilKey.generate_key_pair()

        pub_key = "0x03949D29723DA4B2628224D3EC8E74C518ACA98C6630B00527F86B8349E982CB57"
        private_key = "05C3CF3387F31202CD0798B7AA882327A1BD365331F90954A58C18F61BD08FFC"
        wallet_address = "95B27EC211F86748DD985E1424B4058E94AA5814"

        new_key = crypto.ZilKey(str_public=pub_key)
        assert new_key.address == wallet_address.lower()

        new_key = crypto.ZilKey(str_private=private_key)
        assert crypto.hex_str_to_int(
            new_key.keypair_str.public) == crypto.hex_str_to_int(pub_key)

        assert new_key.address == wallet_address.lower()
Ejemplo n.º 3
0
    async def zil_requestWork(request, pub_key: str, header: str,
                              block_num: str, boundary: str, timeout: str,
                              signature: str) -> bool:
        assert (len(pub_key) == 68 and  # 33 bytes -> "0x" + 66 chars
                len(header) == 66 and  # 32 bytes -> "0x" + 64 chars
                len(block_num) == 18 and  # 8 bytes  -> "0x" + 16 chars
                len(boundary) == 66 and  # 32 bytes -> "0x" + 64 chars
                len(timeout) == 10 and  # 4 bytes  -> "0x" + 8 chars
                len(signature) == 130)  # 64 bytes -> "0x" + 128 chars

        str_block_num, str_timeout = block_num, timeout
        block_num = crypto.hex_str_to_int(block_num)
        timeout = crypto.hex_str_to_int(timeout)

        if config["zilliqa"]["enabled"]:
            # 0. check network info
            if not check_network_info(block_num, boundary, timeout):
                logging.warning(f"Invalid PoW request from {pub_key}")
                return False

        # verify signature
        if not verify_signature(pub_key, signature, pub_key, header,
                                str_block_num, boundary, str_timeout):
            # hotfix for Zilliqa v4.2.0
            # set timeout to 60 and try again
            str_timeout = crypto.int_to_hex_str_0x(60, n_bytes=4)
            if not verify_signature(pub_key, signature, pub_key, header,
                                    str_block_num, boundary, str_timeout):
                logging.warning(f"failed verify signature")
                return False

        node = zilnode.ZilNode.get_by_pub_key(pub_key=pub_key, authorized=True)
        if not (node and node.authorized):
            logging.warning(f"unauthorized public key: {pub_key}")
            return False

        count = pow.PowWork.count(pub_key=pub_key, block_num=block_num)
        if count >= 2:
            logging.warning(
                f"too many PoW requests from {block_num} {pub_key}")
            return False

        work = pow.PowWork.new_work(header,
                                    block_num,
                                    boundary,
                                    pub_key=pub_key,
                                    signature=signature,
                                    timeout=timeout,
                                    pow_fee=node.pow_fee)
        # update pow window
        pow.PoWWindow.update_pow_window(work)

        logging.critical(
            f"PoW work {block_num} {header} requested from {pub_key}")

        return work is not None
Ejemplo n.º 4
0
    def test_hex_str(self):
        hex_str = "DEADBEEF"
        hex_str_odd = "deadbee"
        bin_bytes = b"\xde\xad\xbe\xef"
        bin_bytes_odd = b"\x0d\xea\xdb\xee"

        assert crypto.hex_str_to_bytes(hex_str) == bin_bytes
        assert crypto.bytes_to_hex_str(bin_bytes) == hex_str.lower()

        assert crypto.hex_str_to_int(hex_str) == 0xDEADBEEF
        assert crypto.bytes_to_int(bin_bytes) == 0xdeadbeef

        assert crypto.hex_str_to_int(hex_str_odd) == 0xDEADBEE
        assert crypto.bytes_to_int(bin_bytes_odd) == 0xDEADBEE