From 1dba6735739e9b4556267339f0b67eaec9c609ef Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Fri, 13 Aug 2021 08:34:00 -0700 Subject: [PATCH] refactor!: change GetSigners return type to []sdk.AccAddress (#9915) ## Description Changes the `sdk.Msg` interface method `GetSigners() []string` to `GetSigners() []sdk.AccAddress` Closes: #9885 +change GetSigner return type +defer address checking to each msg's `ValidateBasic` method +clean up consistency/redundancy issues in validate basic --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 1 - baseapp/baseapp.go | 3 +- baseapp/baseapp_test.go | 30 +++++----- client/tx/tx.go | 2 +- server/mock/tx.go | 2 +- server/rosetta/converter.go | 2 +- testutil/testdata/tx.go | 23 ++++++-- types/tx/types.go | 30 +--------- types/tx/types_test.go | 43 -------------- types/tx_msg.go | 4 +- types/tx_msg_test.go | 2 +- x/auth/ante/basic.go | 2 +- x/auth/migrations/legacytx/stdtx.go | 10 +--- x/auth/vesting/types/msgs.go | 22 +++----- x/authz/keeper/keeper.go | 6 +- x/authz/msgs.go | 28 ++++----- x/bank/types/msgs.go | 32 +++++------ x/bank/types/msgs_test.go | 16 +++--- x/crisis/types/msgs.go | 10 ++-- x/distribution/types/msg.go | 49 ++++++++-------- x/evidence/types/msgs.go | 9 +-- x/evidence/types/msgs_test.go | 2 +- x/feegrant/msgs.go | 27 ++++----- x/feegrant/msgs_test.go | 4 +- x/genutil/client/testutil/suite.go | 2 +- x/gov/types/msgs.go | 38 +++++++------ x/slashing/types/msg.go | 15 ++--- x/staking/types/msg.go | 88 ++++++++++++----------------- 28 files changed, 211 insertions(+), 291 deletions(-) delete mode 100644 types/tx/types_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 296baebb8cbc..061baa2e7cab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,7 +59,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#9519](https://github.com/cosmos/cosmos-sdk/pull/9519) `DeleteDeposits` renamed to `DeleteAndBurnDeposits`, `RefundDeposits` renamed to `RefundAndDeleteDeposits` * (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Removed deprecated `clientCtx.JSONCodec` from `client.Context`. * (codec) [\#9521](https://github.com/cosmos/cosmos-sdk/pull/9521) Rename `EncodingConfig.Marshaler` to `Codec`. -* [\#9418](https://github.com/cosmos/cosmos-sdk/pull/9418) `sdk.Msg`'s `GetSigners()` method updated to return `[]string`. * [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) `RESTHandlerFn` argument is removed from the `gov/NewProposalHandler`. * [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) `types/rest` package moved to `testutil/rest`. * [\#9432](https://github.com/cosmos/cosmos-sdk/pull/9432) `ConsensusParamsKeyTable` moved from `params/keeper` to `params/types` diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 903fe55f926e..d45656ac9879 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -19,7 +19,6 @@ import ( "github.com/cosmos/cosmos-sdk/store/rootmulti" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - sdktx "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" ) @@ -511,7 +510,7 @@ func validateBasicTxMsgs(msgs []sdk.Msg) error { } for _, msg := range msgs { - err := sdktx.ValidateMsg(msg) + err := msg.ValidateBasic() if err != nil { return err } diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 4520b231d079..85d07661a3e0 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -717,10 +717,10 @@ func (msg msgCounter) String() string { return "TODO" } func (msg msgCounter) ProtoMessage() {} // Implements Msg -func (msg msgCounter) Route() string { return routeMsgCounter } -func (msg msgCounter) Type() string { return "counter1" } -func (msg msgCounter) GetSignBytes() []byte { return nil } -func (msg msgCounter) GetSigners() []string { return nil } +func (msg msgCounter) Route() string { return routeMsgCounter } +func (msg msgCounter) Type() string { return "counter1" } +func (msg msgCounter) GetSignBytes() []byte { return nil } +func (msg msgCounter) GetSigners() []sdk.AccAddress { return nil } func (msg msgCounter) ValidateBasic() error { if msg.Counter >= 0 { return nil @@ -762,10 +762,10 @@ func (msg msgCounter2) String() string { return "TODO" } func (msg msgCounter2) ProtoMessage() {} // Implements Msg -func (msg msgCounter2) Route() string { return routeMsgCounter2 } -func (msg msgCounter2) Type() string { return "counter2" } -func (msg msgCounter2) GetSignBytes() []byte { return nil } -func (msg msgCounter2) GetSigners() []string { return nil } +func (msg msgCounter2) Route() string { return routeMsgCounter2 } +func (msg msgCounter2) Type() string { return "counter2" } +func (msg msgCounter2) GetSignBytes() []byte { return nil } +func (msg msgCounter2) GetSigners() []sdk.AccAddress { return nil } func (msg msgCounter2) ValidateBasic() error { if msg.Counter >= 0 { return nil @@ -779,13 +779,13 @@ type msgKeyValue struct { Value []byte } -func (msg msgKeyValue) Reset() {} -func (msg msgKeyValue) String() string { return "TODO" } -func (msg msgKeyValue) ProtoMessage() {} -func (msg msgKeyValue) Route() string { return routeMsgKeyValue } -func (msg msgKeyValue) Type() string { return "keyValue" } -func (msg msgKeyValue) GetSignBytes() []byte { return nil } -func (msg msgKeyValue) GetSigners() []string { return nil } +func (msg msgKeyValue) Reset() {} +func (msg msgKeyValue) String() string { return "TODO" } +func (msg msgKeyValue) ProtoMessage() {} +func (msg msgKeyValue) Route() string { return routeMsgKeyValue } +func (msg msgKeyValue) Type() string { return "keyValue" } +func (msg msgKeyValue) GetSignBytes() []byte { return nil } +func (msg msgKeyValue) GetSigners() []sdk.AccAddress { return nil } func (msg msgKeyValue) ValidateBasic() error { if msg.Key == nil { return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "key cannot be nil") diff --git a/client/tx/tx.go b/client/tx/tx.go index b6dffd727a0b..4adaaa0e1c7b 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -35,7 +35,7 @@ func GenerateOrBroadcastTxWithFactory(clientCtx client.Context, txf Factory, msg // Right now, we're factorizing that call inside this function. // ref: https://github.com/cosmos/cosmos-sdk/pull/9236#discussion_r623803504 for _, msg := range msgs { - if err := tx.ValidateMsg(msg); err != nil { + if err := msg.ValidateBasic(); err != nil { return err } } diff --git a/server/mock/tx.go b/server/mock/tx.go index 9bdadf46d0e9..0cb79c28986f 100644 --- a/server/mock/tx.go +++ b/server/mock/tx.go @@ -58,7 +58,7 @@ func (tx kvstoreTx) ValidateBasic() error { return nil } -func (tx kvstoreTx) GetSigners() []string { +func (tx kvstoreTx) GetSigners() []sdk.AccAddress { return nil } diff --git a/server/rosetta/converter.go b/server/rosetta/converter.go index 54808821e247..4fdd87c75b67 100644 --- a/server/rosetta/converter.go +++ b/server/rosetta/converter.go @@ -254,7 +254,7 @@ func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, e op := &rosettatypes.Operation{ Type: opName, Status: &status, - Account: &rosettatypes.AccountIdentifier{Address: signer}, + Account: &rosettatypes.AccountIdentifier{Address: signer.String()}, Metadata: meta, } diff --git a/testutil/testdata/tx.go b/testutil/testdata/tx.go index d4d2a758ebfb..e12f0467986e 100644 --- a/testutil/testdata/tx.go +++ b/testutil/testdata/tx.go @@ -2,6 +2,7 @@ package testdata import ( "encoding/json" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" @@ -62,12 +63,24 @@ func (msg *TestMsg) GetSignBytes() []byte { } return sdk.MustSortJSON(bz) } -func (msg *TestMsg) GetSigners() []string { - return msg.Signers +func (msg *TestMsg) GetSigners() []sdk.AccAddress { + signers := make([]sdk.AccAddress, 0, len(msg.Signers)) + for _, addr := range msg.Signers { + a, _ := sdk.AccAddressFromBech32(addr) + signers = append(signers, a) + } + return signers +} +func (msg *TestMsg) ValidateBasic() error { + for _, addr := range msg.Signers { + if _, err := sdk.AccAddressFromBech32(addr); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid signer address: %s", err) + } + } + return nil } -func (msg *TestMsg) ValidateBasic() error { return nil } var _ sdk.Msg = &MsgCreateDog{} -func (msg *MsgCreateDog) GetSigners() []string { return []string{} } -func (msg *MsgCreateDog) ValidateBasic() error { return nil } +func (msg *MsgCreateDog) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{} } +func (msg *MsgCreateDog) ValidateBasic() error { return nil } diff --git a/types/tx/types.go b/types/tx/types.go index b8b23ddb7f87..f961794cb8a7 100644 --- a/types/tx/types.go +++ b/types/tx/types.go @@ -97,14 +97,9 @@ func (t *Tx) GetSigners() []sdk.AccAddress { for _, msg := range t.GetMsgs() { for _, addr := range msg.GetSigners() { - if !seen[addr] { - signer, err := sdk.AccAddressFromBech32(addr) - if err != nil { - panic(err) - } - - signers = append(signers, signer) - seen[addr] = true + if !seen[addr.String()] { + signers = append(signers, addr) + seen[addr.String()] = true } } } @@ -195,22 +190,3 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterInterface("cosmos.tx.v1beta1.Tx", (*sdk.Tx)(nil)) registry.RegisterImplementations((*sdk.Tx)(nil), &Tx{}) } - -// ValidateMsg calls the `sdk.Msg.ValidateBasic()` -// also validates all the signers are valid bech32 addresses. -func ValidateMsg(msg sdk.Msg) error { - err := msg.ValidateBasic() - if err != nil { - return err - } - - signers := msg.GetSigners() - for _, signer := range signers { - _, err = sdk.AccAddressFromBech32(signer) - if err != nil { - return err - } - } - - return nil -} diff --git a/types/tx/types_test.go b/types/tx/types_test.go deleted file mode 100644 index c5f2660ce2ce..000000000000 --- a/types/tx/types_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package tx_test - -import ( - "testing" - - "github.com/cosmos/cosmos-sdk/testutil/testdata" - "github.com/cosmos/cosmos-sdk/types/tx" - "github.com/stretchr/testify/suite" -) - -type testMsgSuite struct { - suite.Suite -} - -func TestValidateMsg(t *testing.T) { - suite.Run(t, new(testMsgSuite)) -} - -func (s *testMsgSuite) TestMsg() { - cases := []struct { - signer []byte - expErr bool - }{ - { - []byte(""), - true, - }, - { - []byte("validAddress"), - false, - }, - } - - for _, c := range cases { - msg := testdata.NewTestMsg(c.signer) - err := tx.ValidateMsg(msg) - if c.expErr { - s.Require().Error(err) - } else { - s.Require().NoError(err) - } - } -} diff --git a/types/tx_msg.go b/types/tx_msg.go index ae84b9e4fed6..42d01fdc55ee 100644 --- a/types/tx_msg.go +++ b/types/tx_msg.go @@ -15,10 +15,10 @@ type ( // doesn't require access to any other information. ValidateBasic() error - // Signers returns the bech32-encoded addrs of signers that must sign. + // Signers returns the addrs of signers that must sign. // CONTRACT: All signatures must be present to be valid. // CONTRACT: Returns addrs in some deterministic order. - GetSigners() []string + GetSigners() []AccAddress } // Fee defines an interface for an application application-defined concrete diff --git a/types/tx_msg_test.go b/types/tx_msg_test.go index 1fb0e387c1db..7e72035d5175 100644 --- a/types/tx_msg_test.go +++ b/types/tx_msg_test.go @@ -23,7 +23,7 @@ func (s *testMsgSuite) TestMsg() { msg := testdata.NewTestMsg(accAddr) s.Require().NotNil(msg) - s.Require().Equal([]string{accAddr.String()}, msg.GetSigners()) + s.Require().True(accAddr.Equals(msg.GetSigners()[0])) s.Require().Equal("TestMsg", msg.Route()) s.Require().Equal("Test message", msg.Type()) s.Require().Nil(msg.ValidateBasic()) diff --git a/x/auth/ante/basic.go b/x/auth/ante/basic.go index d456b8b1b230..d42aed214444 100644 --- a/x/auth/ante/basic.go +++ b/x/auth/ante/basic.go @@ -11,7 +11,7 @@ import ( authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" ) -// ValidateBasicDecorator will call tx.ValidateBasic, ValidateMsg(for each msg inside tx) +// ValidateBasicDecorator will call tx.ValidateBasic, msg.ValidateBasic(for each msg inside tx) // and return any non-nil error. // If ValidateBasic passes, decorator calls next AnteHandler in chain. Note, // ValidateBasicDecorator decorator will not get executed on ReCheckTx since it diff --git a/x/auth/migrations/legacytx/stdtx.go b/x/auth/migrations/legacytx/stdtx.go index d213fcac9c6b..55ec38e603cc 100644 --- a/x/auth/migrations/legacytx/stdtx.go +++ b/x/auth/migrations/legacytx/stdtx.go @@ -143,13 +143,9 @@ func (tx StdTx) GetSigners() []sdk.AccAddress { for _, msg := range tx.GetMsgs() { for _, addr := range msg.GetSigners() { - if !seen[addr] { - signer, err := sdk.AccAddressFromBech32(addr) - if err != nil { - panic(err) - } - signers = append(signers, signer) - seen[addr] = true + if !seen[addr.String()] { + signers = append(signers, addr) + seen[addr.String()] = true } } } diff --git a/x/auth/vesting/types/msgs.go b/x/auth/vesting/types/msgs.go index 51c85f0695e1..4a0034901e0d 100644 --- a/x/auth/vesting/types/msgs.go +++ b/x/auth/vesting/types/msgs.go @@ -30,20 +30,11 @@ func (msg MsgCreateVestingAccount) Type() string { return TypeMsgCreateVestingAc // ValidateBasic Implements Msg. func (msg MsgCreateVestingAccount) ValidateBasic() error { - from, err := sdk.AccAddressFromBech32(msg.FromAddress) - if err != nil { - return err + if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err) } - to, err := sdk.AccAddressFromBech32(msg.ToAddress) - if err != nil { - return err - } - if err := sdk.VerifyAddressFormat(from); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address: %s", err) - } - - if err := sdk.VerifyAddressFormat(to); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address: %s", err) + if _, err := sdk.AccAddressFromBech32(msg.ToAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err) } if !msg.Amount.IsValid() { @@ -68,6 +59,7 @@ func (msg MsgCreateVestingAccount) GetSignBytes() []byte { } // GetSigners returns the expected signers for a MsgCreateVestingAccount. -func (msg MsgCreateVestingAccount) GetSigners() []string { - return []string{msg.FromAddress} +func (msg MsgCreateVestingAccount) GetSigners() []sdk.AccAddress { + addr, _ := sdk.AccAddressFromBech32(msg.FromAddress) + return []sdk.AccAddress{addr} } diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index 1d71479f01c9..e166a63e5806 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -79,10 +79,8 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] if len(signers) != 1 { return nil, sdkerrors.ErrInvalidRequest.Wrap("authorization can be given to msg with only one signer") } - granter, err := sdk.AccAddressFromBech32(signers[0]) - if err != nil { - return nil, err - } + granter := signers[0] + // if granter != grantee then check authorization.Accept, otherwise we implicitly accept. if !granter.Equals(grantee) { authorization, _ := k.GetCleanAuthorization(ctx, grantee, granter, sdk.MsgTypeURL(msg)) diff --git a/x/authz/msgs.go b/x/authz/msgs.go index a2de4b618663..8ec3c3f00622 100644 --- a/x/authz/msgs.go +++ b/x/authz/msgs.go @@ -42,19 +42,20 @@ func NewMsgGrant(granter sdk.AccAddress, grantee sdk.AccAddress, a Authorization } // GetSigners implements Msg -func (msg MsgGrant) GetSigners() []string { - return []string{msg.Granter} +func (msg MsgGrant) GetSigners() []sdk.AccAddress { + granter, _ := sdk.AccAddressFromBech32(msg.Granter) + return []sdk.AccAddress{granter} } // ValidateBasic implements Msg func (msg MsgGrant) ValidateBasic() error { granter, err := sdk.AccAddressFromBech32(msg.Granter) if err != nil { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid granter address") + return sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err) } grantee, err := sdk.AccAddressFromBech32(msg.Grantee) if err != nil { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid granter address") + return sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err) } if granter.Equals(grantee) { @@ -126,19 +127,20 @@ func NewMsgRevoke(granter sdk.AccAddress, grantee sdk.AccAddress, msgTypeURL str } // GetSigners implements Msg -func (msg MsgRevoke) GetSigners() []string { - return []string{msg.Granter} +func (msg MsgRevoke) GetSigners() []sdk.AccAddress { + granter, _ := sdk.AccAddressFromBech32(msg.Granter) + return []sdk.AccAddress{granter} } // ValidateBasic implements MsgRequest.ValidateBasic func (msg MsgRevoke) ValidateBasic() error { granter, err := sdk.AccAddressFromBech32(msg.Granter) if err != nil { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid granter address") + return sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err) } grantee, err := sdk.AccAddressFromBech32(msg.Grantee) if err != nil { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid grantee address") + return sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err) } if granter.Equals(grantee) { @@ -201,15 +203,15 @@ func (msg MsgExec) GetMessages() ([]sdk.Msg, error) { } // GetSigners implements Msg -func (msg MsgExec) GetSigners() []string { - return []string{msg.Grantee} +func (msg MsgExec) GetSigners() []sdk.AccAddress { + grantee, _ := sdk.AccAddressFromBech32(msg.Grantee) + return []sdk.AccAddress{grantee} } // ValidateBasic implements Msg func (msg MsgExec) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(msg.Grantee) - if err != nil { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid grantee address") + if _, err := sdk.AccAddressFromBech32(msg.Grantee); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err) } if len(msg.Msgs) == 0 { diff --git a/x/bank/types/msgs.go b/x/bank/types/msgs.go index 9a7d6fccedd8..8030e04519f3 100644 --- a/x/bank/types/msgs.go +++ b/x/bank/types/msgs.go @@ -27,14 +27,12 @@ func (msg MsgSend) Type() string { return TypeMsgSend } // ValidateBasic Implements Msg. func (msg MsgSend) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(msg.FromAddress) - if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid sender address (%s)", err) + if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid from address: %s", err) } - _, err = sdk.AccAddressFromBech32(msg.ToAddress) - if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid recipient address (%s)", err) + if _, err := sdk.AccAddressFromBech32(msg.ToAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid to address: %s", err) } if !msg.Amount.IsValid() { @@ -54,8 +52,9 @@ func (msg MsgSend) GetSignBytes() []byte { } // GetSigners Implements Msg. -func (msg MsgSend) GetSigners() []string { - return []string{msg.FromAddress} +func (msg MsgSend) GetSigners() []sdk.AccAddress { + fromAddress, _ := sdk.AccAddressFromBech32(msg.FromAddress) + return []sdk.AccAddress{fromAddress} } var _ sdk.Msg = &MsgMultiSend{} @@ -92,10 +91,11 @@ func (msg MsgMultiSend) GetSignBytes() []byte { } // GetSigners Implements Msg. -func (msg MsgMultiSend) GetSigners() []string { - addrs := make([]string, len(msg.Inputs)) +func (msg MsgMultiSend) GetSigners() []sdk.AccAddress { + addrs := make([]sdk.AccAddress, len(msg.Inputs)) for i, in := range msg.Inputs { - addrs[i] = in.Address + inAddr, _ := sdk.AccAddressFromBech32(in.Address) + addrs[i] = inAddr } return addrs @@ -103,9 +103,8 @@ func (msg MsgMultiSend) GetSigners() []string { // ValidateBasic - validate transaction input func (in Input) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(in.Address) - if err != nil { - return err + if _, err := sdk.AccAddressFromBech32(in.Address); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid input address: %s", err) } if !in.Coins.IsValid() { @@ -130,9 +129,8 @@ func NewInput(addr sdk.AccAddress, coins sdk.Coins) Input { // ValidateBasic - validate transaction output func (out Output) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(out.Address) - if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid output address (%s)", err) + if _, err := sdk.AccAddressFromBech32(out.Address); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid output address: %s", err) } if !out.Coins.IsValid() { diff --git a/x/bank/types/msgs_test.go b/x/bank/types/msgs_test.go index 0deaf09b2932..d11f78711591 100644 --- a/x/bank/types/msgs_test.go +++ b/x/bank/types/msgs_test.go @@ -1,7 +1,6 @@ package types import ( - "fmt" "testing" "github.com/stretchr/testify/require" @@ -40,8 +39,8 @@ func TestMsgSendValidation(t *testing.T) { {"", NewMsgSend(addr1, addrLong, atom123)}, // valid send with long addr recipient {": invalid coins", NewMsgSend(addr1, addr2, atom0)}, // non positive coin {"123atom,0eth: invalid coins", NewMsgSend(addr1, addr2, atom123eth0)}, // non positive coin in multicoins - {"Invalid sender address (empty address string is not allowed): invalid address", NewMsgSend(addrEmpty, addr2, atom123)}, - {"Invalid recipient address (empty address string is not allowed): invalid address", NewMsgSend(addr1, addrEmpty, atom123)}, + {"invalid from address: empty address string is not allowed: invalid address", NewMsgSend(addrEmpty, addr2, atom123)}, + {"invalid to address: empty address string is not allowed: invalid address", NewMsgSend(addr1, addrEmpty, atom123)}, } for _, tc := range cases { @@ -104,7 +103,7 @@ func TestInputValidation(t *testing.T) { {"", NewInput(addr2, multiCoins)}, {"", NewInput(addrLong, someCoins)}, - {"empty address string is not allowed", NewInput(addrEmpty, someCoins)}, + {"invalid input address: empty address string is not allowed: invalid address", NewInput(addrEmpty, someCoins)}, {": invalid coins", NewInput(addr1, emptyCoins)}, // invalid coins {": invalid coins", NewInput(addr1, emptyCoins2)}, // invalid coins {"10eth,0atom: invalid coins", NewInput(addr1, someEmptyCoins)}, // invalid coins @@ -145,7 +144,7 @@ func TestOutputValidation(t *testing.T) { {"", NewOutput(addr2, multiCoins)}, {"", NewOutput(addrLong, someCoins)}, - {"Invalid output address (empty address string is not allowed): invalid address", NewOutput(addrEmpty, someCoins)}, + {"invalid output address: empty address string is not allowed: invalid address", NewOutput(addrEmpty, someCoins)}, {": invalid coins", NewOutput(addr1, emptyCoins)}, // invalid coins {": invalid coins", NewOutput(addr1, emptyCoins2)}, // invalid coins {"10eth,0atom: invalid coins", NewOutput(addr1, someEmptyCoins)}, // invalid coins @@ -241,12 +240,15 @@ func TestMsgMultiSendGetSigners(t *testing.T) { var msg = NewMsgMultiSend(inputs, nil) res := msg.GetSigners() - require.Equal(t, fmt.Sprintf("%v", addrs), fmt.Sprintf("%v", res)) + for i, signer := range res { + require.Equal(t, signer.String(), addrs[i]) + } } func TestMsgSendGetSigners(t *testing.T) { from := sdk.AccAddress([]byte("input111111111111111")) msg := NewMsgSend(from, sdk.AccAddress{}, sdk.NewCoins()) res := msg.GetSigners() - require.Equal(t, fmt.Sprintf("%v", res), fmt.Sprintf("%v", []string{from.String()})) + require.Equal(t, 1, len(res)) + require.True(t, from.Equals(res[0])) } diff --git a/x/crisis/types/msgs.go b/x/crisis/types/msgs.go index f21fd5e500a0..a5450b010e7f 100644 --- a/x/crisis/types/msgs.go +++ b/x/crisis/types/msgs.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) // ensure Msg interface compliance at compile time @@ -21,8 +22,9 @@ func (msg MsgVerifyInvariant) Route() string { return ModuleName } func (msg MsgVerifyInvariant) Type() string { return "verify_invariant" } // get the bytes for the message signer to sign on -func (msg MsgVerifyInvariant) GetSigners() []string { - return []string{msg.Sender} +func (msg MsgVerifyInvariant) GetSigners() []sdk.AccAddress { + sender, _ := sdk.AccAddressFromBech32(msg.Sender) + return []sdk.AccAddress{sender} } // GetSignBytes gets the sign bytes for the msg MsgVerifyInvariant @@ -33,8 +35,8 @@ func (msg MsgVerifyInvariant) GetSignBytes() []byte { // quick validity check func (msg MsgVerifyInvariant) ValidateBasic() error { - if msg.Sender == "" { - return ErrNoSender + if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid sender address: %s", err) } return nil } diff --git a/x/distribution/types/msg.go b/x/distribution/types/msg.go index ad9c6afe8feb..2071ca0282e8 100644 --- a/x/distribution/types/msg.go +++ b/x/distribution/types/msg.go @@ -27,8 +27,9 @@ func (msg MsgSetWithdrawAddress) Route() string { return ModuleName } func (msg MsgSetWithdrawAddress) Type() string { return TypeMsgSetWithdrawAddress } // Return address that must sign over msg.GetSignBytes() -func (msg MsgSetWithdrawAddress) GetSigners() []string { - return []string{msg.DelegatorAddress} +func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress { + delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) + return []sdk.AccAddress{delegator} } // get the bytes for the message signer to sign on @@ -39,11 +40,11 @@ func (msg MsgSetWithdrawAddress) GetSignBytes() []byte { // quick validity check func (msg MsgSetWithdrawAddress) ValidateBasic() error { - if msg.DelegatorAddress == "" { - return ErrEmptyDelegatorAddr + if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } - if msg.WithdrawAddress == "" { - return ErrEmptyWithdrawAddr + if _, err := sdk.AccAddressFromBech32(msg.WithdrawAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid withdraw address: %s", err) } return nil @@ -60,8 +61,9 @@ func (msg MsgWithdrawDelegatorReward) Route() string { return ModuleName } func (msg MsgWithdrawDelegatorReward) Type() string { return TypeMsgWithdrawDelegatorReward } // Return address that must sign over msg.GetSignBytes() -func (msg MsgWithdrawDelegatorReward) GetSigners() []string { - return []string{msg.DelegatorAddress} +func (msg MsgWithdrawDelegatorReward) GetSigners() []sdk.AccAddress { + delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) + return []sdk.AccAddress{delegator} } // get the bytes for the message signer to sign on @@ -72,11 +74,11 @@ func (msg MsgWithdrawDelegatorReward) GetSignBytes() []byte { // quick validity check func (msg MsgWithdrawDelegatorReward) ValidateBasic() error { - if msg.DelegatorAddress == "" { - return ErrEmptyDelegatorAddr + if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } - if msg.ValidatorAddress == "" { - return ErrEmptyValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } return nil } @@ -91,12 +93,9 @@ func (msg MsgWithdrawValidatorCommission) Route() string { return ModuleName } func (msg MsgWithdrawValidatorCommission) Type() string { return TypeMsgWithdrawValidatorCommission } // Return address that must sign over msg.GetSignBytes() -func (msg MsgWithdrawValidatorCommission) GetSigners() []string { - valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress) - if err != nil { - panic(err) - } - return []string{sdk.AccAddress(valAddr).String()} +func (msg MsgWithdrawValidatorCommission) GetSigners() []sdk.AccAddress { + valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress) + return []sdk.AccAddress{sdk.AccAddress(valAddr)} } // get the bytes for the message signer to sign on @@ -107,8 +106,8 @@ func (msg MsgWithdrawValidatorCommission) GetSignBytes() []byte { // quick validity check func (msg MsgWithdrawValidatorCommission) ValidateBasic() error { - if msg.ValidatorAddress == "" { - return ErrEmptyValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } return nil } @@ -130,8 +129,9 @@ func (msg MsgFundCommunityPool) Type() string { return TypeMsgFundCommunityPool // GetSigners returns the signer addresses that are expected to sign the result // of GetSignBytes. -func (msg MsgFundCommunityPool) GetSigners() []string { - return []string{msg.Depositor} +func (msg MsgFundCommunityPool) GetSigners() []sdk.AccAddress { + depositor, _ := sdk.AccAddressFromBech32(msg.Depositor) + return []sdk.AccAddress{depositor} } // GetSignBytes returns the raw bytes for a MsgFundCommunityPool message that @@ -146,9 +146,8 @@ func (msg MsgFundCommunityPool) ValidateBasic() error { if !msg.Amount.IsValid() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) } - if msg.Depositor == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Depositor) + if _, err := sdk.AccAddressFromBech32(msg.Depositor); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid depositor address: %s", err) } - return nil } diff --git a/x/evidence/types/msgs.go b/x/evidence/types/msgs.go index 7581f6e09f76..89b0d414d05d 100644 --- a/x/evidence/types/msgs.go +++ b/x/evidence/types/msgs.go @@ -44,8 +44,8 @@ func (m MsgSubmitEvidence) Type() string { return TypeMsgSubmitEvidence } // ValidateBasic performs basic (non-state-dependant) validation on a MsgSubmitEvidence. func (m MsgSubmitEvidence) ValidateBasic() error { - if m.Submitter == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Submitter) + if _, err := sdk.AccAddressFromBech32(m.Submitter); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid submitter address: %s", err) } evi := m.GetEvidence() @@ -66,8 +66,9 @@ func (m MsgSubmitEvidence) GetSignBytes() []byte { } // GetSigners returns the single expected signer for a MsgSubmitEvidence. -func (m MsgSubmitEvidence) GetSigners() []string { - return []string{m.Submitter} +func (m MsgSubmitEvidence) GetSigners() []sdk.AccAddress { + submitter, _ := sdk.AccAddressFromBech32(m.Submitter) + return []sdk.AccAddress{submitter} } func (m MsgSubmitEvidence) GetEvidence() exported.Evidence { diff --git a/x/evidence/types/msgs_test.go b/x/evidence/types/msgs_test.go index c0dcd224aebf..b591e7fc86ab 100644 --- a/x/evidence/types/msgs_test.go +++ b/x/evidence/types/msgs_test.go @@ -54,7 +54,7 @@ func TestMsgSubmitEvidence(t *testing.T) { require.Equal(t, tc.expectErr, tc.msg.ValidateBasic() != nil, "unexpected result for tc #%d", i) if !tc.expectErr { - require.Equal(t, tc.msg.GetSigners(), []string{tc.submitter.String()}, "unexpected result for tc #%d", i) + require.Equal(t, tc.msg.GetSigners(), []sdk.AccAddress{tc.submitter}, "unexpected result for tc #%d", i) } } } diff --git a/x/feegrant/msgs.go b/x/feegrant/msgs.go index 72741a3db472..703ceccef0ef 100644 --- a/x/feegrant/msgs.go +++ b/x/feegrant/msgs.go @@ -38,16 +38,15 @@ func NewMsgGrantAllowance(feeAllowance FeeAllowanceI, granter, grantee sdk.AccAd // ValidateBasic implements the sdk.Msg interface. func (msg MsgGrantAllowance) ValidateBasic() error { - if msg.Granter == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing granter address") + if _, err := sdk.AccAddressFromBech32(msg.Granter); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err) } - if msg.Grantee == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing grantee address") + if _, err := sdk.AccAddressFromBech32(msg.Grantee); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err) } if msg.Grantee == msg.Granter { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization") } - allowance, err := msg.GetFeeAllowanceI() if err != nil { return err @@ -57,8 +56,9 @@ func (msg MsgGrantAllowance) ValidateBasic() error { } // GetSigners gets the granter account associated with an allowance -func (msg MsgGrantAllowance) GetSigners() []string { - return []string{msg.Granter} +func (msg MsgGrantAllowance) GetSigners() []sdk.AccAddress { + granter, _ := sdk.AccAddressFromBech32(msg.Granter) + return []sdk.AccAddress{granter} } // Type implements the LegacyMsg.Type method. @@ -101,11 +101,11 @@ func NewMsgRevokeAllowance(granter sdk.AccAddress, grantee sdk.AccAddress) MsgRe // ValidateBasic implements the sdk.Msg interface. func (msg MsgRevokeAllowance) ValidateBasic() error { - if msg.Granter == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing granter address") + if _, err := sdk.AccAddressFromBech32(msg.Granter); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err) } - if msg.Grantee == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing grantee address") + if _, err := sdk.AccAddressFromBech32(msg.Grantee); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err) } if msg.Grantee == msg.Granter { return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "addresses must be different") @@ -116,8 +116,9 @@ func (msg MsgRevokeAllowance) ValidateBasic() error { // GetSigners gets the granter address associated with an Allowance // to revoke. -func (msg MsgRevokeAllowance) GetSigners() []string { - return []string{msg.Granter} +func (msg MsgRevokeAllowance) GetSigners() []sdk.AccAddress { + granter, _ := sdk.AccAddressFromBech32(msg.Granter) + return []sdk.AccAddress{granter} } // Type implements the LegacyMsg.Type method. diff --git a/x/feegrant/msgs_test.go b/x/feegrant/msgs_test.go index 2e7d8e654dd7..402687274c86 100644 --- a/x/feegrant/msgs_test.go +++ b/x/feegrant/msgs_test.go @@ -64,7 +64,7 @@ func TestMsgGrantAllowance(t *testing.T) { require.NoError(t, err) addrSlice := msg.GetSigners() - require.Equal(t, tc.granter.String(), addrSlice[0]) + require.True(t, tc.granter.Equals(addrSlice[0])) allowance, err := msg.GetFeeAllowanceI() require.NoError(t, err) @@ -126,7 +126,7 @@ func TestMsgRevokeAllowance(t *testing.T) { if tc.valid { require.NoError(t, err) addrSlice := msg.GetSigners() - require.Equal(t, tc.granter.String(), addrSlice[0]) + require.True(t, tc.granter.Equals(addrSlice[0])) } else { require.Error(t, err) } diff --git a/x/genutil/client/testutil/suite.go b/x/genutil/client/testutil/suite.go index 41aeed9444f8..99f007de002e 100644 --- a/x/genutil/client/testutil/suite.go +++ b/x/genutil/client/testutil/suite.go @@ -88,7 +88,7 @@ func (s *IntegrationTestSuite) TestGenTxCmd() { s.Require().Len(msgs, 1) s.Require().Equal(sdk.MsgTypeURL(&types.MsgCreateValidator{}), sdk.MsgTypeURL(msgs[0])) - s.Require().Equal([]string{val.Address.String()}, msgs[0].GetSigners()) + s.Require().True(val.Address.Equals(msgs[0].GetSigners()[0])) s.Require().Equal(amount, msgs[0].(*types.MsgCreateValidator).Value) s.Require().NoError(tx.ValidateBasic()) } diff --git a/x/gov/types/msgs.go b/x/gov/types/msgs.go index ba86026637c2..d85964345376 100644 --- a/x/gov/types/msgs.go +++ b/x/gov/types/msgs.go @@ -83,8 +83,8 @@ func (m MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal } // ValidateBasic implements Msg func (m MsgSubmitProposal) ValidateBasic() error { - if m.Proposer == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Proposer) + if _, err := sdk.AccAddressFromBech32(m.Proposer); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid proposer address: %s", err) } if !m.InitialDeposit.IsValid() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.InitialDeposit.String()) @@ -114,8 +114,9 @@ func (m MsgSubmitProposal) GetSignBytes() []byte { } // GetSigners implements Msg -func (m MsgSubmitProposal) GetSigners() []string { - return []string{m.Proposer} +func (m MsgSubmitProposal) GetSigners() []sdk.AccAddress { + proposer, _ := sdk.AccAddressFromBech32(m.Proposer) + return []sdk.AccAddress{proposer} } // String implements the Stringer interface @@ -144,8 +145,8 @@ func (msg MsgDeposit) Type() string { return TypeMsgDeposit } // ValidateBasic implements Msg func (msg MsgDeposit) ValidateBasic() error { - if msg.Depositor == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Depositor) + if _, err := sdk.AccAddressFromBech32(msg.Depositor); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid depositor address: %s", err) } if !msg.Amount.IsValid() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) @@ -170,8 +171,9 @@ func (msg MsgDeposit) GetSignBytes() []byte { } // GetSigners implements Msg -func (msg MsgDeposit) GetSigners() []string { - return []string{msg.Depositor} +func (msg MsgDeposit) GetSigners() []sdk.AccAddress { + depositor, _ := sdk.AccAddressFromBech32(msg.Depositor) + return []sdk.AccAddress{depositor} } // NewMsgVote creates a message to cast a vote on an active proposal @@ -188,10 +190,9 @@ func (msg MsgVote) Type() string { return TypeMsgVote } // ValidateBasic implements Msg func (msg MsgVote) ValidateBasic() error { - if msg.Voter == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Voter) + if _, err := sdk.AccAddressFromBech32(msg.Voter); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid voter address: %s", err) } - if !ValidVoteOption(msg.Option) { return sdkerrors.Wrap(ErrInvalidVote, msg.Option.String()) } @@ -212,8 +213,9 @@ func (msg MsgVote) GetSignBytes() []byte { } // GetSigners implements Msg -func (msg MsgVote) GetSigners() []string { - return []string{msg.Voter} +func (msg MsgVote) GetSigners() []sdk.AccAddress { + voter, _ := sdk.AccAddressFromBech32(msg.Voter) + return []sdk.AccAddress{voter} } // NewMsgVoteWeighted creates a message to cast a vote on an active proposal @@ -230,10 +232,9 @@ func (msg MsgVoteWeighted) Type() string { return TypeMsgVoteWeighted } // ValidateBasic implements Msg func (msg MsgVoteWeighted) ValidateBasic() error { - if msg.Voter == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Voter) + if _, err := sdk.AccAddressFromBech32(msg.Voter); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid voter address: %s", err) } - if len(msg.Options) == 0 { return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, WeightedVoteOptions(msg.Options).String()) } @@ -275,6 +276,7 @@ func (msg MsgVoteWeighted) GetSignBytes() []byte { } // GetSigners implements Msg -func (msg MsgVoteWeighted) GetSigners() []string { - return []string{msg.Voter} +func (msg MsgVoteWeighted) GetSigners() []sdk.AccAddress { + voter, _ := sdk.AccAddressFromBech32(msg.Voter) + return []sdk.AccAddress{voter} } diff --git a/x/slashing/types/msg.go b/x/slashing/types/msg.go index b24be89b97e5..91a15f72dbd4 100644 --- a/x/slashing/types/msg.go +++ b/x/slashing/types/msg.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) // slashing message types @@ -22,12 +23,9 @@ func NewMsgUnjail(validatorAddr sdk.ValAddress) *MsgUnjail { func (msg MsgUnjail) Route() string { return RouterKey } func (msg MsgUnjail) Type() string { return TypeMsgUnjail } -func (msg MsgUnjail) GetSigners() []string { - valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddr) - if err != nil { - panic(err) - } - return []string{sdk.AccAddress(valAddr).String()} +func (msg MsgUnjail) GetSigners() []sdk.AccAddress { + valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddr) + return []sdk.AccAddress{sdk.AccAddress(valAddr)} } // GetSignBytes gets the bytes for the message signer to sign on @@ -38,9 +36,8 @@ func (msg MsgUnjail) GetSignBytes() []byte { // ValidateBasic validity check for the AnteHandler func (msg MsgUnjail) ValidateBasic() error { - if msg.ValidatorAddr == "" { - return ErrBadValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddr); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("validator input address: %s", err) } - return nil } diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index 5dc76ae2e7c5..77eaf668befc 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -60,16 +60,14 @@ func (msg MsgCreateValidator) Type() string { return TypeMsgCreateValidator } // must sign over msg.GetSignBytes(). // If the validator address is not same as delegator's, then the validator must // sign the msg as well. -func (msg MsgCreateValidator) GetSigners() []string { +func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress { // delegator is first signer so delegator pays fees - addrs := []string{msg.DelegatorAddress} - valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress) - if err != nil { - panic(err) - } + delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) + addrs := []sdk.AccAddress{delegator} + valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress) - valAccAddr := sdk.AccAddress(valAddr).String() - if msg.DelegatorAddress != valAccAddr { + valAccAddr := sdk.AccAddress(valAddr) + if !delegator.Equals(valAccAddr) { addrs = append(addrs, valAccAddr) } @@ -84,22 +82,14 @@ func (msg MsgCreateValidator) GetSignBytes() []byte { // ValidateBasic implements the sdk.Msg interface. func (msg MsgCreateValidator) ValidateBasic() error { - // note that unmarshaling from bech32 ensures either empty or valid + // note that unmarshaling from bech32 ensures both non-empty and valid delAddr, err := sdk.AccAddressFromBech32(msg.DelegatorAddress) if err != nil { - return err + return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } - if delAddr.Empty() { - return ErrEmptyDelegatorAddr - } - - if msg.ValidatorAddress == "" { - return ErrEmptyValidatorAddr - } - valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress) if err != nil { - return err + return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } if !sdk.AccAddress(valAddr).Equals(delAddr) { return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "validator address is invalid") @@ -163,12 +153,9 @@ func (msg MsgEditValidator) Route() string { return RouterKey } func (msg MsgEditValidator) Type() string { return TypeMsgEditValidator } // GetSigners implements the sdk.Msg interface. -func (msg MsgEditValidator) GetSigners() []string { - valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress) - if err != nil { - panic(err) - } - return []string{sdk.AccAddress(valAddr).String()} +func (msg MsgEditValidator) GetSigners() []sdk.AccAddress { + valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress) + return []sdk.AccAddress{sdk.AccAddress(valAddr)} } // GetSignBytes implements the sdk.Msg interface. @@ -179,8 +166,8 @@ func (msg MsgEditValidator) GetSignBytes() []byte { // ValidateBasic implements the sdk.Msg interface. func (msg MsgEditValidator) ValidateBasic() error { - if msg.ValidatorAddress == "" { - return ErrEmptyValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } if msg.Description == (Description{}) { @@ -220,8 +207,9 @@ func (msg MsgDelegate) Route() string { return RouterKey } func (msg MsgDelegate) Type() string { return TypeMsgDelegate } // GetSigners implements the sdk.Msg interface. -func (msg MsgDelegate) GetSigners() []string { - return []string{msg.DelegatorAddress} +func (msg MsgDelegate) GetSigners() []sdk.AccAddress { + delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) + return []sdk.AccAddress{delegator} } // GetSignBytes implements the sdk.Msg interface. @@ -232,12 +220,11 @@ func (msg MsgDelegate) GetSignBytes() []byte { // ValidateBasic implements the sdk.Msg interface. func (msg MsgDelegate) ValidateBasic() error { - if msg.DelegatorAddress == "" { - return ErrEmptyDelegatorAddr + if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } - - if msg.ValidatorAddress == "" { - return ErrEmptyValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } if !msg.Amount.IsValid() || !msg.Amount.Amount.IsPositive() { @@ -270,8 +257,9 @@ func (msg MsgBeginRedelegate) Route() string { return RouterKey } func (msg MsgBeginRedelegate) Type() string { return TypeMsgBeginRedelegate } // GetSigners implements the sdk.Msg interface -func (msg MsgBeginRedelegate) GetSigners() []string { - return []string{msg.DelegatorAddress} +func (msg MsgBeginRedelegate) GetSigners() []sdk.AccAddress { + delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) + return []sdk.AccAddress{delegator} } // GetSignBytes implements the sdk.Msg interface. @@ -282,16 +270,14 @@ func (msg MsgBeginRedelegate) GetSignBytes() []byte { // ValidateBasic implements the sdk.Msg interface. func (msg MsgBeginRedelegate) ValidateBasic() error { - if msg.DelegatorAddress == "" { - return ErrEmptyDelegatorAddr + if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } - - if msg.ValidatorSrcAddress == "" { - return ErrEmptyValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorSrcAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid source validator address: %s", err) } - - if msg.ValidatorDstAddress == "" { - return ErrEmptyValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorDstAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid destination validator address: %s", err) } if !msg.Amount.IsValid() || !msg.Amount.Amount.IsPositive() { @@ -321,8 +307,9 @@ func (msg MsgUndelegate) Route() string { return RouterKey } func (msg MsgUndelegate) Type() string { return TypeMsgUndelegate } // GetSigners implements the sdk.Msg interface. -func (msg MsgUndelegate) GetSigners() []string { - return []string{msg.DelegatorAddress} +func (msg MsgUndelegate) GetSigners() []sdk.AccAddress { + delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress) + return []sdk.AccAddress{delegator} } // GetSignBytes implements the sdk.Msg interface. @@ -333,12 +320,11 @@ func (msg MsgUndelegate) GetSignBytes() []byte { // ValidateBasic implements the sdk.Msg interface. func (msg MsgUndelegate) ValidateBasic() error { - if msg.DelegatorAddress == "" { - return ErrEmptyDelegatorAddr + if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err) } - - if msg.ValidatorAddress == "" { - return ErrEmptyValidatorAddr + if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } if !msg.Amount.IsValid() || !msg.Amount.Amount.IsPositive() {