Package org.bitcoinj.core
Class BitcoinSerializer
- java.lang.Object
-
- org.bitcoinj.core.MessageSerializer
-
- org.bitcoinj.core.BitcoinSerializer
-
public class BitcoinSerializer extends MessageSerializer
Methods to serialize and de-serialize messages to the Bitcoin network format as defined in the protocol specification.
To be able to serialize and deserialize new Message subclasses the following criteria needs to be met.
- The proper Class instance needs to be mapped to its message name in the names variable below
- There needs to be a constructor matching: NetworkParameters params, byte[] payload
- Message.bitcoinSerializeToStream() needs to be properly subclassed
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BitcoinSerializer.BitcoinPacketHeader
-
Constructor Summary
Constructors Constructor Description BitcoinSerializer(NetworkParameters params, boolean parseRetain)
Constructs a BitcoinSerializer with the given behavior.BitcoinSerializer(NetworkParameters params, int protocolVersion, boolean parseRetain)
Constructs a BitcoinSerializer with the given behavior.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Message
deserialize(java.nio.ByteBuffer in)
Reads a message from the given ByteBuffer and returns it.BitcoinSerializer.BitcoinPacketHeader
deserializeHeader(java.nio.ByteBuffer in)
Deserializes only the header in case packet meta data is needed before decoding the payload.Message
deserializePayload(BitcoinSerializer.BitcoinPacketHeader header, java.nio.ByteBuffer in)
Deserialize payload only.boolean
equals(java.lang.Object o)
NetworkParameters
getParameters()
Get the network parameters for this serializer.int
getProtocolVersion()
Get the protocol version of this serializer.int
hashCode()
boolean
isParseRetainMode()
Whether the serializer will produce cached mode MessagesAddressV1Message
makeAddressV1Message(byte[] payloadBytes, int length)
Make an address message from the payload.AddressV2Message
makeAddressV2Message(byte[] payloadBytes, int length)
Make an address message from the payload.Block
makeBlock(byte[] payloadBytes, int offset, int length)
Make a block from the payload.Message
makeBloomFilter(byte[] payloadBytes)
Make an filter message from the payload.FilteredBlock
makeFilteredBlock(byte[] payloadBytes)
Make a filtered block from the payload.InventoryMessage
makeInventoryMessage(byte[] payloadBytes, int length)
Make an inventory message from the payload.Transaction
makeTransaction(byte[] payloadBytes, int offset, int length, byte[] hashFromHeader)
Make a transaction from the payload.void
seekPastMagicBytes(java.nio.ByteBuffer in)
void
serialize(java.lang.String name, byte[] message, java.io.OutputStream out)
Writes message to to the output stream.void
serialize(Message message, java.io.OutputStream out)
Writes message to to the output stream.BitcoinSerializer
withProtocolVersion(int protocolVersion)
Create a new serializer with a specific protocol version.-
Methods inherited from class org.bitcoinj.core.MessageSerializer
makeBlock, makeBlock, makeTransaction, makeTransaction
-
-
-
-
Constructor Detail
-
BitcoinSerializer
public BitcoinSerializer(NetworkParameters params, boolean parseRetain)
Constructs a BitcoinSerializer with the given behavior.- Parameters:
params
- networkParams used to create Messages instances and determining packetMagicparseRetain
- retain the backing byte array of a message for fast reserialization.
-
BitcoinSerializer
public BitcoinSerializer(NetworkParameters params, int protocolVersion, boolean parseRetain)
Constructs a BitcoinSerializer with the given behavior.- Parameters:
params
- networkParams used to create Messages instances and determining packetMagicprotocolVersion
- the protocol version to useparseRetain
- retain the backing byte array of a message for fast reserialization.
-
-
Method Detail
-
withProtocolVersion
public BitcoinSerializer withProtocolVersion(int protocolVersion)
Description copied from class:MessageSerializer
Create a new serializer with a specific protocol version. Mainly used to disable SegWit when parsing transactions.- Specified by:
withProtocolVersion
in classMessageSerializer
-
getProtocolVersion
public int getProtocolVersion()
Description copied from class:MessageSerializer
Get the protocol version of this serializer.- Specified by:
getProtocolVersion
in classMessageSerializer
-
serialize
public void serialize(java.lang.String name, byte[] message, java.io.OutputStream out) throws java.io.IOException
Writes message to to the output stream.- Specified by:
serialize
in classMessageSerializer
- Throws:
java.io.IOException
-
serialize
public void serialize(Message message, java.io.OutputStream out) throws java.io.IOException
Writes message to to the output stream.- Specified by:
serialize
in classMessageSerializer
- Throws:
java.io.IOException
-
deserialize
public Message deserialize(java.nio.ByteBuffer in) throws ProtocolException, java.io.IOException
Reads a message from the given ByteBuffer and returns it.- Specified by:
deserialize
in classMessageSerializer
- Throws:
ProtocolException
java.io.IOException
-
deserializeHeader
public BitcoinSerializer.BitcoinPacketHeader deserializeHeader(java.nio.ByteBuffer in) throws ProtocolException, java.io.IOException
Deserializes only the header in case packet meta data is needed before decoding the payload. This method assumes you have already called seekPastMagicBytes()- Specified by:
deserializeHeader
in classMessageSerializer
- Throws:
ProtocolException
java.io.IOException
-
deserializePayload
public Message deserializePayload(BitcoinSerializer.BitcoinPacketHeader header, java.nio.ByteBuffer in) throws ProtocolException, java.nio.BufferUnderflowException
Deserialize payload only. You must provide a header, typically obtained by callingdeserializeHeader(java.nio.ByteBuffer)
.- Specified by:
deserializePayload
in classMessageSerializer
- Throws:
ProtocolException
java.nio.BufferUnderflowException
-
getParameters
public NetworkParameters getParameters()
Get the network parameters for this serializer.
-
makeAddressV1Message
public AddressV1Message makeAddressV1Message(byte[] payloadBytes, int length) throws ProtocolException
Make an address message from the payload. Extension point for alternative serialization format support.- Specified by:
makeAddressV1Message
in classMessageSerializer
- Throws:
ProtocolException
-
makeAddressV2Message
public AddressV2Message makeAddressV2Message(byte[] payloadBytes, int length) throws ProtocolException
Make an address message from the payload. Extension point for alternative serialization format support.- Specified by:
makeAddressV2Message
in classMessageSerializer
- Throws:
ProtocolException
-
makeBlock
public Block makeBlock(byte[] payloadBytes, int offset, int length) throws ProtocolException
Make a block from the payload. Extension point for alternative serialization format support.- Specified by:
makeBlock
in classMessageSerializer
- Throws:
ProtocolException
-
makeBloomFilter
public Message makeBloomFilter(byte[] payloadBytes) throws ProtocolException
Make an filter message from the payload. Extension point for alternative serialization format support.- Specified by:
makeBloomFilter
in classMessageSerializer
- Throws:
ProtocolException
-
makeFilteredBlock
public FilteredBlock makeFilteredBlock(byte[] payloadBytes) throws ProtocolException
Make a filtered block from the payload. Extension point for alternative serialization format support.- Specified by:
makeFilteredBlock
in classMessageSerializer
- Throws:
ProtocolException
-
makeInventoryMessage
public InventoryMessage makeInventoryMessage(byte[] payloadBytes, int length) throws ProtocolException
Make an inventory message from the payload. Extension point for alternative serialization format support.- Specified by:
makeInventoryMessage
in classMessageSerializer
- Throws:
ProtocolException
-
makeTransaction
public Transaction makeTransaction(byte[] payloadBytes, int offset, int length, byte[] hashFromHeader) throws ProtocolException
Make a transaction from the payload. Extension point for alternative serialization format support.- Specified by:
makeTransaction
in classMessageSerializer
- Throws:
ProtocolException
-
seekPastMagicBytes
public void seekPastMagicBytes(java.nio.ByteBuffer in) throws java.nio.BufferUnderflowException
- Specified by:
seekPastMagicBytes
in classMessageSerializer
- Throws:
java.nio.BufferUnderflowException
-
isParseRetainMode
public boolean isParseRetainMode()
Whether the serializer will produce cached mode Messages- Specified by:
isParseRetainMode
in classMessageSerializer
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-