def netcode_and_type_for_text(text, netcodes=None): # check for "public pair" try: LENGTH_LOOKUP = { 33: "public_pair", 65: "public_pair", 16: "elc_seed", 32: "elc_prv", 64: "elc_pub", } as_bin = h2b(text) l = len(as_bin) if l in LENGTH_LOOKUP: return None, LENGTH_LOOKUP[l], as_bin except (binascii.Error, TypeError): pass try: hrp, data = bech32_decode(text) decoded = convertbits(data[1:], 5, 8, False) script = bin_script( [int2byte(data[0]), b''.join(int2byte(d) for d in decoded)]) l = bech32_prefixes().get(hrp, []) if netcodes is None: netcodes = network_codes() for netcode in netcodes: if netcode in l: return netcode, "segwit", script except (TypeError, KeyError): pass data = encoding.a2b_hashed_base58(text) netcode, the_type, length = netcode_and_type_for_data(data, netcodes=netcodes) return netcode, the_type, data[length:]
def disassemble_scripts(input_script, output_script, lock_time, signature_for_hash_type_f): "yield pre_annotations, pc, opcode, instruction, post_annotations" input_annotations_f, output_annotations_f = annotation_f_for_scripts( input_script, output_script, signature_for_hash_type_f) pc = 0 while pc < len(input_script): opcode, data, new_pc = get_opcode(input_script, pc) pre_annotations, post_annotations = input_annotations_f(pc, opcode, data) yield pre_annotations, pc, opcode, instruction_for_opcode(opcode, data), post_annotations pc = new_pc pc = 0 while pc < len(output_script): opcode, data, new_pc = get_opcode(output_script, pc) pre_annotations, post_annotations = output_annotations_f(pc, opcode, data) yield pre_annotations, pc, opcode, instruction_for_opcode(opcode, data), post_annotations pc = new_pc if not is_pay_to_script_hash(output_script): return stack = [] eval_script(input_script, signature_for_hash_type_f, lock_time, expected_hash_type=None, stack=stack) if stack: signatures, new_output_script = stack[:-1], stack[-1] new_input_script = bin_script(signatures) else: signatures, new_output_script, new_input_script = [], b'', b'' for r in disassemble_scripts(new_input_script, new_output_script, lock_time, signature_for_hash_type_f): yield r
def netcode_and_type_for_text(text, netcodes=None): # check for "public pair" try: LENGTH_LOOKUP = { 33: "public_pair", 65: "public_pair", 16: "elc_seed", 32: "elc_prv", 64: "elc_pub", } as_bin = h2b(text) l = len(as_bin) if l in LENGTH_LOOKUP: return None, LENGTH_LOOKUP[l], as_bin except (binascii.Error, TypeError): pass try: hrp, data = bech32_decode(text) decoded = convertbits(data[1:], 5, 8, False) script = bin_script([int2byte(data[0]), b''.join(int2byte(d) for d in decoded)]) l = bech32_prefixes().get(hrp, []) if netcodes is None: netcodes = network_codes() for netcode in netcodes: if netcode in l: return netcode, "segwit", script except (TypeError, KeyError): pass data = encoding.a2b_hashed_base58(text) netcode, the_type, length = netcode_and_type_for_data(data, netcodes=netcodes) return netcode, the_type, data[length:]
def test_bytes(as_bytes): script = bin_script([as_bytes]) stack = [] eval_script(script, None, lock_time=0, stack=stack, disallow_long_scripts=False) assert len(stack) == 1 assert stack[0] == as_bytes
def test_nulldata(self): OP_RETURN = tools.compile("OP_RETURN") # note that because chr() is used samples with length > 255 will not work for sample in [b'test', b'me', b'a', b'39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q', b'', b'0'*80]: sample_script = OP_RETURN + tools.bin_script([sample]) nd = ScriptNulldata(sample) self.assertEqual(nd.nulldata, sample) self.assertEqual(nd.script(), sample_script) nd2 = ScriptNulldata.from_script(sample_script) self.assertEqual(nd.nulldata, nd2.nulldata) out = TxOut(1, nd.script()) tx = Tx(0, [], [out]) # ensure we can create a tx self.assertEqual(nd.script(), tools.compile(tools.disassemble(nd.script()))) # convert between asm and back to ensure no bugs with compilation
def test_bytes(as_bytes): script = bin_script([as_bytes]) stack = [] eval_script(script, None, stack=stack, disallow_long_scripts=False) assert len(stack) == 1 assert stack[0] == as_bytes