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.
- 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
- data Nil = Nil {
- unNil :: ()
- 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
data Text :: *
A space efficient, packed, unboxed Unicode text type.
IsList Text | |
Eq Text | |
Data Text | This instance preserves data abstraction at the cost of inefficiency. We omit reflection services for the sake of data abstraction. This instance was created by copying the updated behavior of
The original discussion is archived here: could we get a Data instance for Data.Text.Text? The followup discussion that changed the behavior of |
Ord Text | |
Read Text | |
Show Text | |
IsString Text | |
ToJSON Text | |
FromJSON Text | |
Chunk Text | |
Monoid Text | |
FoldCase Text | |
NFData Text | |
Hashable Text | |
Typeable * Text | |
ToJSON v => ToJSON (HashMap Text v) | |
ToJSON v => ToJSON (Map Text v) | |
FromJSON v => FromJSON (HashMap Text v) | |
FromJSON v => FromJSON (Map Text v) | |
type State Text = Buffer | |
type ChunkElem Text = Char | |
type Item Text = Char |
data Vector a :: * -> *
Boxed vectors, supporting efficient slicing.
Alternative Vector | |
Monad Vector | |
Functor Vector | |
MonadPlus Vector | |
Applicative Vector | |
Foldable Vector | |
Traversable Vector | |
Vector Vector a | |
IsList (Vector a) | |
Eq a => Eq (Vector a) | |
Data a => Data (Vector a) | |
Ord a => Ord (Vector a) | |
Read a => Read (Vector a) | |
Show a => Show (Vector a) | |
ToJSON a => ToJSON (Vector a) | |
FromJSON a => FromJSON (Vector a) | |
Monoid (Vector a) | |
NFData a => NFData (Vector a) | |
Typeable (* -> *) Vector | |
type Mutable Vector = MVector | |
type Item (Vector a) = a |
class FromJSON a where
A type that can be converted from JSON, with the possibility of failure.
When writing an instance, use empty
, mzero
, or fail
to make a
conversion fail, e.g. if an Object
is missing a required key, or
the value is of the wrong type.
An example type and instance:
@{-# LANGUAGE OverloadedStrings #-}
data Coord = Coord { x :: Double, y :: Double }
instance FromJSON Coord where
parseJSON (Object
v) = Coord <$>
v .:
"x" <*>
v .:
"y"
-- A non-Object
value is of the wrong type, so use mzero
to fail.
parseJSON _ = mzero
@
Note the use of the OverloadedStrings
language extension which enables
Text
values to be written as string literals.
Instead of manually writing your FromJSON
instance, there are three 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 will probably be more efficient than the following two options:
- Data.Aeson.Generic provides a generic
fromJSON
function that parses to any type which is an instance ofData
. - If your compiler has support for the
DeriveGeneric
andDefaultSignatures
language extensions,parseJSON
will have a default generic implementation.
To use this, 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 } deriving Generic
instance FromJSON Coord @
Note that, instead of using DefaultSignatures
, it's also possible
to parameterize the generic decoding using genericParseJSON
applied
to your encoding/decoding Options
:
instance FromJSON Coord where parseJSON =genericParseJSON
defaultOptions
Nothing
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.
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.
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.
Eq a => Eq (BitcoinRpcResponse a) | |
Ord a => Ord (BitcoinRpcResponse a) | |
Read a => Read (BitcoinRpcResponse a) | |
Show a => Show (BitcoinRpcResponse a) | |
FromJSON a => FromJSON (BitcoinRpcResponse a) |