Calculate the sighash of a non-segwit transaction.

    If it's SIGHASH_NONE, then digest the inputs but no outputs 
    If it's SIGHASH_SINGLE, then digest all inputs and all outputs up to i (excluding values and scripts), and fully digest the ith input and output
    If it's (something) | SIGHASH_ANYONECANPAY, then only digest the ith input.
    
    Return the double-sha256 digest of the relevant fields.

    THIS DOES NOT WORK WITH SEGWIT OUTPUTS

    Adapted from https://github.com/vbuterin/pybitcointools, by Vitalik Buterin

def btc_tx_sighash( tx, idx, script, hashcode=SIGHASH_ALL):
    """
    Calculate the sighash of a non-segwit transaction.

    If it's SIGHASH_NONE, then digest the inputs but no outputs 
    If it's SIGHASH_SINGLE, then digest all inputs and all outputs up to i (excluding values and scripts), and fully digest the ith input and output
    If it's (something) | SIGHASH_ANYONECANPAY, then only digest the ith input.
    
    Return the double-sha256 digest of the relevant fields.

    THIS DOES NOT WORK WITH SEGWIT OUTPUTS

    Adapted from https://github.com/vbuterin/pybitcointools, by Vitalik Buterin
    """

    txobj = btc_tx_deserialize(tx)

    idx = int(idx)
    hashcode = int(hashcode)
    newtx = copy.deepcopy(txobj)

    # remove all scriptsigs in all inputs, except for the ith input's scriptsig.
    # the other inputs will be 'partially signed', except for SIGHASH_ANYONECANPAY mode.
    for i in xrange(0, len(newtx['ins'])):
        newtx['ins'][i]["script"] = ''
        if i == idx:
            if newtx['ins'][i].has_key('witness_script') and newtx['ins'][i]['witness_script']:
                raise ValueError('this method does not handle segwit inputs')

        if newtx['ins'][i].has_key('witness_script'):
            del newtx['ins'][i]['witness_script']

    newtx["ins"][idx]["script"] = script

    if (hashcode & 0x1f) == SIGHASH_NONE:
        # don't care about the outputs with this signature
        newtx["outs"] = []
        for inp in newtx['ins']:
            inp['sequence'] = 0

    elif (hashcode & 0x1f) == SIGHASH_SINGLE:
        # only signing for this input.
        # all outputs after this input will not be signed.
        # all outputs before this input will be partially signed (but not their values or scripts)
        if len(newtx['ins']) > len(newtx['outs']):
            raise ValueError('invalid hash code: {} inputs but {} outputs'.format(len(newtx['ins']), len(newtx['outs'])))

        newtx["outs"] = newtx["outs"][:len(newtx["ins"])]
        for out in newtx["outs"][:len(newtx["ins"]) - 1]:
            out['value'] = 2**64 - 1
            out['script'] = ""

    elif (hashcode & SIGHASH_ANYONECANPAY) != 0:
        # only going to sign this specific input, and nothing else
        newtx["ins"] = [newtx["ins"][idx]]
   
    signing_tx = btc_tx_serialize(newtx)
    sighash = btc_tx_get_hash( signing_tx, hashcode )
    return sighash