Help us shape the future of Go! Take the 2026 Go Developer Survey

blockchain

package module
v0.0.0-...-9589215 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2014 License: Apache-2.0 Imports: 9 Imported by: 1

README

blockchain

Blockchain.info API Go interface.

GoDoc documentation is available here.

Check the test files for example use. However it is along the lines of:


package main

import (
	"fmt"
	"net/http"

	"github.com/qedus/blockchain"
)

func main() {
	bc := blockchain.New(http.DefaultClient)
	address := &blockchain.Address{Address: "an address hash"}
	if err := bc.Request(address); err != nil {
		panic(err)
	}

	// Loop through all the transactions associated with a certain
	// address "an address hash" and print their transaction hashes.
	for {
		tx, err := address.NextTransaction()
		if err == blockchain.TransactionsDone {
			break
		} else if err != nil {
			panic(err)
		}
		fmt.Println(tx.Hash)
	}
}

Blockchain.info API Issues

By creating this client I have noticed a few issues/inconsistencies with the Blockchain.info API. I have taken the stance of not correcting them with this client and instead document them here.

Coinbase block input has an empty hash

Look at this second block for example. You will notice that the coinbase transaction, i.e. the first transaction, contains an input with an empty hash as so inputs: [{}]. It should really be inputs:[]. The effect this has on the Go JSON parser is to produce an empty Input{} struct for all coinbase transactions. Note that coinbase transactions should have no inputs - not empty ones.

The consequence of this is you cannot use len(tx.Inputs) == 0 to determine if a transaction is a coinbase transaction. Instead I have created tx.IsCoinbase() which should be used to determine if a transaction is a coinbase one.

Transaction input and output counts not always correct

Take transaction 2355913fc1a3d71efbc228c69dc5d74340e07b9012377b4b9f6d5522116d0509 in block 312373 for example. The JSON says there are 14 inputs (vin_sz=14) when in fact it only lists 4 in inputs=[...]. Therefore tx.InputCount and tx.OutputCount do not always match len(tx.Inputs) and len(tx.Outputs). However if you select the transaction individually the correct number of inputs are listed in inputs=[...].

It appears that the block API consolidates inputs with the same address where as the individual transaction API does not. This does not appear to be documented. I therefore advise that if you want to iterate through all transaction inputs and outputs you do not rely on tx.InputCount or tx.OutputCount but instead use len(tx.Inputs) and len(tx.Outputs) or their := range ... equivalents.

    // Do NOT do this.
    for i := 0; i < tx.InputCount; i++ {
        ...
    }
    
    // Do this instead.
    for i, input := range tx.Inputs {
        ...
    }

Warning

If you run the tests ensure that you use a dummy set of credentials for wallet_test.go otherwise the test will spend your money.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Used when an iterator has exhausted its supply.
	IterDone = errors.New("iterator done")
)

Functions

This section is empty.

Types

type Address

type Address struct {
	Hash160          string
	Address          string
	TransactionCount int64         `json:"n_tx"`
	TotalReceived    int64         `json:"total_received"`
	TotalSent        int64         `json:"total_sent"`
	FinalBalance     int64         `json:"final_balance"`
	Transactions     []Transaction `json:"txs"`

	TxSortDescending bool
	// contains filtered or unexported fields
}

func (*Address) NextTransaction

func (a *Address) NextTransaction() (Transaction, error)

type AddressList

type AddressList struct {
	// Optional request parameter
	Confirmations int

	Addresses []struct {
		Balance       int64
		Address       string
		Label         string
		TotalReceived int64 `json:"total_received"`
	}
}

type Block

type Block struct {
	// Request params.
	Hash  string
	Index int64 `json:"block_index"`

	Version          int64  `json:"ver"`
	PreviousBlock    string `json:"prev_block"`
	MerkelRoot       string `json:"mrkl_root"`
	Time             int64
	Bits             int64
	Fee              int64
	Nonce            int64
	TransactionCount int64 `json:"n_tx"`
	Size             int64
	MainChain        bool `json:"main_chain"`
	Height           int64
	ReceivedTime     int64         `json:"received_time"`
	RelayedBy        string        `json:"relayed_by"`
	Transactions     []Transaction `json:"tx"`
}

type BlockChain

type BlockChain struct {
	GUID           string
	Password       string
	SecondPassword string
	APICode        string
	// contains filtered or unexported fields
}

func New

func New(c *http.Client) *BlockChain

func (*BlockChain) Request

func (bc *BlockChain) Request(item Item) error

type BlockHeight

type BlockHeight struct {
	Height int64 `json:"-"`
	Blocks []*Block
}

type Input

type Input struct {
	PrevOut struct {
		Address          string `json:"addr"`
		Number           int64  `json:"n"`
		TransactionIndex int64  `json:"tx_index"`
		Type             int64
		Value            int64
	} `json:"prev_out"`
}

type Item

type Item interface {
	// contains filtered or unexported methods
}

type LatestBlock

type LatestBlock struct {
	Hash               string
	Time               int64
	BlockIndex         int64 `json:"block_index"`
	Height             int64
	TransactionIndexes []int64 `json:"txIndexes"`
}

type NewAddress

type NewAddress struct {
	// Optional request parameter
	Label string

	// Response
	Address string

	// Error Response
	Error string
}

NewAddress allows you to create a new address in your blockchain.info wallet. The Label parameter is optional and if set will label the new address. The struct is passed to Request and Address and/or Error parameters are populated when Request returns.

type Output

type Output struct {
	Address          string `json:"addr"`
	AddressTag       string `json:"addr_tag"`
	AddressTagLink   string `json:"addr_tag_link"`
	Number           int64  `json:"n"`
	TransactionIndex int64  `json:"tx_index"`
	Type             int64
	Value            int64
}

type SendPayment

type SendPayment struct {
	//  Required request parameters.
	ToAddress string
	Amount    int64

	// Optional request parameters.
	FromAddress string
	Shared      bool
	Fee         int64
	Note        string

	// Response
	Message         string
	TransactionHash string `json:"tx_hash"`
	Notice          string

	// Error response
	Error string
}

SendPayment struct allows you to send a payment from a blockchain.info wallet. ToAddress and Amount are required parameters. FromAddress, Shared Fee and Note are optional parameters. Check parameter Error for error respnoses from the blockchain.info wallet.

type Transaction

type Transaction struct {
	// Required request parameters.
	// Either Hash or TransactionIndex are required.
	Hash  string
	Index int64 `json:"tx_index"`

	Inputs      []Input
	InputCount  int64    `json:"vin_sz"`
	Outputs     []Output `json:"out"`
	OutputCount int64    `json:"vout_sz"`
	RelayedBy   string   `json:"relayed_by"`
	Result      int64
	Size        int64
	Time        int64
	BlockHeight int64 `json:"block_height"`
	Version     int64 `json:"ver"`
}

func (*Transaction) Fee

func (t *Transaction) Fee() int64

func (*Transaction) IsCoinbase

func (t *Transaction) IsCoinbase() bool

type UnconfirmedTransactions

type UnconfirmedTransactions struct {
	Transactions []Transaction `json:"txs"`
	// contains filtered or unexported fields
}

func (*UnconfirmedTransactions) NextTransaction

func (uc *UnconfirmedTransactions) NextTransaction() (Transaction, error)

Jump to

Keyboard shortcuts

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