Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: can not mint nft from ibc class #381

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions modules/nft/types/errors.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package types

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
errormod "cosmossdk.io/errors"
)

var (
ErrInvalidCollection = sdkerrors.Register(ModuleName, 9, "invalid nft collection")
ErrUnknownCollection = sdkerrors.Register(ModuleName, 10, "unknown nft collection")
ErrInvalidNFT = sdkerrors.Register(ModuleName, 11, "invalid nft")
ErrNFTAlreadyExists = sdkerrors.Register(ModuleName, 12, "nft already exists")
ErrUnknownNFT = sdkerrors.Register(ModuleName, 13, "unknown nft")
ErrEmptyTokenData = sdkerrors.Register(ModuleName, 14, "nft data can't be empty")
ErrUnauthorized = sdkerrors.Register(ModuleName, 15, "unauthorized address")
ErrInvalidDenom = sdkerrors.Register(ModuleName, 16, "invalid denom")
ErrInvalidTokenID = sdkerrors.Register(ModuleName, 17, "invalid nft id")
ErrInvalidTokenURI = sdkerrors.Register(ModuleName, 18, "invalid nft uri")
ErrInvalidCollection = errormod.Register(ModuleName, 9, "invalid nft collection")
ErrUnknownCollection = errormod.Register(ModuleName, 10, "unknown nft collection")
ErrInvalidNFT = errormod.Register(ModuleName, 11, "invalid nft")
ErrNFTAlreadyExists = errormod.Register(ModuleName, 12, "nft already exists")
ErrUnknownNFT = errormod.Register(ModuleName, 13, "unknown nft")
ErrEmptyTokenData = errormod.Register(ModuleName, 14, "nft data can't be empty")
ErrUnauthorized = errormod.Register(ModuleName, 15, "unauthorized address")
ErrInvalidDenom = errormod.Register(ModuleName, 16, "invalid denom")
ErrInvalidTokenID = errormod.Register(ModuleName, 17, "invalid nft id")
ErrInvalidTokenURI = errormod.Register(ModuleName, 18, "invalid nft uri")
)
31 changes: 18 additions & 13 deletions modules/nft/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

errormod "cosmossdk.io/errors"
"github.com/tidwall/gjson"
)

Expand Down Expand Up @@ -58,11 +60,11 @@ func (msg MsgIssueDenom) ValidateBasic() error {
}

if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}

if len(msg.Data) != 0 && !gjson.Valid(msg.Data) {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
return errormod.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
}
return ValidateKeywords(msg.Id)
}
Expand Down Expand Up @@ -111,15 +113,15 @@ func (msg MsgTransferNFT) ValidateBasic() error {
}

if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}

if _, err := sdk.AccAddressFromBech32(msg.Recipient); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address (%s)", err)
}

if len(msg.Data) != 0 && Modified(msg.Data) && !gjson.Valid(msg.Data) {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
return errormod.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
}
return ValidateTokenID(msg.Id)
}
Expand Down Expand Up @@ -163,7 +165,7 @@ func (msg MsgEditNFT) Type() string { return TypeMsgEditNFT }
// ValidateBasic Implements Msg.
func (msg MsgEditNFT) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}

if err := ValidateDenomID(msg.DenomId); err != nil {
Expand All @@ -175,7 +177,7 @@ func (msg MsgEditNFT) ValidateBasic() error {
}

if len(msg.Data) != 0 && Modified(msg.Data) && !gjson.Valid(msg.Data) {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
return errormod.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
}
return ValidateTokenID(msg.Id)
}
Expand Down Expand Up @@ -220,10 +222,13 @@ func (msg MsgMintNFT) Type() string { return TypeMsgMintNFT }
// ValidateBasic Implements Msg.
func (msg MsgMintNFT) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
if _, err := sdk.AccAddressFromBech32(msg.Recipient); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid receipt address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid receipt address (%s)", err)
}
if IsIBCDenom(msg.DenomId){
return errormod.Wrapf(sdkerrors.ErrInvalidRequest, "can not mint nft in denom (%s)", msg.DenomId)
}
if err := ValidateDenomID(msg.DenomId); err != nil {
return err
Expand All @@ -232,7 +237,7 @@ func (msg MsgMintNFT) ValidateBasic() error {
return err
}
if len(msg.Data) != 0 && !gjson.Valid(msg.Data) {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
return errormod.Wrap(sdkerrors.ErrJSONUnmarshal, "invalid data, must be a JSON string or empty")
}
return ValidateTokenID(msg.Id)
}
Expand Down Expand Up @@ -270,7 +275,7 @@ func (msg MsgBurnNFT) Type() string { return TypeMsgBurnNFT }
// ValidateBasic Implements Msg.
func (msg MsgBurnNFT) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
if err := ValidateDenomID(msg.DenomId); err != nil {
return err
Expand Down Expand Up @@ -311,10 +316,10 @@ func (msg MsgTransferDenom) Type() string { return TypeMsgTransferDenom }
// ValidateBasic Implements Msg.
func (msg MsgTransferDenom) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
if _, err := sdk.AccAddressFromBech32(msg.Recipient); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address (%s)", err)
return errormod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address (%s)", err)
}
if err := ValidateDenomID(msg.Id); err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions modules/nft/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"strings"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
errormod "cosmossdk.io/errors"
)

const (
Expand Down Expand Up @@ -34,23 +34,23 @@ var (
func ValidateDenomID(denomID string) error {
boolPrifix := strings.HasPrefix(denomID, "tibc-")
if !regexpID(denomID) && !boolPrifix {
return sdkerrors.Wrapf(ErrInvalidDenom, "denomID can only accept characters that match the regular expression: (%s),but got (%s)", idString, denomID)
return errormod.Wrapf(ErrInvalidDenom, "denomID can only accept characters that match the regular expression: (%s),but got (%s)", idString, denomID)
}
return nil
}

// ValidateTokenID verify that the tokenID is legal
func ValidateTokenID(tokenID string) error {
if !regexpID(tokenID) {
return sdkerrors.Wrapf(ErrInvalidDenom, "tokenID can only accept characters that match the regular expression: (%s),but got (%s)", idString, tokenID)
return errormod.Wrapf(ErrInvalidDenom, "tokenID can only accept characters that match the regular expression: (%s),but got (%s)", idString, tokenID)
}
return nil
}

// ValidateTokenURI verify that the tokenURI is legal
func ValidateTokenURI(tokenURI string) error {
if len(tokenURI) > MaxTokenURILen {
return sdkerrors.Wrapf(ErrInvalidTokenURI, "the length of nft uri(%s) only accepts value [0, %d]", tokenURI, MaxTokenURILen)
return errormod.Wrapf(ErrInvalidTokenURI, "the length of nft uri(%s) only accepts value [0, %d]", tokenURI, MaxTokenURILen)
}
return nil
}
Expand All @@ -70,7 +70,7 @@ func Modify(origin, target string) string {
// ValidateKeywords checks if the given denomId begins with `DenomKeywords`
func ValidateKeywords(denomId string) error {
if regexpKeyword(denomId) {
return sdkerrors.Wrapf(ErrInvalidDenom, "invalid denomId: %s, can not begin with keyword: (%s)", denomId, keywords)
return errormod.Wrapf(ErrInvalidDenom, "invalid denomId: %s, can not begin with keyword: (%s)", denomId, keywords)
}
return nil
}
Expand Down