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.
This commit is contained in:
Sean Gilligan 2023-03-30 12:50:37 -07:00 committed by Andreas Schildbach
parent 8c6e584e02
commit 16faad2a30
4 changed files with 50 additions and 4 deletions

View File

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

View File

@ -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;
}

View File

@ -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;

View File

@ -46,7 +46,7 @@ public class RegTestParams extends BitcoinNetworkParams {
subsidyDecreaseBlockCount = 150;
port = 18444;
packetMagic = 0xfabfb5daL;
packetMagic = 0xfabfb5da;
dumpedPrivateKeyHeader = 239;
addressHeader = 111;
p2shHeader = 196;