Aliases

General

MystenLabs added aliases support to Sui 1.16.0. Aliases are associations of an alias name to a Sui Address with the intent that you could use an alias name in places where you would otherwise use an address.

pysui enhanced SuiConfiguration to support aliases in 0.41.0.

Runtime

When instantiating a configuration either through SuiConfig.default_config() or SuiConfig.sui_base_config() then the configuration location is searched for a sui.aliases file. If found, the alias associations to addresses are loaded, otherwise aliases are generated for each address and saved out to the file sui.aliases. If generated, this file conforms with what the sui binaries expect.

As SuiConfig.user_config() is purley empemeral, generated aliases are not persisted, however all of the methods added to SuiConfiguration are supported.

New aliases

Support for creating new aliases along with new address/keypair creation has been added for:
  • create_new_keypair_and_address

  • recover_keypair_and_address

  • add_keypair_from_keystring

If an alias name is not provided when calling these, one will be automatically generated.

Use for lookup and substitutions

A number of associative lookups have been added to SuiConfiguration:
  • addr4al - Given an alias name return the associated SuiAddress

  • kp4al - Given an alias name return the associated KeyPair

  • pk4al - Given an alias name return the associated PublicKey

  • al4addr - Given a SuiAddress type or address string, return the assoicated alias name

  • al4kp - Given a KeyPair type, return the assoicated alias name

  • al4pk - Given a PublicKey type, return the assoicated alias name

Renaming aliases

To rename an alias, provide the existing name and a new name to SuiConfiguration.rename_alias().

Example

 1# The underlying configuration and synchronous client
 2from pysui import SuiConfig, SyncClient
 3from pysui.sui.sui_builders.get_builders import GetAllCoins
 4
 5def alias_examples():
 6    """Examples to excercise alias capabilities."""
 7    # Load a default configuration (i.e. ~/.sui/sui_config/client.yaml)
 8    cfg = SuiConfig.default_config()
 9    # Or if testing with suibase
10    # cfg = SuiConfig.sui_base_config()
11
12    # See aliases generated or loaded
13    for alias in cfg.aliases:
14        print(f"Alias {alias}")
15        print(f"Address {cfg.addr4al(alias)}")
16        print(f"PublicKey {cfg.pk4al(alias)}\n")
17
18    # Rename alias for the active_address
19    new_name = "Primary"
20    act_add_alias = cfg.al4addr(cfg.active_address)
21    print(f"Existing alias for active address {cfg.active_address} is {act_add_alias}")
22    cfg.rename_alias(old_alias=act_add_alias,new_alias=new_name)
23    print(f"Address associated to new alias 'Primary' = {cfg.addr4al(new_name)}\n")
24
25    # Instantiate client
26    client = SyncClient(cfg)
27
28    # Using alias for convenience
29    result = client.execute(GetAllCoins(owner=cfg.addr4al(new_name)))
30    if result.is_ok():
31        print(result.result_data.to_json(indent=2))
32    else:
33        print(result.result_string)