In the transition period in Colorado (cannabis is likely to no longer be a Schedule I Controlled Substance as soon as sometime later this year), federal courts have tended to apply illegality bars to cannabis contracts, while state courts have generally honored them as legal and valid, disregarding the federal law illegality (which the federal government has formally tolerated).
I know this from practicing in this area in Colorado in this time period in this area of law.
One consequence of this is that marijuana industry contracts routinely waive a federal court forum contractually and marijuana businesses functionally have no access to the bankruptcy courts or to enforceable federal intellectual property rights like patents and Lanham Act registered trademarks. Likewise, when participants in the marijuana industry litigate against each other, they generally do not allege federal law claims or counterclaims (e.g., federal securities fraud under SEC regulations 10b-5).
How exactly does an illegal purpose void a contract? Is a criminal
prosecution required?
No. Illegal purpose determinations are made in the context of civil lawsuits to enforce contracts or civil declaratory judgment cases seeking to disavow them.
Does the jurisdiction matter?
Yes. The illegal purpose doctrine is a matter of state law so it can vary from jurisdiction to jurisdiction. Also, as noted above, in practice, federal courts and state courts have not seen eye to eye on the matter.
Does the "illegal purpose" require one to demonstrate "mens rea" in
some way that would not be required otherwise?
If the contract is not facially illegal, e.g. "deliver a box to 4567 Fat Street, Pudunk, California 99999", and the party performing the contract doesn't know that the facts that cause the contract to involve illegal transporting controlled substances, it is probably legal and enforceable by the delivery firm.
But, if the contract was to "deliver a box of cocaine to 4567 Fat Street, Pudunk, California 99999" and the party performing the contract doesn't know that cocaine is illegal, this ignorance of the law probably doesn't make the contract enforceable.