meterify.utils

This package provides utility functions for dapps and other packages.

randomHex

    meterify.utils.randomHex(size)

The randomHex library to generate cryptographically strong pseudo-random HEX strings from a given byte size.

Example:


    meterify.utils.randomHex(32)
    > "0xa5b9d60f32436310afebcfda832817a68921beb782fabf7915cc0460b443116a"

    meterify.utils.randomHex(4)
    > "0x6892ffc6"

    meterify.utils.randomHex(2)
    > "0x99d6"

    meterify.utils.randomHex(1)
    > "0x9a"

    meterify.utils.randomHex(0)
    > "0x"

Parameters:

  1. size - Number: The byte size for the HEX string, e.g. 32 will result in a 32 bytes HEX string with 64 characters preficed with "0x".

Returns:

String: The generated random HEX string.

BN

The BN.js library for calculating with big numbers in JavaScript. See the BN.js documentation for details.

Example:

Note: For safe conversion of many types, including BigNumber.js, use utils.toBN.

Parameters:

  1. value - String|Number: A number, number string or HEX string to convert to a BN object.

Returns:

Object: The BN.js_ instance.

isBN

Checks if a given value is a BN.js instance.

Example:

Parameters:

  1. bn - Object: An BN.js instance.

Returns:

Boolean

isBigNumber

Checks if a given value is a BigNumber.js instance.

Example:

Parameters:

  1. BigNumber - Object: A BigNumber.js instance.

Returns:

Boolean

keccak256

Will calculate the keccak256 of the input.

Example:

Note: To mimic the keccak256 behavior of Solidity, use soliditySha3.

Parameters:

  1. string - String: A string to hash.

Returns:

String: the result hash.

soliditySha3

Will calculate the sha3 of given input parameters in the same way solidity would. This means arguments will be ABI converted and tightly packed before being hashed.

Example:

Parameters:

  1. paramX - Mixed: Any type, or an object with {type: 'uint', value: '123456'} or {t: 'bytes', v: '0xfff456'}. Basic types are autodetected as follows:

    1. String non numerical UTF-8 string is interpreted as string.

    2. String|Number|BN|HEX positive number is interpreted as uint256.

    3. String|Number|BN negative number is interpreted as int256.

    4. Boolean as bool.

    5. String HEX string with leading 0x is interpreted as bytes.

    6. HEX HEX number representation is interpreted as uint256.

Returns:

String: the result hash.

isHex

Checks if a given string is a HEX string.

Example:

Parameters:

  1. hex - String|HEX: The given HEX string.

Returns:

Boolean

isHexStrict

Checks if a given string is a HEX string. Difference to meterify.utils.isHex() is that it expects HEX to be prefixed with 0x.

Example:

Parameters:

  1. hex - String|HEX: The given HEX string.

Returns:

Boolean

isAddress

Checks if a given string is a valid Meter address. It will also check the checksum, if the address has upper and lowercase letters.

Example:

Parameters:

  1. address - String: An address string.

  2. chainId - number (optional): Chain id where checksummed address should be valid, defaults to null. RSKIP-60 for details.

Returns:

Boolean

toChecksumAddress

Will convert an upper or lowercase Meter address to a checksum address.

Example:

Parameters:

  1. address - String: An address string.

  2. chainId - number (optional): Chain id where checksummed address should be valid, defaults to null. RSKIP-60 for details.

Returns:

String: The checksum address.

stripHexPrefix

Removes the prefix 0x from a given hex if it exists.

Example:

Parameters:

  1. hex - String: Hex

Returns:

String: Hex without prefix.

checkAddressChecksum

Checks the checksum of a given address. Will also return false on non-checksum addresses.

Example:

Parameters:

  1. address - String: An address string.

  2. chainId - number (optional): Chain id where checksummed address should be valid, defaults to null. RSKIP-60 for details.

Returns:

Boolean: true when the checksum of the address is valid, false if its not a checksum address, or the checksum is invalid.

toHex

Will auto convert any given value to HEX. Number strings will interpreted as numbers. Text strings will be interpreted as UTF-8 strings.

Example:

Parameters:

  1. value - String|Number|BN|BigNumber: The input to convert to HEX.

Returns:

String: The resulting HEX string.

toBN

Will safely convert any given value (including BigNumber.js instances) into a BN.js instance, for handling big numbers in JavaScript.

Example:

Note: For just the BN.js class use utils.BN.

Parameters:

  1. number - String|Number|HEX: Number to convert to a big number.

Returns:

Object: The BN.js instance.

hexToNumberString

Returns the number representation of a given HEX value as a string.

Example:

Parameters:

  1. hexString - String|HEX: A string to hash.

Returns:

String: The number as a string.

hexToNumber

Returns the number representation of a given HEX value.

Example:

Note: This is not useful for big numbers, use utils.toBN instead.

Parameters:

  1. hexString - String|HEX: A string to hash.

Returns:

Number

numberToHex

Returns the HEX representation of a given number value.

Example:

Parameters:

  1. number - String|Number|BN|BigNumber: A number as string or number.

Returns:

String: The HEX value of the given number.

hexToUtf8

Returns the UTF-8 string representation of a given HEX value.

Example:

Parameters:

  1. hex - String: A HEX string to convert to a UTF-8 string.

Returns:

String: The UTF-8 string.

hexToAscii

Returns the ASCII string representation of a given HEX value.

Example:

Parameters:

  1. hex - String: A HEX string to convert to a ASCII string.

Returns:

String: The ASCII string.

utf8ToHex

Returns the HEX representation of a given UTF-8 string.

Example:

Parameters:

  1. string - String: A UTF-8 string to convert to a HEX string.

Returns:

String: The HEX string.

asciiToHex

Returns the HEX representation of a given ASCII string. If you would like to transform an ASCII string into a valid bytes4, bytes8 etc. value then please pass the correct length as the second parameter.

Example:

Parameters:

  1. string - String: A ASCII string to convert to a HEX string.

  2. length - Number: The length of the returned hex string. The default size is 32 e.g.: bytes32.

Returns:

String: The HEX string.

hexToBytes

Returns a byte array from the given HEX string.

Example:

Parameters:

hex - String|HEX: A HEX to convert.

Returns:

Array: The byte array.

bytesToHex

Returns a HEX string from a byte array.

Example:

Parameters:

byteArray - Array: A byte array to convert.

Returns:

String: The HEX string.

toWei

Converts a Meter value into wei.

Example:

Note: wei are the smallest conversion unit, and you should always make calculations in wei and convert only for display reasons.

Parameters:

  1. number - String|BN: The value.

  2. unit - String (optional, defaults to "ether"): The ether to convert from. Possible units are:

    1. noether: '0'

    2. wei: '1'

    3. kwei: '1000'

    4. Kwei: '1000'

    5. babbage: '1000'

    6. femtoether: '1000'

    7. mwei: '1000000'

    8. Mwei: '1000000'

    9. lovelace: '1000000'

    10. picoether: '1000000'

    11. gwei: '1000000000'

    12. Gwei: '1000000000'

    13. shannon: '1000000000'

    14. nanoether: '1000000000'

    15. nano: '1000000000'

    16. szabo: '1000000000000'

    17. microether: '1000000000000'

    18. micro: '1000000000000'

    19. finney: '1000000000000000'

    20. milliether: '1000000000000000'

    21. milli: '1000000000000000'

    22. ether: '1000000000000000000'

    23. kether: '1000000000000000000000'

    24. grand: '1000000000000000000000'

    25. mether: '1000000000000000000000000'

    26. gether: '1000000000000000000000000000'

    27. tether: '1000000000000000000000000000000'

Returns:

String|BN: If a string is given it returns a number string, otherwise a BN.js instance.

fromWei

Converts any wei value into another value.

Example:

Note: wei are the smallest conversion unit, and you should always make calculations in wei and convert only for display reasons.

Parameters:

  1. number - String|BN: The value in wei.

  2. unit - String (optional, defaults to "ether"): The ether to convert to. Possible units are:

    • noether: '0'

    • wei: '1'

    • kwei: '1000'

    • Kwei: '1000'

    • babbage: '1000'

    • femtoether: '1000'

    • mwei: '1000000'

    • Mwei: '1000000'

    • lovelace: '1000000'

    • picoether: '1000000'

    • gwei: '1000000000'

    • Gwei: '1000000000'

    • shannon: '1000000000'

    • nanoether: '1000000000'

    • nano: '1000000000'

    • szabo: '1000000000000'

    • microether: '1000000000000'

    • micro: '1000000000000'

    • finney: '1000000000000000'

    • milliether: '1000000000000000'

    • milli: '1000000000000000'

    • ether: '1000000000000000000'

    • kether: '1000000000000000000000'

    • grand: '1000000000000000000000'

    • mether: '1000000000000000000000000'

    • gether: '1000000000000000000000000000'

    • tether: '1000000000000000000000000000000'

Returns:

String: It always returns a string number.

unitMap

Shows all possible values and their amount in wei.

Example:

Return value:

  • Object with the following properties:

    • noether: '0'

    • wei: '1'

    • kwei: '1000'

    • Kwei: '1000'

    • babbage: '1000'

    • femtoether: '1000'

    • mwei: '1000000'

    • Mwei: '1000000'

    • lovelace: '1000000'

    • picoether: '1000000'

    • gwei: '1000000000'

    • Gwei: '1000000000'

    • shannon: '1000000000'

    • nanoether: '1000000000'

    • nano: '1000000000'

    • szabo: '1000000000000'

    • microether: '1000000000000'

    • micro: '1000000000000'

    • finney: '1000000000000000'

    • milliether: '1000000000000000'

    • milli: '1000000000000000'

    • ether: '1000000000000000000'

    • kether: '1000000000000000000000'

    • grand: '1000000000000000000000'

    • mether: '1000000000000000000000000'

    • gether: '1000000000000000000000000000'

    • tether: '1000000000000000000000000000000'

padLeft

Adds a padding on the left of a string. Useful for adding paddings to HEX strings.

Example:

Parameters:

  1. string - String: The string to add padding on the left.

  2. characterAmount - Number: The number of characters the total string should have.

  3. sign - String (optional): The character sign to use, defaults to "0".

Returns:

String: The padded string.

padRight

Adds a padding on the right of a string, Useful for adding paddings to HEX strings.

Example:

Parameters:

  1. string - String: The string to add padding on the right.

  2. characterAmount - Number: The number of characters the total string should have.

  3. sign - String (optional): The character sign to use, defaults to "0".

Returns:

String: The padded string.

toTwosComplement

Converts a negative number into a two's complement.

Example:

Parameters:

  1. number - Number|String|BigNumber: The number to convert.

Returns:

String: The converted hex string.

getSignatureParameters

Gets the r, s and v values of an ECDSA signature

Example

Parameters:

  1. string - String: An ECDSA signature.

Returns:

Object: Object containing r,s,v values.

Last updated

Was this helpful?