module Cardano.Api.NetworkId (
NetworkId(..),
NetworkMagic(..),
fromNetworkMagic,
toNetworkMagic,
mainnetNetworkMagic,
toByronProtocolMagicId,
toByronNetworkMagic,
toByronRequiresNetworkMagic,
toShelleyNetwork,
fromShelleyNetwork,
) where
import Prelude
import Ouroboros.Network.Magic (NetworkMagic (..))
import qualified Cardano.Chain.Common as Byron (NetworkMagic (..))
import qualified Cardano.Chain.Genesis as Byron (mainnetProtocolMagicId)
import qualified Cardano.Crypto.ProtocolMagic as Byron (ProtocolMagicId (..),
RequiresNetworkMagic (..))
import qualified Cardano.Ledger.BaseTypes as Shelley (Network (..))
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)
fromNetworkMagic :: NetworkMagic -> NetworkId
fromNetworkMagic :: NetworkMagic -> NetworkId
fromNetworkMagic NetworkMagic
nm =
if NetworkMagic
nm NetworkMagic -> NetworkMagic -> Bool
forall a. Eq a => a -> a -> Bool
== NetworkMagic
mainnetNetworkMagic
then NetworkId
Mainnet
else NetworkMagic -> NetworkId
Testnet NetworkMagic
nm
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
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
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"