Skip to content

Commit

Permalink
fix: make field key matching in x/token & x/collection case-sensitive (
Browse files Browse the repository at this point in the history
…#784)

* Apply more strict field key name matching on x/token

* Lint

* Apply more strict field key name matching on x/collection

* Lint

* Update CHANGELOG.md

* Update msgs_test.go

Lint
  • Loading branch information
0Tech authored Nov 21, 2022
1 parent b13f437 commit 82ac240
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions x/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TokenClassUnpackInterfaces(any *codectypes.Any, unpacker codectypes.AnyUnpa
return unpacker.UnpackAny(any, &class)
}

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// FTClass
var _ TokenClass = (*FTClass)(nil)

Expand Down Expand Up @@ -109,7 +109,7 @@ func (c FTClass) ValidateBasic() error {
return nil
}

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// NFTClass
var _ TokenClass = (*NFTClass)(nil)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -217,7 +217,7 @@ func ParseCoin(coinStr string) (*Coin, error) {
return &coin, nil
}

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Coins
type Coins []Coin

Expand Down
18 changes: 9 additions & 9 deletions x/collection/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -114,7 +114,7 @@ func ownerKeyPrefixByContractID(contractID string) []byte {
return key
}

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// nft
func nftKey(contractID string, tokenID string) []byte {
prefix := nftKeyPrefixByContractID(contractID)
Expand Down Expand Up @@ -152,7 +152,7 @@ func splitNFTKey(key []byte) (contractID string, tokenID string) {
return
}

//-----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// parent
func parentKey(contractID string, tokenID string) []byte {
prefix := parentKeyPrefixByContractID(contractID)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 16 additions & 13 deletions x/collection/keeper/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions x/collection/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions x/collection/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion x/token/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions x/token/keeper/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions x/token/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package token_test

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()),
Expand Down
10 changes: 5 additions & 5 deletions x/token/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 82ac240

Please sign in to comment.