package
0.0.0-20191201205449-f379a713d0c2
Repository: https://github.com/rjected/lit.git
Documentation: pkg.go.dev

# README

wire

[Build Status] (https://travis-ci.org/btcsuite/btcd) ![ISC License] (http://img.shields.io/badge/license-ISC-blue.svg) [GoDoc] (http://godoc.org/github.com/adiabat/btcd/wire)

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/adiabat/btcd/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
	btcnet := wire.MainNet

	// Reads and validates the next bitcoin message from conn using the
	// protocol version pver and the bitcoin network btcnet.  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, btcnet)
	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
	btcnet := 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 btcnet.  The return is a possible
	// error.
	err := wire.WriteMessage(conn, msg, pver, btcnet)
	if err != nil {
		// Log and handle the error
	}

GPG Verification Key

All official release tags are signed by Conformal so users can ensure the code has not been tampered with and is coming from the btcsuite developers. To verify the signature perform the following:

  • Download the public key from the Conformal website at https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt

  • Import the public key into your GPG keyring:

    gpg --import GIT-GPG-KEY-conformal.txt
    
  • Verify the release tag with the following command where TAG_NAME is a placeholder for the specific tag:

    git tag -v TAG_NAME
    

License

Package wire is licensed under the copyfree ISC License.

# Functions

NewAlert returns an new Alert with values provided.
NewAlertFromPayload returns an Alert with values deserialized from the serialized payload.
NewBlockHeader returns a new BlockHeader using the provided 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.
NewMsgAlert returns a new bitcoin alert message that conforms to the Message interface.
NewMsgBlock returns a new bitcoin block 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.
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.
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.
NewMsgVersionFromConn is a convenience function that extracts the remote and local address from conn and returns a new bitcoin version message that conforms to the Message interface.
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.
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.
VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.
writeBlockHeader writes a bitcoin block header to w.
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.
BC2Net represents the BC2 test network.
BIP0031Version is the protocol version AFTER which a pong message and nonce field in ping were added (pver > BIP0031Version).
BIP0035Version is the protocol version which added the mempool message (pver >= BIP0035Version).
BIP0037Version is the protocol version which added new connection bloom filtering related messages and extended the version message with a relay flag (pver >= BIP0037Version).
BIP0111Version is the protocol version which added the SFNodeBloom service flag.
BlockVersion is the current latest supported block version.
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.
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.
DefaultUserAgent for wire in the stack.
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.
These constants define the various supported inventory vector types.
These constants define the various supported inventory vector types.
InvWitnessFlag denotes that the inventory vector type is requesting, or sending a version which includes witness data.
LiteRegNet is the litecoin regression test network (distinct from testnet4).
BC2Net represents the BC2 test network.
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.
MaxBlockPayload is the maximum bytes a block message can be in bytes.
MaxBlocksPerMsg is the maximum number of blocks allowed per message.
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.
MaxInvPerMsg is the maximum number of inventory vectors that can be in a single bitcoin inv message.
32MB.
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.
MultipleAddressVersion is the protocol version which added multiple addresses per message (pver >= MultipleAddressVersion).
NetAddressTimeVersion is the protocol version which added the timestamp field (pver >= NetAddressTimeVersion).
ProtocolVersion is the latest protocol version this package supports.
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.
RejectVersion is the protocol version which added a new reject message.
SendHeadersVersion is the protocol version which added a new sendheaders message.
SFNodeBloom is a flag used to indiciate a peer supports bloom filtering.
SFNodeGetUTXO is a flag used to indicate a peer supports the getutxos and utxos commands (BIP0064).
SFNodeNetwork is a flag used to indicate a peer is a full node.
SFNodeWitness is a flag used to indicate a peer supports blocks and transactions including witness data (BIP0144).
SimNet represents the simulation test network.
TestNet represents the regression test network.
TestNet3 represents the test network (version 3).
TxVersion is the current latest supported transaction version.
WitnessEncoding encodes all messages other than transaction messages using the default Bitcoin wire protocol specification.

# Variables

ErrInvalidNetAddr describes an error that indicates the caller didn't specify a TCP address as required.
LatestEncoding is the most recently specified encoding for the Bitcoin wire protocol.

# Structs

Alert contains the data deserialized from the MsgAlert payload.
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 addr message.
MsgAlert implements the Message interface and defines a bitcoin alert message.
MsgBlock implements the Message interface and represents a bitcoin block 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.
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.
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.
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.
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.
InvType represents the allowed types of inventory vectors.
WireEncoding 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.