Module sui::address_alias
- Struct AddressAliasState
- Struct AddressAliases
- Struct AliasKey
- Constants
- Function create
- Function enable
- Function add
- Function replace_all
- Function remove
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::derived_object;
use sui::dynamic_field;
use sui::hex;
use sui::object;
use sui::party;
use sui::transfer;
use sui::tx_context;
use sui::vec_map;
use sui::vec_set;
Struct AddressAliasState
Singleton shared object which manages creation of AddressAliases state. The actual alias configs are created as derived objects with this object as the parent.
public struct AddressAliasState has key
Fields
-
id: sui::object::UID -
version: u64
Struct AddressAliases
Tracks the set of addresses allowed to act as a given sender.
An alias allows transactions signed by the alias address to act as the original address. For example, if address X sets an alias of address Y, then then a transaction signed by Y can set its sender address to X.
public struct AddressAliases has key
Fields
-
id: sui::object::UID -
aliases: sui::vec_set::VecSet<address>
Struct AliasKey
Internal key used for derivation of AddressAliases object addresses.
public struct AliasKey has copy, drop, store
Fields
-
0: address
Constants
#[error]
const ENotSystemAddress: vector<u8> = b"Only the system can create the alias state object.";
#[error]
const ENoSuchAlias: vector<u8> = b"Given alias does not exist.";
#[error]
const EAliasAlreadyExists: vector<u8> = b"Alias already exists.";
#[error]
const ECannotRemoveLastAlias: vector<u8> = b"Cannot remove the last alias.";
#[error]
const ETooManyAliases: vector<u8> = b"The number of aliases exceeds the maximum allowed.";
const CURRENT_VERSION: u64 = 0;
const MAX_ALIASES: u64 = 8;
Function create
Create and share the AddressAliasState object. This function is called exactly once, when the address alias state object is first created. Can only be called by genesis or change_epoch transactions.
fun create(ctx: &sui::tx_context::TxContext)
Implementation
fun create(ctx: &TxContext) {
assert!(ctx.sender() == @0x0, ENotSystemAddress);
let self = AddressAliasState {
id: object::address_alias_state(),
version: CURRENT_VERSION,
};
transfer::share_object(self);
}
Function enable
Enables address alias configuration for the sender address.
By default, an address is its own alias. The provided
AddressAliases
object can be used to change the set of allowed aliases after enabling.
entry fun enable(address_alias_state: &mut sui::address_alias::AddressAliasState, ctx: &sui::tx_context::TxContext)
Implementation
entry fun enable(address_alias_state: &mut AddressAliasState, ctx: &TxContext) {
assert!(
!derived_object::exists(&address_alias_state.id, AliasKey(ctx.sender())),
EAliasAlreadyExists,
);
transfer::party_transfer(
AddressAliases {
id: derived_object::claim(&mut address_alias_state.id, AliasKey(ctx.sender())),
aliases: vec_set::singleton(ctx.sender()),
},
party::single_owner(ctx.sender()),
);
}
Function add
Adds the provided address to the set of aliases for the sender.
entry fun add(aliases: &mut sui::address_alias::AddressAliases, alias: address)
Implementation
entry fun add(aliases: &mut AddressAliases, alias: address) {
assert!(!aliases.aliases.contains(&alias), EAliasAlreadyExists);
aliases.aliases.insert(alias);
assert!(aliases.aliases.length() <= MAX_ALIASES, ETooManyAliases);
}
Function replace_all
Overwrites the aliases for the sender's address with the given set.
entry fun replace_all(aliases: &mut sui::address_alias::AddressAliases, new_aliases: vector<address>)
Implementation
entry fun replace_all(aliases: &mut AddressAliases, new_aliases: vector<address>) {
let new_aliases = vec_set::from_keys(new_aliases);
assert!(new_aliases.length() > 0, ECannotRemoveLastAlias);
assert!(new_aliases.length() <= MAX_ALIASES, ETooManyAliases);
aliases.aliases = new_aliases;
}
Function remove
Removes the given alias from the set of aliases for the sender's address.
entry fun remove(aliases: &mut sui::address_alias::AddressAliases, alias: address)
Implementation
entry fun remove(aliases: &mut AddressAliases, alias: address) {
assert!(aliases.aliases.contains(&alias), ENoSuchAlias);
assert!(aliases.aliases.length() > 1, ECannotRemoveLastAlias);
aliases.aliases.remove(&alias);
}