spiffy_node library

BitcoinSV P2P Library for Dart

A comprehensive library for connecting to and interacting with the BitcoinSV peer-to-peer network. This library provides interfaces and implementations for peer management, wire protocol messages, and network communication.

Usage

import 'package:bsv_p2p/spiffy_node.dart';

// Create a peer manager
final peerManager = PeerManager(
  logger: logger,
  network: BitcoinNetwork.testnet,
);

// Create and add a peer
final peer = Peer(
  address: 'localhost',
  port: 58333,
  network: BitcoinNetwork.testnet,
  handler: handler,
  logger: logger,
);

await peerManager.addPeer(peer);
await peer.connect();

Classes

BlockHeader
BlockHeader defines information about a block and is used in the bitcoin block (MsgBlock) and headers (MsgHeaders) messages.
ChainTip
Represents the consensus chain tip
ChainTipEvent
Event fired when chain tip changes
ChainTipTracker
Tracks blockchain tips across multiple peers for SPV clients
ChainTipTrackerConfig
Configuration for chain tip tracker
Commands
Bitcoin protocol commands
ConnectionManager
Manages multiple peer connections with health monitoring and reconnection
ConnectionManagerConfig
Configuration for connection manager
Hash
Hash represents a Bitcoin hash (typically double SHA256)
HashUtils
Utility functions for hash creation
InvType
Types of inventory items
InvVect
Inventory vector represents an item in an inventory message
MessageFactory
Registry and factory for Bitcoin protocol messages
MessageHeader
Bitcoin message header
MessagePriorityHelper
Utility functions for determining message priority
MessageProcessor
Processes raw data streams into Bitcoin protocol messages
MessageQueue
Outbound message queue with priority handling and overflow protection
MessageQueueConfig
Configuration for message queue behavior
MsgAddr
Bitcoin address message for sharing peer addresses This message is used to provide information about other peers on the network. It can be sent unsolicited or in response to a getaddr message.
MsgBlock
MsgBlock implements the Message interface and represents a bitcoin block message. It is used to deliver block and transaction information in response to a getdata message (MsgGetData) for a given block hash.
MsgGetData
Bitcoin getdata message for requesting specific data This message is used to request one or more data objects from another node. The objects are requested by an inventory vector list.
MsgGetHeaders
MsgGetHeaders implements the Message interface and represents a bitcoin getheaders message. It is used to request a list of block headers for blocks starting after the last known hash in the slice of block locator hashes. The list is returned via a headers message (MsgHeaders) and is limited by a specific hash to stop at or the maximum number of block headers per message, which is currently 2000.
MsgHeaders
MsgHeaders implements the Message interface and represents a bitcoin headers message. It is used to deliver block header information in response to a getheaders message (MsgGetHeaders). The maximum number of block headers per message is currently 2000. See MsgGetHeaders for details on requesting the headers.
MsgInv
Bitcoin inventory message for announcing available data This message is used to announce the knowledge of one or more objects. It can be received unsolicited, or in reply to getblocks.
MsgMemPool
MsgMemPool implements the Message interface and represents a bitcoin mempool message. It is used to request a list of transactions still in the active memory pool of a relay.
MsgPing
Bitcoin ping message for keepalive
MsgPong
Bitcoin pong message sent in response to ping
MsgReject
MsgReject implements the Message interface and represents a bitcoin reject message.
MsgSendHeaders
Bitcoin send headers message This message tells a peer to send headers directly for new blocks instead of inventory (INV) messages. This is more efficient for SPV clients.
MsgTx
Bitcoin transaction message This message contains a complete Bitcoin transaction with inputs and outputs.
MsgVerAck
Bitcoin version acknowledgment message This is sent in response to a version message to acknowledge protocol version
MsgVersion
Bitcoin version message for protocol negotiation
NetworkAddress
Network address structure for Bitcoin protocol
Peer
High-level BitcoinSV peer implementation
PeerChainTip
Represents a blockchain tip from a specific peer
PeerConfig
Configuration for peer behavior
PeerConnection
Represents a TCP connection to a BitcoinSV peer
PeerConnectionConfig
Configuration for peer connection
PeerHandlerI
Interface for handling incoming messages from peers
PeerI
Interface for individual peer connections to BitcoinSV network
PeerManager
High-level peer manager implementing PeerManagerI
PeerManagerConfig
Configuration for peer manager behavior
PeerManagerI
Interface for managing multiple peers in the BitcoinSV network
ProcessingResult
Result of message processing attempt
QueuedMessage
Queued message with metadata
RegisterableMessage
Base class for messages that can be registered with the factory
ServiceFlags
Services flags for Bitcoin nodes
TxIn
Bitcoin transaction input
TxOut
Bitcoin transaction output
VarInt
Helper class for reading variable-length integers
WireMessage
Abstract base class for all wire protocol messages

Enums

BitcoinNetwork
Bitcoin network types
ChainTipEventType
Type of chain tip event
ChainTipSource
Source of chain tip information
ConnectionState
Connection state enum
MessageEncoding
Message encoding types
MessagePriority
Priority levels for messages
PeerState
Peer connection states for Bitcoin protocol
ProcessingState
State of message processing pipeline
RejectCode
RejectCode represents a numeric value by which a remote peer indicates why a message was rejected.

Extensions

ConnectionManagerExtensions on ConnectionManager
Extension methods for connection management
PeerManagerExtensions on PeerManager
Extension methods for PeerManager

Constants

blockHeaderLen → const int
Block header length in bytes (80 bytes total).
commandSize → const int
Size of command field in message header
defaultExcessiveBlockSize → const int
Default excessive block size (32MB)
defaultTransactionAlloc → const int
Default size for transaction allocation to avoid excessive memory growth.
hashSize → const int
Size of Bitcoin hashes in bytes (SHA256)
latestEncoding → const MessageEncoding
Latest encoding version
maxBlockHeaderPayload → const int
Maximum number of bytes a block header can be. Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes + PrevBlock and MerkleRoot hashes (32 bytes each).
maxBlockHeadersPerMsg → const int
MaxBlockHeadersPerMsg is the maximum number of block headers that can be in a single bitcoin headers message.
maxBlockLocatorsPerMsg → const int
Maximum number of block locator hashes per getheaders message. This is based on the Bitcoin protocol specification.
maxBlocksPerMsg → const int
Maximum number of blocks per message.
maxHashStringSize → const int
Maximum length of hash string representation
messageHeaderSize → const int
Size of message header in bytes

Functions

initializeMessages() → void
Initialize all message types with the message factory
maxMessagePayload() int
Get maximum message payload size
setLimits(int excessiveBlockSize) → void
Set limits based on excessive block size

Typedefs

MessageDeserializer = WireMessage Function(Uint8List data, int protocolVersion, MessageEncoding encoding)
Function signature for message deserializers

Exceptions / Errors

ConnectionException
Exception thrown when connection fails to establish
ConnectionLostException
Exception thrown when connection is unexpectedly lost
ConnectionTimeoutException
Exception thrown when connection times out
HashStringSizeException
Exception for invalid hash string sizes
MessageProcessingException
Exception thrown when message processing fails
MessageQueueOverflowException
Exception thrown when message queue overflows
NetworkException
Base exception for network-related errors
PeerMisbehaviorException
Exception thrown when peer misbehaves
PeerNetworkMismatchException
Exception thrown when peer networks don't match
WireException
Exception for wire protocol errors