def get_scriptcode_for_sighash(self) -> bytes:
        """Reconstructs the scriptcode part of the preimage for OP_CHECKSIG,
        for an already complete txin, in order to *verify the signature*.
        """
        scriptpubkey = self.scriptpubkey
        if scriptpubkey is None:
            raise Exception("missing scriptpubkey. 'utxo' not set?")
        script_type = get_script_type_from_output_script(scriptpubkey)
        if script_type == "p2wsh":
            wit_elems = self.witness_elements()
            if not wit_elems:
                raise Exception(f"missing witness for {script_type=}")
            witness_script = wit_elems[-1]
            if self.address != bitcoin.script_to_p2wsh(witness_script):
                raise Exception("witness_script from witness does not match address")
            if opcodes.OP_CODESEPARATOR in [x[0] for x in script_GetOp(witness_script)]:
                raise Exception('OP_CODESEPARATOR black magic is not supported')
            return witness_script
        elif script_type == "p2wpkh":
            pkh = scriptpubkey[-20:]
            assert len(pkh) == 20
            p2pkh_script = bitcoin.pubkeyhash_to_p2pkh_script(pkh)
            return p2pkh_script
        elif script_type == "p2sh":
            if not self.script_sig:
                raise Exception(f"missing script_sig for {script_type=}")
            parsed_ss = list(script_GetOp(self.script_sig))
            redeem_script = parsed_ss[-1][1]
            if self.address != bitcoin.hash160_to_p2sh(hash_160(redeem_script)):
                raise Exception("redeem_script from script_sig does not match address")
            if self.is_segwit():  # p2sh-wrapped-segwit
                inner_script_type = get_script_type_from_output_script(redeem_script)
                if inner_script_type == "p2wsh":
                    wit_elems = self.witness_elements()
                    witness_script = wit_elems[-1]
                    if redeem_script != bitcoin.p2wsh_nested_script(witness_script):
                        raise Exception("witness_script from witness does not match redeem_script")
                    if opcodes.OP_CODESEPARATOR in [x[0] for x in script_GetOp(witness_script)]:
                        raise Exception('OP_CODESEPARATOR black magic is not supported')
                    return witness_script
                elif inner_script_type == "p2wpkh":
                    pkh = self.script_sig[-20:]
                    assert len(pkh) == 20
                    p2pkh_script = bitcoin.pubkeyhash_to_p2pkh_script(pkh)
                    return p2pkh_script
                else:
                    raise Exception(f"unexpected {inner_script_type=} wrapped in p2sh. script_sig={self.script_sig.hex()}")
            else:
                if opcodes.OP_CODESEPARATOR in [x[0] for x in script_GetOp(redeem_script)]:
                    raise Exception('OP_CODESEPARATOR black magic is not supported')
                return redeem_script
        elif script_type in ("p2pkh", "p2pk"):
            # For "raw scriptpubkey" case, it is usually the scriptPubKey that is signed.
            # Complications for the general case:
            # - signatures should be removed ("FindAndDelete")
            # - OP_CODESEPARATOR black magic
            return scriptpubkey
        else:
            raise Exception(f"cannot handle {script_type=} ({scriptpubkey.hex()=})")
        raise Exception("should not get here")
Reconstructs the scriptcode part of the preimage for OP_CHECKSIG,
        for an already complete txin, in order to *verify the signature*.
        
electrum/transaction.py