Utils
This package provides utility functions for Ethereum dapps and other web3.js packages.
For using Utils functions, first install Web3 package using npm i web3
or yarn add web3
.
After that, Web3 Utils functions will be available as mentioned below.
import { Web3 } from 'web3';
const web3 = new Web3();
const value = web3.utils.fromWei("1", "ether")
For using individual package install web3-utils
package using npm i web3-utils
or yarn add web3-utils
and only import required functions.
This is more efficient approach for building lightweight applications.
import { fromWei, soliditySha3Raw } from 'web3-utils';
console.log(fromWei("1", "ether"));
console.log(soliditySha3Raw({ type: "string", value: "helloworld" }))
Functions
asciiToHex
▸ asciiToHex(str
): string
Should be called to get hex representation (prefixed by 0x) of ascii string
Parameters
Name | Type | Description |
---|---|---|
str | string | String to be converted to hex |
Returns
string
- Hex string
Example
console.log(web3.utils.asciiToHex('Hello World'));
> 0x48656c6c6f20576f726c64
Defined in
web3-utils/src/converters.ts:292
bytesToHex
▸ bytesToHex(bytes
): string
Convert a byte array to a hex string
Parameters
Name | Type | Description |
---|---|---|
bytes | Bytes | Byte array to be converted |
Returns
string
- The hex string representation of the input byte array
Example
console.log(web3.utils.bytesToHex(new Uint8Array([72, 12])));
> "0x480c"
#### Defined in
[web3-utils/src/converters.ts:126](https://github.com/web3/web3.js/blob/557132b/packages/web3-utils/src/converters.ts#L126)
___
### bytesToUint8Array
▸ **bytesToUint8Array**(`data`): `Uint8Array`
Convert a value from bytes to Uint8Array
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `data` | `Bytes` | Data to be converted |
#### Returns
`Uint8Array`
- The Uint8Array representation of the input data
**`Example`**
```ts
console.log(web3.utils.bytesToUint8Array("0xab")));
> Uint8Array(1) [ 171 ]
Defined in
web3-utils/src/converters.ts:92
checkAddressCheckSum
▸ checkAddressCheckSum(data
): boolean
Checks the checksum of a given address. Will also return false on non-checksum addresses.
Parameters
Name | Type |
---|---|
data | string |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/address.d.ts:5
compareBlockNumbers
▸ compareBlockNumbers(blockA
, blockB
): 0
| 1
| -1
Compares between block A and block B
Parameters
Name | Type | Description |
---|---|---|
blockA | BlockNumberOrTag | Block number or string |
blockB | BlockNumberOrTag | Block number or string |
Returns
0
| 1
| -1
- Returns -1 if a < b, returns 1 if a > b and returns 0 if a == b
Example
console.log(web3.utils.compareBlockNumbers('latest', 'pending'));
> -1
console.log(web3.utils.compareBlockNumbers(12, 11));
> 1
Defined in
web3-utils/src/validation.ts:127
encodePacked
▸ encodePacked(...values
): string
Encode packed arguments to a hexstring
Parameters
Name | Type |
---|---|
...values | Sha3Input [] |
Returns
string
Defined in
fromAscii
▸ fromAscii(str
): string
Parameters
Name | Type |
---|---|
str | string |
Returns
string
Alias
asciiToHex
Defined in
web3-utils/src/converters.ts:292
fromDecimal
▸ fromDecimal(value
, hexstrict?
): string
Converts value to it's hex representation
Parameters
Name | Type |
---|---|
value | Numbers |
hexstrict? | boolean |
Returns
string
Alias
numberToHex
Defined in
web3-utils/src/converters.ts:183
fromTwosComplement
▸ fromTwosComplement(value
, nibbleWidth?
): number
| bigint
Converts the twos complement into a decimal number or big int.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
value | Numbers | undefined | The value to be converted. |
nibbleWidth | number | 64 | The nibble width of the hex string (default is 64). |
Returns
number
| bigint
The decimal number or big int.
Example
console.log(web3.utils.fromTwosComplement('0x0000000000000000000000000000000d', 32'));
> 13
console.log(web3.utils.fromTwosComplement('0x00000000000000000020000000000000', 32));
> 9007199254740992n
Defined in
web3-utils/src/string_manipulation.ts:148
fromUtf8
▸ fromUtf8(str
): string
Parameters
Name | Type |
---|---|
str | string |
Returns
string
Alias
utf8ToHex
Defined in
web3-utils/src/converters.ts:228
fromWei
▸ fromWei(number
, unit
): string
Takes a number of wei and converts it to any other ether unit.
Parameters
Name | Type | Description |
---|---|---|
number | Numbers | The value in wei |
unit | "noether" | "wei" | "kwei" | "Kwei" | "babbage" | "femtoether" | "mwei" | "Mwei" | "lovelace" | "picoether" | "gwei" | "Gwei" | "shannon" | "nanoether" | "nano" | "szabo" | "microether" | "micro" | "finney" | "milliether" | "milli" | "ether" | "kether" | "grand" | "mether" | "gether" | "tether" | The unit to convert to |
Returns
string
- Returns the converted value in the given unit
Example
console.log(web3.utils.fromWei("1", "ether"));
> 0.000000000000000001
console.log(web3.utils.fromWei("1", "shannon"));
> 0.000000001
Defined in
web3-utils/src/converters.ts:481
getStorageSlotNumForLongString
▸ getStorageSlotNumForLongString(mainSlotNumber
): undefined
| string
Get slot number for storage long string in contract. Basically for getStorage method returns slotNumber where will data placed
Parameters
Name | Type | Description |
---|---|---|
mainSlotNumber | string | number | the slot number where will be stored hash of long string |
Returns
undefined
| string
- the slot number where will be stored long string
Defined in
hexToAscii
▸ hexToAscii(str
): string
Should be called to get ascii from it's hex representation
Parameters
Name | Type | Description |
---|---|---|
str | string | Hex string to be converted to ascii |
Returns
string
- Ascii string
Example
console.log(web3.utils.hexToAscii('0x48656c6c6f20576f726c64'));
> Hello World
Defined in
web3-utils/src/converters.ts:319
hexToBytes
▸ hexToBytes(bytes
): Uint8Array
Convert a hex string to a byte array
Parameters
Name | Type |
---|---|
bytes | string |
Returns
Uint8Array
- The byte array representation of the input hex string
Example
console.log(web3.utils.hexToBytes('0x74657374'));
> Uint8Array(4) [ 116, 101, 115, 116 ]
Defined in
web3-utils/src/converters.ts:140
hexToNumber
▸ hexToNumber(value
): number
| bigint
Converts value to it's number representation
Parameters
Name | Type | Description |
---|---|---|
value | string | Hex string to be converted |
Returns
number
| bigint
- The number representation of the input value
Example
conoslle.log(web3.utils.hexToNumber('0xa'));
> 10
Defined in
web3-utils/src/converters.ts:158
hexToNumberString
▸ hexToNumberString(data
): string
Converts value to it's decimal representation in string
Parameters
Name | Type |
---|---|
data | string |
Returns
string
- The decimal representation of the input value
Example
console.log(web3.utils.hexToNumberString('0xa'));
> "10"
Defined in
web3-utils/src/converters.ts:214
hexToString
▸ hexToString(str
): string
Parameters
Name | Type |
---|---|
str | string |
Returns
string
Alias
hexToUtf8
Defined in
web3-utils/src/converters.ts:261
hexToUtf8
▸ hexToUtf8(str
): string
Should be called to get utf8 from it's hex representation
Parameters
Name | Type | Description |
---|---|---|
str | string | Hex string to be converted |
Returns
string
- Utf8 string
Example
console.log(web3.utils.hexToUtf8('0x48656c6c6f20576f726c64'));
> Hello World
Defined in
web3-utils/src/converters.ts:261
isAddress
▸ isAddress(value
, checkChecksum?
): boolean
Checks if a given string is a valid Ethereum address. It will also check the checksum, if the address has upper and lowercase letters.
Parameters
Name | Type |
---|---|
value | ValidInputTypes |
checkChecksum? | boolean |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/address.d.ts:9
isBloom
▸ isBloom(bloom
): boolean
Returns true if the bloom is a valid bloom https://github.com/joshstevens19/ethereum-bloom-filters/blob/fbeb47b70b46243c3963fe1c2988d7461ef17236/src/index.ts#L7
Parameters
Name | Type |
---|---|
bloom | ValidInputTypes |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/bloom.d.ts:6
isContractAddressInBloom
▸ isContractAddressInBloom(bloom
, contractAddress
): boolean
Returns true if the contract address is part of the given bloom. note: false positives are possible.
Parameters
Name | Type |
---|---|
bloom | string |
contractAddress | string |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/bloom.d.ts:20
isHex
▸ isHex(hex
): boolean
returns true if input is a hexstring, number or bigint
Parameters
Name | Type |
---|---|
hex | ValidInputTypes |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/string.d.ts:15
isHexStrict
▸ isHexStrict(hex
): boolean
Parameters
Name | Type |
---|---|
hex | ValidInputTypes |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/string.d.ts:6
isInBloom
▸ isInBloom(bloom
, value
): boolean
Returns true if the value is part of the given bloom note: false positives are possible.
Parameters
Name | Type |
---|---|
bloom | string |
value | string | Uint8Array |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/bloom.d.ts:11
isTopic
▸ isTopic(topic
): boolean
Checks if its a valid topic
Parameters
Name | Type |
---|---|
topic | string |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/topic.d.ts:4
isTopicInBloom
▸ isTopicInBloom(bloom
, topic
): boolean
Returns true if the topic is part of the given bloom. note: false positives are possible.
Parameters
Name | Type |
---|---|
bloom | string |
topic | string |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/topic.d.ts:9
isUserEthereumAddressInBloom
▸ isUserEthereumAddressInBloom(bloom
, ethereumAddress
): boolean
Returns true if the ethereum users address is part of the given bloom note: false positives are possible.
Parameters
Name | Type |
---|---|
bloom | string |
ethereumAddress | string |
Returns
boolean
Deprecated
Will be removed in next release. Please use web3-validator
package instead.
Defined in
web3-validator/lib/commonjs/validation/bloom.d.ts:15
keccak256Wrapper
▸ keccak256Wrapper(data
): string
A wrapper for ethereum-cryptography/keccak256 to allow hashing a string
and a bigint
in addition to UInt8Array
Parameters
Name | Type | Description |
---|---|---|
data | string | number | bigint | Uint8Array | readonly number [] | the input to hash |
Returns
string
- the Keccak-256 hash of the input
Example
console.log(web3.utils.keccak256Wrapper('web3.js'));
> 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a
console.log(web3.utils.keccak256Wrapper(1));
> 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6
console.log(web3.utils.keccak256Wrapper(0xaf12fd));
> 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c
Defined in
leftPad
▸ leftPad(value
, characterAmount
, sign?
): string
Adds a padding on the left of a string, if value is a integer or bigInt will be converted to a hex string.
Parameters
Name | Type | Default value |
---|---|---|
value | Numbers | undefined |
characterAmount | number | undefined |
sign | string | '0' |
Returns
string
Alias
padLeft
Defined in
web3-utils/src/string_manipulation.ts:41
numberToHex
▸ numberToHex(value
, hexstrict?
): string
Converts value to it's hex representation
Parameters
Name | Type | Description |
---|---|---|
value | Numbers | Value to be converted |
hexstrict? | boolean | Add padding to converted value if odd, to make it hexstrict |
Returns
string
- The hex representation of the input value
Example
console.log(web3.utils.numberToHex(10));
> "0xa"
Defined in
web3-utils/src/converters.ts:183
padLeft
▸ padLeft(value
, characterAmount
, sign?
): string
Adds a padding on the left of a string, if value is a integer or bigInt will be converted to a hex string.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
value | Numbers | undefined | The value to be padded. |
characterAmount | number | undefined | The amount of characters the string should have. |
sign | string | '0' | The sign to be added (default is 0). |