Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
manolisliolios committed Apr 26, 2024
1 parent a532e11 commit cd31b5d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 46 deletions.
29 changes: 5 additions & 24 deletions sdk/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { normalizeSuiNSName } from "@mysten/sui.js/utils";

export function isSubName(name: string): boolean {
return name.split('.').length > 2;
return normalizeSuiNSName(name, 'dot').split('.').length > 2;
}

/**
Expand All @@ -12,27 +13,7 @@ export function isSubName(name: string): boolean {
* @param name The name to check (e.g test.example.sub.sui)
*/
export function isNestedSubName(name: string): boolean {
return name.split('.').length > 3;
}

/**
* Validates a SuiNS name.
*
* 1. Lowercase letters, numbers or hyphens
* 2. Each part must be between 3 and 63 characters
* 3. Must end with '.sui'
*
* @@TODO: Replace with TS SDK validation.
*
* @param name The name to validate (e.g example.sui)
*/
export function validateName(name: string) {
const parts = name.split('.');
if (parts.some((x) => !x.match(/^[a-z0-9-]+$/)))
throw new Error('Invalid SuiNS name (only lowercase letters, numbers and hyphens are allowed)');
if (parts.some((x) => x.length < 3 || x.length > 63))
throw new Error('Invalid SuiNS name (each part must be between 3 and 63 characters)');
if (!name.endsWith('.sui')) throw new Error('Invalid SuiNS name');
return normalizeSuiNSName(name, 'dot').split('.').length > 3;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions sdk/src/suins-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
MAINNET_CONFIG,
TESTNET_CONFIG,
} from './constants.js';
import { isSubName, parsePriceListFromConfig, validateName, validateYears } from './helpers.js';
import { isSubName, parsePriceListFromConfig, validateYears } from './helpers.js';
import type { Constants, NameRecord, SuinsClientConfig, SuinsPriceList } from './types.js';
import { isValidSuiNSName } from '@mysten/sui.js/utils';

/// The SuinsClient is the main entry point for the Suins SDK.
/// It allows you to interact with SuiNS.
Expand Down Expand Up @@ -99,7 +100,7 @@ export class SuinsClient {
}

async getNameRecord(name: string): Promise<NameRecord> {
validateName(name);
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
if (!this.constants.suinsPackageId) throw new Error('Suins package ID is not set');
if (!this.constants.registryTableId) throw new Error('Registry table ID is not set');

Expand Down Expand Up @@ -152,7 +153,7 @@ export class SuinsClient {
years: number;
priceList: SuinsPriceList;
}) {
validateName(name);
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
validateYears(years);
if (isSubName(name)) throw new Error('Subdomains do not have a registration fee');

Expand Down
38 changes: 19 additions & 19 deletions sdk/src/suins-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
import type { TransactionBlock } from '@mysten/sui.js/transactions';
import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui.js/utils';
import { SUI_CLOCK_OBJECT_ID, isValidSuiNSName } from '@mysten/sui.js/utils';

import { AVATAR, CONTENT_HASH } from './constants.js';
import { isNestedSubName, isSubName, validateName, validateYears } from './helpers.js';
import { isNestedSubName, isSubName, validateYears } from './helpers.js';
import type { SuinsClient } from './suins-client.js';
import type { ObjectArgument } from './types.js';

Expand All @@ -28,7 +28,7 @@ export class SuinsTransaction {
renew({ nftId, price, years }: { nftId: ObjectArgument; price: number; years: number }) {
if (!this.#suinsClient.constants.renewalPackageId)
throw new Error('Renewal package id not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
validateYears(years);

this.transactionBlock.moveCall({
Expand All @@ -53,8 +53,8 @@ export class SuinsTransaction {
register({ name, price, years }: { name: string; price: number; years: number }) {
if (!this.#suinsClient.constants.registrationPackageId)
throw new Error('Registration package id not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
validateName(name);
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
validateYears(years);

const nft = this.transactionBlock.moveCall({
Expand Down Expand Up @@ -86,9 +86,9 @@ export class SuinsTransaction {
allowChildCreation: boolean;
allowTimeExtension: boolean;
}) {
validateName(name);
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
const isParentSubdomain = isNestedSubName(name);
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!this.#suinsClient.constants.subNamesPackageId)
throw new Error('Subdomains package ID not found');
if (isParentSubdomain && !this.#suinsClient.constants.tempSubNamesProxyPackageId)
Expand Down Expand Up @@ -126,9 +126,9 @@ export class SuinsTransaction {
name: string;
targetAddress: string;
}) {
validateName(name);
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
const isParentSubdomain = isNestedSubName(name);
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!this.#suinsClient.constants.subNamesPackageId)
throw new Error('Subdomains package ID not found');
if (isParentSubdomain && !this.#suinsClient.constants.tempSubNamesProxyPackageId)
Expand All @@ -152,10 +152,10 @@ export class SuinsTransaction {
* Removes a leaf subname.
*/
removeLeafSubName({ parentNft, name }: { parentNft: ObjectArgument; name: string }) {
validateName(name);
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
const isParentSubdomain = isNestedSubName(name);
if (!isSubName(name)) throw new Error('This can only be invoked for subnames');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!this.#suinsClient.constants.subNamesPackageId)
throw new Error('Subdomains package ID not found');
if (isParentSubdomain && !this.#suinsClient.constants.tempSubNamesProxyPackageId)
Expand Down Expand Up @@ -183,7 +183,7 @@ export class SuinsTransaction {
address?: string;
isSubdomain?: boolean;
}) {
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!this.#suinsClient.constants.utilsPackageId) throw new Error('Utils package ID not found');

if (isSubdomain && !this.#suinsClient.constants.tempSubNamesProxyPackageId)
Expand All @@ -206,8 +206,8 @@ export class SuinsTransaction {

/** Marks a name as default */
setDefault(name: string) {
validateName(name);
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!this.#suinsClient.constants.utilsPackageId) throw new Error('Utils package ID not found');

this.transactionBlock.moveCall({
Expand All @@ -230,9 +230,9 @@ export class SuinsTransaction {
allowChildCreation: boolean;
allowTimeExtension: boolean;
}) {
validateName(name);
if (!isValidSuiNSName(name)) throw new Error('Invalid SuiNS name');
const isParentSubdomain = isNestedSubName(name);
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!isParentSubdomain && !this.#suinsClient.constants.subNamesPackageId)
throw new Error('Subdomains package ID not found');
if (isParentSubdomain && !this.#suinsClient.constants.tempSubNamesProxyPackageId)
Expand Down Expand Up @@ -262,7 +262,7 @@ export class SuinsTransaction {
expirationTimestampMs: number;
isSubdomain?: boolean;
}) {
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (isSubdomain && !this.#suinsClient.constants.tempSubNamesProxyPackageId)
throw new Error('Subdomains proxy package ID not found');
if (!isSubdomain && !this.#suinsClient.constants.subNamesPackageId)
Expand All @@ -279,7 +279,7 @@ export class SuinsTransaction {
}

setUserData({ nft, value, key }: { nft: ObjectArgument; value: string; key: string }) {
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!this.#suinsClient.constants.utilsPackageId) throw new Error('Utils package ID not found');

if (key !== AVATAR && key !== CONTENT_HASH) throw new Error('Invalid key');
Expand All @@ -300,7 +300,7 @@ export class SuinsTransaction {
* Burns an expired NFT to collect storage rebates.
*/
burnExpired({ nft, isSubdomain }: { nft: ObjectArgument; isSubdomain?: boolean }) {
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('Suins Object ID not found');
if (!this.#suinsClient.constants.suinsObjectId) throw new Error('SuiNS Object ID not found');
if (!this.#suinsClient.constants.utilsPackageId) throw new Error('Utils package ID not found');

this.transactionBlock.moveCall({
Expand Down

0 comments on commit cd31b5d

Please sign in to comment.