{-# LANGUAGE DerivingVia #-}

-- | Identifier or Head participants on-chain. That is, a participant in the
-- Hydra Head protocol which authorizes protocol transitions on-chain.
module Hydra.Tx.OnChainId where

import Hydra.Prelude

import Test.QuickCheck (vectorOf)

import Data.ByteString qualified as BS
import Hydra.Cardano.Api (
  HasTypeProxy (..),
  SerialiseAsRawBytes (..),
  UsingRawBytesHex (..),
 )

-- | Identifier for a Hydra Head participant on-chain.
newtype OnChainId = UnsafeOnChainId ByteString
  deriving stock (Int -> OnChainId -> ShowS
[OnChainId] -> ShowS
OnChainId -> String
(Int -> OnChainId -> ShowS)
-> (OnChainId -> String)
-> ([OnChainId] -> ShowS)
-> Show OnChainId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OnChainId -> ShowS
showsPrec :: Int -> OnChainId -> ShowS
$cshow :: OnChainId -> String
show :: OnChainId -> String
$cshowList :: [OnChainId] -> ShowS
showList :: [OnChainId] -> ShowS
Show, OnChainId -> OnChainId -> Bool
(OnChainId -> OnChainId -> Bool)
-> (OnChainId -> OnChainId -> Bool) -> Eq OnChainId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OnChainId -> OnChainId -> Bool
== :: OnChainId -> OnChainId -> Bool
$c/= :: OnChainId -> OnChainId -> Bool
/= :: OnChainId -> OnChainId -> Bool
Eq, Eq OnChainId
Eq OnChainId =>
(OnChainId -> OnChainId -> Ordering)
-> (OnChainId -> OnChainId -> Bool)
-> (OnChainId -> OnChainId -> Bool)
-> (OnChainId -> OnChainId -> Bool)
-> (OnChainId -> OnChainId -> Bool)
-> (OnChainId -> OnChainId -> OnChainId)
-> (OnChainId -> OnChainId -> OnChainId)
-> Ord OnChainId
OnChainId -> OnChainId -> Bool
OnChainId -> OnChainId -> Ordering
OnChainId -> OnChainId -> OnChainId
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 :: OnChainId -> OnChainId -> Ordering
compare :: OnChainId -> OnChainId -> Ordering
$c< :: OnChainId -> OnChainId -> Bool
< :: OnChainId -> OnChainId -> Bool
$c<= :: OnChainId -> OnChainId -> Bool
<= :: OnChainId -> OnChainId -> Bool
$c> :: OnChainId -> OnChainId -> Bool
> :: OnChainId -> OnChainId -> Bool
$c>= :: OnChainId -> OnChainId -> Bool
>= :: OnChainId -> OnChainId -> Bool
$cmax :: OnChainId -> OnChainId -> OnChainId
max :: OnChainId -> OnChainId -> OnChainId
$cmin :: OnChainId -> OnChainId -> OnChainId
min :: OnChainId -> OnChainId -> OnChainId
Ord, (forall x. OnChainId -> Rep OnChainId x)
-> (forall x. Rep OnChainId x -> OnChainId) -> Generic OnChainId
forall x. Rep OnChainId x -> OnChainId
forall x. OnChainId -> Rep OnChainId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OnChainId -> Rep OnChainId x
from :: forall x. OnChainId -> Rep OnChainId x
$cto :: forall x. Rep OnChainId x -> OnChainId
to :: forall x. Rep OnChainId x -> OnChainId
Generic)
  deriving ([OnChainId] -> Value
[OnChainId] -> Encoding
OnChainId -> Bool
OnChainId -> Value
OnChainId -> Encoding
(OnChainId -> Value)
-> (OnChainId -> Encoding)
-> ([OnChainId] -> Value)
-> ([OnChainId] -> Encoding)
-> (OnChainId -> Bool)
-> ToJSON OnChainId
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: OnChainId -> Value
toJSON :: OnChainId -> Value
$ctoEncoding :: OnChainId -> Encoding
toEncoding :: OnChainId -> Encoding
$ctoJSONList :: [OnChainId] -> Value
toJSONList :: [OnChainId] -> Value
$ctoEncodingList :: [OnChainId] -> Encoding
toEncodingList :: [OnChainId] -> Encoding
$comitField :: OnChainId -> Bool
omitField :: OnChainId -> Bool
ToJSON, Maybe OnChainId
Value -> Parser [OnChainId]
Value -> Parser OnChainId
(Value -> Parser OnChainId)
-> (Value -> Parser [OnChainId])
-> Maybe OnChainId
-> FromJSON OnChainId
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser OnChainId
parseJSON :: Value -> Parser OnChainId
$cparseJSONList :: Value -> Parser [OnChainId]
parseJSONList :: Value -> Parser [OnChainId]
$comittedField :: Maybe OnChainId
omittedField :: Maybe OnChainId
FromJSON) via (UsingRawBytesHex OnChainId)

instance SerialiseAsRawBytes OnChainId where
  serialiseToRawBytes :: OnChainId -> ByteString
serialiseToRawBytes (UnsafeOnChainId ByteString
bytes) = ByteString
bytes
  deserialiseFromRawBytes :: AsType OnChainId
-> ByteString -> Either SerialiseAsRawBytesError OnChainId
deserialiseFromRawBytes AsType OnChainId
_ = OnChainId -> Either SerialiseAsRawBytesError OnChainId
forall a b. b -> Either a b
Right (OnChainId -> Either SerialiseAsRawBytesError OnChainId)
-> (ByteString -> OnChainId)
-> ByteString
-> Either SerialiseAsRawBytesError OnChainId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> OnChainId
UnsafeOnChainId

instance HasTypeProxy OnChainId where
  data AsType OnChainId = AsOnChainId
  proxyToAsType :: Proxy OnChainId -> AsType OnChainId
proxyToAsType Proxy OnChainId
_ = AsType OnChainId
AsOnChainId

instance Arbitrary OnChainId where
  arbitrary :: Gen OnChainId
arbitrary = Gen OnChainId
genOnChainId

-- | Generate an arbitrary 'OnChainId' of 28 bytes length.
genOnChainId :: Gen OnChainId
genOnChainId :: Gen OnChainId
genOnChainId = ByteString -> OnChainId
UnsafeOnChainId (ByteString -> OnChainId)
-> ([Word8] -> ByteString) -> [Word8] -> OnChainId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
BS.pack ([Word8] -> OnChainId) -> Gen [Word8] -> Gen OnChainId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Gen Word8 -> Gen [Word8]
forall a. Int -> Gen a -> Gen [a]
vectorOf Int
28 Gen Word8
forall a. Arbitrary a => Gen a
arbitrary