def test_hex_str_padding(self): dead_beef = 0xDEADBEEF assert len(crypto.int_to_bytes(dead_beef)) == crypto.TOKEN_NUM_BYTES assert crypto.int_to_bytes(dead_beef, n_bytes=None) == b"\xde\xad\xbe\xef" for i in range(4): with pytest.raises(OverflowError, message="Expecting OverflowError:"): crypto.int_to_bytes(dead_beef, n_bytes=i) for i in range(5, 130): assert len(crypto.int_to_bytes(dead_beef, n_bytes=i)) == i
def make_work_request(self, work, diff): timeout = crypto.int_to_bytes(self.pow_timeout, n_bytes=4) boundary = ethash.difficulty_to_boundary(diff) # requests are bytes bytes_reqs = [ self.key.keypair_bytes.public, # 33 bytes work["header"], # 32 bytes work["block_num"], # 8 bytes boundary, # 32 bytes timeout, # 4 bytes ] # convert to bytes to sign bytes_to_sign = b"".join(bytes_reqs) assert len(bytes_to_sign) == 33 + 32 * 2 + 8 + 4 signature = self.key.sign( bytes_to_sign) # signature is hex string, len 128 assert len(signature) == 128 signature = "0x" + signature # convert to hex string starts with "0x" req = [crypto.bytes_to_hex_str_0x(b) for b in bytes_reqs] # append signature req.append(signature) return req
def create_work(self): header = crypto.rand_bytes(32) block_num = crypto.int_to_bytes(self.block, n_bytes=8) return { "header": header, # 32 bytes "block_num": block_num, # 8 bytes }
def verify_signature(pub_key, signature, *parameters): if zil_config["verify_sign"] is False: return True key = crypto.ZilKey(str_public=pub_key) msg_to_verify = b"" for param in parameters: if isinstance(param, bytes): b_param = param elif isinstance(param, str): b_param = crypto.hex_str_to_bytes(param) elif isinstance(param, bool): b_param = b"\x01" if param else b"\x00" elif isinstance(param, int): b_param = crypto.int_to_bytes(param, n_bytes=8) else: logging.warning(f"wrong data type") return False msg_to_verify += b_param return key.verify(signature, msg_to_verify)