-
Notifications
You must be signed in to change notification settings - Fork 428
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
Authz redesign: CLI implementation #1079
Changes from all commits
9ae2fd3
1358502
8f30357
6dec6ad
1cb6bd0
fa235fd
14cedff
46aba00
443dbf4
8809e4f
b1acbbd
2d92e12
3d69c2e
498c46f
3eb6e2d
f8b6403
bbe2086
28c1d94
84dd98c
5c45d27
40bdb6c
5aed79c
cc8d9bd
7a11526
04d3b12
4724e4b
170b305
04987bc
f8dd749
5b5ae89
00de894
b5e6ee6
0797367
4ae3098
0e2f8eb
a69f603
2faf404
6732176
91cb542
902ad19
0164d85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,15 @@ import ( | |
"fmt" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/version" | ||
"github.com/cosmos/cosmos-sdk/x/authz" | ||
"github.com/spf13/cobra" | ||
flag "github.com/spf13/pflag" | ||
|
||
|
@@ -32,9 +34,13 @@ const ( | |
flagInstantiateByAddress = "instantiate-only-address" | ||
flagInstantiateByAnyOfAddress = "instantiate-anyof-addresses" | ||
flagUnpinCode = "unpin-code" | ||
flagAllowedMsgs = "allow-msgs" | ||
flagRunOnce = "run-once" | ||
flagAllowedMsgKeys = "allow-msg-keys" | ||
flagAllowedRawMsgs = "allow-raw-msgs" | ||
flagExpiration = "expiration" | ||
flagMaxCalls = "max-calls" | ||
flagMaxFunds = "max-funds" | ||
flagAllowAllMsgs = "allow-all-messages" | ||
flagNoTokenTransfer = "no-token-transfer" //nolint:gosec | ||
) | ||
|
||
// GetTxCmd returns the transaction commands for this module | ||
|
@@ -384,9 +390,17 @@ func parseExecuteArgs(contractAddr string, execMsg string, sender sdk.AccAddress | |
|
||
func GrantAuthorizationCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "grant [grantee] [contract_addr_bech32] --allow-msgs [msg1,msg2,...]", | ||
Use: "grant [grantee] [message_type=\"execution\"|\"migration\"] [contract_addr_bech32] --allow-raw-msgs [msg1,msg2,...] --allow-msg-keys [key1,key2,...] --allow-all-messages", | ||
Short: "Grant authorization to an address", | ||
Args: cobra.ExactArgs(2), | ||
Long: fmt.Sprintf(`Grant authorization to an address. | ||
Examples: | ||
$ %s tx grant <grantee_addr> execution <contract_addr> --allow-all-messages --maxCalls 1 --no-token-transfer --expiration 1667979596 | ||
|
||
$ %s tx grant <grantee_addr> execution <contract_addr> --allow-all-messages --maxFunds 100000uwasm --expiration 1667979596 | ||
|
||
$ %s tx grant <grantee_addr> execution <contract_addr> --allow-all-messages --maxCalls 5 --maxFunds 100000uwasm --expiration 1667979596 | ||
`, version.AppName, version.AppName, version.AppName), | ||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
|
@@ -398,17 +412,27 @@ func GrantAuthorizationCmd() *cobra.Command { | |
return err | ||
} | ||
|
||
contract, err := sdk.AccAddressFromBech32(args[1]) | ||
contract, err := sdk.AccAddressFromBech32(args[2]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msgKeys, err := cmd.Flags().GetStringSlice(flagAllowedMsgKeys) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msgs, err := cmd.Flags().GetStringSlice(flagAllowedMsgs) | ||
rawMsgs, err := cmd.Flags().GetStringSlice(flagAllowedRawMsgs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
once, err := cmd.Flags().GetBool(flagRunOnce) | ||
maxFundsStr, err := cmd.Flags().GetString(flagMaxFunds) | ||
if err != nil { | ||
return fmt.Errorf("max funds: %s", err) | ||
} | ||
|
||
maxCalls, err := cmd.Flags().GetUint64(flagMaxCalls) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -420,18 +444,84 @@ func GrantAuthorizationCmd() *cobra.Command { | |
if exp == 0 { | ||
return errors.New("expiration must be set") | ||
} | ||
_ = clientCtx | ||
_ = grantee | ||
_ = msgs | ||
_ = once | ||
_ = contract | ||
|
||
return errors.New("not implemented") | ||
allowAllMsgs, err := cmd.Flags().GetBool(flagAllowAllMsgs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
noTokenTransfer, err := cmd.Flags().GetBool(flagNoTokenTransfer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var limit types.ContractAuthzLimitX | ||
switch { | ||
case maxFundsStr != "" && maxCalls != 0 && !noTokenTransfer: | ||
maxFunds, err := sdk.ParseCoinsNormalized(maxFundsStr) | ||
if err != nil { | ||
return fmt.Errorf("max funds: %s", err) | ||
} | ||
limit = types.NewCombinedLimit(maxCalls, maxFunds...) | ||
case maxFundsStr != "" && maxCalls == 0 && !noTokenTransfer: | ||
maxFunds, err := sdk.ParseCoinsNormalized(maxFundsStr) | ||
if err != nil { | ||
return fmt.Errorf("max funds: %s", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
limit = types.NewMaxFundsLimit(maxFunds...) | ||
case maxCalls != 0 && noTokenTransfer && maxFundsStr == "": | ||
limit = types.NewMaxCallsLimit(maxCalls) | ||
default: | ||
return errors.New("invalid limit setup") | ||
} | ||
|
||
var filter types.ContractAuthzFilterX | ||
switch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filters are mutual exclusive within one grant. Better fail fast if more than one is set. |
||
case allowAllMsgs && len(msgKeys) != 0 || allowAllMsgs && len(rawMsgs) != 0 || len(msgKeys) != 0 && len(rawMsgs) != 0: | ||
return errors.New("cannot set more than one filter within one grant") | ||
case allowAllMsgs: | ||
filter = types.NewAllowAllMessagesFilter() | ||
case len(msgKeys) != 0: | ||
filter = types.NewAcceptedMessageKeysFilter(msgKeys...) | ||
case len(rawMsgs) != 0: | ||
msgs := make([]types.RawContractMessage, len(rawMsgs)) | ||
for i, msg := range rawMsgs { | ||
msgs[i] = types.RawContractMessage(msg) | ||
} | ||
filter = types.NewAcceptedMessagesFilter(msgs...) | ||
default: | ||
return errors.New("invalid filter setup") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The third option is to set the concrete json message(s) (byte equal). |
||
|
||
grant, err := types.NewContractGrant(contract, limit, filter) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var authorization authz.Authorization | ||
switch args[1] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 this works |
||
case "execution": | ||
authorization = types.NewContractExecutionAuthorization(*grant) | ||
case "migration": | ||
authorization = types.NewContractMigrationAuthorization(*grant) | ||
default: | ||
return fmt.Errorf("%s authorization type not supported", args[1]) | ||
} | ||
|
||
grantMsg, err := authz.NewMsgGrant(clientCtx.GetFromAddress(), grantee, authorization, time.Unix(0, exp)) | ||
if err != nil { | ||
return err | ||
} | ||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), grantMsg) | ||
}, | ||
} | ||
flags.AddTxFlagsToCmd(cmd) | ||
cmd.Flags().StringSlice(flagAllowedMsgs, []string{}, "Allowed msgs") | ||
cmd.Flags().Bool(flagRunOnce, false, "Allow to execute only once") | ||
cmd.Flags().StringSlice(flagAllowedMsgKeys, []string{}, "Allowed msg keys") | ||
cmd.Flags().StringSlice(flagAllowedRawMsgs, []string{}, "Allowed raw msgs") | ||
cmd.Flags().Uint64(flagMaxCalls, 0, "Maximal number of calls to the contract") | ||
alpe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmd.Flags().String(flagMaxFunds, "", "Maximal amount of tokens transferable to the contract.") | ||
cmd.Flags().Int64(flagExpiration, 0, "The Unix timestamp.") | ||
cmd.Flags().Bool(flagAllowAllMsgs, false, "Allow all messages") | ||
cmd.Flags().Bool(flagNoTokenTransfer, false, "Don't allow token transfer") | ||
return cmd | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good check