Safe Haskell | None |
---|---|
Language | Haskell98 |
The API exposed in this module should be considered unstable, and is subject to change between minor revisions.
If the version number is a.b.c.d, and either a or b changes, then the module's whole API may have changed (if only b changes, then it was probably a minor change).
If c changed, then only the internal API may change. The rest of the module is guaranteed to be stable.
If only d changes, then there were no user-facing code changes made.
Synopsis
- module Network.Bitcoin.Types
- data Text
- data Vector a
- class FromJSON a where
- callApi :: FromJSON v => Client -> Text -> [Value] -> IO v
- getClient :: String -> ByteString -> ByteString -> IO Client
- newtype Nil = Nil {
- unNil :: ()
- newtype NilOrArray = NilOrArray {}
- tj :: ToJSON a => a -> Value
- tjm :: ToJSON a => a -> Maybe a -> Value
- tja :: ToJSON a => Maybe a -> [Value]
- newtype AddrAddress = AA (Vector (Address, BTC))
- data BitcoinRpcResponse a = BitcoinRpcResponse {}
Documentation
module Network.Bitcoin.Types
A space efficient, packed, unboxed Unicode text type.
Instances
Hashable Text | |
Defined in Data.Hashable.Class | |
ToJSON Text | |
Defined in Data.Aeson.Types.ToJSON | |
KeyValue Object | Constructs a singleton |
KeyValue Pair | |
ToJSONKey Text | |
Defined in Data.Aeson.Types.ToJSON | |
FromJSON Text | |
FromJSONKey Text | |
Defined in Data.Aeson.Types.FromJSON | |
Chunk Text | |
Defined in Data.Attoparsec.Internal.Types | |
FoldCase Text | |
Defined in Data.CaseInsensitive.Internal | |
FromPairs Value (DList Pair) | |
Defined in Data.Aeson.Types.ToJSON | |
v ~ Value => KeyValuePair v (DList Pair) | |
Defined in Data.Aeson.Types.ToJSON | |
type State Text | |
Defined in Data.Attoparsec.Internal.Types | |
type ChunkElem Text | |
Defined in Data.Attoparsec.Internal.Types | |
type Item Text | |
Boxed vectors, supporting efficient slicing.
Instances
Monad Vector | |
Functor Vector | |
MonadFail Vector | |
Defined in Data.Vector | |
Applicative Vector | |
Foldable Vector | |
Defined in Data.Vector fold :: Monoid m => Vector m -> m # foldMap :: Monoid m => (a -> m) -> Vector a -> m # foldr :: (a -> b -> b) -> b -> Vector a -> b # foldr' :: (a -> b -> b) -> b -> Vector a -> b # foldl :: (b -> a -> b) -> b -> Vector a -> b # foldl' :: (b -> a -> b) -> b -> Vector a -> b # foldr1 :: (a -> a -> a) -> Vector a -> a # foldl1 :: (a -> a -> a) -> Vector a -> a # elem :: Eq a => a -> Vector a -> Bool # maximum :: Ord a => Vector a -> a # minimum :: Ord a => Vector a -> a # | |
Traversable Vector | |
ToJSON1 Vector | |
Defined in Data.Aeson.Types.ToJSON liftToJSON :: (a -> Value) -> ([a] -> Value) -> Vector a -> Value # liftToJSONList :: (a -> Value) -> ([a] -> Value) -> [Vector a] -> Value # liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> Vector a -> Encoding # liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [Vector a] -> Encoding # | |
FromJSON1 Vector | |
Alternative Vector | |
MonadPlus Vector | |
Eq1 Vector | |
Ord1 Vector | |
Defined in Data.Vector | |
Read1 Vector | |
Defined in Data.Vector | |
Show1 Vector | |
MonadZip Vector | |
Vector Vector a | |
Defined in Data.Vector basicUnsafeFreeze :: PrimMonad m => Mutable Vector (PrimState m) a -> m (Vector a) # basicUnsafeThaw :: PrimMonad m => Vector a -> m (Mutable Vector (PrimState m) a) # basicLength :: Vector a -> Int # basicUnsafeSlice :: Int -> Int -> Vector a -> Vector a # basicUnsafeIndexM :: Monad m => Vector a -> Int -> m a # basicUnsafeCopy :: PrimMonad m => Mutable Vector (PrimState m) a -> Vector a -> m () # | |
IsList (Vector a) | |
Eq a => Eq (Vector a) | |
Data a => Data (Vector a) | |
Defined in Data.Vector gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Vector a -> c (Vector a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Vector a) # toConstr :: Vector a -> Constr # dataTypeOf :: Vector a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Vector a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Vector a)) # gmapT :: (forall b. Data b => b -> b) -> Vector a -> Vector a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Vector a -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Vector a -> r # gmapQ :: (forall d. Data d => d -> u) -> Vector a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Vector a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Vector a -> m (Vector a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Vector a -> m (Vector a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Vector a -> m (Vector a) # | |
Ord a => Ord (Vector a) | |
Defined in Data.Vector | |
Read a => Read (Vector a) | |
Show a => Show (Vector a) | |
Semigroup (Vector a) | |
Monoid (Vector a) | |
ToJSON a => ToJSON (Vector a) | |
Defined in Data.Aeson.Types.ToJSON | |
FromJSON a => FromJSON (Vector a) | |
NFData a => NFData (Vector a) | |
Defined in Data.Vector | |
type Mutable Vector | |
Defined in Data.Vector | |
type Item (Vector a) | |
Defined in Data.Vector |
A type that can be converted from JSON, with the possibility of failure.
In many cases, you can get the compiler to generate parsing code for you (see below). To begin, let's cover writing an instance by hand.
There are various reasons a conversion could fail. For example, an
Object
could be missing a required key, an Array
could be of
the wrong size, or a value could be of an incompatible type.
The basic ways to signal a failed conversion are as follows:
fail
yields a custom error message: it is the recommended way of reporting a failure;empty
(ormzero
) is uninformative: use it when the error is meant to be caught by some(
;<|>
)typeMismatch
can be used to report a failure when the encountered value is not of the expected JSON type;unexpected
is an appropriate alternative when more than one type may be expected, or to keep the expected type implicit.
prependFailure
(or modifyFailure
) add more information to a parser's
error messages.
An example type and instance using typeMismatch
and prependFailure
:
-- Allow ourselves to writeText
literals. {-# LANGUAGE OverloadedStrings #-} data Coord = Coord { x :: Double, y :: Double } instanceFromJSON
Coord whereparseJSON
(Object
v) = Coord<$>
v.:
"x"<*>
v.:
"y" -- We do not expect a non-Object
value here. -- We could useempty
to fail, buttypeMismatch
-- gives a much more informative error message.parseJSON
invalid =prependFailure
"parsing Coord failed, " (typeMismatch
"Object" invalid)
For this common case of only being concerned with a single
type of JSON value, the functions withObject
, withScientific
, etc.
are provided. Their use is to be preferred when possible, since
they are more terse. Using withObject
, we can rewrite the above instance
(assuming the same language extension and data type) as:
instanceFromJSON
Coord whereparseJSON
=withObject
"Coord" $ \v -> Coord<$>
v.:
"x"<*>
v.:
"y"
Instead of manually writing your FromJSON
instance, there are two options
to do it automatically:
- Data.Aeson.TH provides Template Haskell functions which will derive an instance at compile time. The generated instance is optimized for your type so it will probably be more efficient than the following option.
- The compiler can provide a default generic implementation for
parseJSON
.
To use the second, simply add a deriving
clause to your
datatype and declare a Generic
FromJSON
instance for your datatype without giving
a definition for parseJSON
.
For example, the previous example can be simplified to just:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics data Coord = Coord { x :: Double, y :: Double } derivingGeneric
instanceFromJSON
Coord
The default implementation will be equivalent to
parseJSON =
; if you need different
options, you can customize the generic decoding by defining:genericParseJSON
defaultOptions
customOptions =defaultOptions
{fieldLabelModifier
=map
toUpper
} instanceFromJSON
Coord whereparseJSON
=genericParseJSON
customOptions
Nothing
Instances
callApi
is a low-level interface for making authenticated API
calls to a Bitcoin daemon. The first argument specifies
rpc client details (URL, username, password)
The second argument is the command name. The third argument provides parameters for the API call.
genHash = do
client <- getClient "http://127.0.0.1:8332" "user" "password" callApi client "getblockhash" [tj 0]
On error, throws a BitcoinException
.
getClient :: String -> ByteString -> ByteString -> IO Client Source #
getClient
takes a url, rpc username, and rpc password
and returns a Client that can be used to make API calls. Each
Client encloses a Manager (from http-client) that re-uses
connections for requests, so long as the same Client is
is used for each call.
Used to allow "null" to decode to a tuple.
newtype NilOrArray Source #
Used to parse "null" or [HexString]
Instances
FromJSON NilOrArray Source # | |
Defined in Network.Bitcoin.Internal parseJSON :: Value -> Parser NilOrArray # parseJSONList :: Value -> Parser [NilOrArray] # |
newtype AddrAddress Source #
A wrapper for a vector of address:amount pairs. The RPC expects that as an object of "address":"amount" pairs, instead of a vector. So that's what we give them with AddrAddress's ToJSON.
Instances
ToJSON AddrAddress Source # | |
Defined in Network.Bitcoin.Internal toJSON :: AddrAddress -> Value # toEncoding :: AddrAddress -> Encoding # toJSONList :: [AddrAddress] -> Value # toEncodingList :: [AddrAddress] -> Encoding # |
data BitcoinRpcResponse a Source #
A response from bitcoind will contain the result of the JSON-RPC call, and an error. The error should be null if a valid response was received.