refactor(authz):remove global bech32 (#15662)
This commit is contained in:
parent
b4d1109efc
commit
521aa4bfdd
@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec/address"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/authz"
|
||||
@ -76,7 +77,7 @@ func (s *E2ETestSuite) TestQueryAuthorizations() {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryGrants()
|
||||
cmd := cli.GetCmdQueryGrants(address.NewBech32Codec("cosmos"))
|
||||
clientCtx := val.ClientCtx
|
||||
resp, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
@ -180,7 +181,7 @@ func (s *E2ETestSuite) TestQueryAuthorization() {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryGrants()
|
||||
cmd := cli.GetCmdQueryGrants(address.NewBech32Codec("cosmos"))
|
||||
clientCtx := val.ClientCtx
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
@ -249,7 +250,7 @@ func (s *E2ETestSuite) TestQueryGranterGrants() {
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetQueryGranterGrants()
|
||||
cmd := cli.GetQueryGranterGrants(address.NewBech32Codec("cosmos"))
|
||||
clientCtx := val.ClientCtx
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
|
||||
@ -8,6 +8,7 @@ import (
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
@ -665,7 +666,7 @@ func (s *E2ETestSuite) TestCmdRevokeAuthorizations() {
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.NewCmdRevokeAuthorization()
|
||||
cmd := cli.NewCmdRevokeAuthorization(address.NewBech32Codec("cosmos"))
|
||||
clientCtx := val.ClientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
|
||||
@ -21,6 +24,10 @@ func NewBech32Codec(prefix string) address.Codec {
|
||||
|
||||
// StringToBytes encodes text to bytes
|
||||
func (bc bech32Codec) StringToBytes(text string) ([]byte, error) {
|
||||
if len(strings.TrimSpace(text)) == 0 {
|
||||
return []byte{}, errors.New("empty address string is not allowed")
|
||||
}
|
||||
|
||||
hrp, bz, err := bech32.DecodeAndConvert(text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -20,7 +20,6 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
@ -207,7 +206,7 @@ func init() {
|
||||
// ProvideAddressCodec provides an address.Codec to the container for any
|
||||
// modules that want to do address string <> bytes conversion.
|
||||
func ProvideAddressCodec(config *modulev1.Module) address.Codec {
|
||||
return codecaddress.NewBech32Codec(config.Bech32Prefix)
|
||||
return keeper.NewBech32Codec(config.Bech32Prefix)
|
||||
}
|
||||
|
||||
type ModuleInputs struct {
|
||||
|
||||
@ -4,18 +4,18 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/version"
|
||||
"github.com/cosmos/cosmos-sdk/x/authz"
|
||||
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
)
|
||||
|
||||
// GetQueryCmd returns the cli query commands for this module
|
||||
func GetQueryCmd() *cobra.Command {
|
||||
func GetQueryCmd(ac address.Codec) *cobra.Command {
|
||||
authorizationQueryCmd := &cobra.Command{
|
||||
Use: authz.ModuleName,
|
||||
Short: "Querying commands for the authz module",
|
||||
@ -26,16 +26,16 @@ func GetQueryCmd() *cobra.Command {
|
||||
}
|
||||
|
||||
authorizationQueryCmd.AddCommand(
|
||||
GetCmdQueryGrants(),
|
||||
GetQueryGranterGrants(),
|
||||
GetQueryGranteeGrants(),
|
||||
GetCmdQueryGrants(ac),
|
||||
GetQueryGranterGrants(ac),
|
||||
GetQueryGranteeGrants(ac),
|
||||
)
|
||||
|
||||
return authorizationQueryCmd
|
||||
}
|
||||
|
||||
// GetCmdQueryGrants implements the query authorization command.
|
||||
func GetCmdQueryGrants() *cobra.Command {
|
||||
func GetCmdQueryGrants(ac address.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "grants [granter-addr] [grantee-addr] [msg-type-url]?",
|
||||
Args: cobra.RangeArgs(2, 3),
|
||||
@ -57,11 +57,11 @@ $ %s query %s grants cosmos1skjw.. cosmos1skjwj.. %s
|
||||
}
|
||||
queryClient := authz.NewQueryClient(clientCtx)
|
||||
|
||||
granter, err := sdk.AccAddressFromBech32(args[0])
|
||||
_, err = ac.StringToBytes(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
grantee, err := sdk.AccAddressFromBech32(args[1])
|
||||
_, err = ac.StringToBytes(args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -77,8 +77,8 @@ $ %s query %s grants cosmos1skjw.. cosmos1skjwj.. %s
|
||||
res, err := queryClient.Grants(
|
||||
cmd.Context(),
|
||||
&authz.QueryGrantsRequest{
|
||||
Granter: granter.String(),
|
||||
Grantee: grantee.String(),
|
||||
Granter: args[0],
|
||||
Grantee: args[1],
|
||||
MsgTypeUrl: msgAuthorized,
|
||||
Pagination: pageReq,
|
||||
},
|
||||
@ -96,7 +96,7 @@ $ %s query %s grants cosmos1skjw.. cosmos1skjwj.. %s
|
||||
}
|
||||
|
||||
// GetQueryGranterGrants returns cmd to query for all grants for a granter.
|
||||
func GetQueryGranterGrants() *cobra.Command {
|
||||
func GetQueryGranterGrants(ac address.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "grants-by-granter [granter-addr]",
|
||||
Args: cobra.ExactArgs(1),
|
||||
@ -114,7 +114,7 @@ $ %s q %s grants-by-granter cosmos1skj..
|
||||
return err
|
||||
}
|
||||
|
||||
granter, err := sdk.AccAddressFromBech32(args[0])
|
||||
_, err = ac.StringToBytes(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -128,7 +128,7 @@ $ %s q %s grants-by-granter cosmos1skj..
|
||||
res, err := queryClient.GranterGrants(
|
||||
cmd.Context(),
|
||||
&authz.QueryGranterGrantsRequest{
|
||||
Granter: granter.String(),
|
||||
Granter: args[0],
|
||||
Pagination: pageReq,
|
||||
},
|
||||
)
|
||||
@ -145,7 +145,7 @@ $ %s q %s grants-by-granter cosmos1skj..
|
||||
}
|
||||
|
||||
// GetQueryGranteeGrants returns cmd to query for all grants for a grantee.
|
||||
func GetQueryGranteeGrants() *cobra.Command {
|
||||
func GetQueryGranteeGrants(ac address.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "grants-by-grantee [grantee-addr]",
|
||||
Args: cobra.ExactArgs(1),
|
||||
@ -163,7 +163,7 @@ $ %s q %s grants-by-grantee cosmos1skj..
|
||||
return err
|
||||
}
|
||||
|
||||
grantee, err := sdk.AccAddressFromBech32(args[0])
|
||||
_, err = ac.StringToBytes(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -177,7 +177,7 @@ $ %s q %s grants-by-grantee cosmos1skj..
|
||||
res, err := queryClient.GranteeGrants(
|
||||
cmd.Context(),
|
||||
&authz.QueryGranteeGrantsRequest{
|
||||
Grantee: grantee.String(),
|
||||
Grantee: args[0],
|
||||
Pagination: pageReq,
|
||||
},
|
||||
)
|
||||
|
||||
@ -7,6 +7,7 @@ import (
|
||||
sdkmath "cosmossdk.io/math"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@ -77,7 +78,7 @@ func (s *CLITestSuite) TestQueryAuthorizations() {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryGrants()
|
||||
cmd := cli.GetCmdQueryGrants(addresscodec.NewBech32Codec("cosmos"))
|
||||
resp, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
s.Require().Error(err)
|
||||
@ -163,7 +164,7 @@ func (s *CLITestSuite) TestQueryAuthorization() {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryGrants()
|
||||
cmd := cli.GetCmdQueryGrants(addresscodec.NewBech32Codec("cosmos"))
|
||||
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
s.Require().Error(err)
|
||||
@ -226,7 +227,7 @@ func (s *CLITestSuite) TestQueryGranterGrants() {
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetQueryGranterGrants()
|
||||
cmd := cli.GetQueryGranterGrants(addresscodec.NewBech32Codec("cosmos"))
|
||||
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
require.Error(err)
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
@ -33,7 +34,7 @@ const (
|
||||
)
|
||||
|
||||
// GetTxCmd returns the transaction commands for this module
|
||||
func GetTxCmd() *cobra.Command {
|
||||
func GetTxCmd(ac address.Codec) *cobra.Command {
|
||||
AuthorizationTxCmd := &cobra.Command{
|
||||
Use: authz.ModuleName,
|
||||
Short: "Authorization transactions subcommands",
|
||||
@ -44,8 +45,8 @@ func GetTxCmd() *cobra.Command {
|
||||
}
|
||||
|
||||
AuthorizationTxCmd.AddCommand(
|
||||
NewCmdGrantAuthorization(),
|
||||
NewCmdRevokeAuthorization(),
|
||||
NewCmdGrantAuthorization(ac),
|
||||
NewCmdRevokeAuthorization(ac),
|
||||
NewCmdExecAuthorization(),
|
||||
)
|
||||
|
||||
@ -53,7 +54,7 @@ func GetTxCmd() *cobra.Command {
|
||||
}
|
||||
|
||||
// NewCmdGrantAuthorization returns a CLI command handler for creating a MsgGrant transaction.
|
||||
func NewCmdGrantAuthorization() *cobra.Command {
|
||||
func NewCmdGrantAuthorization(ac address.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "grant <grantee> <authorization_type=\"send\"|\"generic\"|\"delegate\"|\"unbond\"|\"redelegate\"> --from <granter>",
|
||||
Short: "Grant authorization to an address",
|
||||
@ -72,7 +73,7 @@ Examples:
|
||||
return err
|
||||
}
|
||||
|
||||
grantee, err := sdk.AccAddressFromBech32(args[0])
|
||||
grantee, err := ac.StringToBytes(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -99,7 +100,7 @@ Examples:
|
||||
return err
|
||||
}
|
||||
|
||||
allowed, err := bech32toAccAddresses(allowList)
|
||||
allowed, err := bech32toAccAddresses(allowList, ac)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -214,7 +215,7 @@ func getExpireTime(cmd *cobra.Command) (*time.Time, error) {
|
||||
}
|
||||
|
||||
// NewCmdRevokeAuthorization returns a CLI command handler for creating a MsgRevoke transaction.
|
||||
func NewCmdRevokeAuthorization() *cobra.Command {
|
||||
func NewCmdRevokeAuthorization(ac address.Codec) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "revoke [grantee] [msg-type-url] --from=[granter]",
|
||||
Short: "revoke authorization",
|
||||
@ -231,7 +232,7 @@ Example:
|
||||
return err
|
||||
}
|
||||
|
||||
grantee, err := sdk.AccAddressFromBech32(args[0])
|
||||
grantee, err := ac.StringToBytes(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -300,10 +301,10 @@ func bech32toValAddresses(validators []string) ([]sdk.ValAddress, error) {
|
||||
}
|
||||
|
||||
// bech32toAccAddresses returns []AccAddress from a list of Bech32 string addresses.
|
||||
func bech32toAccAddresses(accAddrs []string) ([]sdk.AccAddress, error) {
|
||||
func bech32toAccAddresses(accAddrs []string, ac address.Codec) ([]sdk.AccAddress, error) {
|
||||
addrs := make([]sdk.AccAddress, len(accAddrs))
|
||||
for i, addr := range accAddrs {
|
||||
accAddr, err := sdk.AccAddressFromBech32(addr)
|
||||
accAddr, err := ac.StringToBytes(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
@ -564,7 +565,7 @@ func (s *CLITestSuite) TestCmdRevokeAuthorizations() {
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.NewCmdRevokeAuthorization()
|
||||
cmd := cli.NewCmdRevokeAuthorization(addresscodec.NewBech32Codec("cosmos"))
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
||||
if tc.expectErr {
|
||||
|
||||
@ -2,12 +2,13 @@ package authz
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
"github.com/cosmos/cosmos-sdk/x/authz/client/cli"
|
||||
)
|
||||
|
||||
func CreateGrant(clientCtx client.Context, args []string) (testutil.BufferWriter, error) {
|
||||
cmd := cli.NewCmdGrantAuthorization()
|
||||
cmd := cli.NewCmdGrantAuthorization(addresscodec.NewBech32Codec("cosmos"))
|
||||
return clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
|
||||
}
|
||||
|
||||
@ -3,11 +3,14 @@ package authz
|
||||
import (
|
||||
context "context"
|
||||
|
||||
"cosmossdk.io/core/address"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// AccountKeeper defines the expected account keeper (noalias)
|
||||
type AccountKeeper interface {
|
||||
address.Codec
|
||||
|
||||
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
|
||||
NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
|
||||
SetAccount(ctx context.Context, acc sdk.AccountI)
|
||||
|
||||
@ -14,15 +14,21 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) {
|
||||
continue
|
||||
}
|
||||
|
||||
grantee := sdk.MustAccAddressFromBech32(entry.Grantee)
|
||||
granter := sdk.MustAccAddressFromBech32(entry.Granter)
|
||||
grantee, err := k.authKeeper.StringToBytes(entry.Grantee)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
granter, err := k.authKeeper.StringToBytes(entry.Granter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
a, ok := entry.Authorization.GetCachedValue().(authz.Authorization)
|
||||
if !ok {
|
||||
panic("expected authorization")
|
||||
}
|
||||
|
||||
err := k.SaveGrant(ctx, grantee, granter, a, entry.Expiration)
|
||||
err = k.SaveGrant(ctx, grantee, granter, a, entry.Expiration)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -23,6 +23,13 @@ import (
|
||||
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
)
|
||||
|
||||
var (
|
||||
granteePub = secp256k1.GenPrivKey().PubKey()
|
||||
granterPub = secp256k1.GenPrivKey().PubKey()
|
||||
granteeAddr = sdk.AccAddress(granteePub.Address())
|
||||
granterAddr = sdk.AccAddress(granterPub.Address())
|
||||
)
|
||||
|
||||
type GenesisTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
@ -43,6 +50,11 @@ func (suite *GenesisTestSuite) SetupTest() {
|
||||
ctrl := gomock.NewController(suite.T())
|
||||
suite.accountKeeper = authztestutil.NewMockAccountKeeper(ctrl)
|
||||
|
||||
suite.accountKeeper.EXPECT().StringToBytes(granteeAddr.String()).Return(granteeAddr, nil).AnyTimes()
|
||||
suite.accountKeeper.EXPECT().BytesToString(granterAddr).Return(granterAddr.String(), nil).AnyTimes()
|
||||
suite.accountKeeper.EXPECT().StringToBytes(granterAddr.String()).Return(granterAddr, nil).AnyTimes()
|
||||
suite.accountKeeper.EXPECT().BytesToString(granterAddr).Return(granterAddr.String(), nil).AnyTimes()
|
||||
|
||||
suite.baseApp = baseapp.NewBaseApp(
|
||||
"authz",
|
||||
log.NewNopLogger(),
|
||||
@ -59,13 +71,6 @@ func (suite *GenesisTestSuite) SetupTest() {
|
||||
suite.keeper = keeper.NewKeeper(key, suite.encCfg.Codec, msr, suite.accountKeeper)
|
||||
}
|
||||
|
||||
var (
|
||||
granteePub = secp256k1.GenPrivKey().PubKey()
|
||||
granterPub = secp256k1.GenPrivKey().PubKey()
|
||||
granteeAddr = sdk.AccAddress(granteePub.Address())
|
||||
granterAddr = sdk.AccAddress(granterPub.Address())
|
||||
)
|
||||
|
||||
func (suite *GenesisTestSuite) TestImportExportGenesis() {
|
||||
coins := sdk.NewCoins(sdk.NewCoin("foo", sdkmath.NewInt(1_000)))
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
@ -24,12 +25,12 @@ func (k Keeper) Grants(c context.Context, req *authz.QueryGrantsRequest) (*authz
|
||||
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
||||
}
|
||||
|
||||
granter, err := sdk.AccAddressFromBech32(req.Granter)
|
||||
granter, err := k.authKeeper.StringToBytes(req.Granter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
grantee, err := sdk.AccAddressFromBech32(req.Grantee)
|
||||
grantee, err := k.authKeeper.StringToBytes(req.Grantee)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -95,7 +96,7 @@ func (k Keeper) GranterGrants(c context.Context, req *authz.QueryGranterGrantsRe
|
||||
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
||||
}
|
||||
|
||||
granter, err := sdk.AccAddressFromBech32(req.Granter)
|
||||
granter, err := k.authKeeper.StringToBytes(req.Granter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -117,7 +118,7 @@ func (k Keeper) GranterGrants(c context.Context, req *authz.QueryGranterGrantsRe
|
||||
|
||||
grantee := firstAddressFromGrantStoreKey(key)
|
||||
return &authz.GrantAuthorization{
|
||||
Granter: granter.String(),
|
||||
Granter: req.Granter,
|
||||
Grantee: grantee.String(),
|
||||
Authorization: any,
|
||||
Expiration: auth.Expiration,
|
||||
@ -141,7 +142,7 @@ func (k Keeper) GranteeGrants(c context.Context, req *authz.QueryGranteeGrantsRe
|
||||
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
||||
}
|
||||
|
||||
grantee, err := sdk.AccAddressFromBech32(req.Grantee)
|
||||
grantee, err := k.authKeeper.StringToBytes(req.Grantee)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -156,7 +157,7 @@ func (k Keeper) GranteeGrants(c context.Context, req *authz.QueryGranteeGrantsRe
|
||||
}
|
||||
|
||||
granter, g, _ := parseGrantStoreKey(append(GrantKey, key...))
|
||||
if !g.Equals(grantee) {
|
||||
if !bytes.Equal(g, grantee) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@ -169,7 +170,7 @@ func (k Keeper) GranteeGrants(c context.Context, req *authz.QueryGranteeGrantsRe
|
||||
Authorization: authorizationAny,
|
||||
Expiration: auth.Expiration,
|
||||
Granter: granter.String(),
|
||||
Grantee: grantee.String(),
|
||||
Grantee: req.Grantee,
|
||||
}, nil
|
||||
}, func() *authz.Grant {
|
||||
return &authz.Grant{}
|
||||
|
||||
@ -19,6 +19,7 @@ func (suite *TestSuite) TestGRPCQueryAuthorization() {
|
||||
req *authz.QueryGrantsRequest
|
||||
expAuthorization authz.Authorization
|
||||
)
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func(require *require.Assertions)
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -65,6 +66,13 @@ func (s *TestSuite) SetupTest() {
|
||||
// gomock initializations
|
||||
ctrl := gomock.NewController(s.T())
|
||||
s.accountKeeper = authztestutil.NewMockAccountKeeper(ctrl)
|
||||
for _, addr := range s.addrs {
|
||||
s.accountKeeper.EXPECT().StringToBytes(addr.String()).Return(addr, nil).AnyTimes()
|
||||
s.accountKeeper.EXPECT().BytesToString(addr).Return(addr.String(), nil).AnyTimes()
|
||||
}
|
||||
s.accountKeeper.EXPECT().StringToBytes("").Return(nil, errors.New("empty address string is not allowed")).AnyTimes()
|
||||
s.accountKeeper.EXPECT().StringToBytes("invalid").Return(nil, errors.New("invalid bech32 string")).AnyTimes()
|
||||
|
||||
s.bankKeeper = authztestutil.NewMockBankKeeper(ctrl)
|
||||
banktypes.RegisterInterfaces(s.encCfg.InterfaceRegistry)
|
||||
banktypes.RegisterMsgServer(s.baseApp.MsgServiceRouter(), s.bankKeeper)
|
||||
|
||||
@ -2,6 +2,7 @@ package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
@ -13,7 +14,7 @@ var _ authz.MsgServer = Keeper{}
|
||||
// Grant implements the MsgServer.Grant method to create a new grant.
|
||||
func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGrantResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
|
||||
grantee, err := k.authKeeper.StringToBytes(msg.Grantee)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -25,7 +26,7 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
|
||||
k.authKeeper.SetAccount(ctx, granteeAcc)
|
||||
}
|
||||
|
||||
granter, err := sdk.AccAddressFromBech32(msg.Granter)
|
||||
granter, err := k.authKeeper.StringToBytes(msg.Granter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -51,11 +52,11 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
|
||||
// Revoke implements the MsgServer.Revoke method.
|
||||
func (k Keeper) Revoke(goCtx context.Context, msg *authz.MsgRevoke) (*authz.MsgRevokeResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
|
||||
grantee, err := k.authKeeper.StringToBytes(msg.Grantee)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
granter, err := sdk.AccAddressFromBech32(msg.Granter)
|
||||
granter, err := k.authKeeper.StringToBytes(msg.Granter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -71,7 +72,10 @@ func (k Keeper) Revoke(goCtx context.Context, msg *authz.MsgRevoke) (*authz.MsgR
|
||||
// Exec implements the MsgServer.Exec method.
|
||||
func (k Keeper) Exec(goCtx context.Context, msg *authz.MsgExec) (*authz.MsgExecResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
grantee, err := sdk.AccAddressFromBech32(msg.Grantee)
|
||||
if msg.Grantee == "" {
|
||||
return nil, errors.New("empty address string is not allowed")
|
||||
}
|
||||
grantee, err := k.authKeeper.StringToBytes(msg.Grantee)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -26,6 +26,8 @@ func (suite *TestSuite) TestGrant() {
|
||||
addrs := suite.createAccounts(2)
|
||||
curBlockTime := ctx.BlockTime()
|
||||
|
||||
suite.accountKeeper.EXPECT().StringToBytes(sdk.AccAddress("valid").String()).Return(sdk.AccAddress("valid"), nil).AnyTimes()
|
||||
|
||||
oneHour := curBlockTime.Add(time.Hour)
|
||||
oneYear := curBlockTime.AddDate(1, 0, 0)
|
||||
|
||||
|
||||
@ -59,6 +59,9 @@ func TestExpiredGrantsQueue(t *testing.T) {
|
||||
accountKeeper.EXPECT().GetAccount(gomock.Any(), grantee3).Return(authtypes.NewBaseAccountWithAddress(grantee3)).AnyTimes()
|
||||
accountKeeper.EXPECT().GetAccount(gomock.Any(), grantee4).Return(authtypes.NewBaseAccountWithAddress(grantee4)).AnyTimes()
|
||||
|
||||
accountKeeper.EXPECT().StringToBytes(granter.String()).Return(granter, nil).AnyTimes()
|
||||
accountKeeper.EXPECT().BytesToString(granter).Return(granter.String(), nil).AnyTimes()
|
||||
|
||||
authzKeeper := keeper.NewKeeper(key, encCfg.Codec, baseApp.MsgServiceRouter(), accountKeeper)
|
||||
|
||||
save := func(grantee sdk.AccAddress, exp *time.Time) {
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
modulev1 "cosmossdk.io/api/cosmos/authz/module/v1"
|
||||
"cosmossdk.io/core/address"
|
||||
"cosmossdk.io/core/appmodule"
|
||||
|
||||
"cosmossdk.io/depinject"
|
||||
@ -38,6 +39,7 @@ var (
|
||||
// AppModuleBasic defines the basic application module used by the authz module.
|
||||
type AppModuleBasic struct {
|
||||
cdc codec.Codec
|
||||
ac address.Codec
|
||||
}
|
||||
|
||||
// Name returns the authz module's name.
|
||||
@ -91,13 +93,13 @@ func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, m
|
||||
}
|
||||
|
||||
// GetQueryCmd returns the cli query commands for the authz module
|
||||
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
|
||||
return cli.GetQueryCmd()
|
||||
func (ab AppModuleBasic) GetQueryCmd() *cobra.Command {
|
||||
return cli.GetQueryCmd(ab.ac)
|
||||
}
|
||||
|
||||
// GetTxCmd returns the transaction commands for the authz module
|
||||
func (AppModuleBasic) GetTxCmd() *cobra.Command {
|
||||
return cli.GetTxCmd()
|
||||
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
|
||||
return cli.GetTxCmd(ab.ac)
|
||||
}
|
||||
|
||||
// AppModule implements the sdk.AppModule interface
|
||||
@ -112,7 +114,7 @@ type AppModule struct {
|
||||
// NewAppModule creates a new AppModule object
|
||||
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak authz.AccountKeeper, bk authz.BankKeeper, registry cdctypes.InterfaceRegistry) AppModule {
|
||||
return AppModule{
|
||||
AppModuleBasic: AppModuleBasic{cdc: cdc},
|
||||
AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak},
|
||||
keeper: keeper,
|
||||
accountKeeper: ak,
|
||||
bankKeeper: bk,
|
||||
|
||||
@ -35,6 +35,21 @@ func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// BytesToString mocks base method.
|
||||
func (m *MockAccountKeeper) BytesToString(bz []byte) (string, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "BytesToString", bz)
|
||||
ret0, _ := ret[0].(string)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// BytesToString indicates an expected call of BytesToString.
|
||||
func (mr *MockAccountKeeperMockRecorder) BytesToString(bz interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BytesToString", reflect.TypeOf((*MockAccountKeeper)(nil).BytesToString), bz)
|
||||
}
|
||||
|
||||
// GetAccount mocks base method.
|
||||
func (m *MockAccountKeeper) GetAccount(ctx context.Context, addr types.AccAddress) types.AccountI {
|
||||
m.ctrl.T.Helper()
|
||||
@ -75,6 +90,21 @@ func (mr *MockAccountKeeperMockRecorder) SetAccount(ctx, acc interface{}) *gomoc
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetAccount), ctx, acc)
|
||||
}
|
||||
|
||||
// StringToBytes mocks base method.
|
||||
func (m *MockAccountKeeper) StringToBytes(text string) ([]byte, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "StringToBytes", text)
|
||||
ret0, _ := ret[0].([]byte)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// StringToBytes indicates an expected call of StringToBytes.
|
||||
func (mr *MockAccountKeeperMockRecorder) StringToBytes(text interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StringToBytes", reflect.TypeOf((*MockAccountKeeper)(nil).StringToBytes), text)
|
||||
}
|
||||
|
||||
// MockBankKeeper is a mock of BankKeeper interface.
|
||||
type MockBankKeeper struct {
|
||||
ctrl *gomock.Controller
|
||||
|
||||
Loading…
Reference in New Issue
Block a user