Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

ethereumjs-util

ethereumjs-util

Table of contents

Namespaces

Enumerations

Classes

Interfaces

Type aliases

Variables

Functions

Type aliases

AddressLike

Ƭ AddressLike: Address | Buffer | PrefixedHexString

A type that represents an Address-like value. To convert to address, use new Address(toBuffer(value))

Defined in

packages/util/src/types.ts:32


BNLike

Ƭ BNLike: BN | PrefixedHexString | number | Buffer

Defined in

packages/util/src/types.ts:9


BufferLike

Ƭ BufferLike: Buffer | Uint8Array | number[] | number | BN | TransformableToBuffer | PrefixedHexString

Defined in

packages/util/src/types.ts:14


NestedBufferArray

Ƭ NestedBufferArray: (Buffer | NestedBufferArray)[]

Defined in

packages/util/src/types.ts:51


NestedUint8Array

Ƭ NestedUint8Array: (Uint8Array | NestedUint8Array)[]

Defined in

packages/util/src/types.ts:50


PrefixedHexString

Ƭ PrefixedHexString: string

Defined in

packages/util/src/types.ts:26


ToBufferInputTypes

Ƭ ToBufferInputTypes: PrefixedHexString | number | BN | Buffer | Uint8Array | number[] | TransformableToArray | TransformableToBuffer | null | undefined

Defined in

packages/util/src/bytes.ts:136


TypeOutputReturnType

Ƭ TypeOutputReturnType: Object

Type declaration

Name Type
0 number
1 BN
2 Buffer
3 PrefixedHexString

Defined in

packages/util/src/types.ts:89

Variables

KECCAK256_NULL

KECCAK256_NULL: Buffer

Keccak-256 hash of null

Defined in

packages/util/src/constants.ts:33


KECCAK256_NULL_S

KECCAK256_NULL_S: "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"

Keccak-256 hash of null

Defined in

packages/util/src/constants.ts:28


KECCAK256_RLP

KECCAK256_RLP: Buffer

Keccak-256 hash of the RLP of null

Defined in

packages/util/src/constants.ts:54


KECCAK256_RLP_ARRAY

KECCAK256_RLP_ARRAY: Buffer

Keccak-256 of an RLP of an empty array

Defined in

packages/util/src/constants.ts:44


KECCAK256_RLP_ARRAY_S

KECCAK256_RLP_ARRAY_S: "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"

Keccak-256 of an RLP of an empty array

Defined in

packages/util/src/constants.ts:38


KECCAK256_RLP_S

KECCAK256_RLP_S: "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"

Keccak-256 hash of the RLP of null

Defined in

packages/util/src/constants.ts:49


MAX_INTEGER

MAX_INTEGER: BN

The max integer that the evm can handle (2^256-1)

Defined in

packages/util/src/constants.ts:12


MAX_UINT64

MAX_UINT64: BN

2^64-1

Defined in

packages/util/src/constants.ts:7


TWO_POW256

TWO_POW256: BN

2^256

Defined in

packages/util/src/constants.ts:20

Functions

addHexPrefix

Const addHexPrefix(str): string

Adds "0x" to a given String if it does not already start with "0x".

Parameters

Name Type
str string

Returns

string

Defined in

packages/util/src/bytes.ts:236


arrToBufArr

arrToBufArr(arr): Buffer

Converts a {@link Uint8Array} or NestedUint8Array to Buffer or NestedBufferArray

Parameters

Name Type
arr Uint8Array

Returns

Buffer

Defined in

packages/util/src/bytes.ts:313

arrToBufArr(arr): NestedBufferArray

Parameters

Name Type
arr NestedUint8Array

Returns

NestedBufferArray

Defined in

packages/util/src/bytes.ts:314

arrToBufArr(arr): Buffer | NestedBufferArray

Parameters

Name Type
arr Uint8Array | NestedUint8Array

Returns

Buffer | NestedBufferArray

Defined in

packages/util/src/bytes.ts:315


arrayContainsArray

arrayContainsArray(superset, subset, some?): boolean

Returns TRUE if the first specified array contains all elements from the second one. FALSE otherwise.

Parameters

Name Type
superset unknown[]
subset unknown[]
some? boolean

Returns

boolean

Defined in

packages/util/src/internal.ts:89


baToJSON

Const baToJSON(ba): any

Converts a Buffer or Array to JSON.

Parameters

Name Type Description
ba any (Buffer|Array)

Returns

any

(Array|String|null)

Defined in

packages/util/src/bytes.ts:277


bnToHex

bnToHex(value): PrefixedHexString

Convert BN to 0x-prefixed hex string.

Parameters

Name Type
value BN

Returns

PrefixedHexString

Defined in

packages/util/src/types.ts:56


bnToRlp

bnToRlp(value): Buffer

Deprecated alias for bnToUnpaddedBuffer

deprecated

Parameters

Name Type
value BN

Returns

Buffer

Defined in

packages/util/src/types.ts:75


bnToUnpaddedBuffer

bnToUnpaddedBuffer(value): Buffer

Convert value from BN to an unpadded Buffer (useful for RLP transport)

Parameters

Name Type Description
value BN value to convert

Returns

Buffer

Defined in

packages/util/src/types.ts:65


bufArrToArr

bufArrToArr(arr): Uint8Array

Converts a Buffer or NestedBufferArray to {@link Uint8Array} or NestedUint8Array

Parameters

Name Type
arr Buffer

Returns

Uint8Array

Defined in

packages/util/src/bytes.ts:326

bufArrToArr(arr): NestedUint8Array

Parameters

Name Type
arr NestedBufferArray

Returns

NestedUint8Array

Defined in

packages/util/src/bytes.ts:327

bufArrToArr(arr): Uint8Array | NestedUint8Array

Parameters

Name Type
arr Buffer | NestedBufferArray

Returns

Uint8Array | NestedUint8Array

Defined in

packages/util/src/bytes.ts:328


bufferToHex

Const bufferToHex(buf): string

Converts a Buffer into a 0x-prefixed hex String.

Parameters

Name Type Description
buf Buffer Buffer object to convert

Returns

string

Defined in

packages/util/src/bytes.ts:212


bufferToInt

Const bufferToInt(buf): number

Converts a Buffer to a Number.

throws If the input number exceeds 53 bits.

Parameters

Name Type Description
buf Buffer Buffer object to convert

Returns

number

Defined in

packages/util/src/bytes.ts:204


defineProperties

Const defineProperties(self, fields, data?): void

Defines properties on a Object. It make the assumption that underlying data is binary.

deprecated

Parameters

Name Type Description
self any the Object to define properties on
fields any an array fields to define. Fields can contain: * name - the name of the properties * length - the number of bytes the field can have * allowLess - if the field can be less than the length * allowEmpty
data? any data to be validated against the definitions

Returns

void

Defined in

packages/util/src/object.ts:17


ecrecover

Const ecrecover(msgHash, v, r, s, chainId?): Buffer

ECDSA public key recovery from signature.

Parameters

Name Type
msgHash Buffer
v BNLike
r Buffer
s Buffer
chainId? BNLike

Returns

Buffer

Recovered public key

Defined in

packages/util/src/signature.ts:65


ecsign

ecsign(msgHash, privateKey, chainId?): ECDSASignature

Returns the ECDSA signature of a message hash.

Parameters

Name Type
msgHash Buffer
privateKey Buffer
chainId? number

Returns

ECDSASignature

Defined in

packages/util/src/signature.ts:23

ecsign(msgHash, privateKey, chainId): ECDSASignatureBuffer

Parameters

Name Type
msgHash Buffer
privateKey Buffer
chainId BNLike

Returns

ECDSASignatureBuffer

Defined in

packages/util/src/signature.ts:24


fromAscii

fromAscii(stringValue): string

Should be called to get hex representation (prefixed by 0x) of ascii string

Parameters

Name Type
stringValue string

Returns

string

hex representation of input string

Defined in

packages/util/src/internal.ts:149


fromRpcSig

Const fromRpcSig(sig): ECDSASignature

Convert signature format of the eth_sign RPC method to signature parameters NOTE: all because of a bug in geth: ethereum/go-ethereum#2053

Parameters

Name Type
sig string

Returns

ECDSASignature

Defined in

packages/util/src/signature.ts:119


fromSigned

Const fromSigned(num): BN

Interprets a Buffer as a signed integer and returns a BN. Assumes 256-bit numbers.

Parameters

Name Type Description
num Buffer Signed integer value

Returns

BN

Defined in

packages/util/src/bytes.ts:221


fromUtf8

fromUtf8(stringValue): string

Should be called to get hex representation (prefixed by 0x) of utf8 string

Parameters

Name Type
stringValue string

Returns

string

hex representation of input string

Defined in

packages/util/src/internal.ts:136


generateAddress

Const generateAddress(from, nonce): Buffer

Generates an address of a newly created contract.

Parameters

Name Type Description
from Buffer The address which is creating this new address
nonce Buffer The nonce of the from account

Returns

Buffer

Defined in

packages/util/src/account.ts:191


generateAddress2

Const generateAddress2(from, salt, initCode): Buffer

Generates an address for a contract created using CREATE2.

Parameters

Name Type Description
from Buffer The address which is creating this new address
salt Buffer A salt
initCode Buffer The init code of the contract being created

Returns

Buffer

Defined in

packages/util/src/account.ts:212


getBinarySize

getBinarySize(str): number

Get the binary size of a string

Parameters

Name Type
str string

Returns

number

the number of bytes contained within the string

Defined in

packages/util/src/internal.ts:73


getKeys

getKeys(params, key, allowEmpty?): string[]

Returns the keys from an array of objects.

example

getKeys([{a: '1', b: '2'}, {a: '3', b: '4'}], 'a') => ['1', '3']

Parameters

Name Type
params Record<string, string>[]
key string
allowEmpty? boolean

Returns

string[]

output just a simple array of output keys

Defined in

packages/util/src/internal.ts:171


hashPersonalMessage

Const hashPersonalMessage(message): Buffer

Returns the keccak-256 hash of message, prefixed with the header used by the eth_sign RPC call. The output of this function can be fed into ecsign to produce the same signature as the eth_sign call for a given message, or fed to ecrecover along with a signature to recover the public key used to produce the signature.

Parameters

Name Type
message Buffer

Returns

Buffer

Defined in

packages/util/src/signature.ts:196


importPublic

Const importPublic(publicKey): Buffer

Converts a public key to the Ethereum format.

Parameters

Name Type
publicKey Buffer

Returns

Buffer

Defined in

packages/util/src/account.ts:292


intToBuffer

Const intToBuffer(i): Buffer

Converts an Number to a Buffer

Parameters

Name Type
i number

Returns

Buffer

Defined in

packages/util/src/bytes.ts:29


intToHex

Const intToHex(i): string

Converts a Number into a hex String

Parameters

Name Type
i number

Returns

string

Defined in

packages/util/src/bytes.ts:17


isHexPrefixed

isHexPrefixed(str): boolean

Returns a Boolean on whether or not the a String starts with '0x'

throws if the str input is not a string

Parameters

Name Type Description
str string the string input value

Returns

boolean

a boolean if it is or is not hex prefixed

Defined in

packages/util/src/internal.ts:31


isHexString

isHexString(value, length?): boolean

Is the string a hex string.

Parameters

Name Type
value string
length? number

Returns

boolean

output the string is a hex string

Defined in

packages/util/src/internal.ts:203


isValidAddress

Const isValidAddress(hexAddress): boolean

Checks if the address is a valid. Accepts checksummed addresses too.

Parameters

Name Type
hexAddress string

Returns

boolean

Defined in

packages/util/src/account.ts:128


isValidChecksumAddress

Const isValidChecksumAddress(hexAddress, eip1191ChainId?): boolean

Checks if the address is a valid checksummed address.

See toChecksumAddress' documentation for details about the eip1191ChainId parameter.

Parameters

Name Type
hexAddress string
eip1191ChainId? BNLike

Returns

boolean

Defined in

packages/util/src/account.ts:179


isValidPrivate

Const isValidPrivate(privateKey): boolean

Checks if the private key satisfies the rules of the curve secp256k1.

Parameters

Name Type
privateKey Buffer

Returns

boolean

Defined in

packages/util/src/account.ts:230


isValidPublic

Const isValidPublic(publicKey, sanitize?): boolean

Checks if the public key satisfies the rules of the curve secp256k1 and the requirements of Ethereum.

Parameters

Name Type Default value Description
publicKey Buffer undefined The two points of an uncompressed key, unless sanitize is enabled
sanitize boolean false Accept public keys in other formats

Returns

boolean

Defined in

packages/util/src/account.ts:240


isValidSignature

Const isValidSignature(v, r, s, homesteadOrLater?, chainId?): boolean

Validate a ECDSA signature.

Parameters

Name Type Default value Description
v BNLike undefined -
r Buffer undefined -
s Buffer undefined -
homesteadOrLater boolean true Indicates whether this is being used on either the homestead hardfork or a later one
chainId? BNLike undefined -

Returns

boolean

Defined in

packages/util/src/signature.ts:155


isZeroAddress

Const isZeroAddress(hexAddress): boolean

Checks if a given address is the zero address.

Parameters

Name Type
hexAddress string

Returns

boolean

Defined in

packages/util/src/account.ts:312


keccak

Const keccak(a, bits?): Buffer

Creates Keccak hash of a Buffer input

Parameters

Name Type Default value Description
a Buffer undefined The input data (Buffer)
bits number 256 (number = 256) The Keccak width

Returns

Buffer

Defined in

packages/util/src/hash.ts:12


keccak256

Const keccak256(a): Buffer

Creates Keccak-256 hash of the input, alias for keccak(a, 256).

Parameters

Name Type Description
a Buffer The input data (Buffer)

Returns

Buffer

Defined in

packages/util/src/hash.ts:37


keccakFromArray

Const keccakFromArray(a, bits?): Buffer

Creates Keccak hash of a number array input

Parameters

Name Type Default value Description
a number[] undefined The input data (number[])
bits number 256 (number = 256) The Keccak width

Returns

Buffer

Defined in

packages/util/src/hash.ts:67


keccakFromHexString

Const keccakFromHexString(a, bits?): Buffer

Creates Keccak hash of an 0x-prefixed string input

Parameters

Name Type Default value Description
a string undefined The input data (String)
bits number 256 (number = 256) The Keccak width

Returns

Buffer

Defined in

packages/util/src/hash.ts:57


keccakFromString

Const keccakFromString(a, bits?): Buffer

Creates Keccak hash of a utf-8 string input

Parameters

Name Type Default value Description
a string undefined The input data (String)
bits number 256 (number = 256) The Keccak width

Returns

Buffer

Defined in

packages/util/src/hash.ts:46


padToEven

padToEven(value): string

Pads a String to have an even length

Parameters

Name Type
value string

Returns

string

output

Defined in

packages/util/src/internal.ts:56


privateToAddress

Const privateToAddress(privateKey): Buffer

Returns the ethereum address of a given private key.

Parameters

Name Type Description
privateKey Buffer A private key must be 256 bits wide

Returns

Buffer

Defined in

packages/util/src/account.ts:285


privateToPublic

Const privateToPublic(privateKey): Buffer

Returns the ethereum public key of a given private key.

Parameters

Name Type Description
privateKey Buffer A private key must be 256 bits wide

Returns

Buffer

Defined in

packages/util/src/account.ts:275


pubToAddress

Const pubToAddress(pubKey, sanitize?): Buffer

Returns the ethereum address of a given public key. Accepts "Ethereum public keys" and SEC1 encoded keys.

Parameters

Name Type Default value Description
pubKey Buffer undefined The two points of an uncompressed key, unless sanitize is enabled
sanitize boolean false Accept public keys in other formats

Returns

Buffer

Defined in

packages/util/src/account.ts:260


publicToAddress

Const publicToAddress(pubKey, sanitize?): Buffer

Parameters

Name Type Default value
pubKey Buffer undefined
sanitize boolean false

Returns

Buffer

Defined in

packages/util/src/account.ts:269


ripemd160

Const ripemd160(a, padded): Buffer

Creates RIPEMD160 hash of a Buffer input.

Parameters

Name Type Description
a Buffer The input data (Buffer)
padded boolean Whether it should be padded to 256 bits or not

Returns

Buffer

Defined in

packages/util/src/hash.ts:128


ripemd160FromArray

Const ripemd160FromArray(a, padded): Buffer

Creates RIPEMD160 hash of a number[] input.

Parameters

Name Type Description
a number[] The input data (number[])
padded boolean Whether it should be padded to 256 bits or not

Returns

Buffer

Defined in

packages/util/src/hash.ts:148


ripemd160FromString

Const ripemd160FromString(a, padded): Buffer

Creates RIPEMD160 hash of a string input.

Parameters

Name Type Description
a string The input data (String)
padded boolean Whether it should be padded to 256 bits or not

Returns

Buffer

Defined in

packages/util/src/hash.ts:138


rlphash

Const rlphash(a): Buffer

Creates SHA-3 hash of the RLP encoded version of the input.

Parameters

Name Type Description
a Input The input data

Returns

Buffer

Defined in

packages/util/src/hash.ts:157


setLengthLeft

Const setLengthLeft(msg, length): Buffer

Left Pads a Buffer with leading zeros till it has length bytes. Or it truncates the beginning if it exceeds.

Parameters

Name Type Description
msg Buffer the value to pad (Buffer)
length number the number of bytes the output should be

Returns

Buffer

(Buffer)

Defined in

packages/util/src/bytes.ts:74


setLengthRight

Const setLengthRight(msg, length): Buffer

Right Pads a Buffer with trailing zeros till it has length bytes. it truncates the end if it exceeds.

Parameters

Name Type Description
msg Buffer the value to pad (Buffer)
length number the number of bytes the output should be

Returns

Buffer

(Buffer)

Defined in

packages/util/src/bytes.ts:86


sha256

Const sha256(a): Buffer

Creates SHA256 hash of a Buffer input.

Parameters

Name Type Description
a Buffer The input data (Buffer)

Returns

Buffer

Defined in

packages/util/src/hash.ts:85


sha256FromArray

Const sha256FromArray(a): Buffer

Creates SHA256 hash of a number[] input.

Parameters

Name Type Description
a number[] The input data (number[])

Returns

Buffer

Defined in

packages/util/src/hash.ts:103


sha256FromString

Const sha256FromString(a): Buffer

Creates SHA256 hash of a string input.

Parameters

Name Type Description
a string The input data (string)

Returns

Buffer

Defined in

packages/util/src/hash.ts:94


stripHexPrefix

Const stripHexPrefix(str): string

Removes '0x' from a given String if present

Parameters

Name Type Description
str string the string value

Returns

string

the string without 0x prefix

Defined in

packages/util/src/internal.ts:44


toAscii

toAscii(hex): string

Should be called to get ascii from its hex representation

Parameters

Name Type
hex string

Returns

string

ascii string representation of hex value

Defined in

packages/util/src/internal.ts:114


toBuffer

Const toBuffer(v): Buffer

Attempts to turn a value into a Buffer. Inputs supported: Buffer, String (hex-prefixed), Number, null/undefined, BN and other objects with a toArray() or toBuffer() method.

Parameters

Name Type Description
v ToBufferInputTypes the value

Returns

Buffer

Defined in

packages/util/src/bytes.ts:154


toChecksumAddress

Const toChecksumAddress(hexAddress, eip1191ChainId?): string

Returns a checksummed address.

If an eip1191ChainId is provided, the chainId will be included in the checksum calculation. This has the effect of checksummed addresses for one chain having invalid checksums for others. For more details see EIP-1191.

WARNING: Checksums with and without the chainId will differ and the EIP-1191 checksum is not backwards compatible to the original widely adopted checksum format standard introduced in EIP-55, so this will break in existing applications. Usage of this EIP is therefore discouraged unless you have a very targeted use case.

Parameters

Name Type
hexAddress string
eip1191ChainId? BNLike

Returns

string

Defined in

packages/util/src/account.ts:150


toCompactSig

Const toCompactSig(v, r, s, chainId?): string

Convert signature parameters into the format of Compact Signature Representation (EIP-2098).

Parameters

Name Type
v BNLike
r Buffer
s Buffer
chainId? BNLike

Returns

string

Signature

Defined in

packages/util/src/signature.ts:99


toRpcSig

Const toRpcSig(v, r, s, chainId?): string

Convert signature parameters into the format of eth_sign RPC method.

Parameters

Name Type
v BNLike
r Buffer
s Buffer
chainId? BNLike

Returns

string

Signature

Defined in

packages/util/src/signature.ts:85


toType

toType<T>(input, outputType): null

Convert an input to a specified type. Input of null/undefined returns null/undefined regardless of the output type.

Type parameters

Name Type
T extends TypeOutput

Parameters

Name Type Description
input null value to convert
outputType T type to output

Returns

null

Defined in

packages/util/src/types.ts:102

toType<T>(input, outputType): undefined

Type parameters

Name Type
T extends TypeOutput

Parameters

Name Type
input undefined
outputType T

Returns

undefined

Defined in

packages/util/src/types.ts:103

toType<T>(input, outputType): TypeOutputReturnType[T]

Type parameters

Name Type
T extends TypeOutput

Parameters

Name Type
input ToBufferInputTypes
outputType T

Returns

TypeOutputReturnType[T]

Defined in

packages/util/src/types.ts:104


toUnsigned

Const toUnsigned(num): Buffer

Converts a BN to an unsigned integer and returns it as a Buffer. Assumes 256-bit numbers.

Parameters

Name Type
num BN

Returns

Buffer

Defined in

packages/util/src/bytes.ts:229


toUtf8

Const toUtf8(hex): string

Returns the utf8 string representation from a hex string.

Examples:

Input 1: '657468657265756d000000000000000000000000000000000000000000000000' Input 2: '657468657265756d' Input 3: '000000000000000000000000000000000000000000000000657468657265756d'

Output (all 3 input variants): 'ethereum'

Note that this method is not intended to be used with hex strings representing quantities in both big endian or little endian notation.

Parameters

Name Type
hex string

Returns

string

Utf8 string

Defined in

packages/util/src/bytes.ts:261


unpadArray

Const unpadArray(a): number[]

Trims leading zeros from an Array (of numbers).

Parameters

Name Type Description
a number[] (number[])

Returns

number[]

(number[])

Defined in

packages/util/src/bytes.ts:120


unpadBuffer

Const unpadBuffer(a): Buffer

Trims leading zeros from a Buffer.

Parameters

Name Type Description
a Buffer (Buffer)

Returns

Buffer

(Buffer)

Defined in

packages/util/src/bytes.ts:110


unpadHexString

Const unpadHexString(a): string

Trims leading zeros from a hex-prefixed String.

Parameters

Name Type Description
a string (String)

Returns

string

(String)

Defined in

packages/util/src/bytes.ts:130


validateNoLeadingZeroes

Const validateNoLeadingZeroes(values): void

Checks provided Buffers for leading zeroes and throws if found.

Examples:

Valid values: 0x1, 0x, 0x01, 0x1234 Invalid values: 0x0, 0x00, 0x001, 0x0001

Note: This method is useful for validating that RLP encoded integers comply with the rule that all integer values encoded to RLP must be in the most compact form and contain no leading zero bytes

throws if any provided value is found to have leading zero bytes

Parameters

Name Type Description
values Object An object containing string keys and Buffer values

Returns

void

Defined in

packages/util/src/bytes.ts:302


zeroAddress

Const zeroAddress(): string

Returns the zero address.

Returns

string

Defined in

packages/util/src/account.ts:303


zeros

Const zeros(bytes): Buffer

Returns a buffer filled with 0s.

Parameters

Name Type Description
bytes number the number of bytes the buffer should be

Returns

Buffer

Defined in

packages/util/src/bytes.ts:38