def test_parse(self): self.assertEqual( script.parse_p2pkh(script.deserialize(b'v\xa9\x14\xf5\x8e:\xac\xc8BO\x04)\\\x14\xacfu\x11\x8b\x7f\n8\r\x88\xac')), b'\xf5\x8e:\xac\xc8BO\x04)\\\x14\xacfu\x11\x8b\x7f\n8\r' ) self.assertEqual( script.parse_p2sh(script.deserialize(b'\xa9\x14\xf5\x8e:\xac\xc8BO\x04)\\\x14\xacfu\x11\x8b\x7f\n8\r\x87')), b'\xf5\x8e:\xac\xc8BO\x04)\\\x14\xacfu\x11\x8b\x7f\n8\r' ) self.assertEqual( script.parse_data(script.deserialize(b'j\x14g\x8b\xe5\xdc\x96m\x10\xed\xf1\xaf\xe6\xf9\x1e\xce\x8b\xd8Gt\xad3')), b'g\x8b\xe5\xdc\x96m\x10\xed\xf1\xaf\xe6\xf9\x1e\xce\x8b\xd8Gt\xad3' )
def sighash(tx, i, prevScript, hashtype = SIGHASH_ALL): assert isinstance(tx, dict) i = int(i) newtx = copy.deepcopy(tx) for inp in newtx["ins"]: inp["script"] = b'' s = script.deserialize(prevScript) s = [x for x in s if x is not script.Opcode.OP_CODESEPARATOR] newtx["ins"][i]["script"] = script.serialize(s) if hashtype & 0x1f == SIGHASH_NONE: newtx["outs"] = [] for inp in range(len(newtx['ins'])): if inp != i: newtx['ins'][inp]['sequence'] = 0 elif hashtype & 0x1f == SIGHASH_SINGLE: newtx["outs"] = newtx["outs"][:i+1] for out in range(i): newtx["outs"][out]['value'] = 2**64 - 1 newtx["outs"][out]['script'] = b'' for inp in range(len(newtx['ins'])): if inp != i: newtx['ins'][inp]['sequence'] = 0 if hashtype & SIGHASH_ANYONECANPAY != 0: newtx["ins"] = [newtx["ins"][i]] hashbytes = convert.int_to_bytes_le(hashtype, 4) return hashes.hash256(serialize(newtx) + hashbytes)
def test_deserialize(self): for s in self.scripts: self.assertEqual( script.deserialize(s[0]), s[1] )