> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sageprotocol.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Registry

> Maintains registries linking user keys to owned/shared user identities.

```rust theme={null}
use std::string;
use sui::package;
use sui::table;
use sage_utils::string_helpers;
```

<Tip>
  Explore this module further in the Mover Registry: @sage/user
</Tip>

## Structs

### `UserRegistry`

Registry that contains a map to all `User`s.

```rust theme={null}
public struct UserRegistry has key
```

<Expandable title="Fields">
  ```rust theme={null}
    id: sui::object::UID,
    address_registry: Table<address, std::string::String>,
    address_reverse_registry: Table<std::string::String, address>,
    user_owned_registry: Table<std::string::String, address>,
    user_owned_reverse_registry: Table<address, std::string::String>,
    user_shared_registry: Table<std::string::String, address>,
    user_shared_reverse_registry: Table<address, std::string::String>
  ```
</Expandable>

## Constants

Error code when a wallet address does not have an associated `User`.

```rust theme={null}
const EAddressRecordDoesNotExist: u64 = 370;
```

Error code when a `User` name does not exist in the registry.

```rust theme={null}
const EUsernameRecordDoesNotExist: u64 = 371;
```

## Functions

### `assert_user_address_exists`

Aborts with `EAddressRecordDoesNotExist` when a wallet address is not associated with a `User`.

```rust theme={null}
public fun assert_user_address_exists(user_registry: &UserRegistry, wallet_address: address)
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun assert_user_address_exists(
      user_registry: &UserRegistry,
      wallet_address: address
    ) {
      let is_user = has_address_record(
        user_registry,
        wallet_address
      );

      assert!(is_user, EAddressRecordDoesNotExist);
    }
  ```
</Expandable>

### `assert_user_name_exists`

Aborts with `EUsernameRecordDoesNotExist` when the `User` name does not exist in the registry.

```rust theme={null}
public fun assert_user_name_exists(user_registry: &UserRegistry, username: String)
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun assert_user_name_exists(
      user_registry: &UserRegistry,
      username: String
    ) {
      let is_user = has_username_record(
        user_registry,
        username
      );

      assert!(is_user, EUsernameRecordDoesNotExist);
    }
  ```
</Expandable>

### `get_owner_address_from_key`

Returns the wallet address for the `User` with the specified lowercase name.

```rust theme={null}
public fun get_owner_address_from_key(user_registry: &UserRegistry, user_key: String): address
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun get_owner_address_from_key (
      user_registry: &UserRegistry,
      user_key: String
    ): address {
      *user_registry.address_reverse_registry.borrow(user_key)
    }
  ```
</Expandable>

### `get_owned_user_address_from_key`

Returns the address for the `OwnedUser` with the specified lowercase name.

```rust theme={null}
public fun get_owned_user_address_from_key(user_registry: &UserRegistry, user_key: String): address
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun get_owned_user_address_from_key (
      user_registry: &UserRegistry,
      user_key: String
    ): address {
      *user_registry.user_owned_registry.borrow(user_key)
    }
  ```
</Expandable>

### `get_shared_user_address_from_key`

Returns the address for the `SharedUser` with the specified lowercase name.

```rust theme={null}
public fun get_shared_user_address_from_key (user_registry: &UserRegistry, user_key: String): address
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun get_shared_user_address_from_key (
      user_registry: &UserRegistry,
      user_key: String
    ): address {
      *user_registry.user_shared_registry.borrow(user_key)
    }
  ```
</Expandable>

### `get_key_from_owner_address`

Returns the `User` lowercase name associated with the wallet address.

```rust theme={null}
public fun get_key_from_owner_address(user_registry: &UserRegistry, user_address: address): String
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun get_key_from_owner_address (
      user_registry: &UserRegistry,
      user_address: address
    ): String {
      *user_registry.address_registry.borrow(user_address)
    }
  ```
</Expandable>

### `get_key_from_owned_user_address`

Returns the `User` lowercase name associated with the `OwnedUser` address.

```rust theme={null}
public fun get_key_from_owned_user_address(user_registry: &UserRegistry, user_address: address): String
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun get_key_from_owned_user_address (
      user_registry: &UserRegistry,
      user_address: address
    ): String {
      *user_registry.user_owned_reverse_registry.borrow(user_address)
    }
  ```
</Expandable>

### `get_key_from_shared_user_address`

Returns the `User` lowercase name associated with the `SharedUser` address.

```rust theme={null}
public fun get_key_from_shared_user_address(user_registry: &UserRegistry, user_address: address): String
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun get_key_from_shared_user_address (
      user_registry: &UserRegistry,
      user_address: address
    ): String {
      *user_registry.user_shared_reverse_registry.borrow(user_address)
    }
  ```
</Expandable>

### `has_address_record`

Returns true or false based on whether the wallet address has an associated `User`.

```rust theme={null}
public fun has_address_record(user_registry: &UserRegistry, wallet_address: address): bool
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun has_address_record (
      user_registry: &UserRegistry,
      wallet_address: address
    ): bool {
      user_registry.address_registry.contains(wallet_address)
    }
  ```
</Expandable>

### `has_username_record`

Returns true or false based on whether the specified lowercase `User` name is in use.

```rust theme={null}
public fun has_username_record(user_registry: &UserRegistry, username: String): bool
```

<Expandable title="Implementation">
  ```rust theme={null}
    public fun has_username_record (
      user_registry: &UserRegistry,
      username: String
    ): bool {
      let user_key = string_helpers::to_lowercase(
        &username
      );

      user_registry.user_owned_registry.contains(user_key) &&
      user_registry.user_shared_registry.contains(user_key)
    }
  ```
</Expandable>
