Coinbits Documentation

Note

This library assumes you have a working familiarity with the Bitcoin Protocol.

Coinbits provides the basic serialization / deserialization code necessary to operate as a peer on the Bitcoin network. Many utilities are provided to help with buffering input, creating transactions, and key management.

This library could be used to do any of the following things easily:

  • To create a full Peer node that accepts and validates transactions, stores blocks, and responds to inventory requests
  • To query blocks from nodes on the network
  • To map the bitcoin network, asking each peer for a list of peers, then those peers for peers, etc.

Basically, anything that requires interaction on the P2P network could utilize this library.

Quick Example

Coinbits includes a basic client example for interacting on the peer-to-peer network. Here’s an example of a client that extends BitcoinClient and requests information on a block hash:

from coinbits.client import BitcoinClient
from coinbits.protocol.serializers import GetBlocks


class MyClient(BitcoinClient):
    def message_received(self, message_header, message):
        print "Got a message:", message_header.command, message
        super(MyClient, self).message_received(message_header, message)

    def send_message(self, message):
        print "Sending a message:", str(message)
        super(MyClient, self).send_message(message)

    def connected(self):
        hash = int('00000000000000000f69e991ee47a3536770f5d452967ec7edeb8d8cb28f9f28', 16)
        gh = GetBlocks([hash])
        self.send_message(gh)

    def handle_inv(self, message_header, message):
        print "Got some inventory:", message

MyClient("bitcoin.sipa.be").loop()

The connected method will be called as soon as the client has connected and finished handshaking. The serializers module contains all of the messages that can be serialized on the network, like the GetBlocks message command (described here). In this case, the send_message and message_received methods have been overwritten just for debugging. The handle_inv method is an example of the method dispatch - any message command type can have an associated handle_* method that will be called whenever a message of that type is received.

Indices and tables