ethereumjs-util
- AddressLike
- BNLike
- BufferLike
- NestedBufferArray
- NestedUint8Array
- PrefixedHexString
- ToBufferInputTypes
- TypeOutputReturnType
- KECCAK256_NULL
- KECCAK256_NULL_S
- KECCAK256_RLP
- KECCAK256_RLP_ARRAY
- KECCAK256_RLP_ARRAY_S
- KECCAK256_RLP_S
- MAX_INTEGER
- MAX_UINT64
- TWO_POW256
- addHexPrefix
- arrToBufArr
- arrayContainsArray
- baToJSON
- bnToHex
- bnToRlp
- bnToUnpaddedBuffer
- bufArrToArr
- bufferToHex
- bufferToInt
- defineProperties
- ecrecover
- ecsign
- fromAscii
- fromRpcSig
- fromSigned
- fromUtf8
- generateAddress
- generateAddress2
- getBinarySize
- getKeys
- hashPersonalMessage
- importPublic
- intToBuffer
- intToHex
- isHexPrefixed
- isHexString
- isValidAddress
- isValidChecksumAddress
- isValidPrivate
- isValidPublic
- isValidSignature
- isZeroAddress
- keccak
- keccak256
- keccakFromArray
- keccakFromHexString
- keccakFromString
- padToEven
- privateToAddress
- privateToPublic
- pubToAddress
- publicToAddress
- ripemd160
- ripemd160FromArray
- ripemd160FromString
- rlphash
- setLengthLeft
- setLengthRight
- sha256
- sha256FromArray
- sha256FromString
- stripHexPrefix
- toAscii
- toBuffer
- toChecksumAddress
- toCompactSig
- toRpcSig
- toType
- toUnsigned
- toUtf8
- unpadArray
- unpadBuffer
- unpadHexString
- validateNoLeadingZeroes
- zeroAddress
- zeros
Ƭ AddressLike: Address
| Buffer
| PrefixedHexString
A type that represents an Address-like value.
To convert to address, use new Address(toBuffer(value))
Ƭ BNLike: BN
| PrefixedHexString
| number
| Buffer
Ƭ BufferLike: Buffer
| Uint8Array
| number
[] | number
| BN
| TransformableToBuffer
| PrefixedHexString
Ƭ NestedBufferArray: (Buffer
| NestedBufferArray
)[]
Ƭ NestedUint8Array: (Uint8Array
| NestedUint8Array
)[]
Ƭ PrefixedHexString: string
Ƭ ToBufferInputTypes: PrefixedHexString
| number
| BN
| Buffer
| Uint8Array
| number
[] | TransformableToArray
| TransformableToBuffer
| null
| undefined
packages/util/src/bytes.ts:136
Ƭ TypeOutputReturnType: Object
Name | Type |
---|---|
0 |
number |
1 |
BN |
2 |
Buffer |
3 |
PrefixedHexString |
• KECCAK256_NULL: Buffer
Keccak-256 hash of null
packages/util/src/constants.ts:33
• KECCAK256_NULL_S: "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
Keccak-256 hash of null
packages/util/src/constants.ts:28
• KECCAK256_RLP: Buffer
Keccak-256 hash of the RLP of null
packages/util/src/constants.ts:54
• KECCAK256_RLP_ARRAY: Buffer
Keccak-256 of an RLP of an empty array
packages/util/src/constants.ts:44
• KECCAK256_RLP_ARRAY_S: "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
Keccak-256 of an RLP of an empty array
packages/util/src/constants.ts:38
• KECCAK256_RLP_S: "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
Keccak-256 hash of the RLP of null
packages/util/src/constants.ts:49
• MAX_INTEGER: BN
The max integer that the evm can handle (2^256-1)
packages/util/src/constants.ts:12
• MAX_UINT64: BN
2^64-1
packages/util/src/constants.ts:7
• TWO_POW256: BN
2^256
packages/util/src/constants.ts:20
▸ Const
addHexPrefix(str
): string
Adds "0x" to a given String
if it does not already start with "0x".
Name | Type |
---|---|
str |
string |
string
packages/util/src/bytes.ts:236
▸ arrToBufArr(arr
): Buffer
Converts a {@link Uint8Array} or NestedUint8Array to Buffer or NestedBufferArray
Name | Type |
---|---|
arr |
Uint8Array |
Buffer
packages/util/src/bytes.ts:313
▸ arrToBufArr(arr
): NestedBufferArray
Name | Type |
---|---|
arr |
NestedUint8Array |
packages/util/src/bytes.ts:314
▸ arrToBufArr(arr
): Buffer
| NestedBufferArray
Name | Type |
---|---|
arr |
Uint8Array | NestedUint8Array |
Buffer
| NestedBufferArray
packages/util/src/bytes.ts:315
▸ arrayContainsArray(superset
, subset
, some?
): boolean
Returns TRUE if the first specified array contains all elements from the second one. FALSE otherwise.
Name | Type |
---|---|
superset |
unknown [] |
subset |
unknown [] |
some? |
boolean |
boolean
packages/util/src/internal.ts:89
▸ Const
baToJSON(ba
): any
Converts a Buffer
or Array
to JSON.
Name | Type | Description |
---|---|---|
ba |
any |
(Buffer|Array) |
any
(Array|String|null)
packages/util/src/bytes.ts:277
▸ bnToHex(value
): PrefixedHexString
Convert BN to 0x-prefixed hex string.
Name | Type |
---|---|
value |
BN |
▸ bnToRlp(value
): Buffer
Deprecated alias for bnToUnpaddedBuffer
deprecated
Name | Type |
---|---|
value |
BN |
Buffer
▸ bnToUnpaddedBuffer(value
): Buffer
Convert value from BN to an unpadded Buffer (useful for RLP transport)
Name | Type | Description |
---|---|---|
value |
BN |
value to convert |
Buffer
▸ bufArrToArr(arr
): Uint8Array
Converts a Buffer or NestedBufferArray to {@link Uint8Array} or NestedUint8Array
Name | Type |
---|---|
arr |
Buffer |
Uint8Array
packages/util/src/bytes.ts:326
▸ bufArrToArr(arr
): NestedUint8Array
Name | Type |
---|---|
arr |
NestedBufferArray |
packages/util/src/bytes.ts:327
▸ bufArrToArr(arr
): Uint8Array
| NestedUint8Array
Name | Type |
---|---|
arr |
Buffer | NestedBufferArray |
Uint8Array
| NestedUint8Array
packages/util/src/bytes.ts:328
▸ Const
bufferToHex(buf
): string
Converts a Buffer
into a 0x
-prefixed hex String
.
Name | Type | Description |
---|---|---|
buf |
Buffer |
Buffer object to convert |
string
packages/util/src/bytes.ts:212
▸ Const
bufferToInt(buf
): number
Converts a Buffer
to a Number
.
throws
If the input number exceeds 53 bits.
Name | Type | Description |
---|---|---|
buf |
Buffer |
Buffer object to convert |
number
packages/util/src/bytes.ts:204
▸ Const
defineProperties(self
, fields
, data?
): void
Defines properties on a Object
. It make the assumption that underlying data is binary.
deprecated
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 |
void
packages/util/src/object.ts:17
▸ Const
ecrecover(msgHash
, v
, r
, s
, chainId?
): Buffer
ECDSA public key recovery from signature.
Name | Type |
---|---|
msgHash |
Buffer |
v |
BNLike |
r |
Buffer |
s |
Buffer |
chainId? |
BNLike |
Buffer
Recovered public key
packages/util/src/signature.ts:65
▸ ecsign(msgHash
, privateKey
, chainId?
): ECDSASignature
Returns the ECDSA signature of a message hash.
Name | Type |
---|---|
msgHash |
Buffer |
privateKey |
Buffer |
chainId? |
number |
packages/util/src/signature.ts:23
▸ ecsign(msgHash
, privateKey
, chainId
): ECDSASignatureBuffer
Name | Type |
---|---|
msgHash |
Buffer |
privateKey |
Buffer |
chainId |
BNLike |
packages/util/src/signature.ts:24
▸ fromAscii(stringValue
): string
Should be called to get hex representation (prefixed by 0x) of ascii string
Name | Type |
---|---|
stringValue |
string |
string
hex representation of input string
packages/util/src/internal.ts:149
▸ 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
Name | Type |
---|---|
sig |
string |
packages/util/src/signature.ts:119
▸ Const
fromSigned(num
): BN
Interprets a Buffer
as a signed integer and returns a BN
. Assumes 256-bit numbers.
Name | Type | Description |
---|---|---|
num |
Buffer |
Signed integer value |
packages/util/src/bytes.ts:221
▸ fromUtf8(stringValue
): string
Should be called to get hex representation (prefixed by 0x) of utf8 string
Name | Type |
---|---|
stringValue |
string |
string
hex representation of input string
packages/util/src/internal.ts:136
▸ Const
generateAddress(from
, nonce
): Buffer
Generates an address of a newly created contract.
Name | Type | Description |
---|---|---|
from |
Buffer |
The address which is creating this new address |
nonce |
Buffer |
The nonce of the from account |
Buffer
packages/util/src/account.ts:191
▸ Const
generateAddress2(from
, salt
, initCode
): Buffer
Generates an address for a contract created using CREATE2.
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 |
Buffer
packages/util/src/account.ts:212
▸ getBinarySize(str
): number
Get the binary size of a string
Name | Type |
---|---|
str |
string |
number
the number of bytes contained within the string
packages/util/src/internal.ts:73
▸ 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']
Name | Type |
---|---|
params |
Record <string , string >[] |
key |
string |
allowEmpty? |
boolean |
string
[]
output just a simple array of output keys
packages/util/src/internal.ts:171
▸ 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.
Name | Type |
---|---|
message |
Buffer |
Buffer
packages/util/src/signature.ts:196
▸ Const
importPublic(publicKey
): Buffer
Converts a public key to the Ethereum format.
Name | Type |
---|---|
publicKey |
Buffer |
Buffer
packages/util/src/account.ts:292
▸ Const
intToBuffer(i
): Buffer
Converts an Number
to a Buffer
Name | Type |
---|---|
i |
number |
Buffer
▸ Const
intToHex(i
): string
Converts a Number
into a hex String
Name | Type |
---|---|
i |
number |
string
▸ 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
Name | Type | Description |
---|---|---|
str |
string |
the string input value |
boolean
a boolean if it is or is not hex prefixed
packages/util/src/internal.ts:31
▸ isHexString(value
, length?
): boolean
Is the string a hex string.
Name | Type |
---|---|
value |
string |
length? |
number |
boolean
output the string is a hex string
packages/util/src/internal.ts:203
▸ Const
isValidAddress(hexAddress
): boolean
Checks if the address is a valid. Accepts checksummed addresses too.
Name | Type |
---|---|
hexAddress |
string |
boolean
packages/util/src/account.ts:128
▸ Const
isValidChecksumAddress(hexAddress
, eip1191ChainId?
): boolean
Checks if the address is a valid checksummed address.
See toChecksumAddress' documentation for details about the eip1191ChainId parameter.
Name | Type |
---|---|
hexAddress |
string |
eip1191ChainId? |
BNLike |
boolean
packages/util/src/account.ts:179
▸ Const
isValidPrivate(privateKey
): boolean
Checks if the private key satisfies the rules of the curve secp256k1.
Name | Type |
---|---|
privateKey |
Buffer |
boolean
packages/util/src/account.ts:230
▸ Const
isValidPublic(publicKey
, sanitize?
): boolean
Checks if the public key satisfies the rules of the curve secp256k1 and the requirements of Ethereum.
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 |
boolean
packages/util/src/account.ts:240
▸ Const
isValidSignature(v
, r
, s
, homesteadOrLater?
, chainId?
): boolean
Validate a ECDSA signature.
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 |
- |
boolean
packages/util/src/signature.ts:155
▸ Const
isZeroAddress(hexAddress
): boolean
Checks if a given address is the zero address.
Name | Type |
---|---|
hexAddress |
string |
boolean
packages/util/src/account.ts:312
▸ Const
keccak(a
, bits?
): Buffer
Creates Keccak hash of a Buffer input
Name | Type | Default value | Description |
---|---|---|---|
a |
Buffer |
undefined |
The input data (Buffer) |
bits |
number |
256 |
(number = 256) The Keccak width |
Buffer
▸ Const
keccak256(a
): Buffer
Creates Keccak-256 hash of the input, alias for keccak(a, 256).
Name | Type | Description |
---|---|---|
a |
Buffer |
The input data (Buffer) |
Buffer
▸ Const
keccakFromArray(a
, bits?
): Buffer
Creates Keccak hash of a number array input
Name | Type | Default value | Description |
---|---|---|---|
a |
number [] |
undefined |
The input data (number[]) |
bits |
number |
256 |
(number = 256) The Keccak width |
Buffer
▸ Const
keccakFromHexString(a
, bits?
): Buffer
Creates Keccak hash of an 0x-prefixed string input
Name | Type | Default value | Description |
---|---|---|---|
a |
string |
undefined |
The input data (String) |
bits |
number |
256 |
(number = 256) The Keccak width |
Buffer
▸ Const
keccakFromString(a
, bits?
): Buffer
Creates Keccak hash of a utf-8 string input
Name | Type | Default value | Description |
---|---|---|---|
a |
string |
undefined |
The input data (String) |
bits |
number |
256 |
(number = 256) The Keccak width |
Buffer
▸ padToEven(value
): string
Pads a String
to have an even length
Name | Type |
---|---|
value |
string |
string
output
packages/util/src/internal.ts:56
▸ Const
privateToAddress(privateKey
): Buffer
Returns the ethereum address of a given private key.
Name | Type | Description |
---|---|---|
privateKey |
Buffer |
A private key must be 256 bits wide |
Buffer
packages/util/src/account.ts:285
▸ Const
privateToPublic(privateKey
): Buffer
Returns the ethereum public key of a given private key.
Name | Type | Description |
---|---|---|
privateKey |
Buffer |
A private key must be 256 bits wide |
Buffer
packages/util/src/account.ts:275
▸ Const
pubToAddress(pubKey
, sanitize?
): Buffer
Returns the ethereum address of a given public key. Accepts "Ethereum public keys" and SEC1 encoded keys.
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 |
Buffer
packages/util/src/account.ts:260
▸ Const
publicToAddress(pubKey
, sanitize?
): Buffer
Name | Type | Default value |
---|---|---|
pubKey |
Buffer |
undefined |
sanitize |
boolean |
false |
Buffer
packages/util/src/account.ts:269
▸ Const
ripemd160(a
, padded
): Buffer
Creates RIPEMD160 hash of a Buffer input.
Name | Type | Description |
---|---|---|
a |
Buffer |
The input data (Buffer) |
padded |
boolean |
Whether it should be padded to 256 bits or not |
Buffer
▸ Const
ripemd160FromArray(a
, padded
): Buffer
Creates RIPEMD160 hash of a number[] input.
Name | Type | Description |
---|---|---|
a |
number [] |
The input data (number[]) |
padded |
boolean |
Whether it should be padded to 256 bits or not |
Buffer
▸ Const
ripemd160FromString(a
, padded
): Buffer
Creates RIPEMD160 hash of a string input.
Name | Type | Description |
---|---|---|
a |
string |
The input data (String) |
padded |
boolean |
Whether it should be padded to 256 bits or not |
Buffer
▸ Const
rlphash(a
): Buffer
Creates SHA-3 hash of the RLP encoded version of the input.
Name | Type | Description |
---|---|---|
a |
Input |
The input data |
Buffer
▸ 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.
Name | Type | Description |
---|---|---|
msg |
Buffer |
the value to pad (Buffer) |
length |
number |
the number of bytes the output should be |
Buffer
(Buffer)
▸ Const
setLengthRight(msg
, length
): Buffer
Right Pads a Buffer
with trailing zeros till it has length
bytes.
it truncates the end if it exceeds.
Name | Type | Description |
---|---|---|
msg |
Buffer |
the value to pad (Buffer) |
length |
number |
the number of bytes the output should be |
Buffer
(Buffer)
▸ Const
sha256(a
): Buffer
Creates SHA256 hash of a Buffer input.
Name | Type | Description |
---|---|---|
a |
Buffer |
The input data (Buffer) |
Buffer
▸ Const
sha256FromArray(a
): Buffer
Creates SHA256 hash of a number[] input.
Name | Type | Description |
---|---|---|
a |
number [] |
The input data (number[]) |
Buffer
▸ Const
sha256FromString(a
): Buffer
Creates SHA256 hash of a string input.
Name | Type | Description |
---|---|---|
a |
string |
The input data (string) |
Buffer
▸ Const
stripHexPrefix(str
): string
Removes '0x' from a given String
if present
Name | Type | Description |
---|---|---|
str |
string |
the string value |
string
the string without 0x prefix
packages/util/src/internal.ts:44
▸ toAscii(hex
): string
Should be called to get ascii from its hex representation
Name | Type |
---|---|
hex |
string |
string
ascii string representation of hex value
packages/util/src/internal.ts:114
▸ 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.
Name | Type | Description |
---|---|---|
v |
ToBufferInputTypes |
the value |
Buffer
packages/util/src/bytes.ts:154
▸ 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.
Name | Type |
---|---|
hexAddress |
string |
eip1191ChainId? |
BNLike |
string
packages/util/src/account.ts:150
▸ Const
toCompactSig(v
, r
, s
, chainId?
): string
Convert signature parameters into the format of Compact Signature Representation (EIP-2098).
Name | Type |
---|---|
v |
BNLike |
r |
Buffer |
s |
Buffer |
chainId? |
BNLike |
string
Signature
packages/util/src/signature.ts:99
▸ Const
toRpcSig(v
, r
, s
, chainId?
): string
Convert signature parameters into the format of eth_sign
RPC method.
Name | Type |
---|---|
v |
BNLike |
r |
Buffer |
s |
Buffer |
chainId? |
BNLike |
string
Signature
packages/util/src/signature.ts:85
▸ toType<T
>(input
, outputType
): null
Convert an input to a specified type. Input of null/undefined returns null/undefined regardless of the output type.
Name | Type |
---|---|
T |
extends TypeOutput |
Name | Type | Description |
---|---|---|
input |
null |
value to convert |
outputType |
T |
type to output |
null
packages/util/src/types.ts:102
▸ toType<T
>(input
, outputType
): undefined
Name | Type |
---|---|
T |
extends TypeOutput |
Name | Type |
---|---|
input |
undefined |
outputType |
T |
undefined
packages/util/src/types.ts:103
▸ toType<T
>(input
, outputType
): TypeOutputReturnType
[T
]
Name | Type |
---|---|
T |
extends TypeOutput |
Name | Type |
---|---|
input |
ToBufferInputTypes |
outputType |
T |
packages/util/src/types.ts:104
▸ Const
toUnsigned(num
): Buffer
Converts a BN
to an unsigned integer and returns it as a Buffer
. Assumes 256-bit numbers.
Name | Type |
---|---|
num |
BN |
Buffer
packages/util/src/bytes.ts:229
▸ 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.
Name | Type |
---|---|
hex |
string |
string
Utf8 string
packages/util/src/bytes.ts:261
▸ Const
unpadArray(a
): number
[]
Trims leading zeros from an Array
(of numbers).
Name | Type | Description |
---|---|---|
a |
number [] |
(number[]) |
number
[]
(number[])
packages/util/src/bytes.ts:120
▸ Const
unpadBuffer(a
): Buffer
Trims leading zeros from a Buffer
.
Name | Type | Description |
---|---|---|
a |
Buffer |
(Buffer) |
Buffer
(Buffer)
packages/util/src/bytes.ts:110
▸ Const
unpadHexString(a
): string
Trims leading zeros from a hex-prefixed String
.
Name | Type | Description |
---|---|---|
a |
string |
(String) |
string
(String)
packages/util/src/bytes.ts:130
▸ 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
Name | Type | Description |
---|---|---|
values |
Object |
An object containing string keys and Buffer values |
void
packages/util/src/bytes.ts:302
▸ Const
zeroAddress(): string
Returns the zero address.
string
packages/util/src/account.ts:303
▸ Const
zeros(bytes
): Buffer
Returns a buffer filled with 0s.
Name | Type | Description |
---|---|---|
bytes |
number |
the number of bytes the buffer should be |
Buffer