When Joseph had been taken down to Egypt, Potiphar, Pharaoh's chief officer, the commander of the royal guard and an Egyptian, purchased him from the Ishmaelites who had brought him down there.
References for Genesis 39:1
The LORD was with Joseph, and he became a successful man and served in his Egyptian master's household.
His master saw that the LORD was with him and that the LORD made everything he did successful.
Potiphar thought highly of Joseph, and Joseph became his assistant; he appointed Joseph head of his household and put everything he had under Joseph's supervision.
From the time he appointed Joseph head of his household and of everything he had, the LORD blessed the Egyptian's household because of Joseph. The LORD blessed everything he had, both in the household and in the field.
So he handed over everything he had to Joseph and didn't pay attention to anything except the food he ate. Now Joseph was well-built and handsome.
Some time later, his master's wife became attracted to Joseph and said, "Sleep with me."
He refused and said to his master's wife, "With me here, my master doesn't pay attention to anything in his household; he's put everything he has under my supervision.
No one is greater than I am in this household, and he hasn't denied me anything except you, since you are his wife. How could I do this terrible thing and sin against God?"
Every single day she tried to convince him, but he wouldn't agree to sleep with her or even to be with her.
One day when Joseph arrived at the house to do his work, none of the household's men were there.
She grabbed his garment, saying, "Lie down with me." But he left his garment in her hands and ran outside.
When she realized that he had left his garment in her hands and run outside,
she summoned the men of her house and said to them, "Look, my husband brought us a Hebrew to ridicule us. He came to me to lie down with me, but I screamed.
When he heard me raise my voice and scream, he left his garment with me and ran outside."
She kept his garment with her until Joseph's master came home,
and she told him the same thing: "The Hebrew slave whom you brought to us, to ridicule me, came to me;
but when I raised my voice and screamed, he left his garment with me and ran outside."
When Joseph's master heard the thing that his wife told him, "This is what your servant did to me," he was incensed.
Joseph's master took him and threw him in jail, the place where the king's prisoners were held. While he was in jail,
the LORD was with Joseph and remained loyal to him. He caused the jail's commander to think highly of Joseph.
The jail's commander put all of the prisoners in the jail under Joseph's supervision, and he was the one who determined everything that happened there.
The jail's commander paid no attention to anything under Joseph's supervision, because the LORD was with him and made everything he did successful.