API Documentation

All modules listed below are under the “protocoin” module.

Fields

class protocoin.fields.Field

Base class for the Fields. This class only implements the counter to keep the order of the fields on the serializer classes.

deserialize(stream)

This method must read the stream data and then deserialize and return the deserialized content.

Returns:the deserialized content
Parameters:stream – stream of data to read
parse(value)

This method should be implemented to parse the value parameter into the field internal representation.

Parameters:value – value to be parsed
serialize()

Serialize the internal representation and return the serialized data.

Returns:the serialized data
class protocoin.fields.FixedStringField(length)

A fixed length string field.

Example of use:

class MessageHeaderSerializer(Serializer):
    model_class = MessageHeader
    magic = fields.UInt32LEField()
    command = fields.FixedStringField(12)
    length = fields.UInt32LEField()
    checksum = fields.UInt32LEField()
class protocoin.fields.Hash

A hash type field.

protocoin.fields.INVENTORY_TYPE = {'MSG_BLOCK': 2, 'MSG_TX': 1, 'ERROR': 0}

The type of the inventories

class protocoin.fields.IPv4AddressField

An IPv4 address field without timestamp and reserved IPv6 space.

class protocoin.fields.Int16LEField

16-bit little-endian integer field.

class protocoin.fields.Int32LEField

32-bit little-endian integer field.

class protocoin.fields.Int64LEField

64-bit little-endian integer field.

class protocoin.fields.ListField(serializer_class)

A field used to serialize/deserialize a list of serializers.

Example of use:

class TxSerializer(Serializer):
    model_class = Tx
    version = fields.UInt32LEField()
    tx_in = fields.ListField(TxInSerializer)
    tx_out = fields.ListField(TxOutSerializer)
    lock_time = fields.UInt32LEField()
protocoin.fields.MAGIC_VALUES = {'amecoin': 4273258233, 'testnet': 3669344250, 'main': 3652501241, 'testnet3': 118034699}

The network magic values

class protocoin.fields.NestedField(serializer_class)

A field used to nest another serializer.

Example of use:

class TxInSerializer(Serializer):
    model_class = TxIn
    previous_output = fields.NestedField(OutPointSerializer)
    signature_script = fields.VariableStringField()
    sequence = fields.UInt32LEField()
protocoin.fields.PROTOCOL_VERSION = 60002

The protocol version

class protocoin.fields.PrimaryField

This is a base class for all fields that has only one value and their value can be represented by a Python struct datatype.

Example of use:

class UInt32LEField(PrimaryField):
    datatype = "<I"
deserialize(stream)

Deserialize the stream using the struct data type specified.

Parameters:stream – the data stream
parse(value)

This method will set the internal value to the specified value.

Parameters:value – the value to be set
serialize()

Serialize the internal data and then return the serialized data.

protocoin.fields.SERVICES = {'NODE_NETWORK': 1}

The available services

class protocoin.fields.UInt16BEField

16-bit big-endian unsigned integer field.

class protocoin.fields.UInt16LEField

16-bit little-endian unsigned integer field.

class protocoin.fields.UInt32LEField

32-bit little-endian unsigned integer field.

class protocoin.fields.UInt64LEField

64-bit little-endian unsigned integer field.

class protocoin.fields.VariableIntegerField

A variable size integer field.

class protocoin.fields.VariableStringField

A variable length string field.

Serializers

class protocoin.serializers.AddressVector

A vector of addresses.

class protocoin.serializers.AddressVectorSerializer

Serializer for the addresses vector.

model_class

alias of AddressVector

class protocoin.serializers.Block

The block message. This message contains all the transactions present in the block.

class protocoin.serializers.BlockHeader

The header of the block.

calculate_hash()

This method will calculate the hash of the block.

class protocoin.serializers.BlockHeaderSerializer

The serializer for the block header.

model_class

alias of BlockHeader

class protocoin.serializers.BlockSerializer

The deserializer for the blocks.

model_class

alias of Block

class protocoin.serializers.GetData

GetData message command.

class protocoin.serializers.GetDataSerializer

Serializer for the GetData command.

model_class

alias of GetData

class protocoin.serializers.HeaderVector

The header only vector.

class protocoin.serializers.HeaderVectorSerializer

Serializer for the block header vector.

model_class

alias of HeaderVector

class protocoin.serializers.IPv4Address

The IPv4 Address (without timestamp).

class protocoin.serializers.IPv4AddressSerializer

Serializer for the IPv4Address.

model_class

alias of IPv4Address

class protocoin.serializers.IPv4AddressTimestamp

The IPv4 Address with timestamp.

class protocoin.serializers.IPv4AddressTimestampSerializer

Serializer for the IPv4AddressTimestamp.

model_class

alias of IPv4AddressTimestamp

class protocoin.serializers.Inventory

The Inventory representation.

type_to_text()

Converts the inventory type to text representation.

class protocoin.serializers.InventorySerializer

The serializer for the Inventory.

model_class

alias of Inventory

class protocoin.serializers.InventoryVector

A vector of inventories.

class protocoin.serializers.InventoryVectorSerializer

The serializer for the vector of inventories.

model_class

alias of InventoryVector

class protocoin.serializers.MemPool

The mempool command.

class protocoin.serializers.MemPoolSerializer

The serializer for the mempool command.

model_class

alias of MemPool

class protocoin.serializers.MessageHeader

The header of all bitcoin messages.

class protocoin.serializers.MessageHeaderSerializer

Serializer for the MessageHeader.

static calc_checksum(payload)

Calculate the checksum of the specified payload.

Parameters:payload – The binary data payload.
model_class

alias of MessageHeader

class protocoin.serializers.NotFound

NotFound command message.

class protocoin.serializers.NotFoundSerializer

Serializer for the NotFound message.

model_class

alias of NotFound

class protocoin.serializers.OutPoint

The OutPoint representation.

class protocoin.serializers.OutPointSerializer

The OutPoint representation serializer.

model_class

alias of OutPoint

class protocoin.serializers.Ping

The ping command, which should always be answered with a Pong.

class protocoin.serializers.PingSerializer

The ping command serializer.

model_class

alias of Ping

class protocoin.serializers.Pong

The pong command, usually returned when a ping command arrives.

class protocoin.serializers.PongSerializer

The pong command serializer.

model_class

alias of Pong

class protocoin.serializers.Serializer

The main serializer class, inherit from this class to create custom serializers.

Example of use:

class VerAckSerializer(Serializer):
    model_class = VerAck
deserialize(stream)

This method will read the stream and then will deserialize the binary data information present on it.

Parameters:stream – A file-like object (StringIO, file, socket, etc.)
serialize(obj, fields=None)

This method will receive an object and then will serialize it according to the fields declared on the serializer.

Parameters:obj – The object to serializer.
class protocoin.serializers.SerializerABC

The serializer abstract base class.

class protocoin.serializers.SerializerMeta

The serializer meta class. This class will create an attribute called ‘_fields’ in each serializer with the ordered dict of fields present on the subclasses.

classmethod get_fields(meta, bases, attrs, field_class)

This method will construct an ordered dict with all the fields present on the serializer classes.

class protocoin.serializers.Tx

The main transaction representation, this object will contain all the inputs and outputs of the transaction.

class protocoin.serializers.TxIn

The transaction input representation.

class protocoin.serializers.TxInSerializer

The transaction input serializer.

model_class

alias of TxIn

class protocoin.serializers.TxOut

The transaction output.

class protocoin.serializers.TxOutSerializer

The transaction output serializer.

model_class

alias of TxOut

class protocoin.serializers.TxSerializer

The transaction serializer.

model_class

alias of Tx

class protocoin.serializers.VerAck

The version acknowledge (verack) command.

class protocoin.serializers.VerAckSerializer

The serializer for the verack command.

model_class

alias of VerAck

class protocoin.serializers.Version

The version command.

class protocoin.serializers.VersionSerializer

The version command serializer.

model_class

alias of Version

Clients

class protocoin.clients.BitcoinBasicClient(socket)

The base class for a Bitcoin network client, this class implements utility functions to create your own class.

close_stream()

This method will close the socket stream.

handle_message_header(message_header, payload)

This method will be called for every message before the message payload deserialization.

Parameters:
  • message_header – The message header
  • payload – The payload of the message
handle_send_message(message_header, message)

This method will be called for every sent message.

Parameters:
  • message_header – The header of the message
  • message – The message to be sent
loop()

This is the main method of the client, it will enter in a receive/send loop.

receive_message()

This method is called inside the loop() method to receive a message from the stream (socket) and then deserialize it.

send_message(message, serializer)

This method will serialize the message using the specified serializer and then it will send it to the socket stream.

Parameters:
  • message – The message object to send
  • serializer – The serializar of the message
class protocoin.clients.BitcoinClient(socket)

This class implements all the protocol rules needed for a client to stay up in the network. It will handle the handshake rules as well answer the ping messages.

handle_ping(message_header, message)

This method will handle the Ping message and then will answer every Ping message with a Pong message using the nonce received.

Parameters:
  • message_header – The header of the Ping message
  • message – The Ping message
handle_version(message_header, message)

This method will handle the Version message and will send a VerAck message when it receives the Version message.

Parameters:
  • message_header – The Version message header
  • message – The Version message
handshake()

This method will implement the handshake of the Bitcoin protocol. It will send the Version message.

Read the Docs v: v0.1
Versions
latest
v0.1
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.