haskoin-core-0.3.0: Implementation of the core Bitcoin protocol features.

Safe HaskellNone
LanguageHaskell98

Network.Haskoin.Util

Contents

Description

This module defines various utility functions used across the Network.Haskoin modules.

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

decodeHex :: ByteString -> Maybe ByteString Source

Decode hexadecimal ByteString. This function can fail if the string contains invalid hexadecimal (0-9, a-f, A-F) characters

Data.Binary helpers

encode' :: Binary a => a -> ByteString Source

Strict version of encode

decode' :: Binary a => ByteString -> a Source

Strict version of decode

runPut' :: Put -> ByteString Source

Strict version of runPut

runGet' :: Binary a => Get a -> ByteString -> a Source

Strict version of runGet

fromDecode Source

Arguments

:: Binary a 
=> ByteString

The bytestring to decode

-> b

Default value to return when decoding fails

-> (a -> b)

Function to apply when decoding succeeds

-> b

Final result

Try to decode a Binary value. If decoding succeeds, apply the function to the result. Otherwise, return the default value.

fromRunGet Source

Arguments

:: Binary a 
=> Get a

The Get monad to run

-> ByteString

The bytestring to decode

-> b

Default value to return when decoding fails

-> (a -> b)

Function to apply when decoding succeeds

-> b

Final result

Try to run a Get monad. If decoding succeeds, apply a function to the result. Otherwise, return the default value.

decodeToEither :: Binary a => ByteString -> Either String a Source

Decode a Binary value. A Right value is returned with the result upon success. Otherwise a Left value with the error message is returned.

decodeToMaybe :: Binary a => ByteString -> Maybe a Source

Decode a Binary value. A Just value is returned with the result upon success. Otherwise, Nothing is returned.

isolate :: Binary a => Int -> Get a -> Get a Source

Isolate a Get monad for the next Int bytes. Only the next Int bytes of the input ByteString will be available for the Get monad to consume. This function will fail if the Get monad fails or some of the input is not consumed.

Maybe and Either monad helpers

isLeft :: Either a b -> Bool Source

Returns True if the Either value is Left

isRight :: Either a b -> Bool Source

Returns True if the Either value is Right

fromRight :: Either a b -> b Source

Extract the Right value from an Either value. Fails if the value is Left

fromLeft :: Either a b -> a Source

Extract the Left value from an Either value. Fails if the value is Right

eitherToMaybe :: Either a b -> Maybe b Source

Transforms 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

Transforms a Maybe value into an Either value. Just is mapped to Right and Nothing is mapped to Left. You also pass in an error value in case Left is returned.

liftEither :: Monad m => Either b a -> EitherT b m a Source

Lift a Either computation into the EitherT monad

liftMaybe :: Monad m => b -> Maybe a -> EitherT b m a Source

Lift a Maybe computation into the EitherT monad

Various helpers

updateIndex Source

Arguments

:: Int

The index of the element to change

-> [a]

The list of elements

-> (a -> a)

The function to apply

-> [a]

The 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]

The input list

-> [b]

The list to serve as a template

-> (a -> b -> Bool)

The comparison function

-> [Maybe a]

Results of the template matching

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.

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.

MonadState

modify' :: MonadState s m => (s -> s) -> m () Source

Strict evaluation of the new state

JSON Utilities