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
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionBitcoinSerializer(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
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.booleanGet the network parameters for this serializer.intGet the protocol version of this serializer.inthashCode()booleanWhether the serializer will produce cached mode MessagesmakeAddressV1Message(byte[] payloadBytes, int length) Make an address message from the payload.makeAddressV2Message(byte[] payloadBytes, int length) Make an address message from the payload.makeBlock(byte[] payloadBytes, int offset, int length) Make a block from the payload.makeBloomFilter(byte[] payloadBytes) Make an filter message from the payload.makeFilteredBlock(byte[] payloadBytes) Make a filtered block from the payload.makeInventoryMessage(byte[] payloadBytes, int length) Make an inventory message from the payload.makeTransaction(byte[] payloadBytes, int offset, int length, byte[] hashFromHeader) Make a transaction from the payload.voidvoidserialize(String name, byte[] message, OutputStream out) Writes message to to the output stream.voidserialize(Message message, OutputStream out) Writes message to to the output stream.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 Details
-
BitcoinSerializer
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
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 Details
-
withProtocolVersion
Description copied from class:MessageSerializerCreate a new serializer with a specific protocol version. Mainly used to disable SegWit when parsing transactions.- Specified by:
withProtocolVersionin classMessageSerializer
-
getProtocolVersion
public int getProtocolVersion()Description copied from class:MessageSerializerGet the protocol version of this serializer.- Specified by:
getProtocolVersionin classMessageSerializer
-
serialize
Writes message to to the output stream.- Specified by:
serializein classMessageSerializer- Throws:
IOException
-
serialize
Writes message to to the output stream.- Specified by:
serializein classMessageSerializer- Throws:
IOException
-
deserialize
Reads a message from the given ByteBuffer and returns it.- Specified by:
deserializein classMessageSerializer- Throws:
ProtocolExceptionIOException
-
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:
deserializeHeaderin classMessageSerializer- Throws:
ProtocolExceptionIOException
-
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:
deserializePayloadin classMessageSerializer- Throws:
ProtocolExceptionBufferUnderflowException
-
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:
makeAddressV1Messagein 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:
makeAddressV2Messagein classMessageSerializer- Throws:
ProtocolException
-
makeBlock
Make a block from the payload. Extension point for alternative serialization format support.- Specified by:
makeBlockin classMessageSerializer- Throws:
ProtocolException
-
makeBloomFilter
Make an filter message from the payload. Extension point for alternative serialization format support.- Specified by:
makeBloomFilterin classMessageSerializer- Throws:
ProtocolException
-
makeFilteredBlock
Make a filtered block from the payload. Extension point for alternative serialization format support.- Specified by:
makeFilteredBlockin 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:
makeInventoryMessagein 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:
makeTransactionin classMessageSerializer- Throws:
ProtocolException
-
seekPastMagicBytes
- Specified by:
seekPastMagicBytesin classMessageSerializer- Throws:
BufferUnderflowException
-
isParseRetainMode
public boolean isParseRetainMode()Whether the serializer will produce cached mode Messages- Specified by:
isParseRetainModein classMessageSerializer
-
equals
-
hashCode
public int hashCode()
-