Make bone roll match normal facing?

I am working on a script that generates bones down an edge loop. I have figured out how to create a bone and move its head and tail into position at the top and bottom of a face, or onto a specific vertex. The problem now is how to roll the bone so that it is flat on the face normal (or if on an edge loop, so that it based on the vertex normals.)

Is there some existing way to do this, or any existing addons/scripts that handle this problem? Or do I have to somehow derive the bone’s angle?