Your walnuts were damaged by the walnut husk fly. This fruit fly lays eggs in the green husks. The larvae feed in the husk which causes them to decay and turn black. The husks dry out and are difficult to remove when the nuts are harvested. The husk fly larvae do not feed on the nut meats, but juice from the damaged husks may stain the nut meats.
Unfortunately there is no effective chemical treatment which home gardeners can use to control the walnut husk fly. Most gardeners tolerate the insects’ damage since the nut meats can still be eaten.
Codling moth is another pest of walnuts, but unlike the husk fly larvea, it does feed on the meat of the nut. This pest can be controlled by applying an insecticide spray when the nuts are small to kill the larvae before they enter the nut.
For more information see
UC Pest Note 7430, Walnut Husk Fly: http://www.ipm.ucdavis.edu/PMG/PESTNOTES/pn7430.html
Pest Note 7412, Codling Moth: http://www.ipm.ucdavis.edu/PMG/PESTNOTES/pn7412.html
By V. Lazaneo, Urban Horticulture Advisor, Emeritus, UC Cooperative Extension,