Skip to content

AccountAddress

MarcoDotIO edited this page Jun 30, 2023 · 3 revisions

AccountAddress

The Aptos Blockchain Account Address

public struct AccountAddress: KeyProtocol, Equatable, CustomStringConvertible, Hashable 

Inheritance

CustomStringConvertible, Equatable, Hashable, KeyProtocol

Initializers

init(address:)

public init(address: Data) throws 

Properties

address

The address data itself

public let address: Data

description

public var description: String 

Methods

hex()

Gives the hex value of the address

public func hex() -> String 

Returns

A String value represnting the address's hex value

fromHex(_:)

Create an AccountAddress instance from a hexadecimal string.

public static func fromHex(_ address: String) throws -> AccountAddress 

This function creates an AccountAddress instance from a hexadecimal string representing the account address. If the provided hexadecimal string starts with "0x", the function removes it before attempting to create the AccountAddress instance. If the length of the hexadecimal string is less than AccountAddress.length times 2, the function pads it with leading zeros to reach the required length.

Parameters

  • address: A string representing the hexadecimal account address to create the AccountAddress instance from.

Throws

An error of type AptosError indicating that the provided hexadecimal string is invalid and cannot be converted to an AccountAddress instance.

Returns

An AccountAddress instance created from the provided hexadecimal string.

fromKey(_:)

Create an AccountAddress instance from a PublicKey.

public static func fromKey(_ key: PublicKey) throws -> AccountAddress 

This function creates an AccountAddress instance from a provided PublicKey. The function generates a new address by appending the ED25519 authorization key scheme value to the byte representation of the provided public key, and then computes the SHA3-256 hash of the resulting byte array. The resulting hash is used to create a new AccountAddress instance.

Parameters

  • key: A PublicKey instance representing the public key to create the AccountAddress instance from.

Throws

An error of type AptosError indicating that the provided PublicKey is invalid and cannot be converted to an AccountAddress instance.

Returns

An AccountAddress instance created from the provided PublicKey.

fromMultiEd25519(keys:)

Create an AccountAddress instance from a MultiPublicKey.

public static func fromMultiEd25519(keys: MultiPublicKey) throws -> AccountAddress 

This function creates an AccountAddress instance from a provided MultiPublicKey. The function generates a new address by appending the Multi ED25519 authorization key scheme value to the byte representation of the provided MultiPublicKey, and then computes the SHA3-256 hash of the resulting byte array. The resulting hash is used to create a new AccountAddress instance.

Parameters

  • keys: A MultiPublicKey instance representing the multiple public keys to create the AccountAddress instance from.

Throws

An error of type AptosError indicating that the provided MultiPublicKey is invalid and cannot be converted to an AccountAddress instance.

Returns

An AccountAddress instance created from the provided MultiPublicKey.

forResourceAccount(_:seed:)

Create an AccountAddress instance for a resource account.

public static func forResourceAccount(_ creator: AccountAddress, seed: Data) throws -> AccountAddress 

This function creates an AccountAddress instance for a resource account given the creator's address and a seed value. The function generates a new address by concatenating the byte representation of the creator's address, the provided seed value, and the DERIVE_RESOURCE_ACCOUNT_ADDRESS authorization key scheme value. It then computes the SHA3-256 hash of the resulting byte array to generate a new AccountAddress instance.

Parameters

  • creator: An AccountAddress instance representing the address of the account that will create the resource account.
  • seed: A Data value used to create a unique resource account.

Throws

An error of type AptosError indicating that the provided creator address or seed is invalid and cannot be used to create a resource account.

Returns

An AccountAddress instance representing the newly created resource account.

forGuidObject(_:_:)

Generates an AccountAddress for a GUID object.

public static func forGuidObject(_ creator: AccountAddress, _ creationNum: Int) throws -> AccountAddress 

This function takes in a creator address and a creationNum which it uses to serialize into an array of bytes. It then appends the creator address and deriveObjectAddressFromGuid to this array. It uses this byte array to compute a SHA-256 hash. This hash is then returned as a new AccountAddress.

Note: This function uses the SHA-256 algorithm for hashing which is a part of the SHA-2 (Secure Hash Algorithm 2) set of cryptographic hash functions. Hash functions are fundamental to modern cryptography. These functions map binary strings of an arbitrary length to small binary strings of a fixed length.

Parameters

  • creator: The account address of the creator.
  • creationNum: The creation number of the object.

Throws

This function throws an error if the process of creating the AccountAddress fails.

Returns

An AccountAddress which is generated for a GUID object.

forNamedObject(_:seed:)

Create an AccountAddress instance for a named object.

public static func forNamedObject(_ creator: AccountAddress, seed: Data) throws -> AccountAddress 

This function creates an AccountAddress instance for a named object given the creator's address and a seed value. The function generates a new address by concatenating the byte representation of the creator's address, the provided seed value, and the DERIVE_OBJECT_ADDRESS_FROM_SEED authorization key scheme value. It then computes the SHA3-256 hash of the resulting byte array to generate a new AccountAddress instance.

Parameters

  • creator: An AccountAddress instance representing the address of the account that will create the named object.
  • seed: A Data value used to create a unique named object.

Throws

An error of type AptosError indicating that the provided creator address or seed is invalid and cannot be used to create a named object.

Returns

An AccountAddress instance representing the newly created named object.

forNamedToken(_:_:_:)

Generates an AccountAddress for a named token by concatenating the collectionName, the tokenName, and the separator ":​:​" as a Data and calling the forNamedObject function with the resulting Data as the seed.

public static func forNamedToken(_ creator: AccountAddress, _ collectionName: String, _ tokenName: String) throws -> AccountAddress 

Parameters

  • creator: The AccountAddress of the account that creates the token.
  • collectionName: A String that represents the name of the collection to which the token belongs.
  • tokenName: A String that represents the name of the token.

Throws

An error of type AptosError.stringToDataFailure if collectionName, tokenName, or ":​:​" can't be converted into Data.

Returns

An AccountAddress object that represents the named token.

forNamedCollection(_:_:)

Derive an AccountAddress for a named collection.

public static func forNamedCollection(_ creator: AccountAddress, _ collectionName: String) throws -> AccountAddress 

This function takes the creator's AccountAddress and the name of the collection as a String. The collection name is then converted to data using UTF-8 encoding. The forNamedObject function is called with the creator's address and the collection name data as the seed. This returns an AccountAddress derived from the creator's address and collection name seed, which represents the named collection.

Parameters

  • creator: The creator's AccountAddress.
  • collectionName: The name of the collection as a String.

Throws

An AptosError object of type stringToDataFailure if the conversion of the collection name string to data using UTF-8 encoding fails.

Returns

An AccountAddress that represents the named collection.

deserialize(from:)

public static func deserialize(from deserializer: Deserializer) throws -> AccountAddress 

serialize(_:)

public func serialize(_ serializer: Serializer) throws 
Types
Protocols
Global Variables
Global Functions
Extensions
Clone this wiki locally