bitcoin

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package bitcoin holds the strict readers of the Bitcoin bytes a commitment entry carries, for the Bitcoin evidence of DERIVE (SPEC.md section 10.2, step 9 (d)): a raw_transaction and a txoutproof, each judged well formed or not by the rules that step lists, and the values the step reads out of a well-formed one. It reads only bytes in memory and never the network.

| File            | What it reads                                                                 |
|-----------------|-------------------------------------------------------------------------------|
| bitcoin.go      | SHA-256d, display order and the compact size                                 |
| transaction.go  | a raw_transaction: its rules, its OP_RETURN, and rev(SHA-256d) of its bytes   |
| merkle_block.go | a txoutproof: its rules, the partial Merkle tree's walk and the matched hash  |
| header.go       | the 80-octet header: its fields, its hash and the target its nBits encodes    |

SHA-256d(x) is SHA-256(SHA-256(x)), and rev(x) is x with its bytes in reverse order (SPEC.md section 2.2). SHA-256d is a hash an outside protocol fixes, so it is outside the byte-prefix registry (section 4.1): [sha256d] is this package's one use of crypto/sha256, and internal/registry/rule2_test.go names it, with each of its calls, as an exemption from its check that crypto/sha256 is used only in the registry's digest. A Bitcoin hash is written in display order, rev of the order it is hashed in, as a proof's members are (the encoding row of section 7.3); Display writes one.

The compact size is Bitcoin's variable-length integer: one octet below 0xfd, or 0xfd, 0xfe or 0xff followed by 2, 4 or 8 octets little-endian. Step 9 (d) requires every compact size in a raw_transaction and a txoutproof in its shortest form, so [compactSize] reads only that form.

A structure that is not well formed is refused with a Refusal, the first rule it breaks. The rules of a raw_transaction are named as vectors/corpus/bitcoin/ names them (SPEC.md section 13.2): parse, length and op_return. SPEC.md names no rule of a txoutproof, so each clause of step 9 (d)'s bullets is named here: parse and total_txs (the first bullet), partial_tree and duplicate_pair (the second), matches and merkle_root (the third) and target (the fourth).

Index

Constants

View Source
const HeaderOctets = 80

HeaderOctets is the length of a block header.

Variables

View Source
var (
	// ErrNegativeTarget: the encoding is negative.
	ErrNegativeTarget = errors.New("the nBits encoding is negative")
	// ErrTargetOverflow: the encoding overflows.
	ErrTargetOverflow = errors.New("the nBits encoding overflows")
)

The two ways an nBits value fails to encode a target.

Functions

func Display

func Display(hash [32]byte) string

Display writes a Bitcoin hash in display order: rev(hash) as lowercase hex (the encoding row of SPEC.md section 7.3).

func HeaderHash

func HeaderHash(header Header) [32]byte

HeaderHash is SHA-256d(header), in the order it is hashed in; rev of it is header_hash.

func HeaderTime

func HeaderTime(header Header) uint32

HeaderTime is the header's nTime, in Unix seconds.

func MeetsTarget

func MeetsTarget(header Header) bool

MeetsTarget reports whether header meets the target its nBits field encodes (see the file's doc).

func MerkleRootField

func MerkleRootField(header Header) [32]byte

MerkleRootField is the header's Merkle root field, octets 36 to 67, in the order it is hashed in; rev of it is header_merkle_root.

func NBits

func NBits(header Header) uint32

NBits is the header's nBits value.

func ParseTransaction

func ParseTransaction(raw []byte) ([32]byte, error)

ParseTransaction judges raw as a raw_transaction (see the file's doc): the 32 bytes its one OP_RETURN script pushes, in the order pushed, when it is well formed, and otherwise the first rule it breaks, a Refusal.

func Target

func Target(n uint32) (*big.Int, error)

Target is the target the nBits value n encodes (see the file's doc), or ErrNegativeTarget or ErrTargetOverflow.

func TransactionHash

func TransactionHash(raw []byte) [32]byte

TransactionHash is SHA-256d(raw), in the order it is hashed in, of the bytes as carried; rev of it is transaction_hash (SPEC.md section 3.4).

Types

type Header [HeaderOctets]byte

Header is an 80-octet block header.

type MerkleBlock

type MerkleBlock struct {
	Header   Header
	TotalTxs uint32
	Matched  [32]byte
}

MerkleBlock is what a well-formed txoutproof gives: its header, its total_txs and the one hash it matches, in the order it is hashed in.

func ParseMerkleBlock

func ParseMerkleBlock(b []byte) (MerkleBlock, error)

ParseMerkleBlock judges b as a txoutproof (see the file's doc): what it gives when it is well formed, and otherwise the first rule it breaks, a Refusal.

type Refusal

type Refusal string

Refusal is the first rule of a well-formed raw_transaction or txoutproof (SPEC.md section 10.2, step 9 (d)) that bytes break. Its text is the rule's name.

const (
	// RefusedParse: the bytes do not parse completely as the structure,
	// with every compact size in its shortest form and nothing after.
	RefusedParse Refusal = "parse"
	// RefusedLength: a raw_transaction is exactly 64 octets long
	// (CVE-2017-12842).
	RefusedLength Refusal = "length"
	// RefusedOpReturn: a raw_transaction has no output script that begins
	// with 0x6a, or more than one, or its one such script is not exactly
	// 0x6a 0x20 followed by 32 bytes.
	RefusedOpReturn Refusal = "op_return"
	// RefusedTotalTxs: a txoutproof's total_txs is not from 1 to
	// BITCOIN_MAX_TOTAL_TXS.
	RefusedTotalTxs Refusal = "total_txs"
	// RefusedPartialTree: a txoutproof's partial Merkle tree does not walk
	// as Bitcoin Core's CPartialMerkleTree walks one, using every hash and
	// every flag octet, with the bits after the last one used zero.
	RefusedPartialTree Refusal = "partial_tree"
	// RefusedDuplicatePair: a node of the walk has a right child equal to
	// its left (CVE-2012-2459).
	RefusedDuplicatePair Refusal = "duplicate_pair"
	// RefusedMatches: the walk does not match exactly one transaction.
	RefusedMatches Refusal = "matches"
	// RefusedMerkleRoot: the root the walk gives is not the header's
	// Merkle root field.
	RefusedMerkleRoot Refusal = "merkle_root"
	// RefusedTarget: the header does not meet the target its nBits field
	// encodes.
	RefusedTarget Refusal = "target"
)

The rules a raw_transaction or a txoutproof can break (see the package's doc).

func (Refusal) Error

func (r Refusal) Error() string

Error returns the rule's name.

Jump to

Keyboard shortcuts

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