public class Utils
extends java.lang.Object
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static java.math.BigInteger |
bnToBn(java.lang.Object value)
Creates a BN value from a BN.js bignumber or number input.
|
static java.lang.String |
bnToHex(java.math.BigInteger value,
boolean isLe,
boolean isNegtive,
int bitLength) |
static java.lang.String |
bnToHex(java.math.BigInteger value,
int bitLength)
Creates a hex value from a BN.js bignumber object.
|
static byte[] |
bnToU8a(java.math.BigInteger value,
boolean isLe,
boolean isNegative,
int bitLength) |
static byte[] |
bnToU8a(java.math.BigInteger value,
boolean isLe,
int bitLength)
Creates a Uint8Array object from a BN.
|
static byte[] |
compactAddLength(byte[] input)
Adds a length prefix to the input value example
|
static <any> |
compactFromU8a(java.lang.Object input) |
static <any> |
compactFromU8a(java.lang.Object _input,
int bitLength)
Retrievs the offset and encoded length from a compact-prefixed value example
|
static <any> |
compactStripLength(byte[] input) |
static <any> |
compactStripLength(byte[] input,
int bitLength)
Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length example
|
static byte[] |
compactToU8a(java.lang.Object _value)
Encodes a number into a compact representation example
|
static boolean |
hexHasPrefix(java.lang.String value)
Tests for the existence of a
0x prefix. |
static java.lang.String |
hexStripPrefix(java.lang.String value) |
static java.math.BigInteger |
hexToBn(java.lang.Object value,
boolean isLe,
boolean isNegative) |
static byte[] |
hexToU8a(java.lang.String value) |
static byte[] |
hexToU8a(java.lang.String value,
int bitLength)
Creates a Buffer object from a hex string.
|
static boolean |
isContainer(java.lang.Object object) |
static boolean |
isHex(java.lang.Object value) |
static boolean |
isHex(java.lang.Object value,
int bitLength,
boolean ignoreLength) |
static boolean |
isU8a(java.lang.Object value)
Tests for a
Uint8Array object instance. |
static void |
main(java.lang.String[] argv) |
static byte[] |
randomAsU8a()
Creates a Uint8Array filled with random bytes.
|
static byte[] |
randomAsU8a(int length) |
static java.lang.String |
stringCamelCase(java.lang.String input) |
static java.lang.String |
stringLowerFirst(java.lang.String value)
Lowercase the first letter of a string Lowercase the first letter of a string example
|
static byte[] |
stringToU8a(java.lang.String value)
Creates a Uint8Array object from a utf-8 string.
|
static java.math.BigInteger |
toBn(java.lang.Object value) |
static byte[] |
toByteArrayLittleEndianUnsigned(java.math.BigInteger bi) |
static byte[] |
toByteArrayUnsigned(java.math.BigInteger bi) |
static java.lang.String |
toU8aString(byte[] bytes) |
static byte[] |
u8aConcat(java.util.List<byte[]> _list)
Creates a concatenated Uint8Array from the inputs.
|
static byte[] |
u8aFixLength(byte[] value,
int bitLength,
boolean atStart)
Shifts a Uint8Array to a specific bitLength Returns a uint8Array with the specified number of bits contained in the return value.
|
static boolean |
u8aStrEq(byte[] u8a1,
byte[] u8a2) |
static java.math.BigInteger |
u8aToBn(byte[] value,
boolean isLe,
boolean isNegative) |
static java.lang.String |
u8aToHex(byte[] value) |
static java.lang.String |
u8aToHex(byte[] value,
int bitLength,
boolean isPrefixed)
Creates a hex string from a Uint8Array object.
|
static java.lang.String |
u8aToString(byte[] value)
Creates a utf-8 string from a Uint8Array object.
|
static byte[] |
u8aToU8a(java.lang.Object value)
Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
|
public static boolean isHex(java.lang.Object value)
public static boolean isHex(java.lang.Object value, int bitLength, boolean ignoreLength)
public static byte[] hexToU8a(java.lang.String value, int bitLength)
Creates a Buffer object from a hex string. null
inputs returns an empty Uint8Array
result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the 0x
prefix) throws an error. example
hexToU8a("0x80001f"); // Uint8Array([0x80, 0x00, 0x1f])
hexToU8a("0x80001f", 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
public static byte[] hexToU8a(java.lang.String value)
public static java.lang.String hexStripPrefix(java.lang.String value)
public static boolean hexHasPrefix(java.lang.String value)
Tests for the existence of a 0x
prefix. Checks for a valid hex input value and if the start matched 0x
example
System.out.println(hexHasPrefix("0x1234")); // => true
public static boolean isU8a(java.lang.Object value)
Tests for a Uint8Array
object instance. Checks to see if the input object is an instance of Uint8Array
. example
System.out.println(isU8a([])); // => false
public static java.math.BigInteger hexToBn(java.lang.Object value, boolean isLe, boolean isNegative)
_value
- The value to convert_options
- Options to pass while converting_options.isLe
- Convert using Little Endian_options.isNegative
- Convert using two’s complement Creates a BN.js bignumber object from a hex string. null
inputs returns a BN(0)
result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the 0x
prefix) throws an error. example
hexToBn("0x123480001f"); // => BN(0x123480001f)
public static java.math.BigInteger u8aToBn(byte[] value, boolean isLe, boolean isNegative)
value
- The value to convertoptions
- Options to pass while convertingoptions.isLe
- Convert using Little Endianoptions.isNegative
- Convert using two’s complement Creates a BN from a Uint8Array object. UInt8Array
input values return the actual BN. null
or undefined
values returns an 0x0
value. example
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
public static java.math.BigInteger bnToBn(java.lang.Object value)
Creates a BN value from a BN.js bignumber or number input. null
inputs returns a 0x0
result, BN values returns the value, numnbers returns a BN representation. example
bnToBn(0x1234); // => BN(0x1234)
bnToBn(new BN(0x1234)); // => BN(0x1234)
public static java.lang.String bnToHex(java.math.BigInteger value, int bitLength)
Creates a hex value from a BN.js bignumber object. null
inputs returns a 0x
result, BN values return the actual value as a 0x
prefixed hex value. Anything that is not a BN object throws an error. With bitLength
set, it fixes the number to the specified length. example
bnToHex(new BN(0x123456)); // => '0x123456'
public static java.lang.String bnToHex(java.math.BigInteger value, boolean isLe, boolean isNegtive, int bitLength)
public static byte[] bnToU8a(java.math.BigInteger value, boolean isLe, int bitLength)
Creates a Uint8Array object from a BN. null
/undefined
/NaN
inputs returns an empty Uint8Array
result. BN
input values return the actual bytes value converted to a Uint8Array
. Optionally convert using little-endian format if isLE
is set. example
bnToU8a(new BN(0x1234)); // => [0x12, 0x34]
public static byte[] bnToU8a(java.math.BigInteger value, boolean isLe, boolean isNegative, int bitLength)
public static void main(java.lang.String[] argv)
public static byte[] toByteArrayLittleEndianUnsigned(java.math.BigInteger bi)
public static byte[] toByteArrayUnsigned(java.math.BigInteger bi)
public static <any> compactFromU8a(java.lang.Object _input, int bitLength)
Retrievs the offset and encoded length from a compact-prefixed value example
Pair<Integer, BigInteger> = compactFromU8a(new Uint8Array([254, 255, 3, 0]), 32));
System.out.printf("value offset=%d length=%d", offset, length); // 4, 0xffff
public static <any> compactFromU8a(java.lang.Object input)
public static java.lang.String u8aToString(byte[] value)
Creates a utf-8 string from a Uint8Array object. UInt8Array
input values return the actual decoded utf-8 string. null
or undefined
values returns an empty string. example
u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
public static java.lang.String u8aToHex(byte[] value, int bitLength, boolean isPrefixed)
Creates a hex string from a Uint8Array object. UInt8Array
input values return the actual hex string. null
or undefined
values returns an 0x
string. example
u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
public static java.lang.String u8aToHex(byte[] value)
public static byte[] stringToU8a(java.lang.String value)
Creates a Uint8Array object from a utf-8 string. String input values return the actual encoded UInt8Array
. null
or undefined
values returns an empty encoded array. example
stringToU8a("hello"); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
public static byte[] compactAddLength(byte[] input)
Adds a length prefix to the input value example
System.out.println(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef])
public static byte[] compactToU8a(java.lang.Object _value)
Encodes a number into a compact representation example
System.out.println(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
public static byte[] u8aConcat(java.util.List<byte[]> _list)
Creates a concatenated Uint8Array from the inputs. Concatenates the input arrays into a single UInt8Array
. example
u8aConcat(
new Uint8Array([1, 2, 3]),
new Uint8Array([4, 5, 6])
); // [1, 2, 3, 4, 5, 6]
public static byte[] u8aToU8a(java.lang.Object value)
Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input. null
ior undefined
nputs returns a []
result, Uint8Array values returns the value, hex strings returns a Uint8Array representation. example
u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
public static java.lang.String stringLowerFirst(java.lang.String value)
Lowercase the first letter of a string Lowercase the first letter of a string example
stringLowerFirst("ABC"); // => 'aBC'
public static boolean isContainer(java.lang.Object object)
public static java.math.BigInteger toBn(java.lang.Object value)
public static java.lang.String stringCamelCase(java.lang.String input)
public static byte[] u8aFixLength(byte[] value, int bitLength, boolean atStart)
Shifts a Uint8Array to a specific bitLength Returns a uint8Array with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. example
u8aFixLength("0x12", -1, false) // => 0x12
u8aFixLength("0x12", 16, false) // => 0x0012
u8aFixLength("0x1234", 8, false) // => 0x12
public static boolean u8aStrEq(byte[] u8a1, byte[] u8a2)
public static byte[] randomAsU8a()
Creates a Uint8Array filled with random bytes. Returns a Uint8Array
with the specified (optional) length filled with random bytes. example
randomAsU8a(); // => Uint8Array([...])
public static byte[] randomAsU8a(int length)
public static <any> compactStripLength(byte[] input, int bitLength)
Removes the length prefix, returning both the total length (including the value + compact encoding) and the decoded value with the correct length example
System.out.println(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
public static <any> compactStripLength(byte[] input)
public static java.lang.String toU8aString(byte[] bytes)