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.

    Parameters

    • input: Uint8Array

    Returns AccountAddress

Properties

data: Uint8Array

This is the internal representation of an account address.

LENGTH: number

The number of bytes that make up an account address.

LONG_STRING_LENGTH: number

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

Methods

  • Serializes a Serializable value to its BCS representation. This function is the Typescript SDK equivalent of bcs::to_bytes in Move.

    Returns Uint8Array

    the BCS representation of the Serializable instance as a byte buffer

  • Helper function to get a value's BCS-serialized bytes as a Hex instance.

    Returns Hex

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

  • Return whether AccountAddresses are equal. AccountAddresses are considered equal if their underlying byte data is identical.

    Parameters

    Returns boolean

    true if the AccountAddresses are equal, false if not.

  • Returns whether an address is special, where special is defined as 0x0 to 0xf inclusive. In other words, the last byte of the address must be < 0b10000 (16) and every other byte must be zero.

    For more information on how special addresses are defined see AIP-40: https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-40.md.

    Returns boolean

    true if the address is special, 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.
  • Serialize an argument as a type-agnostic, fixed byte sequence. The byte sequence contains the number of the following bytes followed by the BCS-serialized bytes for a typed argument.

    Parameters

    Returns void

  • Serialize an argument to BCS-serialized bytes as a type aware byte sequence. The byte sequence contains an enum variant index followed by the BCS-serialized bytes for a typed argument.

    Parameters

    Returns void

  • Return the AccountAddress as a string as per AIP-40. https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-40.md.

    In short, it means that special addresses are represented in SHORT form, meaning 0x0 through to 0xf inclusive, and every other address is represented in LONG form, meaning 0x + 64 hex characters.

    Returns `0x${string}`

    AccountAddress as a string conforming to AIP-40.

  • NOTE: Prefer to use toString where possible.

    Whereas toString will format special addresses (as defined by isSpecial) using the SHORT form (no leading 0s), this format the address in the LONG format unconditionally.

    This means it will be 0x + 64 hex characters.

    Returns `0x${string}`

    AccountAddress as a string in LONG form.

  • NOTE: Prefer to use toString where possible.

    Whereas toString will format special addresses (as defined by isSpecial) using the SHORT form (no leading 0s), this function will include leading zeroes. The string will not have a leading zero.

    This means it will be 64 hex characters without a leading 0x.

    Returns string

    AccountAddress as a string in LONG form without a leading 0x.

  • NOTE: Prefer to use toString where possible.

    Return the AccountAddress as a string as per AIP-40 but without the leading 0x.

    Learn more by reading the docstring of toString.

    Returns string

    AccountAddress as a string conforming to AIP-40 but without the leading 0x.

  • Get the inner hex data. The inner data is already a Uint8Array so no conversion is taking place here, it just returns the inner data.

    Returns Uint8Array

    Hex data as Uint8Array

  • Deserialize an AccountAddress from the byte buffer in a Deserializer instance.

    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 all known inputs.

    This handles, Uint8array, string, and AccountAddress itself

    Parameters

    Returns AccountAddress

  • Convenience method for creating an AccountAddress from all known inputs.

    This handles, Uint8array, string, and AccountAddress itself

    Parameters

    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 is only provided for backwards compatibility.

    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.
    • 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.

    Returns AccountAddress

    An instance of AccountAddress.

  • 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.

    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.

    Returns AccountAddress

    An instance of AccountAddress.

  • Check if the string is a valid AccountAddress.

    Parameters

    • args: {
          input: AccountAddressInput;
          strict?: boolean;
      }
      • 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>

    valid = true if the string is valid, valid = false if not. If the string is not valid, invalidReason will be set explaining why it is invalid.