ethers
beta-exports.16
DOCUMENTATION
Getting Started
Ethereum Basics
Application Programming Interface
Application Binary Interface
Addresses
Constants
Contracts
Cryptographic Functions
Hashing Utilities
Providers
Transactions
Utilities
Base58 Encoding
Base64 Encoding
Data Helpers
Math Helpers
Properties
Recursive-Length Prefix
Strings and UTF-8
Unit Conversion
UUID
Errors
Events
Fetching Web Content
Fixed-Point Maths
Wallets
Wordlists
Migrating from v5
Contributings and Hacking
License and Copyright
Documentation »API »Utilities
 Utilities

There are many simple utilities required to interact with Ethereum and to simplify the library, without increasing the library dependencies for simple functions.

  • Base58 Encoding
  • Base64 Encoding
  • Data Helpers
  • Math Helpers
  • Properties
  • Recursive-Length Prefix
  • Strings and UTF-8
  • Unit Conversion
  • UUID
 Base58 Encoding

The Base58 Encoding scheme allows a numeric value to be encoded as a compact string using a radix of 58 using only alpha-numeric characters. Confusingly similar characters are omitted (i.e. "l0O").

Note that Base58 encodes a numeric value, not arbitrary bytes, since any zero-bytes on the left would get removed. To mitigate this issue most schemes that use Base58 choose specific high-order values to ensure non-zero prefixes.

 FUNCTIONS
 <src>decodeBase58(value: string)⇒ bigint

Decode the Base58-encoded value.

 <src>encodeBase58(value: BytesLike)⇒ string

Encode value as a Base58-encoded string.

 Base64 Encoding

Base64 encoding using 6-bit words to encode arbitrary bytes into a string using 65 printable symbols, the upper-case and lower-case alphabet, the digits 0 through 9, "+" and "/" with the "=" used for padding.

 FUNCTIONS
 <src>decodeBase64(value: string)⇒ Uint8Array

Decodes the base-64 encoded value.

 <src>encodeBase64(data: BytesLike)⇒ string

Encodes data as a base-64 encoded string.

 Data Helpers

Some data helpers.

 TYPES
 <src>BytesLike⇒ DataHexString | Uint8Array

An object that can be used to represent binary data.

 <src>DataHexString⇒ string

A HexString whose length is even, which ensures it is a valid representation of binary data.

 <src>HexString⇒ string

A string which is prefixed with 0x and followed by any number of case-agnostic hexadecimal characters.

It must match the regular expression /0x[0-9A-Fa-f]*/.

 FUNCTIONS
 <src>concat(datas: ReadonlyArray< BytesLike >)⇒ string

Returns a DataHexString by concatenating all values within data.

 <src>dataLength(data: BytesLike)⇒ number

Returns the length of data, in bytes.

 <src>dataSlice(data: BytesLike, start?: number, end?: number)⇒ string

Returns a DataHexString by slicing data from the start offset to the end offset.

By default start is 0 and end is the length of data.

 <src>getBytes(value: BytesLike, name?: string)⇒ Uint8Array

Get a typed Uint8Array for value. If already a Uint8Array the original value is returned; if a copy is required use getBytesCopy.

 <src>getBytesCopy(value: BytesLike, name?: string)⇒ Uint8Array

Get a typed Uint8Array for value, creating a copy if necessary to prevent any modifications of the returned value from being reflected elsewhere.

 <src>hexlify(data: BytesLike)⇒ string

Returns a DataHexString representation of data.

 <src>isBytesLike(value: any)⇒ boolean

Returns true if value is a valid representation of arbitrary data (i.e. a valid DataHexString or a Uint8Array).

 <src>isHexString(value: any, length?: number | boolean)⇒ boolean

Returns true if value is a valid HexString.

If length is true or a number, it also checks that value is a valid DataHexString of length (if a number) bytes of data (e.g. 0x1234 is 2 bytes).

 <src>stripZerosLeft(data: BytesLike)⇒ string

Return the DataHexString result by stripping all leading * zero bytes from data.

 <src>zeroPadBytes(data: BytesLike, length: number)⇒ string

Return the DataHexString of data padded on the right to length bytes.

If data already exceeds length, a BufferOverrun is thrown.

This pads data the same as bytes are in Solidity (e.g. bytes16).

 <src>zeroPadValue(data: BytesLike, length: number)⇒ string

Return the DataHexString of data padded on the left to length bytes.

If data already exceeds length, a BufferOverrun is thrown.

This pads data the same as values are in Solidity (e.g. uint128).

 Math Helpers

Some mathematic operations.

 TYPES
 <src>BigNumberish⇒ string | Numeric

Any type that can be used where a big number is needed.

 <src>Numeric⇒ number | bigint

Any type that can be used where a numeric value is needed.

 FUNCTIONS
 <src>fromTwos(value: BigNumberish, width: Numeric)⇒ bigint

Convert value from a twos-compliment representation of width bits to its value.

If the highest bit is 1, the result will be negative.

 <src>getBigInt(value: BigNumberish, name?: string)⇒ bigint

Gets a BigInt from value. If it is an invalid value for a BigInt, then an ArgumentError will be thrown for name.

 <src>getNumber(value: BigNumberish, name?: string)⇒ number

Gets a number from value. If it is an invalid value for a number, then an ArgumentError will be thrown for name.

 <src>getUint(value: BigNumberish, name?: string)⇒ bigint
 <src>mask(value: BigNumberish, bits: Numeric)⇒ bigint

Mask value with a bitmask of bits ones.

 <src>toBeArray(value: BigNumberish)⇒ Uint8Array

Converts value to a Big Endian Uint8Array.

 <src>toBeHex(value: BigNumberish, width?: Numeric)⇒ string

Converts value to a Big Endian hexstring, optionally padded to width bytes.

 <src>toBigInt(value: BigNumberish | Uint8Array)⇒ bigint
 <src>toNumber(value: BigNumberish | Uint8Array)⇒ number

Converts value to a number. If value is a Uint8Array, it is treated as Big Endian data. Throws if the value is not safe.

 <src>toQuantity(value: BytesLike | BigNumberish)⇒ string

Returns a HexString for value safe to use as a Quantity.

A Quantity does not have and leading 0 values unless the value is the literal value `0x0`. This is most commonly used for JSSON-RPC numeric values.

 <src>toTwos(value: BigNumberish, width: Numeric)⇒ bigint

Convert value to a twos-compliment representation of width bits.

The result will always be positive.

 Properties

Property helper functions.

 FUNCTIONS
 <src>defineProperties(target: T, values: TODO(A4BRecord<@TODO-005: @TODO-006>), types?: TODO(A4BRecord<@TODO-005: @TODO-006>))⇒ void

Assigns the values to target as read-only values.

It types is specified, the values are checked.

 <src>resolveProperties(value: TODO(A4BRecord<@TODO-005: @TODO-006>))⇒ Promise< T >

Resolves to a new object that is a copy of value, but with all values resolved.

 Recursive-Length Prefix

The Recursive-Length Prefix (RLP) encoding is used throughout Ethereum to serialize nested structures of Arrays and data.

 TYPES
 <src>RlpStructuredData⇒ string | Array< RlpStructuredData >

An RLP-encoded structure.

 FUNCTIONS
 <src>decodeRlp(data: BytesLike)⇒ RlpStructuredData

Decodes data into the structured data it represents.

 <src>encodeRlp(object: RlpStructuredData)⇒ string

Encodes object as an RLP-encoded DataHexString.

 Strings and UTF-8

Using strings in Ethereum (or any security-basd system) requires additional care. These utilities attempt to mitigate some of the safety issues as well as provide the ability to recover and analyse strings.

 TYPES
 <src>UnicodeNormalizationForm⇒ "NFC" | "NFD" | "NFKC" | "NFKD"

The stanard normalization forms.

 <src>Utf8ErrorFunc⇒ (reason: Utf8ErrorReason, offset: number, bytes: Uint8Array, output: Array< number >, badCodepoint?: number) => number

A callback that can be used with toUtf8String to analysis or recovery from invalid UTF-8 data.

Parsing UTF-8 data is done through a simple Finite-State Machine (FSM) which calls the Utf8ErrorFunc if a fault is detected.

The reason indicates where in the FSM execution the fault occurred and the offset indicates where the input failed.

The bytes represents the raw UTF-8 data that was provided and output is the current array of UTF-8 code-points, which may be updated by the Utf8ErrorFunc.

The value of the badCodepoint depends on the reason. See Utf8ErrorReason for details.

The function should return the number of bytes that should be skipped when control resumes to the FSM.

 <src>Utf8ErrorReason⇒ string

When using the UTF-8 error API the following errors can be intercepted and processed as the reason passed to the Utf8ErrorFunc.

"UNEXPECTED_CONTINUE" - a continuation byte was present where there was nothing to continue.

"BAD_PREFIX" - an invalid (non-continuation) byte to start a UTF-8 codepoint was found.

"OVERRUN" - the string is too short to process the expected codepoint length.

"MISSING_CONTINUE" - a missing continuation byte was expected but not found. The offset indicates the index the continuation byte was expected at.

"OUT_OF_RANGE" - the computed code point is outside the range for UTF-8. The badCodepoint indicates the computed codepoint, which was outside the valid UTF-8 range.

"UTF16_SURROGATE" - the UTF-8 strings contained a UTF-16 surrogate pair. The badCodepoint is the computed codepoint, which was inside the UTF-16 surrogate range.

"OVERLONG" - the string is an overlong representation. The badCodepoint indicates the computed codepoint, which has already been bounds checked.

 CONSTANTS
 <src>Utf8ErrorFuncs⇒ Readonly< Record< "error" | "ignore" | "replace", Utf8ErrorFunc > >
 FUNCTIONS
 <src>toUtf8Bytes(str: string, form?: UnicodeNormalizationForm)⇒ Uint8Array

Returns the UTF-8 byte representation of str.

If form is specified, the string is normalized.

 <src>toUtf8CodePoints(str: string, form?: UnicodeNormalizationForm)⇒ Array< number >

Returns the UTF-8 code-points for str.

If form is specified, the string is normalized.

 <src>toUtf8String(bytes: BytesLike, onError?: Utf8ErrorFunc)⇒ string

Returns the string represented by the UTF-8 data bytes.

When onError function is specified, it is called on UTF-8 errors allowing recovery using the Utf8ErrorFunc API. (default: error)

 Unit Conversion

Most interactions with Ethereum requires integer values, which use the smallest magnitude unit.

For example, imagine dealing with dollars and cents. Since dollars are divisible, non-integer values are possible, such as $10.77. By using the smallest indivisible unit (i.e. cents), the value can be kept as the integer 1077.

When receiving decimal input from the user (as a decimal string), the value should be converted to an integer and when showing a user a value, the integer value should be converted to a decimal string.

This creates a clear distinction, between values to be used by code (integers) and values used for display logic to users (decimals).

The native unit in Ethereum, ether is divisible to 18 decimal places, where each individual unit is called a wei.

 FUNCTIONS
 <src>formatEther(wei: BigNumberish)⇒ string

Converts value into a decimal string using 18 decimal places.

 <src>formatUnits(value: BigNumberish, unit?: string | Numeric)⇒ string

Converts value into a decimal string, assuming unit decimal places. The unit may be the number of decimal places or the name of a unit (e.g. "gwei" for 9 decimal places).

 <src>parseEther(ether: string)⇒ bigint

Converts the decimal string ether to a BigInt, using 18 decimal places.

 <src>parseUnits(value: string, unit?: string | Numeric)⇒ bigint

Converts the decimal string value to a BigInt, assuming unit decimal places. The unit may the number of decimal places or the name of a unit (e.g. "gwei" for 9 decimal places).

 UUID

Explain UUID and link to RFC here.

 FUNCTIONS
 <src>uuidV4(randomBytes: BytesLike)⇒ string

Returns the version 4 UUID for the randomBytes.

← Transactions
Errors→
The content of this site is licensed under the Creative Commons License. Generated on December 30, 2022, 4:28pm.