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

fix: use accAddress to compare in validatebasic function in collection & token modules #1288

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/auth) [#1274](https://github.com/Finschia/finschia-sdk/pull/1274) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking.
* (x/collection) [\#1276](https://github.com/Finschia/finschia-sdk/pull/1276) eliminates potential risk for Insufficient Sanity Check of tokenID in Genesis
* (x/foundation) [\#1277](https://github.com/Finschia/finschia-sdk/pull/1277) add init logic of foundation module accounts to InitGenesis in order to eliminate potential panic
* (x/collection, x/token) [\#1288](https://github.com/Finschia/finschia-sdk/pull/1288) use accAddress to compare in validatebasic function in collection & token modules

### Removed

Expand Down
9 changes: 6 additions & 3 deletions x/collection/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,17 @@ func (m MsgAuthorizeOperator) ValidateBasic() error {
return err
}

if _, err := sdk.AccAddressFromBech32(m.Holder); err != nil {
holderAcc, err := sdk.AccAddressFromBech32(m.Holder)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid holder address: %s", m.Holder)
}
if _, err := sdk.AccAddressFromBech32(m.Operator); err != nil {

operatorAcc, err := sdk.AccAddressFromBech32(m.Operator)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid operator address: %s", m.Operator)
}

if m.Operator == m.Holder {
if holderAcc.Equals(operatorAcc) {
return ErrApproverProxySame
}

Expand Down
26 changes: 19 additions & 7 deletions x/collection/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,15 @@ func TestMsgOperatorSendNFT(t *testing.T) {
}

func TestMsgAuthorizeOperator(t *testing.T) {
addrs := make([]sdk.AccAddress, 2)
addrs := make([]string, 2)
for i := range addrs {
addrs[i] = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
addrs[i] = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String()
}

testCases := map[string]struct {
contractID string
holder sdk.AccAddress
operator sdk.AccAddress
holder string
operator string
err error
}{
"valid msg": {
Expand All @@ -407,22 +407,34 @@ func TestMsgAuthorizeOperator(t *testing.T) {
holder: addrs[0],
err: sdkerrors.ErrInvalidAddress,
},
"proxy and approver should be different": {
contractID: "deadbeef",
holder: addrs[0],
operator: addrs[0],
err: collection.ErrApproverProxySame,
},
"proxy and approver should be different (uppercase)": {
contractID: "deadbeef",
holder: addrs[0],
operator: strings.ToUpper(addrs[0]),
err: collection.ErrApproverProxySame,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
msg := collection.MsgAuthorizeOperator{
ContractId: tc.contractID,
Holder: tc.holder.String(),
Operator: tc.operator.String(),
Holder: tc.holder,
Operator: tc.operator,
}

require.ErrorIs(t, msg.ValidateBasic(), tc.err)
if tc.err != nil {
return
}

require.Equal(t, []sdk.AccAddress{tc.holder}, msg.GetSigners())
require.Equal(t, []sdk.AccAddress{sdk.MustAccAddressFromBech32(tc.holder)}, msg.GetSigners())
})
}
}
Expand Down
9 changes: 6 additions & 3 deletions x/token/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,17 @@ func (m MsgAuthorizeOperator) ValidateBasic() error {
return err
}

if _, err := sdk.AccAddressFromBech32(m.Holder); err != nil {
holderAcc, err := sdk.AccAddressFromBech32(m.Holder)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid holder address: %s", m.Holder)
}
if _, err := sdk.AccAddressFromBech32(m.Operator); err != nil {

operatorAcc, err := sdk.AccAddressFromBech32(m.Operator)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid operator address: %s", m.Operator)
}

if m.Operator == m.Holder {
if holderAcc.Equals(operatorAcc) {
return ErrApproverProxySame
}

Expand Down
20 changes: 13 additions & 7 deletions x/token/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ func TestMsgRevokeOperator(t *testing.T) {
}

func TestMsgAuthorizeOperator(t *testing.T) {
addrs := make([]sdk.AccAddress, 2)
addrs := make([]string, 2)
for i := range addrs {
addrs[i] = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
addrs[i] = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String()
}

testCases := map[string]struct {
contractID string
holder sdk.AccAddress
operator sdk.AccAddress
holder string
operator string
err error
}{
"valid msg": {
Expand Down Expand Up @@ -258,14 +258,20 @@ func TestMsgAuthorizeOperator(t *testing.T) {
operator: addrs[0],
err: token.ErrApproverProxySame,
},
"proxy and approver should be different (uppercase)": {
contractID: "deadbeef",
holder: addrs[0],
operator: strings.ToUpper(addrs[0]),
err: token.ErrApproverProxySame,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
msg := token.MsgAuthorizeOperator{
ContractId: tc.contractID,
Holder: tc.holder.String(),
Operator: tc.operator.String(),
Holder: tc.holder,
Operator: tc.operator,
}

err := msg.ValidateBasic()
Expand All @@ -274,7 +280,7 @@ func TestMsgAuthorizeOperator(t *testing.T) {
return
}

require.Equal(t, []sdk.AccAddress{tc.holder}, msg.GetSigners())
require.Equal(t, []sdk.AccAddress{sdk.MustAccAddressFromBech32(tc.holder)}, msg.GetSigners())
})
}
}
Expand Down
Loading