module Cometa.Pool.Metadata
( Url,
mkUrl,
urlText,
MetadataHash,
metadataHashFromBytes,
metadataHashBytes,
Metadata (..),
hashData,
encodeCbor,
decodeCbor,
)
where
import qualified Cometa.Crypto.Blake2b as Blake2b
import Cometa.Error (Error)
import Cometa.Internal.Pool (Metadata (..), MetadataHash, Url, metadataHashBytes, metadataHashFromBytes, mkUrl, urlText)
import qualified Cometa.Internal.Pool as Internal
import Data.ByteString (ByteString)
encodeCbor :: Metadata -> ByteString
encodeCbor :: Metadata -> ByteString
encodeCbor = (Metadata -> Encoding) -> Metadata -> ByteString
forall a. (a -> Encoding) -> a -> ByteString
Internal.encode Metadata -> Encoding
Internal.metadataEncoding
decodeCbor :: ByteString -> Either Error Metadata
decodeCbor :: ByteString -> Either Error Metadata
decodeCbor = (forall s. Decoder s Metadata)
-> ByteString -> Either Error Metadata
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Internal.decodeComplete Decoder s Metadata
forall s. Decoder s Metadata
Internal.metadataDecoder
hashData :: ByteString -> IO (Either Error MetadataHash)
hashData :: ByteString -> IO (Either Error MetadataHash)
hashData ByteString
bytes = do
Either Error Blake2bHash
result <- Int -> ByteString -> IO (Either Error Blake2bHash)
Blake2b.compute Int
32 ByteString
bytes
Either Error MetadataHash -> IO (Either Error MetadataHash)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Blake2bHash
result Either Error Blake2bHash
-> (Blake2bHash -> Either Error MetadataHash)
-> Either Error MetadataHash
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error MetadataHash
metadataHashFromBytes (ByteString -> Either Error MetadataHash)
-> (Blake2bHash -> ByteString)
-> Blake2bHash
-> Either Error MetadataHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Blake2bHash -> ByteString
Blake2b.toBytes)