Class Utils


  • public class Utils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Utils()  
    • Method Summary

      Modifier and Type Method 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** ```java System.out.println(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef]) ```
      static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​java.math.BigInteger> compactFromU8a​(java.lang.Object input)  
      static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​java.math.BigInteger> compactFromU8a​(java.lang.Object _input, int bitLength)
      Retrievs the offset and encoded length from a compact-prefixed value **example** ```java Pair = compactFromU8a(new Uint8Array([254, 255, 3, 0]), 32)); System.out.printf("value offset=%d length=%d", offset, length); // 4, 0xffff ```
      static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​byte[]> compactStripLength​(byte[] input)  
      static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​byte[]> 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** ```java System.out.println(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]] ```
      static byte[] compactToU8a​(java.lang.Object _value)
      Encodes a number into a compact representation **example** ```java System.out.println(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111]) ```
      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** ```java stringLowerFirst("ABC"); // => 'aBC' ```
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Utils

        public Utils()
    • Method Detail

      • isHex

        public static boolean isHex​(java.lang.Object value)
      • isHex

        public static boolean isHex​(java.lang.Object value,
                                    int bitLength,
                                    boolean ignoreLength)
      • hexToU8a

        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** ```java hexToU8a("0x80001f"); // Uint8Array([0x80, 0x00, 0x1f]) hexToU8a("0x80001f", 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f]) ```
      • hexToU8a

        public static byte[] hexToU8a​(java.lang.String value)
      • hexStripPrefix

        public static java.lang.String hexStripPrefix​(java.lang.String value)
      • hexHasPrefix

        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** ```java System.out.println(hexHasPrefix("0x1234")); // => true ```
      • isU8a

        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** ```java System.out.println(isU8a([])); // => false ```
      • hexToBn

        public static java.math.BigInteger hexToBn​(java.lang.Object value,
                                                   boolean isLe,
                                                   boolean isNegative)
        Parameters:
        _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** ```java hexToBn("0x123480001f"); // => BN(0x123480001f) ```
      • u8aToBn

        public static java.math.BigInteger u8aToBn​(byte[] value,
                                                   boolean isLe,
                                                   boolean isNegative)
        Parameters:
        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 from a Uint8Array object. `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value. **example** ```java u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f ```
      • bnToBn

        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** ```java bnToBn(0x1234); // => BN(0x1234) bnToBn(new BN(0x1234)); // => BN(0x1234) ```
      • bnToHex

        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** ```java bnToHex(new BN(0x123456)); // => '0x123456' ```
      • bnToHex

        public static java.lang.String bnToHex​(java.math.BigInteger value,
                                               boolean isLe,
                                               boolean isNegtive,
                                               int bitLength)
      • bnToU8a

        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** ```java bnToU8a(new BN(0x1234)); // => [0x12, 0x34] ```
      • bnToU8a

        public static byte[] bnToU8a​(java.math.BigInteger value,
                                     boolean isLe,
                                     boolean isNegative,
                                     int bitLength)
      • main

        public static void main​(java.lang.String[] argv)
      • toByteArrayLittleEndianUnsigned

        public static byte[] toByteArrayLittleEndianUnsigned​(java.math.BigInteger bi)
      • toByteArrayUnsigned

        public static byte[] toByteArrayUnsigned​(java.math.BigInteger bi)
      • compactFromU8a

        public static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​java.math.BigInteger> compactFromU8a​(java.lang.Object _input,
                                                                                                                       int bitLength)
        Retrievs the offset and encoded length from a compact-prefixed value **example** ```java Pair = compactFromU8a(new Uint8Array([254, 255, 3, 0]), 32)); System.out.printf("value offset=%d length=%d", offset, length); // 4, 0xffff ```
      • compactFromU8a

        public static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​java.math.BigInteger> compactFromU8a​(java.lang.Object input)
      • u8aToString

        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** ```java u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello ```
      • u8aToHex

        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** ```java u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f ```
      • u8aToHex

        public static java.lang.String u8aToHex​(byte[] value)
      • stringToU8a

        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** ```java stringToU8a("hello"); // [0x68, 0x65, 0x6c, 0x6c, 0x6f] ```
      • compactAddLength

        public static byte[] compactAddLength​(byte[] input)
        Adds a length prefix to the input value **example** ```java System.out.println(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef]) ```
      • compactToU8a

        public static byte[] compactToU8a​(java.lang.Object _value)
        Encodes a number into a compact representation **example** ```java System.out.println(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111]) ```
      • u8aConcat

        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** ```java u8aConcat( new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]) ); // [1, 2, 3, 4, 5, 6] ```
      • u8aToU8a

        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** ```java u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34]) u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34]) ```
      • stringLowerFirst

        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** ```java stringLowerFirst("ABC"); // => 'aBC' ```
      • isContainer

        public static boolean isContainer​(java.lang.Object object)
      • toBn

        public static java.math.BigInteger toBn​(java.lang.Object value)
      • stringCamelCase

        public static java.lang.String stringCamelCase​(java.lang.String input)
      • u8aFixLength

        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** ```java u8aFixLength("0x12", -1, false) // => 0x12 u8aFixLength("0x12", 16, false) // => 0x0012 u8aFixLength("0x1234", 8, false) // => 0x12 ```
      • u8aStrEq

        public static boolean u8aStrEq​(byte[] u8a1,
                                       byte[] u8a2)
      • randomAsU8a

        public static byte[] randomAsU8a()
        Creates a Uint8Array filled with random bytes. Returns a `Uint8Array` with the specified (optional) length filled with random bytes. **example** ```java randomAsU8a(); // => Uint8Array([...]) ```
      • randomAsU8a

        public static byte[] randomAsU8a​(int length)
      • compactStripLength

        public static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​byte[]> 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** ```java System.out.println(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]] ```
      • compactStripLength

        public static org.apache.commons.lang3.tuple.Pair<java.lang.Integer,​byte[]> compactStripLength​(byte[] input)
      • toU8aString

        public static java.lang.String toU8aString​(byte[] bytes)