def read_entry(f, expected_name): name, value = f.readline().strip().split("=") name, value = name.strip(), value.strip() assert name == expected_name if not value: return b"" return utils.hex_str_to_bytes(value)
async def send_data(self, path, data, session_id=0): print('Client->Device msg :', path, session_id, str_to_bytes(data).hex()) try: resp = input('Enter device->client msg : ') except Exception as err: print('error:', err) return None return hex_str_to_bytes(resp)
def onSendSettingsAsciiClicked(self): self.config["sendAscii"] = True try: data = self.sendArea.toPlainText().replace("\n"," ").strip() self.sendArea.clear() if data != "": data = utils.hex_str_to_bytes(data).decode(self.configGlobal["encoding"],'ignore') self.sendArea.insertPlainText(data) except Exception as e: # QMessageBox.information(self,self.strings.strWriteFormatError,self.strings.strWriteFormatError) print("format error")
def test_pow(self): block_num = 22 header = utils.hex_str_to_bytes("372eca2454ead349c3df0ab5d00b0b706b23e49d469387db91811cee0358fc6d") excepted_result = utils.hex_str_to_bytes("00000b184f1fdd88bfd94c86c39e65db0c36144d5e43f745f722196e730cb614") excepted_mix = b'/t\xcd\xeb\x19\x8a\xf0\xb9\xab\xe6]"\xd3r\xe2/\xb2\xd4t7\x17t\xa9X<\x1c\xc4\'\xa0y9\xf5' nonce = 0x495732e0ed7a801c boundary20 = pow.difficulty_to_boundary(20) boundary21 = pow.difficulty_to_boundary(21) calc_mix_digest, calc_result = pow.pow_hash(block_num, header, nonce) assert calc_result == excepted_result assert calc_mix_digest == excepted_mix assert pow.verify_pow_work(block_num, header, excepted_mix, nonce, boundary20) assert not pow.verify_pow_work(block_num, header, excepted_mix, nonce, boundary21) assert pow.verify_pow_work(0, header, excepted_mix, nonce, boundary20) assert pow.verify_pow_work(29999, header, excepted_mix, nonce, boundary20) assert not pow.verify_pow_work(30000, header, excepted_mix, nonce, boundary20) assert not pow.verify_pow_work(30001, header, excepted_mix, nonce, boundary20)
def build_transaction_params(self, zil_key: ZilKey, to_addr: str, amount: Union[str, int], nonce: Union[str, int], gas_price: Union[str, int], gas_limit: Union[str, int], code="", data="", priority=False): if not is_valid_checksum_address(to_addr): raise ValueError("invalid checksum address") txn_proto = pb2.ProtoTransactionCoreInfo() txn_proto.version = self.version txn_proto.nonce = int(nonce) txn_proto.toaddr = utils.hex_str_to_bytes(to_addr) txn_proto.senderpubkey.data = zil_key.keypair_bytes.public txn_proto.amount.data = utils.int_to_bytes(int(amount), n_bytes=16) txn_proto.gasprice.data = utils.int_to_bytes(int(gas_price), n_bytes=16) txn_proto.gaslimit = int(gas_limit) if code: txn_proto.code = code.encode("utf-8") if data: txn_proto.data = data.encode("utf-8") data_to_sign = txn_proto.SerializeToString() signature = zil_key.sign_str(data_to_sign) # assert zil_key.verify(signature, data_to_sign) params = { "version": txn_proto.version, "nonce": txn_proto.nonce, "toAddr": to_addr, "amount": str(int(amount)), "pubKey": zil_key.keypair_str.public, "gasPrice": str(gas_price), "gasLimit": str(gas_limit), "code": code or None, "data": data or None, "signature": signature, "priority": priority, } return params
def parseSendData(self, data:str, encoding, usrCRLF=False, isHexStr=False, escape=False): if not data: return b'' if usrCRLF: data = data.replace("\n", "\r\n") if isHexStr: if usrCRLF: data = data.replace("\r\n", " ") else: data = data.replace("\n", " ") data = utils.hex_str_to_bytes(data) if data == -1: self.hintSignal.emit("error", _("Error"), _("Format error, should be like 00 01 02 03")) return b'' else: if not escape: data = data.encode(encoding,"ignore") else: # '11234abcd\n123你好\r\n\thello\x00\x01\x02' try: data = utils.str_to_bytes(data, escape=True, encoding=encoding) except Exception as e: self.hintSignal.emit("error", _("Error"), _("Escape is on, but escape error:") + str(e)) return b'' return data