In a step that indicates indictments in the Iran-contra affair are not far off, Independent Counsel Lawrence E. Walsh said yesterday that he has appointed a former federal judge to handle any legal challenges stemming from congressional grants of immunity.

Former U.S. District Court judge Herbert J. Stern will represent Walsh's office in courtroom proceedings "which may challenge a possible prosecution" because of the limited immunity Congress granted to some key figures in the affair, Walsh said.

Stern, now a partner in a New Jersey law firm, spent 13 years as a U.S. District Court judge before resigning last year to take up private practice. He was a career prosecutor before becoming a judge and had been U.S. attorney for New Jersey. Walsh said Stern will not join the staff full time, likening his role to "that of a special counsel."

Sources familiar with the investigation said the appointment is a clear signal that indictments may be returned in a few weeks. Three of the publicly identified targets of Walsh's Iran-contra inquiry -- Lt. Col. Oliver L. North, former White House national security adviser John M. Poindexter and arms dealer Albert Hakim -- were granted immunity from prosecution last year for what they told the Senate-House Iran-contra committees.

If they are indicted, their lawyers are expected to contend that Walsh's evidence must have been tainted by their testimony.

Another target of Walsh's investigation, retired Air Force general Richard V. Secord, testified without immunity. Secord's unusual bid to have an indictment halted pending his being allowed to present evidence to the grand jury was thrown out by U.S. Circuit Court of Appeals here yesterday.

Walsh and most of his staff say they shielded themselves from the immunized testimony and collected their evidence independently.