refactor(authz):remove global bech32 (#15662)

This commit is contained in:
Marko
2023-04-03 13:55:32 +02:00
committed by GitHub
parent b4d1109efc
commit 521aa4bfdd
20 changed files with 141 additions and 64 deletions
+16 -16
View File
@@ -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,
},
)
+4 -3
View File
@@ -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)
+11 -10
View File
@@ -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
}
+2 -1
View File
@@ -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 {