Skip to content

Commit 6318aa7

Browse files
committed
chore: support hyphens for handles
1 parent 3e0c0ce commit 6318aa7

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/namespaces/handles.cairo

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod Handles {
7676
const ASCII_0: u8 = 48;
7777
const ASCII_9: u8 = 57;
7878
const ASCII_UNDERSCORE: u8 = 95;
79+
const ASCII_HYPHEN: u8 = 45;
7980

8081
// *************************************************************************
8182
// EVENTS
@@ -270,7 +271,7 @@ pub mod Handles {
270271
}
271272

272273
/// @notice validates that a local name contains only [a-z,0-9,_] and does not begin with an
273-
/// underscore.
274+
/// underscore or hyphen.
274275
/// @param local_name username to be minted
275276
fn _validate_local_name(self: @ContractState, local_name: felt252) {
276277
let mut value: u256 = local_name.into();
@@ -282,7 +283,9 @@ pub mod Handles {
282283
}
283284
last_char = (value & 0xFF).try_into().unwrap();
284285
assert(
285-
(self._is_alpha_numeric(last_char) || last_char == ASCII_UNDERSCORE),
286+
(self._is_alpha_numeric(last_char)
287+
|| last_char == ASCII_UNDERSCORE
288+
|| last_char == ASCII_HYPHEN),
286289
Errors::INVALID_LOCAL_NAME
287290
);
288291

@@ -291,7 +294,10 @@ pub mod Handles {
291294

292295
// Note that for performance reason, the local_name is parsed in reverse order,
293296
// so the first character is the last processed one.
294-
assert(last_char != ASCII_UNDERSCORE.into(), Errors::INVALID_LOCAL_NAME);
297+
assert(
298+
last_char != ASCII_UNDERSCORE.into() && last_char != ASCII_HYPHEN.into(),
299+
Errors::INVALID_LOCAL_NAME
300+
);
295301
}
296302

297303
// @notice checks that a character is alpha numeric

tests/test_handle.cairo

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ const TEST_LOCAL_NAME: felt252 = 'coloniz';
2020
const TEST_LOCAL_NAME_TWO: felt252 = 'coloniz_two';
2121
const TEST_LOCAL_NAME_THREE: felt252 = 'colonizdoe_';
2222
const TEST_LOCAL_NAME_FOUR: felt252 = 'colonizdoe2';
23+
const TEST_LOCAL_NAME_FIVE: felt252 = 'coloniz-doe2';
2324
const TEST_BAD_LOCAL_NAME_1: felt252 = '_coloniz';
2425
const TEST_BAD_LOCAL_NAME_2: felt252 = 'Coloniz';
25-
const TEST_BAD_LOCAL_NAME_3: felt252 = 'coloniz-';
26+
const TEST_BAD_LOCAL_NAME_3: felt252 = '-coloniz';
2627

2728
const TEST_TOKEN_ID: u256 =
2829
3382571571946029498300446473584208976610422898231982587356291365654797409289;
@@ -60,6 +61,17 @@ fn test_mint_handle_two() {
6061
assert(local_name == TEST_LOCAL_NAME_TWO, 'invalid local name two');
6162
}
6263

64+
fn test_mint_handle_supports_hyphens() {
65+
let handles_contract_address = __setup__();
66+
let handles_dispatcher = IHandleDispatcher { contract_address: handles_contract_address };
67+
68+
let token_id = handles_dispatcher
69+
.mint_handle(TEST_LOCAL_NAME_FIVE, USER_ONE.try_into().unwrap());
70+
71+
let local_name: felt252 = handles_dispatcher.get_local_name(token_id);
72+
assert(local_name == TEST_LOCAL_NAME_FIVE, 'invalid local name five');
73+
}
74+
6375
#[test]
6476
#[should_panic(expected: ('coloniz: invalid local name!',))]
6577
fn test_mint_handle_with_bad_local_name_1() {
@@ -68,6 +80,7 @@ fn test_mint_handle_with_bad_local_name_1() {
6880

6981
handles_dispatcher.mint_handle(TEST_BAD_LOCAL_NAME_1, USER_ONE.try_into().unwrap());
7082
}
83+
7184
#[test]
7285
#[should_panic(expected: ('coloniz: invalid local name!',))]
7386
fn test_mint_handle_with_bad_local_name_2() {

0 commit comments

Comments
 (0)