@ethereumjs/util
- AccountBodyBuffer
- AddressLike
- BigIntLike
- BufferLike
- NestedBufferArray
- NestedUint8Array
- PrefixedHexString
- ToBufferInputTypes
- TypeOutputReturnType
- WithdrawalBuffer
- WithdrawalData
- GWEI_TO_WEI
- KECCAK256_NULL
- KECCAK256_NULL_S
- KECCAK256_RLP
- KECCAK256_RLP_ARRAY
- KECCAK256_RLP_ARRAY_S
- KECCAK256_RLP_S
- MAX_INTEGER
- MAX_INTEGER_BIGINT
- MAX_UINT64
- MAX_WITHDRAWALS_PER_PAYLOAD
- RLP_EMPTY_STRING
- SECP256K1_ORDER
- SECP256K1_ORDER_DIV_2
- TWO_POW256
- accountBodyFromSlim
- accountBodyToRLP
- accountBodyToSlim
- addHexPrefix
- arrToBufArr
- arrayContainsArray
- baToJSON
- bigIntToBuffer
- bigIntToHex
- bigIntToUnpaddedBuffer
- bufArrToArr
- bufferToBigInt
- bufferToHex
- bufferToInt
- ecrecover
- ecsign
- fromAscii
- fromRpcSig
- fromSigned
- fromUtf8
- generateAddress
- generateAddress2
- getBinarySize
- getKeys
- hashPersonalMessage
- importPublic
- intToBuffer
- intToHex
- intToUnpaddedBuffer
- isHexPrefixed
- isHexString
- isValidAddress
- isValidChecksumAddress
- isValidPrivate
- isValidPublic
- isValidSignature
- isZeroAddress
- padToEven
- privateToAddress
- privateToPublic
- pubToAddress
- publicToAddress
- setLengthLeft
- setLengthRight
- short
- stripHexPrefix
- toAscii
- toBuffer
- toChecksumAddress
- toCompactSig
- toRpcSig
- toType
- toUnsigned
- toUtf8
- unpadArray
- unpadBuffer
- unpadHexString
- validateNoLeadingZeroes
- zeroAddress
- zeros
Ƭ AccountBodyBuffer: [Buffer
, Buffer
, Buffer
| Uint8Array
, Buffer
| Uint8Array
]
packages/util/src/account.ts:30
Ƭ AddressLike: Address
| Buffer
| PrefixedHexString
A type that represents an input that can be converted to an Address.
Ƭ BigIntLike: bigint
| PrefixedHexString
| number
| Buffer
Ƭ BufferLike: Buffer
| Uint8Array
| number
[] | number
| bigint
| TransformableToBuffer
| PrefixedHexString
Ƭ NestedBufferArray: (Buffer
| NestedBufferArray
)[]
Ƭ NestedUint8Array: (Uint8Array
| NestedUint8Array
)[]
Ƭ PrefixedHexString: string
Ƭ ToBufferInputTypes: PrefixedHexString
| number
| bigint
| Buffer
| Uint8Array
| number
[] | TransformableToArray
| TransformableToBuffer
| null
| undefined
packages/util/src/bytes.ts:136
Ƭ TypeOutputReturnType: Object
Name | Type |
---|---|
0 |
number |
1 |
bigint |
2 |
Buffer |
3 |
PrefixedHexString |
Ƭ WithdrawalBuffer: [Buffer
, Buffer
, Buffer
, Buffer
]
packages/util/src/withdrawal.ts:29
Ƭ WithdrawalData: Object
Flexible input data type for EIP-4895 withdrawal data with amount in Gwei to match CL representation and for eventual ssz withdrawalsRoot
Name | Type |
---|---|
address |
AddressLike |
amount |
BigIntLike |
index |
BigIntLike |
validatorIndex |
BigIntLike |
packages/util/src/withdrawal.ts:11
• Const
GWEI_TO_WEI: bigint
Easy conversion from Gwei to wei
• Const
KECCAK256_NULL: Buffer
Keccak-256 hash of null
packages/util/src/constants.ts:39
• Const
KECCAK256_NULL_S: "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
Keccak-256 hash of null
packages/util/src/constants.ts:34
• Const
KECCAK256_RLP: Buffer
Keccak-256 hash of the RLP of null
packages/util/src/constants.ts:60
• Const
KECCAK256_RLP_ARRAY: Buffer
Keccak-256 of an RLP of an empty array
packages/util/src/constants.ts:50
• Const
KECCAK256_RLP_ARRAY_S: "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
Keccak-256 of an RLP of an empty array
packages/util/src/constants.ts:44
• Const
KECCAK256_RLP_S: "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
Keccak-256 hash of the RLP of null
packages/util/src/constants.ts:55
• Const
MAX_INTEGER: bigint
The max integer that the evm can handle (2^256-1)
packages/util/src/constants.ts:12
• Const
MAX_INTEGER_BIGINT: bigint
The max integer that the evm can handle (2^256-1) as a bigint
packages/util/src/constants.ts:19
• Const
MAX_UINT64: bigint
2^64-1
packages/util/src/constants.ts:7
• Const
MAX_WITHDRAWALS_PER_PAYLOAD: 16
packages/util/src/constants.ts:67
• Const
RLP_EMPTY_STRING: Buffer
RLP encoded empty string
packages/util/src/constants.ts:65
• Const
SECP256K1_ORDER: bigint
= CURVE.n
packages/util/src/constants.ts:21
• Const
SECP256K1_ORDER_DIV_2: bigint
packages/util/src/constants.ts:22
• Const
TWO_POW256: bigint
2^256
packages/util/src/constants.ts:27
▸ accountBodyFromSlim(body
): (Uint8Array
| Buffer
)[]
Name | Type |
---|---|
body |
AccountBodyBuffer |
(Uint8Array
| Buffer
)[]
packages/util/src/account.ts:347
▸ accountBodyToRLP(body
, couldBeSlim?
): Buffer
Converts a slim account RLP to a normal account RLP
Name | Type | Default value |
---|---|---|
body |
AccountBodyBuffer |
undefined |
couldBeSlim |
boolean |
true |
Buffer
packages/util/src/account.ts:371
▸ accountBodyToSlim(body
): (Uint8Array
| Buffer
)[]
Name | Type |
---|---|
body |
AccountBodyBuffer |
(Uint8Array
| Buffer
)[]
packages/util/src/account.ts:358
▸ 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:258
▸ arrToBufArr(arr
): Buffer
Converts a Uint8Array or NestedUint8Array to Buffer or NestedBufferArray
Name | Type |
---|---|
arr |
Uint8Array |
Buffer
packages/util/src/bytes.ts:351
▸ arrToBufArr(arr
): NestedBufferArray
Name | Type |
---|---|
arr |
NestedUint8Array |
packages/util/src/bytes.ts:352
▸ arrToBufArr(arr
): Buffer
| NestedBufferArray
Name | Type |
---|---|
arr |
Uint8Array | NestedUint8Array |
Buffer
| NestedBufferArray
packages/util/src/bytes.ts:353
▸ 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
▸ 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:315
▸ bigIntToBuffer(num
): Buffer
Converts a bigint to a Buffer
Name | Type |
---|---|
num |
bigint |
Buffer
packages/util/src/bytes.ts:224
▸ bigIntToHex(num
): string
Converts a bigint to a 0x
prefixed hex string
Name | Type |
---|---|
num |
bigint |
string
packages/util/src/bytes.ts:377
▸ bigIntToUnpaddedBuffer(value
): Buffer
Convert value from bigint to an unpadded Buffer (useful for RLP transport)
Name | Type | Description |
---|---|---|
value |
bigint |
value to convert |
Buffer
packages/util/src/bytes.ts:386
▸ bufArrToArr(arr
): Uint8Array
Converts a Buffer or NestedBufferArray to Uint8Array or NestedUint8Array
Name | Type |
---|---|
arr |
Buffer |
Uint8Array
packages/util/src/bytes.ts:364
▸ bufArrToArr(arr
): NestedUint8Array
Name | Type |
---|---|
arr |
NestedBufferArray |
packages/util/src/bytes.ts:365
▸ bufArrToArr(arr
): Uint8Array
| NestedUint8Array
Name | Type |
---|---|
arr |
Buffer | NestedBufferArray |
Uint8Array
| NestedUint8Array
packages/util/src/bytes.ts:366
▸ bufferToBigInt(buf
): bigint
Converts a Buffer to a bigint
Name | Type |
---|---|
buf |
Buffer |
bigint
packages/util/src/bytes.ts:213
▸ 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:205
▸ 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:233
▸ ecrecover(msgHash
, v
, r
, s
, chainId?
): Buffer
ECDSA public key recovery from signature.
NOTE: Accepts v === 0 | v === 1
for EIP1559 transactions
Name | Type |
---|---|
msgHash |
Buffer |
v |
bigint |
r |
Buffer |
s |
Buffer |
chainId? |
bigint |
Buffer
Recovered public key
packages/util/src/signature.ts:52
▸ ecsign(msgHash
, privateKey
, chainId?
): ECDSASignature
Returns the ECDSA signature of a message hash.
If chainId
is provided assume an EIP-155-style signature and calculate the v
value
accordingly, otherwise return a "static" v
just derived from the recovery
bit
Name | Type |
---|---|
msgHash |
Buffer |
privateKey |
Buffer |
chainId? |
bigint |
packages/util/src/signature.ts:20
▸ 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
▸ fromRpcSig(sig
): ECDSASignature
Convert signature format of the eth_sign
RPC method to signature parameters
NOTE: For an extracted v
value < 27 (see Geth bug ethereum/go-ethereum#2053)
v + 27
is returned for the v
value
NOTE: After EIP1559, v
could be 0
or 1
but this function assumes
it's a signed message (EIP-191 or EIP-712) adding 27
at the end. Remove if needed.
Name | Type |
---|---|
sig |
string |
packages/util/src/signature.ts:112
▸ fromSigned(num
): bigint
Interprets a Buffer
as a signed integer and returns a BigInt
. Assumes 256-bit numbers.
Name | Type | Description |
---|---|---|
num |
Buffer |
Signed integer value |
bigint
packages/util/src/bytes.ts:243
▸ 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
▸ 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:199
▸ 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:219
▸ 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']
@param params @param key @param allowEmpty @returns output just a simple array of output keys
Name | Type |
---|---|
params |
Record <string , string >[] |
key |
string |
allowEmpty? |
boolean |
string
[]
packages/util/src/internal.ts:171
▸ 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:189
▸ importPublic(publicKey
): Buffer
Converts a public key to the Ethereum format.
Name | Type |
---|---|
publicKey |
Buffer |
Buffer
packages/util/src/account.ts:316
▸ intToBuffer(i
): Buffer
Converts an Number
to a Buffer
Name | Type |
---|---|
i |
number |
Buffer
▸ intToHex(i
): string
Converts a Number
into a hex String
Name | Type |
---|---|
i |
number |
string
▸ intToUnpaddedBuffer(value
): Buffer
Name | Type |
---|---|
value |
number |
Buffer
packages/util/src/bytes.ts:390
▸ 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
▸ isValidAddress(hexAddress
): boolean
Checks if the address is a valid. Accepts checksummed addresses too.
Name | Type |
---|---|
hexAddress |
string |
boolean
packages/util/src/account.ts:132
▸ 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? |
BigIntLike |
boolean
packages/util/src/account.ts:187
▸ 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:241
▸ 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:251
▸ isValidSignature(v
, r
, s
, homesteadOrLater?
, chainId?
): boolean
Validate a ECDSA signature.
NOTE: Accepts v === 0 | v === 1
for EIP1559 transactions
Name | Type | Default value | Description |
---|---|---|---|
v |
bigint |
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? |
bigint |
undefined |
- |
boolean
packages/util/src/signature.ts:149
▸ isZeroAddress(hexAddress
): boolean
Checks if a given address is the zero address.
Name | Type |
---|---|
hexAddress |
string |
boolean
packages/util/src/account.ts:336
▸ padToEven(value
): string
Pads a String
to have an even length
Name | Type |
---|---|
value |
string |
string
output
packages/util/src/internal.ts:56
▸ 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:309
▸ 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:299
▸ 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:282
▸ publicToAddress(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:282
▸ 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)
▸ 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)
▸ short(buffer
, maxLength?
): string
Shortens a string or buffer's hex string representation to maxLength (default 50).
Examples:
Input: '657468657265756d000000000000000000000000000000000000000000000000' Output: '657468657265756d0000000000000000000000000000000000…'
Name | Type | Default value |
---|---|---|
buffer |
string | Buffer |
undefined |
maxLength |
number |
50 |
string
packages/util/src/bytes.ts:274
▸ 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
▸ toBuffer(v
): Buffer
Attempts to turn a value into a Buffer
.
Inputs supported: Buffer
, String
(hex-prefixed), Number
, null/undefined, BigInt
and other objects
with a toArray()
or toBuffer()
method.
Name | Type | Description |
---|---|---|
v |
ToBufferInputTypes |
the value |
Buffer
packages/util/src/bytes.ts:154
▸ 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? |
BigIntLike |
string
packages/util/src/account.ts:154
▸ toCompactSig(v
, r
, s
, chainId?
): string
Convert signature parameters into the format of Compact Signature Representation (EIP-2098).
NOTE: Accepts v === 0 | v === 1
for EIP1559 transactions
Name | Type |
---|---|
v |
bigint |
r |
Buffer |
s |
Buffer |
chainId? |
bigint |
string
Signature
packages/util/src/signature.ts:89
▸ toRpcSig(v
, r
, s
, chainId?
): string
Convert signature parameters into the format of eth_sign
RPC method.
NOTE: Accepts v === 0 | v === 1
for EIP1559 transactions
Name | Type |
---|---|
v |
bigint |
r |
Buffer |
s |
Buffer |
chainId? |
bigint |
string
Signature
packages/util/src/signature.ts:74
▸ 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
▸ toType<T
>(input
, outputType
): undefined
Name | Type |
---|---|
T |
extends TypeOutput |
Name | Type |
---|---|
input |
undefined |
outputType |
T |
undefined
▸ toType<T
>(input
, outputType
): TypeOutputReturnType
[T
]
Name | Type |
---|---|
T |
extends TypeOutput |
Name | Type |
---|---|
input |
ToBufferInputTypes |
outputType |
T |
▸ toUnsigned(num
): Buffer
Converts a BigInt
to an unsigned integer and returns it as a Buffer
. Assumes 256-bit numbers.
Name | Type |
---|---|
num |
bigint |
Buffer
packages/util/src/bytes.ts:251
▸ 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:299
▸ 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
▸ unpadBuffer(a
): Buffer
Trims leading zeros from a Buffer
.
Name | Type | Description |
---|---|---|
a |
Buffer |
(Buffer) |
Buffer
(Buffer)
packages/util/src/bytes.ts:110
▸ 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
▸ 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:340
▸ zeroAddress(): string
Returns the zero address.
string
packages/util/src/account.ts:327
▸ zeros(bytes
): Buffer
Returns a buffer filled with 0s.
Name | Type | Description |
---|---|---|
bytes |
number |
the number of bytes the buffer should be |
Buffer