Skip to content

Commit 82ac240

Browse files
authored
fix: make field key matching in x/token & x/collection case-sensitive (#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
1 parent b13f437 commit 82ac240

10 files changed

+58
-51
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4949
* (global) [\#782](https://github.com/line/lbm-sdk/pull/782) add unhandled return error handling
5050
* (x/collection,x/token) [\#798](https://github.com/line/lbm-sdk/pull/798) Fix x/collection ModifyContract
5151
* (ci) [\#803](https://github.com/line/lbm-sdk/pull/803) fix test flow to install libsodium
52+
* (x/collection,token) [\#784](https://github.com/line/lbm-sdk/pull/784) Make field key matching in x/token & x/collection case-sensitive
5253

5354
### Breaking Changes
5455
* (cli) [\#773](https://github.com/line/lbm-sdk/pull/773) guide users to use generate-only in messages for x/foundation authority

x/collection/collection.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TokenClassUnpackInterfaces(any *codectypes.Any, unpacker codectypes.AnyUnpa
7272
return unpacker.UnpackAny(any, &class)
7373
}
7474

75-
//-----------------------------------------------------------------------------
75+
// ----------------------------------------------------------------------------
7676
// FTClass
7777
var _ TokenClass = (*FTClass)(nil)
7878

@@ -109,7 +109,7 @@ func (c FTClass) ValidateBasic() error {
109109
return nil
110110
}
111111

112-
//-----------------------------------------------------------------------------
112+
// ----------------------------------------------------------------------------
113113
// NFTClass
114114
var _ TokenClass = (*NFTClass)(nil)
115115

@@ -143,7 +143,7 @@ func (c NFTClass) ValidateBasic() error {
143143
return nil
144144
}
145145

146-
//-----------------------------------------------------------------------------
146+
// ----------------------------------------------------------------------------
147147
// Coin
148148
func NewFTCoin(classID string, amount sdk.Int) Coin {
149149
return NewCoin(NewFTID(classID), amount)
@@ -217,7 +217,7 @@ func ParseCoin(coinStr string) (*Coin, error) {
217217
return &coin, nil
218218
}
219219

220-
//-----------------------------------------------------------------------------
220+
// ----------------------------------------------------------------------------
221221
// Coins
222222
type Coins []Coin
223223

x/collection/keeper/keys.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func splitBalanceKey(key []byte) (contractID string, address sdk.AccAddress, tok
8787
return
8888
}
8989

90-
//-----------------------------------------------------------------------------
90+
// ----------------------------------------------------------------------------
9191
// owner
9292
func ownerKey(contractID string, tokenID string) []byte {
9393
prefix := ownerKeyPrefixByContractID(contractID)
@@ -114,7 +114,7 @@ func ownerKeyPrefixByContractID(contractID string) []byte {
114114
return key
115115
}
116116

117-
//-----------------------------------------------------------------------------
117+
// ----------------------------------------------------------------------------
118118
// nft
119119
func nftKey(contractID string, tokenID string) []byte {
120120
prefix := nftKeyPrefixByContractID(contractID)
@@ -152,7 +152,7 @@ func splitNFTKey(key []byte) (contractID string, tokenID string) {
152152
return
153153
}
154154

155-
//-----------------------------------------------------------------------------
155+
// ----------------------------------------------------------------------------
156156
// parent
157157
func parentKey(contractID string, tokenID string) []byte {
158158
prefix := parentKeyPrefixByContractID(contractID)
@@ -190,7 +190,7 @@ func splitParentKey(key []byte) (contractID string, tokenID string) {
190190
return
191191
}
192192

193-
//-----------------------------------------------------------------------------
193+
// ----------------------------------------------------------------------------
194194
// child
195195
func childKey(contractID string, tokenID, childID string) []byte {
196196
prefix := childKeyPrefixByTokenID(contractID, tokenID)
@@ -248,7 +248,7 @@ func splitChildKey(key []byte) (contractID string, tokenID, childID string) {
248248
return
249249
}
250250

251-
//-----------------------------------------------------------------------------
251+
// ----------------------------------------------------------------------------
252252
func contractKey(contractID string) []byte {
253253
key := make([]byte, len(contractKeyPrefix)+len(contractID))
254254

@@ -328,7 +328,7 @@ func nextClassIDKey(contractID string) []byte {
328328
return key
329329
}
330330

331-
//-----------------------------------------------------------------------------
331+
// ----------------------------------------------------------------------------
332332
func authorizationKey(contractID string, operator, holder sdk.AccAddress) []byte {
333333
prefix := authorizationKeyPrefixByOperator(contractID, operator)
334334
key := make([]byte, len(prefix)+len(holder))
@@ -385,7 +385,7 @@ func splitAuthorizationKey(key []byte) (contractID string, operator, holder sdk.
385385
return
386386
}
387387

388-
//-----------------------------------------------------------------------------
388+
// ----------------------------------------------------------------------------
389389
func grantKey(contractID string, grantee sdk.AccAddress, permission collection.Permission) []byte {
390390
prefix := grantKeyPrefixByGrantee(contractID, grantee)
391391
key := make([]byte, len(prefix)+1)
@@ -442,7 +442,7 @@ func splitGrantKey(key []byte) (contractID string, grantee sdk.AccAddress, permi
442442
return
443443
}
444444

445-
//-----------------------------------------------------------------------------
445+
// ----------------------------------------------------------------------------
446446
// statistics
447447
func statisticKey(keyPrefix []byte, contractID string, classID string) []byte {
448448
prefix := statisticKeyPrefixByContractID(keyPrefix, contractID)
@@ -480,7 +480,7 @@ func splitStatisticKey(keyPrefix, key []byte) (contractID string, classID string
480480
return
481481
}
482482

483-
//-----------------------------------------------------------------------------
483+
// ----------------------------------------------------------------------------
484484
// legacy keys
485485
func legacyTokenKey(contractID string, tokenID string) []byte {
486486
prefix := legacyTokenKeyPrefixByContractID(contractID)

x/collection/keeper/supply.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,20 @@ func (k Keeper) ModifyContract(ctx sdk.Context, contractID string, operator sdk.
317317
return err
318318
}
319319

320-
modifiers := map[string]func(string){
321-
collection.AttributeKeyName.String(): func(name string) {
320+
modifiers := map[collection.AttributeKey]func(string){
321+
collection.AttributeKeyName: func(name string) {
322322
contract.Name = name
323323
},
324-
collection.AttributeKeyBaseImgURI.String(): func(uri string) {
324+
collection.AttributeKeyBaseImgURI: func(uri string) {
325325
contract.BaseImgUri = uri
326326
},
327-
collection.AttributeKeyMeta.String(): func(meta string) {
327+
collection.AttributeKeyMeta: func(meta string) {
328328
contract.Meta = meta
329329
},
330330
}
331331
for _, change := range changes {
332-
modifiers[change.Key](change.Value)
332+
key := collection.AttributeKeyFromString(change.Key)
333+
modifiers[key](change.Value)
333334
}
334335

335336
k.setContract(ctx, *contract)
@@ -351,16 +352,17 @@ func (k Keeper) ModifyTokenClass(ctx sdk.Context, contractID string, classID str
351352
return err
352353
}
353354

354-
modifiers := map[string]func(string){
355-
collection.AttributeKeyName.String(): func(name string) {
355+
modifiers := map[collection.AttributeKey]func(string){
356+
collection.AttributeKeyName: func(name string) {
356357
class.SetName(name)
357358
},
358-
collection.AttributeKeyMeta.String(): func(meta string) {
359+
collection.AttributeKeyMeta: func(meta string) {
359360
class.SetMeta(meta)
360361
},
361362
}
362363
for _, change := range changes {
363-
modifiers[change.Key](change.Value)
364+
key := collection.AttributeKeyFromString(change.Key)
365+
modifiers[key](change.Value)
364366
}
365367

366368
k.setTokenClass(ctx, contractID, class)
@@ -383,16 +385,17 @@ func (k Keeper) ModifyNFT(ctx sdk.Context, contractID string, tokenID string, op
383385
return err
384386
}
385387

386-
modifiers := map[string]func(string){
387-
collection.AttributeKeyName.String(): func(name string) {
388+
modifiers := map[collection.AttributeKey]func(string){
389+
collection.AttributeKeyName: func(name string) {
388390
token.Name = name
389391
},
390-
collection.AttributeKeyMeta.String(): func(meta string) {
392+
collection.AttributeKeyMeta: func(meta string) {
391393
token.Meta = meta
392394
},
393395
}
394396
for _, change := range changes {
395-
modifiers[change.Key](change.Value)
397+
key := collection.AttributeKeyFromString(change.Key)
398+
modifiers[key](change.Value)
396399
}
397400

398401
k.setNFT(ctx, contractID, *token)

x/collection/msgs.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,26 @@ func ValidatePermission(permission Permission) error {
166166
}
167167

168168
func validateContractChange(change Attribute) error {
169-
validators := map[AttributeKey]func(string) error{
170-
AttributeKeyName: validateName,
171-
AttributeKeyBaseImgURI: validateBaseImgURI,
172-
AttributeKeyMeta: validateMeta,
169+
validators := map[string]func(string) error{
170+
AttributeKeyName.String(): validateName,
171+
AttributeKeyBaseImgURI.String(): validateBaseImgURI,
172+
AttributeKeyMeta.String(): validateMeta,
173173
}
174174

175175
return validateChange(change, validators)
176176
}
177177

178178
func validateTokenClassChange(change Attribute) error {
179-
validators := map[AttributeKey]func(string) error{
180-
AttributeKeyName: validateName,
181-
AttributeKeyMeta: validateMeta,
179+
validators := map[string]func(string) error{
180+
AttributeKeyName.String(): validateName,
181+
AttributeKeyMeta.String(): validateMeta,
182182
}
183183

184184
return validateChange(change, validators)
185185
}
186186

187-
func validateChange(change Attribute, validators map[AttributeKey]func(string) error) error {
188-
validator, ok := validators[AttributeKeyFromString(change.Key)]
187+
func validateChange(change Attribute, validators map[string]func(string) error) error {
188+
validator, ok := validators[change.Key]
189189
if !ok {
190190
return sdkerrors.ErrInvalidRequest.Wrapf("invalid field: %s", change.Key)
191191
}

x/collection/msgs_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package collection_test
22

33
import (
44
"fmt"
5-
"github.com/line/lbm-sdk/x/auth/legacy/legacytx"
5+
"strings"
66
"testing"
77

88
"github.com/stretchr/testify/require"
99

1010
"github.com/line/lbm-sdk/crypto/keys/secp256k1"
1111
sdk "github.com/line/lbm-sdk/types"
12+
"github.com/line/lbm-sdk/x/auth/legacy/legacytx"
1213
"github.com/line/lbm-sdk/x/collection"
1314
)
1415

@@ -1108,7 +1109,7 @@ func TestMsgModify(t *testing.T) {
11081109
addrs[i] = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
11091110
}
11101111

1111-
changes := []collection.Change{{Field: "name", Value: "New test"}}
1112+
changes := []collection.Change{{Field: collection.AttributeKeyName.String(), Value: "New test"}}
11121113
testCases := map[string]struct {
11131114
contractID string
11141115
owner sdk.AccAddress
@@ -1149,7 +1150,7 @@ func TestMsgModify(t *testing.T) {
11491150
"invalid key of change": {
11501151
contractID: "deadbeef",
11511152
owner: addrs[0],
1152-
changes: []collection.Change{{Value: "tt"}},
1153+
changes: []collection.Change{{Field: strings.ToUpper(collection.AttributeKeyName.String()) , Value: "tt"}},
11531154
},
11541155
"invalid value of change": {
11551156
contractID: "deadbeef",

x/token/keeper/msg_server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (s *KeeperTestSuite) TestMsgModify() {
412412
req := &token.MsgModify{
413413
ContractId: s.contractID,
414414
Owner: tc.grantee.String(),
415-
Changes: []token.Pair{{Field: token.AttributeKeyName.String(), Value: "hello"}},
415+
Changes: []token.Pair{{Field: token.AttributeKeyImageURI.String(), Value: "uri"}},
416416
}
417417
res, err := s.msgServer.Modify(sdk.WrapSDKContext(ctx), req)
418418
if !tc.valid {

x/token/keeper/supply.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,20 @@ func (k Keeper) modify(ctx sdk.Context, contractID string, changes []token.Pair)
285285
return err
286286
}
287287

288-
modifiers := map[string]func(string){
289-
token.AttributeKeyName.String(): func(name string) {
288+
modifiers := map[token.AttributeKey]func(string){
289+
token.AttributeKeyName: func(name string) {
290290
class.Name = name
291291
},
292-
token.AttributeKeyImageURI.String(): func(uri string) {
292+
token.AttributeKeyImageURI: func(uri string) {
293293
class.ImageUri = uri
294294
},
295-
token.AttributeKeyMeta.String(): func(meta string) {
295+
token.AttributeKeyMeta: func(meta string) {
296296
class.Meta = meta
297297
},
298298
}
299299
for _, change := range changes {
300-
modifiers[change.Field](change.Value)
300+
key := token.AttributeKeyFromString(change.Field)
301+
modifiers[key](change.Value)
301302
}
302303

303304
k.setClass(ctx, *class)

x/token/msgs_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package token_test
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/stretchr/testify/require"
@@ -594,7 +595,7 @@ func TestMsgModify(t *testing.T) {
594595
"invalid key of change": {
595596
contractID: "deadbeef",
596597
grantee: addrs[0],
597-
changes: []token.Pair{{Value: "tt"}},
598+
changes: []token.Pair{{Field: strings.ToUpper(token.AttributeKeyName.String()), Value: "tt"}},
598599
},
599600
"invalid value of change": {
600601
contractID: "deadbeef",
@@ -864,7 +865,7 @@ func TestAminoJSON(t *testing.T) {
864865
&token.MsgModify{
865866
ContractId: contractId,
866867
Owner: addrs[0].String(),
867-
Changes: []token.Pair{token.Pair{Field: token.AttributeKeyName.String(), Value: "New test"}},
868+
Changes: []token.Pair{{Field: token.AttributeKeyName.String(), Value: "New test"}},
868869
},
869870
"/lbm.token.v1.MsgModify",
870871
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()),

x/token/validation.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ func ValidatePermission(permission Permission) error {
8181
}
8282

8383
func validateChange(change Pair) error {
84-
validators := map[AttributeKey]func(string) error{
85-
AttributeKeyName: validateName,
86-
AttributeKeyImageURI: validateImageURI,
87-
AttributeKeyMeta: validateMeta,
84+
validators := map[string]func(string) error{
85+
AttributeKeyName.String(): validateName,
86+
AttributeKeyImageURI.String(): validateImageURI,
87+
AttributeKeyMeta.String(): validateMeta,
8888
}
8989

90-
validator, ok := validators[AttributeKeyFromString(change.Field)]
90+
validator, ok := validators[change.Field]
9191
if !ok {
9292
return sdkerrors.ErrInvalidRequest.Wrapf("invalid field: %s", change.Field)
9393
}

0 commit comments

Comments
 (0)