Skip to content

Latest commit

 

History

History
2008 lines (1161 loc) · 43.8 KB

README.md

File metadata and controls

2008 lines (1161 loc) · 43.8 KB

@ethereumjs/util

@ethereumjs/util

Table of contents

Namespaces

Enumerations

Classes

Interfaces

Type Aliases

Variables

Functions

Type Aliases

AccountBodyBuffer

Ƭ AccountBodyBuffer: [Buffer, Buffer, Buffer | Uint8Array, Buffer | Uint8Array]

Defined in

packages/util/src/account.ts:30


AddressLike

Ƭ AddressLike: Address | Buffer | PrefixedHexString

A type that represents an input that can be converted to an Address.

Defined in

packages/util/src/types.ts:32


BigIntLike

Ƭ BigIntLike: bigint | PrefixedHexString | number | Buffer

Defined in

packages/util/src/types.ts:10


BufferLike

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

Defined in

packages/util/src/types.ts:15


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:27


ToBufferInputTypes

Ƭ ToBufferInputTypes: PrefixedHexString | number | bigint | 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 bigint
2 Buffer
3 PrefixedHexString

Defined in

packages/util/src/types.ts:63


WithdrawalBuffer

Ƭ WithdrawalBuffer: [Buffer, Buffer, Buffer, Buffer]

Defined in

packages/util/src/withdrawal.ts:29


WithdrawalData

Ƭ WithdrawalData: Object

Flexible input data type for EIP-4895 withdrawal data with amount in Gwei to match CL representation and for eventual ssz withdrawalsRoot

Type declaration

Name Type
address AddressLike
amount BigIntLike
index BigIntLike
validatorIndex BigIntLike

Defined in

packages/util/src/withdrawal.ts:11

Variables

GWEI_TO_WEI

Const GWEI_TO_WEI: bigint

Easy conversion from Gwei to wei

Defined in

packages/util/src/units.ts:2


KECCAK256_NULL

Const KECCAK256_NULL: Buffer

Keccak-256 hash of null

Defined in

packages/util/src/constants.ts:39


KECCAK256_NULL_S

Const KECCAK256_NULL_S: "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"

Keccak-256 hash of null

Defined in

packages/util/src/constants.ts:34


KECCAK256_RLP

Const KECCAK256_RLP: Buffer

Keccak-256 hash of the RLP of null

Defined in

packages/util/src/constants.ts:60


KECCAK256_RLP_ARRAY

Const KECCAK256_RLP_ARRAY: Buffer

Keccak-256 of an RLP of an empty array

Defined in

packages/util/src/constants.ts:50


KECCAK256_RLP_ARRAY_S

Const KECCAK256_RLP_ARRAY_S: "1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"

Keccak-256 of an RLP of an empty array

Defined in

packages/util/src/constants.ts:44


KECCAK256_RLP_S

Const KECCAK256_RLP_S: "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"

Keccak-256 hash of the RLP of null

Defined in

packages/util/src/constants.ts:55


MAX_INTEGER

Const MAX_INTEGER: bigint

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

Defined in

packages/util/src/constants.ts:12


MAX_INTEGER_BIGINT

Const MAX_INTEGER_BIGINT: bigint

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

Defined in

packages/util/src/constants.ts:19


MAX_UINT64

Const MAX_UINT64: bigint

2^64-1

Defined in

packages/util/src/constants.ts:7


MAX_WITHDRAWALS_PER_PAYLOAD

Const MAX_WITHDRAWALS_PER_PAYLOAD: 16

Defined in

packages/util/src/constants.ts:67


RLP_EMPTY_STRING

Const RLP_EMPTY_STRING: Buffer

RLP encoded empty string

Defined in

packages/util/src/constants.ts:65


SECP256K1_ORDER

Const SECP256K1_ORDER: bigint = CURVE.n

Defined in

packages/util/src/constants.ts:21


SECP256K1_ORDER_DIV_2

Const SECP256K1_ORDER_DIV_2: bigint

Defined in

packages/util/src/constants.ts:22


TWO_POW256

Const TWO_POW256: bigint

2^256

Defined in

packages/util/src/constants.ts:27

Functions

accountBodyFromSlim

accountBodyFromSlim(body): (Uint8Array | Buffer)[]

Parameters

Name Type
body AccountBodyBuffer

Returns

(Uint8Array | Buffer)[]

Defined in

packages/util/src/account.ts:347


accountBodyToRLP

accountBodyToRLP(body, couldBeSlim?): Buffer

Converts a slim account RLP to a normal account RLP

Parameters

Name Type Default value
body AccountBodyBuffer undefined
couldBeSlim boolean true

Returns

Buffer

Defined in

packages/util/src/account.ts:371


accountBodyToSlim

accountBodyToSlim(body): (Uint8Array | Buffer)[]

Parameters

Name Type
body AccountBodyBuffer

Returns

(Uint8Array | Buffer)[]

Defined in

packages/util/src/account.ts:358


addHexPrefix

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:258


arrToBufArr

arrToBufArr(arr): Buffer

Converts a Uint8Array or NestedUint8Array to Buffer or NestedBufferArray

Parameters

Name Type
arr Uint8Array

Returns

Buffer

Defined in

packages/util/src/bytes.ts:351

arrToBufArr(arr): NestedBufferArray

Parameters

Name Type
arr NestedUint8Array

Returns

NestedBufferArray

Defined in

packages/util/src/bytes.ts:352

arrToBufArr(arr): Buffer | NestedBufferArray

Parameters

Name Type
arr Uint8Array | NestedUint8Array

Returns

Buffer | NestedBufferArray

Defined in

packages/util/src/bytes.ts:353


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

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:315


bigIntToBuffer

bigIntToBuffer(num): Buffer

Converts a bigint to a Buffer

Parameters

Name Type
num bigint

Returns

Buffer

Defined in

packages/util/src/bytes.ts:224


bigIntToHex

bigIntToHex(num): string

Converts a bigint to a 0x prefixed hex string

Parameters

Name Type
num bigint

Returns

string

Defined in

packages/util/src/bytes.ts:377


bigIntToUnpaddedBuffer

bigIntToUnpaddedBuffer(value): Buffer

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

Parameters

Name Type Description
value bigint value to convert

Returns

Buffer

Defined in

packages/util/src/bytes.ts:386


bufArrToArr

bufArrToArr(arr): Uint8Array

Converts a Buffer or NestedBufferArray to Uint8Array or NestedUint8Array

Parameters

Name Type
arr Buffer

Returns

Uint8Array

Defined in

packages/util/src/bytes.ts:364

bufArrToArr(arr): NestedUint8Array

Parameters

Name Type
arr NestedBufferArray

Returns

NestedUint8Array

Defined in

packages/util/src/bytes.ts:365

bufArrToArr(arr): Uint8Array | NestedUint8Array

Parameters

Name Type
arr Buffer | NestedBufferArray

Returns

Uint8Array | NestedUint8Array

Defined in

packages/util/src/bytes.ts:366


bufferToBigInt

bufferToBigInt(buf): bigint

Converts a Buffer to a bigint

Parameters

Name Type
buf Buffer

Returns

bigint

Defined in

packages/util/src/bytes.ts:213


bufferToHex

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:205


bufferToInt

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:233


ecrecover

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

ECDSA public key recovery from signature. NOTE: Accepts v === 0 | v === 1 for EIP1559 transactions

Parameters

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

Returns

Buffer

Recovered public key

Defined in

packages/util/src/signature.ts:52


ecsign

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

Parameters

Name Type
msgHash Buffer
privateKey Buffer
chainId? bigint

Returns

ECDSASignature

Defined in

packages/util/src/signature.ts:20


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

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.

Parameters

Name Type
sig string

Returns

ECDSASignature

Defined in

packages/util/src/signature.ts:112


fromSigned

fromSigned(num): bigint

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

Parameters

Name Type Description
num Buffer Signed integer value

Returns

bigint

Defined in

packages/util/src/bytes.ts:243


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

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:199


generateAddress2

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:219


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']

@param params @param key @param allowEmpty @returns output just a simple array of output keys

Parameters

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

Returns

string[]

Defined in

packages/util/src/internal.ts:171


hashPersonalMessage

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:189


importPublic

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:316


intToBuffer

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

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


intToUnpaddedBuffer

intToUnpaddedBuffer(value): Buffer

Parameters

Name Type
value number

Returns

Buffer

Defined in

packages/util/src/bytes.ts:390


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

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:132


isValidChecksumAddress

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? BigIntLike

Returns

boolean

Defined in

packages/util/src/account.ts:187


isValidPrivate

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:241


isValidPublic

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:251


isValidSignature

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

Validate a ECDSA signature. NOTE: Accepts v === 0 | v === 1 for EIP1559 transactions

Parameters

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 -

Returns

boolean

Defined in

packages/util/src/signature.ts:149


isZeroAddress

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:336


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

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:309


privateToPublic

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:299


pubToAddress

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:282


publicToAddress

publicToAddress(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:282


setLengthLeft

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

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


short

short(buffer, maxLength?): string

Shortens a string or buffer's hex string representation to maxLength (default 50).

Examples:

Input: '657468657265756d000000000000000000000000000000000000000000000000' Output: '657468657265756d0000000000000000000000000000000000…'

Parameters

Name Type Default value
buffer string | Buffer undefined
maxLength number 50

Returns

string

Defined in

packages/util/src/bytes.ts:274


stripHexPrefix

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

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.

Parameters

Name Type Description
v ToBufferInputTypes the value

Returns

Buffer

Defined in

packages/util/src/bytes.ts:154


toChecksumAddress

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? BigIntLike

Returns

string

Defined in

packages/util/src/account.ts:154


toCompactSig

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

Parameters

Name Type
v bigint
r Buffer
s Buffer
chainId? bigint

Returns

string

Signature

Defined in

packages/util/src/signature.ts:89


toRpcSig

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

Parameters

Name Type
v bigint
r Buffer
s Buffer
chainId? bigint

Returns

string

Signature

Defined in

packages/util/src/signature.ts:74


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:76

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:77

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:78


toUnsigned

toUnsigned(num): Buffer

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

Parameters

Name Type
num bigint

Returns

Buffer

Defined in

packages/util/src/bytes.ts:251


toUtf8

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:299


unpadArray

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

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

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

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:340


zeroAddress

zeroAddress(): string

Returns the zero address.

Returns

string

Defined in

packages/util/src/account.ts:327


zeros

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