When Michael D. Cohen stood before a federal judge to ask for leniency he attributed much of his behavior to the influence of one man: Donald J. Trump.

“Time and time again,” Mr. Cohen told the judge at his sentencing in late 2018, “I felt it was my duty to cover up his dirty deeds.”

Ever since, Mr. Cohen has made it his work to expose those deeds. He testified for roughly seven hours at a Congressional hearing in 2019, describing Mr. Trump as a liar and a cheater who made racist remarks. Mr. Cohen also met with the special counsel Robert S. Mueller II’s investigators and federal prosecutors in New York. He was the impetus for the New York Attorney General’s investigation into Mr. Trump’s business practices, laying the groundwork for a lawsuit that accused the former president of inflating his net worth by billions of dollars.

Mr. Cohen’s transformation from trusted fixer to chief antagonist — a 180-degree turn against a man he once vowed to take a bullet for — upended his life. He went to prison for 13 months and then faced home confinement for more than a year. He endured years of attacks from Mr. Trump’s allies, ultimately emerging with a book deal, cable news appearances and a podcast, “Mea Culpa.”

Now, Mr. Cohen is poised to seize his biggest moment yet: a day in court against Mr. Trump.

Mr. Cohen is the key witness in the Manhattan district attorney’s investigation into a hush-money payment to a porn star named Stormy Daniels. The payment, which Mr. Cohen said he made at Mr. Trump’s direction during the final days of the 2016 presidential campaign, blocked Ms. Daniels from telling her story of an affair with Mr. Trump years earlier.

Mr. Cohen has met with the prosecutors some 20 times and recently testified before a grand jury that could indict Mr. Trump as soon as this week, people with knowledge of the matter said. And he has provided documentation that bolsters his testimony, the people added.

Mr. Trump has denied having any sexual encounter with Ms. Daniels and accused the district attorney, Alvin L. Bragg, a Democrat, of carrying out a political “witch hunt” against him.

Mr. Trump’s team and Mr. Cohen’s critics maintain he is playing a cynical game based on rescuing his reputation and capitalizing on his guilty plea. But his supporters — in Congress, in the Democratic Party and on his expansive social media presence — credit him with a high-risk decision to challenge a president, and force the first significant cracks in Mr. Trump’s edifice.

This account of the long, strange and now historically consequential arc of Mr. Trump’s once-loyal lawyer and fixer is drawn from interviews with nearly a dozen people who know him, and records from his various legal entanglements. Collectively, they paint a portrait of a complicated witness — a convicted liar and an opportunist, but also a compelling presence, who notes that his lies were on Mr. Trump’s behalf, and whose emotional vulnerability and blunt recitation of history prosecutors may rely on to charm a jury.

“I know there’s a debate about the utilization of Michael as a witness, and that is going to be a colorful cross-examination,” said Norman Eisen, who served as the counsel for House Democrats during the first impeachment inquiry and developed a relationship with Mr. Cohen over the course of multiple meetings.

“In dealing with me, he has never varied from our first meeting in 2019 to today in the details of what happened both in the hush-money and in the larger financial frauds.”

Mr. Cohen, the son of a Holocaust survivor, was a 2003 New York City Council candidate and a mega-fan of Mr. Trump’s public persona before going to work for him. He got the job after impressing Mr. Trump, defending him at a condo board meeting at a Trump building in 2006.

And he endeared himself to Mr. Trump by trying to be an indispensable aide and pit bull adviser to a real-estate developer and reality-television star.

Part of his role became anticipating Mr. Trump’s whims and desires, and interpreting directions spoken in what Mr. Cohen would later describe as “code.”

Mr. Trump had a penchant for compartmentalizing his life. When one of Mr. Trump’s friends asked Mr. Trump why he kept Mr. Cohen around, Mr. Trump replied, “He has his purpose.”

That purpose, he later said, included cleaning up some of Mr. Trump’s messes. In October 2016, while visiting his daughter in London, Mr. Cohen received a call from the editor of The National Enquirer, which had forged close ties to Mr. Trump over the years. The editor warned that Ms. Daniels was looking to sell her story.

Within days, Mr. Cohen hammered out the hush money deal with Ms. Daniels’s lawyer, securing Ms. Daniels’s silence at a crucial moment for the campaign.

When Mr. Trump won the presidency soon after, Mr. Cohen did not accompany him to Washington, and left behind full-time employment at the Trump Organization to set up an office at the law firm Squire Patton Boggs in Midtown Manhattan.

The Trump presidency was shaping up to be lucrative for Mr. Cohen: He soon had a roster of corporate clients, including a private equity firm, a large pharmaceutical company and even AT&T, as he held himself out as the personal lawyer to the president.

But one issue trailed him: a complaint had been filed with the Federal Election Commission by the good-government group Common Cause about his payment to Ms. Daniels, which was publicly revealed in January 2018 by The Wall Street Journal.

Soon, Mr. Cohen acknowledged to the F.E.C. and The New York Times that he had made the payment, insisting he did it on his own and that neither the Trump Organization nor the Trump campaign had been a party to it. But he would not say whether Mr. Trump had been aware of the payment.

At that time in Washington, Mr. Mueller’s investigation into whether Mr. Trump’s campaign had conspired with Russians in 2016, and whether Mr. Trump had obstructed justice, was proceeding apace. So were congressional investigations into Mr. Trump’s connections to Russia.

Mr. Cohen testified to Congress that discussions about a Trump Tower project in Moscow stopped in January 2016. That turned out to be a lie, for which he would later fault Mr. Trump; the discussions went on until June 2016, into the presidential campaign.

Mr. Mueller’s team was also scrutinizing Mr. Cohen, including for the hush money deal, but soon handed off that inquiry to federal prosecutors in the Southern District of New York.

The inquiry came to a head in April 2018, when F.B.I. agents searched Mr. Cohen’s office, home and a hotel where his family stayed while repair work went on at their apartment, taking emails, business records and other material. The event went off like a political bomb: The personal lawyer for a sitting president was the subject of an F.B.I. search based on probable cause that a crime was committed.

It also imploded Mr. Cohen’s life. He confided in friends at the time that he was suicidal.

As the search garnered wall-to-wall news coverage, Mr. Cohen received a call from Mr. Trump at the White House, with a message: stay strong.

But as Mr. Cohen’s legal bills piled up, officials at the family-run Trump Organization began to balk at paying his lawyer, planting the seeds for Mr. Cohen’s break from a man he once idolized.

Within months, the fracture between Mr. Trump and Mr. Cohen was clear.

Mr. Cohen soon hired Lanny Davis, a Democrat and a veteran Washington lawyer who worked in Bill Clinton’s White House.

Mr. Davis had seen Mr. Cohen on television and reached out to Stephen Ryan, Mr. Cohen’s lawyer at the time. Soon, Mr. Davis and Mr. Cohen were virtually inseparable.

In August of 2018, the federal prosecutors in the Southern District readied charges against Mr. Cohen for the hush money and a range of unrelated financial crimes. Mr. Davis said the prosecutors threatened to charge Mr. Cohen’s wife, Laura, with the tax crimes as well.

Mr. Cohen pleaded guilty in that case, and later, in another case brought by Mr. Mueller related to his congressional testimony about the potential Trump hotel deal in Moscow.

At his first plea hearing, on the hush money payment, Mr. Cohen pointed the finger at Mr. Trump, who he said directed him to pay it, an accusation that prosecutors later substantiated.

Mr. Cohen was sentenced to three years in prison.

Mr. Davis told Mr. Cohen that he had a path to winning back his credibility, but it wasn’t going to be enough to simply say he was sorry for what he had done. He would have to fully come clean about Mr. Trump, Mr. Davis said. Mr. Cohen told Mr. Davis he was ready.

They directed their effort at congressional Democrats, who were heading into their third year of investigations into Mr. Trump.

In February 2019, Democrats announced that Mr. Cohen would appear at an unusual public hearing, the sole witness discussing the 45th president.

Even before he arrived, Mr. Trump’s allies tried to intimidate him. Representative Matt Gaetz, a Republican from Florida, posted on Twitter an accusation that Mr. Cohen had been unfaithful to his wife — and she might not be loyal while he was in prison. Two of Mr. Trump’s closest allies, Representatives Jim Jordan of Ohio and Mark Meadows of North Carolina, wrote a joint op-ed attacking Mr. Cohen as a “liar.”

But when Mr. Cohen assumed a seat at a witness table for what would become a day-long event, he appeared prepared for the onslaught. He fought back, potentially foreshadowing how he might respond to attacks from Mr. Trump’s lawyers on the witness stand in the Manhattan case.

“By coming today, I have caused my family to be the target of personal, scurrilous attacks by the president and his lawyer trying to intimidate me from appearing before this panel,” Mr. Cohen said in opening remarks at the congressional hearing. “Mr. Trump called me a rat for choosing to tell the truth, much like a mobster would do when one of his men decides to cooperate with the government.”

As Mr. Jordan tried to rattle him, Mr. Cohen replied sternly, “Shame on you.”

And Mr. Cohen delivered a striking prediction about what might happen the following year: “Given my experience working for Mr. Trump, I fear that if he loses the election in 2020, there will never be a peaceful transition of power,” he said.

Representative Elijah Cummings, Democrat from Baltimore and the committee chair, who knew Mr. Davis and had invited Mr. Cohen, told him “I know that you are worried about your family, but this is a part of your destiny.”

In May 2019, Mr. Cohen began serving his time at a minimum security facility at Otisville, N.Y. It was there that he began to meet with the Manhattan district attorney’s office.

Mr. Cohen was released in May 2020 on a medical furlough. But he was soon thrown back in prison by the Trump administration’s Bureau of Prisons, after he refused to sign a document stating he would not write a book, something he was doing.

About two weeks later, a judge ordered him released, saying the move was “retaliatory.” He has told friends that he spent 51 days overall in solitary confinement.

By early 2022, Mr. Cohen was home from prison and his visits with prosecutors moved to their offices in Lower Manhattan. Beginning in January of this year, he seemed to visit almost weekly, staging impromptu news conferences outside to tell reporters that his former boss was in trouble.

Mr. Cohen is hardly a perfect witness. Mr. Trump’s lawyers will undoubtedly attack his character and invoke his criminal record. Some appear eager to cross-examine him.

This week, at the request of Mr. Trump’s lawyers, one of Mr. Cohen’s former legal advisers testified before the grand jury in hopes of undercutting Mr. Cohen’s credibility. The witness, Robert J. Costello, briefly advised Mr. Cohen when he was facing the federal investigation in 2018, but they had a falling out as Mr. Cohen began taking public swipes at Mr. Trump.

Mr. Costello, who was close with Mr. Trump’s personal lawyer at the time, Rudolph W. Giuliani, said he told the grand jury that Mr. Cohen was a liar. Mr. Cohen, in turn, said on MSNBC that Mr. Costello “lacks for any sense of veracity.”

His cable news appearances, in which he makes off-the-cuff remarks about Mr. Trump and the investigation, have become quite frequent. Even the prosecutors who are relying on Mr. Cohen — and have decided to stake a large part of their case on his testimony — occasionally shake their heads at his media presence, according to a person close to the case.

But Mr. Cohen, who has said he feels the need to defend himself publicly, has largely won the at least qualified approval of the district attorney’s office. In his book, Mark F. Pomerantz, the prosecutor who helped lead the investigation until early 2022, wrote that Mr. Cohen had impressed him as “smart but manipulative.

“He struck me as a somewhat feral creature,” Mr. Pomerantz continued. “Most importantly, I thought he was telling the truth.”

Mr. Pomerantz argued that Mr. Cohen would play well with jurors, and that his anger at Mr. Trump could be explained: “He was angry with Trump because Trump had seduced Cohen into his criminal orbit, and Cohen had been the only one of Trump’s enablers to have gone to prison. Cohen was angry with himself for allowing himself to be seduced by Trump.”

Mr. Cohen’s comprehensive knowledge of the hush-money case is likely another draw for prosecutors. The former fixer could connect all the dots that led to the payment. He liaised with each witness, and with Mr. Trump himself.

On the first day of his grand jury testimony this month, when Mr. Cohen stopped outside the courthouse to entertain questions from reporters, he harkened back to what he had told the judge five years earlier.

“This is all about accountability,” he said. “He needs to be held accountable for his dirty deeds.”