Meter Docs
Search…
meterify.utils
This package provides utility functions for dapps and other packages.

randomHex

1
meterify.utils.randomHex(size)
Copied!
The randomHex library to generate cryptographically strong pseudo-random HEX strings from a given byte size.
Example:
1
2
meterify.utils.randomHex(32)
3
> "0xa5b9d60f32436310afebcfda832817a68921beb782fabf7915cc0460b443116a"
4
5
meterify.utils.randomHex(4)
6
> "0x6892ffc6"
7
8
meterify.utils.randomHex(2)
9
> "0x99d6"
10
11
meterify.utils.randomHex(1)
12
> "0x9a"
13
14
meterify.utils.randomHex(0)
15
> "0x"
Copied!
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

1
meterify.utils.BN(mixed)
Copied!
The BN.js library for calculating with big numbers in JavaScript. See the BN.js documentation for details.
Example:
1
2
const BN = meterify.utils.BN;
3
4
new BN(1234).toString();
5
> "1234"
6
7
new BN('1234').add(new BN('1')).toString();
8
> "1235"
9
10
new BN('0xea').toString();
11
> "234"
Copied!
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

1
2
meterify.utils.isBN(bn)
Copied!
Checks if a given value is a BN.js instance.
Example:
1
2
const number = new BN(10);
3
4
meterify.utils.isBN(number);
5
> true
Copied!
Parameters:
    1.
    bn - Object: An BN.js instance.
Returns:
Boolean

isBigNumber

1
meterify.utils.isBigNumber(bignumber)
Copied!
Checks if a given value is a BigNumber.js instance.
Example:
1
2
const number = new BigNumber(10);
3
4
meterify.utils.isBigNumber(number);
5
> true
Copied!
Parameters:
    1.
    BigNumber - Object: A BigNumber.js instance.
Returns:
Boolean

keccak256

1
2
meterify.utils.keccak256(string)
3
meterify.utils.sha3(string) // ALIAS
Copied!
Will calculate the keccak256 of the input.
Example:
1
2
meterify.utils.keccak256('234'); // taken as string
3
> "0xc1912fee45d61c87cc5ea59dae311904cd86b84fee17cc96966216f811ce6a79"
4
5
meterify.utils.keccak256(new BN('234'));
6
> "0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a"
7
8
meterify.utils.keccak256(234);
9
> null // can't calculate the hash of a number
10
11
meterify.utils.keccak256(0xea); // same as above, just the HEX representation of the number
12
> null
13
14
meterify.utils.keccak256('0xea'); // will be converted to a byte array first, and then hashed
15
> "0x2f20677459120677484f7104c76deb6846a2c071f9b3152c103bb12cd54d1a4a"
Copied!
Note: To mimic the keccak256 behavior of Solidity, use soliditySha3.
Parameters:
    1.
    string - String: A string to hash.
Returns:
String: the result hash.

soliditySha3

1
meterify.utils.soliditySha3(param1 [, param2, ...])
Copied!
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:
1
meterify.utils.soliditySha3('234564535', '0xfff23243', true, -10);
2
// auto detects: uint256, bytes, bool, int256
3
> "0x3e27a893dc40ef8a7f0841d96639de2f58a132be5ae466d40087a2cfa83b7179"
4
5
meterify.utils.soliditySha3('Hello!%'); // auto detects: string
6
> "0x661136a4267dba9ccdf6bfddb7c00e714de936674c4bdb065a531cf1cb15c7fc"
7
8
meterify.utils.soliditySha3('234'); // auto detects: uint256
9
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
10
11
meterify.utils.soliditySha3(0xea); // same as above
12
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
13
14
meterify.utils.soliditySha3(new BN('234')); // same as above
15
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
16
17
meterify.utils.soliditySha3({type: 'uint256', value: '234'})); // same as above
18
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
19
20
meterify.utils.soliditySha3({t: 'uint', v: new BN('234')})); // same as above
21
> "0x61c831beab28d67d1bb40b5ae1a11e2757fa842f031a2d0bc94a7867bc5d26c2"
22
23
meterify.utils.soliditySha3('0x407D73d8a49eeb85D32Cf465507dd71d507100c1');
24
> "0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b"
25
26
meterify.utils.soliditySha3({t: 'bytes', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'});
27
> "0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b" // same result as above
28
29
meterify.utils.soliditySha3({t: 'address', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'});
30
> "0x4e8ebbefa452077428f93c9520d3edd60594ff452a29ac7d2ccc11d47f3ab95b" // same as above, but will do a checksum check, if its multi case
31
32
meterify.utils.soliditySha3({t: 'bytes32', v: '0x407D73d8a49eeb85D32Cf465507dd71d507100c1'});
33
> "0x3c69a194aaf415ba5d6afca734660d0a3d45acdc05d54cd1ca89a8988e7625b4" // different result as above
34
35
meterify.utils.soliditySha3({t: 'string', v: 'Hello!%'}, {t: 'int8', v:-23}, {t: 'address', v: '0x85F43D8a49eeB85d32Cf465507DD71d507100C1d'});
36
> "0xa13b31627c1ed7aaded5aecec71baf02fe123797fffd45e662eac8e06fbe4955"
Copied!
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

1
meterify.utils.isHex(hex)
Copied!
Checks if a given string is a HEX string.
Example:
1
meterify.utils.isHex('0xc1912');
2
> true
3
4
meterify.utils.isHex(0xc1912);
5
> true
6
7
meterify.utils.isHex('c1912');
8
> true
9
10
meterify.utils.isHex(345);
11
> true // this is tricky, as 345 can be a a HEX representation or a number, be careful when not having a 0x in front!
12
13
meterify.utils.isHex('0xZ1912');
14
> false
15
16
meterify.utils.isHex('Hello');
17
> false
Copied!
Parameters:
    1.
    hex - String|HEX: The given HEX string.
Returns:
Boolean

isHexStrict

1
meterify.utils.isHexStrict(hex)
Copied!
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:
1
meterify.utils.isHexStrict('0xc1912');
2
> true
3
4
meterify.utils.isHexStrict(0xc1912);
5
> false
6
7
meterify.utils.isHexStrict('c1912');
8
> false
9
10
meterify.utils.isHexStrict(345);
11
> false // this is tricky, as 345 can be a a HEX representation or a number, be careful when not having a 0x in front!
12
13
meterify.utils.isHexStrict('0xZ1912');
14
> false
15
16
meterify.utils.isHex('Hello');
17
> false
Copied!
Parameters:
    1.
    hex - String|HEX: The given HEX string.
Returns:
Boolean

isAddress

1
meterify.utils.isAddress(address, [, chainId])
Copied!
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:
1
meterify.utils.isAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d');
2
> true
3
4
meterify.utils.isAddress('c1912fee45d61c87cc5ea59dae31190fffff232d');
5
> true
6
7
meterify.utils.isAddress('0XC1912FEE45D61C87CC5EA59DAE31190FFFFF232D');
8
> true // as all is uppercase, no checksum will be checked
9
10
meterify.utils.isAddress('0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d');
11
> true
12
13
meterify.utils.isAddress('0xC1912fEE45d61C87Cc5EA59DaE31190FFFFf232d');
14
> false // wrong checksum
15
16
meterify.utils.isAddress('0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD', 30);
17
> true
Copied!
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

1
meterify.utils.toChecksumAddress(address[, chainId])
Copied!
Will convert an upper or lowercase Meter address to a checksum address.
Example:
1
2
meterify.utils.toChecksumAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d');
3
> "0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d"
4
5
meterify.utils.toChecksumAddress('0XC1912FEE45D61C87CC5EA59DAE31190FFFFF232D');
6
> "0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d" // same as above
7
8
meterify.utils.toChecksumAddress('0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed', 30);
9
> "0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD"
Copied!
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

1
meterify.utils.stripHexPrefix(address)
Copied!
Removes the prefix 0x from a given hex if it exists.
Example:
1
meterify.utils.stripHexPrefix('0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d');
2
> "c1912fEE45d61C87Cc5EA59DaE31190FFFFf232d"
Copied!
Parameters:
    1.
    hex - String: Hex
Returns:
String: Hex without prefix.

checkAddressChecksum

1
2
meterify.utils.checkAddressChecksum(address [, chainId])
Copied!
Checks the checksum of a given address. Will also return false on non-checksum addresses.
Example:
1
2
meterify.utils.checkAddressChecksum('0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d');
3
> true
4
5
meterify.utils.checkAddressChecksum('0x5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd', 31);
6
> true
Copied!
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

1
2
meterify.utils.toHex(mixed)
Copied!
Will auto convert any given value to HEX. Number strings will interpreted as numbers. Text strings will be interpreted as UTF-8 strings.
Example:
1
2
meterify.utils.toHex('234');
3
> "0xea"
4
5
meterify.utils.toHex(234);
6
> "0xea"
7
8
meterify.utils.toHex(new BN('234'));
9
> "0xea"
10
11
meterify.utils.toHex(new BigNumber('234'));
12
> "0xea"
13
14
meterify.utils.toHex('I have 100€');
15
> "0x49206861766520313030e282ac"
Copied!
Parameters:
    1.
    value - String|Number|BN|BigNumber: The input to convert to HEX.
Returns:
String: The resulting HEX string.

toBN

1
meterify.utils.toBN(number)
Copied!
Will safely convert any given value (including BigNumber.js instances) into a BN.js instance, for handling big numbers in JavaScript.
Example:
1
2
meterify.utils.toBN(1234).toString();
3
> "1234"
4
5
meterify.utils.toBN('1234').add(meterify.utils.toBN('1')).toString();
6
> "1235"
7
8
meterify.utils.toBN('0xea').toString();
9
> "234"
Copied!
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

1
meterify.utils.hexToNumberString(hex)
Copied!
Returns the number representation of a given HEX value as a string.
Example:
1
meterify.utils.hexToNumberString('0xea');
2
> "234"
Copied!
Parameters:
    1.
    hexString - String|HEX: A string to hash.
Returns:
String: The number as a string.

hexToNumber

1
2
meterify.utils.hexToNumber(hex)
3
meterify.utils.toDecimal(hex) // ALIAS, deprecated
Copied!
Returns the number representation of a given HEX value.
Example:
1
2
meterify.utils.hexToNumber('0xea');
3
> 234
Copied!
Note: This is not useful for big numbers, use utils.toBN instead.
Parameters:
    1.
    hexString - String|HEX: A string to hash.
Returns:
Number

numberToHex

1
2
meterify.utils.numberToHex(number)
3
meterify.utils.fromDecimal(number) // ALIAS, deprecated
Copied!
Returns the HEX representation of a given number value.
Example:
1
2
meterify.utils.numberToHex('234');
3
> '0xea'
Copied!
Parameters:
    1.
    number - String|Number|BN|BigNumber: A number as string or number.
Returns:
String: The HEX value of the given number.

hexToUtf8

1
2
meterify.utils.hexToUtf8(hex)
3
meterify.utils.hexToString(hex) // ALIAS
4
meterify.utils.toUtf8(hex) // ALIAS, deprecated
Copied!
Returns the UTF-8 string representation of a given HEX value.
Example:
1
2
meterify.utils.hexToUtf8('0x49206861766520313030e282ac');
3
> "I have 100€"
Copied!
Parameters:
    1.
    hex - String: A HEX string to convert to a UTF-8 string.
Returns:
String: The UTF-8 string.

hexToAscii

1
2
meterify.utils.hexToAscii(hex)
3
meterify.utils.toAscii(hex) // ALIAS, deprecated
Copied!
Returns the ASCII string representation of a given HEX value.
Example:
1
2
meterify.utils.hexToAscii('0x4920686176652031303021');
3
> "I have 100!"
Copied!
Parameters:
    1.
    hex - String: A HEX string to convert to a ASCII string.
Returns:
String: The ASCII string.

utf8ToHex

1
meterify.utils.utf8ToHex(string)
2
meterify.utils.stringToHex(string) // ALIAS
3
meterify.utils.fromUtf8(string) // ALIAS, deprecated
Copied!
Returns the HEX representation of a given UTF-8 string.
Example:
1
meterify.utils.utf8ToHex('I have 100€');
2
> "0x49206861766520313030e282ac"
Copied!
Parameters:
    1.
    string - String: A UTF-8 string to convert to a HEX string.
Returns:
String: The HEX string.

asciiToHex

1
meterify.utils.asciiToHex(string)
2
meterify.utils.fromAscii(string) // ALIAS, deprecated
Copied!
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:
1
meterify.utils.asciiToHex('I have 100!');
2
> "0x4920686176652031303021000000000000000000000000000000000000000000"
3
4
// transforming to a bytes4 value:
5
meterify.utils.asciiToHex('yes', 4);
6
7
// transforming to a bytes8 value:
8
meterify.utils.asciiToHex('yes', 8);
9
10
//etc.
Copied!
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

1
meterify.utils.hexToBytes(hex)
Copied!
Returns a byte array from the given HEX string.
Example:
1
meterify.utils.hexToBytes('0x000000ea');
2
> [ 0, 0, 0, 234 ]
3
4
meterify.utils.hexToBytes(0x000000ea);
5
> [ 234 ]
Copied!
Parameters:
hex - String|HEX: A HEX to convert.
Returns:
Array: The byte array.

bytesToHex

1
meterify.utils.bytesToHex(byteArray)
Copied!
Returns a HEX string from a byte array.
Example:
1
meterify.utils.bytesToHex([ 72, 101, 108, 108, 111, 33, 36 ]);
2
> "0x48656c6c6f2125"
Copied!
Parameters:
byteArray - Array: A byte array to convert.
Returns:
String: The HEX string.

toWei

1
meterify.utils.toWei(number [, unit])
Copied!
Converts a Meter value into wei.
Example:
1
meterify.utils.toWei('1', 'ether');
2
> "1000000000000000000"
3
4
meterify.utils.toWei('1', 'finney');
5
> "1000000000000000"
6
7
meterify.utils.toWei('1', 'szabo');
8
> "1000000000000"
9
10
meterify.utils.toWei('1', 'shannon');
11
> "1000000000"
Copied!
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

1
meterify.utils.fromWei(number [, unit])
Copied!
Converts any wei value into another value.
Example:
1
2
meterify.utils.fromWei('1', 'ether');
3
> "0.000000000000000001"
4
5
meterify.utils.fromWei('1', 'finney');
6
> "0.000000000000001"
7
8
meterify.utils.fromWei('1', 'szabo');
9
> "0.000000000001"
10
11
meterify.utils.fromWei('1', 'shannon');
12
> "0.000000001"
Copied!
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

1
2
meterify.utils.unitMap
Copied!
Shows all possible values and their amount in wei.
Example:
1
2
meterify.utils.unitMap
3