From 286d3725eb62f92f45961d6e5436f077bef59c38 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 7 Nov 2022 07:20:15 +0000 Subject: [PATCH 1/6] Apply more strict field key name matching on x/token --- x/token/keeper/msg_server_test.go | 2 +- x/token/keeper/supply.go | 11 ++++++----- x/token/msgs_test.go | 3 ++- x/token/validation.go | 10 +++++----- 4 files changed, 14 insertions(+), 12 deletions(-) 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..8905ddcad3 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", 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) } From 86e5fb1b5c84dec90f96b2b0652f80b2f8b771e6 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 7 Nov 2022 08:05:32 +0000 Subject: [PATCH 2/6] Lint --- x/token/msgs_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/token/msgs_test.go b/x/token/msgs_test.go index 8905ddcad3..20fb7e10ad 100644 --- a/x/token/msgs_test.go +++ b/x/token/msgs_test.go @@ -865,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()), From 045e6ed1e19ae6f27e6db7f4cd4d295c45cfe1b2 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 7 Nov 2022 08:29:05 +0000 Subject: [PATCH 3/6] Apply more strict field key name matching on x/collection --- x/collection/keeper/supply.go | 29 ++++++++++++++++------------- x/collection/msgs.go | 18 +++++++++--------- x/collection/msgs_test.go | 8 +++++--- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/x/collection/keeper/supply.go b/x/collection/keeper/supply.go index ca02e5a285..8c7cb05edd 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.Name = 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..843c01d0a1 100644 --- a/x/collection/msgs_test.go +++ b/x/collection/msgs_test.go @@ -2,9 +2,11 @@ package collection_test import ( "fmt" - "github.com/line/lbm-sdk/x/auth/legacy/legacytx" + "strings" "testing" + "github.com/line/lbm-sdk/x/auth/legacy/legacytx" + "github.com/stretchr/testify/require" "github.com/line/lbm-sdk/crypto/keys/secp256k1" @@ -1108,7 +1110,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 +1151,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", From 88f7e1d2ca8397ab7bfd082af71bc648ba9414ea Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 7 Nov 2022 08:29:22 +0000 Subject: [PATCH 4/6] Lint --- x/collection/collection.go | 8 ++++---- x/collection/keeper/keys.go | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) 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) From cdffffc77b4047e13182b871e086d5eeb713d0b7 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Mon, 7 Nov 2022 08:36:48 +0000 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64a5775a25..37177993eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes * (x/foundation) [\#772](https://github.com/line/lbm-sdk/pull/772) export x/foundation pool +* (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 From 87a51b801e8d5fcfe3b4333dd3fbf786b6f5d0b5 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Tue, 8 Nov 2022 13:44:06 +0900 Subject: [PATCH 6/6] Update msgs_test.go Lint --- x/collection/msgs_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/collection/msgs_test.go b/x/collection/msgs_test.go index 843c01d0a1..06c74482a5 100644 --- a/x/collection/msgs_test.go +++ b/x/collection/msgs_test.go @@ -5,12 +5,11 @@ import ( "strings" "testing" - "github.com/line/lbm-sdk/x/auth/legacy/legacytx" - "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" )