From 16faad2a308f177eab232ea2413ad2360c4b49dc Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Thu, 30 Mar 2023 12:50:37 -0700 Subject: [PATCH] NetworkParameters: change `packetMagic` to an `int` It's just a binary value and does not have signedness. Methods for writing 32-bit integers via `ByteUtils.writeInt32BE(int, ...)` have been added. --- .../org/bitcoinj/base/internal/ByteUtils.java | 46 +++++++++++++++++++ .../org/bitcoinj/core/NetworkParameters.java | 4 +- .../org/bitcoinj/params/MainNetParams.java | 2 +- .../org/bitcoinj/params/RegTestParams.java | 2 +- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java b/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java index 6a69a8056..cae8a3209 100644 --- a/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java +++ b/core/src/main/java/org/bitcoinj/base/internal/ByteUtils.java @@ -235,6 +235,21 @@ public class ByteUtils { return buf.order(ByteOrder.BIG_ENDIAN).putInt((int) val); } + /** + * Write a 32-bit integer to a given buffer in big-endian format. + *

+ * The value is expected as a signed or unsigned {@code int}. If you've got an unsigned {@code long} as per the + * Java Unsigned Integer API, use {@link #writeInt32BE(long, ByteBuffer)}. + * + * @param val value to be written + * @param buf buffer to be written into + * @return the buffer + * @throws BufferOverflowException if the value doesn't fit the remaining buffer + */ + public static ByteBuffer writeInt32BE(int val, ByteBuffer buf) throws BufferOverflowException { + return buf.order(ByteOrder.BIG_ENDIAN).putInt((int) val); + } + /** * Write a 32-bit integer to a given byte array in big-endian format, starting at a given offset. *

@@ -252,6 +267,22 @@ public class ByteUtils { writeInt32BE(val, ByteBuffer.wrap(out, offset, out.length - offset)); } + /** + * Write a 32-bit integer to a given byte array in big-endian format, starting at a given offset. + *

+ * The value is expected as a signed or unsigned {@code int}. If you've got an unsigned {@code long} as per the + * Java Unsigned Integer API, use {@link #writeInt32BE(long, byte[], int)}. + * + * @param val value to be written + * @param out buffer to be written into + * @param offset offset into the buffer + * @throws ArrayIndexOutOfBoundsException if offset points outside of the buffer, or + * if the value doesn't fit the remaining buffer + */ + public static void writeInt32BE(int val, byte[] out, int offset) throws ArrayIndexOutOfBoundsException { + writeInt32BE(val, ByteBuffer.wrap(out, offset, out.length - offset)); + } + /** * Write a 64-bit integer to a given buffer in little-endian format. *

@@ -359,6 +390,21 @@ public class ByteUtils { stream.write(buf); } + /** + * Write a 32-bit integer to a given output stream in big-endian format. + *

+ * The value is expected as a signed or unsigned {@code int}. + * + * @param val value to be written + * @param stream strean to be written into + * @throws IOException if an I/O error occurs + */ + public static void writeInt32BE(int val, OutputStream stream) throws IOException { + byte[] buf = new byte[4]; + writeInt32BE(val, ByteBuffer.wrap(buf)); + stream.write(buf); + } + /** * Write a 64-bit integer to a given output stream in little-endian format. *

diff --git a/core/src/main/java/org/bitcoinj/core/NetworkParameters.java b/core/src/main/java/org/bitcoinj/core/NetworkParameters.java index 6e92045b6..43c18ab80 100644 --- a/core/src/main/java/org/bitcoinj/core/NetworkParameters.java +++ b/core/src/main/java/org/bitcoinj/core/NetworkParameters.java @@ -69,7 +69,7 @@ public abstract class NetworkParameters { protected BigInteger maxTarget; protected int port; - protected long packetMagic; // Indicates message origin network and is used to seek to the next message when stream state is unknown. + protected int packetMagic; // Indicates message origin network and is used to seek to the next message when stream state is unknown. protected int addressHeader; protected int p2shHeader; protected int dumpedPrivateKeyHeader; @@ -300,7 +300,7 @@ public abstract class NetworkParameters { * The header bytes that identify the start of a packet on this network. * @return header bytes as a long */ - public long getPacketMagic() { + public int getPacketMagic() { return packetMagic; } diff --git a/core/src/main/java/org/bitcoinj/params/MainNetParams.java b/core/src/main/java/org/bitcoinj/params/MainNetParams.java index b49ed3f65..3362d5d11 100644 --- a/core/src/main/java/org/bitcoinj/params/MainNetParams.java +++ b/core/src/main/java/org/bitcoinj/params/MainNetParams.java @@ -44,7 +44,7 @@ public class MainNetParams extends BitcoinNetworkParams { maxTarget = ByteUtils.decodeCompactBits(Block.STANDARD_MAX_DIFFICULTY_TARGET); port = 8333; - packetMagic = 0xf9beb4d9L; + packetMagic = 0xf9beb4d9; dumpedPrivateKeyHeader = 128; addressHeader = 0; p2shHeader = 5; diff --git a/core/src/main/java/org/bitcoinj/params/RegTestParams.java b/core/src/main/java/org/bitcoinj/params/RegTestParams.java index b4f41c674..e56c968f4 100644 --- a/core/src/main/java/org/bitcoinj/params/RegTestParams.java +++ b/core/src/main/java/org/bitcoinj/params/RegTestParams.java @@ -46,7 +46,7 @@ public class RegTestParams extends BitcoinNetworkParams { subsidyDecreaseBlockCount = 150; port = 18444; - packetMagic = 0xfabfb5daL; + packetMagic = 0xfabfb5da; dumpedPrivateKeyHeader = 239; addressHeader = 111; p2shHeader = 196;