def control_block_for_taproot_script_spend(
    *, internal_pubkey: bytes, script_tree: TapTree, script_num: int,
) -> Tuple[bytes, bytes]:
    """Constructs the control block necessary for spending a taproot UTXO using a script.
    script_num indicates which script to use, which indexes into (flattened) script_tree.
    """
    assert isinstance(internal_pubkey, bytes), type(internal_pubkey)
    assert len(internal_pubkey) == 32, len(internal_pubkey)
    info, merkle_root = taproot_tree_helper(script_tree)
    (leaf_version, leaf_script), merkle_path = info[script_num]
    output_pubkey_y_parity, _ = taproot_tweak_pubkey(internal_pubkey, merkle_root)
    pubkey_data = bytes([output_pubkey_y_parity + leaf_version]) + internal_pubkey
    control_block = pubkey_data + merkle_path
    return (leaf_script, control_block)
Constructs the control block necessary for spending a taproot UTXO using a script.
    script_num indicates which script to use, which indexes into (flattened) script_tree.
    
electrum/bitcoin.py