public class Utils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
BITCOIN_SIGNED_MESSAGE_HEADER
The string that prefixes all text messages signed using Bitcoin keys.
|
static byte[] |
BITCOIN_SIGNED_MESSAGE_HEADER_BYTES |
static BaseEncoding |
HEX
Hex encoding used throughout the framework.
|
static Date |
mockTime
If non-null, overrides the return value of now().
|
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
appendByte(byte[] bytes,
byte b)
Creates a copy of bytes and appends b to the end of it
|
static byte[] |
bigIntegerToBytes(BigInteger b,
int numBytes)
The regular
BigInteger.toByteArray() method isn't quite what we often need: it appends a
leading zero to indicate that the number is positive and may need padding. |
static boolean |
checkBitLE(byte[] data,
int index)
Checks if the given bit is set in data, using little endian (not the same as Java native big endian)
|
static InputStream |
closeUnchecked(InputStream stream) |
static OutputStream |
closeUnchecked(OutputStream stream) |
static byte[] |
copyOf(byte[] in,
int length) |
static long |
currentTimeMillis()
Returns the current time in milliseconds since the epoch, or a mocked out equivalent.
|
static long |
currentTimeSeconds() |
static String |
dateTimeFormat(Date dateTime)
Formats a given date+time value to an ISO 8601 string.
|
static String |
dateTimeFormat(long dateTime)
Formats a given date+time value to an ISO 8601 string.
|
static BigInteger |
decodeCompactBits(long compact)
The "compact" format is a representation of a whole number N using an unsigned 32 bit number similar to a
floating point format.
|
static BigInteger |
decodeMPI(byte[] mpi,
boolean hasLength)
MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
|
static long |
encodeCompactBits(BigInteger value) |
static byte[] |
encodeMPI(BigInteger value,
boolean includeLength)
MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
|
static void |
finishMockSleep()
Let the sleeping thread pass the synchronization point any number of times.
|
static byte[] |
formatMessageForSigning(String message)
Given a textual message, returns a byte buffer formatted as follows:
|
static String |
getResourceAsString(URL url)
Reads and joins together with LF char (\n) all the lines from given file.
|
static void |
int64ToByteStreamLE(long val,
OutputStream stream) |
static boolean |
isAndroidRuntime() |
static boolean |
isLessThanOrEqualToUnsigned(long n1,
long n2)
Work around lack of unsigned types in Java.
|
static boolean |
isLessThanUnsigned(long n1,
long n2)
Work around lack of unsigned types in Java.
|
static boolean |
isWindows() |
static <T> String |
join(Iterable<T> items)
Returns a string containing the string representation of the given items,
delimited by a single space character.
|
static int |
maxOfMostFreq(int... items) |
static int |
maxOfMostFreq(List<Integer> items) |
static Date |
now()
Returns the current time, or a mocked out equivalent.
|
static byte[] |
parseAsHexOrBase58(String data)
Attempts to parse the given string as arbitrary-length hex or base58 and then return the results, or null if
neither parse was successful.
|
static void |
passMockSleep()
Let sleeping thread pass the synchronization point.
|
static long |
readInt64(byte[] bytes,
int offset)
Parse 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format.
|
static int |
readUint16BE(byte[] bytes,
int offset)
Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format.
|
static long |
readUint32(byte[] bytes,
int offset)
Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.
|
static long |
readUint32BE(byte[] bytes,
int offset)
Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format.
|
static byte[] |
reverseBytes(byte[] bytes)
Returns a copy of the given byte array in reverse order.
|
static byte[] |
reverseDwordBytes(byte[] bytes,
int trimLength)
Returns a copy of the given byte array with the bytes of each double-word (4 bytes) reversed.
|
static Date |
rollMockClock(int seconds)
Advances (or rewinds) the mock clock by the given number of seconds.
|
static Date |
rollMockClockMillis(long millis)
Advances (or rewinds) the mock clock by the given number of milliseconds.
|
static void |
setBitLE(byte[] data,
int index)
Sets the given bit in data to one, using little endian (not the same as Java native big endian)
|
static void |
setMockClock()
Sets the mock clock to the current time.
|
static void |
setMockClock(long mockClockSeconds)
Sets the mock clock to the given time (in seconds).
|
static void |
setMockSleep(boolean isEnable)
Enable or disable mock sleep.
|
static byte[] |
sha256hash160(byte[] input)
Calculates RIPEMD160(SHA256(input)).
|
static void |
sleep(long millis)
Sleep for a span of time, or mock sleep if enabled
|
static byte[] |
toBytes(CharSequence str,
String charsetName)
Encodes the given string into a sequence of bytes using the named charset.
|
static String |
toString(byte[] bytes,
String charsetName)
Constructs a new String by decoding the given bytes using the specified charset.
|
static void |
uint32ToByteArrayBE(long val,
byte[] out,
int offset) |
static void |
uint32ToByteArrayLE(long val,
byte[] out,
int offset) |
static void |
uint32ToByteStreamLE(long val,
OutputStream stream) |
static void |
uint64ToByteArrayLE(long val,
byte[] out,
int offset) |
static void |
uint64ToByteStreamLE(BigInteger val,
OutputStream stream) |
public static final String BITCOIN_SIGNED_MESSAGE_HEADER
public static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES
public static final BaseEncoding HEX
public static volatile Date mockTime
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes)
BigInteger.toByteArray()
method isn't quite what we often need: it appends a
leading zero to indicate that the number is positive and may need padding.b
- the integer to format into a byte arraynumBytes
- the desired size of the resulting byte arraypublic static void uint32ToByteArrayBE(long val, byte[] out, int offset)
public static void uint32ToByteArrayLE(long val, byte[] out, int offset)
public static void uint64ToByteArrayLE(long val, byte[] out, int offset)
public static void uint32ToByteStreamLE(long val, OutputStream stream) throws IOException
IOException
public static void int64ToByteStreamLE(long val, OutputStream stream) throws IOException
IOException
public static void uint64ToByteStreamLE(BigInteger val, OutputStream stream) throws IOException
IOException
public static boolean isLessThanUnsigned(long n1, long n2)
public static boolean isLessThanOrEqualToUnsigned(long n1, long n2)
public static byte[] reverseBytes(byte[] bytes)
public static byte[] reverseDwordBytes(byte[] bytes, int trimLength)
bytes
- length must be divisible by 4.trimLength
- trim output to this length. If positive, must be divisible by 4.public static long readUint32(byte[] bytes, int offset)
public static long readInt64(byte[] bytes, int offset)
public static long readUint32BE(byte[] bytes, int offset)
public static int readUint16BE(byte[] bytes, int offset)
public static byte[] sha256hash160(byte[] input)
public static BigInteger decodeMPI(byte[] mpi, boolean hasLength)
hasLength
- can be set to false if the given array is missing the 4 byte length fieldpublic static byte[] encodeMPI(BigInteger value, boolean includeLength)
includeLength
- indicates whether the 4 byte length field should be includedpublic static BigInteger decodeCompactBits(long compact)
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).
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.
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.
public static long encodeCompactBits(BigInteger value)
decodeCompactBits(long)
public static Date rollMockClock(int seconds)
public static Date rollMockClockMillis(long millis)
public static void setMockClock()
public static void setMockClock(long mockClockSeconds)
public static Date now()
public static long currentTimeMillis()
public static long currentTimeSeconds()
public static String dateTimeFormat(Date dateTime)
dateTime
- value to format, as a Datepublic static String dateTimeFormat(long dateTime)
dateTime
- value to format, unix time (ms)public static <T> String join(Iterable<T> items)
T
- the item typeitems
- the items to joinpublic static byte[] copyOf(byte[] in, int length)
public static byte[] appendByte(byte[] bytes, byte b)
public static String toString(byte[] bytes, String charsetName)
This is a convenience method which wraps the checked exception with a RuntimeException. The exception can never occur given the charsets US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16LE or UTF-16BE.
bytes
- the bytes to be decoded into characterscharsetName
- the name of a supported charsetpublic static byte[] toBytes(CharSequence str, String charsetName)
This is a convenience method which wraps the checked exception with a RuntimeException. The exception can never occur given the charsets US-ASCII, ISO-8859-1, UTF-8, UTF-16, UTF-16LE or UTF-16BE.
str
- the string to encode into bytescharsetName
- the name of a supported charsetpublic static byte[] parseAsHexOrBase58(String data)
public static boolean isWindows()
public static byte[] formatMessageForSigning(String message)
Given a textual message, returns a byte buffer formatted as follows:
[24] "Bitcoin Signed Message:\n" [message.length as a varint] message
public static boolean checkBitLE(byte[] data, int index)
public static void setBitLE(byte[] data, int index)
public static void sleep(long millis)
public static void setMockSleep(boolean isEnable)
public static void passMockSleep()
public static void finishMockSleep()
public static boolean isAndroidRuntime()
public static int maxOfMostFreq(int... items)
public static String getResourceAsString(URL url) throws IOException
IOException
public static InputStream closeUnchecked(InputStream stream)
public static OutputStream closeUnchecked(OutputStream stream)
Copyright © 2016. All rights reserved.