-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}

-- | Immutable pool values and schema workers. No native effects occur here.
module Cometa.Internal.Pool
  ( Ipv4,
    ipv4FromBytes,
    ipv4Bytes,
    ipv4FromText,
    ipv4Text,
    Ipv6,
    ipv6FromBytes,
    ipv6Bytes,
    ipv6FromText,
    ipv6Text,
    DnsName,
    mkDnsName,
    dnsNameText,
    Relay (..),
    Url,
    mkUrl,
    urlText,
    MetadataHash,
    metadataHashFromBytes,
    metadataHashBytes,
    Metadata (..),
    VrfKeyHash,
    vrfKeyHashFromBytes,
    vrfKeyHashBytes,
    BlsKey,
    mkBlsKey,
    publicKeyBytes,
    possessionProofBytes,
    PoolParameters (..),
    parametersFieldCount,
    relayEncoding,
    relayDecoder,
    metadataEncoding,
    metadataDecoder,
    blsEncoding,
    blsDecoder,
    parametersEncoding,
    parametersDecoder,
    parametersDecoderWithFieldCount,
    parametersValueEncoding,
    parametersValueEncodingWith,
    parametersValueDecoder,
    encode,
    decodeComplete,
  )
where

-- IMPORTS --------------------------------------------------------------------

import qualified Codec.CBOR.Decoding as Decode
import qualified Codec.CBOR.Encoding as Encode
import qualified Codec.CBOR.Read as Read
import qualified Codec.CBOR.Write as Write
import qualified Cometa.Address.Reward as Reward
import qualified Cometa.Credential as Credential
import Cometa.Encoding.Set (SetEncoding (..))
import Cometa.Error (Error (..))
import qualified Cometa.Internal.ProtocolValues as Protocol
import Control.Monad (replicateM, unless, when)
import Data.Bits (shiftR, (.&.))
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as Lazy
import Data.Char (isDigit, isHexDigit)
import Data.List (intercalate)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Word (Word16, Word64)
import Numeric (readHex, showHex)
import Text.Read (readMaybe)

-- TYPES ----------------------------------------------------------------------

-- | IPv4 address in network byte order. Equality and ordering compare bytes.
newtype Ipv4 = Ipv4 ByteString deriving (Ipv4 -> Ipv4 -> Bool
(Ipv4 -> Ipv4 -> Bool) -> (Ipv4 -> Ipv4 -> Bool) -> Eq Ipv4
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Ipv4 -> Ipv4 -> Bool
== :: Ipv4 -> Ipv4 -> Bool
$c/= :: Ipv4 -> Ipv4 -> Bool
/= :: Ipv4 -> Ipv4 -> Bool
Eq, Eq Ipv4
Eq Ipv4 =>
(Ipv4 -> Ipv4 -> Ordering)
-> (Ipv4 -> Ipv4 -> Bool)
-> (Ipv4 -> Ipv4 -> Bool)
-> (Ipv4 -> Ipv4 -> Bool)
-> (Ipv4 -> Ipv4 -> Bool)
-> (Ipv4 -> Ipv4 -> Ipv4)
-> (Ipv4 -> Ipv4 -> Ipv4)
-> Ord Ipv4
Ipv4 -> Ipv4 -> Bool
Ipv4 -> Ipv4 -> Ordering
Ipv4 -> Ipv4 -> Ipv4
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Ipv4 -> Ipv4 -> Ordering
compare :: Ipv4 -> Ipv4 -> Ordering
$c< :: Ipv4 -> Ipv4 -> Bool
< :: Ipv4 -> Ipv4 -> Bool
$c<= :: Ipv4 -> Ipv4 -> Bool
<= :: Ipv4 -> Ipv4 -> Bool
$c> :: Ipv4 -> Ipv4 -> Bool
> :: Ipv4 -> Ipv4 -> Bool
$c>= :: Ipv4 -> Ipv4 -> Bool
>= :: Ipv4 -> Ipv4 -> Bool
$cmax :: Ipv4 -> Ipv4 -> Ipv4
max :: Ipv4 -> Ipv4 -> Ipv4
$cmin :: Ipv4 -> Ipv4 -> Ipv4
min :: Ipv4 -> Ipv4 -> Ipv4
Ord, Int -> Ipv4 -> ShowS
[Ipv4] -> ShowS
Ipv4 -> [Char]
(Int -> Ipv4 -> ShowS)
-> (Ipv4 -> [Char]) -> ([Ipv4] -> ShowS) -> Show Ipv4
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Ipv4 -> ShowS
showsPrec :: Int -> Ipv4 -> ShowS
$cshow :: Ipv4 -> [Char]
show :: Ipv4 -> [Char]
$cshowList :: [Ipv4] -> ShowS
showList :: [Ipv4] -> ShowS
Show)

-- | IPv6 address in network byte order. Equality and ordering compare bytes.
newtype Ipv6 = Ipv6 ByteString deriving (Ipv6 -> Ipv6 -> Bool
(Ipv6 -> Ipv6 -> Bool) -> (Ipv6 -> Ipv6 -> Bool) -> Eq Ipv6
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Ipv6 -> Ipv6 -> Bool
== :: Ipv6 -> Ipv6 -> Bool
$c/= :: Ipv6 -> Ipv6 -> Bool
/= :: Ipv6 -> Ipv6 -> Bool
Eq, Eq Ipv6
Eq Ipv6 =>
(Ipv6 -> Ipv6 -> Ordering)
-> (Ipv6 -> Ipv6 -> Bool)
-> (Ipv6 -> Ipv6 -> Bool)
-> (Ipv6 -> Ipv6 -> Bool)
-> (Ipv6 -> Ipv6 -> Bool)
-> (Ipv6 -> Ipv6 -> Ipv6)
-> (Ipv6 -> Ipv6 -> Ipv6)
-> Ord Ipv6
Ipv6 -> Ipv6 -> Bool
Ipv6 -> Ipv6 -> Ordering
Ipv6 -> Ipv6 -> Ipv6
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Ipv6 -> Ipv6 -> Ordering
compare :: Ipv6 -> Ipv6 -> Ordering
$c< :: Ipv6 -> Ipv6 -> Bool
< :: Ipv6 -> Ipv6 -> Bool
$c<= :: Ipv6 -> Ipv6 -> Bool
<= :: Ipv6 -> Ipv6 -> Bool
$c> :: Ipv6 -> Ipv6 -> Bool
> :: Ipv6 -> Ipv6 -> Bool
$c>= :: Ipv6 -> Ipv6 -> Bool
>= :: Ipv6 -> Ipv6 -> Bool
$cmax :: Ipv6 -> Ipv6 -> Ipv6
max :: Ipv6 -> Ipv6 -> Ipv6
$cmin :: Ipv6 -> Ipv6 -> Ipv6
min :: Ipv6 -> Ipv6 -> Ipv6
Ord, Int -> Ipv6 -> ShowS
[Ipv6] -> ShowS
Ipv6 -> [Char]
(Int -> Ipv6 -> ShowS)
-> (Ipv6 -> [Char]) -> ([Ipv6] -> ShowS) -> Show Ipv6
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Ipv6 -> ShowS
showsPrec :: Int -> Ipv6 -> ShowS
$cshow :: Ipv6 -> [Char]
show :: Ipv6 -> [Char]
$cshowList :: [Ipv6] -> ShowS
showList :: [Ipv6] -> ShowS
Show)

-- | Relay DNS label, containing 1 through 64 UTF-8 bytes and no NUL.
-- DNS syntax and reachability are not checked. Comparison uses text.
newtype DnsName = DnsName Text deriving (DnsName -> DnsName -> Bool
(DnsName -> DnsName -> Bool)
-> (DnsName -> DnsName -> Bool) -> Eq DnsName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DnsName -> DnsName -> Bool
== :: DnsName -> DnsName -> Bool
$c/= :: DnsName -> DnsName -> Bool
/= :: DnsName -> DnsName -> Bool
Eq, Eq DnsName
Eq DnsName =>
(DnsName -> DnsName -> Ordering)
-> (DnsName -> DnsName -> Bool)
-> (DnsName -> DnsName -> Bool)
-> (DnsName -> DnsName -> Bool)
-> (DnsName -> DnsName -> Bool)
-> (DnsName -> DnsName -> DnsName)
-> (DnsName -> DnsName -> DnsName)
-> Ord DnsName
DnsName -> DnsName -> Bool
DnsName -> DnsName -> Ordering
DnsName -> DnsName -> DnsName
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: DnsName -> DnsName -> Ordering
compare :: DnsName -> DnsName -> Ordering
$c< :: DnsName -> DnsName -> Bool
< :: DnsName -> DnsName -> Bool
$c<= :: DnsName -> DnsName -> Bool
<= :: DnsName -> DnsName -> Bool
$c> :: DnsName -> DnsName -> Bool
> :: DnsName -> DnsName -> Bool
$c>= :: DnsName -> DnsName -> Bool
>= :: DnsName -> DnsName -> Bool
$cmax :: DnsName -> DnsName -> DnsName
max :: DnsName -> DnsName -> DnsName
$cmin :: DnsName -> DnsName -> DnsName
min :: DnsName -> DnsName -> DnsName
Ord, Int -> DnsName -> ShowS
[DnsName] -> ShowS
DnsName -> [Char]
(Int -> DnsName -> ShowS)
-> (DnsName -> [Char]) -> ([DnsName] -> ShowS) -> Show DnsName
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DnsName -> ShowS
showsPrec :: Int -> DnsName -> ShowS
$cshow :: DnsName -> [Char]
show :: DnsName -> [Char]
$cshowList :: [DnsName] -> ShowS
showList :: [DnsName] -> ShowS
Show)

-- | Relay alternatives. Ordering follows CBOR codes, then constructor fields.
-- Ports are unsigned 16-bit port numbers; absent fields encode as null.
data Relay
  = -- | IP relay: optional port, IPv4 and IPv6. All may be absent.
    SingleHostAddress !(Maybe Word16) !(Maybe Ipv4) !(Maybe Ipv6)
  | -- | A or AAAA DNS record, with optional port.
    SingleHostName !(Maybe Word16) !DnsName
  | -- | SRV DNS record; its port is supplied by DNS.
    MultiHostName !DnsName
  deriving (Relay -> Relay -> Bool
(Relay -> Relay -> Bool) -> (Relay -> Relay -> Bool) -> Eq Relay
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Relay -> Relay -> Bool
== :: Relay -> Relay -> Bool
$c/= :: Relay -> Relay -> Bool
/= :: Relay -> Relay -> Bool
Eq, Eq Relay
Eq Relay =>
(Relay -> Relay -> Ordering)
-> (Relay -> Relay -> Bool)
-> (Relay -> Relay -> Bool)
-> (Relay -> Relay -> Bool)
-> (Relay -> Relay -> Bool)
-> (Relay -> Relay -> Relay)
-> (Relay -> Relay -> Relay)
-> Ord Relay
Relay -> Relay -> Bool
Relay -> Relay -> Ordering
Relay -> Relay -> Relay
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Relay -> Relay -> Ordering
compare :: Relay -> Relay -> Ordering
$c< :: Relay -> Relay -> Bool
< :: Relay -> Relay -> Bool
$c<= :: Relay -> Relay -> Bool
<= :: Relay -> Relay -> Bool
$c> :: Relay -> Relay -> Bool
> :: Relay -> Relay -> Bool
$c>= :: Relay -> Relay -> Bool
>= :: Relay -> Relay -> Bool
$cmax :: Relay -> Relay -> Relay
max :: Relay -> Relay -> Relay
$cmin :: Relay -> Relay -> Relay
min :: Relay -> Relay -> Relay
Ord, Int -> Relay -> ShowS
[Relay] -> ShowS
Relay -> [Char]
(Int -> Relay -> ShowS)
-> (Relay -> [Char]) -> ([Relay] -> ShowS) -> Show Relay
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Relay -> ShowS
showsPrec :: Int -> Relay -> ShowS
$cshow :: Relay -> [Char]
show :: Relay -> [Char]
$cshowList :: [Relay] -> ShowS
showList :: [Relay] -> ShowS
Show)

-- | Pool metadata location of 0 through 128 UTF-8 bytes with no NUL.
-- No URI syntax or reachability check is implied. Comparison uses text.
newtype Url = Url Text deriving (Url -> Url -> Bool
(Url -> Url -> Bool) -> (Url -> Url -> Bool) -> Eq Url
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Url -> Url -> Bool
== :: Url -> Url -> Bool
$c/= :: Url -> Url -> Bool
/= :: Url -> Url -> Bool
Eq, Eq Url
Eq Url =>
(Url -> Url -> Ordering)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Bool)
-> (Url -> Url -> Url)
-> (Url -> Url -> Url)
-> Ord Url
Url -> Url -> Bool
Url -> Url -> Ordering
Url -> Url -> Url
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Url -> Url -> Ordering
compare :: Url -> Url -> Ordering
$c< :: Url -> Url -> Bool
< :: Url -> Url -> Bool
$c<= :: Url -> Url -> Bool
<= :: Url -> Url -> Bool
$c> :: Url -> Url -> Bool
> :: Url -> Url -> Bool
$c>= :: Url -> Url -> Bool
>= :: Url -> Url -> Bool
$cmax :: Url -> Url -> Url
max :: Url -> Url -> Url
$cmin :: Url -> Url -> Url
min :: Url -> Url -> Url
Ord, Int -> Url -> ShowS
[Url] -> ShowS
Url -> [Char]
(Int -> Url -> ShowS)
-> (Url -> [Char]) -> ([Url] -> ShowS) -> Show Url
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Url -> ShowS
showsPrec :: Int -> Url -> ShowS
$cshow :: Url -> [Char]
show :: Url -> [Char]
$cshowList :: [Url] -> ShowS
showList :: [Url] -> ShowS
Show)

-- | Exact 32-byte BLAKE2b-256 metadata digest. Comparison uses bytes.
newtype MetadataHash = MetadataHash ByteString deriving (MetadataHash -> MetadataHash -> Bool
(MetadataHash -> MetadataHash -> Bool)
-> (MetadataHash -> MetadataHash -> Bool) -> Eq MetadataHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MetadataHash -> MetadataHash -> Bool
== :: MetadataHash -> MetadataHash -> Bool
$c/= :: MetadataHash -> MetadataHash -> Bool
/= :: MetadataHash -> MetadataHash -> Bool
Eq, Eq MetadataHash
Eq MetadataHash =>
(MetadataHash -> MetadataHash -> Ordering)
-> (MetadataHash -> MetadataHash -> Bool)
-> (MetadataHash -> MetadataHash -> Bool)
-> (MetadataHash -> MetadataHash -> Bool)
-> (MetadataHash -> MetadataHash -> Bool)
-> (MetadataHash -> MetadataHash -> MetadataHash)
-> (MetadataHash -> MetadataHash -> MetadataHash)
-> Ord MetadataHash
MetadataHash -> MetadataHash -> Bool
MetadataHash -> MetadataHash -> Ordering
MetadataHash -> MetadataHash -> MetadataHash
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: MetadataHash -> MetadataHash -> Ordering
compare :: MetadataHash -> MetadataHash -> Ordering
$c< :: MetadataHash -> MetadataHash -> Bool
< :: MetadataHash -> MetadataHash -> Bool
$c<= :: MetadataHash -> MetadataHash -> Bool
<= :: MetadataHash -> MetadataHash -> Bool
$c> :: MetadataHash -> MetadataHash -> Bool
> :: MetadataHash -> MetadataHash -> Bool
$c>= :: MetadataHash -> MetadataHash -> Bool
>= :: MetadataHash -> MetadataHash -> Bool
$cmax :: MetadataHash -> MetadataHash -> MetadataHash
max :: MetadataHash -> MetadataHash -> MetadataHash
$cmin :: MetadataHash -> MetadataHash -> MetadataHash
min :: MetadataHash -> MetadataHash -> MetadataHash
Ord, Int -> MetadataHash -> ShowS
[MetadataHash] -> ShowS
MetadataHash -> [Char]
(Int -> MetadataHash -> ShowS)
-> (MetadataHash -> [Char])
-> ([MetadataHash] -> ShowS)
-> Show MetadataHash
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MetadataHash -> ShowS
showsPrec :: Int -> MetadataHash -> ShowS
$cshow :: MetadataHash -> [Char]
show :: MetadataHash -> [Char]
$cshowList :: [MetadataHash] -> ShowS
showList :: [MetadataHash] -> ShowS
Show)

-- | Metadata reference. Fetching and checking the payload is a caller operation.
data Metadata = Metadata
  { -- | Off-chain location, including the native schema's empty location.
    Metadata -> Url
url :: !Url,
    -- | Expected digest of exact payload bytes.
    Metadata -> MetadataHash
metadataHash :: !MetadataHash
  }
  deriving (Metadata -> Metadata -> Bool
(Metadata -> Metadata -> Bool)
-> (Metadata -> Metadata -> Bool) -> Eq Metadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Metadata -> Metadata -> Bool
== :: Metadata -> Metadata -> Bool
$c/= :: Metadata -> Metadata -> Bool
/= :: Metadata -> Metadata -> Bool
Eq, Eq Metadata
Eq Metadata =>
(Metadata -> Metadata -> Ordering)
-> (Metadata -> Metadata -> Bool)
-> (Metadata -> Metadata -> Bool)
-> (Metadata -> Metadata -> Bool)
-> (Metadata -> Metadata -> Bool)
-> (Metadata -> Metadata -> Metadata)
-> (Metadata -> Metadata -> Metadata)
-> Ord Metadata
Metadata -> Metadata -> Bool
Metadata -> Metadata -> Ordering
Metadata -> Metadata -> Metadata
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Metadata -> Metadata -> Ordering
compare :: Metadata -> Metadata -> Ordering
$c< :: Metadata -> Metadata -> Bool
< :: Metadata -> Metadata -> Bool
$c<= :: Metadata -> Metadata -> Bool
<= :: Metadata -> Metadata -> Bool
$c> :: Metadata -> Metadata -> Bool
> :: Metadata -> Metadata -> Bool
$c>= :: Metadata -> Metadata -> Bool
>= :: Metadata -> Metadata -> Bool
$cmax :: Metadata -> Metadata -> Metadata
max :: Metadata -> Metadata -> Metadata
$cmin :: Metadata -> Metadata -> Metadata
min :: Metadata -> Metadata -> Metadata
Ord, Int -> Metadata -> ShowS
[Metadata] -> ShowS
Metadata -> [Char]
(Int -> Metadata -> ShowS)
-> (Metadata -> [Char]) -> ([Metadata] -> ShowS) -> Show Metadata
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Metadata -> ShowS
showsPrec :: Int -> Metadata -> ShowS
$cshow :: Metadata -> [Char]
show :: Metadata -> [Char]
$cshowList :: [Metadata] -> ShowS
showList :: [Metadata] -> ShowS
Show)

-- | Exact 32-byte VRF verification-key hash. Importing does not verify a key.
newtype VrfKeyHash = VrfKeyHash ByteString deriving (VrfKeyHash -> VrfKeyHash -> Bool
(VrfKeyHash -> VrfKeyHash -> Bool)
-> (VrfKeyHash -> VrfKeyHash -> Bool) -> Eq VrfKeyHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VrfKeyHash -> VrfKeyHash -> Bool
== :: VrfKeyHash -> VrfKeyHash -> Bool
$c/= :: VrfKeyHash -> VrfKeyHash -> Bool
/= :: VrfKeyHash -> VrfKeyHash -> Bool
Eq, Eq VrfKeyHash
Eq VrfKeyHash =>
(VrfKeyHash -> VrfKeyHash -> Ordering)
-> (VrfKeyHash -> VrfKeyHash -> Bool)
-> (VrfKeyHash -> VrfKeyHash -> Bool)
-> (VrfKeyHash -> VrfKeyHash -> Bool)
-> (VrfKeyHash -> VrfKeyHash -> Bool)
-> (VrfKeyHash -> VrfKeyHash -> VrfKeyHash)
-> (VrfKeyHash -> VrfKeyHash -> VrfKeyHash)
-> Ord VrfKeyHash
VrfKeyHash -> VrfKeyHash -> Bool
VrfKeyHash -> VrfKeyHash -> Ordering
VrfKeyHash -> VrfKeyHash -> VrfKeyHash
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: VrfKeyHash -> VrfKeyHash -> Ordering
compare :: VrfKeyHash -> VrfKeyHash -> Ordering
$c< :: VrfKeyHash -> VrfKeyHash -> Bool
< :: VrfKeyHash -> VrfKeyHash -> Bool
$c<= :: VrfKeyHash -> VrfKeyHash -> Bool
<= :: VrfKeyHash -> VrfKeyHash -> Bool
$c> :: VrfKeyHash -> VrfKeyHash -> Bool
> :: VrfKeyHash -> VrfKeyHash -> Bool
$c>= :: VrfKeyHash -> VrfKeyHash -> Bool
>= :: VrfKeyHash -> VrfKeyHash -> Bool
$cmax :: VrfKeyHash -> VrfKeyHash -> VrfKeyHash
max :: VrfKeyHash -> VrfKeyHash -> VrfKeyHash
$cmin :: VrfKeyHash -> VrfKeyHash -> VrfKeyHash
min :: VrfKeyHash -> VrfKeyHash -> VrfKeyHash
Ord, Int -> VrfKeyHash -> ShowS
[VrfKeyHash] -> ShowS
VrfKeyHash -> [Char]
(Int -> VrfKeyHash -> ShowS)
-> (VrfKeyHash -> [Char])
-> ([VrfKeyHash] -> ShowS)
-> Show VrfKeyHash
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VrfKeyHash -> ShowS
showsPrec :: Int -> VrfKeyHash -> ShowS
$cshow :: VrfKeyHash -> [Char]
show :: VrfKeyHash -> [Char]
$cshowList :: [VrfKeyHash] -> ShowS
showList :: [VrfKeyHash] -> ShowS
Show)

-- | BLS public key (96 bytes) and possession proof (48 bytes). Only lengths
-- are checked; construction does not verify group membership or proof validity.
-- Equality and ordering compare public-key bytes, then proof bytes.
data BlsKey = BlsKey !ByteString !ByteString deriving (BlsKey -> BlsKey -> Bool
(BlsKey -> BlsKey -> Bool)
-> (BlsKey -> BlsKey -> Bool) -> Eq BlsKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlsKey -> BlsKey -> Bool
== :: BlsKey -> BlsKey -> Bool
$c/= :: BlsKey -> BlsKey -> Bool
/= :: BlsKey -> BlsKey -> Bool
Eq, Eq BlsKey
Eq BlsKey =>
(BlsKey -> BlsKey -> Ordering)
-> (BlsKey -> BlsKey -> Bool)
-> (BlsKey -> BlsKey -> Bool)
-> (BlsKey -> BlsKey -> Bool)
-> (BlsKey -> BlsKey -> Bool)
-> (BlsKey -> BlsKey -> BlsKey)
-> (BlsKey -> BlsKey -> BlsKey)
-> Ord BlsKey
BlsKey -> BlsKey -> Bool
BlsKey -> BlsKey -> Ordering
BlsKey -> BlsKey -> BlsKey
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: BlsKey -> BlsKey -> Ordering
compare :: BlsKey -> BlsKey -> Ordering
$c< :: BlsKey -> BlsKey -> Bool
< :: BlsKey -> BlsKey -> Bool
$c<= :: BlsKey -> BlsKey -> Bool
<= :: BlsKey -> BlsKey -> Bool
$c> :: BlsKey -> BlsKey -> Bool
> :: BlsKey -> BlsKey -> Bool
$c>= :: BlsKey -> BlsKey -> Bool
>= :: BlsKey -> BlsKey -> Bool
$cmax :: BlsKey -> BlsKey -> BlsKey
max :: BlsKey -> BlsKey -> BlsKey
$cmin :: BlsKey -> BlsKey -> BlsKey
min :: BlsKey -> BlsKey -> BlsKey
Ord, Int -> BlsKey -> ShowS
[BlsKey] -> ShowS
BlsKey -> [Char]
(Int -> BlsKey -> ShowS)
-> (BlsKey -> [Char]) -> ([BlsKey] -> ShowS) -> Show BlsKey
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlsKey -> ShowS
showsPrec :: Int -> BlsKey -> ShowS
$cshow :: BlsKey -> [Char]
show :: BlsKey -> [Char]
$cshowList :: [BlsKey] -> ShowS
showList :: [BlsKey] -> ShowS
Show)

-- | Registration parameters. Assembly and record updates are total because
-- constrained fields are validated values. Equality is semantic (numeric margin,
-- unordered owners, ordered relays); Show is diagnostic. No ledger-era admission,
-- minimum cost, pledge funding, or BLS proof verification is implied.
data PoolParameters = PoolParameters
  { -- | Pool operator verification-key hash, exactly 28 bytes.
    PoolParameters -> KeyHash StakePool
operator :: !(Credential.KeyHash Credential.StakePool),
    -- | VRF verification-key hash, exactly 32 bytes.
    PoolParameters -> VrfKeyHash
vrfKeyHash :: !VrfKeyHash,
    -- | Optional Dijkstra BLS key and proof; absent in earlier registrations.
    PoolParameters -> Maybe BlsKey
blsKey :: !(Maybe BlsKey),
    -- | Pledged lovelace, from zero through 2^64 - 1.
    PoolParameters -> Word64
pledge :: !Word64,
    -- | Fixed pool cost in lovelace, from zero through 2^64 - 1.
    PoolParameters -> Word64
cost :: !Word64,
    -- | Exact share of remaining rewards, zero through one inclusive.
    PoolParameters -> UnitInterval
margin :: !Protocol.UnitInterval,
    -- | Account receiving operator rewards, with its network tag preserved.
    PoolParameters -> RewardAddress
rewardAccount :: !Reward.RewardAddress,
    -- | Staking key hashes. Set semantics remove repeated owners.
    PoolParameters -> Set (KeyHash Stake)
owners :: !(Set (Credential.KeyHash Credential.Stake)),
    -- | Relay sequence; order and duplicates are preserved.
    PoolParameters -> [Relay]
relays :: ![Relay],
    -- | Optional off-chain metadata reference.
    PoolParameters -> Maybe Metadata
metadata :: !(Maybe Metadata)
  }
  deriving (PoolParameters -> PoolParameters -> Bool
(PoolParameters -> PoolParameters -> Bool)
-> (PoolParameters -> PoolParameters -> Bool) -> Eq PoolParameters
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PoolParameters -> PoolParameters -> Bool
== :: PoolParameters -> PoolParameters -> Bool
$c/= :: PoolParameters -> PoolParameters -> Bool
/= :: PoolParameters -> PoolParameters -> Bool
Eq, Eq PoolParameters
Eq PoolParameters =>
(PoolParameters -> PoolParameters -> Ordering)
-> (PoolParameters -> PoolParameters -> Bool)
-> (PoolParameters -> PoolParameters -> Bool)
-> (PoolParameters -> PoolParameters -> Bool)
-> (PoolParameters -> PoolParameters -> Bool)
-> (PoolParameters -> PoolParameters -> PoolParameters)
-> (PoolParameters -> PoolParameters -> PoolParameters)
-> Ord PoolParameters
PoolParameters -> PoolParameters -> Bool
PoolParameters -> PoolParameters -> Ordering
PoolParameters -> PoolParameters -> PoolParameters
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PoolParameters -> PoolParameters -> Ordering
compare :: PoolParameters -> PoolParameters -> Ordering
$c< :: PoolParameters -> PoolParameters -> Bool
< :: PoolParameters -> PoolParameters -> Bool
$c<= :: PoolParameters -> PoolParameters -> Bool
<= :: PoolParameters -> PoolParameters -> Bool
$c> :: PoolParameters -> PoolParameters -> Bool
> :: PoolParameters -> PoolParameters -> Bool
$c>= :: PoolParameters -> PoolParameters -> Bool
>= :: PoolParameters -> PoolParameters -> Bool
$cmax :: PoolParameters -> PoolParameters -> PoolParameters
max :: PoolParameters -> PoolParameters -> PoolParameters
$cmin :: PoolParameters -> PoolParameters -> PoolParameters
min :: PoolParameters -> PoolParameters -> PoolParameters
Ord, Int -> PoolParameters -> ShowS
[PoolParameters] -> ShowS
PoolParameters -> [Char]
(Int -> PoolParameters -> ShowS)
-> (PoolParameters -> [Char])
-> ([PoolParameters] -> ShowS)
-> Show PoolParameters
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PoolParameters -> ShowS
showsPrec :: Int -> PoolParameters -> ShowS
$cshow :: PoolParameters -> [Char]
show :: PoolParameters -> [Char]
$cshowList :: [PoolParameters] -> ShowS
showList :: [PoolParameters] -> ShowS
Show)

-- DEFINITIONS ----------------------------------------------------------------

-- | Validate an exact four-byte network-order IPv4 address.
-- Wrong lengths return 'InvalidPoolIpLength' with expected and actual lengths.
ipv4FromBytes :: ByteString -> Either Error Ipv4
ipv4FromBytes :: ByteString -> Either Error Ipv4
ipv4FromBytes ByteString
bytes = ByteString -> Ipv4
Ipv4 (ByteString -> Ipv4)
-> Either Error ByteString -> Either Error Ipv4
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> ByteString -> Either Error ByteString
exactIp Int
4 ByteString
bytes

-- | Exact four-byte address.
ipv4Bytes :: Ipv4 -> ByteString
ipv4Bytes :: Ipv4 -> ByteString
ipv4Bytes (Ipv4 ByteString
bytes) = ByteString
bytes

-- | Parse four decimal octets, each 0 through 255, separated by dots.
-- Leading zeros are accepted; signs, whitespace and empty octets are rejected.
-- Malformed input returns 'InvalidPoolIpText' with a diagnostic.
ipv4FromText :: Text -> Either Error Ipv4
ipv4FromText :: Text -> Either Error Ipv4
ipv4FromText Text
text = case (Text -> Maybe Word8) -> [Text] -> Maybe [Word8]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Text -> Maybe Word8
forall {a}. Num a => Text -> Maybe a
octet (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
Text.splitOn Text
"." Text
text) of
  Just [Word8]
values | [Word8] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word8]
values Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
4 -> ByteString -> Either Error Ipv4
ipv4FromBytes ([Word8] -> ByteString
ByteString.pack [Word8]
values)
  Maybe [Word8]
_ -> Error -> Either Error Ipv4
forall a b. a -> Either a b
Left (Text -> Error
InvalidPoolIpText Text
"Invalid dotted IPv4 address")
  where
    octet :: Text -> Maybe a
octet Text
part
      | Text -> Bool
Text.null Text
part Bool -> Bool -> Bool
|| Text -> Int
Text.length Text
part Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
3 Bool -> Bool -> Bool
|| Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
Text.all Char -> Bool
isDigit Text
part) = Maybe a
forall a. Maybe a
Nothing
      | Bool
otherwise = do
          Int
value <- [Char] -> Maybe Int
forall a. Read a => [Char] -> Maybe a
readMaybe (Text -> [Char]
Text.unpack Text
part) :: Maybe Int
          if Int
value Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
255 then a -> Maybe a
forall a. a -> Maybe a
Just (Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
value) else Maybe a
forall a. Maybe a
Nothing

-- | Canonical dotted decimal, without leading zeros.
ipv4Text :: Ipv4 -> Text
ipv4Text :: Ipv4 -> Text
ipv4Text = [Char] -> Text
Text.pack ([Char] -> Text) -> (Ipv4 -> [Char]) -> Ipv4 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"." ([[Char]] -> [Char]) -> (Ipv4 -> [[Char]]) -> Ipv4 -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> [Char]) -> [Word8] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> [Char]
forall a. Show a => a -> [Char]
show ([Word8] -> [[Char]]) -> (Ipv4 -> [Word8]) -> Ipv4 -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
ByteString.unpack (ByteString -> [Word8]) -> (Ipv4 -> ByteString) -> Ipv4 -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ipv4 -> ByteString
ipv4Bytes

-- | Validate an exact sixteen-byte network-order IPv6 address.
ipv6FromBytes :: ByteString -> Either Error Ipv6
ipv6FromBytes :: ByteString -> Either Error Ipv6
ipv6FromBytes ByteString
bytes = ByteString -> Ipv6
Ipv6 (ByteString -> Ipv6)
-> Either Error ByteString -> Either Error Ipv6
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> ByteString -> Either Error ByteString
exactIp Int
16 ByteString
bytes

-- | Exact sixteen-byte address.
ipv6Bytes :: Ipv6 -> ByteString
ipv6Bytes :: Ipv6 -> ByteString
ipv6Bytes (Ipv6 ByteString
bytes) = ByteString
bytes

-- | Parse IPv6 hexadecimal groups, accepting a single @::@ compression and
-- an optional final dotted IPv4 address. Hexadecimal letter case is ignored;
-- signs, whitespace, repeated compression and incorrect group counts return
-- 'InvalidPoolIpText'.
ipv6FromText :: Text -> Either Error Ipv6
ipv6FromText :: Text -> Either Error Ipv6
ipv6FromText Text
text = case Text -> Maybe Text
expandIpv4Suffix Text
text Maybe Text -> (Text -> Maybe [Word16]) -> Maybe [Word16]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Maybe [Word16]
ipv6Groups of
  Just [Word16]
groups -> ByteString -> Either Error Ipv6
ipv6FromBytes ([Word8] -> ByteString
ByteString.pack ((Word16 -> [Word8]) -> [Word16] -> [Word8]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Word16 -> [Word8]
forall {a} {a}. (Integral a, Bits a, Num a) => a -> [a]
networkBytes [Word16]
groups))
  Maybe [Word16]
Nothing -> Error -> Either Error Ipv6
forall a b. a -> Either a b
Left (Text -> Error
InvalidPoolIpText Text
"Invalid IPv6 address")
  where
    networkBytes :: a -> [a]
networkBytes a
value = [a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
value a -> Int -> a
forall a. Bits a => a -> Int -> a
`shiftR` Int
8), a -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
value a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
255)]

ipv6Groups :: Text -> Maybe [Word16]
ipv6Groups :: Text -> Maybe [Word16]
ipv6Groups Text
text = case HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
Text.splitOn Text
"::" Text
text of
  [Text
whole] -> do
    [Word16]
groups <- Text -> Maybe [Word16]
forall {a}. (Eq a, Num a) => Text -> Maybe [a]
side Text
whole
    if [Word16] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word16]
groups Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
8 then [Word16] -> Maybe [Word16]
forall a. a -> Maybe a
Just [Word16]
groups else Maybe [Word16]
forall a. Maybe a
Nothing
  [Text
before, Text
after] -> do
    [Word16]
left <- Text -> Maybe [Word16]
forall {a}. (Eq a, Num a) => Text -> Maybe [a]
side Text
before
    [Word16]
right <- Text -> Maybe [Word16]
forall {a}. (Eq a, Num a) => Text -> Maybe [a]
side Text
after
    let missing :: Int
missing = Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Word16] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word16]
left Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Word16] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word16]
right
    if Int
missing Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then [Word16] -> Maybe [Word16]
forall a. a -> Maybe a
Just ([Word16]
left [Word16] -> [Word16] -> [Word16]
forall a. [a] -> [a] -> [a]
++ Int -> Word16 -> [Word16]
forall a. Int -> a -> [a]
replicate Int
missing Word16
0 [Word16] -> [Word16] -> [Word16]
forall a. [a] -> [a] -> [a]
++ [Word16]
right) else Maybe [Word16]
forall a. Maybe a
Nothing
  [Text]
_ -> Maybe [Word16]
forall a. Maybe a
Nothing
  where
    side :: Text -> Maybe [a]
side Text
part
      | Text -> Bool
Text.null Text
part = [a] -> Maybe [a]
forall a. a -> Maybe a
Just []
      | Bool
otherwise = (Text -> Maybe a) -> [Text] -> Maybe [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Text -> Maybe a
forall {a}. (Eq a, Num a) => Text -> Maybe a
group (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
Text.splitOn Text
":" Text
part)
    group :: Text -> Maybe a
group Text
part
      | Text -> Bool
Text.null Text
part Bool -> Bool -> Bool
|| Text -> Int
Text.length Text
part Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
4 Bool -> Bool -> Bool
|| Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
Text.all Char -> Bool
isHexDigit Text
part) = Maybe a
forall a. Maybe a
Nothing
      | Bool
otherwise = case ReadS a
forall a. (Eq a, Num a) => ReadS a
readHex (Text -> [Char]
Text.unpack Text
part) of
          [(a
value, [Char]
"")] -> a -> Maybe a
forall a. a -> Maybe a
Just a
value
          [(a, [Char])]
_ -> Maybe a
forall a. Maybe a
Nothing

-- Convert a final dotted address into two hexadecimal groups before parsing
-- compression. Dotted input anywhere else remains invalid hexadecimal text.
expandIpv4Suffix :: Text -> Maybe Text
expandIpv4Suffix :: Text -> Maybe Text
expandIpv4Suffix Text
text
  | Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
Text.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.') Text
text) = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
text
  | Bool
otherwise = do
      let (Text
prefix, Text
suffix) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
Text.breakOnEnd Text
":" Text
text
      Bool -> Maybe () -> Maybe ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Text -> Bool
Text.null Text
prefix) Maybe ()
forall a. Maybe a
Nothing
      Ipv4
address <- (Error -> Maybe Ipv4)
-> (Ipv4 -> Maybe Ipv4) -> Either Error Ipv4 -> Maybe Ipv4
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Ipv4 -> Error -> Maybe Ipv4
forall a b. a -> b -> a
const Maybe Ipv4
forall a. Maybe a
Nothing) Ipv4 -> Maybe Ipv4
forall a. a -> Maybe a
Just (Text -> Either Error Ipv4
ipv4FromText Text
suffix)
      case ByteString -> [Word8]
ByteString.unpack (Ipv4 -> ByteString
ipv4Bytes Ipv4
address) of
        [Word8
a, Word8
b, Word8
c, Word8
d] -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Word8 -> Word8 -> Text
forall {a} {a}. (Integral a, Integral a) => a -> a -> Text
pair Word8
a Word8
b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Word8 -> Word8 -> Text
forall {a} {a}. (Integral a, Integral a) => a -> a -> Text
pair Word8
c Word8
d)
        [Word8]
_ -> Maybe Text
forall a. Maybe a
Nothing
  where
    pair :: a -> a -> Text
pair a
a a
b = [Char] -> Text
Text.pack (Word16 -> ShowS
forall a. Integral a => a -> ShowS
showHex (a -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
a Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
* Word16
256 Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
+ a -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
b :: Word16) [Char]
"")

-- | Lowercase, fully expanded IPv6 text with eight four-digit groups.
ipv6Text :: Ipv6 -> Text
ipv6Text :: Ipv6 -> Text
ipv6Text = Text -> [Text] -> Text
Text.intercalate Text
":" ([Text] -> Text) -> (Ipv6 -> [Text]) -> Ipv6 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> [Text]
forall {p}. Integral p => [p] -> [Text]
pairs ([Word8] -> [Text]) -> (Ipv6 -> [Word8]) -> Ipv6 -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
ByteString.unpack (ByteString -> [Word8]) -> (Ipv6 -> ByteString) -> Ipv6 -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ipv6 -> ByteString
ipv6Bytes
  where
    pairs :: [p] -> [Text]
pairs (p
a : p
b : [p]
rest) = [Char] -> Text
Text.pack (p -> [Char]
forall {p}. Integral p => p -> [Char]
pad p
a [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ p -> [Char]
forall {p}. Integral p => p -> [Char]
pad p
b) Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [p] -> [Text]
pairs [p]
rest
    pairs [p]
_ = []
    pad :: p -> [Char]
pad p
value = let digits :: [Char]
digits = p -> ShowS
forall a. Integral a => a -> ShowS
showHex p
value [Char]
"" in Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Char] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
digits) Char
'0' [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
digits

exactIp :: Int -> ByteString -> Either Error ByteString
exactIp :: Int -> ByteString -> Either Error ByteString
exactIp Int
size ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
size = ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right ByteString
bytes
  | Bool
otherwise = Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Int -> Int -> Error
InvalidPoolIpLength Int
size (ByteString -> Int
ByteString.length ByteString
bytes))

-- | Validate 1 through 64 UTF-8 bytes and reject NUL, returning
-- 'InvalidPoolDnsName' on either violation. DNS syntax is not checked.
mkDnsName :: Text -> Either Error DnsName
mkDnsName :: Text -> Either Error DnsName
mkDnsName Text
text
  | Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1 Bool -> Bool -> Bool
|| Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
64 Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
Text.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\0') Text
text = Error -> Either Error DnsName
forall a b. a -> Either a b
Left (Text -> Error
InvalidPoolDnsName Text
"Expected 1 through 64 UTF-8 bytes without NUL")
  | Bool
otherwise = DnsName -> Either Error DnsName
forall a b. b -> Either a b
Right (Text -> DnsName
DnsName Text
text)
  where
    size :: Int
size = ByteString -> Int
ByteString.length (Text -> ByteString
Text.encodeUtf8 Text
text)

-- | Original DNS text.
dnsNameText :: DnsName -> Text
dnsNameText :: DnsName -> Text
dnsNameText (DnsName Text
text) = Text
text

-- | Validate at most 128 UTF-8 bytes without NUL. Empty is valid in this
-- pool schema. Violations return 'InvalidPoolMetadataUrl'.
mkUrl :: Text -> Either Error Url
mkUrl :: Text -> Either Error Url
mkUrl Text
text
  | ByteString -> Int
ByteString.length (Text -> ByteString
Text.encodeUtf8 Text
text) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
128 Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
Text.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\0') Text
text = Error -> Either Error Url
forall a b. a -> Either a b
Left (Text -> Error
InvalidPoolMetadataUrl Text
"Expected at most 128 UTF-8 bytes without NUL")
  | Bool
otherwise = Url -> Either Error Url
forall a b. b -> Either a b
Right (Text -> Url
Url Text
text)

-- | Exact original location text.
urlText :: Url -> Text
urlText :: Url -> Text
urlText (Url Text
text) = Text
text

-- | Import exactly 32 metadata digest bytes without hashing; other lengths
-- return 'InvalidPoolMetadataHashLength' with the actual byte count.
metadataHashFromBytes :: ByteString -> Either Error MetadataHash
metadataHashFromBytes :: ByteString -> Either Error MetadataHash
metadataHashFromBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = MetadataHash -> Either Error MetadataHash
forall a b. b -> Either a b
Right (ByteString -> MetadataHash
MetadataHash ByteString
bytes)
  | Bool
otherwise = Error -> Either Error MetadataHash
forall a b. a -> Either a b
Left (Int -> Error
InvalidPoolMetadataHashLength (ByteString -> Int
ByteString.length ByteString
bytes))

-- | Exact metadata digest bytes.
metadataHashBytes :: MetadataHash -> ByteString
metadataHashBytes :: MetadataHash -> ByteString
metadataHashBytes (MetadataHash ByteString
bytes) = ByteString
bytes

-- | Import exactly 32 VRF digest bytes without hashing; other lengths return
-- 'InvalidPoolHashLength' with the actual byte count.
vrfKeyHashFromBytes :: ByteString -> Either Error VrfKeyHash
vrfKeyHashFromBytes :: ByteString -> Either Error VrfKeyHash
vrfKeyHashFromBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = VrfKeyHash -> Either Error VrfKeyHash
forall a b. b -> Either a b
Right (ByteString -> VrfKeyHash
VrfKeyHash ByteString
bytes)
  | Bool
otherwise = Error -> Either Error VrfKeyHash
forall a b. a -> Either a b
Left (Int -> Error
InvalidPoolHashLength (ByteString -> Int
ByteString.length ByteString
bytes))

-- | Exact VRF digest bytes.
vrfKeyHashBytes :: VrfKeyHash -> ByteString
vrfKeyHashBytes :: VrfKeyHash -> ByteString
vrfKeyHashBytes (VrfKeyHash ByteString
bytes) = ByteString
bytes

-- | Validate public-key bytes (first argument, 96 bytes) and possession-proof
-- bytes (second argument, 48 bytes). 'InvalidPoolBlsKeyLength' reports both
-- actual lengths. No cryptographic validation occurs.
mkBlsKey :: ByteString -> ByteString -> Either Error BlsKey
mkBlsKey :: ByteString -> ByteString -> Either Error BlsKey
mkBlsKey ByteString
key ByteString
proof
  | ByteString -> Int
ByteString.length ByteString
key Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
96 Bool -> Bool -> Bool
&& ByteString -> Int
ByteString.length ByteString
proof Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
48 = BlsKey -> Either Error BlsKey
forall a b. b -> Either a b
Right (ByteString -> ByteString -> BlsKey
BlsKey ByteString
key ByteString
proof)
  | Bool
otherwise = Error -> Either Error BlsKey
forall a b. a -> Either a b
Left (Int -> Int -> Error
InvalidPoolBlsKeyLength (ByteString -> Int
ByteString.length ByteString
key) (ByteString -> Int
ByteString.length ByteString
proof))

-- | Exact 96 public-key bytes.
publicKeyBytes :: BlsKey -> ByteString
publicKeyBytes :: BlsKey -> ByteString
publicKeyBytes (BlsKey ByteString
key ByteString
_) = ByteString
key

-- | Exact 48 possession-proof bytes.
possessionProofBytes :: BlsKey -> ByteString
possessionProofBytes :: BlsKey -> ByteString
possessionProofBytes (BlsKey ByteString
_ ByteString
proof) = ByteString
proof

encode :: (a -> Encode.Encoding) -> a -> ByteString
encode :: forall a. (a -> Encoding) -> a -> ByteString
encode a -> Encoding
encoder = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString) -> (a -> Encoding) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding
encoder

decodeComplete :: (forall s. Decode.Decoder s a) -> ByteString -> Either Error a
decodeComplete :: forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decodeComplete forall s. Decoder s a
decoder ByteString
bytes = case (forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
Read.deserialiseFromBytes Decoder s a
forall s. Decoder s a
decoder (ByteString -> ByteString
Lazy.fromStrict ByteString
bytes) of
  Left DeserialiseFailure
failure -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidPoolCbor ([Char] -> Text
Text.pack (DeserialiseFailure -> [Char]
forall a. Show a => a -> [Char]
show DeserialiseFailure
failure)))
  Right (ByteString
remaining, a
value)
    | ByteString -> Bool
Lazy.null ByteString
remaining -> a -> Either Error a
forall a b. b -> Either a b
Right a
value
    | Bool
otherwise -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
InvalidPoolCbor Text
"Trailing pool CBOR bytes")

metadataEncoding :: Metadata -> Encode.Encoding
metadataEncoding :: Metadata -> Encoding
metadataEncoding (Metadata Url
location MetadataHash
digest) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
Encode.encodeString (Url -> Text
urlText Url
location) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (MetadataHash -> ByteString
metadataHashBytes MetadataHash
digest)

metadataDecoder :: Decode.Decoder s Metadata
metadataDecoder :: forall s. Decoder s Metadata
metadataDecoder = Int -> Decoder s Metadata -> Decoder s Metadata
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s Metadata -> Decoder s Metadata)
-> Decoder s Metadata -> Decoder s Metadata
forall a b. (a -> b) -> a -> b
$ Url -> MetadataHash -> Metadata
Metadata (Url -> MetadataHash -> Metadata)
-> Decoder s Url -> Decoder s (MetadataHash -> Metadata)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text -> Either Error Url) -> Text -> Decoder s Url
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated Text -> Either Error Url
mkUrl (Text -> Decoder s Url) -> Decoder s Text -> Decoder s Url
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s Text
forall s. Decoder s Text
Decode.decodeString) Decoder s (MetadataHash -> Metadata)
-> Decoder s MetadataHash -> Decoder s Metadata
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((ByteString -> Either Error MetadataHash)
-> ByteString -> Decoder s MetadataHash
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error MetadataHash
metadataHashFromBytes (ByteString -> Decoder s MetadataHash)
-> Decoder s ByteString -> Decoder s MetadataHash
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes)

blsEncoding :: BlsKey -> Encode.Encoding
blsEncoding :: BlsKey -> Encoding
blsEncoding (BlsKey ByteString
key ByteString
proof) = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes ByteString
key Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes ByteString
proof

blsDecoder :: Decode.Decoder s BlsKey
blsDecoder :: forall s. Decoder s BlsKey
blsDecoder = Int -> Decoder s BlsKey -> Decoder s BlsKey
forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
2 (Decoder s BlsKey -> Decoder s BlsKey)
-> Decoder s BlsKey -> Decoder s BlsKey
forall a b. (a -> b) -> a -> b
$ do
  ByteString
key <- Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  ByteString
proof <- Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  ((ByteString, ByteString) -> Either Error BlsKey)
-> (ByteString, ByteString) -> Decoder s BlsKey
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ((ByteString -> ByteString -> Either Error BlsKey)
-> (ByteString, ByteString) -> Either Error BlsKey
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ByteString -> ByteString -> Either Error BlsKey
mkBlsKey) (ByteString
key, ByteString
proof)

relayEncoding :: Relay -> Encode.Encoding
relayEncoding :: Relay -> Encoding
relayEncoding Relay
relay = case Relay
relay of
  SingleHostAddress Maybe Word16
port Maybe Ipv4
ipv4 Maybe Ipv6
ipv6 -> Word -> Encoding
Encode.encodeListLen Word
4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Word16 -> Encoding) -> Maybe Word16 -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
optional Word16 -> Encoding
Encode.encodeWord16 Maybe Word16
port Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Ipv4 -> Encoding) -> Maybe Ipv4 -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
optional (ByteString -> Encoding
Encode.encodeBytes (ByteString -> Encoding)
-> (Ipv4 -> ByteString) -> Ipv4 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ipv4 -> ByteString
ipv4Bytes) Maybe Ipv4
ipv4 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Ipv6 -> Encoding) -> Maybe Ipv6 -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
optional (ByteString -> Encoding
Encode.encodeBytes (ByteString -> Encoding)
-> (Ipv6 -> ByteString) -> Ipv6 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ipv6 -> ByteString
ipv6Bytes) Maybe Ipv6
ipv6
  SingleHostName Maybe Word16
port DnsName
dns -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Word16 -> Encoding) -> Maybe Word16 -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
optional Word16 -> Encoding
Encode.encodeWord16 Maybe Word16
port Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
Encode.encodeString (DnsName -> Text
dnsNameText DnsName
dns)
  MultiHostName DnsName
dns -> Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Text -> Encoding
Encode.encodeString (DnsName -> Text
dnsNameText DnsName
dns)

relayDecoder :: Decode.Decoder s Relay
relayDecoder :: forall s. Decoder s Relay
relayDecoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  Word
kind <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
  Int
expected <- case Word
kind of
    Word
0 -> Int -> Decoder s Int
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
4
    Word
1 -> Int -> Decoder s Int
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
3
    Word
2 -> Int -> Decoder s Int
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
2
    Word
_ -> [Char] -> Decoder s Int
forall a. [Char] -> Decoder s a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Unknown relay kind"
  Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size
  Relay
value <- case Word
kind of
    Word
0 -> Maybe Word16 -> Maybe Ipv4 -> Maybe Ipv6 -> Relay
SingleHostAddress (Maybe Word16 -> Maybe Ipv4 -> Maybe Ipv6 -> Relay)
-> Decoder s (Maybe Word16)
-> Decoder s (Maybe Ipv4 -> Maybe Ipv6 -> Relay)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word16 -> Decoder s (Maybe Word16)
forall s a. Decoder s a -> Decoder s (Maybe a)
nullable Decoder s Word16
forall s. Decoder s Word16
Decode.decodeWord16 Decoder s (Maybe Ipv4 -> Maybe Ipv6 -> Relay)
-> Decoder s (Maybe Ipv4) -> Decoder s (Maybe Ipv6 -> Relay)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Ipv4 -> Decoder s (Maybe Ipv4)
forall s a. Decoder s a -> Decoder s (Maybe a)
nullable ((ByteString -> Either Error Ipv4) -> ByteString -> Decoder s Ipv4
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error Ipv4
ipv4FromBytes (ByteString -> Decoder s Ipv4)
-> Decoder s ByteString -> Decoder s Ipv4
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes) Decoder s (Maybe Ipv6 -> Relay)
-> Decoder s (Maybe Ipv6) -> Decoder s Relay
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s Ipv6 -> Decoder s (Maybe Ipv6)
forall s a. Decoder s a -> Decoder s (Maybe a)
nullable ((ByteString -> Either Error Ipv6) -> ByteString -> Decoder s Ipv6
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error Ipv6
ipv6FromBytes (ByteString -> Decoder s Ipv6)
-> Decoder s ByteString -> Decoder s Ipv6
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes)
    Word
1 -> Maybe Word16 -> DnsName -> Relay
SingleHostName (Maybe Word16 -> DnsName -> Relay)
-> Decoder s (Maybe Word16) -> Decoder s (DnsName -> Relay)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word16 -> Decoder s (Maybe Word16)
forall s a. Decoder s a -> Decoder s (Maybe a)
nullable Decoder s Word16
forall s. Decoder s Word16
Decode.decodeWord16 Decoder s (DnsName -> Relay)
-> Decoder s DnsName -> Decoder s Relay
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Text -> Either Error DnsName) -> Text -> Decoder s DnsName
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated Text -> Either Error DnsName
mkDnsName (Text -> Decoder s DnsName) -> Decoder s Text -> Decoder s DnsName
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s Text
forall s. Decoder s Text
Decode.decodeString)
    Word
_ -> DnsName -> Relay
MultiHostName (DnsName -> Relay) -> Decoder s DnsName -> Decoder s Relay
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text -> Either Error DnsName) -> Text -> Decoder s DnsName
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated Text -> Either Error DnsName
mkDnsName (Text -> Decoder s DnsName) -> Decoder s Text -> Decoder s DnsName
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s Text
forall s. Decoder s Text
Decode.decodeString)
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size
  Relay -> Decoder s Relay
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Relay
value

parametersFieldCount :: PoolParameters -> Word
parametersFieldCount :: PoolParameters -> Word
parametersFieldCount PoolParameters
value = Word -> (BlsKey -> Word) -> Maybe BlsKey -> Word
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Word
9 (Word -> BlsKey -> Word
forall a b. a -> b -> a
const Word
10) (PoolParameters -> Maybe BlsKey
blsKey PoolParameters
value)

parametersEncoding :: PoolParameters -> Encode.Encoding
parametersEncoding :: PoolParameters -> Encoding
parametersEncoding = SetEncoding -> PoolParameters -> Encoding
parametersEncodingWith SetEncoding
TaggedSet

parametersEncodingWith :: SetEncoding -> PoolParameters -> Encode.Encoding
parametersEncodingWith :: SetEncoding -> PoolParameters -> Encoding
parametersEncodingWith SetEncoding
requested PoolParameters
value =
  ByteString -> Encoding
Encode.encodeBytes (KeyHash StakePool -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes (PoolParameters -> KeyHash StakePool
operator PoolParameters
value))
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (VrfKeyHash -> ByteString
vrfKeyHashBytes (PoolParameters -> VrfKeyHash
vrfKeyHash PoolParameters
value))
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (BlsKey -> Encoding) -> Maybe BlsKey -> Encoding
forall m a. Monoid m => (a -> m) -> Maybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap BlsKey -> Encoding
blsEncoding (PoolParameters -> Maybe BlsKey
blsKey PoolParameters
value)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 (PoolParameters -> Word64
pledge PoolParameters
value)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 (PoolParameters -> Word64
cost PoolParameters
value)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> UnitInterval -> Encoding
Protocol.encodeUnitInterval (PoolParameters -> UnitInterval
margin PoolParameters
value)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (RewardAddress -> ByteString
Reward.toBytes (PoolParameters -> RewardAddress
rewardAccount PoolParameters
value))
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (case SetEncoding
requested of SetEncoding
TaggedSet -> Word -> Encoding
Encode.encodeTag Word
258; SetEncoding
UntaggedSet -> Encoding
forall a. Monoid a => a
mempty)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Set (KeyHash Stake) -> Int
forall a. Set a -> Int
Set.size (PoolParameters -> Set (KeyHash Stake)
owners PoolParameters
value)))
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (KeyHash Stake -> Encoding) -> [KeyHash Stake] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> Encoding
Encode.encodeBytes (ByteString -> Encoding)
-> (KeyHash Stake -> ByteString) -> KeyHash Stake -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyHash Stake -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes) (Set (KeyHash Stake) -> [KeyHash Stake]
forall a. Set a -> [a]
Set.toAscList (PoolParameters -> Set (KeyHash Stake)
owners PoolParameters
value))
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Relay] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (PoolParameters -> [Relay]
relays PoolParameters
value)))
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Relay -> Encoding) -> [Relay] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Relay -> Encoding
relayEncoding (PoolParameters -> [Relay]
relays PoolParameters
value)
    Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (Metadata -> Encoding) -> Maybe Metadata -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
optional Metadata -> Encoding
metadataEncoding (PoolParameters -> Maybe Metadata
metadata PoolParameters
value)

parametersDecoder :: Decode.Decoder s PoolParameters
parametersDecoder :: forall s. Decoder s PoolParameters
parametersDecoder = (Word, PoolParameters) -> PoolParameters
forall a b. (a, b) -> b
snd ((Word, PoolParameters) -> PoolParameters)
-> Decoder s (Word, PoolParameters) -> Decoder s PoolParameters
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Word, PoolParameters)
forall s. Decoder s (Word, PoolParameters)
parametersDecoderWithFieldCount

parametersDecoderWithFieldCount :: Decode.Decoder s (Word, PoolParameters)
parametersDecoderWithFieldCount :: forall s. Decoder s (Word, PoolParameters)
parametersDecoderWithFieldCount = do
  KeyHash StakePool
poolOperator <- (ByteString -> Either Error (KeyHash StakePool))
-> ByteString -> Decoder s (KeyHash StakePool)
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error (KeyHash StakePool)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes (ByteString -> Decoder s (KeyHash StakePool))
-> Decoder s ByteString -> Decoder s (KeyHash StakePool)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  VrfKeyHash
vrf <- (ByteString -> Either Error VrfKeyHash)
-> ByteString -> Decoder s VrfKeyHash
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error VrfKeyHash
vrfKeyHashFromBytes (ByteString -> Decoder s VrfKeyHash)
-> Decoder s ByteString -> Decoder s VrfKeyHash
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  Maybe BlsKey
bls <- case TokenType
token of
    TokenType
Decode.TypeListLen -> BlsKey -> Maybe BlsKey
forall a. a -> Maybe a
Just (BlsKey -> Maybe BlsKey)
-> Decoder s BlsKey -> Decoder s (Maybe BlsKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s BlsKey
forall s. Decoder s BlsKey
blsDecoder
    TokenType
Decode.TypeListLenIndef -> BlsKey -> Maybe BlsKey
forall a. a -> Maybe a
Just (BlsKey -> Maybe BlsKey)
-> Decoder s BlsKey -> Decoder s (Maybe BlsKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s BlsKey
forall s. Decoder s BlsKey
blsDecoder
    TokenType
Decode.TypeNull -> Decoder s ()
forall s. Decoder s ()
Decode.decodeNull Decoder s ()
-> Decoder s (Maybe BlsKey) -> Decoder s (Maybe BlsKey)
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe BlsKey -> Decoder s (Maybe BlsKey)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe BlsKey
forall a. Maybe a
Nothing
    TokenType
_ -> Maybe BlsKey -> Decoder s (Maybe BlsKey)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe BlsKey
forall a. Maybe a
Nothing
  Word64
poolPledge <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
  Word64
poolCost <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
  UnitInterval
poolMargin <- Decoder s UnitInterval
forall s. Decoder s UnitInterval
Protocol.decodeUnitInterval
  RewardAddress
account <- (ByteString -> Either Error RewardAddress)
-> ByteString -> Decoder s RewardAddress
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error RewardAddress
Reward.fromBytes (ByteString -> Decoder s RewardAddress)
-> Decoder s ByteString -> Decoder s RewardAddress
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
  Set (KeyHash Stake)
ownerSet <- Decoder s (Set (KeyHash Stake))
forall s. Decoder s (Set (KeyHash Stake))
ownersDecoder
  [Relay]
relayList <- Decoder s Relay -> Decoder s [Relay]
forall s a. Decoder s a -> Decoder s [a]
listOf Decoder s Relay
forall s. Decoder s Relay
relayDecoder
  Maybe Metadata
reference <- Decoder s Metadata -> Decoder s (Maybe Metadata)
forall s a. Decoder s a -> Decoder s (Maybe a)
nullable Decoder s Metadata
forall s. Decoder s Metadata
metadataDecoder
  let count :: Word
count = case TokenType
token of
        TokenType
Decode.TypeListLen -> Word
10
        TokenType
Decode.TypeListLenIndef -> Word
10
        TokenType
Decode.TypeNull -> Word
10
        TokenType
_ -> Word
9
  (Word, PoolParameters) -> Decoder s (Word, PoolParameters)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word
count, KeyHash StakePool
-> VrfKeyHash
-> Maybe BlsKey
-> Word64
-> Word64
-> UnitInterval
-> RewardAddress
-> Set (KeyHash Stake)
-> [Relay]
-> Maybe Metadata
-> PoolParameters
PoolParameters KeyHash StakePool
poolOperator VrfKeyHash
vrf Maybe BlsKey
bls Word64
poolPledge Word64
poolCost UnitInterval
poolMargin RewardAddress
account Set (KeyHash Stake)
ownerSet [Relay]
relayList Maybe Metadata
reference)

parametersValueEncoding :: PoolParameters -> Encode.Encoding
parametersValueEncoding :: PoolParameters -> Encoding
parametersValueEncoding = SetEncoding -> PoolParameters -> Encoding
parametersValueEncodingWith SetEncoding
TaggedSet

-- | The explicit tag choice applies only to the pool owner set.
parametersValueEncodingWith :: SetEncoding -> PoolParameters -> Encode.Encoding
parametersValueEncodingWith :: SetEncoding -> PoolParameters -> Encoding
parametersValueEncodingWith SetEncoding
requested PoolParameters
value = Word -> Encoding
Encode.encodeListLen (Word -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PoolParameters -> Word
parametersFieldCount PoolParameters
value)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> SetEncoding -> PoolParameters -> Encoding
parametersEncodingWith SetEncoding
requested PoolParameters
value

parametersValueDecoder :: Decode.Decoder s PoolParameters
parametersValueDecoder :: forall s. Decoder s PoolParameters
parametersValueDecoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  (Word
count, PoolParameters
value) <- Decoder s (Word, PoolParameters)
forall s. Decoder s (Word, PoolParameters)
parametersDecoderWithFieldCount
  Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize (Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
count) Maybe Int
size
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size
  PoolParameters -> Decoder s PoolParameters
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PoolParameters
value

ownersDecoder :: Decode.Decoder s (Set (Credential.KeyHash Credential.Stake))
ownersDecoder :: forall s. Decoder s (Set (KeyHash Stake))
ownersDecoder = do
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeTag Bool -> Bool -> Bool
|| TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeTag64) (Decoder s () -> Decoder s ()) -> Decoder s () -> Decoder s ()
forall a b. (a -> b) -> a -> b
$ do
    Word64
tag <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeTag64
    Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word64
tag Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
258) ([Char] -> Decoder s ()
forall a. [Char] -> Decoder s a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Incorrect pool-owner set tag")
  [KeyHash Stake]
values <- Decoder s (KeyHash Stake) -> Decoder s [KeyHash Stake]
forall s a. Decoder s a -> Decoder s [a]
listOf ((ByteString -> Either Error (KeyHash Stake))
-> ByteString -> Decoder s (KeyHash Stake)
forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated ByteString -> Either Error (KeyHash Stake)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes (ByteString -> Decoder s (KeyHash Stake))
-> Decoder s ByteString -> Decoder s (KeyHash Stake)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes)
  let unique :: Set (KeyHash Stake)
unique = [KeyHash Stake] -> Set (KeyHash Stake)
forall a. Ord a => [a] -> Set a
Set.fromList [KeyHash Stake]
values
  Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Set (KeyHash Stake) -> Int
forall a. Set a -> Int
Set.size Set (KeyHash Stake)
unique Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [KeyHash Stake] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [KeyHash Stake]
values) ([Char] -> Decoder s ()
forall a. [Char] -> Decoder s a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Duplicate pool owner")
  Set (KeyHash Stake) -> Decoder s (Set (KeyHash Stake))
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Set (KeyHash Stake)
unique

optional :: (a -> Encode.Encoding) -> Maybe a -> Encode.Encoding
optional :: forall a. (a -> Encoding) -> Maybe a -> Encoding
optional = Encoding -> (a -> Encoding) -> Maybe a -> Encoding
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Encoding
Encode.encodeNull

nullable :: Decode.Decoder s a -> Decode.Decoder s (Maybe a)
nullable :: forall s a. Decoder s a -> Decoder s (Maybe a)
nullable Decoder s a
decoder = do
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  if TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeNull then Decoder s ()
forall s. Decoder s ()
Decode.decodeNull Decoder s () -> Decoder s (Maybe a) -> Decoder s (Maybe a)
forall a b. Decoder s a -> Decoder s b -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe a -> Decoder s (Maybe a)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing else a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> Decoder s a -> Decoder s (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
decoder

listOf :: Decode.Decoder s a -> Decode.Decoder s [a]
listOf :: forall s a. Decoder s a -> Decoder s [a]
listOf Decoder s a
decoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  case Maybe Int
size of
    Just Int
count -> Int -> Decoder s a -> Decoder s [a]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count Decoder s a
decoder
    Maybe Int
Nothing -> Decoder s [a]
go
  where
    go :: Decoder s [a]
go = do
      Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
      if Bool
ended then [a] -> Decoder s [a]
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [] else (:) (a -> [a] -> [a]) -> Decoder s a -> Decoder s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s a
decoder Decoder s ([a] -> [a]) -> Decoder s [a] -> Decoder s [a]
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s [a]
go

arrayOf :: Int -> Decode.Decoder s a -> Decode.Decoder s a
arrayOf :: forall s a. Int -> Decoder s a -> Decoder s a
arrayOf Int
expected Decoder s a
decoder = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  Int -> Maybe Int -> Decoder s ()
forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size
  a
value <- Decoder s a
decoder
  Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size
  a -> Decoder s a
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value

checkSize :: Int -> Maybe Int -> Decode.Decoder s ()
checkSize :: forall s. Int -> Maybe Int -> Decoder s ()
checkSize Int
expected Maybe Int
size = case Maybe Int
size of
  Just Int
actual -> Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
actual Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
expected) ([Char] -> Decoder s ()
forall a. [Char] -> Decoder s a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Incorrect pool array length")
  Maybe Int
Nothing -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

endArray :: Maybe Int -> Decode.Decoder s ()
endArray :: forall s. Maybe Int -> Decoder s ()
endArray Maybe Int
size = case Maybe Int
size of
  Just Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Maybe Int
Nothing -> do
    Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
    Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
ended ([Char] -> Decoder s ()
forall a. [Char] -> Decoder s a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"Extra pool array elements")

validated :: (a -> Either Error b) -> a -> Decode.Decoder s b
validated :: forall a b s. (a -> Either Error b) -> a -> Decoder s b
validated a -> Either Error b
validate a
value = (Error -> Decoder s b)
-> (b -> Decoder s b) -> Either Error b -> Decoder s b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> Decoder s b
forall a. [Char] -> Decoder s a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Decoder s b)
-> (Error -> [Char]) -> Error -> Decoder s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> [Char]
forall a. Show a => a -> [Char]
show) b -> Decoder s b
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either Error b
validate a
value)