This paper proposes bilateral contract networks as a new scalable market design for peer-to-peer energy trading. Coordinating small-scale distributed energy resources to shape overall demand could offer significant value to power systems, by alleviating the need for investments in upstream generation and transmission infrastructure, increasing network efficiency and increasing energy security. However, incentivising coordination between the owners of large-scale and small-scale energy resources at different levels of the power system remains an unsolved challenge. This paper introduces real-time and forward markets, consisting of energy contracts offered between generators with fuel-based sources, suppliers acting as intermediaries and consumers with inflexible loads, time-coupled flexible loads and/or renewable sources. For each type of agent, utility-maximising preferences for real-time contracts and forward contracts are derived. It is shown that these preferences satisfy full substitutability conditions essential for establishing the existence of a stable outcome-an agreed network of contracts specifying energy trades and prices, which agents do not wish to mutually deviate from. Important characteristics of energy trading are incorporated, including upstream-downstream energy balance and forward market uncertainty. Full substitutability ensures a distributed price-adjustment process can be used, which only requires local agent decisions and agent-to-agent communication between trading partners.