The law and the good, in European languages, are associated with straight lines; the bad with the crooked. Think about the word crooked itself, literally! Or about right/rectangle, or the Greek ortho- for straight, hence, orthodox as well as orthodontics.
This is why it makes sense that Derecho — Spanish for straight and also for law — comes from the same Latin root that gives us direct.
The “ct” in the original direct turned into a “ch” in Spanish, in the usual pattern of “ct” turning into “ch” as Latin grew into Spanish.