def gen_valid_vectors(): '''Generate valid test vectors''' while True: for template in templates: prefix = bytearray(template[0]) payload = os.urandom(template[1]) suffix = bytearray(template[2]) chk_b = bytearray() chk_b.extend(prefix) chk_b.extend(payload) chk_b.extend(suffix) metadata = dict([(x, y) for (x, y) in zip(metadata_keys, template[3]) if y is not None]) rv = b58encode_chk(chk_b) assert is_valid(rv) if (metadata['isPrivkey']): yield (rv, payload.hex(), metadata) else: if (metadata['addrType'] == 'pubkey'): # OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG; p2pkh = CScript([ OP_DUP, OP_HASH160, payload, OP_EQUALVERIFY, OP_CHECKSIG ]) yield (rv, p2pkh.hex(), metadata) else: # OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL; p2sh = CScript([OP_HASH160, payload, OP_EQUAL]) yield (rv, p2sh.hex(), metadata)
def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt_suffix): '''Generate possibly invalid vector''' if corrupt_prefix: prefix = os.urandom(1) else: prefix = bytearray(template[0]) if randomize_payload_size: payload = os.urandom(max(int(random.expovariate(0.5)), 50)) else: payload = os.urandom(template[1]) if corrupt_suffix: suffix = os.urandom(len(template[2])) else: suffix = bytearray(template[2]) chk_b = bytearray() chk_b.extend(prefix) chk_b.extend(payload) chk_b.extend(suffix) return b58encode_chk(chk_b)