def decimal_to_formatted_hex(s, loc, toks=None): """Convert decimal to hex.""" if toks is None: return for i, t in enumerate(toks): token = hex(int(t)) new_tok = format_hex_string(token) toks[i] = new_tok return toks
def from_human(cls, data): hex_str = [] try: d = shlex.split(data, posix=False) except Exception: d = data.split() while 1: if len(d) == 0: break word = d[0] d = d[1:] if word.startswith('PUSHDATA'): continue opcode = opcodes.opcodes_by_name.get(word) if opcode is not None: hex_str.append( format_hex_string(hex(opcode), with_prefix=False)) continue # data to be pushed pushdata = word # Make sure hex is formatted. if is_hex(pushdata): if pushdata.startswith('0x'): pushdata = pushdata[2:] if len(pushdata) % 2 != 0: pushdata = ''.join(['0', pushdata]) # Hex-encode text. else: if pushdata.startswith('"') and pushdata.endswith('"'): pushdata = pushdata[1:-1] pushdata = pushdata.encode('hex') hex_str.append(push_script(pushdata)) hex_str = ''.join(hex_str) return cls(hex_str.decode('hex'))
def from_human(cls, data): hex_str = [] try: d = shlex.split(data, posix=False) except Exception: d = data.split() while 1: if len(d) == 0: break word = d[0] d = d[1:] if word.startswith('PUSHDATA'): continue opcode = opcodes.opcodes_by_name.get(word) if opcode is not None: hex_str.append(format_hex_string(hex(opcode), with_prefix=False)) continue # data to be pushed pushdata = word # Make sure hex is formatted. if is_hex(pushdata): if pushdata.startswith('0x'): pushdata = pushdata[2:] if len(pushdata) % 2 != 0: pushdata = ''.join(['0', pushdata]) # Hex-encode text. else: if pushdata.startswith('"') and pushdata.endswith('"'): pushdata = pushdata[1:-1] pushdata = pushdata.encode('hex') hex_str.append(push_script(pushdata)) hex_str = ''.join(hex_str) return cls(hex_str.decode('hex'))
def hex_to_formatted_hex(s, loc, toks): """Add "0x" prefix and ensure even length.""" for i, t in enumerate(toks): new_tok = format_hex_string(t) toks[i] = new_tok return toks