The main module of the SuiNS application, defines the SuiNS object and
the authorization mechanism for interacting with the main data storage.
Authorization mechanic:
The Admin can authorize applications to access protected features of the
SuiNS, they're named with a prefix app_*. Once authorized, application can
get mutable access to the Registry and add to the application Balance.
At any moment any of the applications can be deathorized by the Admin making it impossible for the deauthorized module to access the registry.
Package Upgrades in mind:
-
None of the public functions of the SuiNS feature any specific types - instead we use generics to define the actual types in arbitrary modules.
-
The
Registryitself (the main feature of the application) is stored as a dynamic field so that we can change the type and the module that serves the registry without breaking the SuiNS compatibility. -
Any of the old modules can be deauthorized hence disabling its access to the registry and the balance.
use 0x2::balance;
use 0x2::coin;
use 0x2::dynamic_field;
use 0x2::object;
use 0x2::package;
use 0x2::sui;
use 0x2::transfer;
use 0x2::tx_context;
An admin capability. The admin has full control over the application. This object must be issued only once during module initialization.
struct AdminCap has store, key
Fields
-
id: object::UID
The main application object. Stores the state of the application, used for adding / removing and reading name records.
Dynamic fields:
registry: RegistryKey<R> -> Rconfig: ConfigKey<C> -> C
struct SuiNS has key
Fields
-
id: object::UID -
balance: balance::Balance<sui::SUI> - The total balance of the SuiNS. Can be added to by authorized apps. Can be withdrawn only by the application Admin.
The one-time-witness used to claim Publisher object.
struct SUINS has drop
Fields
-
dummy_field: bool
Key under which a configuration is stored. It is type dependent, so
that different configurations can be stored at the same time. Eg
currently we store application Config (and Promotion configuration).
struct ConfigKey<Config> has copy, drop, store
Fields
-
dummy_field: bool
Key under which the Registry object is stored.
In the V1, the object stored under this key is Registry, however, for
future migration purposes (if we ever need to change the Registry), we
keep the phantom parameter so two different Registries can co-exist.
struct RegistryKey<Config> has copy, drop, store
Fields
-
dummy_field: bool
An authorization Key kept in the SuiNS - allows applications access
protected features of the SuiNS (such as app_add_balance, etc.)
The App type parameter is a witness which should be defined in the
original module (Controller, Registry, Registrar - whatever).
struct AppKey<App: drop> has copy, drop, store
Fields
-
dummy_field: bool
An application is not authorized to access the feature.
const EAppNotAuthorized: u64 = 1;
Trying to withdraw from an empty balance.
const ENoProfits: u64 = 0;
Module initializer:
- create SuiNS object
- create admin capability
- claim Publisher object (for Display and TransferPolicy)
fun init(otw: suins::SUINS, ctx: &mut tx_context::TxContext)
Withdraw from the SuiNS balance directly and access the Coins within the same transaction. This is useful for the admin to withdraw funds from the SuiNS and then send them somewhere specific or keep at the address.
public fun withdraw(_: &suins::AdminCap, self: &mut suins::SuiNS, ctx: &mut tx_context::TxContext): coin::Coin<sui::SUI>
Authorize an application to access protected features of the SuiNS.
public fun authorize_app<App: drop>(_: &suins::AdminCap, self: &mut suins::SuiNS)
Deauthorize an application by removing its authorization key.
public fun deauthorize_app<App: drop>(_: &suins::AdminCap, self: &mut suins::SuiNS): bool
Check if an application is authorized to access protected features of the SuiNS.
public fun is_app_authorized<App: drop>(self: &suins::SuiNS): bool
Assert that an application is authorized to access protected features of
the SuiNS. Aborts with EAppNotAuthorized if not.
public fun assert_app_is_authorized<App: drop>(self: &suins::SuiNS)
Adds balance to the SuiNS.
public fun app_add_balance<App: drop>(_: App, self: &mut suins::SuiNS, balance: balance::Balance<sui::SUI>)
Get a mutable access to the Registry object. Can only be performed by authorized
applications.
public fun app_registry_mut<App: drop, R: store>(_: App, self: &mut suins::SuiNS): &mut R
Attach dynamic configuration object to the application.
public fun add_config<Config: drop, store>(_: &suins::AdminCap, self: &mut suins::SuiNS, config: Config)
Borrow configuration object. Read-only mode for applications.
public fun get_config<Config: drop, store>(self: &suins::SuiNS): &Config
Get the configuration object for editing. The admin should put it back
after editing (no extra check performed). Can be used to swap
configuration since the T has drop. Eg nothing is stopping the admin
from removing the configuration object and adding a new one.
Fully taking the config also allows for edits within a transaction.
public fun remove_config<Config: drop, store>(_: &suins::AdminCap, self: &mut suins::SuiNS): Config
Get a read-only access to the Registry object.
public fun registry<R: store>(self: &suins::SuiNS): &R
Add a registry to the SuiNS. Can only be performed by the admin.
public fun add_registry<R: store>(_: &suins::AdminCap, self: &mut suins::SuiNS, registry: R)