def is_less_or_equal(hash_1: Union[str, bytes], hash_2: Union[str, bytes]) -> bool: """check hash result.""" if isinstance(hash_1, str): hash_1 = utils.hex_str_to_bytes(hash_1) if isinstance(hash_2, str): hash_2 = utils.hex_str_to_bytes(hash_2) assert isinstance(hash_1, bytes) assert isinstance(hash_2, bytes) return utils.bytes_to_int(hash_1) <= utils.bytes_to_int(hash_2)
def boundary_to_hashpower(boundary: Union[str, bytes]) -> int: """boundary to hashrate.""" dividend = 0xffff000000000000000000000000000000000000000000000000000000000000 if isinstance(boundary, str): return dividend // utils.hex_str_to_int(boundary) elif isinstance(boundary, bytes): return dividend // utils.bytes_to_int(boundary) raise TypeError("Type of boundary should be str or bytes")
def boundary_to_difficulty_divided(boundary, n_divided: int = 8, n_divided_start: int = 32) -> int: """Boundary to divided difficulty.""" if isinstance(boundary, str): boundary = utils.hex_str_to_bytes(boundary) difficulty_level = boundary_to_difficulty(boundary) if difficulty_level < n_divided_start: return difficulty_level n_level = difficulty_level - n_divided_start int_cur_boundary = utils.bytes_to_int(boundary) int_cur_level = utils.bytes_to_int( difficulty_to_boundary(difficulty_level)) step = (int_cur_level >> 1) // n_divided m_sub_level = (int_cur_level - int_cur_boundary) // step new_difficulty = n_divided_start + n_level * n_divided + m_sub_level return new_difficulty
def _generate_keys(self): if self._bytes_private: self._private_key = utils.bytes_to_int(self._bytes_private) assert self._private_key < schnorr.CURVE.q if self._bytes_public: self._public_key = schnorr.decode_public(self._bytes_public) if self._private_key and self._public_key: _pub_key = schnorr.get_public_key(self._private_key) assert _pub_key == self._public_key, "public/private key mismatch" # generate public key from private key if self._private_key and not self._public_key: self._public_key = schnorr.get_public_key(self._private_key)
def difficulty_to_boundary_divided(difficulty: int, n_divided: int = 8, n_divided_start: int = 32) -> bytes: """Zilliqa divided difficulty to boundary.""" if difficulty < n_divided_start: return difficulty_to_boundary(difficulty) n_level = (difficulty - n_divided_start) // n_divided m_sub_level = (difficulty - n_divided_start) % n_divided difficulty_level = n_divided_start + n_level int_boundary = utils.bytes_to_int(difficulty_to_boundary(difficulty_level)) boundary_change_step = (int_boundary >> 1) // n_divided int_boundary -= boundary_change_step * m_sub_level return utils.int_to_bytes(int_boundary, n_bytes=32)
def to_checksum_address(address: str, prefix="0x") -> Optional[str]: """Convert address to checksum address.""" if not is_valid_address(address): return None address = address.lower().replace("0x", "") address_bytes = utils.hex_str_to_bytes(address) v = utils.bytes_to_int(tools.hash256_bytes(address_bytes)) checksum_address = prefix for i, c in enumerate(address): if not c.isdigit(): if v & (1 << 255 - 6 * i): c = c.upper() else: c = c.lower() checksum_address += c return checksum_address