-- | The 'NetworkId' type and related functions
--
module Cardano.Api.NetworkId (
    -- * Network types
    NetworkId(..),
    NetworkMagic(..),
    toNetworkMagic,
    mainnetNetworkMagic,

    -- * Internal conversion functions
    toByronProtocolMagicId,
    toByronNetworkMagic,
    toByronRequiresNetworkMagic,
    toShelleyNetwork,
    fromShelleyNetwork,
  ) where

import           Prelude

import           Ouroboros.Network.Magic (NetworkMagic (..))

import qualified Cardano.Crypto.ProtocolMagic as Byron
                   (ProtocolMagicId(..), RequiresNetworkMagic(..))
import qualified Cardano.Chain.Common as Byron (NetworkMagic(..))
import qualified Cardano.Chain.Genesis as Byron (mainnetProtocolMagicId)
import qualified Cardano.Ledger.BaseTypes as Shelley (Network(..))


-- ----------------------------------------------------------------------------
-- NetworkId type
--

data NetworkId = Mainnet
               | Testnet !NetworkMagic
  deriving (NetworkId -> NetworkId -> Bool
(NetworkId -> NetworkId -> Bool)
-> (NetworkId -> NetworkId -> Bool) -> Eq NetworkId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NetworkId -> NetworkId -> Bool
$c/= :: NetworkId -> NetworkId -> Bool
== :: NetworkId -> NetworkId -> Bool
$c== :: NetworkId -> NetworkId -> Bool
Eq, Int -> NetworkId -> ShowS
[NetworkId] -> ShowS
NetworkId -> String
(Int -> NetworkId -> ShowS)
-> (NetworkId -> String)
-> ([NetworkId] -> ShowS)
-> Show NetworkId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NetworkId] -> ShowS
$cshowList :: [NetworkId] -> ShowS
show :: NetworkId -> String
$cshow :: NetworkId -> String
showsPrec :: Int -> NetworkId -> ShowS
$cshowsPrec :: Int -> NetworkId -> ShowS
Show)

toNetworkMagic :: NetworkId -> NetworkMagic
toNetworkMagic :: NetworkId -> NetworkMagic
toNetworkMagic (Testnet NetworkMagic
nm) = NetworkMagic
nm
toNetworkMagic NetworkId
Mainnet      = NetworkMagic
mainnetNetworkMagic

mainnetNetworkMagic :: NetworkMagic
mainnetNetworkMagic :: NetworkMagic
mainnetNetworkMagic = Word32 -> NetworkMagic
NetworkMagic
                    (Word32 -> NetworkMagic)
-> (ProtocolMagicId -> Word32) -> ProtocolMagicId -> NetworkMagic
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProtocolMagicId -> Word32
Byron.unProtocolMagicId
                    (ProtocolMagicId -> NetworkMagic)
-> ProtocolMagicId -> NetworkMagic
forall a b. (a -> b) -> a -> b
$ ProtocolMagicId
Byron.mainnetProtocolMagicId


-- ----------------------------------------------------------------------------
-- Byron conversion functions
--

toByronProtocolMagicId :: NetworkId -> Byron.ProtocolMagicId
toByronProtocolMagicId :: NetworkId -> ProtocolMagicId
toByronProtocolMagicId NetworkId
Mainnet = ProtocolMagicId
Byron.mainnetProtocolMagicId
toByronProtocolMagicId (Testnet (NetworkMagic Word32
pm)) = Word32 -> ProtocolMagicId
Byron.ProtocolMagicId Word32
pm

toByronNetworkMagic :: NetworkId -> Byron.NetworkMagic
toByronNetworkMagic :: NetworkId -> NetworkMagic
toByronNetworkMagic NetworkId
Mainnet                     = NetworkMagic
Byron.NetworkMainOrStage
toByronNetworkMagic (Testnet (NetworkMagic Word32
nm)) = Word32 -> NetworkMagic
Byron.NetworkTestnet Word32
nm

toByronRequiresNetworkMagic :: NetworkId -> Byron.RequiresNetworkMagic
toByronRequiresNetworkMagic :: NetworkId -> RequiresNetworkMagic
toByronRequiresNetworkMagic NetworkId
Mainnet   = RequiresNetworkMagic
Byron.RequiresNoMagic
toByronRequiresNetworkMagic Testnet{} = RequiresNetworkMagic
Byron.RequiresMagic


-- ----------------------------------------------------------------------------
-- Shelley conversion functions
--

toShelleyNetwork :: NetworkId -> Shelley.Network
toShelleyNetwork :: NetworkId -> Network
toShelleyNetwork  NetworkId
Mainnet    = Network
Shelley.Mainnet
toShelleyNetwork (Testnet NetworkMagic
_) = Network
Shelley.Testnet

fromShelleyNetwork :: Shelley.Network -> NetworkMagic -> NetworkId
fromShelleyNetwork :: Network -> NetworkMagic -> NetworkId
fromShelleyNetwork Network
Shelley.Testnet NetworkMagic
nm = NetworkMagic -> NetworkId
Testnet NetworkMagic
nm
fromShelleyNetwork Network
Shelley.Mainnet NetworkMagic
nm
  | NetworkMagic
nm NetworkMagic -> NetworkMagic -> Bool
forall a. Eq a => a -> a -> Bool
== NetworkMagic
mainnetNetworkMagic = NetworkId
Mainnet
  | Bool
otherwise = String -> NetworkId
forall a. HasCallStack => String -> a
error String
"fromShelleyNetwork Mainnet: wrong mainnet network magic"