Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add x/msg_authorization module #7370

Closed
wants to merge 89 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
5a34c06
WIP: Msg authorization module added
atheeshp Sep 23, 2020
d30572c
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_author…
atheeshp Sep 24, 2020
5087ad3
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth_p…
atheeshp Sep 25, 2020
3eac303
fixing errors
atheeshp Sep 25, 2020
beb7cc2
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth_p…
atheeshp Sep 25, 2020
39f1715
fixed errors
atheeshp Sep 25, 2020
0f71c06
fixed module.go
atheeshp Sep 29, 2020
ffd988e
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth_p…
atheeshp Sep 29, 2020
14870e4
Add msg_tests
aleem1314 Sep 30, 2020
c2d6db2
fixes compile issues
atheeshp Sep 30, 2020
307cc99
Merge branch 'msg_auth_proto' of https://github.com/cosmos/cosmos-sdk…
aleem1314 Sep 30, 2020
5362872
fix test
atheeshp Sep 30, 2020
61834b6
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth_p…
atheeshp Sep 30, 2020
7b80c0f
fix test
atheeshp Sep 30, 2020
ca9062e
Merge branch 'master' into msg_authorization
atheeshp Sep 30, 2020
f27b8f1
Merge branch 'msg_authorization' of github.com:cosmos/cosmos-sdk into…
atheeshp Sep 30, 2020
64224ea
Merge branch 'msg_auth_proto' of https://github.com/cosmos/cosmos-sdk…
aleem1314 Sep 30, 2020
5d9da55
Add msg types tests
aleem1314 Oct 1, 2020
e4b8dd5
Fix Getmsgs
aleem1314 Oct 3, 2020
4f2e40e
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into al…
aleem1314 Oct 3, 2020
3b487ad
fixed codec issue
atheeshp Oct 3, 2020
c0ec30e
Fix syntax issues
aleem1314 Oct 3, 2020
df76719
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth_p…
atheeshp Oct 3, 2020
8d480cd
Merge branch 'msg_auth_proto' of https://github.com/cosmos/cosmos-sdk…
aleem1314 Oct 3, 2020
3e985e7
Fix keeper
aleem1314 Oct 3, 2020
f6024d3
fixed proto issues
atheeshp Oct 3, 2020
6243445
Fix keeper tests
aleem1314 Oct 3, 2020
2490825
Merge branch 'msg_auth_proto' of https://github.com/cosmos/cosmos-sdk…
aleem1314 Oct 3, 2020
2f75c50
fixed router in keeper
atheeshp Oct 3, 2020
4cb8b77
Merge branch 'master' into msg_authorization
atheeshp Oct 5, 2020
9745393
Merge branch 'msg_authorization' of github.com:cosmos/cosmos-sdk into…
atheeshp Oct 5, 2020
24da343
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_author…
atheeshp Oct 5, 2020
fbbcbac
Merge branch 'msg_authorization' of github.com:cosmos/cosmos-sdk into…
atheeshp Oct 5, 2020
d280dad
Merge branch 'msg_auth_proto' of github.com:cosmos/cosmos-sdk into ms…
atheeshp Oct 5, 2020
63acf05
Fix query proto
aleem1314 Oct 5, 2020
f22d8be
Merge branch 'aleem/msg_authorization' into msg_authorization
aleem1314 Oct 5, 2020
6d4f640
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_author…
atheeshp Oct 5, 2020
cd4dba1
Merge branch 'msg_authorization' of github.com:cosmos/cosmos-sdk into…
atheeshp Oct 5, 2020
0f6266b
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 6, 2020
38566b6
Fix cli txs
aleem1314 Oct 6, 2020
b118e4c
Add grpc query client implementation
aleem1314 Oct 6, 2020
01ad577
Add grpc-keeper test
aleem1314 Oct 6, 2020
246f3f4
Add grpc query tests
aleem1314 Oct 7, 2020
f4a5734
Fix linting issues
aleem1314 Oct 7, 2020
d442400
Fix cli query
aleem1314 Oct 7, 2020
2bae46c
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 7, 2020
2b759a7
fix lint errors
aleem1314 Oct 7, 2020
6f6cf7e
Add Genesis state
aleem1314 Oct 8, 2020
35e107d
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 8, 2020
edf46c8
Fix query authorization
aleem1314 Oct 9, 2020
4bd54a6
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 9, 2020
9d1040d
Review changes
aleem1314 Oct 9, 2020
d8a2a18
Fix grant authorization handler
aleem1314 Oct 12, 2020
d100e49
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 12, 2020
9ec3f39
Add cli tests
aleem1314 Oct 12, 2020
ab3f14a
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 12, 2020
f1e434b
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 13, 2020
5c37e32
Add cli tests
aleem1314 Oct 14, 2020
8be9599
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 14, 2020
8b16c06
Fix genesis test
aleem1314 Oct 14, 2020
291ec19
Fix issues
aleem1314 Oct 14, 2020
d81ec6b
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 14, 2020
1d6aa18
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 15, 2020
cd72441
update module to use proto msg services
aleem1314 Oct 15, 2020
53462e6
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 15, 2020
39f876b
Add simultion tests
aleem1314 Oct 15, 2020
8656392
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 15, 2020
04f3c8a
Fix lint
aleem1314 Oct 16, 2020
cf097d4
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 16, 2020
5f95ca8
fix lint
aleem1314 Oct 16, 2020
721aab3
Merge branch 'msg_authorization' of github.com:cosmos/cosmos-sdk into…
atheeshp Oct 16, 2020
fc60444
Merge branch 'master' into msg_authorization
atheeshp Oct 16, 2020
f417681
WIP simulations
aleem1314 Oct 16, 2020
eb782c5
Merge branch 'msg_authorization' of https://github.com/cosmos/cosmos-…
aleem1314 Oct 16, 2020
d78a517
Merge branch 'msg_authorization' of github.com:cosmos/cosmos-sdk into…
atheeshp Oct 16, 2020
8d28432
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 19, 2020
1c166e0
WIP simulations
aleem1314 Oct 19, 2020
3d43482
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 19, 2020
1ea7693
add msg tests
aleem1314 Oct 19, 2020
fc9438a
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into ms…
aleem1314 Oct 20, 2020
39bb25d
Fix simulation
aleem1314 Oct 20, 2020
f728be7
Fix errors
aleem1314 Oct 20, 2020
be3c14d
Merge branch 'msg_authorization' of github.com:cosmos/cosmos-sdk into…
atheeshp Oct 20, 2020
5c5d126
fix genesis import export
atheeshp Oct 20, 2020
bbd3930
fix sim tests
atheeshp Oct 20, 2020
85dfb75
fix sim
atheeshp Oct 20, 2020
a4a81c7
fix test
atheeshp Oct 20, 2020
f5bfa38
Merge branch 'master' into msg_authorization
atheeshp Oct 20, 2020
b2b2106
Merge branch 'master' into msg_authorization
atheeshp Oct 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions x/msg_authorization/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// nolint
// autogenerated code using github.com/rigelrozanski/multitool
// aliases generated for the following subdirectories:
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types/
// ALIASGEN: github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/keeper/
package msg_authorization

import (
"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/keeper"
"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types"
)

const (
ModuleName = types.ModuleName
StoreKey = types.StoreKey
RouterKey = types.RouterKey
QuerierRoute = types.QuerierRoute
)

var (
// functions aliases
RegisterCodec = types.RegisterCodec
ErrInvalidGranter = types.ErrInvalidGranter
ErrInvalidGrantee = types.ErrInvalidGrantee
ErrInvalidExpirationTime = types.ErrInvalidExpirationTime
NewMsgGrantAuthorization = types.NewMsgGrantAuthorization
NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization
NewMsgExecAuthorized = types.NewMsgExecAuthorized
NewKeeper = keeper.NewKeeper

// variable aliases
ModuleCdc = types.ModuleCdc
)

type (
Authorization = types.Authorization
SendAuthorization = types.SendAuthorization
AuthorizationGrant = types.AuthorizationGrant
GenericAuthorization = types.GenericAuthorization
MsgGrantAuthorization = types.MsgGrantAuthorization
MsgRevokeAuthorization = types.MsgRevokeAuthorization
MsgExecAuthorized = types.MsgExecAuthorized
Keeper = keeper.Keeper
)
3 changes: 3 additions & 0 deletions x/msg_authorization/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cli

const FlagExpiration = "expiration"
74 changes: 74 additions & 0 deletions x/msg_authorization/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cli

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
authorizationQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the msg authorization module",
Long: "",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

authorizationQueryCmd.AddCommand(flags.GetCommands(
GetCmdQueryAuthorization(queryRoute, cdc),
)...)

return authorizationQueryCmd
}

// GetCmdQueryAuthorization implements the query authorizations command.
func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command {
//TODO update description
return &cobra.Command{
Use: "authorization",
Args: cobra.ExactArgs(3),
Short: "query authorization for a granter-grantee pair",
Long: "query authorization for a granter-grantee pair",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

granterAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

granteeAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}

msgAuthorized := args[2]

res, _, err := cliCtx.QueryStore(types.GetActorAuthorizationKey(granteeAddr, granterAddr, msgAuthorized), storeName)
if err != nil {
return err
}

if len(res) == 0 {
return fmt.Errorf("no authorization found for given address pair ")
}

var grant types.AuthorizationGrant
err = cdc.UnmarshalBinaryBare(res, &grant)
if err != nil {
return err
}

return cliCtx.PrintOutput(grant)
},
}
}
152 changes: 152 additions & 0 deletions x/msg_authorization/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package cli

import (
"bufio"
"io/ioutil"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
AuthorizationTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Authorization transactions subcommands",
Long: "Authorize and revoke access to execute transactions on behalf of your address",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

AuthorizationTxCmd.AddCommand(flags.PostCommands(
GetCmdGrantAuthorization(cdc),
GetCmdRevokeAuthorization(cdc),
GetCmdSendAs(cdc),
)...)

return AuthorizationTxCmd
}

func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grant [grantee_address] [authorization] --from [granter_address_or_key]",
Short: "Grant authorization to an address",
Long: "Grant authorization to an address to execute a transaction on your behalf",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

granter := cliCtx.FromAddress
grantee, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

bz, err := ioutil.ReadFile(args[1])
if err != nil {
return err
}

var authorization types.Authorization
err = cdc.UnmarshalJSON(bz, &authorization)
if err != nil {
return err
}
expirationString := viper.GetString(FlagExpiration)
expiration, err := time.Parse(time.RFC3339, expirationString)
if err != nil {
return err
}

msg := types.NewMsgGrantAuthorization(granter, grantee, authorization, expiration)
if err := msg.ValidateBasic(); err != nil {
return err
}

return authclient.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg})

},
}
cmd.Flags().String(FlagExpiration, "9999-12-31T23:59:59.52Z", "The time upto which the authorization is active for the user")

return cmd
}

func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "revoke [grantee_address] [msg_type] --from [granter]",
Short: "revoke authorization",
Long: "revoke authorization from an address for a transaction",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

granter := cliCtx.FromAddress
grantee, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

msgAuthorized := args[1]

msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized)
if err := msg.ValidateBasic(); err != nil {
return err
}

return authclient.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg})
},
}
return cmd
}

func GetCmdSendAs(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send-as [grantee] [msg_tx_json] --from [grantee]",
Short: "execute tx on behalf of granter account",
Long: "execute tx on behalf of granter account",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

grantee := cliCtx.FromAddress

var stdTx auth.StdTx
bz, err := ioutil.ReadFile(args[1])
if err != nil {
return err
}

err = cdc.UnmarshalJSON(bz, &stdTx)
if err != nil {
return err
}

msg := types.NewMsgExecAuthorized(grantee, stdTx.Msgs)

if err := msg.ValidateBasic(); err != nil {
return err
}

return authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
return cmd
}
11 changes: 11 additions & 0 deletions x/msg_authorization/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package rest

import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/gorilla/mux"
)

func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {

registerTxRoutes(cliCtx, r)
}
80 changes: 80 additions & 0 deletions x/msg_authorization/client/rest/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package rest

import (
"net/http"
"time"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types"
)

func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/msg_authorization/grant", grantHandler(cliCtx)).Methods("POST")
r.HandleFunc("/msg_authorization/revoke", revokeHandler(cliCtx)).Methods("POST")
}

type GrantRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Granter sdk.AccAddress `json:"granter"`
Grantee sdk.AccAddress `json:"grantee"`
Authorization types.Authorization `json:"authorization"`
Expiration time.Time `json:"expiration"`
}

type RevokeRequest struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Granter sdk.AccAddress `json:"granter"`
Grantee sdk.AccAddress `json:"grantee"`
AuthorizationMsgType string `json:"authorization_msg_type"`
}

func grantHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req GrantRequest

if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

msg := types.NewMsgGrantAuthorization(req.Granter, req.Grantee, req.Authorization, req.Expiration)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

authclient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}

func revokeHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req RevokeRequest

if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

msg := types.NewMsgRevokeAuthorization(req.Granter, req.Grantee, req.AuthorizationMsgType)
if err := msg.ValidateBasic(); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

authclient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}
25 changes: 25 additions & 0 deletions x/msg_authorization/exported/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package exported

import (
"time"

"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type Keeper interface {
//DispatchActions executes the provided messages via authorization grants from the message signer to the grantee
DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result

// Grants the provided authorization to the grantee on the granter's account with the provided expiration time
// If there is an existing authorization grant for the same sdk.Msg type, this grant overwrites that.
Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time)

//Revokes any authorization for the provided message type granted to the grantee by the granter.
Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg)

//Returns any Authorization (or nil), with the expiration time,
// granted to the grantee by the granter for the provided msg type.
GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Authorization, expiration time.Time)
}
Loading