package
3.5.7+incompatible
Repository: https://github.com/classzz/classzz.git
Documentation: pkg.go.dev

# README

wire

Build Status ISC License GoDoc

Package wire implements the bitcoin wire protocol. A comprehensive suite of tests with 100% test coverage is provided to ensure proper functionality.

There is an associated blog post about the release of this package here.

This package has intentionally been designed so it can be used as a standalone package for any projects needing to interface with bitcoin peers at the wire protocol level.

Installation and Updating

$ go get -u github.com/classzz/classzz/wire

Bitcoin Message Overview

The bitcoin protocol consists of exchanging messages between peers. Each message is preceded by a header which identifies information about it such as which bitcoin network it is a part of, its type, how big it is, and a checksum to verify validity. All encoding and decoding of message headers is handled by this package.

To accomplish this, there is a generic interface for bitcoin messages named Message which allows messages of any type to be read, written, or passed around through channels, functions, etc. In addition, concrete implementations of most of the currently supported bitcoin messages are provided. For these supported messages, all of the details of marshalling and unmarshalling to and from the wire using bitcoin encoding are handled so the caller doesn't have to concern themselves with the specifics.

Reading Messages Example

In order to unmarshal bitcoin messages from the wire, use the ReadMessage function. It accepts any io.Reader, but typically this will be a net.Conn to a remote node running a bitcoin peer. Example syntax is:

	// Use the most recent protocol version supported by the package and the
	// main bitcoin network.
	pver := wire.ProtocolVersion
	czznet := wire.MainNet

	// Reads and validates the next bitcoin message from conn using the
	// protocol version pver and the bitcoin network czznet.  The returns
	// are a wire.Message, a []byte which contains the unmarshalled
	// raw payload, and a possible error.
	msg, rawPayload, err := wire.ReadMessage(conn, pver, czznet)
	if err != nil {
		// Log and handle the error
	}

See the package documentation for details on determining the message type.

Writing Messages Example

In order to marshal bitcoin messages to the wire, use the WriteMessage function. It accepts any io.Writer, but typically this will be a net.Conn to a remote node running a bitcoin peer. Example syntax to request addresses from a remote peer is:

	// Use the most recent protocol version supported by the package and the
	// main bitcoin network.
	pver := wire.ProtocolVersion
	czznet := wire.MainNet

	// Create a new getaddr bitcoin message.
	msg := wire.NewMsgGetAddr()

	// Writes a bitcoin message msg to conn using the protocol version
	// pver, and the bitcoin network czznet.  The return is a possible
	// error.
	err := wire.WriteMessage(conn, msg, pver, czznet)
	if err != nil {
		// Log and handle the error
	}

License

Package wire is licensed under the copyfree ISC License.

# Functions

MaxBlockPayload returns the maximum bytes a block message can be in bytes.
NewBlockHeader returns a new BlockHeader using the provided version, previous block hash, merkle root hash, difficulty bits, and nonce used to generate the block with defaults for the remaining fields.
NewInvVect returns a new InvVect using the provided type and hash.
NewMsgAddr returns a new bitcoin addr message that conforms to the Message interface.
NewMsgBlock returns a new bitcoin block message that conforms to the Message interface.
NewMsgBlockTxns returns a new bitcoin blocktxn message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgCFCheckpt returns a new bitcoin cfheaders message that conforms to the Message interface.
NewMsgCFHeaders returns a new bitcoin cfheaders message that conforms to the Message interface.
NewMsgCFilter returns a new bitcoin cfilter message that conforms to the Message interface.
NewMsgCmpctBlock returns a new bitcoin cmpctblock message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgCmpctBlockFromBlock builds a cmpctblock message from a block using a known inventory map.
NewMsgFeeFilter returns a new bitcoin feefilter message that conforms to the Message interface.
NewMsgFilterAdd returns a new bitcoin filteradd message that conforms to the Message interface.
NewMsgFilterClear returns a new bitcoin filterclear message that conforms to the Message interface.
NewMsgFilterLoad returns a new bitcoin filterload message that conforms to the Message interface.
NewMsgGetAddr returns a new bitcoin getaddr message that conforms to the Message interface.
NewMsgGetBlocks returns a new bitcoin getblocks message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetBlockTxns returns a new bitcoin getblocktxn message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetBlockTxnsFromBlock parses a block and for each nil transasction ads a index to the getblocktxn message that is returned.
NewMsgGetCFCheckpt returns a new bitcoin getcfcheckpt message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetCFHeaders returns a new bitcoin getcfheader message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetCFilters returns a new bitcoin getcfilters message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgGetCFMempool returns a new bitcoin getcfmempool message that conforms to the Message interface using the passed parameters.
NewMsgGetData returns a new bitcoin getdata message that conforms to the Message interface.
NewMsgGetDataSizeHint returns a new bitcoin getdata message that conforms to the Message interface.
NewMsgGetHeaders returns a new bitcoin getheaders message that conforms to the Message interface.
NewMsgHeaders returns a new bitcoin headers message that conforms to the Message interface.
NewMsgInv returns a new bitcoin inv message that conforms to the Message interface.
NewMsgInvSizeHint returns a new bitcoin inv message that conforms to the Message interface.
NewMsgMemPool returns a new bitcoin pong message that conforms to the Message interface.
NewMsgMerkleBlock returns a new bitcoin merkleblock message that conforms to the Message interface.
NewMsgNotFound returns a new bitcoin notfound message that conforms to the Message interface.
NewMsgPing returns a new bitcoin ping message that conforms to the Message interface.
NewMsgPong returns a new bitcoin pong message that conforms to the Message interface.
NewMsgReject returns a new bitcoin reject message that conforms to the Message interface.
NewMsgSendCmpct returns a new bitcoin sendcmpct message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgSendHeaders returns a new bitcoin sendheaders message that conforms to the Message interface.
NewMsgTx returns a new bitcoin tx message that conforms to the Message interface.
NewMsgVerAck returns a new bitcoin verack message that conforms to the Message interface.
NewMsgVersion returns a new bitcoin version message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewMsgXVerAck returns a new bitcoin verack message that conforms to the Message interface.
NewMsgXVersion returns a new bitcoin xversion message that conforms to the Message interface using the passed parameters and defaults for the remaining fields.
NewNetAddress returns a new NetAddress using the provided TCP address and supported services with defaults for the remaining fields.
NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and supported services with defaults for the remaining fields.
NewNetAddressTimestamp returns a new NetAddress using the provided timestamp, IP, port, and supported services.
NewOutPoint returns a new bitcoin transaction outpoint point with the provided hash and index.
NewTxIn returns a new bitcoin transaction input with the provided previous outpoint point and signature script with a default sequence of MaxTxInSequenceNum.
NewTxOut returns a new bitcoin transaction output with the provided transaction value and public key script.
RandomUint64 returns a cryptographically random uint64 value.
ReadMessage reads, validates, and parses the next bitcoin Message from r for the provided protocol version and bitcoin network.
ReadMessageN reads, validates, and parses the next bitcoin Message from r for the provided protocol version and bitcoin network.
ReadMessageWithEncodingN reads, validates, and parses the next bitcoin Message from r for the provided protocol version and bitcoin network.
ReadVarBytes reads a variable length byte array.
ReadVarInt reads a variable length integer from r and returns it as a uint64.
ReadVarString reads a variable length string from r and returns it as a Go string.
SetLimits adjusts various message limits based on max block size configuration.
VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.
WriteMessage writes a bitcoin Message to w including the necessary header information.
WriteMessageN writes a bitcoin Message to w including the necessary header information and returns the number of bytes written.
WriteMessageWithEncodingN writes a bitcoin Message to w including the necessary header information and returns the number of bytes written.
WriteTxOut encodes to into the bitcoin protocol encoding for a transaction output (TxOut) to w.
WriteVarBytes serializes a variable length byte array to w as a varInt containing the number of bytes, followed by the bytes themselves.
WriteVarInt serializes val to w using a variable number of bytes depending on its value.
WriteVarString serializes str to w as a variable length integer containing the length of the string followed by the bytes that represent the string itself.

# Constants

BaseEncoding encodes all messages in the default format specified for the Bitcoin wire protocol.
No description provided by the author
BloomUpdateAll indicates if the filter matches any data element in a public key script, the outpoint is serialized and inserted into the filter.
BloomUpdateNone indicates the filter is not adjusted when a match is found.
BloomUpdateP2PubkeyOnly indicates if the filter matches a data element in a public key script and the script is of the standard pay-to-pubkey or multisig, the outpoint is serialized and inserted into the filter.
CFCheckptInterval is the gap (in number of blocks) between each filter header checkpoint.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
Commands used in bitcoin message headers which describe the type of message.
CommandSize is the fixed size of all commands in the common bitcoin message header.
CompactBlocksProtocolVersion is the current version of the compact blocks protocol.
DefaultUserAgent for wire in the stack.
GCSFilterRegular is the regular filter type.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
MainNet represents the main bitcoin network.
MaxAddrPerMsg is the maximum number of addresses that can be in a single bitcoin addr message (MsgAddr).
MaxBlockHeaderPayload is the maximum number of bytes a block header can be.
MaxBlockHeadersPerMsg is the maximum number of block headers that can be in a single bitcoin headers message.
MaxBlockLocatorsPerMsg is the maximum number of block locator hashes allowed per message.
MaxBlocksPerMsg is the maximum number of blocks allowed per message.
MaxCFHeaderPayload is the maximum byte size of a committed filter header.
MaxCFHeadersPerMsg is the maximum number of committed filter headers that can be in a single bitcoin cfheaders message.
MaxCFilterDataSize is the maximum byte size of a committed filter.
MaxFilterAddDataSize is the maximum byte size of a data element to add to the Bloom filter.
MaxFilterLoadFilterSize is the maximum size in bytes a filter may be.
MaxFilterLoadHashFuncs is the maximum number of hash functions to load into the Bloom filter.
MaxGetCFiltersReqRange the maximum number of filters that may be requested in a getcfheaders message.
MaxInvPerMsg is the maximum number of inventory vectors that can be in a single bitcoin inv message.
MaxPrevOutIndex is the maximum index the index field of a previous outpoint can be.
MaxTxInSequenceNum is the maximum sequence number the sequence field of a transaction input can be.
MaxUserAgentLen is the maximum allowed length for the user agent field in a version message (MsgVersion).
MaxVarIntPayload is the maximum payload size for a variable length integer.
MessageHeaderSize is the number of bytes in a bitcoin message header.
MinTxOutPayload is the minimum payload size for a transaction output.
ProtocolVersion is the latest protocol version this package supports.
TestNet represents the regression test network.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
These constants define the various supported reject codes.
SequenceLockTimeDisabled is a flag that if set on a transaction input's sequence number, the sequence number will not be interpreted as a relative locktime.
SequenceLockTimeGranularity is the defined time based granularity for seconds-based relative time locks.
SequenceLockTimeIsSeconds is a flag that if set on a transaction input's sequence number, the relative locktime has units of 512 seconds.
SequenceLockTimeMask is a mask that extracts the relative locktime when masked against the transaction input sequence number.
SFNodeBitcoinCash indicates a node is running on the Bitcoin Cash network.
SFNodeBloom is a flag used to indicate a peer supports bloom filtering.
SFNodeCF is a flag used to indicate a peer supports committed filters (CFs).
SFNodeGetUTXO is a flag used to indicate a peer supports the getutxos and utxos commands (BIP0064).
SFNodeGraphene is a flag used to indicate a peer supports graphene block relay.
SFNodeNetwork is a flag used to indicate a peer is a full node.
SFNodeNetworkLimited is used to indicate the node is a pruned node and may only be capable of limited services.
SFNodeWeakBlocks is a flag used to indicate a peer supports the weak block protocol.
SFNodeWitness is a flag used to indicate a peer supports blocks and transactions including witness data (BIP0144).
SFNodeXthin is a flag used to indicate a peer supports xthin blocks.
SFNodeXThinner is a placeholder for the xthinner block compression protocol being developed by Johnathan Toomim.
ShortIDSize is the number of bytes in a short ID.
SimNet represents the simulation test network.
TestNet represents the test network (version 3).
TxVersion is the current latest supported transaction version.
No description provided by the author

# Variables

No description provided by the author
ErrInsaneCFHeaderCount signals that we were asked to decode an unreasonable number of cfilter headers.
LatestEncoding is the most recently specified encoding for the Bitcoin wire protocol.

# Structs

BlockHeader defines information about a block and is used in the bitcoin block (MsgBlock) and headers (MsgHeaders) messages.
InvVect defines a bitcoin inventory vector which is used to describe data, as specified by the Type field, that a peer wants, has, or does not have to another peer.
MessageError describes an issue with a message.
MsgAddr implements the Message interface and represents a bitcoin cash addr message.
MsgBlock implements the Message interface and represents a bitcoin block message.
MsgBlockTxns implements the Message interface and represents a Bitcoin blocktxn message.
MsgCFCheckpt implements the Message interface and represents a bitcoin cfcheckpt message.
MsgCFHeaders implements the Message interface and represents a bitcoin cfheaders message.
MsgCFilter implements the Message interface and represents a bitcoin cfilter message.
MsgCmpctBlock implements the Message interface and represents a Bitcoin cmpctblock message.
MsgFeeFilter implements the Message interface and represents a bitcoin feefilter message.
MsgFilterAdd implements the Message interface and represents a bitcoin filteradd message.
MsgFilterClear implements the Message interface and represents a bitcoin filterclear message which is used to reset a Bloom filter.
MsgFilterLoad implements the Message interface and represents a bitcoin filterload message which is used to reset a Bloom filter.
MsgGetAddr implements the Message interface and represents a bitcoin getaddr message.
MsgGetBlocks implements the Message interface and represents a bitcoin getblocks message.
MsgGetBlockTxns implements the Message interface and represents a Bitcoin getblocktxn message.
MsgGetCFCheckpt is a request for filter headers at evenly spaced intervals throughout the blockchain history.
MsgGetCFHeaders is a message similar to MsgGetHeaders, but for committed filter headers.
MsgGetCFilters implements the Message interface and represents a bitcoin getcfilters message.
MsgGetCFMempool is a request for a filter of the remote peer's mempool.
MsgGetData implements the Message interface and represents a bitcoin getdata message.
MsgGetHeaders implements the Message interface and represents a bitcoin getheaders message.
MsgHeaders implements the Message interface and represents a bitcoin headers message.
MsgInv implements the Message interface and represents a bitcoin inv message.
MsgMemPool implements the Message interface and represents a bitcoin mempool message.
MsgMerkleBlock implements the Message interface and represents a bitcoin merkleblock message which is used to reset a Bloom filter.
MsgNotFound defines a bitcoin notfound message which is sent in response to a getdata message if any of the requested data in not available on the peer.
MsgPing implements the Message interface and represents a bitcoin ping message.
MsgPong implements the Message interface and represents a bitcoin pong message which is used primarily to confirm that a connection is still valid in response to a bitcoin ping message (MsgPing).
MsgReject implements the Message interface and represents a bitcoin reject message.
MsgSendCmpct implements the Message interface and represents a Bitcoin sendcmpct message.
MsgSendHeaders implements the Message interface and represents a bitcoin sendheaders message.
MsgTx implements the Message interface and represents a bitcoin tx message.
MsgVerAck defines a bitcoin verack message which is used for a peer to acknowledge a version message (MsgVersion) after it has used the information to negotiate parameters.
MsgVersion implements the Message interface and represents a bitcoin version message.
MsgXVerAck defines a bitcoin xverack message which is used for a peer to acknowledge an xversion message (MsgXVersion).
MsgXVersion implements the Message interface and represents a bitcoin xversion message.
NetAddress defines information about a peer on the network including the time it was last seen, the services it supports, its IP address, and port.
OutPoint defines a bitcoin data type that is used to track previous transaction outputs.
PrefilledTx is a transaction that is sent along with the compact block.
TxIn defines a bitcoin transaction input.
TxLoc holds locator data for the offset and length of where a transaction is located within a MsgBlock data buffer.
TxOut defines a bitcoin transaction output.

# Interfaces

Message is an interface that describes a bitcoin message.

# Type aliases

BitcoinNet represents which bitcoin network a message belongs to.
BloomUpdateType specifies how the filter is updated when a match is found.
FilterType is used to represent a filter type.
InvType represents the allowed types of inventory vectors.
MessageEncoding represents the wire message encoding format to be used.
RejectCode represents a numeric value by which a remote peer indicates why a message was rejected.
ServiceFlag identifies services supported by a bitcoin peer.
TxWitness defines the witness for a TxIn.