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)
Constructs a BitcoinSerializer with the given behavior.BitcoinSerializer(NetworkParameters params, int protocolVersion)
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.NetworkParameters
getParameters()
Get the network parameters for this serializer.int
getProtocolVersion()
Get the protocol version of this serializer.AddressV1Message
makeAddressV1Message(java.nio.ByteBuffer payload)
Make an address message from the payload.AddressV2Message
makeAddressV2Message(java.nio.ByteBuffer payload)
Make an address message from the payload.Block
makeBlock(java.nio.ByteBuffer payload)
Make a block from the payload.Message
makeBloomFilter(java.nio.ByteBuffer payload)
Make an filter message from the payload.FilteredBlock
makeFilteredBlock(java.nio.ByteBuffer payload)
Make a filtered block from the payload.InventoryMessage
makeInventoryMessage(java.nio.ByteBuffer payload)
Make an inventory message from the payload.Transaction
makeTransaction(java.nio.ByteBuffer payload)
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.
-
-
-
Constructor Detail
-
BitcoinSerializer
public BitcoinSerializer(NetworkParameters params)
Constructs a BitcoinSerializer with the given behavior.- Parameters:
params
- networkParams used to create Messages instances and determining packetMagic
-
BitcoinSerializer
public BitcoinSerializer(NetworkParameters params, int protocolVersion)
Constructs a BitcoinSerializer with the given behavior.- Parameters:
params
- networkParams used to create Messages instances and determining packetMagicprotocolVersion
- the protocol version to use
-
-
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(java.nio.ByteBuffer payload) 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(java.nio.ByteBuffer payload) 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(java.nio.ByteBuffer payload) 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(java.nio.ByteBuffer payload) 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(java.nio.ByteBuffer payload) 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(java.nio.ByteBuffer payload) 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(java.nio.ByteBuffer payload) 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
-
-