By THOMAS KAPLAN

April 9, 2013

On a drizzly Monday in Albany, Nelson L. Castro, in sharp red tie, rose on the floor of the State Assembly to welcome a delegation of fellow Dominican-Americans visiting from New York City. The 199th anniversary of the birth of Juan Pablo Duarte, a founding father of the Dominican Republic, was approaching, and Mr. Castro had prepared a resolution to honor him.

Four days later, after traveling back to the Bronx, Mr. Castro met with two businessmen who had enlisted his help in their efforts to open an adult day care center in the borough. One of the men handed Mr. Castro three manila envelopes stuffed with $12,000 in cash and explained, according to a criminal complaint, “Consider this a contribution.”

Unbeknown to the men, Mr. Castro was recording the conversation. Since 2009, just nine months after he was elected to the Assembly, he had led a double life: simultaneously representing the West Bronx in the Legislature, and informing on his colleagues to state and federal prosecutors investigating public corruption.

On Thursday, Mr. Castro abruptly said he would resign under a deal to avoid prosecution himself, after the United States attorney in Manhattan announced that his cooperation had led to bribery charges against Assemblyman Eric A. Stevenson, a fellow Democrat whose district adjoins that of Mr. Castro, as well as four other men.

“He was always very friendly,” Assemblyman Jeffrey Dinowitz, also a Bronx Democrat, said of Mr. Castro. “Now I know why.”

The Bronx meeting was one of several instances in January 2012 in which Mr. Castro appeared to shift seamlessly from his public legislative duties — attending sessions, drafting bills, responding to requests from constituents — and his covert work for prosecutors, who had equipped him with audio and video recorders.

Early that month, he joined his colleagues in Albany for the State of the State address; back in the Bronx two days later, at the instruction of federal law enforcement, he recorded himself exacting a pledge of “special treatment” from the two businessmen.

Then, a few weeks later, while more than two dozen of his colleagues gathered at the Legislative Office Building in Albany for a state budget hearing about work force issues, Mr. Castro met in the Bronx with an associate of the businessmen. It was Mr. Castro’s 40th birthday, and the man assured him that he would soon receive “a nice birthday gift” for his assistance with the day care center. Mr. Castro recorded that interaction, too.

Lawmakers are transfixed — and, in some corners, alarmed — by a situation that seems more Hollywood than Albany: a legislator working for years as a law enforcement mole, all the while introducing bills and casting votes like any other legislator. And the United States attorney in Manhattan, Preet Bharara, suggested Mr. Castro might not have been alone.

“If you are a corrupt official in New York,” he said, “you have to worry that one of your colleagues is working with us.”

Mr. Castro himself made it clear that Mr. Stevenson might not be the only person affected by his informing. In a statement issued by his lawyer, he said that he had worked with prosecutors on “various investigations” and that the charges against Mr. Stevenson were “one result of this cooperation.” He added, “I continue to cooperate with state and federal authorities in this prosecution and in other investigations.”

The idea of a mole in their midst struck some lawmakers as a plot from a movie — “a bad movie,” Assemblyman Luis R. Sepúlveda, a Bronx Democrat, put it.

“People who would have been cracking jokes with him, or making off-color comments, they didn’t expect him to be wiretapped,” Mr. Sepúlveda said.

Mr. Castro, 41, agreed to work with prosecutors after he was secretly indicted on three counts of perjury in the summer of 2009; the government claimed that in 2008, before he was elected, he lied during testimony he gave under oath during a lawsuit brought by a political rival who had accused him of election law violations. Almost immediately after he was notified of the sealed indictment, in August 2009, he began cooperating with the Bronx district attorney’s office; in 2011 he began working with the United States Attorney’s Office for the Southern District of New York.

Mr. Castro’s lawyer, Michael C. Farkas, said the assemblyman decided to cooperate with the government because he thought “he could do some good and make some amends.”

Nelson L. Castro

Librado Romero / The New York Times

“Say what you will about his misdeeds, he cared — and still cares — about the people served by the government,” Mr. Farkas said.

Mr. Farkas said that, while he could not discuss specifics of his client’s efforts for prosecutors, he understood that Mr. Castro’s activities, when he was recording, were generally focused on “specific missions.” He added that neither the prosecutors in the Bronx nor those in Mr. Bharara’s office had any influence on Mr. Castro’s legitimate activities as an assemblyman.

“I’m sure that no one is going to have much sympathy for Nelson Castro, but you better believe that the last few years have been very stressful for him,” Mr. Farkas said. “It was the ever-present sword of Damocles.”

Mr. Castro has not said much publicly, but on Saturday he posted on his Facebook page to thank his friends for their support. “Believe me that I have had better days,” he wrote in Spanish, adding, “Eventually, when everything can be said about the things that have happened, I will speak and I will explain my reality.”

Mr. Castro’s cooperation was apparently valuable enough that prosecutors kept from the public the fact that he had been indicted, allowing him twice to successfully run for re-election, in 2010 and 2012. That fact disturbed some in his district, like Francisco Santos, 45, a mover, who said that the revelation that Mr. Castro was an informer had made him re-evaluate his assemblyman.

“He’s supposed to be helping the community, but I think he was too busy doing the wire thing,” Mr. Santos said. He added, “I feel like he wasn’t doing his job for four years because you don’t have time to be in the Assembly if you’re going around trying to get indictments on other people.”

At the Capitol, Mr. Castro seemed like any other backbencher. He traveled weekly to Albany from his home in the Bronx, spending the night at a Holiday Inn Express. At night, he was a regular presence in the handful of bars frequented by lawmakers and their staff, often chatting about women, and at karaoke Wednesdays at the Pinto and Hobbs Tavern, where one night last spring he joined in a rendition of “La Bamba.”

He arrived in Albany with some baggage. He had a minor criminal record before running for office. After he was sworn in, The Daily News had a field day with his thousands of dollars in unpaid parking tickets.

But he pressed on, becoming involved with legislative causes like expanding taxi access in the boroughs outside Manhattan and helping immigrants overcome language barriers. He even scored one big victory, persuading the Legislature to pass a so-called Nutcracker Bill, increasing penalties for barbershops and bodegas selling sweet homemade cocktails to minors. He was a delegate to the Democratic National Convention last year, and this year, he even won a leadership position in the Assembly, a coup for a young lawmaker — the speaker, Sheldon Silver, named him in January as the chairman of the Assembly’s Task Force on New Americans.

News of Mr. Castro’s undercover work was unexpected in the Bronx, where he represented the neighborhoods of University Heights, Fordham and Tremont.

Mr. Castro’s chief of staff, Angelica Pascacio, said she had learned of her boss’s double life only on Thursday — after Mr. Stevenson was arrested and reporters began calling.

“We didn’t know anything,” she said.

And Jorge Herrera, 32, who volunteers at Mr. Castro’s office, said he had taken Mr. Castro’s clothes to the dry cleaners and helped him put on jackets and never saw a recording device. “I just don’t believe it,” he said. “I’ve never seen him nervous.”

Bernice Williams, the Democratic state committeewoman from Mr. Castro’s Assembly district, said that, in hindsight, she thought Mr. Castro had started to act differently in recent months.

“He wasn’t getting things done, to me, as I thought he could,” she said. “So that was odd to me. He was a little more — I don’t know, something changed. He was moving a little bit slower than normal.”

Nonetheless, she was stunned by the cinematic turn of events, which, she said, reminded her of “gangster movies.”

“That really took me for a loop,” she said, adding, “He never seemed like a spy.”