def parse_script(s): def ishex(s): return set(s).issubset(set('0123456789abcdefABCDEF')) r = [] opcodes_by_name = {} for name, code in OPCODES_BY_NAME.items(): opcodes_by_name[name] = code opcodes_by_name[name[3:]] = code for word in s.split(): if word.isdigit() or (word[0] == '-' and word[1:].isdigit()): if int(word) in (0, 80): r.append(None) elif int(word) < 0: raise ValueError("Negative 1 error!") elif 81 <= int(word) <= 96: raise ValueError("Range of 81-96, should be 1-16\n" "WE SHOULDN'T GET HERE") # check r.append(int(word)) elif word.startswith('0x') and ishex(word[2:]): if int(word[2:], 16) <= 0x4e: continue # we don't need push codes else: r.append(word[2:]) elif len(word) >= 2 and ((word[0], word[-1]) == ("'", "'") or (word[0], word[-1]) == ("[", "]")): r.append(word[1:-1]) elif word in opcodes_by_name: # Should be op_codes > 0x60 assert opcodes_by_name[word] > 96 r.append(opcodes_by_name[word]) # r.append(get_op(v[3:])) else: raise ValueError("could not parse script! (word=\t%s)" % str(word)) sc = serialize_script(r) # 7,12,13,24 don't work return sc
def parse_script(spk): from bitcoin.transaction import serialize_script spk, res = str(spk), [] if all([x in spk for x in ['[', ']']]): # HASH160 0x14 [0xdc44b1164188067c3a32d4780f5996fa14a4f2d9] EQUALVERIFY spk = spk.replace('[', '0x').replace(']', '') for word in spk.split(): if word.isdigit() or (word[0] == '-' and word[1:].isdigit()): res.append(int(word, 0)) elif word.startswith('0x') and re.match('^[0-9a-fA-F]*$', word[2:]): if int(word[2:], 16) < 0x4c: continue else: res.append(word[2:]) elif len(word) >= 2 and word[0] == "'" and word[-1] == "'": res.append(word[1:-1]) elif word.startswith('[0x') and word.endswith(']') and re.match('^[0-9a-fA-F]*$', word[3:-1]): word = word[1:-1] if int(word[2:], 16) < 0x4c: continue else: res.append(word[2:]) elif word in OPname: res.append(OPname[word]) # r.append(get_op(v[3:])) return serialize_script(res)