NOT based on a militia but on British Common Law that provided for a citizen's right to self defense and hunting as well as the DUTY to provide for the common defense.
You might want to read Tucker's Blackstone which was his commentaries on Law and used as THE definitive text even by the Supreme Court and still is today.
I hope you will bookmark that link because it is not something one will sit down and read in a matter of minutes or even days. I am providing the following link from NRA-ILA that points out some of the specific particulars and with the link to the commentaries EVERY SPECIFIC can easily be verified (in other words if you don't happen to believe what the NRA is saying you can look it up in the original source). This is a relatively short read but do read the whole thing past the list of Treatises addressing the Second Amendment as it is AFTER that list that Tucker is mostly quoted.
The following is quoted from the link above -
Tucker`s American Blackstone reprinted Blackstone`s commentary on what Blackstone called the five "auxiliary rights of the subject." These were rights (such as the right to seek legal redress in court, and the right to petition) whose main purpose was to safeguard primary rights (personal liberty, personal security, and property). Blackstone had written:
"The fifth and last auxiliary right of the subject, that I shall at present mention, is that of having arms for their defense suitable to their condition and degree, and such as are allowed by law. Which is also declared by the same statute 1 W. & M. st. 2 c. 2, and its indeed, a public allowance under due restrictions, of the natural rights of resistance and self-preservation, when the sanctions of society and laws are found insufficient to restrain the violence of oppression."
Blackstone was explaining the 1689 English Bill of Rights, which provided: "That the Subjects which are Protestants may have Arms for their Defense suitable to their Conditions, and as allowed by Law."
Tucker added his own analysis in two footnotes:
"The right of the people to keep and bear arms shall not be infringed. Amendments to the C.U.S. Art. 4 and this without any qualification as to their condition or degree, as is the case in the British government."
In other words, the American right to arms (like the American right to freedom of the press) was broader than its British counterpart; the American right contained none of the limitations expressed in the British right. (Tucker cited what we today call "the Second Amendment" as "article 4" because the Bill of Rights, as originally proposed to states, contained two amendments which were not immediately ratified by the states, while the latter ten amendments were speedily ratified. Thus, the modern First Amendment was originally proposed as the third amendment.)
Tucker`s other footnote explained: "Whoever examines the forest and game laws in the British code, will readily perceive that the right of keeping arms is effectually taken away from the people of England. The commentator himself [Blackstone] informs us, Vol. II, p. 412, that the prevention of popular insurrections and resistance [sic] to government by disarming the bulk of the people, is a reason oftener meant than avowed by the makers of the forest and game laws.`"
"In America we may reasonably hope that the people will never cease to regard the right of keeping and bearing arms as the surest pledge of their liberty."
--St. George Tucker