haskoin-store-data: Data types and API client of Haskoin Store

[ bitcoin, finance, library, mit, network ] [ Propose Tags ] [ Report a vulnerability ]

The types of the data that Haskoin Store serves (blocks, transactions, balances, unspent outputs, extended public keys, health checks, events and the Blockchain.info compatible formats), with their JSON, and a client for its REST API. Use it to talk to a Haskoin Store server from Haskell.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.25.0, 0.25.1, 0.25.2, 0.25.3, 0.25.4, 0.26.0, 0.26.1, 0.26.2, 0.26.3, 0.26.4, 0.26.5, 0.26.6, 0.27.0, 0.28.0, 0.29.0, 0.29.1, 0.29.3, 0.30.0, 0.30.1, 0.31.0, 0.32.0, 0.32.1, 0.32.2, 0.32.3, 0.33.0, 0.33.1, 0.34.0, 0.34.1, 0.34.2, 0.34.3, 0.34.4, 0.34.5, 0.34.6, 0.34.7, 0.35.0, 0.35.1, 0.35.2, 0.36.0, 0.36.2, 0.36.4, 0.37.0, 0.37.1, 0.37.2, 0.37.3, 0.37.4, 0.37.5, 0.38.0, 0.38.1, 0.40.0, 0.40.2, 0.41.0, 0.41.3, 0.42.1, 0.43.0, 0.44.0, 0.45.0, 0.46.1, 0.46.2, 0.46.3, 0.46.4, 0.46.6, 0.47.0, 0.47.1, 0.47.2, 0.47.3, 0.47.4, 0.48.0, 0.49.0, 0.50.1, 0.51.0, 0.52.0, 0.52.5, 0.52.6, 0.52.9, 0.52.10, 0.52.11, 0.52.12, 0.52.13, 0.53.0, 0.53.1, 0.53.2, 0.53.3, 0.53.4, 0.53.5, 0.53.6, 0.53.7, 0.53.8, 0.53.9, 0.53.10, 0.53.11, 0.54.0, 0.55.0, 0.56.0, 0.57.0, 0.58.0, 0.59.0, 0.60.0, 0.61.0, 0.61.1, 0.62.0, 0.62.1, 0.63.0, 0.64.0, 0.64.1, 0.64.2, 0.64.3, 0.64.4, 0.64.5, 0.64.6, 0.64.7, 0.64.8, 0.64.9, 0.64.10, 0.64.11, 0.64.12, 0.64.13, 0.64.14, 0.64.15, 0.64.16, 0.64.17, 0.64.18, 0.64.19, 0.65.0, 0.65.1, 0.65.2, 0.65.5, 0.65.11, 0.65.12, 1.0.0, 1.0.1, 1.0.2, 1.1.0, 1.2.0, 1.2.2, 1.2.5, 1.3.0, 1.4.0, 1.5.16, 1.6.0, 1.6.3, 1.7.0, 1.8.0, 1.8.1, 1.9.0, 1.10.0, 1.11.0, 2.0.0, 2.0.1, 2.0.2, 3.0.0, 3.0.1, 4.0.0
Change log CHANGELOG.md
Dependencies aeson (>=2.2.5.1 && <2.3), base (>=4.21.2.0 && <4.22), binary (>=0.8.9.3 && <0.9), bytes (>=0.17.5 && <0.18), bytestring (>=0.12.2.0 && <0.13), cereal (>=0.5.8.3 && <0.6), containers (>=0.7 && <0.8), data-default (>=0.8.0.2 && <0.9), deepseq (>=1.5.1.0 && <1.6), hashable (>=1.5.1.0 && <1.6), haskoin-core (>=3.0.0 && <3.1), http-client (>=0.7.19 && <0.8), http-types (>=0.12.6 && <0.13), lens (>=5.3.6 && <5.4), network (>=3.2.12.0 && <3.3), string-conversions (>=0.4.0.1 && <0.5), text (>=2.1.4 && <2.2), time (>=1.14 && <1.15), unordered-containers (>=0.2.21 && <0.3), vector (>=0.13.2.0 && <0.14), wreq (>=0.5.4.5 && <0.6) [details]
Tested with ghc ==9.12.4
License MIT
Copyright (c) Haskoin Developers
Author JP Rupp
Maintainer jprupp@protonmail.ch
Uploaded by jprupp at 2026-11-25T23:47:01Z
Category Bitcoin, Finance, Network
Home page https://github.com/jprupp/haskoin-store-data#readme
Bug tracker https://github.com/jprupp/haskoin-store-data/issues
Source repo head: git clone https://github.com/jprupp/haskoin-store-data
Distributions LTSHaskell:1.5.16, Stackage:4.0.0
Reverse Dependencies 2 direct, 1 indirect [details]
Downloads 66438 total (287 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for haskoin-store-data-4.0.0

[back to package description]

haskoin-store-data

Data types and API client of Haskoin Store, an indexer for Bitcoin and Bitcoin Cash with a REST API.

Use this package to talk to a Haskoin Store server from Haskell. It has no database or network node of its own, so it is small to depend on.

Modules

  • Haskoin.Store.Data: the types that the server answers with (blocks, transactions, balances, unspent outputs, extended public keys, health checks, events, and the Blockchain.info compatible formats), with their JSON. The types that the server keeps in its database also have a binary encoding, for the server's own use.
  • Haskoin.Store.WebCommon: the requests of the API, one type per endpoint, with their paths and parameters.
  • Haskoin.Store.WebClient: apiCall, which sends a request and reads the answer, and apiEvents, which listens to the events of a server.

Example

import Data.Default (def)
import Haskoin (btc, textToAddr)
import Haskoin.Store.WebClient

main :: IO ()
main = do
  let cfg = def {net = btc} -- https://api.haskoin.com/btc/...
      Just addr = textToAddr btc "bc1q…"
  r <- apiCall cfg (GetAddrBalance addr)
  print r

The client appends the name of the network to the host, as in the layout of api.haskoin.com. The API speaks JSON, and so does the client.

A store on the same machine may listen on a Unix socket (--socket). Give its path in socket, and every request goes to it, whatever host the URL names:

let cfg = def {net = btc, host = "http://localhost/", socket = Just "/run/haskoin-store/web.sock"}

Tokens of Bitcoin Cash

On Bitcoin Cash an output can hold tokens (CashTokens), written at the front of its script field. scriptTokens net script reads them from the script of a StoreOutput, a StoreInput or an Unspent, which have no field of their own for them. Their JSON has a token field on the Bitcoin Cash networks: the category, the fungible amount as a string, and the non-fungible token, or null. On Bitcoin the field is not there.

The requests of the token endpoints of haskoin-store 2.0.0:

Request Endpoint Answer
GetAddrTokens address /address/:address/tokens a TokenBalance per category that the address holds
GetTokenHolders category limit offset /token/:category/holders a TokenBalance per address that holds the category
GetTokenUnspent category limits /token/:category/unspent the Unspent outputs that hold it, newest first
GetTokenTxs category limits /token/:category/transactions the TxRefs that moved it, newest first

A TokenBalance counts the mempool, as a Balance does: confirmedTokens is what blocks hold and the mempool does not spend, and unconfirmedTokens what the mempool adds, so their sum is what the address holds now. nfts counts its non-fungible tokens, tokenUtxo the unspent outputs that hold the category, tokenTxs the transactions that moved it to or from the address, and receivedTokens every fungible token it ever received. A category is the TxHash of the transaction whose output 0 the first transaction of the tokens spent. On Bitcoin the server answers these requests with empty lists.

{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

import Data.Default (def)
import Haskoin (bch, textToAddr)
import Haskoin.Store.Data (SerialList (..), TokenBalance (..), Unspent (..), scriptTokens)
import Haskoin.Store.WebClient

main :: IO ()
main = do
  let cfg = def {net = bch}
      Just addr = textToAddr bch "bitcoincash:qz…"
  Right (SerialList balances) <- apiCall cfg (GetAddrTokens addr)
  mapM_ (\b -> print (b.category, b.confirmedTokens + b.unconfirmedTokens)) balances
  -- the outputs of the first category, with the tokens of each
  case balances of
    b : _ -> do
      let limits = LimitsParam Nothing (OffsetParam 0) Nothing
      Right (SerialList outs) <- apiCall cfg (GetTokenUnspent b.category limits)
      mapM_ (\u -> print (u.outpoint, scriptTokens bch u.script)) outs
    [] -> return ()

Errors come back as Left an Except.

Events

apiEvents calls a function with each new block and each transaction that enters or leaves the mempool, for as long as the function answers True. After a lost connection, apiEventsAfter takes up from the last event heard, so nothing is missed; if the server no longer has that event it answers ThingNotFound, and the client reads what it follows again.

Over the WebSocket at /events a client can also choose the transactions it hears about. EventCommand is what it sends: addresses to watch, extended public keys (one command for each type of address), or a Bloom filter as in BIP 37, each a JSON object sent in a text frame. This package has no WebSocket client of its own; send the command with the one you use:

import Haskoin (marshalJSON)
import Haskoin.Store.Data (EventCommand (..))

-- a text frame: {"type":"watch","addresses":["bc1q…"]}
frame = marshalJSON net (EventWatch [addr])

Blocks are always told. A connection that has sent no command hears of every transaction, unless its URL says what to watch, or quiet=true for blocks alone.

The JSON of every type, for someone who writes a client in another language, is in swagger.yaml in the haskoin-store repository.

Building

The package builds with Stack and needs libsecp256k1 (libsecp256k1-dev on Debian and Ubuntu), or Nix with stack --nix:

stack build
stack test

These commands build the package as it is released, with optimisation. For the time between two commits there is a faster loop: haskell-dev, from jprupp/tools, on your PATH.

haskell-dev build          # build without optimisation
haskell-dev test           # run the tests in several processes at once
haskell-dev docs           # build the documentation and check its comments

haskell-dev.toml tells it which spec modules are slow, so that it shares them between the test processes. Do not mix the two kinds of build in one folder: the flags differ, so each switch compiles everything again. Never time code with haskell-dev.

The tests check every type by law: JSON round-trips, the fast encoders write the same JSON as toJSON, the client reads what the server writes, the binary encodings of the database round-trip and agree between Serial, cereal and binary, on good bytes and on bad ones, and the format of the basic types is pinned.

Versions

Up to 1.8.1, this package was released from the haskoin-store repository with the same version number as the server. It now has its own releases; a server speaks the format of the haskoin-store-data version it depends on.

License

MIT