# Path: bitcoinpy/lib/core.py
# class CTxOut(object):
#     def __init__(self):
#         self.nValue = -1
#         self.scriptPubKey = b""
#     def deserialize(self, f):
#         self.nValue = struct.unpack(b"<q", f.read(8))[0]
#         self.scriptPubKey = deser_string(f)
#     def serialize(self):
#         r = b""
#         r += struct.pack(b"<q", self.nValue)
#         r += ser_string(self.scriptPubKey)
#         return r
#     def is_valid(self):
#         if not MoneyRange(self.nValue):
#             print("invalid money range")
#             return False
#         script = CScript()
#         if not script.tokenize(self.scriptPubKey):
#             print(self.scriptPubKey)
#             print("script invalid")
#             return False
#         return True
#     def copy(self, old_txout):
#         self.nValue = old_txout.nValue
#         self.scriptPubKey = old_txout.scriptPubKey
#     def __repr__(self):
#         return "CTxOut(nValue=%i.%08i scriptPubKey=%s)" % (self.nValue // 100000000, self.nValue % 100000000, binascii.hexlify(self.scriptPubKey))
# 
# class CTransaction(object):
#     def __init__(self):
#         # serialized
#         self.nVersion = 1
#         self.vin = []
#         self.vout = []
#         self.nLockTime = 0
# 
#         # used at runtime
#         self.sha256 = None
#         self.nFeesPaid = 0
#         self.dFeePerKB = None
#         self.dPriority = None
#         self.ser_size = 0
#     def deserialize(self, f):
#         self.nVersion = struct.unpack(b"<i", f.read(4))[0]
#         self.vin = deser_vector(f, CTxIn)
#         self.vout = deser_vector(f, CTxOut)
#         self.nLockTime = struct.unpack(b"<I", f.read(4))[0]
#     def serialize(self):
#         r = b""
#         r += struct.pack(b"<i", self.nVersion)
#         r += ser_vector(self.vin)
#         r += ser_vector(self.vout)
#         r += struct.pack(b"<I", self.nLockTime)
#         return r
#     def calc_sha256(self):
#         if self.sha256 is None:
#             self.sha256 = Hash(self.serialize())
#     def is_valid(self):
#         self.calc_sha256()
#         if not self.is_coinbase():
#             for tin in self.vin:
#                 if not tin.is_valid():
#                     print("tin is invalid >>>>>>>>>>>")
#                     return False
#         for tout in self.vout:
#             if not tout.is_valid():
#                 print("tout is invalid >>>>>>>>>>>>>>")
#                 return False
#         return True
#     def is_final(self):
#         for tin in self.vin:
#             if not tin.is_final():
#                 return False
#         return True
#     def is_coinbase(self):
#         #print(len(self.vin))
#         #print(self.vin[0].prevout.is_null())
#         return len(self.vin) == 1 and self.vin[0].prevout.is_null()
# 
#     def copy(self, old_tx):
#         self.nVersion = old_tx.nVersion
#         self.vin = []
#         self.vout = []
#         self.nLockTime = old_tx.nLockTime
#         self.sha256 = None
# 
#         for old_txin in old_tx.vin:
#             txin = CTxIn()
#             txin.copy(old_txin)
#             self.vin.append(txin)
# 
#         for old_txout in old_tx.vout:
#             txout = CTxOut()
#             txout.copy(old_txout)
#             self.vout.append(txout)
# 
#     def __repr__(self):
#         return "CTransaction(nVersion=%i vin=%s vout=%s nLockTime=%i)" % (self.nVersion, repr(self.vin), repr(self.vout), self.nLockTime)


import sys
import hashlib
from bitcoinpy.lib.serialize import Hash, Hash160, ser_uint256, ser_uint160
from bitcoinpy.lib.script import *
from bitcoinpy.lib.core import CTxOut, CTransaction
from bitcoinpy.lib.key import CKey
from bitcoinpy.lib.bignum import bn2vch, vch2bn
# scripteval.py
#
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from __future__ import absolute_import, division, print_function, unicode_literals

if sys.version > '3':
    long = int


def SignatureHash(script, txTo, inIdx, hashtype):
    if inIdx >= len(txTo.vin):
        return (1, "inIdx %d out of range (%d)" % (inIdx, len(txTo.vin)))

# Path: bitcoinpy/lib/core.py
# class CTxOut(object):
#     def __init__(self):
#         self.nValue = -1
#         self.scriptPubKey = b""
#     def deserialize(self, f):
#         self.nValue = struct.unpack(b"<q", f.read(8))[0]
#         self.scriptPubKey = deser_string(f)
#     def serialize(self):
#         r = b""
#         r += struct.pack(b"<q", self.nValue)
#         r += ser_string(self.scriptPubKey)
#         return r
#     def is_valid(self):
#         if not MoneyRange(self.nValue):
#             print("invalid money range")
#             return False
#         script = CScript()
#         if not script.tokenize(self.scriptPubKey):
#             print(self.scriptPubKey)
#             print("script invalid")
#             return False
#         return True
#     def copy(self, old_txout):
#         self.nValue = old_txout.nValue
#         self.scriptPubKey = old_txout.scriptPubKey
#     def __repr__(self):
#         return "CTxOut(nValue=%i.%08i scriptPubKey=%s)" % (self.nValue // 100000000, self.nValue % 100000000, binascii.hexlify(self.scriptPubKey))
# 
# class CTransaction(object):
#     def __init__(self):
#         # serialized
#         self.nVersion = 1
#         self.vin = []
#         self.vout = []
#         self.nLockTime = 0
# 
#         # used at runtime
#         self.sha256 = None
#         self.nFeesPaid = 0
#         self.dFeePerKB = None
#         self.dPriority = None
#         self.ser_size = 0
#     def deserialize(self, f):
#         self.nVersion = struct.unpack(b"<i", f.read(4))[0]
#         self.vin = deser_vector(f, CTxIn)
#         self.vout = deser_vector(f, CTxOut)
#         self.nLockTime = struct.unpack(b"<I", f.read(4))[0]
#     def serialize(self):
#         r = b""
#         r += struct.pack(b"<i", self.nVersion)
#         r += ser_vector(self.vin)
#         r += ser_vector(self.vout)
#         r += struct.pack(b"<I", self.nLockTime)
#         return r
#     def calc_sha256(self):
#         if self.sha256 is None:
#             self.sha256 = Hash(self.serialize())
#     def is_valid(self):
#         self.calc_sha256()
#         if not self.is_coinbase():
#             for tin in self.vin:
#                 if not tin.is_valid():
#                     print("tin is invalid >>>>>>>>>>>")
#                     return False
#         for tout in self.vout:
#             if not tout.is_valid():
#                 print("tout is invalid >>>>>>>>>>>>>>")
#                 return False
#         return True
#     def is_final(self):
#         for tin in self.vin:
#             if not tin.is_final():
#                 return False
#         return True
#     def is_coinbase(self):
#         #print(len(self.vin))
#         #print(self.vin[0].prevout.is_null())
#         return len(self.vin) == 1 and self.vin[0].prevout.is_null()
# 
#     def copy(self, old_tx):
#         self.nVersion = old_tx.nVersion
#         self.vin = []
#         self.vout = []
#         self.nLockTime = old_tx.nLockTime
#         self.sha256 = None
# 
#         for old_txin in old_tx.vin:
#             txin = CTxIn()
#             txin.copy(old_txin)
#             self.vin.append(txin)
# 
#         for old_txout in old_tx.vout:
#             txout = CTxOut()
#             txout.copy(old_txout)
#             self.vout.append(txout)
# 
#     def __repr__(self):
#         return "CTransaction(nVersion=%i vin=%s vout=%s nLockTime=%i)" % (self.nVersion, repr(self.vin), repr(self.vout), self.nLockTime)


# Path: bitcoinpy/lib/scripteval.py
import sys
import hashlib
from bitcoinpy.lib.serialize import Hash, Hash160, ser_uint256, ser_uint160
from bitcoinpy.lib.script import *
from bitcoinpy.lib.core import CTxOut, CTransaction
from bitcoinpy.lib.key import CKey
from bitcoinpy.lib.bignum import bn2vch, vch2bn

# scripteval.py
#
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from __future__ import absolute_import, division, print_function, unicode_literals

if sys.version > '3':
    long = int


def SignatureHash(script, txTo, inIdx, hashtype):
    if inIdx >= len(txTo.vin):
        return (1, "inIdx %d out of range (%d)" % (inIdx, len(txTo.vin)))

    txtmp = CTransaction()