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

    -- * Internal conversion functions
    toColeProtocolMagicId,
    toColeNetworkMagic,
    toColeRequiresNetworkMagic,
    toSophieNetwork,
    fromSophieNetwork,
  ) where

import           Prelude

import           Shardagnostic.Network.Magic (NetworkMagic (..))

import qualified Bcc.Crypto.ProtocolMagic as Cole
                   (ProtocolMagicId(..), RequiresNetworkMagic(..))
import qualified Bcc.Chain.Common as Cole (NetworkMagic(..))
import qualified Bcc.Chain.Genesis as Cole (mainnetProtocolMagicId)
import qualified Bcc.Ledger.BaseTypes as Sophie (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
Cole.unProtocolMagicId
                    (ProtocolMagicId -> NetworkMagic)
-> ProtocolMagicId -> NetworkMagic
forall a b. (a -> b) -> a -> b
$ ProtocolMagicId
Cole.mainnetProtocolMagicId


-- ----------------------------------------------------------------------------
-- Cole conversion functions
--

toColeProtocolMagicId :: NetworkId -> Cole.ProtocolMagicId
toColeProtocolMagicId :: NetworkId -> ProtocolMagicId
toColeProtocolMagicId NetworkId
Mainnet = ProtocolMagicId
Cole.mainnetProtocolMagicId
toColeProtocolMagicId (Testnet (NetworkMagic Word32
pm)) = Word32 -> ProtocolMagicId
Cole.ProtocolMagicId Word32
pm

toColeNetworkMagic :: NetworkId -> Cole.NetworkMagic
toColeNetworkMagic :: NetworkId -> NetworkMagic
toColeNetworkMagic NetworkId
Mainnet                     = NetworkMagic
Cole.NetworkMainOrStage
toColeNetworkMagic (Testnet (NetworkMagic Word32
nm)) = Word32 -> NetworkMagic
Cole.NetworkTestnet Word32
nm

toColeRequiresNetworkMagic :: NetworkId -> Cole.RequiresNetworkMagic
toColeRequiresNetworkMagic :: NetworkId -> RequiresNetworkMagic
toColeRequiresNetworkMagic NetworkId
Mainnet   = RequiresNetworkMagic
Cole.RequiresNoMagic
toColeRequiresNetworkMagic Testnet{} = RequiresNetworkMagic
Cole.RequiresMagic


-- ----------------------------------------------------------------------------
-- Sophie conversion functions
--

toSophieNetwork :: NetworkId -> Sophie.Network
toSophieNetwork :: NetworkId -> Network
toSophieNetwork  NetworkId
Mainnet    = Network
Sophie.Mainnet
toSophieNetwork (Testnet NetworkMagic
_) = Network
Sophie.Testnet

fromSophieNetwork :: Sophie.Network -> NetworkMagic -> NetworkId
fromSophieNetwork :: Network -> NetworkMagic -> NetworkId
fromSophieNetwork Network
Sophie.Testnet NetworkMagic
nm = NetworkMagic -> NetworkId
Testnet NetworkMagic
nm
fromSophieNetwork Network
Sophie.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
"fromSophieNetwork Mainnet: wrong mainnet network magic"