mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-22 06:21:47 +01:00
org.bitcoinj.base: move Sha256Hash and byte/math Utils members to base
* move o.b.core.Sha256Hash to o.b.base.Sha256Hash * move byte and math utility methods from o.b.core.Utils to a new o.b.base.ByteUtils class
This commit is contained in:
parent
84e737acdf
commit
93cc0ac9c5
125 changed files with 989 additions and 836 deletions
|
@ -15,10 +15,11 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.bitcoinj.core;
|
||||
package org.bitcoinj.base;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
@ -67,7 +68,7 @@ public class Sha256Hash implements Comparable<Sha256Hash> {
|
|||
* hex string, or if it does not represent exactly 32 bytes
|
||||
*/
|
||||
public static Sha256Hash wrap(String hexString) {
|
||||
return wrap(Utils.HEX.decode(hexString));
|
||||
return wrap(ByteUtils.HEX.decode(hexString));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,7 +79,7 @@ public class Sha256Hash implements Comparable<Sha256Hash> {
|
|||
* @throws IllegalArgumentException if the given array length is not exactly 32
|
||||
*/
|
||||
public static Sha256Hash wrapReversed(byte[] rawHashBytes) {
|
||||
return wrap(Utils.reverseBytes(rawHashBytes));
|
||||
return wrap(ByteUtils.reverseBytes(rawHashBytes));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -236,14 +237,14 @@ public class Sha256Hash implements Comparable<Sha256Hash> {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Utils.HEX.encode(bytes);
|
||||
return ByteUtils.HEX.encode(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bytes interpreted as a positive integer.
|
||||
*/
|
||||
public BigInteger toBigInteger() {
|
||||
return Utils.bytesToBigInteger(bytes);
|
||||
return ByteUtils.bytesToBigInteger(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -257,7 +258,7 @@ public class Sha256Hash implements Comparable<Sha256Hash> {
|
|||
* Returns a reversed copy of the internal byte array.
|
||||
*/
|
||||
public byte[] getReversedBytes() {
|
||||
return Utils.reverseBytes(bytes);
|
||||
return ByteUtils.reverseBytes(bytes);
|
||||
}
|
||||
|
||||
@Override
|
359
core/src/main/java/org/bitcoinj/base/utils/ByteUtils.java
Normal file
359
core/src/main/java/org/bitcoinj/base/utils/ByteUtils.java
Normal file
|
@ -0,0 +1,359 @@
|
|||
/*
|
||||
* Copyright by the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.bitcoinj.base.utils;
|
||||
|
||||
import com.google.common.io.BaseEncoding;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
/**
|
||||
* Utility methods for bit, byte, and integer manipulation and conversion. Most of these were moved here
|
||||
* from {@code org.bitcoinj.core.Utils}.
|
||||
*/
|
||||
public class ByteUtils {
|
||||
/** Hex encoding used throughout the framework. Use with HEX.encode(byte[]) or HEX.decode(CharSequence). */
|
||||
public static final BaseEncoding HEX = BaseEncoding.base16().lowerCase();
|
||||
// 00000001, 00000010, 00000100, 00001000, ...
|
||||
private static final int[] bitMask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The regular {@link BigInteger#toByteArray()} includes the sign bit of the number and
|
||||
* might result in an extra byte addition. This method removes this extra byte.
|
||||
* </p>
|
||||
* <p>
|
||||
* Assuming only positive numbers, it's possible to discriminate if an extra byte
|
||||
* is added by checking if the first element of the array is 0 (0000_0000).
|
||||
* Due to the minimal representation provided by BigInteger, it means that the bit sign
|
||||
* is the least significant bit 0000_000<b>0</b> .
|
||||
* Otherwise the representation is not minimal.
|
||||
* For example, if the sign bit is 0000_00<b>0</b>0, then the representation is not minimal due to the rightmost zero.
|
||||
* </p>
|
||||
* This is the antagonist to {@link #bytesToBigInteger(byte[])}.
|
||||
* @param b the integer to format into a byte array
|
||||
* @param numBytes the desired size of the resulting byte array
|
||||
* @return numBytes byte long array.
|
||||
*/
|
||||
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
|
||||
checkArgument(b.signum() >= 0, "b must be positive or zero");
|
||||
checkArgument(numBytes > 0, "numBytes must be positive");
|
||||
byte[] src = b.toByteArray();
|
||||
byte[] dest = new byte[numBytes];
|
||||
boolean isFirstByteOnlyForSign = src[0] == 0;
|
||||
int length = isFirstByteOnlyForSign ? src.length - 1 : src.length;
|
||||
checkArgument(length <= numBytes, "The given number does not fit in " + numBytes);
|
||||
int srcPos = isFirstByteOnlyForSign ? 1 : 0;
|
||||
int destPos = numBytes - length;
|
||||
System.arraycopy(src, srcPos, dest, destPos, length);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of bytes into a positive BigInteger. This is the antagonist to
|
||||
* {@link #bigIntegerToBytes(BigInteger, int)}.
|
||||
*
|
||||
* @param bytes to convert into a BigInteger
|
||||
* @return the converted BigInteger
|
||||
*/
|
||||
public static BigInteger bytesToBigInteger(byte[] bytes) {
|
||||
return new BigInteger(1, bytes);
|
||||
}
|
||||
|
||||
/** Write 2 bytes to the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. */
|
||||
public static void uint16ToByteArrayLE(int val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & val);
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 8));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */
|
||||
public static void uint32ToByteArrayLE(long val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & val);
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 8));
|
||||
out[offset + 2] = (byte) (0xFF & (val >> 16));
|
||||
out[offset + 3] = (byte) (0xFF & (val >> 24));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in big endian format. */
|
||||
public static void uint32ToByteArrayBE(long val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & (val >> 24));
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 16));
|
||||
out[offset + 2] = (byte) (0xFF & (val >> 8));
|
||||
out[offset + 3] = (byte) (0xFF & val);
|
||||
}
|
||||
|
||||
/** Write 8 bytes to the byte array (starting at the offset) as signed 64-bit integer in little endian format. */
|
||||
public static void int64ToByteArrayLE(long val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & val);
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 8));
|
||||
out[offset + 2] = (byte) (0xFF & (val >> 16));
|
||||
out[offset + 3] = (byte) (0xFF & (val >> 24));
|
||||
out[offset + 4] = (byte) (0xFF & (val >> 32));
|
||||
out[offset + 5] = (byte) (0xFF & (val >> 40));
|
||||
out[offset + 6] = (byte) (0xFF & (val >> 48));
|
||||
out[offset + 7] = (byte) (0xFF & (val >> 56));
|
||||
}
|
||||
|
||||
/** Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format. */
|
||||
public static void uint16ToByteStreamLE(int val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & val));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
}
|
||||
|
||||
/** Write 2 bytes to the output stream as unsigned 16-bit integer in big endian format. */
|
||||
public static void uint16ToByteStreamBE(int val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & val));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the output stream as unsigned 32-bit integer in little endian format. */
|
||||
public static void uint32ToByteStreamLE(long val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & val));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & (val >> 16)));
|
||||
stream.write((int) (0xFF & (val >> 24)));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the output stream as unsigned 32-bit integer in big endian format. */
|
||||
public static void uint32ToByteStreamBE(long val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & (val >> 24)));
|
||||
stream.write((int) (0xFF & (val >> 16)));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & val));
|
||||
}
|
||||
|
||||
/** Write 8 bytes to the output stream as signed 64-bit integer in little endian format. */
|
||||
public static void int64ToByteStreamLE(long val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & val));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & (val >> 16)));
|
||||
stream.write((int) (0xFF & (val >> 24)));
|
||||
stream.write((int) (0xFF & (val >> 32)));
|
||||
stream.write((int) (0xFF & (val >> 40)));
|
||||
stream.write((int) (0xFF & (val >> 48)));
|
||||
stream.write((int) (0xFF & (val >> 56)));
|
||||
}
|
||||
|
||||
/** Write 8 bytes to the output stream as unsigned 64-bit integer in little endian format. */
|
||||
public static void uint64ToByteStreamLE(BigInteger val, OutputStream stream) throws IOException {
|
||||
byte[] bytes = val.toByteArray();
|
||||
if (bytes.length > 8) {
|
||||
throw new RuntimeException("Input too large to encode into a uint64");
|
||||
}
|
||||
bytes = reverseBytes(bytes);
|
||||
stream.write(bytes);
|
||||
if (bytes.length < 8) {
|
||||
for (int i = 0; i < 8 - bytes.length; i++)
|
||||
stream.write(0);
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. */
|
||||
public static int readUint16(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xff) |
|
||||
((bytes[offset + 1] & 0xff) << 8);
|
||||
}
|
||||
|
||||
/** Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */
|
||||
public static long readUint32(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xffl) |
|
||||
((bytes[offset + 1] & 0xffl) << 8) |
|
||||
((bytes[offset + 2] & 0xffl) << 16) |
|
||||
((bytes[offset + 3] & 0xffl) << 24);
|
||||
}
|
||||
|
||||
/** Parse 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format. */
|
||||
public static long readInt64(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xffl) |
|
||||
((bytes[offset + 1] & 0xffl) << 8) |
|
||||
((bytes[offset + 2] & 0xffl) << 16) |
|
||||
((bytes[offset + 3] & 0xffl) << 24) |
|
||||
((bytes[offset + 4] & 0xffl) << 32) |
|
||||
((bytes[offset + 5] & 0xffl) << 40) |
|
||||
((bytes[offset + 6] & 0xffl) << 48) |
|
||||
((bytes[offset + 7] & 0xffl) << 56);
|
||||
}
|
||||
|
||||
/** Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format. */
|
||||
public static long readUint32BE(byte[] bytes, int offset) {
|
||||
return ((bytes[offset] & 0xffl) << 24) |
|
||||
((bytes[offset + 1] & 0xffl) << 16) |
|
||||
((bytes[offset + 2] & 0xffl) << 8) |
|
||||
(bytes[offset + 3] & 0xffl);
|
||||
}
|
||||
|
||||
/** Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format. */
|
||||
public static int readUint16BE(byte[] bytes, int offset) {
|
||||
return ((bytes[offset] & 0xff) << 8) |
|
||||
(bytes[offset + 1] & 0xff);
|
||||
}
|
||||
|
||||
/** Parse 2 bytes from the stream as unsigned 16-bit integer in little endian format. */
|
||||
public static int readUint16FromStream(InputStream is) {
|
||||
try {
|
||||
return (is.read() & 0xff) |
|
||||
((is.read() & 0xff) << 8);
|
||||
} catch (IOException x) {
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format. */
|
||||
public static long readUint32FromStream(InputStream is) {
|
||||
try {
|
||||
return (is.read() & 0xffl) |
|
||||
((is.read() & 0xffl) << 8) |
|
||||
((is.read() & 0xffl) << 16) |
|
||||
((is.read() & 0xffl) << 24);
|
||||
} catch (IOException x) {
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the given byte array in reverse order.
|
||||
*/
|
||||
public static byte[] reverseBytes(byte[] bytes) {
|
||||
// We could use the XOR trick here but it's easier to understand if we don't. If we find this is really a
|
||||
// performance issue the matter can be revisited.
|
||||
byte[] buf = new byte[bytes.length];
|
||||
for (int i = 0; i < bytes.length; i++)
|
||||
buf[i] = bytes[bytes.length - 1 - i];
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of
|
||||
* a 4 byte big endian length field, followed by the stated number of bytes representing
|
||||
* the number in big endian format (with a sign bit).
|
||||
* @param hasLength can be set to false if the given array is missing the 4 byte length field
|
||||
*/
|
||||
public static BigInteger decodeMPI(byte[] mpi, boolean hasLength) {
|
||||
byte[] buf;
|
||||
if (hasLength) {
|
||||
int length = (int) readUint32BE(mpi, 0);
|
||||
buf = new byte[length];
|
||||
System.arraycopy(mpi, 4, buf, 0, length);
|
||||
} else
|
||||
buf = mpi;
|
||||
if (buf.length == 0)
|
||||
return BigInteger.ZERO;
|
||||
boolean isNegative = (buf[0] & 0x80) == 0x80;
|
||||
if (isNegative)
|
||||
buf[0] &= 0x7f;
|
||||
BigInteger result = new BigInteger(buf);
|
||||
return isNegative ? result.negate() : result;
|
||||
}
|
||||
|
||||
/**
|
||||
* MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of
|
||||
* a 4 byte big endian length field, followed by the stated number of bytes representing
|
||||
* the number in big endian format (with a sign bit).
|
||||
* @param includeLength indicates whether the 4 byte length field should be included
|
||||
*/
|
||||
public static byte[] encodeMPI(BigInteger value, boolean includeLength) {
|
||||
if (value.equals(BigInteger.ZERO)) {
|
||||
if (!includeLength)
|
||||
return new byte[] {};
|
||||
else
|
||||
return new byte[] {0x00, 0x00, 0x00, 0x00};
|
||||
}
|
||||
boolean isNegative = value.signum() < 0;
|
||||
if (isNegative)
|
||||
value = value.negate();
|
||||
byte[] array = value.toByteArray();
|
||||
int length = array.length;
|
||||
if ((array[0] & 0x80) == 0x80)
|
||||
length++;
|
||||
if (includeLength) {
|
||||
byte[] result = new byte[length + 4];
|
||||
System.arraycopy(array, 0, result, length - array.length + 3, array.length);
|
||||
uint32ToByteArrayBE(length, result, 0);
|
||||
if (isNegative)
|
||||
result[4] |= 0x80;
|
||||
return result;
|
||||
} else {
|
||||
byte[] result;
|
||||
if (length != array.length) {
|
||||
result = new byte[length];
|
||||
System.arraycopy(array, 0, result, 1, array.length);
|
||||
}else
|
||||
result = array;
|
||||
if (isNegative)
|
||||
result[0] |= 0x80;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>The "compact" format is a representation of a whole number N using an unsigned 32 bit number similar to a
|
||||
* floating point format. The most significant 8 bits are the unsigned exponent of base 256. This exponent can
|
||||
* be thought of as "number of bytes of N". The lower 23 bits are the mantissa. Bit number 24 (0x800000) represents
|
||||
* the sign of N. Therefore, N = (-1^sign) * mantissa * 256^(exponent-3).</p>
|
||||
*
|
||||
* <p>Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn(). MPI uses the most significant bit of the
|
||||
* first byte as sign. Thus 0x1234560000 is compact 0x05123456 and 0xc0de000000 is compact 0x0600c0de. Compact
|
||||
* 0x05c0de00 would be -0x40de000000.</p>
|
||||
*
|
||||
* <p>Bitcoin only uses this "compact" format for encoding difficulty targets, which are unsigned 256bit quantities.
|
||||
* Thus, all the complexities of the sign bit and using base 256 are probably an implementation accident.</p>
|
||||
*/
|
||||
public static BigInteger decodeCompactBits(long compact) {
|
||||
int size = ((int) (compact >> 24)) & 0xFF;
|
||||
byte[] bytes = new byte[4 + size];
|
||||
bytes[3] = (byte) size;
|
||||
if (size >= 1) bytes[4] = (byte) ((compact >> 16) & 0xFF);
|
||||
if (size >= 2) bytes[5] = (byte) ((compact >> 8) & 0xFF);
|
||||
if (size >= 3) bytes[6] = (byte) (compact & 0xFF);
|
||||
return decodeMPI(bytes, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #decodeCompactBits(long)
|
||||
*/
|
||||
public static long encodeCompactBits(BigInteger value) {
|
||||
long result;
|
||||
int size = value.toByteArray().length;
|
||||
if (size <= 3)
|
||||
result = value.longValue() << 8 * (3 - size);
|
||||
else
|
||||
result = value.shiftRight(8 * (size - 3)).longValue();
|
||||
// The 0x00800000 bit denotes the sign.
|
||||
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
|
||||
if ((result & 0x00800000L) != 0) {
|
||||
result >>= 8;
|
||||
size++;
|
||||
}
|
||||
result |= size << 24;
|
||||
result |= value.signum() == -1 ? 0x00800000 : 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Checks if the given bit is set in data, using little endian (not the same as Java native big endian) */
|
||||
public static boolean checkBitLE(byte[] data, int index) {
|
||||
return (data[index >>> 3] & bitMask[7 & index]) != 0;
|
||||
}
|
||||
|
||||
/** Sets the given bit in data to one, using little endian (not the same as Java native big endian) */
|
||||
public static void setBitLE(byte[] data, int index) {
|
||||
data[index >>> 3] |= bitMask[7 & index];
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.listeners.NewBestBlockListener;
|
||||
import org.bitcoinj.core.listeners.ReorganizeListener;
|
||||
import org.bitcoinj.core.listeners.TransactionReceivedInBlockListener;
|
||||
|
|
|
@ -17,6 +17,9 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
@ -156,7 +159,7 @@ public class Base58 {
|
|||
}
|
||||
|
||||
public static BigInteger decodeToBigInteger(String input) throws AddressFormatException {
|
||||
return Utils.bytesToBigInteger(decode(input));
|
||||
return ByteUtils.bytesToBigInteger(decode(input));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -29,9 +31,9 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.core.Utils.readUint32;
|
||||
import static org.bitcoinj.core.Utils.uint32ToByteArrayBE;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.readUint32;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.uint32ToByteArrayBE;
|
||||
|
||||
/**
|
||||
* <p>Methods to serialize and de-serialize messages to the Bitcoin network format as defined in
|
||||
|
@ -128,7 +130,7 @@ public class BitcoinSerializer extends MessageSerializer {
|
|||
header[4 + i] = (byte) (name.codePointAt(i) & 0xFF);
|
||||
}
|
||||
|
||||
Utils.uint32ToByteArrayLE(message.length, header, 4 + COMMAND_LEN);
|
||||
ByteUtils.uint32ToByteArrayLE(message.length, header, 4 + COMMAND_LEN);
|
||||
|
||||
byte[] hash = Sha256Hash.hashTwice(message);
|
||||
System.arraycopy(hash, 0, header, 4 + COMMAND_LEN + 4, 4);
|
||||
|
|
|
@ -20,6 +20,8 @@ package org.bitcoinj.core;
|
|||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.internal.InternalUtils;
|
||||
import org.bitcoinj.params.AbstractBitcoinNetParams;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
@ -43,7 +45,7 @@ import java.util.Locale;
|
|||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static org.bitcoinj.base.Coin.FIFTY_COINS;
|
||||
import static org.bitcoinj.core.Sha256Hash.hashTwice;
|
||||
import static org.bitcoinj.base.Sha256Hash.hashTwice;
|
||||
|
||||
/**
|
||||
* <p>A block is a group of transactions, and is one of the fundamental data structures of the Bitcoin system.
|
||||
|
@ -285,14 +287,14 @@ public class Block extends Message {
|
|||
// A script containing the difficulty bits and the following message:
|
||||
//
|
||||
// "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
|
||||
private static final byte[] genesisTxInputScriptBytes = Utils.HEX.decode
|
||||
private static final byte[] genesisTxInputScriptBytes = ByteUtils.HEX.decode
|
||||
("04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73");
|
||||
|
||||
private static final byte[] genesisTxScriptPubKeyBytes;
|
||||
static {
|
||||
ByteArrayOutputStream scriptPubKeyBytes = new ByteArrayOutputStream();
|
||||
try {
|
||||
Script.writeBytes(scriptPubKeyBytes, Utils.HEX.decode
|
||||
Script.writeBytes(scriptPubKeyBytes, ByteUtils.HEX.decode
|
||||
("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e); // Cannot happen.
|
||||
|
@ -316,12 +318,12 @@ public class Block extends Message {
|
|||
return;
|
||||
}
|
||||
// fall back to manual write
|
||||
Utils.uint32ToByteStreamLE(version, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(version, stream);
|
||||
stream.write(prevBlockHash.getReversedBytes());
|
||||
stream.write(getMerkleRoot().getReversedBytes());
|
||||
Utils.uint32ToByteStreamLE(time, stream);
|
||||
Utils.uint32ToByteStreamLE(difficultyTarget, stream);
|
||||
Utils.uint32ToByteStreamLE(nonce, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(time, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(difficultyTarget, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(nonce, stream);
|
||||
}
|
||||
|
||||
private void writeTransactions(OutputStream stream) throws IOException {
|
||||
|
@ -554,7 +556,7 @@ public class Block extends Message {
|
|||
* is thrown.
|
||||
*/
|
||||
public BigInteger getDifficultyTargetAsInteger() throws VerificationException {
|
||||
BigInteger target = Utils.decodeCompactBits(difficultyTarget);
|
||||
BigInteger target = ByteUtils.decodeCompactBits(difficultyTarget);
|
||||
if (target.signum() <= 0 || target.compareTo(params.maxTarget) > 0)
|
||||
throw new VerificationException("Difficulty target is bad: " + target.toString());
|
||||
return target;
|
||||
|
@ -696,9 +698,9 @@ public class Block extends Message {
|
|||
// The right hand node can be the same as the left hand, in the case where we don't have enough
|
||||
// transactions.
|
||||
int right = Math.min(left + 1, levelSize - 1);
|
||||
byte[] leftBytes = Utils.reverseBytes(tree.get(levelOffset + left));
|
||||
byte[] rightBytes = Utils.reverseBytes(tree.get(levelOffset + right));
|
||||
tree.add(Utils.reverseBytes(hashTwice(leftBytes, rightBytes)));
|
||||
byte[] leftBytes = ByteUtils.reverseBytes(tree.get(levelOffset + left));
|
||||
byte[] rightBytes = ByteUtils.reverseBytes(tree.get(levelOffset + right));
|
||||
tree.add(ByteUtils.reverseBytes(hashTwice(leftBytes, rightBytes)));
|
||||
}
|
||||
// Move to the next level.
|
||||
levelOffset += levelSize;
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.store.BlockStore;
|
||||
import org.bitcoinj.store.BlockStoreException;
|
||||
import org.bitcoinj.store.MemoryBlockStore;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.internal.InternalUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.ScriptChunk;
|
||||
import org.bitcoinj.script.ScriptPattern;
|
||||
|
@ -161,8 +163,8 @@ public class BloomFilter extends Message {
|
|||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
stream.write(new VarInt(data.length).encode());
|
||||
stream.write(data);
|
||||
Utils.uint32ToByteStreamLE(hashFuncs, stream);
|
||||
Utils.uint32ToByteStreamLE(nTweak, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(hashFuncs, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(nTweak, stream);
|
||||
stream.write(nFlags);
|
||||
}
|
||||
|
||||
|
@ -231,7 +233,7 @@ public class BloomFilter extends Message {
|
|||
*/
|
||||
public synchronized boolean contains(byte[] object) {
|
||||
for (int i = 0; i < hashFuncs; i++) {
|
||||
if (!Utils.checkBitLE(data, murmurHash3(data, nTweak, i, object)))
|
||||
if (!ByteUtils.checkBitLE(data, murmurHash3(data, nTweak, i, object)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@ -240,7 +242,7 @@ public class BloomFilter extends Message {
|
|||
/** Insert the given arbitrary data into the filter */
|
||||
public synchronized void insert(byte[] object) {
|
||||
for (int i = 0; i < hashFuncs; i++)
|
||||
Utils.setBitLE(data, murmurHash3(data, nTweak, i, object));
|
||||
ByteUtils.setBitLE(data, murmurHash3(data, nTweak, i, object));
|
||||
}
|
||||
|
||||
/** Inserts the given key and equivalent hashed form (for the address). */
|
||||
|
@ -322,7 +324,7 @@ public class BloomFilter extends Message {
|
|||
Transaction tx = txns.get(i);
|
||||
txHashes.add(tx.getTxId());
|
||||
if (applyAndUpdate(tx)) {
|
||||
Utils.setBitLE(bits, i);
|
||||
ByteUtils.setBitLE(bits, i);
|
||||
matched.add(tx);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import com.google.common.hash.HashCode;
|
|||
import com.google.common.hash.Hasher;
|
||||
import com.google.common.hash.Hashing;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.store.BlockStore;
|
||||
import org.bitcoinj.store.BlockStoreException;
|
||||
import org.bitcoinj.store.FullPrunedBlockStore;
|
||||
|
|
|
@ -24,6 +24,8 @@ import com.google.common.primitives.UnsignedBytes;
|
|||
import org.bitcoin.NativeSecp256k1;
|
||||
import org.bitcoin.NativeSecp256k1Util;
|
||||
import org.bitcoin.Secp256k1Context;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.crypto.EncryptableItem;
|
||||
import org.bitcoinj.crypto.EncryptedData;
|
||||
import org.bitcoinj.crypto.KeyCrypter;
|
||||
|
@ -232,7 +234,7 @@ public class ECKey implements EncryptableItem {
|
|||
* public key is compressed.
|
||||
*/
|
||||
public static ECKey fromPrivate(byte[] privKeyBytes) {
|
||||
return fromPrivate(Utils.bytesToBigInteger(privKeyBytes));
|
||||
return fromPrivate(ByteUtils.bytesToBigInteger(privKeyBytes));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -240,7 +242,7 @@ public class ECKey implements EncryptableItem {
|
|||
* @param compressed Determines whether the resulting ECKey will use a compressed encoding for the public key.
|
||||
*/
|
||||
public static ECKey fromPrivate(byte[] privKeyBytes, boolean compressed) {
|
||||
return fromPrivate(Utils.bytesToBigInteger(privKeyBytes), compressed);
|
||||
return fromPrivate(ByteUtils.bytesToBigInteger(privKeyBytes), compressed);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -261,7 +263,7 @@ public class ECKey implements EncryptableItem {
|
|||
public static ECKey fromPrivateAndPrecalculatedPublic(byte[] priv, byte[] pub) {
|
||||
checkNotNull(priv);
|
||||
checkNotNull(pub);
|
||||
return new ECKey(Utils.bytesToBigInteger(priv), new LazyECPoint(CURVE.getCurve(), pub));
|
||||
return new ECKey(ByteUtils.bytesToBigInteger(priv), new LazyECPoint(CURVE.getCurve(), pub));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -359,7 +361,7 @@ public class ECKey implements EncryptableItem {
|
|||
|
||||
/**
|
||||
* Returns public key bytes from the given private key. To convert a byte array into a BigInteger,
|
||||
* use {@link Utils#bytesToBigInteger(byte[])}
|
||||
* use {@link ByteUtils#bytesToBigInteger(byte[])}
|
||||
*/
|
||||
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
|
||||
ECPoint point = publicPointFromPrivate(privKey);
|
||||
|
@ -368,7 +370,7 @@ public class ECKey implements EncryptableItem {
|
|||
|
||||
/**
|
||||
* Returns public key point from the given private key. To convert a byte array into a BigInteger,
|
||||
* use {@link Utils#bytesToBigInteger(byte[])}
|
||||
* use {@link ByteUtils#bytesToBigInteger(byte[])}
|
||||
*/
|
||||
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
|
||||
/*
|
||||
|
@ -577,7 +579,7 @@ public class ECKey implements EncryptableItem {
|
|||
try {
|
||||
byte[] signature = NativeSecp256k1.sign(
|
||||
input.getBytes(),
|
||||
Utils.bigIntegerToBytes(privateKeyForSigning, 32)
|
||||
ByteUtils.bigIntegerToBytes(privateKeyForSigning, 32)
|
||||
);
|
||||
return ECDSASignature.decodeFromDER(signature);
|
||||
} catch (NativeSecp256k1Util.AssertFailException e) {
|
||||
|
@ -715,7 +717,7 @@ public class ECKey implements EncryptableItem {
|
|||
else if (encoded.length == 65 && encoded[0] == 0x04)
|
||||
return false;
|
||||
else
|
||||
throw new IllegalArgumentException(Utils.HEX.encode(encoded));
|
||||
throw new IllegalArgumentException(ByteUtils.HEX.encode(encoded));
|
||||
}
|
||||
|
||||
private static ECKey extractKeyFromASN1(byte[] asn1privkey) {
|
||||
|
@ -741,7 +743,7 @@ public class ECKey implements EncryptableItem {
|
|||
"Input is of wrong version");
|
||||
|
||||
byte[] privbits = ((ASN1OctetString) seq.getObjectAt(1)).getOctets();
|
||||
BigInteger privkey = Utils.bytesToBigInteger(privbits);
|
||||
BigInteger privkey = ByteUtils.bytesToBigInteger(privbits);
|
||||
|
||||
ASN1TaggedObject pubkey = (ASN1TaggedObject) seq.getObjectAt(3);
|
||||
checkArgument(pubkey.getTagNo() == 1, "Input has 'publicKey' with bad tag number");
|
||||
|
@ -788,8 +790,8 @@ public class ECKey implements EncryptableItem {
|
|||
int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
|
||||
byte[] sigData = new byte[65]; // 1 header + 32 bytes for R + 32 bytes for S
|
||||
sigData[0] = (byte)headerByte;
|
||||
System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
|
||||
System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
|
||||
System.arraycopy(ByteUtils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
|
||||
System.arraycopy(ByteUtils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
|
||||
return new String(Base64.encode(sigData), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
|
@ -820,8 +822,8 @@ public class ECKey implements EncryptableItem {
|
|||
// 0x1D = second key with even y, 0x1E = second key with odd y
|
||||
if (header < 27 || header > 34)
|
||||
throw new SignatureException("Header byte out of range: " + header);
|
||||
BigInteger r = Utils.bytesToBigInteger(Arrays.copyOfRange(signatureEncoded, 1, 33));
|
||||
BigInteger s = Utils.bytesToBigInteger(Arrays.copyOfRange(signatureEncoded, 33, 65));
|
||||
BigInteger r = ByteUtils.bytesToBigInteger(Arrays.copyOfRange(signatureEncoded, 1, 33));
|
||||
BigInteger s = ByteUtils.bytesToBigInteger(Arrays.copyOfRange(signatureEncoded, 33, 65));
|
||||
ECDSASignature sig = new ECDSASignature(r, s);
|
||||
byte[] messageBytes = formatMessageForSigning(message);
|
||||
// Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
|
||||
|
@ -952,7 +954,7 @@ public class ECKey implements EncryptableItem {
|
|||
* @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key bytes are missing/encrypted.
|
||||
*/
|
||||
public byte[] getPrivKeyBytes() {
|
||||
return Utils.bigIntegerToBytes(getPrivKey(), 32);
|
||||
return ByteUtils.bigIntegerToBytes(getPrivKey(), 32);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1165,11 +1167,11 @@ public class ECKey implements EncryptableItem {
|
|||
}
|
||||
|
||||
public String getPrivateKeyAsHex() {
|
||||
return Utils.HEX.encode(getPrivKeyBytes());
|
||||
return ByteUtils.HEX.encode(getPrivKeyBytes());
|
||||
}
|
||||
|
||||
public String getPublicKeyAsHex() {
|
||||
return Utils.HEX.encode(pub.getEncoded());
|
||||
return ByteUtils.HEX.encode(pub.getEncoded());
|
||||
}
|
||||
|
||||
public String getPrivateKeyAsWiF(NetworkParameters params) {
|
||||
|
@ -1214,7 +1216,7 @@ public class ECKey implements EncryptableItem {
|
|||
if (!isCompressed())
|
||||
builder.append(" UNCOMPRESSED");
|
||||
builder.append(" hash160:");
|
||||
builder.append(Utils.HEX.encode(getPubKeyHash()));
|
||||
builder.append(ByteUtils.HEX.encode(getPubKeyHash()));
|
||||
if (creationTimeSeconds > 0)
|
||||
builder.append(" creationTimeSeconds:").append(creationTimeSeconds).append(" [")
|
||||
.append(Utils.dateTimeFormat(creationTimeSeconds * 1000)).append("]");
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
@ -40,7 +41,7 @@ public class FeeFilterMessage extends Message {
|
|||
@Override
|
||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
super.bitcoinSerializeToStream(stream);
|
||||
Utils.uint64ToByteStreamLE(BigInteger.valueOf(feeRate.value), stream);
|
||||
ByteUtils.uint64ToByteStreamLE(BigInteger.valueOf(feeRate.value), stream);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Collections;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.params.AbstractBitcoinNetParams;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.Script.VerifyFlag;
|
||||
|
|
|
@ -17,6 +17,9 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
@ -74,7 +77,7 @@ public class GetBlocksMessage extends Message {
|
|||
@Override
|
||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
// Version, for some reason.
|
||||
Utils.uint32ToByteStreamLE(serializer.getProtocolVersion(), stream);
|
||||
ByteUtils.uint32ToByteStreamLE(serializer.getProtocolVersion(), stream);
|
||||
// Then a vector of block hashes. This is actually a "block locator", a set of block
|
||||
// identifiers that spans the entire chain with exponentially increasing gaps between
|
||||
// them, until we end up at the genesis block. See CBlockLocator::Set()
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
/**
|
||||
* <p>Represents the "getdata" P2P network message, which requests the contents of blocks or transactions given their
|
||||
* hashes.</p>
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
/**
|
||||
* <p>The "getheaders" command is structurally identical to "getblocks", but has different meaning. On receiving this
|
||||
* message a Bitcoin node returns matching blocks up to the limit, but without the bodies. It is useful as an
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class InventoryItem {
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
@ -99,7 +100,7 @@ public abstract class ListMessage extends Message {
|
|||
stream.write(new VarInt(items.size()).encode());
|
||||
for (InventoryItem i : items) {
|
||||
// Write out the type code.
|
||||
Utils.uint32ToByteStreamLE(i.type.code, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(i.type.code, stream);
|
||||
// And now the hash.
|
||||
stream.write(i.hash.getReversedBytes());
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -271,7 +273,7 @@ public abstract class Message {
|
|||
|
||||
protected long readUint32() throws ProtocolException {
|
||||
try {
|
||||
long u = Utils.readUint32(payload, cursor);
|
||||
long u = ByteUtils.readUint32(payload, cursor);
|
||||
cursor += 4;
|
||||
return u;
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
|
@ -281,7 +283,7 @@ public abstract class Message {
|
|||
|
||||
protected long readInt64() throws ProtocolException {
|
||||
try {
|
||||
long u = Utils.readInt64(payload, cursor);
|
||||
long u = ByteUtils.readInt64(payload, cursor);
|
||||
cursor += 8;
|
||||
return u;
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
|
@ -291,7 +293,7 @@ public abstract class Message {
|
|||
|
||||
protected BigInteger readUint64() throws ProtocolException {
|
||||
// Java does not have an unsigned 64 bit type. So scrape it off the wire then flip.
|
||||
return new BigInteger(Utils.reverseBytes(readBytes(8)));
|
||||
return new BigInteger(ByteUtils.reverseBytes(readBytes(8)));
|
||||
}
|
||||
|
||||
protected VarInt readVarInt() throws ProtocolException {
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.RegTestParams;
|
||||
import org.bitcoinj.params.SigNetParams;
|
||||
|
|
|
@ -18,6 +18,9 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
@ -25,9 +28,9 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.bitcoinj.core.Utils.checkBitLE;
|
||||
import static org.bitcoinj.core.Utils.reverseBytes;
|
||||
import static org.bitcoinj.core.Utils.uint32ToByteStreamLE;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.checkBitLE;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.reverseBytes;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.uint32ToByteStreamLE;
|
||||
|
||||
/**
|
||||
* <p>A data structure that contains proofs of block inclusion for one or more transactions, in an efficient manner.</p>
|
||||
|
@ -99,7 +102,7 @@ public class PartialMerkleTree extends Message {
|
|||
byte[] bits = new byte[(int)Math.ceil(bitList.size() / 8.0)];
|
||||
for (int i = 0; i < bitList.size(); i++)
|
||||
if (bitList.get(i))
|
||||
Utils.setBitLE(bits, i);
|
||||
ByteUtils.setBitLE(bits, i);
|
||||
return new PartialMerkleTree(params, bits, hashes, allLeafHashes.size());
|
||||
}
|
||||
|
||||
|
@ -136,7 +139,7 @@ public class PartialMerkleTree extends Message {
|
|||
boolean parentOfMatch = false;
|
||||
// Is this node a parent of at least one matched hash?
|
||||
for (int p = pos << height; p < (pos+1) << height && p < allLeafHashes.size(); p++) {
|
||||
if (Utils.checkBitLE(includeBits, p)) {
|
||||
if (ByteUtils.checkBitLE(includeBits, p)) {
|
||||
parentOfMatch = true;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import com.google.common.base.Strings;
|
|||
import com.google.common.base.Throwables;
|
||||
import net.jcip.annotations.GuardedBy;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.listeners.BlocksDownloadedEventListener;
|
||||
import org.bitcoinj.core.listeners.ChainDownloadStartedEventListener;
|
||||
import org.bitcoinj.core.listeners.GetDataEventListener;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bouncycastle.jcajce.provider.digest.SHA3;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -52,7 +53,7 @@ public class PeerAddress extends ChildMessage {
|
|||
private long time;
|
||||
|
||||
private static final BaseEncoding BASE32 = BaseEncoding.base32().lowerCase();
|
||||
private static final byte[] ONIONCAT_PREFIX = Utils.HEX.decode("fd87d87eeb43");
|
||||
private static final byte[] ONIONCAT_PREFIX = ByteUtils.HEX.decode("fd87d87eeb43");
|
||||
|
||||
/**
|
||||
* Construct a peer address from a serialized payload.
|
||||
|
@ -130,7 +131,7 @@ public class PeerAddress extends ChildMessage {
|
|||
throw new IllegalStateException("invalid protocolVersion: " + protocolVersion);
|
||||
|
||||
if (protocolVersion >= 1) {
|
||||
Utils.uint32ToByteStreamLE(time, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(time, stream);
|
||||
}
|
||||
if (protocolVersion == 2) {
|
||||
stream.write(new VarInt(services.longValue()).encode());
|
||||
|
@ -172,7 +173,7 @@ public class PeerAddress extends ChildMessage {
|
|||
throw new IllegalStateException();
|
||||
}
|
||||
} else {
|
||||
Utils.uint64ToByteStreamLE(services, stream); // nServices.
|
||||
ByteUtils.uint64ToByteStreamLE(services, stream); // nServices.
|
||||
if (addr != null) {
|
||||
// Java does not provide any utility to map an IPv4 address into IPv6 space, so we have to do it by
|
||||
// hand.
|
||||
|
@ -199,7 +200,7 @@ public class PeerAddress extends ChildMessage {
|
|||
}
|
||||
}
|
||||
// And write out the port. Unlike the rest of the protocol, address and port is in big endian byte order.
|
||||
Utils.uint16ToByteStreamBE(port, stream);
|
||||
ByteUtils.uint16ToByteStreamBE(port, stream);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -271,7 +272,7 @@ public class PeerAddress extends ChildMessage {
|
|||
hostname = null;
|
||||
}
|
||||
}
|
||||
port = Utils.readUint16BE(payload, cursor);
|
||||
port = ByteUtils.readUint16BE(payload, cursor);
|
||||
cursor += 2;
|
||||
length += 2;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
@ -51,7 +53,7 @@ public class Ping extends Message {
|
|||
@Override
|
||||
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
if (hasNonce)
|
||||
Utils.int64ToByteStreamLE(nonce, stream);
|
||||
ByteUtils.int64ToByteStreamLE(nonce, stream);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
|
@ -46,7 +48,7 @@ public class Pong extends Message {
|
|||
|
||||
@Override
|
||||
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
Utils.int64ToByteStreamLE(nonce, stream);
|
||||
ByteUtils.int64ToByteStreamLE(nonce, stream);
|
||||
}
|
||||
|
||||
/** Returns the nonce sent by the remote peer. */
|
||||
|
|
|
@ -18,6 +18,8 @@ package org.bitcoinj.core;
|
|||
|
||||
// TODO: Rename PrunedException to something like RequiredDataWasPrunedException
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
/**
|
||||
* PrunedException is thrown in cases where a fully verifying node has deleted (pruned) old block data that turned
|
||||
* out to be necessary for handling a re-org. Normally this should never happen unless you're playing with the testnet
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.store.BlockStore;
|
||||
import org.bitcoinj.store.BlockStoreException;
|
||||
|
||||
|
@ -142,7 +143,7 @@ public class StoredBlock {
|
|||
public static StoredBlock deserializeCompact(NetworkParameters params, ByteBuffer buffer) throws ProtocolException {
|
||||
byte[] chainWorkBytes = new byte[StoredBlock.CHAIN_WORK_BYTES];
|
||||
buffer.get(chainWorkBytes);
|
||||
BigInteger chainWork = Utils.bytesToBigInteger(chainWorkBytes);
|
||||
BigInteger chainWork = ByteUtils.bytesToBigInteger(chainWorkBytes);
|
||||
int height = buffer.getInt(); // +4 bytes
|
||||
byte[] header = new byte[Block.HEADER_SIZE + 1]; // Extra byte for the 00 transactions length.
|
||||
buffer.get(header, 0, Block.HEADER_SIZE);
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,6 +21,8 @@ import com.google.common.base.MoreObjects;
|
|||
import com.google.common.collect.Lists;
|
||||
import com.google.common.math.IntMath;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||
import org.bitcoinj.crypto.TransactionSignature;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
@ -59,8 +61,8 @@ import static com.google.common.base.Preconditions.checkArgument;
|
|||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static org.bitcoinj.core.NetworkParameters.ProtocolVersion.WITNESS_VERSION;
|
||||
import static org.bitcoinj.core.Utils.uint32ToByteStreamLE;
|
||||
import static org.bitcoinj.core.Utils.uint64ToByteStreamLE;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.uint32ToByteStreamLE;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.uint64ToByteStreamLE;
|
||||
|
||||
/**
|
||||
* <p>A transaction represents the movement of coins from some addresses to some other addresses. It can also represent
|
||||
|
@ -942,7 +944,7 @@ public class Transaction extends ChildMessage {
|
|||
* @return raw transaction in hex format
|
||||
*/
|
||||
public String toHexString() {
|
||||
return Utils.HEX.encode(unsafeBitcoinSerialize());
|
||||
return ByteUtils.HEX.encode(unsafeBitcoinSerialize());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.wallet.Wallet;
|
||||
import org.bitcoinj.wallet.WalletTransaction;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.collect.Iterators;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.utils.ListenableCompletableFuture;
|
||||
import org.bitcoinj.utils.ListenerRegistration;
|
||||
import org.bitcoinj.utils.Threading;
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.internal.InternalUtils;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.ScriptException;
|
||||
|
@ -173,7 +175,7 @@ public class TransactionInput extends ChildMessage {
|
|||
outpoint.bitcoinSerialize(stream);
|
||||
stream.write(new VarInt(scriptBytes.length).encode());
|
||||
stream.write(scriptBytes);
|
||||
Utils.uint32ToByteStreamLE(sequence, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(sequence, stream);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.ScriptError;
|
||||
import org.bitcoinj.script.ScriptException;
|
||||
|
@ -106,7 +108,7 @@ public class TransactionOutPoint extends ChildMessage {
|
|||
@Override
|
||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
stream.write(hash.getReversedBytes());
|
||||
Utils.uint32ToByteStreamLE(index, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(index, stream);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.ScriptBuilder;
|
||||
import org.bitcoinj.script.ScriptException;
|
||||
|
@ -153,7 +155,7 @@ public class TransactionOutput extends ChildMessage {
|
|||
@Override
|
||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
checkNotNull(scriptBytes);
|
||||
Utils.int64ToByteStreamLE(value, stream);
|
||||
ByteUtils.int64ToByteStreamLE(value, stream);
|
||||
// TODO: Move script serialization into the Script class, where it belongs.
|
||||
stream.write(new VarInt(scriptBytes.length).encode());
|
||||
stream.write(scriptBytes);
|
||||
|
@ -348,7 +350,7 @@ public class TransactionOutput extends ChildMessage {
|
|||
|| ScriptPattern.isP2SH(script))
|
||||
buf.append(" to ").append(script.getToAddress(params));
|
||||
else if (ScriptPattern.isP2PK(script))
|
||||
buf.append(" to pubkey ").append(Utils.HEX.encode(ScriptPattern.extractKeyFromP2PK(script)));
|
||||
buf.append(" to pubkey ").append(ByteUtils.HEX.encode(ScriptPattern.extractKeyFromP2PK(script)));
|
||||
else if (ScriptPattern.isSentToMultisig(script))
|
||||
buf.append(" to multisig");
|
||||
else
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
@ -38,12 +40,12 @@ public class TransactionOutputChanges {
|
|||
}
|
||||
|
||||
public TransactionOutputChanges(InputStream in) throws IOException {
|
||||
int numOutsCreated = (int) Utils.readUint32FromStream(in);
|
||||
int numOutsCreated = (int) ByteUtils.readUint32FromStream(in);
|
||||
txOutsCreated = new LinkedList<>();
|
||||
for (int i = 0; i < numOutsCreated; i++)
|
||||
txOutsCreated.add(UTXO.fromStream(in));
|
||||
|
||||
int numOutsSpent = (int) Utils.readUint32FromStream(in);
|
||||
int numOutsSpent = (int) ByteUtils.readUint32FromStream(in);
|
||||
txOutsSpent = new LinkedList<>();
|
||||
for (int i = 0; i < numOutsSpent; i++)
|
||||
txOutsSpent.add(UTXO.fromStream(in));
|
||||
|
@ -51,13 +53,13 @@ public class TransactionOutputChanges {
|
|||
|
||||
public void serializeToStream(OutputStream bos) throws IOException {
|
||||
int numOutsCreated = txOutsCreated.size();
|
||||
Utils.uint32ToByteStreamLE(numOutsCreated, bos);
|
||||
ByteUtils.uint32ToByteStreamLE(numOutsCreated, bos);
|
||||
for (UTXO output : txOutsCreated) {
|
||||
output.serializeToStream(bos);
|
||||
}
|
||||
|
||||
int numOutsSpent = txOutsSpent.size();
|
||||
Utils.uint32ToByteStreamLE(numOutsSpent, bos);
|
||||
ByteUtils.uint32ToByteStreamLE(numOutsSpent, bos);
|
||||
for (UTXO output : txOutsSpent) {
|
||||
output.serializeToStream(bos);
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.internal.InternalUtils;
|
||||
import org.bitcoinj.crypto.TransactionSignature;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
@ -94,7 +95,7 @@ public class TransactionWitness {
|
|||
} else if (push.length == 0) {
|
||||
stringPushes.add("EMPTY");
|
||||
} else {
|
||||
stringPushes.add(Utils.HEX.encode(push));
|
||||
stringPushes.add(ByteUtils.HEX.encode(push));
|
||||
}
|
||||
}
|
||||
return InternalUtils.SPACE_JOINER.join(stringPushes);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.utils.Threading;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
||||
import java.io.EOFException;
|
||||
|
@ -144,13 +146,13 @@ public class UTXO {
|
|||
}
|
||||
|
||||
public void serializeToStream(OutputStream bos) throws IOException {
|
||||
Utils.uint64ToByteStreamLE(BigInteger.valueOf(value.value), bos);
|
||||
ByteUtils.uint64ToByteStreamLE(BigInteger.valueOf(value.value), bos);
|
||||
byte[] scriptBytes = script.getProgram();
|
||||
Utils.uint32ToByteStreamLE(scriptBytes.length, bos);
|
||||
ByteUtils.uint32ToByteStreamLE(scriptBytes.length, bos);
|
||||
bos.write(scriptBytes);
|
||||
bos.write(hash.getBytes());
|
||||
Utils.uint32ToByteStreamLE(index, bos);
|
||||
Utils.uint32ToByteStreamLE(height, bos);
|
||||
ByteUtils.uint32ToByteStreamLE(index, bos);
|
||||
ByteUtils.uint32ToByteStreamLE(height, bos);
|
||||
bos.write(new byte[] { (byte)(coinbase ? 1 : 0) });
|
||||
}
|
||||
|
||||
|
@ -158,9 +160,9 @@ public class UTXO {
|
|||
byte[] valueBytes = new byte[8];
|
||||
if (in.read(valueBytes, 0, 8) != 8)
|
||||
throw new EOFException();
|
||||
Coin value = Coin.valueOf(Utils.readInt64(valueBytes, 0));
|
||||
Coin value = Coin.valueOf(ByteUtils.readInt64(valueBytes, 0));
|
||||
|
||||
int scriptBytesLength = (int) Utils.readUint32FromStream(in);
|
||||
int scriptBytesLength = (int) ByteUtils.readUint32FromStream(in);
|
||||
byte[] scriptBytes = new byte[scriptBytesLength];
|
||||
if (in.read(scriptBytes) != scriptBytesLength)
|
||||
throw new EOFException();
|
||||
|
@ -174,9 +176,9 @@ public class UTXO {
|
|||
byte[] indexBytes = new byte[4];
|
||||
if (in.read(indexBytes) != 4)
|
||||
throw new EOFException();
|
||||
long index = Utils.readUint32(indexBytes, 0);
|
||||
long index = ByteUtils.readUint32(indexBytes, 0);
|
||||
|
||||
int height = (int) Utils.readUint32FromStream(in);
|
||||
int height = (int) ByteUtils.readUint32FromStream(in);
|
||||
|
||||
byte[] coinbaseByte = new byte[1];
|
||||
in.read(coinbaseByte);
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
/**
|
||||
* <p>Instances of this class are not safe for use by multiple threads.</p>
|
||||
*/
|
||||
|
@ -31,6 +33,6 @@ public class UnknownMessage extends EmptyMessage {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Unknown message [" + name + "]" + (payload == null ? "" : ": " + Utils.HEX.encode(payload));
|
||||
return "Unknown message [" + name + "]" + (payload == null ? "" : ": " + ByteUtils.HEX.encode(payload));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,16 +19,13 @@ package org.bitcoinj.core;
|
|||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.internal.InternalUtils;
|
||||
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
|
@ -38,8 +35,6 @@ import java.util.Locale;
|
|||
import java.util.TimeZone;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
/**
|
||||
* A collection of various utility methods that are helpful for working with the Bitcoin protocol.
|
||||
* To enable debug logging from the library, run with -Dbitcoinj.logging=true on your command line.
|
||||
|
@ -60,9 +55,6 @@ public class Utils {
|
|||
@Deprecated
|
||||
public static final Splitter WHITESPACE_SPLITTER = Splitter.on(Pattern.compile("\\s+"));
|
||||
|
||||
/** Hex encoding used throughout the framework. Use with HEX.encode(byte[]) or HEX.decode(CharSequence). */
|
||||
public static final BaseEncoding HEX = BaseEncoding.base16().lowerCase();
|
||||
|
||||
/**
|
||||
* Max initial size of variable length arrays and ArrayLists that could be attacked.
|
||||
* Avoids this attack: Attacker sends a msg indicating it will contain a huge number (eg 2 billion) elements (eg transaction inputs) and
|
||||
|
@ -72,211 +64,6 @@ public class Utils {
|
|||
|
||||
private static final Logger log = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The regular {@link BigInteger#toByteArray()} includes the sign bit of the number and
|
||||
* might result in an extra byte addition. This method removes this extra byte.
|
||||
* </p>
|
||||
* <p>
|
||||
* Assuming only positive numbers, it's possible to discriminate if an extra byte
|
||||
* is added by checking if the first element of the array is 0 (0000_0000).
|
||||
* Due to the minimal representation provided by BigInteger, it means that the bit sign
|
||||
* is the least significant bit 0000_000<b>0</b> .
|
||||
* Otherwise the representation is not minimal.
|
||||
* For example, if the sign bit is 0000_00<b>0</b>0, then the representation is not minimal due to the rightmost zero.
|
||||
* </p>
|
||||
* This is the antagonist to {@link #bytesToBigInteger(byte[])}.
|
||||
* @param b the integer to format into a byte array
|
||||
* @param numBytes the desired size of the resulting byte array
|
||||
* @return numBytes byte long array.
|
||||
*/
|
||||
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
|
||||
checkArgument(b.signum() >= 0, "b must be positive or zero");
|
||||
checkArgument(numBytes > 0, "numBytes must be positive");
|
||||
byte[] src = b.toByteArray();
|
||||
byte[] dest = new byte[numBytes];
|
||||
boolean isFirstByteOnlyForSign = src[0] == 0;
|
||||
int length = isFirstByteOnlyForSign ? src.length - 1 : src.length;
|
||||
checkArgument(length <= numBytes, "The given number does not fit in " + numBytes);
|
||||
int srcPos = isFirstByteOnlyForSign ? 1 : 0;
|
||||
int destPos = numBytes - length;
|
||||
System.arraycopy(src, srcPos, dest, destPos, length);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of bytes into a positive BigInteger. This is the antagonist to
|
||||
* {@link #bigIntegerToBytes(BigInteger, int)}.
|
||||
*
|
||||
* @param bytes to convert into a BigInteger
|
||||
* @return the converted BigInteger
|
||||
*/
|
||||
public static BigInteger bytesToBigInteger(byte[] bytes) {
|
||||
return new BigInteger(1, bytes);
|
||||
}
|
||||
|
||||
/** Write 2 bytes to the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. */
|
||||
public static void uint16ToByteArrayLE(int val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & val);
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 8));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */
|
||||
public static void uint32ToByteArrayLE(long val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & val);
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 8));
|
||||
out[offset + 2] = (byte) (0xFF & (val >> 16));
|
||||
out[offset + 3] = (byte) (0xFF & (val >> 24));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in big endian format. */
|
||||
public static void uint32ToByteArrayBE(long val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & (val >> 24));
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 16));
|
||||
out[offset + 2] = (byte) (0xFF & (val >> 8));
|
||||
out[offset + 3] = (byte) (0xFF & val);
|
||||
}
|
||||
|
||||
/** Write 8 bytes to the byte array (starting at the offset) as signed 64-bit integer in little endian format. */
|
||||
public static void int64ToByteArrayLE(long val, byte[] out, int offset) {
|
||||
out[offset] = (byte) (0xFF & val);
|
||||
out[offset + 1] = (byte) (0xFF & (val >> 8));
|
||||
out[offset + 2] = (byte) (0xFF & (val >> 16));
|
||||
out[offset + 3] = (byte) (0xFF & (val >> 24));
|
||||
out[offset + 4] = (byte) (0xFF & (val >> 32));
|
||||
out[offset + 5] = (byte) (0xFF & (val >> 40));
|
||||
out[offset + 6] = (byte) (0xFF & (val >> 48));
|
||||
out[offset + 7] = (byte) (0xFF & (val >> 56));
|
||||
}
|
||||
|
||||
/** Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format. */
|
||||
public static void uint16ToByteStreamLE(int val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & val));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
}
|
||||
|
||||
/** Write 2 bytes to the output stream as unsigned 16-bit integer in big endian format. */
|
||||
public static void uint16ToByteStreamBE(int val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & val));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the output stream as unsigned 32-bit integer in little endian format. */
|
||||
public static void uint32ToByteStreamLE(long val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & val));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & (val >> 16)));
|
||||
stream.write((int) (0xFF & (val >> 24)));
|
||||
}
|
||||
|
||||
/** Write 4 bytes to the output stream as unsigned 32-bit integer in big endian format. */
|
||||
public static void uint32ToByteStreamBE(long val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & (val >> 24)));
|
||||
stream.write((int) (0xFF & (val >> 16)));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & val));
|
||||
}
|
||||
|
||||
/** Write 8 bytes to the output stream as signed 64-bit integer in little endian format. */
|
||||
public static void int64ToByteStreamLE(long val, OutputStream stream) throws IOException {
|
||||
stream.write((int) (0xFF & val));
|
||||
stream.write((int) (0xFF & (val >> 8)));
|
||||
stream.write((int) (0xFF & (val >> 16)));
|
||||
stream.write((int) (0xFF & (val >> 24)));
|
||||
stream.write((int) (0xFF & (val >> 32)));
|
||||
stream.write((int) (0xFF & (val >> 40)));
|
||||
stream.write((int) (0xFF & (val >> 48)));
|
||||
stream.write((int) (0xFF & (val >> 56)));
|
||||
}
|
||||
|
||||
/** Write 8 bytes to the output stream as unsigned 64-bit integer in little endian format. */
|
||||
public static void uint64ToByteStreamLE(BigInteger val, OutputStream stream) throws IOException {
|
||||
byte[] bytes = val.toByteArray();
|
||||
if (bytes.length > 8) {
|
||||
throw new RuntimeException("Input too large to encode into a uint64");
|
||||
}
|
||||
bytes = reverseBytes(bytes);
|
||||
stream.write(bytes);
|
||||
if (bytes.length < 8) {
|
||||
for (int i = 0; i < 8 - bytes.length; i++)
|
||||
stream.write(0);
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. */
|
||||
public static int readUint16(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xff) |
|
||||
((bytes[offset + 1] & 0xff) << 8);
|
||||
}
|
||||
|
||||
/** Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. */
|
||||
public static long readUint32(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xffl) |
|
||||
((bytes[offset + 1] & 0xffl) << 8) |
|
||||
((bytes[offset + 2] & 0xffl) << 16) |
|
||||
((bytes[offset + 3] & 0xffl) << 24);
|
||||
}
|
||||
|
||||
/** Parse 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format. */
|
||||
public static long readInt64(byte[] bytes, int offset) {
|
||||
return (bytes[offset] & 0xffl) |
|
||||
((bytes[offset + 1] & 0xffl) << 8) |
|
||||
((bytes[offset + 2] & 0xffl) << 16) |
|
||||
((bytes[offset + 3] & 0xffl) << 24) |
|
||||
((bytes[offset + 4] & 0xffl) << 32) |
|
||||
((bytes[offset + 5] & 0xffl) << 40) |
|
||||
((bytes[offset + 6] & 0xffl) << 48) |
|
||||
((bytes[offset + 7] & 0xffl) << 56);
|
||||
}
|
||||
|
||||
/** Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format. */
|
||||
public static long readUint32BE(byte[] bytes, int offset) {
|
||||
return ((bytes[offset] & 0xffl) << 24) |
|
||||
((bytes[offset + 1] & 0xffl) << 16) |
|
||||
((bytes[offset + 2] & 0xffl) << 8) |
|
||||
(bytes[offset + 3] & 0xffl);
|
||||
}
|
||||
|
||||
/** Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format. */
|
||||
public static int readUint16BE(byte[] bytes, int offset) {
|
||||
return ((bytes[offset] & 0xff) << 8) |
|
||||
(bytes[offset + 1] & 0xff);
|
||||
}
|
||||
|
||||
/** Parse 2 bytes from the stream as unsigned 16-bit integer in little endian format. */
|
||||
public static int readUint16FromStream(InputStream is) {
|
||||
try {
|
||||
return (is.read() & 0xff) |
|
||||
((is.read() & 0xff) << 8);
|
||||
} catch (IOException x) {
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format. */
|
||||
public static long readUint32FromStream(InputStream is) {
|
||||
try {
|
||||
return (is.read() & 0xffl) |
|
||||
((is.read() & 0xffl) << 8) |
|
||||
((is.read() & 0xffl) << 16) |
|
||||
((is.read() & 0xffl) << 24);
|
||||
} catch (IOException x) {
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the given byte array in reverse order.
|
||||
*/
|
||||
public static byte[] reverseBytes(byte[] bytes) {
|
||||
// We could use the XOR trick here but it's easier to understand if we don't. If we find this is really a
|
||||
// performance issue the matter can be revisited.
|
||||
byte[] buf = new byte[bytes.length];
|
||||
for (int i = 0; i < bytes.length; i++)
|
||||
buf[i] = bytes[bytes.length - 1 - i];
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
|
||||
*/
|
||||
|
@ -289,113 +76,6 @@ public class Utils {
|
|||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of
|
||||
* a 4 byte big endian length field, followed by the stated number of bytes representing
|
||||
* the number in big endian format (with a sign bit).
|
||||
* @param hasLength can be set to false if the given array is missing the 4 byte length field
|
||||
*/
|
||||
public static BigInteger decodeMPI(byte[] mpi, boolean hasLength) {
|
||||
byte[] buf;
|
||||
if (hasLength) {
|
||||
int length = (int) readUint32BE(mpi, 0);
|
||||
buf = new byte[length];
|
||||
System.arraycopy(mpi, 4, buf, 0, length);
|
||||
} else
|
||||
buf = mpi;
|
||||
if (buf.length == 0)
|
||||
return BigInteger.ZERO;
|
||||
boolean isNegative = (buf[0] & 0x80) == 0x80;
|
||||
if (isNegative)
|
||||
buf[0] &= 0x7f;
|
||||
BigInteger result = new BigInteger(buf);
|
||||
return isNegative ? result.negate() : result;
|
||||
}
|
||||
|
||||
/**
|
||||
* MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of
|
||||
* a 4 byte big endian length field, followed by the stated number of bytes representing
|
||||
* the number in big endian format (with a sign bit).
|
||||
* @param includeLength indicates whether the 4 byte length field should be included
|
||||
*/
|
||||
public static byte[] encodeMPI(BigInteger value, boolean includeLength) {
|
||||
if (value.equals(BigInteger.ZERO)) {
|
||||
if (!includeLength)
|
||||
return new byte[] {};
|
||||
else
|
||||
return new byte[] {0x00, 0x00, 0x00, 0x00};
|
||||
}
|
||||
boolean isNegative = value.signum() < 0;
|
||||
if (isNegative)
|
||||
value = value.negate();
|
||||
byte[] array = value.toByteArray();
|
||||
int length = array.length;
|
||||
if ((array[0] & 0x80) == 0x80)
|
||||
length++;
|
||||
if (includeLength) {
|
||||
byte[] result = new byte[length + 4];
|
||||
System.arraycopy(array, 0, result, length - array.length + 3, array.length);
|
||||
uint32ToByteArrayBE(length, result, 0);
|
||||
if (isNegative)
|
||||
result[4] |= 0x80;
|
||||
return result;
|
||||
} else {
|
||||
byte[] result;
|
||||
if (length != array.length) {
|
||||
result = new byte[length];
|
||||
System.arraycopy(array, 0, result, 1, array.length);
|
||||
}else
|
||||
result = array;
|
||||
if (isNegative)
|
||||
result[0] |= 0x80;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>The "compact" format is a representation of a whole number N using an unsigned 32 bit number similar to a
|
||||
* floating point format. The most significant 8 bits are the unsigned exponent of base 256. This exponent can
|
||||
* be thought of as "number of bytes of N". The lower 23 bits are the mantissa. Bit number 24 (0x800000) represents
|
||||
* the sign of N. Therefore, N = (-1^sign) * mantissa * 256^(exponent-3).</p>
|
||||
*
|
||||
* <p>Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn(). MPI uses the most significant bit of the
|
||||
* first byte as sign. Thus 0x1234560000 is compact 0x05123456 and 0xc0de000000 is compact 0x0600c0de. Compact
|
||||
* 0x05c0de00 would be -0x40de000000.</p>
|
||||
*
|
||||
* <p>Bitcoin only uses this "compact" format for encoding difficulty targets, which are unsigned 256bit quantities.
|
||||
* Thus, all the complexities of the sign bit and using base 256 are probably an implementation accident.</p>
|
||||
*/
|
||||
public static BigInteger decodeCompactBits(long compact) {
|
||||
int size = ((int) (compact >> 24)) & 0xFF;
|
||||
byte[] bytes = new byte[4 + size];
|
||||
bytes[3] = (byte) size;
|
||||
if (size >= 1) bytes[4] = (byte) ((compact >> 16) & 0xFF);
|
||||
if (size >= 2) bytes[5] = (byte) ((compact >> 8) & 0xFF);
|
||||
if (size >= 3) bytes[6] = (byte) (compact & 0xFF);
|
||||
return decodeMPI(bytes, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Utils#decodeCompactBits(long)
|
||||
*/
|
||||
public static long encodeCompactBits(BigInteger value) {
|
||||
long result;
|
||||
int size = value.toByteArray().length;
|
||||
if (size <= 3)
|
||||
result = value.longValue() << 8 * (3 - size);
|
||||
else
|
||||
result = value.shiftRight(8 * (size - 3)).longValue();
|
||||
// The 0x00800000 bit denotes the sign.
|
||||
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
|
||||
if ((result & 0x00800000L) != 0) {
|
||||
result >>= 8;
|
||||
size++;
|
||||
}
|
||||
result |= size << 24;
|
||||
result |= value.signum() == -1 ? 0x00800000 : 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* If non-null, overrides the return value of now().
|
||||
*/
|
||||
|
@ -482,19 +162,6 @@ public class Utils {
|
|||
return iso8601.format(dateTime);
|
||||
}
|
||||
|
||||
// 00000001, 00000010, 00000100, 00001000, ...
|
||||
private static final int[] bitMask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
|
||||
|
||||
/** Checks if the given bit is set in data, using little endian (not the same as Java native big endian) */
|
||||
public static boolean checkBitLE(byte[] data, int index) {
|
||||
return (data[index >>> 3] & bitMask[7 & index]) != 0;
|
||||
}
|
||||
|
||||
/** Sets the given bit in data to one, using little endian (not the same as Java native big endian) */
|
||||
public static void setBitLE(byte[] data, int index) {
|
||||
data[index >>> 3] |= bitMask[7 & index];
|
||||
}
|
||||
|
||||
private enum Runtime {
|
||||
ANDROID, OPENJDK, ORACLE_JAVA
|
||||
}
|
||||
|
@ -558,7 +225,7 @@ public class Utils {
|
|||
public static String toString(List<byte[]> stack) {
|
||||
List<String> parts = new ArrayList<>(stack.size());
|
||||
for (byte[] push : stack)
|
||||
parts.add('[' + HEX.encode(push) + ']');
|
||||
parts.add('[' + ByteUtils.HEX.encode(push) + ']');
|
||||
return InternalUtils.SPACE_JOINER.join(parts);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
/**
|
||||
* A variable-length encoded unsigned integer using Satoshi's encoding (a.k.a. "CompactSize").
|
||||
|
@ -48,13 +49,13 @@ public class VarInt {
|
|||
value = first;
|
||||
originallyEncodedSize = 1; // 1 data byte (8 bits)
|
||||
} else if (first == 253) {
|
||||
value = Utils.readUint16(buf, offset + 1);
|
||||
value = ByteUtils.readUint16(buf, offset + 1);
|
||||
originallyEncodedSize = 3; // 1 marker + 2 data bytes (16 bits)
|
||||
} else if (first == 254) {
|
||||
value = Utils.readUint32(buf, offset + 1);
|
||||
value = ByteUtils.readUint32(buf, offset + 1);
|
||||
originallyEncodedSize = 5; // 1 marker + 4 data bytes (32 bits)
|
||||
} else {
|
||||
value = Utils.readInt64(buf, offset + 1);
|
||||
value = ByteUtils.readInt64(buf, offset + 1);
|
||||
originallyEncodedSize = 9; // 1 marker + 8 data bytes (64 bits)
|
||||
}
|
||||
}
|
||||
|
@ -109,17 +110,17 @@ public class VarInt {
|
|||
case 3:
|
||||
bytes = new byte[3];
|
||||
bytes[0] = (byte) 253;
|
||||
Utils.uint16ToByteArrayLE((int) value, bytes, 1);
|
||||
ByteUtils.uint16ToByteArrayLE((int) value, bytes, 1);
|
||||
return bytes;
|
||||
case 5:
|
||||
bytes = new byte[5];
|
||||
bytes[0] = (byte) 254;
|
||||
Utils.uint32ToByteArrayLE(value, bytes, 1);
|
||||
ByteUtils.uint32ToByteArrayLE(value, bytes, 1);
|
||||
return bytes;
|
||||
default:
|
||||
bytes = new byte[9];
|
||||
bytes[0] = (byte) 255;
|
||||
Utils.int64ToByteArrayLE(value, bytes, 1);
|
||||
ByteUtils.int64ToByteArrayLE(value, bytes, 1);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.net.InetAddresses;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.internal.InternalUtils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -156,24 +157,24 @@ public class VersionMessage extends Message {
|
|||
|
||||
@Override
|
||||
public void bitcoinSerializeToStream(OutputStream buf) throws IOException {
|
||||
Utils.uint32ToByteStreamLE(clientVersion, buf);
|
||||
Utils.uint32ToByteStreamLE(localServices, buf);
|
||||
Utils.uint32ToByteStreamLE(localServices >> 32, buf);
|
||||
Utils.uint32ToByteStreamLE(time, buf);
|
||||
Utils.uint32ToByteStreamLE(time >> 32, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(clientVersion, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(localServices, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(localServices >> 32, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(time, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(time >> 32, buf);
|
||||
receivingAddr.bitcoinSerializeToStream(buf);
|
||||
fromAddr.bitcoinSerializeToStream(buf);
|
||||
// Next up is the "local host nonce", this is to detect the case of connecting
|
||||
// back to yourself. We don't care about this as we won't be accepting inbound
|
||||
// connections.
|
||||
Utils.uint32ToByteStreamLE(0, buf);
|
||||
Utils.uint32ToByteStreamLE(0, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(0, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(0, buf);
|
||||
// Now comes subVer.
|
||||
byte[] subVerBytes = subVer.getBytes(StandardCharsets.UTF_8);
|
||||
buf.write(new VarInt(subVerBytes.length).encode());
|
||||
buf.write(subVerBytes);
|
||||
// Size of known block chain.
|
||||
Utils.uint32ToByteStreamLE(bestHeight, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(bestHeight, buf);
|
||||
if (clientVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLOOM_FILTER)) {
|
||||
buf.write(relayTxesBeforeFilter ? 1 : 0);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
package org.bitcoinj.core.listeners;
|
||||
|
||||
import org.bitcoinj.core.AbstractBlockChain;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.VerificationException;
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.bitcoinj.core.listeners;
|
|||
|
||||
import org.bitcoinj.core.BlockChain;
|
||||
import org.bitcoinj.core.FilteredBlock;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.VerificationException;
|
||||
|
|
|
@ -17,14 +17,14 @@
|
|||
package org.bitcoinj.crypto;
|
||||
|
||||
import com.google.common.primitives.Bytes;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.AddressFormatException;
|
||||
import org.bitcoinj.core.Base58;
|
||||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.PrefixedChecksummedBytes;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bouncycastle.crypto.generators.SCrypt;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
|
@ -160,7 +160,7 @@ public class BIP38PrivateKey extends PrefixedChecksummedBytes {
|
|||
checkState(hashBytes.length == 40);
|
||||
passFactorBytes = Sha256Hash.hashTwice(hashBytes);
|
||||
}
|
||||
BigInteger passFactor = Utils.bytesToBigInteger(passFactorBytes);
|
||||
BigInteger passFactor = ByteUtils.bytesToBigInteger(passFactorBytes);
|
||||
ECKey k = ECKey.fromPrivate(passFactor, true);
|
||||
|
||||
byte[] salt = Bytes.concat(addressHash, ownerEntropy);
|
||||
|
@ -186,7 +186,7 @@ public class BIP38PrivateKey extends PrefixedChecksummedBytes {
|
|||
|
||||
byte[] seed = Bytes.concat(decrypted1, Arrays.copyOfRange(decrypted2, 8, 16));
|
||||
checkState(seed.length == 24);
|
||||
BigInteger seedFactor = Utils.bytesToBigInteger(Sha256Hash.hashTwice(seed));
|
||||
BigInteger seedFactor = ByteUtils.bytesToBigInteger(Sha256Hash.hashTwice(seed));
|
||||
checkState(passFactor.signum() >= 0);
|
||||
checkState(seedFactor.signum() >= 0);
|
||||
BigInteger priv = passFactor.multiply(seedFactor).mod(ECKey.CURVE.getN());
|
||||
|
|
|
@ -19,11 +19,12 @@ package org.bitcoinj.crypto;
|
|||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Address;
|
||||
import org.bitcoinj.core.Base58;
|
||||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bouncycastle.crypto.params.KeyParameter;
|
||||
|
@ -40,7 +41,7 @@ import java.util.Objects;
|
|||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
|
||||
/**
|
||||
* A deterministic key is a node in a {@link DeterministicHierarchy}. As per
|
||||
|
@ -401,7 +402,7 @@ public class DeterministicKey extends ECKey {
|
|||
if (decryptedKey.length != 32)
|
||||
throw new KeyCrypterException.InvalidCipherText(
|
||||
"Decrypted key must be 32 bytes long, but is " + decryptedKey.length);
|
||||
return Utils.bytesToBigInteger(decryptedKey);
|
||||
return ByteUtils.bytesToBigInteger(decryptedKey);
|
||||
}
|
||||
// Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for
|
||||
// the first key that has some encrypted private key data.
|
||||
|
@ -438,7 +439,7 @@ public class DeterministicKey extends ECKey {
|
|||
|
||||
private BigInteger derivePrivateKeyDownwards(DeterministicKey cursor, byte[] parentalPrivateKeyBytes) {
|
||||
DeterministicKey downCursor = new DeterministicKey(cursor.childNumberPath, cursor.chainCode,
|
||||
cursor.pub, Utils.bytesToBigInteger(parentalPrivateKeyBytes), cursor.parent);
|
||||
cursor.pub, ByteUtils.bytesToBigInteger(parentalPrivateKeyBytes), cursor.parent);
|
||||
// Now we have to rederive the keys along the path back to ourselves. That path can be found by just truncating
|
||||
// our path with the length of the parents path.
|
||||
List<ChildNumber> path = childNumberPath.subList(cursor.getPath().size(), childNumberPath.size());
|
||||
|
@ -611,7 +612,7 @@ public class DeterministicKey extends ECKey {
|
|||
if (pub) {
|
||||
return new DeterministicKey(path, chainCode, new LazyECPoint(ECKey.CURVE.getCurve(), data), parent, depth, parentFingerprint);
|
||||
} else {
|
||||
return new DeterministicKey(path, chainCode, Utils.bytesToBigInteger(data), parent, depth, parentFingerprint);
|
||||
return new DeterministicKey(path, chainCode, ByteUtils.bytesToBigInteger(data), parent, depth, parentFingerprint);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -661,7 +662,7 @@ public class DeterministicKey extends ECKey {
|
|||
@Override
|
||||
public String toString() {
|
||||
final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this).omitNullValues();
|
||||
helper.add("pub", Utils.HEX.encode(pub.getEncoded()));
|
||||
helper.add("pub", ByteUtils.HEX.encode(pub.getEncoded()));
|
||||
helper.add("chainCode", HEX.encode(chainCode));
|
||||
helper.add("path", getPathAsString());
|
||||
if (parent != null)
|
||||
|
@ -677,7 +678,7 @@ public class DeterministicKey extends ECKey {
|
|||
public void formatKeyWithAddress(boolean includePrivateKeys, @Nullable KeyParameter aesKey, StringBuilder builder,
|
||||
NetworkParameters params, Script.ScriptType outputScriptType, @Nullable String comment) {
|
||||
builder.append(" addr:").append(Address.fromKey(params, this, outputScriptType).toString());
|
||||
builder.append(" hash160:").append(Utils.HEX.encode(getPubKeyHash()));
|
||||
builder.append(" hash160:").append(ByteUtils.HEX.encode(getPubKeyHash()));
|
||||
builder.append(" (").append(getPathAsString());
|
||||
if (comment != null)
|
||||
builder.append(", ").append(comment);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.bitcoinj.crypto;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bouncycastle.math.ec.ECPoint;
|
||||
|
@ -85,7 +86,7 @@ public final class HDKeyDerivation {
|
|||
* @throws HDDerivationException if privKeyBytes is invalid (not between 0 and n inclusive).
|
||||
*/
|
||||
public static DeterministicKey createMasterPrivKeyFromBytes(byte[] privKeyBytes, byte[] chainCode) throws HDDerivationException {
|
||||
BigInteger priv = Utils.bytesToBigInteger(privKeyBytes);
|
||||
BigInteger priv = ByteUtils.bytesToBigInteger(privKeyBytes);
|
||||
assertNonZero(priv, "Generated master key is invalid.");
|
||||
assertLessThanN(priv, "Generated master key is invalid.");
|
||||
return new DeterministicKey(HDPath.m(), chainCode, priv, null);
|
||||
|
@ -153,7 +154,7 @@ public final class HDKeyDerivation {
|
|||
throws HDDerivationException {
|
||||
RawKeyBytes rawKey = deriveChildKeyBytesFromPrivate(parent, childNumber);
|
||||
return new DeterministicKey(parent.getPath().extend(childNumber), rawKey.chainCode,
|
||||
Utils.bytesToBigInteger(rawKey.keyBytes), parent);
|
||||
ByteUtils.bytesToBigInteger(rawKey.keyBytes), parent);
|
||||
}
|
||||
|
||||
public static RawKeyBytes deriveChildKeyBytesFromPrivate(DeterministicKey parent,
|
||||
|
@ -172,7 +173,7 @@ public final class HDKeyDerivation {
|
|||
checkState(i.length == 64, i.length);
|
||||
byte[] il = Arrays.copyOfRange(i, 0, 32);
|
||||
byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
|
||||
BigInteger ilInt = Utils.bytesToBigInteger(il);
|
||||
BigInteger ilInt = ByteUtils.bytesToBigInteger(il);
|
||||
assertLessThanN(ilInt, "Illegal derived key: I_L >= n");
|
||||
final BigInteger priv = parent.getPrivKey();
|
||||
BigInteger ki = priv.add(ilInt).mod(ECKey.CURVE.getN());
|
||||
|
@ -203,7 +204,7 @@ public final class HDKeyDerivation {
|
|||
checkState(i.length == 64, i.length);
|
||||
byte[] il = Arrays.copyOfRange(i, 0, 32);
|
||||
byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
|
||||
BigInteger ilInt = Utils.bytesToBigInteger(il);
|
||||
BigInteger ilInt = ByteUtils.bytesToBigInteger(il);
|
||||
assertLessThanN(ilInt, "Illegal derived key: I_L >= n");
|
||||
|
||||
final BigInteger N = ECKey.CURVE.getN();
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
package org.bitcoinj.crypto;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.core.internal.InternalUtils;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -37,7 +37,7 @@ import java.util.List;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
|
||||
/**
|
||||
* A MnemonicCode object may be used to convert between binary seed values and
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
package org.bitcoinj.jni;
|
||||
|
||||
import org.bitcoinj.core.BlockChain;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.VerificationException;
|
||||
|
|
|
@ -16,8 +16,8 @@
|
|||
|
||||
package org.bitcoinj.net.discovery;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Utils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.net.InetAddress;
|
||||
|
@ -107,7 +107,7 @@ public class SeedPeers implements PeerDiscovery {
|
|||
|
||||
private InetAddress convertAddress(int seed) throws UnknownHostException {
|
||||
byte[] v4addr = new byte[4];
|
||||
Utils.uint32ToByteArrayLE(seed, v4addr, 0);
|
||||
ByteUtils.uint32ToByteArrayLE(seed, v4addr, 0);
|
||||
return InetAddress.getByAddress(v4addr);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,15 +18,15 @@
|
|||
package org.bitcoinj.params;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.BitcoinSerializer;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.TransactionOutput;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.core.VerificationException;
|
||||
import org.bitcoinj.store.BlockStore;
|
||||
import org.bitcoinj.store.BlockStoreException;
|
||||
|
@ -146,7 +146,7 @@ public abstract class AbstractBitcoinNetParams extends NetworkParameters {
|
|||
if (timespan > targetTimespan * 4)
|
||||
timespan = targetTimespan * 4;
|
||||
|
||||
BigInteger newTarget = Utils.decodeCompactBits(prev.getDifficultyTarget());
|
||||
BigInteger newTarget = ByteUtils.decodeCompactBits(prev.getDifficultyTarget());
|
||||
newTarget = newTarget.multiply(BigInteger.valueOf(timespan));
|
||||
newTarget = newTarget.divide(BigInteger.valueOf(targetTimespan));
|
||||
|
||||
|
@ -161,7 +161,7 @@ public abstract class AbstractBitcoinNetParams extends NetworkParameters {
|
|||
// The calculated difficulty is to a higher precision than received, so reduce here.
|
||||
BigInteger mask = BigInteger.valueOf(0xFFFFFFL).shiftLeft(accuracyBytes * 8);
|
||||
newTarget = newTarget.and(mask);
|
||||
long newTargetCompact = Utils.encodeCompactBits(newTarget);
|
||||
long newTargetCompact = ByteUtils.encodeCompactBits(newTarget);
|
||||
|
||||
if (newTargetCompact != receivedTargetCompact)
|
||||
throw new VerificationException("Network provided difficulty bits do not match what was calculated: " +
|
||||
|
|
|
@ -17,9 +17,9 @@
|
|||
|
||||
package org.bitcoinj.params;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
|
@ -39,7 +39,7 @@ public class MainNetParams extends AbstractBitcoinNetParams {
|
|||
id = ID_MAINNET;
|
||||
|
||||
targetTimespan = TARGET_TIMESPAN;
|
||||
maxTarget = Utils.decodeCompactBits(Block.STANDARD_MAX_DIFFICULTY_TARGET);
|
||||
maxTarget = ByteUtils.decodeCompactBits(Block.STANDARD_MAX_DIFFICULTY_TARGET);
|
||||
|
||||
port = 8333;
|
||||
packetMagic = 0xf9beb4d9L;
|
||||
|
|
|
@ -17,9 +17,9 @@
|
|||
|
||||
package org.bitcoinj.params;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
|
@ -36,7 +36,7 @@ public class RegTestParams extends AbstractBitcoinNetParams {
|
|||
id = ID_REGTEST;
|
||||
|
||||
targetTimespan = TARGET_TIMESPAN;
|
||||
maxTarget = Utils.decodeCompactBits(Block.EASIEST_DIFFICULTY_TARGET);
|
||||
maxTarget = ByteUtils.decodeCompactBits(Block.EASIEST_DIFFICULTY_TARGET);
|
||||
// Difficulty adjustments are disabled for regtest.
|
||||
// By setting the block interval for difficulty adjustments to Integer.MAX_VALUE we make sure difficulty never
|
||||
// changes.
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
|
||||
package org.bitcoinj.params;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class SigNetParams extends AbstractBitcoinNetParams {
|
|||
id = ID_SIGNET;
|
||||
|
||||
targetTimespan = TARGET_TIMESPAN;
|
||||
maxTarget = Utils.decodeCompactBits(Block.EASIEST_DIFFICULTY_TARGET);
|
||||
maxTarget = ByteUtils.decodeCompactBits(Block.EASIEST_DIFFICULTY_TARGET);
|
||||
|
||||
port = 38333;
|
||||
packetMagic = 0x0a03cf40;
|
||||
|
|
|
@ -17,11 +17,11 @@
|
|||
|
||||
package org.bitcoinj.params;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.core.VerificationException;
|
||||
import org.bitcoinj.store.BlockStore;
|
||||
import org.bitcoinj.store.BlockStoreException;
|
||||
|
@ -48,7 +48,7 @@ public class TestNet3Params extends AbstractBitcoinNetParams {
|
|||
id = ID_TESTNET;
|
||||
|
||||
targetTimespan = TARGET_TIMESPAN;
|
||||
maxTarget = Utils.decodeCompactBits(Block.STANDARD_MAX_DIFFICULTY_TARGET);
|
||||
maxTarget = ByteUtils.decodeCompactBits(Block.STANDARD_MAX_DIFFICULTY_TARGET);
|
||||
|
||||
port = 18333;
|
||||
packetMagic = 0x0b110907;
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.bitcoinj.params;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.Utils;
|
||||
|
||||
|
@ -34,7 +35,7 @@ public class UnitTestParams extends AbstractBitcoinNetParams {
|
|||
id = ID_UNITTESTNET;
|
||||
|
||||
targetTimespan = 200000000; // 6 years. Just a very big number.
|
||||
maxTarget = Utils.decodeCompactBits(Block.EASIEST_DIFFICULTY_TARGET);
|
||||
maxTarget = ByteUtils.decodeCompactBits(Block.EASIEST_DIFFICULTY_TARGET);
|
||||
interval = 10;
|
||||
subsidyDecreaseBlockCount = 100;
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.bitcoinj.script;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Address;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.core.ECKey;
|
||||
|
@ -26,7 +27,7 @@ import org.bitcoinj.core.LegacyAddress;
|
|||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.ProtocolException;
|
||||
import org.bitcoinj.core.SegwitAddress;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.SignatureDecodeException;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.TransactionInput;
|
||||
|
@ -338,12 +339,12 @@ public class Script {
|
|||
} else if (opcode == OP_PUSHDATA2) {
|
||||
// Read a short, then read that many bytes of data.
|
||||
if (bis.available() < 2) throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Unexpected end of script");
|
||||
dataToRead = Utils.readUint16FromStream(bis);
|
||||
dataToRead = ByteUtils.readUint16FromStream(bis);
|
||||
} else if (opcode == OP_PUSHDATA4) {
|
||||
// Read a uint32, then read that many bytes of data.
|
||||
// Though this is allowed, because its value cannot be > 520, it should never actually be used
|
||||
if (bis.available() < 4) throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Unexpected end of script");
|
||||
dataToRead = Utils.readUint32FromStream(bis);
|
||||
dataToRead = ByteUtils.readUint32FromStream(bis);
|
||||
}
|
||||
|
||||
ScriptChunk chunk;
|
||||
|
@ -437,7 +438,7 @@ public class Script {
|
|||
os.write(buf);
|
||||
} else if (buf.length < 65536) {
|
||||
os.write(OP_PUSHDATA2);
|
||||
Utils.uint16ToByteStreamLE(buf.length, os);
|
||||
ByteUtils.uint16ToByteStreamLE(buf.length, os);
|
||||
os.write(buf);
|
||||
} else {
|
||||
throw new RuntimeException("Unimplemented");
|
||||
|
@ -610,7 +611,7 @@ public class Script {
|
|||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException("Could not find matching key for signature on " + hash.toString() + " sig " + Utils.HEX.encode(signatureBytes));
|
||||
throw new IllegalStateException("Could not find matching key for signature on " + hash.toString() + " sig " + ByteUtils.HEX.encode(signatureBytes));
|
||||
}
|
||||
|
||||
|
||||
|
@ -787,9 +788,9 @@ public class Script {
|
|||
} else if (opcode == OP_PUSHDATA1) {
|
||||
additionalBytes = (0xFF & inputScript[cursor]) + 1;
|
||||
} else if (opcode == OP_PUSHDATA2) {
|
||||
additionalBytes = Utils.readUint16(inputScript, cursor) + 2;
|
||||
additionalBytes = ByteUtils.readUint16(inputScript, cursor) + 2;
|
||||
} else if (opcode == OP_PUSHDATA4) {
|
||||
additionalBytes = (int) Utils.readUint32(inputScript, cursor) + 4;
|
||||
additionalBytes = (int) ByteUtils.readUint32(inputScript, cursor) + 4;
|
||||
}
|
||||
if (!skip) {
|
||||
try {
|
||||
|
@ -866,7 +867,7 @@ public class Script {
|
|||
}
|
||||
}
|
||||
|
||||
return Utils.decodeMPI(Utils.reverseBytes(chunk), false);
|
||||
return ByteUtils.decodeMPI(ByteUtils.reverseBytes(chunk), false);
|
||||
}
|
||||
|
||||
/** @deprecated use {@link ScriptPattern#isOpReturn(Script)} */
|
||||
|
@ -978,7 +979,7 @@ public class Script {
|
|||
|
||||
// OP_0 is no opcode
|
||||
case OP_1NEGATE:
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(BigInteger.ONE.negate(), false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(BigInteger.ONE.negate(), false)));
|
||||
break;
|
||||
case OP_1:
|
||||
case OP_2:
|
||||
|
@ -996,7 +997,7 @@ public class Script {
|
|||
case OP_14:
|
||||
case OP_15:
|
||||
case OP_16:
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(BigInteger.valueOf(decodeFromOpN(opcode)), false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(BigInteger.valueOf(decodeFromOpN(opcode)), false)));
|
||||
break;
|
||||
case OP_NOP:
|
||||
break;
|
||||
|
@ -1087,7 +1088,7 @@ public class Script {
|
|||
stack.add(stack.getLast());
|
||||
break;
|
||||
case OP_DEPTH:
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(BigInteger.valueOf(stack.size()), false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(BigInteger.valueOf(stack.size()), false)));
|
||||
break;
|
||||
case OP_DROP:
|
||||
if (stack.size() < 1)
|
||||
|
@ -1152,7 +1153,7 @@ public class Script {
|
|||
case OP_SIZE:
|
||||
if (stack.size() < 1)
|
||||
throw new ScriptException(ScriptError.SCRIPT_ERR_INVALID_STACK_OPERATION, "Attempted OP_SIZE on an empty stack");
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(BigInteger.valueOf(stack.getLast().length), false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(BigInteger.valueOf(stack.getLast().length), false)));
|
||||
break;
|
||||
case OP_EQUAL:
|
||||
if (stack.size() < 2)
|
||||
|
@ -1205,7 +1206,7 @@ public class Script {
|
|||
throw new AssertionError("Unreachable");
|
||||
}
|
||||
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(numericOPnum, false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(numericOPnum, false)));
|
||||
break;
|
||||
case OP_ADD:
|
||||
case OP_SUB:
|
||||
|
@ -1296,7 +1297,7 @@ public class Script {
|
|||
throw new RuntimeException("Opcode switched at runtime?");
|
||||
}
|
||||
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(numericOPresult, false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(numericOPresult, false)));
|
||||
break;
|
||||
case OP_NUMEQUALVERIFY:
|
||||
if (stack.size() < 2)
|
||||
|
@ -1314,9 +1315,9 @@ public class Script {
|
|||
BigInteger OPWITHINnum2 = castToBigInteger(stack.pollLast(), verifyFlags.contains(VerifyFlag.MINIMALDATA));
|
||||
BigInteger OPWITHINnum1 = castToBigInteger(stack.pollLast(), verifyFlags.contains(VerifyFlag.MINIMALDATA));
|
||||
if (OPWITHINnum2.compareTo(OPWITHINnum1) <= 0 && OPWITHINnum1.compareTo(OPWITHINnum3) < 0)
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(BigInteger.ONE, false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(BigInteger.ONE, false)));
|
||||
else
|
||||
stack.add(Utils.reverseBytes(Utils.encodeMPI(BigInteger.ZERO, false)));
|
||||
stack.add(ByteUtils.reverseBytes(ByteUtils.encodeMPI(BigInteger.ZERO, false)));
|
||||
break;
|
||||
case OP_RIPEMD160:
|
||||
if (stack.size() < 1)
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.bitcoinj.core.Address;
|
|||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.SegwitAddress;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.crypto.TransactionSignature;
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
package org.bitcoinj.script;
|
||||
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
@ -120,11 +120,11 @@ public class ScriptChunk {
|
|||
} else if (opcode == OP_PUSHDATA2) {
|
||||
checkState(data.length <= 0xFFFF);
|
||||
stream.write(OP_PUSHDATA2);
|
||||
Utils.uint16ToByteStreamLE(data.length, stream);
|
||||
ByteUtils.uint16ToByteStreamLE(data.length, stream);
|
||||
} else if (opcode == OP_PUSHDATA4) {
|
||||
checkState(data.length <= Script.MAX_SCRIPT_ELEMENT_SIZE);
|
||||
stream.write(OP_PUSHDATA4);
|
||||
Utils.uint32ToByteStreamLE(data.length, stream);
|
||||
ByteUtils.uint32ToByteStreamLE(data.length, stream);
|
||||
} else {
|
||||
throw new RuntimeException("Unimplemented");
|
||||
}
|
||||
|
@ -165,7 +165,7 @@ public class ScriptChunk {
|
|||
public String toString() {
|
||||
if (data == null)
|
||||
return getOpCodeName(opcode);
|
||||
return String.format("%s[%s]", getPushDataName(opcode), Utils.HEX.encode(data));
|
||||
return String.format("%s[%s]", getPushDataName(opcode), ByteUtils.HEX.encode(data));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
|
||||
package org.bitcoinj.script;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.SegwitAddress;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -261,7 +261,7 @@ public class ScriptPattern {
|
|||
return chunks.size() > 0 && chunks.get(0).equalsOpCode(ScriptOpCodes.OP_RETURN);
|
||||
}
|
||||
|
||||
private static final byte[] SEGWIT_COMMITMENT_HEADER = Utils.HEX.decode("aa21a9ed");
|
||||
private static final byte[] SEGWIT_COMMITMENT_HEADER = ByteUtils.HEX.decode("aa21a9ed");
|
||||
|
||||
/**
|
||||
* Returns whether this script matches the pattern for a segwit commitment (in an output of the coinbase
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
package org.bitcoinj.signers;
|
||||
|
||||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.TransactionInput;
|
||||
import org.bitcoinj.core.TransactionOutput;
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.bitcoinj.store;
|
|||
|
||||
import org.bitcoinj.core.BlockChain;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
package org.bitcoinj.store;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Address;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.base.Coin;
|
||||
|
@ -25,14 +26,13 @@ import org.bitcoinj.core.ECKey;
|
|||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.ProtocolException;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.StoredUndoableBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.TransactionOutputChanges;
|
||||
import org.bitcoinj.core.UTXO;
|
||||
import org.bitcoinj.core.UTXOProviderException;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.core.VerificationException;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.Script.ScriptType;
|
||||
|
@ -684,7 +684,7 @@ public abstract class DatabaseFullPrunedBlockStore implements FullPrunedBlockSto
|
|||
txOutChanges = bos.toByteArray();
|
||||
} else {
|
||||
int numTxn = undoableBlock.getTransactions().size();
|
||||
Utils.uint32ToByteStreamLE(numTxn, bos);
|
||||
ByteUtils.uint32ToByteStreamLE(numTxn, bos);
|
||||
for (Transaction tx : undoableBlock.getTransactions())
|
||||
tx.bitcoinSerialize(bos);
|
||||
transactions = bos.toByteArray();
|
||||
|
@ -812,7 +812,7 @@ public abstract class DatabaseFullPrunedBlockStore implements FullPrunedBlockSto
|
|||
byte[] transactions = results.getBytes(2);
|
||||
StoredUndoableBlock block;
|
||||
if (txOutChanges == null) {
|
||||
int numTxn = (int) Utils.readUint32(transactions, 0);
|
||||
int numTxn = (int) ByteUtils.readUint32(transactions, 0);
|
||||
int offset = 4;
|
||||
List<Transaction> transactionList = new LinkedList<>();
|
||||
for (int i = 0; i < numTxn; i++) {
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
package org.bitcoinj.store;
|
||||
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.StoredUndoableBlock;
|
||||
import org.bitcoinj.core.UTXO;
|
||||
|
|
|
@ -18,7 +18,7 @@ package org.bitcoinj.store;
|
|||
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.VerificationException;
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ import org.bitcoinj.core.Address;
|
|||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.StoredUndoableBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
|
|
|
@ -18,12 +18,12 @@
|
|||
|
||||
package org.bitcoinj.store;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Address;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.StoredUndoableBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -186,7 +186,7 @@ public class PostgresFullPrunedBlockStore extends DatabaseFullPrunedBlockStore {
|
|||
txOutChanges = bos.toByteArray();
|
||||
} else {
|
||||
int numTxn = undoableBlock.getTransactions().size();
|
||||
Utils.uint32ToByteStreamLE(numTxn, bos);
|
||||
ByteUtils.uint32ToByteStreamLE(numTxn, bos);
|
||||
for (Transaction tx : undoableBlock.getTransactions())
|
||||
tx.bitcoinSerialize(bos);
|
||||
transactions = bos.toByteArray();
|
||||
|
@ -199,7 +199,7 @@ public class PostgresFullPrunedBlockStore extends DatabaseFullPrunedBlockStore {
|
|||
|
||||
try {
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Looking for undoable block with hash: " + Utils.HEX.encode(hashBytes));
|
||||
log.debug("Looking for undoable block with hash: " + ByteUtils.HEX.encode(hashBytes));
|
||||
|
||||
PreparedStatement findS = conn.get().prepareStatement(SELECT_UNDOABLEBLOCKS_EXISTS_SQL);
|
||||
findS.setBytes(1, hashBytes);
|
||||
|
@ -217,7 +217,7 @@ public class PostgresFullPrunedBlockStore extends DatabaseFullPrunedBlockStore {
|
|||
s.setBytes(3, hashBytes);
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Updating undoable block with hash: " + Utils.HEX.encode(hashBytes));
|
||||
log.debug("Updating undoable block with hash: " + ByteUtils.HEX.encode(hashBytes));
|
||||
|
||||
if (transactions == null) {
|
||||
s.setBytes(1, txOutChanges);
|
||||
|
@ -238,7 +238,7 @@ public class PostgresFullPrunedBlockStore extends DatabaseFullPrunedBlockStore {
|
|||
s.setInt(2, height);
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Inserting undoable block with hash: " + Utils.HEX.encode(hashBytes) + " at height " + height);
|
||||
log.debug("Inserting undoable block with hash: " + ByteUtils.HEX.encode(hashBytes) + " at height " + height);
|
||||
|
||||
if (transactions == null) {
|
||||
s.setBytes(3, txOutChanges);
|
||||
|
|
|
@ -19,7 +19,7 @@ package org.bitcoinj.store;
|
|||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.ProtocolException;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.utils.Threading;
|
||||
import org.slf4j.Logger;
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.bitcoinj.core.LegacyAddress;
|
|||
import org.bitcoinj.core.MessageSerializer;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.ProtocolException;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.TransactionConfidence;
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
|
||||
package org.bitcoinj.utils;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.ProtocolException;
|
||||
import org.bitcoinj.core.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
@ -156,7 +156,7 @@ public class BlockFileLoader implements Iterable<Block>, Iterator<Block> {
|
|||
}
|
||||
byte[] bytes = new byte[4];
|
||||
currentFileStream.read(bytes, 0, 4);
|
||||
long size = Utils.readUint32BE(Utils.reverseBytes(bytes), 0);
|
||||
long size = ByteUtils.readUint32BE(ByteUtils.reverseBytes(bytes), 0);
|
||||
bytes = new byte[(int) size];
|
||||
currentFileStream.read(bytes, 0, (int) size);
|
||||
try {
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.bitcoinj.wallet;
|
|||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.base.Stopwatch;
|
||||
import com.google.protobuf.ByteString;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.BloomFilter;
|
||||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
|
@ -907,7 +908,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
|
|||
boolean isMarried = !isFollowingKey && !chains.isEmpty() && chains.get(chains.size() - 1).isFollowing();
|
||||
// If this has a private key but no seed, then all we know is the spending key H
|
||||
if (seed == null && key.hasSecretBytes()) {
|
||||
DeterministicKey accountKey = new DeterministicKey(path, chainCode, pubkey, Utils.bytesToBigInteger(key.getSecretBytes().toByteArray()), null);
|
||||
DeterministicKey accountKey = new DeterministicKey(path, chainCode, pubkey, ByteUtils.bytesToBigInteger(key.getSecretBytes().toByteArray()), null);
|
||||
accountKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000);
|
||||
chain = factory.makeSpendingKeyChain(accountKey, isMarried, outputScriptType);
|
||||
isSpendingKey = true;
|
||||
|
@ -934,7 +935,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
|
|||
DeterministicKey detkey;
|
||||
if (key.hasSecretBytes()) {
|
||||
// Not encrypted: private key is available.
|
||||
final BigInteger priv = Utils.bytesToBigInteger(key.getSecretBytes().toByteArray());
|
||||
final BigInteger priv = ByteUtils.bytesToBigInteger(key.getSecretBytes().toByteArray());
|
||||
detkey = new DeterministicKey(path, chainCode, pubkey, priv, parent);
|
||||
} else {
|
||||
if (key.hasEncryptedData()) {
|
||||
|
|
|
@ -36,7 +36,7 @@ import java.util.Objects;
|
|||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
|
||||
/**
|
||||
* Holds the seed bytes for the BIP32 deterministic wallet algorithm, inside a
|
||||
|
|
|
@ -18,10 +18,10 @@ package org.bitcoinj.wallet;
|
|||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.protobuf.ByteString;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.BloomFilter;
|
||||
import org.bitcoinj.core.ECKey;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.Utils;
|
||||
import org.bitcoinj.crypto.ChildNumber;
|
||||
import org.bitcoinj.crypto.DeterministicKey;
|
||||
import org.bitcoinj.crypto.KeyCrypter;
|
||||
|
@ -250,7 +250,7 @@ public class MarriedKeyChain extends DeterministicKeyChain {
|
|||
builder.append(" addr:");
|
||||
builder.append(script.getToAddress(params));
|
||||
builder.append(" hash160:");
|
||||
builder.append(Utils.HEX.encode(script.getPubKeyHash()));
|
||||
builder.append(ByteUtils.HEX.encode(script.getPubKeyHash()));
|
||||
if (script.getCreationTimeSeconds() > 0)
|
||||
builder.append(" creationTimeSeconds:").append(script.getCreationTimeSeconds());
|
||||
builder.append('\n');
|
||||
|
|
|
@ -40,7 +40,7 @@ import org.bitcoinj.core.NetworkParameters;
|
|||
import org.bitcoinj.core.Peer;
|
||||
import org.bitcoinj.core.PeerFilterProvider;
|
||||
import org.bitcoinj.core.PeerGroup;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.StoredBlock;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.TransactionBag;
|
||||
|
|
|
@ -24,7 +24,7 @@ import com.google.protobuf.WireFormat;
|
|||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.core.NetworkParameters;
|
||||
import org.bitcoinj.core.PeerAddress;
|
||||
import org.bitcoinj.core.Sha256Hash;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.bitcoinj.core.TransactionConfidence;
|
||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||
|
|
235
core/src/test/java/org/bitcoinj/base/utils/ByteUtilsTest.java
Normal file
235
core/src/test/java/org/bitcoinj/base/utils/ByteUtilsTest.java
Normal file
|
@ -0,0 +1,235 @@
|
|||
/*
|
||||
* Copyright by the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.bitcoinj.base.utils;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ByteUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testReverseBytes() {
|
||||
assertArrayEquals(new byte[]{1, 2, 3, 4, 5}, ByteUtils.reverseBytes(new byte[]{5, 4, 3, 2, 1}));
|
||||
assertArrayEquals(new byte[]{0}, ByteUtils.reverseBytes(new byte[]{0}));
|
||||
assertArrayEquals(new byte[]{}, ByteUtils.reverseBytes(new byte[]{}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compactEncoding() {
|
||||
assertEquals(new BigInteger("1234560000", 16), ByteUtils.decodeCompactBits(0x05123456L));
|
||||
assertEquals(new BigInteger("c0de000000", 16), ByteUtils.decodeCompactBits(0x0600c0de));
|
||||
assertEquals(0x05123456L, ByteUtils.encodeCompactBits(new BigInteger("1234560000", 16)));
|
||||
assertEquals(0x0600c0deL, ByteUtils.encodeCompactBits(new BigInteger("c0de000000", 16)));
|
||||
// UnitTest difficulty
|
||||
assertEquals(new BigInteger("7fffff0000000000000000000000000000000000000000000000000000000000", 16), ByteUtils.decodeCompactBits(0x207fFFFFL));
|
||||
assertEquals(0x207fFFFFL, ByteUtils.encodeCompactBits(new BigInteger("7fffff0000000000000000000000000000000000000000000000000000000000", 16)));
|
||||
// MainNet starting difficulty
|
||||
assertEquals(new BigInteger("00000000FFFF0000000000000000000000000000000000000000000000000000", 16), ByteUtils.decodeCompactBits(0x1d00ffffL));
|
||||
assertEquals(0x1d00ffffL, ByteUtils.encodeCompactBits(new BigInteger("00000000FFFF0000000000000000000000000000000000000000000000000000", 16)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bigIntegerToBytes_roundTrip() {
|
||||
int ITERATIONS = 100;
|
||||
int LENGTH = 32;
|
||||
Random rnd = new Random();
|
||||
byte[] bytes = new byte[LENGTH];
|
||||
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
rnd.nextBytes(bytes);
|
||||
BigInteger bi = ByteUtils.bytesToBigInteger(bytes);
|
||||
assertArrayEquals(ByteUtils.HEX.encode(bytes), bytes, ByteUtils.bigIntegerToBytes(bi, LENGTH));
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bigIntegerToBytes_convertNegativeNumber() {
|
||||
BigInteger b = BigInteger.valueOf(-1);
|
||||
ByteUtils.bigIntegerToBytes(b, 32);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bigIntegerToBytes_convertWithNegativeLength() {
|
||||
BigInteger b = BigInteger.valueOf(10);
|
||||
ByteUtils.bigIntegerToBytes(b, -1);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bigIntegerToBytes_convertWithZeroLength() {
|
||||
BigInteger b = BigInteger.valueOf(10);
|
||||
ByteUtils.bigIntegerToBytes(b, 0);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bigIntegerToBytes_insufficientLength() {
|
||||
BigInteger b = BigInteger.valueOf(0b1000__0000_0000); // base 2
|
||||
ByteUtils.bigIntegerToBytes(b, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bigIntegerToBytes_convertZero() {
|
||||
BigInteger b = BigInteger.valueOf(0);
|
||||
byte[] expected = new byte[]{0b0000_0000};
|
||||
byte[] actual = ByteUtils.bigIntegerToBytes(b, 1);
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bigIntegerToBytes_singleByteSignFit() {
|
||||
BigInteger b = BigInteger.valueOf(0b0000_1111);
|
||||
byte[] expected = new byte[]{0b0000_1111};
|
||||
byte[] actual = ByteUtils.bigIntegerToBytes(b, 1);
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bigIntegerToBytes_paddedSingleByte() {
|
||||
BigInteger b = BigInteger.valueOf(0b0000_1111);
|
||||
byte[] expected = new byte[]{0, 0b0000_1111};
|
||||
byte[] actual = ByteUtils.bigIntegerToBytes(b, 2);
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bigIntegerToBytes_singleByteSignDoesNotFit() {
|
||||
BigInteger b = BigInteger.valueOf(0b1000_0000); // 128 (2-compl does not fit in one byte)
|
||||
byte[] expected = new byte[]{-128}; // -128 == 1000_0000 (compl-2)
|
||||
byte[] actual = ByteUtils.bigIntegerToBytes(b, 1);
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUint16() {
|
||||
assertEquals(258L, ByteUtils.readUint16(new byte[]{2, 1}, 0));
|
||||
assertEquals(258L, ByteUtils.readUint16(new byte[]{2, 1, 3, 4}, 0));
|
||||
assertEquals(772L, ByteUtils.readUint16(new byte[]{1, 2, 4, 3}, 2));
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint16ThrowsException1() {
|
||||
ByteUtils.readUint16(new byte[]{1}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint16ThrowsException2() {
|
||||
ByteUtils.readUint16(new byte[]{1, 2, 3}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint16ThrowsException3() {
|
||||
ByteUtils.readUint16(new byte[]{1, 2, 3}, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUint32() {
|
||||
assertEquals(258L, ByteUtils.readUint32(new byte[]{2, 1, 0, 0}, 0));
|
||||
assertEquals(258L, ByteUtils.readUint32(new byte[]{2, 1, 0, 0, 3, 4}, 0));
|
||||
assertEquals(772L, ByteUtils.readUint32(new byte[]{1, 2, 4, 3, 0, 0}, 2));
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint32ThrowsException1() {
|
||||
ByteUtils.readUint32(new byte[]{1, 2, 3}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint32ThrowsException2() {
|
||||
ByteUtils.readUint32(new byte[]{1, 2, 3, 4, 5}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint32ThrowsException3() {
|
||||
ByteUtils.readUint32(new byte[]{1, 2, 3, 4, 5}, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadInt64() {
|
||||
assertEquals(258L, ByteUtils.readInt64(new byte[]{2, 1, 0, 0, 0, 0, 0, 0}, 0));
|
||||
assertEquals(258L, ByteUtils.readInt64(new byte[]{2, 1, 0, 0, 0, 0, 0, 0, 3, 4}, 0));
|
||||
assertEquals(772L, ByteUtils.readInt64(new byte[]{1, 2, 4, 3, 0, 0, 0, 0, 0, 0}, 2));
|
||||
assertEquals(-1L, ByteUtils.readInt64(new byte[]{-1, -1, -1, -1, -1, -1, -1, -1}, 0));
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadInt64ThrowsException1() {
|
||||
ByteUtils.readInt64(new byte[]{1, 2, 3, 4, 5, 6, 7}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadInt64ThrowsException2() {
|
||||
ByteUtils.readInt64(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadInt64ThrowsException3() {
|
||||
ByteUtils.readInt64(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUInt32BE() {
|
||||
assertEquals(258L, ByteUtils.readUint32BE(new byte[]{0, 0, 1, 2}, 0));
|
||||
assertEquals(258L, ByteUtils.readUint32BE(new byte[]{0, 0, 1, 2, 3, 4}, 0));
|
||||
assertEquals(772L, ByteUtils.readUint32BE(new byte[]{1, 2, 0, 0, 3, 4}, 2));
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint32BEThrowsException1() {
|
||||
ByteUtils.readUint32BE(new byte[]{1, 2, 3}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint32BEThrowsException2() {
|
||||
ByteUtils.readUint32BE(new byte[]{1, 2, 3, 4, 5}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint32BEThrowsException3() {
|
||||
ByteUtils.readUint32BE(new byte[]{1, 2, 3, 4, 5}, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUint16BE() {
|
||||
assertEquals(258L, ByteUtils.readUint16BE(new byte[]{1, 2}, 0));
|
||||
assertEquals(258L, ByteUtils.readUint16BE(new byte[]{1, 2, 3, 4}, 0));
|
||||
assertEquals(772L, ByteUtils.readUint16BE(new byte[]{0, 0, 3, 4}, 2));
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint16BEThrowsException1() {
|
||||
ByteUtils.readUint16BE(new byte[]{1}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint16BEThrowsException2() {
|
||||
ByteUtils.readUint16BE(new byte[]{1, 2, 3}, 2);
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void testReadUint16BEThrowsException3() {
|
||||
ByteUtils.readUint16BE(new byte[]{1, 2, 3}, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeMPI() {
|
||||
assertEquals(BigInteger.ZERO, ByteUtils.decodeMPI(new byte[]{}, false));
|
||||
}
|
||||
}
|
|
@ -23,7 +23,7 @@ import java.net.Inet4Address;
|
|||
import java.net.Inet6Address;
|
||||
import java.util.List;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
|
@ -23,7 +23,7 @@ import java.net.Inet4Address;
|
|||
import java.net.Inet6Address;
|
||||
import java.util.List;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -26,6 +27,6 @@ public class Base58DecodeToBigIntegerTest {
|
|||
@Test
|
||||
public void testDecodeToBigInteger() {
|
||||
byte[] input = Base58.decode("129");
|
||||
assertEquals(Utils.bytesToBigInteger(input), Base58.decodeToBigInteger("129"));
|
||||
assertEquals(ByteUtils.bytesToBigInteger(input), Base58.decodeToBigInteger("129"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
import org.junit.Test;
|
||||
|
@ -28,7 +29,7 @@ import java.nio.BufferUnderflowException;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
@ -160,7 +161,7 @@ public class BitcoinSerializerTest {
|
|||
Block block = headersMessage.getBlockHeaders().get(0);
|
||||
assertEquals("00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048", block.getHashAsString());
|
||||
assertNotNull(block.transactions);
|
||||
assertEquals("0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098", Utils.HEX.encode(block.getMerkleRoot().getBytes()));
|
||||
assertEquals("0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098", ByteUtils.HEX.encode(block.getMerkleRoot().getBytes()));
|
||||
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
serializer.serialize(headersMessage, byteArrayOutputStream);
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.bitcoinj.core;
|
|||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.Iterables;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.net.NioClient;
|
||||
import org.bitcoinj.params.RegTestParams;
|
||||
import org.bitcoinj.store.BlockStoreException;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
import org.bitcoinj.params.UnitTestParams;
|
||||
|
|
|
@ -19,6 +19,8 @@ package org.bitcoinj.core;
|
|||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.AbstractBlockChain.NewBlockType;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
|
@ -39,7 +41,7 @@ import java.util.Arrays;
|
|||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
@ -331,12 +333,12 @@ public class BlockTest {
|
|||
Block block = new Block(UNITTEST, 1, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 1, 1, 1, new ArrayList<Transaction>()) {
|
||||
@Override
|
||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
Utils.uint32ToByteStreamLE(getVersion(), stream);
|
||||
ByteUtils.uint32ToByteStreamLE(getVersion(), stream);
|
||||
stream.write(getPrevBlockHash().getReversedBytes());
|
||||
stream.write(getMerkleRoot().getReversedBytes());
|
||||
Utils.uint32ToByteStreamLE(getTimeSeconds(), stream);
|
||||
Utils.uint32ToByteStreamLE(getDifficultyTarget(), stream);
|
||||
Utils.uint32ToByteStreamLE(getNonce(), stream);
|
||||
ByteUtils.uint32ToByteStreamLE(getTimeSeconds(), stream);
|
||||
ByteUtils.uint32ToByteStreamLE(getDifficultyTarget(), stream);
|
||||
ByteUtils.uint32ToByteStreamLE(getNonce(), stream);
|
||||
|
||||
stream.write(new VarInt(Integer.MAX_VALUE).encode());
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.bitcoinj.wallet.KeyChainGroup;
|
|||
import org.bitcoinj.wallet.Wallet;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||
import org.bitcoinj.params.UnitTestParams;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.ECKey.ECDSASignature;
|
||||
import org.bitcoinj.crypto.EncryptedData;
|
||||
import org.bitcoinj.crypto.KeyCrypter;
|
||||
|
@ -45,8 +47,8 @@ import java.util.stream.Collectors;
|
|||
import java.util.stream.IntStream;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.core.Utils.reverseBytes;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.reverseBytes;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
@ -104,7 +106,7 @@ public class ECKeyTest {
|
|||
public void testSignatures() throws Exception {
|
||||
// Test that we can construct an ECKey from a private key (deriving the public from the private), then signing
|
||||
// a message with it.
|
||||
BigInteger privkey = Utils.bytesToBigInteger(HEX.decode("180cb41c7c600be951b5d3d0a7334acc7506173875834f7a6c4c786a28fcbb19"));
|
||||
BigInteger privkey = ByteUtils.bytesToBigInteger(HEX.decode("180cb41c7c600be951b5d3d0a7334acc7506173875834f7a6c4c786a28fcbb19"));
|
||||
ECKey key = ECKey.fromPrivate(privkey);
|
||||
byte[] output = key.sign(Sha256Hash.ZERO_HASH).encodeToDER();
|
||||
assertTrue(key.verify(Sha256Hash.ZERO_HASH.getBytes(), output));
|
||||
|
@ -203,8 +205,8 @@ public class ECKeyTest {
|
|||
ECKey key = new ECKey();
|
||||
ECKey key1 = DumpedPrivateKey.fromBase58(TESTNET,
|
||||
key.getPrivateKeyEncoded(TESTNET).toString()).getKey();
|
||||
assertEquals(Utils.HEX.encode(key.getPrivKeyBytes()),
|
||||
Utils.HEX.encode(key1.getPrivKeyBytes()));
|
||||
assertEquals(ByteUtils.HEX.encode(key.getPrivKeyBytes()),
|
||||
ByteUtils.HEX.encode(key1.getPrivKeyBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -321,7 +323,7 @@ public class ECKeyTest {
|
|||
public void testEncryptedCreate() {
|
||||
ECKey unencryptedKey = new ECKey();
|
||||
byte[] originalPrivateKeyBytes = checkNotNull(unencryptedKey.getPrivKeyBytes());
|
||||
log.info("Original private key = " + Utils.HEX.encode(originalPrivateKeyBytes));
|
||||
log.info("Original private key = " + ByteUtils.HEX.encode(originalPrivateKeyBytes));
|
||||
EncryptedData encryptedPrivateKey = keyCrypter.encrypt(unencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
|
||||
ECKey encryptedKey = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, unencryptedKey.getPubKey());
|
||||
assertTrue(encryptedKey.isEncrypted());
|
||||
|
@ -478,7 +480,7 @@ public class ECKeyTest {
|
|||
// We dump failed data to error log because this test is not expected to be deterministic
|
||||
ECKey key = new ECKey();
|
||||
if (!ECKey.isPubKeyCanonical(key.getPubKey())) {
|
||||
log.error(Utils.HEX.encode(key.getPubKey()));
|
||||
log.error(ByteUtils.HEX.encode(key.getPubKey()));
|
||||
fail();
|
||||
}
|
||||
|
||||
|
@ -488,7 +490,7 @@ public class ECKeyTest {
|
|||
byte[] encodedSig = Arrays.copyOf(sigBytes, sigBytes.length + 1);
|
||||
encodedSig[sigBytes.length] = Transaction.SigHash.ALL.byteValue();
|
||||
if (!TransactionSignature.isEncodingCanonical(encodedSig)) {
|
||||
log.error(Utils.HEX.encode(sigBytes));
|
||||
log.error(ByteUtils.HEX.encode(sigBytes));
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ package org.bitcoinj.core;
|
|||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.core.Transaction.SigHash;
|
||||
import org.bitcoinj.crypto.TransactionSignature;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
@ -184,11 +186,11 @@ public class FullBlockTestGenerator {
|
|||
public boolean add(Rule element) {
|
||||
if (outStream != null && element instanceof BlockAndValidity) {
|
||||
try {
|
||||
Utils.uint32ToByteStreamBE(params.getPacketMagic(), outStream);
|
||||
ByteUtils.uint32ToByteStreamBE(params.getPacketMagic(), outStream);
|
||||
byte[] block = ((BlockAndValidity)element).block.bitcoinSerialize();
|
||||
byte[] length = new byte[4];
|
||||
Utils.uint32ToByteArrayBE(block.length, length, 0);
|
||||
outStream.write(Utils.reverseBytes(length));
|
||||
ByteUtils.uint32ToByteArrayBE(block.length, length, 0);
|
||||
outStream.write(ByteUtils.reverseBytes(length));
|
||||
outStream.write(block);
|
||||
((BlockAndValidity)element).block = null;
|
||||
} catch (IOException e) {
|
||||
|
@ -1214,8 +1216,8 @@ public class FullBlockTestGenerator {
|
|||
|
||||
byte[] varIntBytes = new byte[9];
|
||||
varIntBytes[0] = (byte) 255;
|
||||
Utils.uint32ToByteArrayLE((long)b64Original.block.getTransactions().size(), varIntBytes, 1);
|
||||
Utils.uint32ToByteArrayLE(((long)b64Original.block.getTransactions().size()) >>> 32, varIntBytes, 5);
|
||||
ByteUtils.uint32ToByteArrayLE((long)b64Original.block.getTransactions().size(), varIntBytes, 1);
|
||||
ByteUtils.uint32ToByteArrayLE(((long)b64Original.block.getTransactions().size()) >>> 32, varIntBytes, 5);
|
||||
stream.write(varIntBytes);
|
||||
checkState(new VarInt(varIntBytes, 0).intValue() == b64Original.block.getTransactions().size());
|
||||
|
||||
|
@ -1373,7 +1375,7 @@ public class FullBlockTestGenerator {
|
|||
Arrays.fill(outputScript, (byte) OP_CHECKSIG);
|
||||
// If we push an element that is too large, the CHECKSIGs after that push are still counted
|
||||
outputScript[Block.MAX_BLOCK_SIGOPS - sigOps] = OP_PUSHDATA4;
|
||||
Utils.uint32ToByteArrayLE(Script.MAX_SCRIPT_ELEMENT_SIZE + 1, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1);
|
||||
ByteUtils.uint32ToByteArrayLE(Script.MAX_SCRIPT_ELEMENT_SIZE + 1, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1);
|
||||
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
|
||||
addOnlyInputToTransaction(tx, b73);
|
||||
b73.addTransaction(tx);
|
||||
|
@ -1439,7 +1441,7 @@ public class FullBlockTestGenerator {
|
|||
Arrays.fill(outputScript, (byte) OP_CHECKSIG);
|
||||
// If we push an element that is filled with CHECKSIGs, they (obviously) arent counted
|
||||
outputScript[Block.MAX_BLOCK_SIGOPS - sigOps] = OP_PUSHDATA4;
|
||||
Utils.uint32ToByteArrayLE(Block.MAX_BLOCK_SIGOPS, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1);
|
||||
ByteUtils.uint32ToByteArrayLE(Block.MAX_BLOCK_SIGOPS, outputScript, Block.MAX_BLOCK_SIGOPS - sigOps + 1);
|
||||
tx.addOutput(new TransactionOutput(params, tx, SATOSHI, outputScript));
|
||||
addOnlyInputToTransaction(tx, b76);
|
||||
b76.addTransaction(tx);
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.bitcoinj.core;
|
|||
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import nl.jqno.equalsverifier.Warning;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.Networks;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
|
@ -33,7 +34,7 @@ import java.io.InputStreamReader;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
@ -68,10 +69,10 @@ public class LegacyAddressTest {
|
|||
@Test
|
||||
public void decoding() {
|
||||
LegacyAddress a = LegacyAddress.fromBase58(TESTNET, "n4eA2nbYqErp7H6jebchxAN59DmNpksexv");
|
||||
assertEquals("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc", Utils.HEX.encode(a.getHash()));
|
||||
assertEquals("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc", ByteUtils.HEX.encode(a.getHash()));
|
||||
|
||||
LegacyAddress b = LegacyAddress.fromBase58(MAINNET, "17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL");
|
||||
assertEquals("4a22c3c4cbb31e4d03b15550636762bda0baf85a", Utils.HEX.encode(b.getHash()));
|
||||
assertEquals("4a22c3c4cbb31e4d03b15550636762bda0baf85a", ByteUtils.HEX.encode(b.getHash()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.UnitTestParams;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
@ -31,7 +32,7 @@ import java.nio.ByteBuffer;
|
|||
|
||||
import static org.bitcoinj.base.Coin.COIN;
|
||||
import static org.bitcoinj.base.Coin.valueOf;
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.bitcoinj.testing.FakeTxBuilder.createFakeBlock;
|
||||
import static org.bitcoinj.testing.FakeTxBuilder.createFakeTx;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
@ -449,8 +450,8 @@ public class ParseByteCacheTest {
|
|||
if (sup.length < sub.length)
|
||||
return false;
|
||||
|
||||
String superstring = Utils.HEX.encode(sup);
|
||||
String substring = Utils.HEX.encode(sub);
|
||||
String superstring = ByteUtils.HEX.encode(sup);
|
||||
String substring = ByteUtils.HEX.encode(sub);
|
||||
|
||||
int ind = superstring.indexOf(substring);
|
||||
|
||||
|
|
|
@ -17,13 +17,14 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
@ -48,7 +49,7 @@ public class PeerAddressTest {
|
|||
MessageSerializer serializer = MAINNET.getDefaultSerializer().withProtocolVersion(0);
|
||||
PeerAddress pa = new PeerAddress(MAINNET, InetAddress.getByName(null), 8333, BigInteger.ZERO,
|
||||
serializer);
|
||||
assertEquals("000000000000000000000000000000000000ffff7f000001208d", Utils.HEX.encode(pa.bitcoinSerialize()));
|
||||
assertEquals("000000000000000000000000000000000000ffff7f000001208d", ByteUtils.HEX.encode(pa.bitcoinSerialize()));
|
||||
assertEquals(26, pa.length);
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ import org.easymock.Mock;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.bitcoinj.core;
|
|||
import com.google.common.base.MoreObjects;
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import nl.jqno.equalsverifier.Warning;
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
import org.bitcoinj.script.Script;
|
||||
|
@ -54,7 +55,7 @@ public class SegwitAddressTest {
|
|||
|
||||
assertEquals(MAINNET, address.params);
|
||||
assertEquals("0014751e76e8199196d454941c45d1b3a323f1433bd6",
|
||||
Utils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WPKH, address.getOutputScriptType());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toBech32());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toString());
|
||||
|
@ -68,7 +69,7 @@ public class SegwitAddressTest {
|
|||
|
||||
assertEquals(MAINNET, address.params);
|
||||
assertEquals("00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262",
|
||||
Utils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WSH, address.getOutputScriptType());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toBech32());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toString());
|
||||
|
@ -82,7 +83,7 @@ public class SegwitAddressTest {
|
|||
|
||||
assertEquals(TESTNET, address.params);
|
||||
assertEquals("0014751e76e8199196d454941c45d1b3a323f1433bd6",
|
||||
Utils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WPKH, address.getOutputScriptType());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toBech32());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toString());
|
||||
|
@ -96,7 +97,7 @@ public class SegwitAddressTest {
|
|||
|
||||
assertEquals(TESTNET, address.params);
|
||||
assertEquals("00201863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262",
|
||||
Utils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(ScriptType.P2WSH, address.getOutputScriptType());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toBech32());
|
||||
assertEquals(bech32.toLowerCase(Locale.ROOT), address.toString());
|
||||
|
@ -109,10 +110,10 @@ public class SegwitAddressTest {
|
|||
|
||||
assertEquals(valid.expectedParams, address.params);
|
||||
assertEquals(valid.expectedScriptPubKey,
|
||||
Utils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
ByteUtils.HEX.encode(ScriptBuilder.createOutputScript(address).getProgram()));
|
||||
assertEquals(valid.address.toLowerCase(Locale.ROOT), address.toBech32());
|
||||
if (valid.expectedWitnessVersion == 0) {
|
||||
Script expectedScriptPubKey = new Script(Utils.HEX.decode(valid.expectedScriptPubKey));
|
||||
Script expectedScriptPubKey = new Script(ByteUtils.HEX.decode(valid.expectedScriptPubKey));
|
||||
assertEquals(address, SegwitAddress.fromHash(valid.expectedParams,
|
||||
ScriptPattern.extractHashFromP2WH(expectedScriptPubKey)));
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ import org.junit.Test;
|
|||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SendHeadersMessageTest {
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.bitcoinj.core;
|
|||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.params.UnitTestParams;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.ScriptBuilder;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||
import org.bitcoinj.crypto.TransactionSignature;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
|
@ -46,8 +47,8 @@ import java.util.concurrent.Executors;
|
|||
import java.util.function.IntFunction;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.bitcoinj.core.Utils.HEX;
|
||||
import static org.bitcoinj.core.Utils.uint32ToByteStreamLE;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.HEX;
|
||||
import static org.bitcoinj.base.utils.ByteUtils.uint32ToByteStreamLE;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.utils.ByteUtils;
|
||||
import org.bitcoinj.crypto.TransactionSignature;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
|
@ -33,9 +34,9 @@ public class TransactionWitnessTest {
|
|||
assertEquals("", w2.toString());
|
||||
|
||||
TransactionWitness w3 = new TransactionWitness(3);
|
||||
w3.setPush(0, Utils.HEX.decode("123aaa"));
|
||||
w3.setPush(1, Utils.HEX.decode("123bbb"));
|
||||
w3.setPush(3, Utils.HEX.decode("123ccc"));
|
||||
w3.setPush(0, ByteUtils.HEX.decode("123aaa"));
|
||||
w3.setPush(1, ByteUtils.HEX.decode("123bbb"));
|
||||
w3.setPush(3, ByteUtils.HEX.decode("123ccc"));
|
||||
assertEquals("123aaa 123bbb EMPTY 123ccc", w3.toString());
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.params.UnitTestParams;
|
||||
import org.bitcoinj.testing.FakeTxBuilder;
|
||||
import org.bitcoinj.utils.BriefLogFormatter;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.bitcoinj.core;
|
||||
|
||||
import org.bitcoinj.base.Coin;
|
||||
import org.bitcoinj.base.Sha256Hash;
|
||||
import org.bitcoinj.script.ScriptBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue