blockchain

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: GPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OP_DUP           = 0x76 // duplicates the top stack item
	OP_HASH160       = 0xa9 // SHA-256 and RIPEMD-160 hashed input
	OP_EQUALVERIFY   = 0x88 // inputs equality + verify result
	OP_CHECKSIG      = 0xac // valid signature for this hash and public key
	OP_1             = 0x51 // 1 / True
	OP_3             = 0x53 // 3
	OP_CHECKMULTISIG = 0xae // compares signatures
	OP_EQUAL         = 0x87 // checks inputs equality
	OP_RETURN        = 0x6a // no-op, invalid transaction
	OP_PUSHDATA1     = 0x4c // next byte contains the number of bytes to push

)

Variables

View Source
var (

	// Params are exported parameters to enable loading features
	Params = SParams{
		NBlocks:       0,
		StartBlock:    0,
		P2WPrefix:     "bc",
		LoadAddr:      false,
		LoadInAddr:    false,
		LoadTxid:      false,
		LoadBlockHash: false,
		LoadRaw:       false,
		TransacFunc:   nil,
		BlockFunc:     nil,
	}
)

Functions

func GetFileRecordByDate

func GetFileRecordByDate(db *leveldb.DB, t string) (uint64, error)

func GetRecordByHash

func GetRecordByHash(db *leveldb.DB, h string) ([]byte, error)

GetRecordByHash returns the block record corresponding to a database and hash given as parameters

func GetRecordByHeight

func GetRecordByHeight(db *leveldb.DB, h uint64) ([]byte, error)

GetRecordByHeight returns the block record corresponding to a database and block height given as parameters

func Hash160ToAddress

func Hash160ToAddress(pk []byte, version byte) []byte

Hash160ToAddress converts a Hash160 to an actual address Adapted from https://strm.sh/post/bitcoin-address-generation/

func MkWitAddr

func MkWitAddr(s []byte) (string, error)

MkWitAddr creates a valid bech32 address from []byte

func OpenDB

func OpenDB(f string) (*leveldb.DB, error)

OpenDB opens a LevelDB database and returns its handle

func Pk2Address

func Pk2Address(s []byte) ([]string, string, error)

Pk2Address converts public key from lock / scriptPubKey to plain address

func ReadBlockRecord

func ReadBlockRecord(data []byte, r string) (uint64, error)

ReadBlockRecord returns the value of record `r` in Block Index Record LevelDB value `data`

func ReadFileRecord

func ReadFileRecord(data []byte, r string) (uint64, error)

ReadFileRecord returns the value of record `r` in File Information Record LevelDB value `data`

func ReadVarint

func ReadVarint(buf *bytes.Reader) (uint64, int, error)

ReadVarint returns a VarInt value and the varint size https://learnmeabitcoin.com/guide/varint https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers This is not the same varint as in base 128 varint

func SSig2Addr

func SSig2Addr(in *Inputs) error

SSig2Addr Exctract addresses from scriptSig when possible

func SSigToPk

func SSigToPk(s []byte, ssize uint64) ([]byte, error)

SSigToPk extracts public key from Script Sig P2PKH type

func ValidPkSize

func ValidPkSize(size int, s byte) bool

ValidPkSize Determines if this is a valid public key size, uncompressed or compressed

Types

type Block

type Block struct {
	Magic     uint32        `json:"magic"`
	Size      uint32        `json:"size"`
	Header    BlockHeader   `json:"header"`
	TxCount   uint64        `json:"txcount"`
	Transac   []TransacData `json:"transacdata"`
	BlockHash string        `json:"blockhash,omitempty"`
}

Block holds the full block structure

func LoadBlocks

func LoadBlocks(blk string) ([]Block, error)

LoadBlocks reads the blkXXXXX.dat file and returns structured data. It can optionally compute and load txid for each transaction, and every address involved in inputs and outputs. Those two options are enabled by setting txid and addrs to true.

func (*Block) ReadHeader

func (b *Block) ReadHeader(header []byte)

ReadHeader reads block header

func (*Block) ReadInputs

func (b *Block) ReadInputs(incount uint64, buf *bytes.Reader) ([]Inputs, error)

ReadInputs reads and returns `incount` number of inputs

func (*Block) ReadOutputs

func (b *Block) ReadOutputs(outcount uint64, buf *bytes.Reader) ([]Outputs, error)

ReadOutputs read and returns `outcount` number of outputs

func (*Block) ReadTransactions

func (b *Block) ReadTransactions(t []byte) error

ReadTransactions loops through transactions and loads them This function receives a byte slice beginning with TxCount, then the raw transaction

type BlockHeader

type BlockHeader struct {
	Version    uint32 `json:"version"`
	PrevHash   string `json:"prevhash"`
	MerkleRoot string `json:"merkleroot"`
	TimeStamp  uint32 `json:"timestamp"`
	Target     uint32 `json::"target"`
	Nonce      uint32 `json:"nonce"`
}

BlockHeader stores block's headers

type Inputs

type Inputs struct {
	PrevTxid string   `json:"prevtxid"` // Previous (output) TXID
	PrevOut  uint32   `json:"prevout"`  // Previous output number
	SSigSize uint64   `json:"ssigsize"`
	SSig     []byte   `json:"ssig"`
	Sequence uint32   `json:"sequence"`
	Address  []string `json:"address,omitempty"`
	Coinbase bool     `json:"coinbase"`
	Height   uint32   `json:"height,omitempty"`
	Witness  [][]byte `json:"witness,omitempty"`
	Raw      []byte   `json:"raw,omitempty"`
	LockType string   `json:"locktype,omitempty"`
}

Inputs / spending structure https://learnmeabitcoin.com/guide/input Plus custom fields (Coinbase, Height, Raw and LockType)

type Outputs

type Outputs struct {
	Value       uint64 `json:"value"`
	SPubKeySize uint64 `json:"spubkeysize"`
	SPubKey     []byte `json:"spubkey"`
	Address     string `json:"address,omitempty"`
	LockType    string `json:"locktype,omitempty"`
	Raw         []byte `json:"raw,omitempty"`
}

Outputs https://learnmeabitcoin.com/guide/output Plus custom fields (LockType and Raw)

type SParams

type SParams struct {
	NBlocks       uint32      `json:"nblocks"`       // number of blocks to load, 0 for all
	StartBlock    uint32      `json:"startblock"`    // block number to start loading at
	StartHash     uint64      `json:"starthash"`     // block hash to start reading at
	P2WPrefix     string      `json:"p2wprefix"`     // SegWit address prefix
	LoadAddr      bool        `json:"loadaddr"`      // load (output) addresses
	LoadInAddr    bool        `json:"loadinaddr"`    // load / guess input addresses
	LoadTxid      bool        `json:"loadtxid"`      // compute and load TXIDs
	LoadBlockHash bool        `json:"loadblockhash"` // compute and load current block hash
	LoadRaw       bool        `json:"loadraw"`       // load in and out raw data
	TransacFunc   interface{} `json:"transacfunc"`   // optional transactions helper function
	TFuncParam    string      `json:"tfuncparam"`    // optional parameter for transaction callback
	BlockFunc     interface{} `json:"blockfunc"`     // optional block load helper function
	BFuncParam    string      `json:"bfuncparam"`    // optional parameter for block callback
}

SParams holds block loading parameters

type TransacData

type TransacData struct {
	Version  uint32    `json:"version"`
	InCount  uint64    `json:"incount"`
	Ins      []Inputs  `json:"inputs"`
	OutCount uint64    `json:"outcount"`
	Outs     []Outputs `json:"outputs"`
	LockTime uint32    `json:"locktime"`
	Raw      []byte    `json:"raw,omitempty"`
	Txid     string    `json:"txid,omitempty"`
	SegWit   bool      `json:"segwit"`
	WTxid    string    `json:"wtxid,omitempty"`
}

TransacData holds the transaction data https://learnmeabitcoin.com/guide/transaction-data

Jump to

Keyboard shortcuts

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