diff --git a/CHANGELOG.md b/CHANGELOG.md index ef4d4c7fba..3c4b01a161 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (global) [\#782](https://github.com/line/lbm-sdk/pull/782) add unhandled return error handling * (x/collection,x/token) [\#798](https://github.com/line/lbm-sdk/pull/798) Fix x/collection ModifyContract * (ci) [\#803](https://github.com/line/lbm-sdk/pull/803) fix test flow to install libsodium +* (x/collection,token) [\#784](https://github.com/line/lbm-sdk/pull/784) Make field key matching in x/token & x/collection case-sensitive ### Breaking Changes * (cli) [\#773](https://github.com/line/lbm-sdk/pull/773) guide users to use generate-only in messages for x/foundation authority diff --git a/x/collection/collection.go b/x/collection/collection.go index 40a430a50c..4c3ca03674 100644 --- a/x/collection/collection.go +++ b/x/collection/collection.go @@ -72,7 +72,7 @@ func TokenClassUnpackInterfaces(any *codectypes.Any, unpacker codectypes.AnyUnpa return unpacker.UnpackAny(any, &class) } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // FTClass var _ TokenClass = (*FTClass)(nil) @@ -109,7 +109,7 @@ func (c FTClass) ValidateBasic() error { return nil } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // NFTClass var _ TokenClass = (*NFTClass)(nil) @@ -143,7 +143,7 @@ func (c NFTClass) ValidateBasic() error { return nil } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // Coin func NewFTCoin(classID string, amount sdk.Int) Coin { return NewCoin(NewFTID(classID), amount) @@ -217,7 +217,7 @@ func ParseCoin(coinStr string) (*Coin, error) { return &coin, nil } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // Coins type Coins []Coin diff --git a/x/collection/keeper/keys.go b/x/collection/keeper/keys.go index 0c6d8a21f2..464f48e9b4 100644 --- a/x/collection/keeper/keys.go +++ b/x/collection/keeper/keys.go @@ -87,7 +87,7 @@ func splitBalanceKey(key []byte) (contractID string, address sdk.AccAddress, tok return } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // owner func ownerKey(contractID string, tokenID string) []byte { prefix := ownerKeyPrefixByContractID(contractID) @@ -114,7 +114,7 @@ func ownerKeyPrefixByContractID(contractID string) []byte { return key } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // nft func nftKey(contractID string, tokenID string) []byte { prefix := nftKeyPrefixByContractID(contractID) @@ -152,7 +152,7 @@ func splitNFTKey(key []byte) (contractID string, tokenID string) { return } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // parent func parentKey(contractID string, tokenID string) []byte { prefix := parentKeyPrefixByContractID(contractID) @@ -190,7 +190,7 @@ func splitParentKey(key []byte) (contractID string, tokenID string) { return } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // child func childKey(contractID string, tokenID, childID string) []byte { prefix := childKeyPrefixByTokenID(contractID, tokenID) @@ -248,7 +248,7 @@ func splitChildKey(key []byte) (contractID string, tokenID, childID string) { return } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- func contractKey(contractID string) []byte { key := make([]byte, len(contractKeyPrefix)+len(contractID)) @@ -328,7 +328,7 @@ func nextClassIDKey(contractID string) []byte { return key } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- func authorizationKey(contractID string, operator, holder sdk.AccAddress) []byte { prefix := authorizationKeyPrefixByOperator(contractID, operator) key := make([]byte, len(prefix)+len(holder)) @@ -385,7 +385,7 @@ func splitAuthorizationKey(key []byte) (contractID string, operator, holder sdk. return } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- func grantKey(contractID string, grantee sdk.AccAddress, permission collection.Permission) []byte { prefix := grantKeyPrefixByGrantee(contractID, grantee) key := make([]byte, len(prefix)+1) @@ -442,7 +442,7 @@ func splitGrantKey(key []byte) (contractID string, grantee sdk.AccAddress, permi return } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // statistics func statisticKey(keyPrefix []byte, contractID string, classID string) []byte { prefix := statisticKeyPrefixByContractID(keyPrefix, contractID) @@ -480,7 +480,7 @@ func splitStatisticKey(keyPrefix, key []byte) (contractID string, classID string return } -//----------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- // legacy keys func legacyTokenKey(contractID string, tokenID string) []byte { prefix := legacyTokenKeyPrefixByContractID(contractID) diff --git a/x/collection/keeper/supply.go b/x/collection/keeper/supply.go index 245518245c..72ad4f7c05 100644 --- a/x/collection/keeper/supply.go +++ b/x/collection/keeper/supply.go @@ -317,19 +317,20 @@ func (k Keeper) ModifyContract(ctx sdk.Context, contractID string, operator sdk. return err } - modifiers := map[string]func(string){ - collection.AttributeKeyName.String(): func(name string) { + modifiers := map[collection.AttributeKey]func(string){ + collection.AttributeKeyName: func(name string) { contract.Name = name }, - collection.AttributeKeyBaseImgURI.String(): func(uri string) { + collection.AttributeKeyBaseImgURI: func(uri string) { contract.BaseImgUri = uri }, - collection.AttributeKeyMeta.String(): func(meta string) { + collection.AttributeKeyMeta: func(meta string) { contract.Meta = meta }, } for _, change := range changes { - modifiers[change.Key](change.Value) + key := collection.AttributeKeyFromString(change.Key) + modifiers[key](change.Value) } k.setContract(ctx, *contract) @@ -351,16 +352,17 @@ func (k Keeper) ModifyTokenClass(ctx sdk.Context, contractID string, classID str return err } - modifiers := map[string]func(string){ - collection.AttributeKeyName.String(): func(name string) { + modifiers := map[collection.AttributeKey]func(string){ + collection.AttributeKeyName: func(name string) { class.SetName(name) }, - collection.AttributeKeyMeta.String(): func(meta string) { + collection.AttributeKeyMeta: func(meta string) { class.SetMeta(meta) }, } for _, change := range changes { - modifiers[change.Key](change.Value) + key := collection.AttributeKeyFromString(change.Key) + modifiers[key](change.Value) } k.setTokenClass(ctx, contractID, class) @@ -383,16 +385,17 @@ func (k Keeper) ModifyNFT(ctx sdk.Context, contractID string, tokenID string, op return err } - modifiers := map[string]func(string){ - collection.AttributeKeyName.String(): func(name string) { + modifiers := map[collection.AttributeKey]func(string){ + collection.AttributeKeyName: func(name string) { token.Name = name }, - collection.AttributeKeyMeta.String(): func(meta string) { + collection.AttributeKeyMeta: func(meta string) { token.Meta = meta }, } for _, change := range changes { - modifiers[change.Key](change.Value) + key := collection.AttributeKeyFromString(change.Key) + modifiers[key](change.Value) } k.setNFT(ctx, contractID, *token) diff --git a/x/collection/msgs.go b/x/collection/msgs.go index c32fd3961a..3ce1c6a708 100644 --- a/x/collection/msgs.go +++ b/x/collection/msgs.go @@ -166,26 +166,26 @@ func ValidatePermission(permission Permission) error { } func validateContractChange(change Attribute) error { - validators := map[AttributeKey]func(string) error{ - AttributeKeyName: validateName, - AttributeKeyBaseImgURI: validateBaseImgURI, - AttributeKeyMeta: validateMeta, + validators := map[string]func(string) error{ + AttributeKeyName.String(): validateName, + AttributeKeyBaseImgURI.String(): validateBaseImgURI, + AttributeKeyMeta.String(): validateMeta, } return validateChange(change, validators) } func validateTokenClassChange(change Attribute) error { - validators := map[AttributeKey]func(string) error{ - AttributeKeyName: validateName, - AttributeKeyMeta: validateMeta, + validators := map[string]func(string) error{ + AttributeKeyName.String(): validateName, + AttributeKeyMeta.String(): validateMeta, } return validateChange(change, validators) } -func validateChange(change Attribute, validators map[AttributeKey]func(string) error) error { - validator, ok := validators[AttributeKeyFromString(change.Key)] +func validateChange(change Attribute, validators map[string]func(string) error) error { + validator, ok := validators[change.Key] if !ok { return sdkerrors.ErrInvalidRequest.Wrapf("invalid field: %s", change.Key) } diff --git a/x/collection/msgs_test.go b/x/collection/msgs_test.go index 4d8d8ba24c..06c74482a5 100644 --- a/x/collection/msgs_test.go +++ b/x/collection/msgs_test.go @@ -2,13 +2,14 @@ package collection_test import ( "fmt" - "github.com/line/lbm-sdk/x/auth/legacy/legacytx" + "strings" "testing" "github.com/stretchr/testify/require" "github.com/line/lbm-sdk/crypto/keys/secp256k1" sdk "github.com/line/lbm-sdk/types" + "github.com/line/lbm-sdk/x/auth/legacy/legacytx" "github.com/line/lbm-sdk/x/collection" ) @@ -1108,7 +1109,7 @@ func TestMsgModify(t *testing.T) { addrs[i] = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) } - changes := []collection.Change{{Field: "name", Value: "New test"}} + changes := []collection.Change{{Field: collection.AttributeKeyName.String(), Value: "New test"}} testCases := map[string]struct { contractID string owner sdk.AccAddress @@ -1149,7 +1150,7 @@ func TestMsgModify(t *testing.T) { "invalid key of change": { contractID: "deadbeef", owner: addrs[0], - changes: []collection.Change{{Value: "tt"}}, + changes: []collection.Change{{Field: strings.ToUpper(collection.AttributeKeyName.String()) , Value: "tt"}}, }, "invalid value of change": { contractID: "deadbeef", diff --git a/x/token/keeper/msg_server_test.go b/x/token/keeper/msg_server_test.go index 1b7439b3fa..448ff958b8 100644 --- a/x/token/keeper/msg_server_test.go +++ b/x/token/keeper/msg_server_test.go @@ -412,7 +412,7 @@ func (s *KeeperTestSuite) TestMsgModify() { req := &token.MsgModify{ ContractId: s.contractID, Owner: tc.grantee.String(), - Changes: []token.Pair{{Field: token.AttributeKeyName.String(), Value: "hello"}}, + Changes: []token.Pair{{Field: token.AttributeKeyImageURI.String(), Value: "uri"}}, } res, err := s.msgServer.Modify(sdk.WrapSDKContext(ctx), req) if !tc.valid { diff --git a/x/token/keeper/supply.go b/x/token/keeper/supply.go index 871390e00a..328c1a8b60 100644 --- a/x/token/keeper/supply.go +++ b/x/token/keeper/supply.go @@ -285,19 +285,20 @@ func (k Keeper) modify(ctx sdk.Context, contractID string, changes []token.Pair) return err } - modifiers := map[string]func(string){ - token.AttributeKeyName.String(): func(name string) { + modifiers := map[token.AttributeKey]func(string){ + token.AttributeKeyName: func(name string) { class.Name = name }, - token.AttributeKeyImageURI.String(): func(uri string) { + token.AttributeKeyImageURI: func(uri string) { class.ImageUri = uri }, - token.AttributeKeyMeta.String(): func(meta string) { + token.AttributeKeyMeta: func(meta string) { class.Meta = meta }, } for _, change := range changes { - modifiers[change.Field](change.Value) + key := token.AttributeKeyFromString(change.Field) + modifiers[key](change.Value) } k.setClass(ctx, *class) diff --git a/x/token/msgs_test.go b/x/token/msgs_test.go index 7d0565eeed..20fb7e10ad 100644 --- a/x/token/msgs_test.go +++ b/x/token/msgs_test.go @@ -2,6 +2,7 @@ package token_test import ( "fmt" + "strings" "testing" "github.com/stretchr/testify/require" @@ -594,7 +595,7 @@ func TestMsgModify(t *testing.T) { "invalid key of change": { contractID: "deadbeef", grantee: addrs[0], - changes: []token.Pair{{Value: "tt"}}, + changes: []token.Pair{{Field: strings.ToUpper(token.AttributeKeyName.String()), Value: "tt"}}, }, "invalid value of change": { contractID: "deadbeef", @@ -864,7 +865,7 @@ func TestAminoJSON(t *testing.T) { &token.MsgModify{ ContractId: contractId, Owner: addrs[0].String(), - Changes: []token.Pair{token.Pair{Field: token.AttributeKeyName.String(), Value: "New test"}}, + Changes: []token.Pair{{Field: token.AttributeKeyName.String(), Value: "New test"}}, }, "/lbm.token.v1.MsgModify", fmt.Sprintf("{\"account_number\":\"1\",\"chain_id\":\"foo\",\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"memo\",\"msgs\":[{\"type\":\"lbm-sdk/token/MsgModify\",\"value\":{\"changes\":[{\"field\":\"name\",\"value\":\"New test\"}],\"contract_id\":\"deadbeef\",\"owner\":\"%s\"}}],\"sequence\":\"1\",\"timeout_height\":\"1\"}", addrs[0].String()), diff --git a/x/token/validation.go b/x/token/validation.go index 97400e8ec0..7310108320 100644 --- a/x/token/validation.go +++ b/x/token/validation.go @@ -81,13 +81,13 @@ func ValidatePermission(permission Permission) error { } func validateChange(change Pair) error { - validators := map[AttributeKey]func(string) error{ - AttributeKeyName: validateName, - AttributeKeyImageURI: validateImageURI, - AttributeKeyMeta: validateMeta, + validators := map[string]func(string) error{ + AttributeKeyName.String(): validateName, + AttributeKeyImageURI.String(): validateImageURI, + AttributeKeyMeta.String(): validateMeta, } - validator, ok := validators[AttributeKeyFromString(change.Field)] + validator, ok := validators[change.Field] if !ok { return sdkerrors.ErrInvalidRequest.Wrapf("invalid field: %s", change.Field) }