Package org.bitcoinj.core
Class BitcoinSerializer
java.lang.Object
org.bitcoinj.core.MessageSerializer
org.bitcoinj.core.BitcoinSerializer
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
-
Constructor Summary
ConstructorDescriptionBitcoinSerializer
(Network network) Constructs a BitcoinSerializer with the given behavior.BitcoinSerializer
(Network network, int protocolVersion) Constructs a BitcoinSerializer with the given behavior.BitcoinSerializer
(NetworkParameters params) Deprecated.BitcoinSerializer
(NetworkParameters params, int protocolVersion) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionReads a message from the given ByteBuffer and returns it.Deserializes only the header in case packet meta data is needed before decoding the payload.Deserialize payload only.Deprecated.int
Get the protocol version of this serializer.makeAddressV1Message
(ByteBuffer payload) Make an address message from the payload.makeAddressV2Message
(ByteBuffer payload) Make an address message from the payload.makeBlock
(ByteBuffer payload) Make a block from the payload.makeBloomFilter
(ByteBuffer payload) Make an filter message from the payload.makeFilteredBlock
(ByteBuffer payload) Make a filtered block from the payload.makeInventoryMessage
(ByteBuffer payload) Make an inventory message from the payload.makeTransaction
(ByteBuffer payload) Make a transaction from the payload.void
void
serialize
(String name, byte[] message, OutputStream out) Writes message to the output stream.void
serialize
(Message message, OutputStream out) Writes message to the output stream.withProtocolVersion
(int protocolVersion) Create a new serializer with a specific protocol version.
-
Constructor Details
-
BitcoinSerializer
Deprecated.Constructs a BitcoinSerializer with the given behavior.- Parameters:
params
- networkParams used to determine packetMagic
-
BitcoinSerializer
Constructs a BitcoinSerializer with the given behavior.- Parameters:
network
- used to determine packetMagic
-
BitcoinSerializer
Deprecated.Constructs a BitcoinSerializer with the given behavior.- Parameters:
params
- networkParams used to determine packetMagicprotocolVersion
- the protocol version to use
-
BitcoinSerializer
Constructs a BitcoinSerializer with the given behavior.- Parameters:
network
- used to determine packetMagicprotocolVersion
- the protocol version to use
-
-
Method Details
-
withProtocolVersion
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
Writes message to the output stream.- Specified by:
serialize
in classMessageSerializer
- Throws:
IOException
-
serialize
Writes message to the output stream.- Specified by:
serialize
in classMessageSerializer
- Throws:
IOException
-
deserialize
Reads a message from the given ByteBuffer and returns it.- Specified by:
deserialize
in classMessageSerializer
- Throws:
ProtocolException
IOException
-
deserializeHeader
public BitcoinSerializer.BitcoinPacketHeader deserializeHeader(ByteBuffer in) throws ProtocolException, 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
IOException
-
deserializePayload
public Message deserializePayload(BitcoinSerializer.BitcoinPacketHeader header, ByteBuffer in) throws ProtocolException, BufferUnderflowException Deserialize payload only. You must provide a header, typically obtained by callingdeserializeHeader(java.nio.ByteBuffer)
.- Specified by:
deserializePayload
in classMessageSerializer
- Throws:
ProtocolException
BufferUnderflowException
-
getParameters
Deprecated.Get the network parameters for this serializer. -
makeAddressV1Message
Make an address message from the payload. Extension point for alternative serialization format support.- Specified by:
makeAddressV1Message
in classMessageSerializer
- Throws:
ProtocolException
-
makeAddressV2Message
Make an address message from the payload. Extension point for alternative serialization format support.- Specified by:
makeAddressV2Message
in classMessageSerializer
- Throws:
ProtocolException
-
makeBlock
Make a block from the payload. Extension point for alternative serialization format support.- Specified by:
makeBlock
in classMessageSerializer
- Throws:
ProtocolException
-
makeBloomFilter
Make an filter message from the payload. Extension point for alternative serialization format support.- Specified by:
makeBloomFilter
in classMessageSerializer
- Throws:
ProtocolException
-
makeFilteredBlock
Make a filtered block from the payload. Extension point for alternative serialization format support.- Specified by:
makeFilteredBlock
in classMessageSerializer
- Throws:
ProtocolException
-
makeInventoryMessage
Make an inventory message from the payload. Extension point for alternative serialization format support.- Specified by:
makeInventoryMessage
in classMessageSerializer
- Throws:
ProtocolException
-
makeTransaction
Make a transaction from the payload. Extension point for alternative serialization format support.- Specified by:
makeTransaction
in classMessageSerializer
- Throws:
ProtocolException
-
seekPastMagicBytes
- Specified by:
seekPastMagicBytes
in classMessageSerializer
- Throws:
BufferUnderflowException
-