haskoin-core-0.20.4: Bitcoin & Bitcoin Cash library for Haskell
CopyrightNo rights reserved
LicenseMIT
Maintainerjprupp@protonmail.ch
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Haskoin.Util

Description

This module defines various utility functions used across the library.

Synopsis

ByteString Helpers

bsToInteger :: ByteString -> Integer Source #

Decode a big endian Integer from a ByteString.

integerToBS :: Integer -> ByteString Source #

Encode an Integer to a ByteString as big endian.

encodeHexLazy :: ByteString -> Text Source #

Encode as string of human-readable hex characters.

decodeHexLazy :: Text -> Maybe ByteString Source #

Decode string of human-readable hex characters.

getBits :: Int -> ByteString -> ByteString Source #

Obtain Int bits from beginning of ByteString. Resulting ByteString will be smallest required to hold that many bits, padded with zeroes to the right.

Maybe & Either Helpers

eitherToMaybe :: Either a b -> Maybe b Source #

Transform an Either value into a Maybe value. Right is mapped to Just and Left is mapped to Nothing. The value inside Left is lost.

maybeToEither :: b -> Maybe a -> Either b a Source #

Transform a Maybe value into an Either value. Just is mapped to Right and Nothing is mapped to Left. Default Left required.

liftEither :: MonadError e m => Either e a -> m a #

Lifts an Either e into any MonadError e.

do { val <- liftEither =<< action1; action2 }

where action1 returns an Either to represent errors.

Since: mtl-2.2.2

liftMaybe :: Monad m => b -> Maybe a -> ExceptT b m a Source #

Lift a Maybe computation into the ExceptT monad.

Other Helpers

updateIndex Source #

Arguments

:: Int

index of the element to change

-> [a]

list of elements

-> (a -> a)

function to apply

-> [a]

result with one element changed

Applies a function to only one element of a list defined by its index. If the index is out of the bounds of the list, the original list is returned.

matchTemplate Source #

Arguments

:: [a]

input list

-> [b]

list to serve as a template

-> (a -> b -> Bool)

comparison function

-> [Maybe a] 

Use the list [b] as a template and try to match the elements of [a] against it. For each element of [b] return the (first) matching element of [a], or Nothing. Output list has same size as [b] and contains results in same order. Elements of [a] can only appear once.

convertBits :: Bool -> Int -> Int -> [Word] -> ([Word], Bool) Source #

Convert from one power-of-two base to another, as long as it fits in a Word.

Triples

fst3 :: (a, b, c) -> a Source #

Returns the first value of a triple.

snd3 :: (a, b, c) -> b Source #

Returns the second value of a triple.

lst3 :: (a, b, c) -> c Source #

Returns the last value of a triple.

JSON Utilities

dropFieldLabel :: Int -> Options Source #

Field label goes lowercase and first n characters get removed.

dropSumLabels :: Int -> Int -> String -> Options Source #

Transformation from dropFieldLabel is applied with argument f, plus constructor tags are lowercased and first c characters removed. tag is used as the name of the object field name that will hold the transformed constructor tag as its value.

Serialization Helpers

putList :: MonadPut m => (a -> m ()) -> [a] -> m () Source #

getList :: MonadGet m => m a -> m [a] Source #

putMaybe :: MonadPut m => (a -> m ()) -> Maybe a -> m () Source #

getMaybe :: MonadGet m => m a -> m (Maybe a) Source #

putInt32be :: MonadPut m => Int32 -> m () Source #

putInt64be :: MonadPut m => Int64 -> m () Source #

getIntMap :: MonadGet m => m Int -> m a -> m (IntMap a) Source #

Read as a list of pairs of int and element.

putIntMap :: MonadPut m => (Int -> m ()) -> (a -> m ()) -> IntMap a -> m () Source #

getTwo :: MonadGet m => m a -> m b -> m (a, b) Source #

putTwo :: MonadPut m => (a -> m ()) -> (b -> m ()) -> (a, b) -> m () Source #