A theory for the origin of a self-replicating chemical system