blockchain

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package blockchain is the library behind the blockchain command line: the HTTP client, request shaping, and the typed data models for blockchain.info public endpoints.

The Client here is the spine every command shares. It sets a real User-Agent, paces requests so a busy session stays polite, and retries transient failures (429 and 5xx) that a public API can throw under load. Build your endpoint calls and JSON decoding on top of it.

Index

Constants

View Source
const BaseURL = "https://" + Host

BaseURL is the root every request is built from.

View Source
const DefaultUserAgent = "blockchain-cli/dev (+https://github.com/tamnd/blockchain-cli)"

DefaultUserAgent identifies the client to blockchain.info. A real, honest User-Agent is both polite and the thing most likely to keep you unblocked.

View Source
const Host = "blockchain.info"

Host is the site this client talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address struct {
	Address       string `kit:"id" json:"address"`
	TxCount       int    `json:"tx_count"`
	TotalReceived int64  `json:"total_received"`
	TotalSent     int64  `json:"total_sent"`
	FinalBalance  int64  `json:"final_balance"`
}

Address is address info from /rawaddr/<address>.

type Block

type Block struct {
	Hash       string `kit:"id" json:"hash"`
	Height     int    `json:"height"`
	Time       int64  `json:"time"`
	BlockIndex int    `json:"block_index"`
	TxCount    int    `json:"tx_count"`
}

Block is the latest block summary from /latestblock.

type Client

type Client struct {
	HTTP      *http.Client
	UserAgent string
	// Rate is the minimum gap between requests. Zero means no pacing.
	Rate    time.Duration
	Retries int
	// contains filtered or unexported fields
}

Client talks to blockchain.info over HTTP.

func NewClient

func NewClient() *Client

NewClient returns a Client with sensible defaults.

func (*Client) GetAddress

func (c *Client) GetAddress(ctx context.Context, address string, limit int) (*Address, error)

GetAddress fetches /rawaddr/<address>?limit=<limit>.

func (*Client) GetLatestBlock

func (c *Client) GetLatestBlock(ctx context.Context) (*Block, error)

GetLatestBlock fetches /latestblock and returns the latest block.

func (*Client) GetStats

func (c *Client) GetStats(ctx context.Context) (*Stats, error)

GetStats fetches /stats and /latestblock, combines them into one Stats record.

func (*Client) GetTicker

func (c *Client) GetTicker(ctx context.Context, currency string) ([]*Price, error)

GetTicker fetches /ticker and returns prices for all currencies. If currency is non-empty, only that currency is returned.

func (*Client) GetTransaction

func (c *Client) GetTransaction(ctx context.Context, hash string) (*Transaction, error)

GetTransaction fetches /rawtx/<hash> and computes input/output sums.

type Config

type Config struct {
	UserAgent string
	Rate      time.Duration
	Retries   int
	Timeout   time.Duration
}

Config holds tuneable knobs a host or the CLI can override.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

type Domain

type Domain struct{}

Domain is the blockchain driver. It carries no state; the per-run client is built by the factory Register hands kit.

func (Domain) Classify

func (Domain) Classify(input string) (uriType, id string, err error)

Classify turns any accepted input into the canonical (type, id). Bitcoin address starts with 1, 3, or bc1; 64-char hex is a tx hash.

func (Domain) Info

func (Domain) Info() kit.DomainInfo

Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate returns the live https URL for a (type, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Price

type Price struct {
	Currency string  `kit:"id" json:"currency"`
	Last     float64 `json:"last"`
	Buy      float64 `json:"buy"`
	Sell     float64 `json:"sell"`
	Symbol   string  `json:"symbol"`
}

Price is the BTC price in one currency from /ticker.

type Stats

type Stats struct {
	Difficulty           float64 `kit:"id" json:"difficulty"`
	HashRate             float64 `json:"hash_rate"`
	TotalBitcoins        float64 `json:"total_bitcoins"`
	MarketPriceUSD       float64 `json:"market_price_usd"`
	TradeVolumeBTC       float64 `json:"trade_volume_btc"`
	MinutesBetweenBlocks float64 `json:"minutes_between_blocks"`
	BlockHeight          int     `json:"block_height"`
}

Stats is the Bitcoin network snapshot from /stats.

type Transaction

type Transaction struct {
	Hash        string `kit:"id" json:"hash"`
	Time        int64  `json:"time"`
	BlockHeight int    `json:"block_height"`
	Fee         int64  `json:"fee"`
	InputCount  int    `json:"input_count"`
	OutputCount int    `json:"output_count"`
	InputSum    int64  `json:"input_sum"`
	OutputSum   int64  `json:"output_sum"`
}

Transaction is a raw transaction from /rawtx/<hash>.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL