Skip to content

Commit

Permalink
allow more characters to token subunit
Browse files Browse the repository at this point in the history
  • Loading branch information
miladz68 committed Oct 31, 2023
1 parent 53e9c98 commit 7f8ee3b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 5 additions & 2 deletions x/asset/ft/types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
// The length is 50 since the demon is {subunit}-{address} and
// the address length might be up to 66 symbols and the demon length must be less than 127 symbols
// according to bank validation.
subunitRegexStr = `^[a-z][a-z0-9]{0,50}$`
subunitRegexStr = `^[a-z][a-z0-9/:._]{0,50}$`
subunitRegex *regexp.Regexp

symbolRegexStr = `^[a-zA-Z][a-zA-Z0-9-]{2,127}$`
Expand Down Expand Up @@ -89,7 +89,6 @@ var reserved = []string{
strings.ToLower(constant.DenomTestDisplay),
strings.ToLower(constant.DenomMain),
strings.ToLower(constant.DenomMainDisplay),
strings.ToLower(ibctypes.DenomPrefix),
}

// ValidateSubunit checks the provided subunit is valid.
Expand All @@ -98,6 +97,10 @@ func ValidateSubunit(subunit string) error {
return sdkerrors.Wrapf(ErrInvalidInput, "%s is a reserved subunit", subunit)
}

if strings.HasPrefix(strings.ToLower(subunit), ibctypes.DenomPrefix) {
return sdkerrors.Wrapf(ErrInvalidInput, "subunit cannot start with ibc")
}

if !subunitRegex.MatchString(subunit) {
return sdkerrors.Wrapf(ErrInvalidInput, "subunit must match regex format '%s'", subunitRegexStr)
}
Expand Down
21 changes: 19 additions & 2 deletions x/asset/ft/types/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func TestValidateSubunit(t *testing.T) {
"ABC1",
"ABC-1",
"ABC/1",
"ABC.1",
"ABC:1",
"btc-devcore1phjrez5j2wp5qzp0zvlqavasvw60mkp2zmfe6h",
"BTC-devcore1phjrez5j2wp5qzp0zvlqavasvw60mkp2zmfe6h",
"core",
Expand All @@ -77,6 +79,17 @@ func TestValidateSubunit(t *testing.T) {
ibctypes.DenomPrefix + "-",
ibctypes.DenomPrefix + "/",
ibctypes.DenomPrefix + "/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
"/abc",
".abc",
":abc",
"ibc",
"ibc/1",
"ibc.1",
"ibc:1",
"IBC",
"IBC/1",
"IBC.1",
"IBC:1",
}

acceptableSubunits := []string{
Expand All @@ -88,14 +101,18 @@ func TestValidateSubunit(t *testing.T) {
"ucoreum",
"coreeum",
"a1234567890123456789012345678901234567890123456789",
"abc/1",
"abc.1",
"abc:1",
}

assertValidSubunit := func(symbol string, isValid bool) {
err := types.ValidateSubunit(symbol)
if isValid {
requireT.NoError(err)
requireT.NoError(err, "symbol: %s", symbol)
} else {
requireT.True(types.ErrInvalidInput.Is(err))
requireT.Error(err, "symbol: %s", symbol)
requireT.ErrorIs(err, types.ErrInvalidInput, "symbol: %s", symbol)
}
}

Expand Down

0 comments on commit 7f8ee3b

Please sign in to comment.