Skip to content

Commit

Permalink
Config V2
Browse files Browse the repository at this point in the history
  • Loading branch information
manolisliolios committed Nov 29, 2024
1 parent 17518a5 commit 690861e
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 507 deletions.
94 changes: 0 additions & 94 deletions packages/suins/sources/actions/update_image.move

This file was deleted.

7 changes: 4 additions & 3 deletions packages/suins/sources/admin.move
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module suins::admin;
use std::string::String;
use sui::clock::Clock;
use sui::tx_context::sender;
use suins::config;
use suins::core_config::CoreConfig;
use suins::domain;
use suins::registry::Registry;
use suins::suins::{Self, AdminCap, SuiNS};
Expand All @@ -34,7 +34,7 @@ public fun reserve_domain(
ctx: &mut TxContext,
): SuinsRegistration {
let domain = domain::new(domain_name);
config::assert_valid_user_registerable_domain(&domain);
suins.get_config<CoreConfig>().assert_is_valid_for_sale(&domain);
let registry = suins::app_registry_mut<Admin, Registry>(Admin {}, suins);
registry.add_record(domain, no_years, clock, ctx)
}
Expand All @@ -49,10 +49,11 @@ entry fun reserve_domains(
ctx: &mut TxContext,
) {
let sender = sender(ctx);
let config = *suins.get_config<CoreConfig>();
let registry = suins::app_registry_mut<Admin, Registry>(Admin {}, suins);
while (!domains.is_empty()) {
let domain = domain::new(domains.pop_back());
config::assert_valid_user_registerable_domain(&domain);
config.assert_is_valid_for_sale(&domain);
let nft = registry.add_record(domain, no_years, clock, ctx);
sui::transfer::public_transfer(nft, sender);
};
Expand Down
154 changes: 0 additions & 154 deletions packages/suins/sources/config.move

This file was deleted.

3 changes: 3 additions & 0 deletions packages/suins/sources/constants.move
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ public fun subdomain_allow_extension_key(): String {

/// A getter for a leaf name record's expiration timestamp.
public fun leaf_expiration_timestamp(): u64 { LEAF_EXPIRATION_TIMESTAMP }

/// The `PaymentIntent` version that can be used by this package for payments.
public macro fun payments_version(): u8 { 1 }
101 changes: 101 additions & 0 deletions packages/suins/sources/core_config.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/// Core configuration of the SuiNS application.
///
/// This configuration is used to validate domains for registration and renewal.
/// It can only be stored as a valid config in the `SuiNS` object by an admin,
/// hence why all the functions are public. Having just the config object cannot
/// pose a security risk as it cannot be used.
module suins::core_config;

use std::string::String;
use sui::vec_map::{Self, VecMap};
use sui::vec_set::{Self, VecSet};
use suins::constants;
use suins::domain::Domain;

#[error]
const EInvalidLength: vector<u8> = b"Invalid length for the label part of the domain.";

#[error]
const EInvalidTld: vector<u8> = b"Invalid TLD";

#[error]
const ESubnameNotSupported: vector<u8> = b"Subdomains are not supported for sales.";

public struct CoreConfig has copy, store, drop {
/// Public key of the API server. Currently only used for direct setup.
public_key: vector<u8>,
/// Minimum length of the label part of the domain. This is different from
/// the base `domain` checks. This is our minimum acceptable length (for sales).
/// TODO: Shoudl we consider removing this? Our range is [1,63] by design, and
/// the `PricingConfig` won't have 1,2 digits if we don't want to have.
min_label_length: u8,
/// Maximum length of the label part of the domain.
max_label_length: u8,
/// List of valid TLDs for registration / renewals.
valid_tlds: VecSet<String>,
/// The `PaymentIntent` version that can be used for handling sales.
payments_version: u8,
// Extra fields for future use.
extra: VecMap<String, String>,
}

public fun new(
public_key: vector<u8>,
min_label_length: u8,
max_label_length: u8,
payments_version: u8,
valid_tlds: vector<String>,
extra: VecMap<String, String>,
): CoreConfig {
CoreConfig {
public_key,
min_label_length,
max_label_length,
payments_version,
valid_tlds: vec_set::from_keys(valid_tlds),
extra,
}
}

public fun default(): CoreConfig {
new(b"", 3, 63, constants::payments_version!(), vector[constants::sui_tld()], vec_map::empty())
}

public fun public_key(config: &CoreConfig): vector<u8> {
config.public_key
}

public fun min_label_length(config: &CoreConfig): u8 {
config.min_label_length
}

public fun max_label_length(config: &CoreConfig): u8 {
config.max_label_length
}

public fun is_valid_tld(config: &CoreConfig, tld: &String): bool {
config.valid_tlds.contains(tld)
}

public fun payments_version(config: &CoreConfig): u8 {
config.payments_version
}

public(package) fun is_valid_for_sale(config: &CoreConfig, domain: &Domain): bool {
// We block subdomains for regular sales.
if (domain.is_subdomain()) return false;
let sld_len = domain.sld().length();
if (sld_len < (config.min_label_length as u64) || sld_len > (config.max_label_length as u64)) {
return false
};

return config.is_valid_tld(domain.tld())
}

public(package) fun assert_is_valid_for_sale(config: &CoreConfig, domain: &Domain) {
assert!(!domain.is_subdomain(), ESubnameNotSupported);
assert!(config.is_valid_tld(domain.tld()), EInvalidTld);

let sld_len = domain.sld().length();
assert!(sld_len >= (config.min_label_length as u64) && sld_len <= (config.max_label_length as u64), EInvalidLength);
}
Loading

0 comments on commit 690861e

Please sign in to comment.