NOTE: Only use this class for account addresses. For other hex data, e.g. transaction hashes, use the Hex class.

AccountAddress is used for working with account addresses. Account addresses, when represented as a string, generally look like these examples:

  • 0x1
  • 0xaa86fe99004361f747f91342ca13c426ca0cccb0c1217677180c9493bad6ef0c

Proper formatting and parsing of account addresses is defined by AIP-40. To learn more about the standard, read the AIP here: https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-40.md.

The comments in this class make frequent reference to the LONG and SHORT formats, as well as "special" addresses. To learn what these refer to see AIP-40.

Hierarchy (view full)

Implements

Constructors

  • Creates an instance of AccountAddress from a Uint8Array.

    This function ensures that the input data is exactly 32 bytes long, which is required for a valid account address.

    Parameters

    • input: Uint8Array

      A Uint8Array representing an account address.

    Returns AccountAddress

    ParsingError if the input length is not equal to 32 bytes.

Methods

  • Converts the BCS-serialized bytes of a value into a Hex instance. This function provides a Hex representation of the BCS-serialized data for easier handling and manipulation.

    Returns Hex

    A Hex instance with the BCS-serialized bytes loaded into its underlying Uint8Array.

  • Determine if two AccountAddresses are equal based on their underlying byte data.

    Parameters

    Returns boolean

    true if the AccountAddresses are equal, false if not.

  • Serialize the AccountAddress to a Serializer instance's data buffer.

    Parameters

    • serializer: Serializer

      The serializer to serialize the AccountAddress to.

    Returns void

    void

    const serializer = new Serializer();
    const address = AccountAddress.fromString("0x1");
    address.serialize(serializer);
    const bytes = serializer.toUint8Array();
    // `bytes` is now the BCS-serialized address.
  • Serializes the current instance into a byte sequence suitable for entry functions. This allows for the proper encoding of data when interacting with entry functions in the blockchain.

    Parameters

    • serializer: Serializer

      The serializer instance used to convert the data into bytes.

    Returns void

  • Serializes the current instance for use in a script function by encoding it into a byte sequence. This process involves serializing the variant index and the instance data, making it suitable for transmission.

    Parameters

    • serializer: Serializer

      The serializer instance used to perform the serialization.

    Returns void

  • Convert the account address to a string in LONG format, which is always 0x followed by 64 hex characters.

    NOTE: Prefer to use toString where possible, as it formats special addresses using the SHORT form (no leading 0s).

    Returns `0x${string}`

    AccountAddress as a string in LONG form.

  • Returns the account address as a string in LONG form without a leading 0x. This function will include leading zeroes and will produce a string of 64 hex characters.

    NOTE: Prefer to use toString where possible, as it formats special addresses using the SHORT form (no leading 0s).

    Returns string

    The account address in LONG form.

  • Get the inner data as a Uint8Array. The inner data is already a Uint8Array, so no conversion takes place.

    Returns Uint8Array

    Hex data as Uint8Array

  • Deserialize an AccountAddress from the byte buffer in a Deserializer instance. This function allows you to convert a byte representation of an AccountAddress into an instance of AccountAddress.

    Parameters

    • deserializer: Deserializer

      The deserializer to deserialize the AccountAddress from.

    Returns AccountAddress

    An instance of AccountAddress.

    const bytes = hexToBytes("0x0102030405060708091011121314151617181920212223242526272829303132");
    const deserializer = new Deserializer(bytes);
    const address = AccountAddress.deserialize(deserializer);
    // `address` is now an instance of AccountAddress.
  • Convenience method for creating an AccountAddress from various input types. This function accepts a string, Uint8Array, or an existing AccountAddress instance and returns the corresponding AccountAddress.

    Parameters

    • input: AccountAddressInput

      The input to convert into an AccountAddress. This can be a string representation of an address, a Uint8Array, or an existing AccountAddress.

    • __namedParameters: {
          maxMissingChars?: number;
      } = {}
      • OptionalmaxMissingChars?: number

    Returns AccountAddress

  • NOTE: This function has relaxed parsing behavior. For strict behavior, please use the fromStringStrict function. Where possible use fromStringStrict rather than this function, fromString.

    Creates an instance of AccountAddress from a hex string.

    This function allows all formats defined by AIP-40. In short this means the following formats are accepted:

    • LONG, with or without leading 0x
    • SHORT*, with or without leading 0x

    Where:

    • LONG is 64 hex characters.
    • SHORT* is 1 to 63 hex characters inclusive. The address can have missing values up to maxMissingChars before it is padded.
    • Padding zeroes are allowed, e.g. 0x0123 is valid.

    Learn more about the different address formats by reading AIP-40: https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-40.md.

    Parameters

    • input: string

      A hex string representing an account address.

    • __namedParameters: {
          maxMissingChars?: number;
      } = {}
      • OptionalmaxMissingChars?: number

    Returns AccountAddress

    An instance of AccountAddress.

    ParsingError if the hex string is too short, too long, or contains invalid characters.

  • NOTE: This function has strict parsing behavior. For relaxed behavior, please use the fromString function.

    Creates an instance of AccountAddress from a hex string.

    This function allows only the strictest formats defined by AIP-40. In short this means only the following formats are accepted:

    • LONG
    • SHORT for special addresses

    Where:

    • LONG is defined as 0x + 64 hex characters.
    • SHORT for special addresses is 0x0 to 0xf inclusive without padding zeroes.

    This means the following are not accepted:

    • SHORT for non-special addresses.
    • Any address without a leading 0x.

    Parameters

    • input: string

      A hex string representing an account address.

    Returns AccountAddress

    An instance of AccountAddress.

    If the hex string does not start with 0x or is not in a valid format.

    This function has strict parsing behavior. For relaxed behavior, please use the fromString function.

    AIP-40 documentation for more details on address formats: https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-40.md.

  • Check if the provided input is a valid AccountAddress.

    Parameters

    • args: {
          input: AccountAddressInput;
          strict?: boolean;
      }

      The arguments for validation.

      • input: AccountAddressInput

        A hex string representing an account address.

      • Optionalstrict?: boolean

        If true, use strict parsing behavior; if false, use relaxed parsing behavior.

    Returns ParsingResult<AddressInvalidReason>

    An object indicating whether the address is valid. If valid, valid = true; if not, valid = false with additional details. If the address is invalid, invalidReason will explain why it is invalid, and invalidReasonMessage will provide the error message.

Properties

data: Uint8Array

This is the internal representation of an account address.

FOUR: AccountAddress = ...
LENGTH: number = 32

The number of bytes that make up an account address.

LONG_STRING_LENGTH: number = 64

The length of an address string in LONG form without a leading 0x.

ONE: AccountAddress = ...
THREE: AccountAddress = ...
TWO: AccountAddress = ...
ZERO: AccountAddress = ...