In my campaign setting gunpowder comes from two sources - the old fashioned way, which doesn't make very good gunpowder, and tends to be expensive, or a form of semi stabilised Elemental matter, taken directly from the Elemental Chaos and Elemental planes.
Thanks to an easily mastered ritual, and certain machineries, any rift to the element planes can be used to draw out the essence, which is stabilized into a powder, that can be detonated with a simple percussion hammer. Once put into a cartridge, it's said to be perfectly safe. Several Artificers have discovered ways to mass produce the stuff, as well as play around with it composition to create Elementally Infused ammunition. This definitely isn't perfectly safe.
It does have a few major drawbacks, the process leaves large areas of grey, tapped, lifeless matter in the Elemental Planes/Chaos. Something the inhabitants really are not pleased about. The process also isn't as safe as it could be, with several of the Factories that produce the stuff exploding from time to time. Still can't let things like that stand in the way of progress.