module Cometa.PolicyId
( PolicyId,
fromBytes,
toBytes,
fromHex,
toHex,
fromScriptHash,
toScriptHash,
)
where
import qualified Cometa.Credential as Credential
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)
newtype PolicyId = PolicyId Credential.ScriptHash deriving (PolicyId -> PolicyId -> Bool
(PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool) -> Eq PolicyId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PolicyId -> PolicyId -> Bool
== :: PolicyId -> PolicyId -> Bool
$c/= :: PolicyId -> PolicyId -> Bool
/= :: PolicyId -> PolicyId -> Bool
Eq, Eq PolicyId
Eq PolicyId =>
(PolicyId -> PolicyId -> Ordering)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> PolicyId)
-> (PolicyId -> PolicyId -> PolicyId)
-> Ord PolicyId
PolicyId -> PolicyId -> Bool
PolicyId -> PolicyId -> Ordering
PolicyId -> PolicyId -> PolicyId
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 :: PolicyId -> PolicyId -> Ordering
compare :: PolicyId -> PolicyId -> Ordering
$c< :: PolicyId -> PolicyId -> Bool
< :: PolicyId -> PolicyId -> Bool
$c<= :: PolicyId -> PolicyId -> Bool
<= :: PolicyId -> PolicyId -> Bool
$c> :: PolicyId -> PolicyId -> Bool
> :: PolicyId -> PolicyId -> Bool
$c>= :: PolicyId -> PolicyId -> Bool
>= :: PolicyId -> PolicyId -> Bool
$cmax :: PolicyId -> PolicyId -> PolicyId
max :: PolicyId -> PolicyId -> PolicyId
$cmin :: PolicyId -> PolicyId -> PolicyId
min :: PolicyId -> PolicyId -> PolicyId
Ord, Int -> PolicyId -> ShowS
[PolicyId] -> ShowS
PolicyId -> String
(Int -> PolicyId -> ShowS)
-> (PolicyId -> String) -> ([PolicyId] -> ShowS) -> Show PolicyId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PolicyId -> ShowS
showsPrec :: Int -> PolicyId -> ShowS
$cshow :: PolicyId -> String
show :: PolicyId -> String
$cshowList :: [PolicyId] -> ShowS
showList :: [PolicyId] -> ShowS
Show)
fromBytes :: ByteString -> Either Error PolicyId
fromBytes :: ByteString -> Either Error PolicyId
fromBytes ByteString
bytes
| ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
28 = Error -> Either Error PolicyId
forall a b. a -> Either a b
Left (Int -> Error
InvalidPolicyIdLength (ByteString -> Int
ByteString.length ByteString
bytes))
| Bool
otherwise = ScriptHash -> PolicyId
PolicyId (ScriptHash -> PolicyId)
-> Either Error ScriptHash -> Either Error PolicyId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes ByteString
bytes
toBytes :: PolicyId -> ByteString
toBytes :: PolicyId -> ByteString
toBytes = ScriptHash -> ByteString
Credential.scriptHashBytes (ScriptHash -> ByteString)
-> (PolicyId -> ScriptHash) -> PolicyId -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PolicyId -> ScriptHash
toScriptHash
fromHex :: Text -> Either Error PolicyId
fromHex :: Text -> Either Error PolicyId
fromHex Text
text = Either Error PolicyId
-> (ByteString -> Either Error PolicyId)
-> Maybe ByteString
-> Either Error PolicyId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error PolicyId
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error PolicyId
fromBytes (Text -> Maybe ByteString
Hex.decodeHex Text
text)
toHex :: PolicyId -> Text
toHex :: PolicyId -> Text
toHex = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> (PolicyId -> ByteString) -> PolicyId -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PolicyId -> ByteString
toBytes
fromScriptHash :: Credential.ScriptHash -> PolicyId
fromScriptHash :: ScriptHash -> PolicyId
fromScriptHash = ScriptHash -> PolicyId
PolicyId
toScriptHash :: PolicyId -> Credential.ScriptHash
toScriptHash :: PolicyId -> ScriptHash
toScriptHash (PolicyId ScriptHash
scriptHash) = ScriptHash
scriptHash