An application of blockchain technology to manage electricity flows in a Microgrid (MG) is studied. The grid is considered for civil use, equipped with generation and storage systems, and interfaced with the national grid. It is composed of energy nodes such as user, generator, accumulator, and customs officer node, each characterized by its own time profile, in a limited time interval, of energy flow (consumption, generation, charge/discharge, purchase/sold). The proposed management strategy is based on the principle of free trade managed through blockchain. A simulation tool is developed to evaluate the electricity flows for different scenarios. The blockchain of energy transactions is generated by means of a dedicated software and having as reference a set of ethical/economical rules. It is demonstrated that it is possible to design energy management and exchange systems that allow in an economic way, within the delicate balance between energy availability, technology, market, safety, freedom and needs of individual users, to optimize the exchange of resources and to decrease waste. This blockchain exchange strategy can be implemented without building new plants and avoiding expensive and complex systems, by installing only bidirectional meters equipped with telecommunication, in addition to a common computer management system. The aim of this work is to demonstrate importance and technical feasibility of enforcing rules on the correct use of energy. The peer-to-peer operating mode is different from the technique currently in force in conventional energy systems, in which always a control body (energy, economic, financial) supervises and manages any transaction between users.