From 429e77d7f30f6a7d3179d912b04eee9179fd4dc8 Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 001/162] Added code skeleton for msg authorization --- x/msg_authorization/client/cli/query.go | 2 ++ x/msg_authorization/client/cli/tx.go | 36 +++++++++++++++++++ x/msg_authorization/client/rest/query.go | 1 + x/msg_authorization/client/rest/tx.go | 1 + x/msg_authorization/exported/keeper.go | 23 ++++++++++++ x/msg_authorization/handler.go | 1 + x/msg_authorization/internal/keeper/keeper.go | 21 +++++++++++ .../internal/types/capabilities.go | 1 + x/msg_authorization/internal/types/keys.go | 15 ++++++++ x/msg_authorization/internal/types/msg.go | 34 ++++++++++++++++++ x/msg_authorization/module.go | 1 + 11 files changed, 136 insertions(+) create mode 100644 x/msg_authorization/client/cli/query.go create mode 100644 x/msg_authorization/client/cli/tx.go create mode 100644 x/msg_authorization/client/rest/query.go create mode 100644 x/msg_authorization/client/rest/tx.go create mode 100644 x/msg_authorization/exported/keeper.go create mode 100644 x/msg_authorization/handler.go create mode 100644 x/msg_authorization/internal/keeper/keeper.go create mode 100644 x/msg_authorization/internal/types/capabilities.go create mode 100644 x/msg_authorization/internal/types/keys.go create mode 100644 x/msg_authorization/internal/types/msg.go create mode 100644 x/msg_authorization/module.go diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go new file mode 100644 index 000000000000..c56a80569b50 --- /dev/null +++ b/x/msg_authorization/client/cli/query.go @@ -0,0 +1,2 @@ +package cli + diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go new file mode 100644 index 000000000000..b6e782d44f35 --- /dev/null +++ b/x/msg_authorization/client/cli/tx.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + AuthorizationTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Authorization transactions subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + AuthorizationTxCmd.AddCommand(client.PostCommands( + GetCmdGrantCapability(cdc), + GetCmdRevokeCapability(cdc), + )...) + + return AuthorizationTxCmd +} + +func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} + +func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} \ No newline at end of file diff --git a/x/msg_authorization/client/rest/query.go b/x/msg_authorization/client/rest/query.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/query.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/tx.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go new file mode 100644 index 000000000000..3ca0e9d31005 --- /dev/null +++ b/x/msg_authorization/exported/keeper.go @@ -0,0 +1,23 @@ +package exported + +import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" + +) + +type Keeper interface { + //DispatchActions executes the provided messages via capability grants from the message signer to the grantee + DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result + + // Grants the provided capability to the grantee on the granter's account with the provided expiration time + // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + + //Revokes any capability 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 Capability (or nil), with the expiration time, + // granted to the grantee by the granter for the provided msg type. + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) +} diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/handler.go @@ -0,0 +1 @@ +package msg_authorization diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go new file mode 100644 index 000000000000..8731cf48790c --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -0,0 +1,21 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + router sdk.Router +} + +// NewKeeper constructs a message authorisation Keeper +func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keeper { + return Keeper{ + storeKey: storeKey, + cdc: cdc, + router: router, + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go new file mode 100644 index 000000000000..ab1254f4c2be --- /dev/null +++ b/x/msg_authorization/internal/types/capabilities.go @@ -0,0 +1 @@ +package types diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go new file mode 100644 index 000000000000..8d327e683e07 --- /dev/null +++ b/x/msg_authorization/internal/types/keys.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the module name constant used in many places + ModuleName = "msg_authorization" + + // StoreKey is the store key string for msg_authorization + StoreKey = ModuleName + + // RouterKey is the message route for msg_authorization + RouterKey = ModuleName + + // QuerierRoute is the querier route for msg_authorization + QuerierRoute = ModuleName +) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/module.go @@ -0,0 +1 @@ +package msg_authorization From b2517cabb6378e4a963e053f064e6ae32d546a04 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 002/162] Implemented sdk.msg methods --- x/msg_authorization/alias.go | 25 ++++++ x/msg_authorization/client/cli/query.go | 1 - x/msg_authorization/client/cli/tx.go | 4 +- x/msg_authorization/exported/keeper.go | 8 +- x/msg_authorization/handler.go | 14 ++++ .../internal/types/capabilities.go | 4 + x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++- 7 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 x/msg_authorization/alias.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go new file mode 100644 index 000000000000..bf996e6bff6d --- /dev/null +++ b/x/msg_authorization/alias.go @@ -0,0 +1,25 @@ +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 ( + NewKeeper = keeper.NewKeeper +) + +type ( + Keeper = keeper.Keeper + Capability = types.Capability + MsgGrantAuthorization = types.MsgGrantAuthorization + MsgRevokeAuthorization = types.MsgRevokeAuthorization + MsgExecDelegated = types.MsgExecDelegated +) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index c56a80569b50..7f1e458cd3ab 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -1,2 +1 @@ package cli - diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index b6e782d44f35..47244a825235 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -3,8 +3,8 @@ package cli import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/spf13/cobra" ) // GetTxCmd returns the transaction commands for this module @@ -33,4 +33,4 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { //TODO return nil -} \ No newline at end of file +} diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 3ca0e9d31005..ead70b2428ac 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,9 @@ package exported import ( - "time" sdk "github.com/cosmos/cosmos-sdk/types" - + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper interface { @@ -12,12 +12,12 @@ type Keeper interface { // Grants the provided capability to the grantee on the granter's account with the provided expiration time // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. - Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) //Revokes any capability 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 Capability (or nil), with the expiration time, // granted to the grantee by the granter for the provided msg type. - GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) } diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index d552162c4c47..e8fb5a08a29d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1 +1,15 @@ package msg_authorization + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + + switch msg := msg.(type) { + //TODO + } + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index ab1254f4c2be..17d0d6992f96 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1 +1,5 @@ package types + +type Capability interface { + //TODO +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From cbe122ec98ccf45438e899b020395ab37ac0ffa2 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 003/162] Created handler for authorization --- x/msg_authorization/handler.go | 28 +++++++++++++++++-- x/msg_authorization/internal/keeper/keeper.go | 2 ++ x/msg_authorization/internal/types/codec.go | 9 ++++++ x/msg_authorization/internal/types/msg.go | 9 ++++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 x/msg_authorization/internal/types/codec.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index e8fb5a08a29d..9684b5c01187 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1,15 +1,39 @@ package msg_authorization import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) func NewHandler(k Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { ctx = ctx.WithEventManager(sdk.NewEventManager()) - switch msg := msg.(type) { - //TODO + case MsgGrantAuthorization: + return handleMsgGrantAuthorization(ctx, msg, k) + case MsgRevokeAuthorization: + return handleMsgRevokeAuthorization(ctx, msg, k) + case MsgExecDelegated: + return handleMsgExecDelegated(ctx, msg, k) + default: + errMsg := fmt.Sprintf("unrecognized authorization message type: %T", msg) + return sdk.ErrUnknownRequest(errMsg).Result() } } } + +func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 8731cf48790c..a7b70c5d1f03 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -19,3 +19,5 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe router: router, } } + +//TODO implement all keeper methods diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go new file mode 100644 index 000000000000..fe10042d1583 --- /dev/null +++ b/x/msg_authorization/internal/types/codec.go @@ -0,0 +1,9 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +var ModuleCdc = codec.New() + +//ToDO register concrete diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From 59db8cad3a0a866fa51029f4e939e4d008ece641 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 004/162] Added validate basic to all messages --- .../internal/types/capabilities.go | 8 +++- x/msg_authorization/internal/types/errors.go | 27 ++++++++++++ .../internal/types/{msg.go => msgs.go} | 43 ++++++++++++------- 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/internal/types/errors.go rename x/msg_authorization/internal/types/{msg.go => msgs.go} (78%) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 17d0d6992f96..3806f3a66684 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,5 +1,11 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + type Capability interface { - //TODO + MsgType() sdk.Msg + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } diff --git a/x/msg_authorization/internal/types/errors.go b/x/msg_authorization/internal/types/errors.go new file mode 100644 index 000000000000..7e685bba677c --- /dev/null +++ b/x/msg_authorization/internal/types/errors.go @@ -0,0 +1,27 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type CodeType = sdk.CodeType + +const ( + // Default slashing codespace + DefaultCodespace sdk.CodespaceType = ModuleName + + CodeInvalidGranter CodeType = 101 + CodeInvalidGrantee CodeType = 102 + CodeInvalidExpirationTime CodeType = 103 +) + +func ErrInvalidGranter(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGranter, "invalid granter address") +} + +func ErrInvalidGrantee(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGrantee, "invalid grantee address") +} +func ErrInvalidExpirationTime(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidExpirationTime, "expiration time of authorization should be more than current time") +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msgs.go similarity index 78% rename from x/msg_authorization/internal/types/msg.go rename to x/msg_authorization/internal/types/msgs.go index ce6d370b3a9b..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -28,17 +28,25 @@ func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return ErrInvalidGranter(DefaultCodespace) + } + if msg.Grantee.Empty() { + return ErrInvalidGrantee(DefaultCodespace) + } + if msg.Expiration.Unix() < time.Now().Unix() { + return ErrInvalidExpirationTime(DefaultCodespace) + } + return nil } @@ -64,17 +72,21 @@ func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return sdk.ErrInvalidAddress(msg.Granter.String()) + } + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } @@ -96,16 +108,17 @@ func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Grantee} } func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } From 1b2377073269c7655b0d249fc25a8870dbdb133e Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:23:17 +0530 Subject: [PATCH 005/162] Added base for internal keeper --- x/msg_authorization/internal/keeper/keeper.go | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index a7b70c5d1f03..750f747684d3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -3,6 +3,8 @@ package keeper import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper struct { @@ -20,4 +22,20 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } -//TODO implement all keeper methods +func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { + //TODO + return sdk.Result{} +} + +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { + //TODO +} + +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { + //TODO +} + +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { + //TODO + return nil, time.Now() +} From bc63ef714e57aaaa6c2e32be3cd4083df47460ce Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:33:25 +0530 Subject: [PATCH 006/162] Added message registration to codec --- x/msg_authorization/internal/keeper/keeper.go | 4 ++-- x/msg_authorization/internal/types/codec.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 750f747684d3..077b6c37c2f3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -31,11 +31,11 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd //TODO } -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { //TODO } -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { //TODO return nil, time.Now() } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index fe10042d1583..d9f8eb7ba7f6 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -6,4 +6,8 @@ import ( var ModuleCdc = codec.New() -//ToDO register concrete +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) + cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) + cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) +} From b04b90ddaccb9fef8ee1a8690d0df5a0ca94e866 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 22 Nov 2019 12:43:18 +0530 Subject: [PATCH 007/162] Added Send Capability --- .../internal/types/capabilities.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 3806f3a66684..5ce76eb3c43d 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" abci "github.com/tendermint/tendermint/abci/types" ) @@ -9,3 +10,29 @@ type Capability interface { MsgType() sdk.Msg Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From 5ddb995d21e53d166a7cdc6df154eea2be4927fe Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 2 Dec 2019 19:53:06 +0530 Subject: [PATCH 008/162] Added keeper methods for authorization --- x/msg_authorization/internal/keeper/keeper.go | 75 +++++++++++++++++-- .../internal/keeper/keeper_test.go | 1 + .../internal/types/capabilities.go | 16 ++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/keeper_test.go diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 077b6c37c2f3..811451ef002b 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -1,10 +1,13 @@ package keeper import ( + "bytes" + "fmt" + "time" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper struct { @@ -22,20 +25,80 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } +func (k Keeper) getActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +} + +func (k Keeper) getCapabilityGrant(ctx sdk.Context, actor []byte) (grant types.CapabilityGrant, found bool) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(actor) + if bz == nil { + return grant, false + } + k.cdc.MustUnmarshalBinaryBare(bz, &grant) + return grant, true +} + +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Capability) { + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, updated.MsgType())) + if !found { + return + } + grant.Capability = updated +} + func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { - //TODO + var res sdk.Result + for _, msg := range msgs { + signers := msg.GetSigners() + if len(signers) != 1 { + return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + } + granter := signers[0] + if !bytes.Equal(granter, grantee) { + capability, _ := k.GetCapability(ctx, grantee, granter, msg) + if capability == nil { + return sdk.ErrUnauthorized("unauthorized").Result() + } + allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) + if !allow { + return sdk.ErrUnauthorized("unauthorized").Result() + } + if del { + k.Revoke(ctx, grantee, granter, msg) + } else if updated != nil { + k.update(ctx, grantee, granter, updated) + } + } + res = k.router.Route(msg.Route())(ctx, msg) + if !res.IsOK() { + return res + } + } + return sdk.Result{} } func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { - //TODO + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinaryBare(types.CapabilityGrant{Capability: capability, Expiration: expiration}) + actor := k.getActorCapabilityKey(grantee, granter, capability.MsgType()) + store.Set(actor, bz) } func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { - //TODO + store := ctx.KVStore(k.storeKey) + store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) } func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { - //TODO - return nil, time.Now() + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) + if !found { + return nil, time.Time{} + } + if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { + k.Revoke(ctx, grantee, granter, msgType) + return nil, time.Time{} + } + return grant.Capability, grant.Expiration } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go new file mode 100644 index 000000000000..b55569d4a442 --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -0,0 +1 @@ +package keeper diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 5ce76eb3c43d..acbb26e1593e 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -3,6 +3,9 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + + "time" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -36,3 +39,16 @@ func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow b } return false, nil, false } + +type CapabilityGrant struct { + Capability Capability + + Expiration time.Time +} + +// GenericCapability grants the permission to execute any transaction of the provided +// sdk.Msg type without restrictions +type GenericCapability struct { + // MsgType is the type of Msg this capability grant allows + MsgType sdk.Msg +} From 2253d4fce3705ed4732603f919bdebfe087af109 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 009/162] Added keeper implementations --- x/msg_authorization/alias.go | 36 +++++++++++++--- x/msg_authorization/exported/keeper.go | 3 +- x/msg_authorization/internal/keeper/keeper.go | 6 +-- .../internal/keeper/keeper_test.go | 2 +- .../internal/keeper/test_common.go | 43 +++++++++++++++++++ .../internal/types/capabilities.go | 31 +------------ x/msg_authorization/internal/types/msgs.go | 3 +- .../internal/types/send_capability.go | 34 +++++++++++++++ 8 files changed, 117 insertions(+), 41 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/test_common.go create mode 100644 x/msg_authorization/internal/types/send_capability.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index bf996e6bff6d..701280c3c5f2 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -1,3 +1,8 @@ +// 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 ( @@ -6,20 +11,39 @@ import ( ) const ( - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - QuerierRoute = types.QuerierRoute + DefaultCodespace = types.DefaultCodespace + CodeInvalidGranter = types.CodeInvalidGranter + CodeInvalidGrantee = types.CodeInvalidGrantee + CodeInvalidExpirationTime = types.CodeInvalidExpirationTime + ModuleName = types.ModuleName + StoreKey = types.StoreKey + RouterKey = types.RouterKey + QuerierRoute = types.QuerierRoute ) var ( - NewKeeper = keeper.NewKeeper + // functions aliases + RegisterCodec = types.RegisterCodec + ErrInvalidGranter = types.ErrInvalidGranter + ErrInvalidGrantee = types.ErrInvalidGrantee + ErrInvalidExpirationTime = types.ErrInvalidExpirationTime + NewMsgGrantAuthorization = types.NewMsgGrantAuthorization + NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization + NewMsgExecDelegated = types.NewMsgExecDelegated + NewKeeper = keeper.NewKeeper + + // variable aliases + ModuleCdc = types.ModuleCdc ) type ( - Keeper = keeper.Keeper Capability = types.Capability + SendCapability = types.SendCapability + CapabilityGrant = types.CapabilityGrant + GenericCapability = types.GenericCapability + CodeType = types.CodeType MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization MsgExecDelegated = types.MsgExecDelegated + Keeper = keeper.Keeper ) diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index ead70b2428ac..69d581b5f182 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,10 @@ package exported import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper interface { diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 811451ef002b..acb95937b9b9 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -52,17 +52,17 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] for _, msg := range msgs { signers := msg.GetSigners() if len(signers) != 1 { - return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + return sdk.ErrUnknownRequest("authorization can be given to msg with only one signer").Result() } granter := signers[0] if !bytes.Equal(granter, grantee) { capability, _ := k.GetCapability(ctx, grantee, granter, msg) if capability == nil { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized("authorization not found").Result() } allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) if !allow { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized(" ").Result() } if del { k.Revoke(ctx, grantee, granter, msg) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index b55569d4a442..9429264902a9 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1 @@ -package keeper +package keeper_test diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go new file mode 100644 index 000000000000..97a0e177613c --- /dev/null +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -0,0 +1,43 @@ +package keeper + +import ( + "github.com/tendermint/tendermint/crypto/ed25519" + + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type testInput struct { + cdc *codec.Codec + ctx sdk.Context + ak auth.AccountKeeper + pk params.Keeper + bk bank.Keeper + dk Keeper + router sdk.Router +} + +func setupTestInput() testInput { + db := dbm.NewMemDB() + + cdc := codec.New() + auth.RegisterCodec(cdc) + bank.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + + //TODO create test input + return testInput{} +} + +var ( + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) +) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index acbb26e1593e..8e47d7c461e4 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,11 +1,10 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - "time" + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -14,32 +13,6 @@ type Capability interface { Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } -type SendCapability struct { - // SpendLimit specifies the maximum amount of tokens that can be spent - // by this capability and will be updated as tokens are spent. If it is - // empty, there is no spend limit and any amount of coins can be spent. - SpendLimit sdk.Coins -} - -func (capability SendCapability) MsgType() sdk.Msg { - return bank.MsgSend{} -} - -func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { - switch msg := msg.(type) { - case bank.MsgSend: - limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) - if isNegative { - return false, nil, false - } - if limitLeft.IsZero() { - return true, nil, true - } - return true, SendCapability{SpendLimit: limitLeft}, false - } - return false, nil, false -} - type CapabilityGrant struct { Capability Capability diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's diff --git a/x/msg_authorization/internal/types/send_capability.go b/x/msg_authorization/internal/types/send_capability.go new file mode 100644 index 000000000000..230643102e4b --- /dev/null +++ b/x/msg_authorization/internal/types/send_capability.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" + + abci "github.com/tendermint/tendermint/abci/types" +) + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From ec6ef9b3f13621b3dd4bc0a25b7bad36adac5ed5 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 5 Dec 2019 20:52:08 +0530 Subject: [PATCH 010/162] Added test input for keeper --- .../internal/keeper/test_common.go | 75 +++++++++++++++---- x/msg_authorization/test_common.go | 3 + 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/test_common.go diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 97a0e177613c..988e873a6c51 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,28 +1,40 @@ package keeper import ( + "testing" + "time" + + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/supply" ) -type testInput struct { - cdc *codec.Codec - ctx sdk.Context - ak auth.AccountKeeper - pk params.Keeper - bk bank.Keeper - dk Keeper - router sdk.Router -} +func makeTestCodec() *codec.Codec { + var cdc = codec.New() + auth.RegisterCodec(cdc) + types.RegisterCodec(cdc) + supply.RegisterCodec(cdc) + staking.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) -func setupTestInput() testInput { + return cdc +} +func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -31,13 +43,44 @@ func setupTestInput() testInput { sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - //TODO create test input - return testInput{} + keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + keyParams := sdk.NewKVStoreKey(params.StoreKey) + keyAuthorization := sdk.NewKVStoreKey(types.StoreKey) + tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) + + ms := store.NewCommitMultiStore(db) + ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) + + err := ms.LoadLatestVersion() + require.Nil(t, err) + + ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) + cdc = makeTestCodec() + + blacklistedAddrs := make(map[string]bool) + + paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace) + authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) + bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) + bankKeeper.SetSendEnabled(ctx, true) + + router := baseapp.NewRouter() + router.AddRoute("bank", bank.NewHandler(bankKeeper)) + + authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) + + authKeeper.SetParams(ctx, auth.DefaultParams()) + + return ctx, authKeeper, paramsKeeper, bankKeeper, authorizationKeeper, router } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + authorizedPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) + authorizedAddr = sdk.AccAddress(authorizedPub.Address()) ) diff --git a/x/msg_authorization/test_common.go b/x/msg_authorization/test_common.go new file mode 100644 index 000000000000..d0cb9b8dfcc2 --- /dev/null +++ b/x/msg_authorization/test_common.go @@ -0,0 +1,3 @@ +package msg_authorization + +//TODO create a mock app to demonstrate msg_authorization functionality From b50902a8381afaad8d1b910f19b84aa712ae8125 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 9 Dec 2019 20:06:35 +0530 Subject: [PATCH 011/162] Added tests for keeper --- x/msg_authorization/handler.go | 23 +++++++- .../internal/keeper/keeper_test.go | 55 ++++++++++++++++++- .../internal/keeper/test_common.go | 20 +++---- x/msg_authorization/internal/types/events.go | 13 +++++ 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 x/msg_authorization/internal/types/events.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 9684b5c01187..c1a496fe679d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) func NewHandler(k Keeper) sdk.Handler { @@ -24,12 +25,30 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { - //TODO + k.Grant(ctx, msg.Grantee, msg.Granter, msg.Capability, msg.Expiration) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { - //TODO + k.Revoke(ctx, msg.Grantee, msg.Granter, msg.CapabilityMsgType) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 9429264902a9..a846474bf037 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1,54 @@ -package keeper_test +package keeper + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type TestSuite struct { + suite.Suite + ctx sdk.Context + accountKeeper auth.AccountKeeper + paramsKeeper params.Keeper + bankKeeper bank.Keeper + keeper Keeper + router sdk.Router +} + +func (s *TestSuite) SetupTest() { + s.ctx, s.accountKeeper, s.paramsKeeper, s.bankKeeper, s.keeper, s.router = SetupTestInput() + +} + +func (s *TestSuite) TestKeeper(t *testing.T) { + err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + require.Nil(t, err) + require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + + t.Log("Verify that no capability returns nil") + capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + //require.Nil(t, expiration) + now := s.ctx.BlockHeader().Time + require.NotNil(t, now) + + newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) + capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + require.NotNil(t, capability) + require.NotNil(t, expiration) + //TODO add more cases +} + +func TestTestSuite(t *testing.T) { + suite.Run(t, new(TestSuite)) +} diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 988e873a6c51..f7874ff21cfe 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,10 +1,8 @@ package keeper import ( - "testing" "time" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" abci "github.com/tendermint/tendermint/abci/types" @@ -34,7 +32,7 @@ func makeTestCodec() *codec.Codec { return cdc } -func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { +func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -51,10 +49,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyAuthorization, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) - err := ms.LoadLatestVersion() - require.Nil(t, err) + ms.LoadLatestVersion() ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) cdc = makeTestCodec() @@ -77,10 +75,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - authorizedPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) - authorizedAddr = sdk.AccAddress(authorizedPub.Address()) + granteePub = ed25519.GenPrivKey().PubKey() + granterPub = ed25519.GenPrivKey().PubKey() + recepientPub = ed25519.GenPrivKey().PubKey() + granteeAddr = sdk.AccAddress(granteePub.Address()) + granterAddr = sdk.AccAddress(granterPub.Address()) + recepientAddr = sdk.AccAddress(recepientPub.Address()) ) diff --git a/x/msg_authorization/internal/types/events.go b/x/msg_authorization/internal/types/events.go new file mode 100644 index 000000000000..823ad3ce8cc5 --- /dev/null +++ b/x/msg_authorization/internal/types/events.go @@ -0,0 +1,13 @@ +package types + +// msg_authorization module events +const ( + EventGrantAuthorization = "grant-authorization" + EventRevokeAuthorization = "revoke-authorization" + EventExecuteAuthorization = "execute-authorization" + + AttributeKeyGranteeAddress = "grantee" + AttributeKeyGranterAddress = "granter" + + AttributeValueCategory = ModuleName +) From 6c43fd543755bf1e4c9d710f9c748c15f815fa34 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Mon, 9 Dec 2019 21:11:00 +0530 Subject: [PATCH 012/162] Added grant,revoke to cli --- x/msg_authorization/client/cli/tx.go | 73 ++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 47244a825235..9242e3f61dff 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -1,8 +1,13 @@ package cli import ( + "bufio" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" ) @@ -12,6 +17,7 @@ func GetTxCmd(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, @@ -26,11 +32,70 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { } func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "grant", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var capability types.Capability + err = cdc.UnmarshalJSON([]byte(args[1]), &capability) + if err != nil { + return err + } + + msg := types.NewMsgGrantAuthorization(granter, grantee, capability, expiration) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + + }, + } + return cmd } func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "revoke", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var msgAuthorized sdk.Msg + err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) + if err != nil { + return err + } + + msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + }, + } + return cmd } From 47b2974502ab5003d4cab8eb0e007cd6d47fa71b Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 10 Dec 2019 13:16:55 +0530 Subject: [PATCH 013/162] Added more test cases for keeper --- .../internal/keeper/keeper_test.go | 43 ++++++++++++++----- x/msg_authorization/internal/types/codec.go | 4 ++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index a846474bf037..76fafcdb3e52 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,24 +28,48 @@ func (s *TestSuite) SetupTest() { } -func (s *TestSuite) TestKeeper(t *testing.T) { +func (s *TestSuite) TestKeeper() { err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) - require.Nil(t, err) - require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + s.Require().Nil(err) + s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - t.Log("Verify that no capability returns nil") + s.T().Log("Verify that no capability returns nil") capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time - require.NotNil(t, now) + s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.T().Log("Verify if expired capability is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - require.NotNil(t, capability) - require.NotNil(t, expiration) - //TODO add more cases + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify if capability is accepted") + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + s.Require().Equal(capability.MsgType(), bank.MsgSend{}) + + s.T().Log("Verify fetching capability with wrong msg type fails") + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify fetching capability with wrong grantee fails") + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke fails with wrong information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke executes with correct information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + } func TestTestSuite(t *testing.T) { diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index d9f8eb7ba7f6..b6003860776a 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -10,4 +10,8 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) + cdc.RegisterConcrete(SendCapability{}, "cosmos-sdk/SendCapability", nil) + cdc.RegisterConcrete(CapabilityGrant{}, "cosmos-sdk/CapabilityGrant", nil) + + cdc.RegisterInterface((*Capability)(nil), nil) } From a92593f1e55219731f1ec37cdf9ef948e7ce08ff Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 10 Dec 2019 19:53:49 +0530 Subject: [PATCH 014/162] Added expiration flag to grant tx --- x/msg_authorization/client/cli/flags.go | 3 +++ x/msg_authorization/client/cli/tx.go | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 x/msg_authorization/client/cli/flags.go diff --git a/x/msg_authorization/client/cli/flags.go b/x/msg_authorization/client/cli/flags.go new file mode 100644 index 000000000000..a77534c3551f --- /dev/null +++ b/x/msg_authorization/client/cli/flags.go @@ -0,0 +1,3 @@ +package cli + +const FlagExpiration = "expiration" diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 9242e3f61dff..ff33d7fa57ae 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,8 @@ package cli import ( "bufio" + "time" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" @@ -10,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // GetTxCmd returns the transaction commands for this module @@ -53,6 +56,11 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { 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, capability, expiration) if err := msg.ValidateBasic(); err != nil { @@ -63,6 +71,8 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { }, } + cmd.Flags().String(FlagExpiration, "9999-12-31 23:59:59.52Z", "The time upto which the authorization is active for the user") + return cmd } From cb0152ff479665ecc2b3df573ee433bd753e9e0a Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 015/162] Added code skeleton for msg authorization --- x/msg_authorization/client/cli/query.go | 2 ++ x/msg_authorization/client/cli/tx.go | 36 +++++++++++++++++++ x/msg_authorization/client/rest/query.go | 1 + x/msg_authorization/client/rest/tx.go | 1 + x/msg_authorization/exported/keeper.go | 23 ++++++++++++ x/msg_authorization/handler.go | 1 + x/msg_authorization/internal/keeper/keeper.go | 21 +++++++++++ .../internal/types/capabilities.go | 1 + x/msg_authorization/internal/types/keys.go | 15 ++++++++ x/msg_authorization/internal/types/msg.go | 34 ++++++++++++++++++ x/msg_authorization/module.go | 1 + 11 files changed, 136 insertions(+) create mode 100644 x/msg_authorization/client/cli/query.go create mode 100644 x/msg_authorization/client/cli/tx.go create mode 100644 x/msg_authorization/client/rest/query.go create mode 100644 x/msg_authorization/client/rest/tx.go create mode 100644 x/msg_authorization/exported/keeper.go create mode 100644 x/msg_authorization/handler.go create mode 100644 x/msg_authorization/internal/keeper/keeper.go create mode 100644 x/msg_authorization/internal/types/capabilities.go create mode 100644 x/msg_authorization/internal/types/keys.go create mode 100644 x/msg_authorization/internal/types/msg.go create mode 100644 x/msg_authorization/module.go diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go new file mode 100644 index 000000000000..c56a80569b50 --- /dev/null +++ b/x/msg_authorization/client/cli/query.go @@ -0,0 +1,2 @@ +package cli + diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go new file mode 100644 index 000000000000..b6e782d44f35 --- /dev/null +++ b/x/msg_authorization/client/cli/tx.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + AuthorizationTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Authorization transactions subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + AuthorizationTxCmd.AddCommand(client.PostCommands( + GetCmdGrantCapability(cdc), + GetCmdRevokeCapability(cdc), + )...) + + return AuthorizationTxCmd +} + +func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} + +func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} \ No newline at end of file diff --git a/x/msg_authorization/client/rest/query.go b/x/msg_authorization/client/rest/query.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/query.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/tx.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go new file mode 100644 index 000000000000..3ca0e9d31005 --- /dev/null +++ b/x/msg_authorization/exported/keeper.go @@ -0,0 +1,23 @@ +package exported + +import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" + +) + +type Keeper interface { + //DispatchActions executes the provided messages via capability grants from the message signer to the grantee + DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result + + // Grants the provided capability to the grantee on the granter's account with the provided expiration time + // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + + //Revokes any capability 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 Capability (or nil), with the expiration time, + // granted to the grantee by the granter for the provided msg type. + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) +} diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/handler.go @@ -0,0 +1 @@ +package msg_authorization diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go new file mode 100644 index 000000000000..8731cf48790c --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -0,0 +1,21 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + router sdk.Router +} + +// NewKeeper constructs a message authorisation Keeper +func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keeper { + return Keeper{ + storeKey: storeKey, + cdc: cdc, + router: router, + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go new file mode 100644 index 000000000000..ab1254f4c2be --- /dev/null +++ b/x/msg_authorization/internal/types/capabilities.go @@ -0,0 +1 @@ +package types diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go new file mode 100644 index 000000000000..8d327e683e07 --- /dev/null +++ b/x/msg_authorization/internal/types/keys.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the module name constant used in many places + ModuleName = "msg_authorization" + + // StoreKey is the store key string for msg_authorization + StoreKey = ModuleName + + // RouterKey is the message route for msg_authorization + RouterKey = ModuleName + + // QuerierRoute is the querier route for msg_authorization + QuerierRoute = ModuleName +) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/module.go @@ -0,0 +1 @@ +package msg_authorization From b3ed7a80757a3fcc845859fd544818e467cb3b4a Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 016/162] Implemented sdk.msg methods --- x/msg_authorization/alias.go | 25 ++++++ x/msg_authorization/client/cli/query.go | 1 - x/msg_authorization/client/cli/tx.go | 4 +- x/msg_authorization/exported/keeper.go | 8 +- x/msg_authorization/handler.go | 14 ++++ .../internal/types/capabilities.go | 4 + x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++- 7 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 x/msg_authorization/alias.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go new file mode 100644 index 000000000000..bf996e6bff6d --- /dev/null +++ b/x/msg_authorization/alias.go @@ -0,0 +1,25 @@ +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 ( + NewKeeper = keeper.NewKeeper +) + +type ( + Keeper = keeper.Keeper + Capability = types.Capability + MsgGrantAuthorization = types.MsgGrantAuthorization + MsgRevokeAuthorization = types.MsgRevokeAuthorization + MsgExecDelegated = types.MsgExecDelegated +) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index c56a80569b50..7f1e458cd3ab 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -1,2 +1 @@ package cli - diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index b6e782d44f35..47244a825235 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -3,8 +3,8 @@ package cli import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/spf13/cobra" ) // GetTxCmd returns the transaction commands for this module @@ -33,4 +33,4 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { //TODO return nil -} \ No newline at end of file +} diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 3ca0e9d31005..ead70b2428ac 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,9 @@ package exported import ( - "time" sdk "github.com/cosmos/cosmos-sdk/types" - + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper interface { @@ -12,12 +12,12 @@ type Keeper interface { // Grants the provided capability to the grantee on the granter's account with the provided expiration time // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. - Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) //Revokes any capability 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 Capability (or nil), with the expiration time, // granted to the grantee by the granter for the provided msg type. - GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) } diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index d552162c4c47..e8fb5a08a29d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1 +1,15 @@ package msg_authorization + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + + switch msg := msg.(type) { + //TODO + } + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index ab1254f4c2be..17d0d6992f96 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1 +1,5 @@ package types + +type Capability interface { + //TODO +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From 0f4567bc19f2376a86a724237801c5f94cf574a2 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 017/162] Created handler for authorization --- x/msg_authorization/handler.go | 28 +++++++++++++++++-- x/msg_authorization/internal/keeper/keeper.go | 2 ++ x/msg_authorization/internal/types/codec.go | 9 ++++++ x/msg_authorization/internal/types/msg.go | 9 ++++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 x/msg_authorization/internal/types/codec.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index e8fb5a08a29d..9684b5c01187 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1,15 +1,39 @@ package msg_authorization import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) func NewHandler(k Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { ctx = ctx.WithEventManager(sdk.NewEventManager()) - switch msg := msg.(type) { - //TODO + case MsgGrantAuthorization: + return handleMsgGrantAuthorization(ctx, msg, k) + case MsgRevokeAuthorization: + return handleMsgRevokeAuthorization(ctx, msg, k) + case MsgExecDelegated: + return handleMsgExecDelegated(ctx, msg, k) + default: + errMsg := fmt.Sprintf("unrecognized authorization message type: %T", msg) + return sdk.ErrUnknownRequest(errMsg).Result() } } } + +func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 8731cf48790c..a7b70c5d1f03 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -19,3 +19,5 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe router: router, } } + +//TODO implement all keeper methods diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go new file mode 100644 index 000000000000..fe10042d1583 --- /dev/null +++ b/x/msg_authorization/internal/types/codec.go @@ -0,0 +1,9 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +var ModuleCdc = codec.New() + +//ToDO register concrete diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From 56f84ae960dd5aa05db7104b66aede569a7aa698 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 018/162] Added validate basic to all messages --- .../internal/types/capabilities.go | 8 +++- x/msg_authorization/internal/types/errors.go | 27 ++++++++++++ .../internal/types/{msg.go => msgs.go} | 43 ++++++++++++------- 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/internal/types/errors.go rename x/msg_authorization/internal/types/{msg.go => msgs.go} (78%) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 17d0d6992f96..3806f3a66684 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,5 +1,11 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + type Capability interface { - //TODO + MsgType() sdk.Msg + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } diff --git a/x/msg_authorization/internal/types/errors.go b/x/msg_authorization/internal/types/errors.go new file mode 100644 index 000000000000..7e685bba677c --- /dev/null +++ b/x/msg_authorization/internal/types/errors.go @@ -0,0 +1,27 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type CodeType = sdk.CodeType + +const ( + // Default slashing codespace + DefaultCodespace sdk.CodespaceType = ModuleName + + CodeInvalidGranter CodeType = 101 + CodeInvalidGrantee CodeType = 102 + CodeInvalidExpirationTime CodeType = 103 +) + +func ErrInvalidGranter(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGranter, "invalid granter address") +} + +func ErrInvalidGrantee(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGrantee, "invalid grantee address") +} +func ErrInvalidExpirationTime(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidExpirationTime, "expiration time of authorization should be more than current time") +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msgs.go similarity index 78% rename from x/msg_authorization/internal/types/msg.go rename to x/msg_authorization/internal/types/msgs.go index ce6d370b3a9b..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -28,17 +28,25 @@ func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return ErrInvalidGranter(DefaultCodespace) + } + if msg.Grantee.Empty() { + return ErrInvalidGrantee(DefaultCodespace) + } + if msg.Expiration.Unix() < time.Now().Unix() { + return ErrInvalidExpirationTime(DefaultCodespace) + } + return nil } @@ -64,17 +72,21 @@ func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return sdk.ErrInvalidAddress(msg.Granter.String()) + } + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } @@ -96,16 +108,17 @@ func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Grantee} } func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } From 1fa819b584a93a10015551b10a9d51fa8569bc06 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:23:17 +0530 Subject: [PATCH 019/162] Added base for internal keeper --- x/msg_authorization/internal/keeper/keeper.go | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index a7b70c5d1f03..750f747684d3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -3,6 +3,8 @@ package keeper import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper struct { @@ -20,4 +22,20 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } -//TODO implement all keeper methods +func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { + //TODO + return sdk.Result{} +} + +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { + //TODO +} + +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { + //TODO +} + +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { + //TODO + return nil, time.Now() +} From ee7aef94d04d3aaafc0079fcbbd916432604e0db Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:33:25 +0530 Subject: [PATCH 020/162] Added message registration to codec --- x/msg_authorization/internal/keeper/keeper.go | 4 ++-- x/msg_authorization/internal/types/codec.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 750f747684d3..077b6c37c2f3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -31,11 +31,11 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd //TODO } -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { //TODO } -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { //TODO return nil, time.Now() } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index fe10042d1583..d9f8eb7ba7f6 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -6,4 +6,8 @@ import ( var ModuleCdc = codec.New() -//ToDO register concrete +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) + cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) + cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) +} From 396afae1b3aabc4b3bfc762aee2c26ee4a886f69 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 22 Nov 2019 12:43:18 +0530 Subject: [PATCH 021/162] Added Send Capability --- .../internal/types/capabilities.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 3806f3a66684..5ce76eb3c43d 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" abci "github.com/tendermint/tendermint/abci/types" ) @@ -9,3 +10,29 @@ type Capability interface { MsgType() sdk.Msg Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From dabf01ced74845374464e365d4a04cfd6ec66680 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 2 Dec 2019 19:53:06 +0530 Subject: [PATCH 022/162] Added keeper methods for authorization --- x/msg_authorization/internal/keeper/keeper.go | 75 +++++++++++++++++-- .../internal/keeper/keeper_test.go | 1 + .../internal/types/capabilities.go | 16 ++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/keeper_test.go diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 077b6c37c2f3..811451ef002b 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -1,10 +1,13 @@ package keeper import ( + "bytes" + "fmt" + "time" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper struct { @@ -22,20 +25,80 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } +func (k Keeper) getActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +} + +func (k Keeper) getCapabilityGrant(ctx sdk.Context, actor []byte) (grant types.CapabilityGrant, found bool) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(actor) + if bz == nil { + return grant, false + } + k.cdc.MustUnmarshalBinaryBare(bz, &grant) + return grant, true +} + +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Capability) { + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, updated.MsgType())) + if !found { + return + } + grant.Capability = updated +} + func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { - //TODO + var res sdk.Result + for _, msg := range msgs { + signers := msg.GetSigners() + if len(signers) != 1 { + return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + } + granter := signers[0] + if !bytes.Equal(granter, grantee) { + capability, _ := k.GetCapability(ctx, grantee, granter, msg) + if capability == nil { + return sdk.ErrUnauthorized("unauthorized").Result() + } + allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) + if !allow { + return sdk.ErrUnauthorized("unauthorized").Result() + } + if del { + k.Revoke(ctx, grantee, granter, msg) + } else if updated != nil { + k.update(ctx, grantee, granter, updated) + } + } + res = k.router.Route(msg.Route())(ctx, msg) + if !res.IsOK() { + return res + } + } + return sdk.Result{} } func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { - //TODO + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinaryBare(types.CapabilityGrant{Capability: capability, Expiration: expiration}) + actor := k.getActorCapabilityKey(grantee, granter, capability.MsgType()) + store.Set(actor, bz) } func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { - //TODO + store := ctx.KVStore(k.storeKey) + store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) } func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { - //TODO - return nil, time.Now() + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) + if !found { + return nil, time.Time{} + } + if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { + k.Revoke(ctx, grantee, granter, msgType) + return nil, time.Time{} + } + return grant.Capability, grant.Expiration } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go new file mode 100644 index 000000000000..b55569d4a442 --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -0,0 +1 @@ +package keeper diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 5ce76eb3c43d..acbb26e1593e 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -3,6 +3,9 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + + "time" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -36,3 +39,16 @@ func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow b } return false, nil, false } + +type CapabilityGrant struct { + Capability Capability + + Expiration time.Time +} + +// GenericCapability grants the permission to execute any transaction of the provided +// sdk.Msg type without restrictions +type GenericCapability struct { + // MsgType is the type of Msg this capability grant allows + MsgType sdk.Msg +} From c7acac3327b2448a4d0ac66eebda55011374ad61 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 023/162] Added keeper implementations --- x/msg_authorization/alias.go | 36 +++++++++++++--- x/msg_authorization/exported/keeper.go | 3 +- x/msg_authorization/internal/keeper/keeper.go | 6 +-- .../internal/keeper/keeper_test.go | 2 +- .../internal/keeper/test_common.go | 43 +++++++++++++++++++ .../internal/types/capabilities.go | 31 +------------ x/msg_authorization/internal/types/msgs.go | 3 +- .../internal/types/send_capability.go | 34 +++++++++++++++ 8 files changed, 117 insertions(+), 41 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/test_common.go create mode 100644 x/msg_authorization/internal/types/send_capability.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index bf996e6bff6d..701280c3c5f2 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -1,3 +1,8 @@ +// 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 ( @@ -6,20 +11,39 @@ import ( ) const ( - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - QuerierRoute = types.QuerierRoute + DefaultCodespace = types.DefaultCodespace + CodeInvalidGranter = types.CodeInvalidGranter + CodeInvalidGrantee = types.CodeInvalidGrantee + CodeInvalidExpirationTime = types.CodeInvalidExpirationTime + ModuleName = types.ModuleName + StoreKey = types.StoreKey + RouterKey = types.RouterKey + QuerierRoute = types.QuerierRoute ) var ( - NewKeeper = keeper.NewKeeper + // functions aliases + RegisterCodec = types.RegisterCodec + ErrInvalidGranter = types.ErrInvalidGranter + ErrInvalidGrantee = types.ErrInvalidGrantee + ErrInvalidExpirationTime = types.ErrInvalidExpirationTime + NewMsgGrantAuthorization = types.NewMsgGrantAuthorization + NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization + NewMsgExecDelegated = types.NewMsgExecDelegated + NewKeeper = keeper.NewKeeper + + // variable aliases + ModuleCdc = types.ModuleCdc ) type ( - Keeper = keeper.Keeper Capability = types.Capability + SendCapability = types.SendCapability + CapabilityGrant = types.CapabilityGrant + GenericCapability = types.GenericCapability + CodeType = types.CodeType MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization MsgExecDelegated = types.MsgExecDelegated + Keeper = keeper.Keeper ) diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index ead70b2428ac..69d581b5f182 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,10 @@ package exported import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper interface { diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 811451ef002b..acb95937b9b9 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -52,17 +52,17 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] for _, msg := range msgs { signers := msg.GetSigners() if len(signers) != 1 { - return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + return sdk.ErrUnknownRequest("authorization can be given to msg with only one signer").Result() } granter := signers[0] if !bytes.Equal(granter, grantee) { capability, _ := k.GetCapability(ctx, grantee, granter, msg) if capability == nil { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized("authorization not found").Result() } allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) if !allow { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized(" ").Result() } if del { k.Revoke(ctx, grantee, granter, msg) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index b55569d4a442..9429264902a9 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1 @@ -package keeper +package keeper_test diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go new file mode 100644 index 000000000000..97a0e177613c --- /dev/null +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -0,0 +1,43 @@ +package keeper + +import ( + "github.com/tendermint/tendermint/crypto/ed25519" + + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type testInput struct { + cdc *codec.Codec + ctx sdk.Context + ak auth.AccountKeeper + pk params.Keeper + bk bank.Keeper + dk Keeper + router sdk.Router +} + +func setupTestInput() testInput { + db := dbm.NewMemDB() + + cdc := codec.New() + auth.RegisterCodec(cdc) + bank.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + + //TODO create test input + return testInput{} +} + +var ( + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) +) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index acbb26e1593e..8e47d7c461e4 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,11 +1,10 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - "time" + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -14,32 +13,6 @@ type Capability interface { Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } -type SendCapability struct { - // SpendLimit specifies the maximum amount of tokens that can be spent - // by this capability and will be updated as tokens are spent. If it is - // empty, there is no spend limit and any amount of coins can be spent. - SpendLimit sdk.Coins -} - -func (capability SendCapability) MsgType() sdk.Msg { - return bank.MsgSend{} -} - -func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { - switch msg := msg.(type) { - case bank.MsgSend: - limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) - if isNegative { - return false, nil, false - } - if limitLeft.IsZero() { - return true, nil, true - } - return true, SendCapability{SpendLimit: limitLeft}, false - } - return false, nil, false -} - type CapabilityGrant struct { Capability Capability diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's diff --git a/x/msg_authorization/internal/types/send_capability.go b/x/msg_authorization/internal/types/send_capability.go new file mode 100644 index 000000000000..230643102e4b --- /dev/null +++ b/x/msg_authorization/internal/types/send_capability.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" + + abci "github.com/tendermint/tendermint/abci/types" +) + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From 9fc6ce8adebb134b86598c3003bbea8582d049f6 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 5 Dec 2019 20:52:08 +0530 Subject: [PATCH 024/162] Added test input for keeper --- .../internal/keeper/test_common.go | 75 +++++++++++++++---- x/msg_authorization/test_common.go | 3 + 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/test_common.go diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 97a0e177613c..988e873a6c51 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,28 +1,40 @@ package keeper import ( + "testing" + "time" + + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/supply" ) -type testInput struct { - cdc *codec.Codec - ctx sdk.Context - ak auth.AccountKeeper - pk params.Keeper - bk bank.Keeper - dk Keeper - router sdk.Router -} +func makeTestCodec() *codec.Codec { + var cdc = codec.New() + auth.RegisterCodec(cdc) + types.RegisterCodec(cdc) + supply.RegisterCodec(cdc) + staking.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) -func setupTestInput() testInput { + return cdc +} +func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -31,13 +43,44 @@ func setupTestInput() testInput { sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - //TODO create test input - return testInput{} + keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + keyParams := sdk.NewKVStoreKey(params.StoreKey) + keyAuthorization := sdk.NewKVStoreKey(types.StoreKey) + tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) + + ms := store.NewCommitMultiStore(db) + ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) + + err := ms.LoadLatestVersion() + require.Nil(t, err) + + ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) + cdc = makeTestCodec() + + blacklistedAddrs := make(map[string]bool) + + paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace) + authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) + bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) + bankKeeper.SetSendEnabled(ctx, true) + + router := baseapp.NewRouter() + router.AddRoute("bank", bank.NewHandler(bankKeeper)) + + authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) + + authKeeper.SetParams(ctx, auth.DefaultParams()) + + return ctx, authKeeper, paramsKeeper, bankKeeper, authorizationKeeper, router } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + authorizedPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) + authorizedAddr = sdk.AccAddress(authorizedPub.Address()) ) diff --git a/x/msg_authorization/test_common.go b/x/msg_authorization/test_common.go new file mode 100644 index 000000000000..d0cb9b8dfcc2 --- /dev/null +++ b/x/msg_authorization/test_common.go @@ -0,0 +1,3 @@ +package msg_authorization + +//TODO create a mock app to demonstrate msg_authorization functionality From 7a0f87e3b0653828b2dc34fee5fbe615963caff0 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 9 Dec 2019 20:06:35 +0530 Subject: [PATCH 025/162] Added tests for keeper --- x/msg_authorization/handler.go | 23 +++++++- .../internal/keeper/keeper_test.go | 55 ++++++++++++++++++- .../internal/keeper/test_common.go | 20 +++---- x/msg_authorization/internal/types/events.go | 13 +++++ 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 x/msg_authorization/internal/types/events.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 9684b5c01187..c1a496fe679d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) func NewHandler(k Keeper) sdk.Handler { @@ -24,12 +25,30 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { - //TODO + k.Grant(ctx, msg.Grantee, msg.Granter, msg.Capability, msg.Expiration) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { - //TODO + k.Revoke(ctx, msg.Grantee, msg.Granter, msg.CapabilityMsgType) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 9429264902a9..a846474bf037 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1,54 @@ -package keeper_test +package keeper + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type TestSuite struct { + suite.Suite + ctx sdk.Context + accountKeeper auth.AccountKeeper + paramsKeeper params.Keeper + bankKeeper bank.Keeper + keeper Keeper + router sdk.Router +} + +func (s *TestSuite) SetupTest() { + s.ctx, s.accountKeeper, s.paramsKeeper, s.bankKeeper, s.keeper, s.router = SetupTestInput() + +} + +func (s *TestSuite) TestKeeper(t *testing.T) { + err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + require.Nil(t, err) + require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + + t.Log("Verify that no capability returns nil") + capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + //require.Nil(t, expiration) + now := s.ctx.BlockHeader().Time + require.NotNil(t, now) + + newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) + capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + require.NotNil(t, capability) + require.NotNil(t, expiration) + //TODO add more cases +} + +func TestTestSuite(t *testing.T) { + suite.Run(t, new(TestSuite)) +} diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 988e873a6c51..f7874ff21cfe 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,10 +1,8 @@ package keeper import ( - "testing" "time" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" abci "github.com/tendermint/tendermint/abci/types" @@ -34,7 +32,7 @@ func makeTestCodec() *codec.Codec { return cdc } -func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { +func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -51,10 +49,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyAuthorization, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) - err := ms.LoadLatestVersion() - require.Nil(t, err) + ms.LoadLatestVersion() ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) cdc = makeTestCodec() @@ -77,10 +75,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - authorizedPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) - authorizedAddr = sdk.AccAddress(authorizedPub.Address()) + granteePub = ed25519.GenPrivKey().PubKey() + granterPub = ed25519.GenPrivKey().PubKey() + recepientPub = ed25519.GenPrivKey().PubKey() + granteeAddr = sdk.AccAddress(granteePub.Address()) + granterAddr = sdk.AccAddress(granterPub.Address()) + recepientAddr = sdk.AccAddress(recepientPub.Address()) ) diff --git a/x/msg_authorization/internal/types/events.go b/x/msg_authorization/internal/types/events.go new file mode 100644 index 000000000000..823ad3ce8cc5 --- /dev/null +++ b/x/msg_authorization/internal/types/events.go @@ -0,0 +1,13 @@ +package types + +// msg_authorization module events +const ( + EventGrantAuthorization = "grant-authorization" + EventRevokeAuthorization = "revoke-authorization" + EventExecuteAuthorization = "execute-authorization" + + AttributeKeyGranteeAddress = "grantee" + AttributeKeyGranterAddress = "granter" + + AttributeValueCategory = ModuleName +) From d4b45793ac683f7ebe1be40d23cdcc672bb5cedd Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 10 Dec 2019 13:16:55 +0530 Subject: [PATCH 026/162] Added more test cases for keeper --- .../internal/keeper/keeper_test.go | 43 ++++++++++++++----- x/msg_authorization/internal/types/codec.go | 4 ++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index a846474bf037..76fafcdb3e52 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,24 +28,48 @@ func (s *TestSuite) SetupTest() { } -func (s *TestSuite) TestKeeper(t *testing.T) { +func (s *TestSuite) TestKeeper() { err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) - require.Nil(t, err) - require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + s.Require().Nil(err) + s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - t.Log("Verify that no capability returns nil") + s.T().Log("Verify that no capability returns nil") capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time - require.NotNil(t, now) + s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.T().Log("Verify if expired capability is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - require.NotNil(t, capability) - require.NotNil(t, expiration) - //TODO add more cases + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify if capability is accepted") + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + s.Require().Equal(capability.MsgType(), bank.MsgSend{}) + + s.T().Log("Verify fetching capability with wrong msg type fails") + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify fetching capability with wrong grantee fails") + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke fails with wrong information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke executes with correct information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + } func TestTestSuite(t *testing.T) { diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index d9f8eb7ba7f6..b6003860776a 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -10,4 +10,8 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) + cdc.RegisterConcrete(SendCapability{}, "cosmos-sdk/SendCapability", nil) + cdc.RegisterConcrete(CapabilityGrant{}, "cosmos-sdk/CapabilityGrant", nil) + + cdc.RegisterInterface((*Capability)(nil), nil) } From a543ff99d6531785c38a3ba281939935fc7faf4f Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Mon, 9 Dec 2019 21:11:00 +0530 Subject: [PATCH 027/162] Added grant,revoke to cli --- x/msg_authorization/client/cli/tx.go | 73 ++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 47244a825235..9242e3f61dff 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -1,8 +1,13 @@ package cli import ( + "bufio" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" ) @@ -12,6 +17,7 @@ func GetTxCmd(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, @@ -26,11 +32,70 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { } func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "grant", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var capability types.Capability + err = cdc.UnmarshalJSON([]byte(args[1]), &capability) + if err != nil { + return err + } + + msg := types.NewMsgGrantAuthorization(granter, grantee, capability, expiration) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + + }, + } + return cmd } func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "revoke", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var msgAuthorized sdk.Msg + err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) + if err != nil { + return err + } + + msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + }, + } + return cmd } From 1aec3d0c40d0de238a6dced399b6e2c8ecf86214 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 10 Dec 2019 19:53:49 +0530 Subject: [PATCH 028/162] Added expiration flag to grant tx --- x/msg_authorization/client/cli/flags.go | 3 +++ x/msg_authorization/client/cli/tx.go | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 x/msg_authorization/client/cli/flags.go diff --git a/x/msg_authorization/client/cli/flags.go b/x/msg_authorization/client/cli/flags.go new file mode 100644 index 000000000000..a77534c3551f --- /dev/null +++ b/x/msg_authorization/client/cli/flags.go @@ -0,0 +1,3 @@ +package cli + +const FlagExpiration = "expiration" diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 9242e3f61dff..ff33d7fa57ae 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,8 @@ package cli import ( "bufio" + "time" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" @@ -10,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // GetTxCmd returns the transaction commands for this module @@ -53,6 +56,11 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { 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, capability, expiration) if err := msg.ValidateBasic(); err != nil { @@ -63,6 +71,8 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { }, } + cmd.Flags().String(FlagExpiration, "9999-12-31 23:59:59.52Z", "The time upto which the authorization is active for the user") + return cmd } From 01f6483251eb5d8b1701b9d730b5c4e4e48ef101 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 19 Dec 2019 04:32:21 +0530 Subject: [PATCH 029/162] Added txs to rest --- x/msg_authorization/client/rest/query.go | 10 +++ x/msg_authorization/client/rest/tx.go | 77 +++++++++++++++++++ .../internal/types/capabilities.go | 7 -- x/msg_authorization/internal/types/codec.go | 1 + .../internal/types/generic_capability.go | 21 +++++ 5 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 x/msg_authorization/internal/types/generic_capability.go diff --git a/x/msg_authorization/client/rest/query.go b/x/msg_authorization/client/rest/query.go index 0062e0ca87d9..ffa566349c9d 100644 --- a/x/msg_authorization/client/rest/query.go +++ b/x/msg_authorization/client/rest/query.go @@ -1 +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) +} diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go index 0062e0ca87d9..eee62e84a0c2 100644 --- a/x/msg_authorization/client/rest/tx.go +++ b/x/msg_authorization/client/rest/tx.go @@ -1 +1,78 @@ package rest + +import ( + "github.com/cosmos/cosmos-sdk/client/context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/cosmos/cosmos-sdk/x/auth/client/utils" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/gorilla/mux" + "net/http" + "time" +) + +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"` + Capability types.Capability `json:"capability"` + 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"` + CapabilityMsgType sdk.Msg `json:"capability_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.Capability, req.Expiration) + if err := msg.ValidateBasic(); err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } + + utils.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.CapabilityMsgType) + if err := msg.ValidateBasic(); err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } + + utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 8e47d7c461e4..7029cb4228cc 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -18,10 +18,3 @@ type CapabilityGrant struct { Expiration time.Time } - -// GenericCapability grants the permission to execute any transaction of the provided -// sdk.Msg type without restrictions -type GenericCapability struct { - // MsgType is the type of Msg this capability grant allows - MsgType sdk.Msg -} diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index b6003860776a..cc2d381601b6 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -12,6 +12,7 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) cdc.RegisterConcrete(SendCapability{}, "cosmos-sdk/SendCapability", nil) cdc.RegisterConcrete(CapabilityGrant{}, "cosmos-sdk/CapabilityGrant", nil) + cdc.RegisterConcrete(GenericCapability{}, "cosmos-sdk/GenericCapability", nil) cdc.RegisterInterface((*Capability)(nil), nil) } diff --git a/x/msg_authorization/internal/types/generic_capability.go b/x/msg_authorization/internal/types/generic_capability.go new file mode 100644 index 000000000000..a269d726367b --- /dev/null +++ b/x/msg_authorization/internal/types/generic_capability.go @@ -0,0 +1,21 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// GenericCapability grants the permission to execute any transaction of the provided +// sdk.Msg type without restrictions +type GenericCapability struct { + // MsgType is the type of Msg this capability grant allows + Msg sdk.Msg +} + +func (cap GenericCapability) MsgType() sdk.Msg { + return cap.MsgType() +} + +func (cap GenericCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + return true, cap, false +} From 80669219c6c9ba375021746b42bb0f8f0456539d Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 19 Dec 2019 04:34:28 +0530 Subject: [PATCH 030/162] Added module.go --- x/msg_authorization/module.go | 128 ++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index d552162c4c47..092cf9c0538b 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -1 +1,129 @@ package msg_authorization + +import ( + "encoding/json" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/client/rest" + "github.com/cosmos/cosmos-sdk/x/upgrade/client/cli" + "github.com/gorilla/mux" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" +) + +// module codec +var moduleCdc = codec.New() + +func init() { + RegisterCodec(moduleCdc) +} + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +type AppModuleBasic struct{} + +// Name returns the ModuleName +func (AppModuleBasic) Name() string { + return ModuleName +} + +// RegisterCodec registers the msg_authorization types on the amino codec +func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { + RegisterCodec(cdc) +} + +// RegisterRESTRoutes registers all REST query handlers +func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, r *mux.Router) { + rest.RegisterRoutes(ctx, r) +} + +//GetQueryCmd returns the cli query commands for this module +func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { + queryCmd := &cobra.Command{ + Use: "msg_authorization", + Short: "Querying commands for the msg_authorization module", + } + queryCmd.AddCommand(client.GetCommands( + cli.GetPlanCmd(StoreKey, cdc), + cli.GetAppliedHeightCmd(StoreKey, cdc), + )...) + + return queryCmd +} + +// GetTxCmd returns the transaction commands for this module +func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { + txCmd := &cobra.Command{ + Use: "msg_authorization", + Short: "msg_authorization transaction subcommands", + } + txCmd.AddCommand(client.PostCommands()...) + return txCmd +} + +// AppModule implements the sdk.AppModule interface +type AppModule struct { + AppModuleBasic + keeper Keeper +} + +// NewAppModule creates a new AppModule object +func NewAppModule(keeper Keeper) AppModule { + return AppModule{ + AppModuleBasic: AppModuleBasic{}, + keeper: keeper, + } +} + +// RegisterInvariants does nothing, there are no invariants to enforce +func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// Route is empty, as we do not handle Messages (just proposals) +func (AppModule) Route() string { return RouterKey } + +// NewHandler is empty, as we do not handle Messages (just proposals) +func (am AppModule) NewHandler() sdk.Handler { return nil } + +// QuerierRoute returns the route we respond to for abci queries +func (AppModule) QuerierRoute() string { return QuerierRoute } + +/// NewQuerierHandler registers a query handler to respond to the module-specific queries +func (am AppModule) NewQuerierHandler() sdk.Querier { + //return NewQuerier(am.keeper) + return nil +} + +// InitGenesis is ignored, no sense in serializing future upgrades +func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} + +// DefaultGenesis is an empty object +func (AppModuleBasic) DefaultGenesis() json.RawMessage { + return []byte("{}") +} + +// ValidateGenesis is always successful, as we ignore the value +func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { + return nil +} + +// ExportGenesis is always empty, as InitGenesis does nothing either +func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { + return am.DefaultGenesis() +} + +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { + +} + +// EndBlock does nothing +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} From 5421223b310618278adb0da366633b6d8f3af28b Mon Sep 17 00:00:00 2001 From: anilCSE Date: Sun, 29 Dec 2019 16:22:49 +0530 Subject: [PATCH 031/162] Update review changes --- x/msg_authorization/client/cli/tx.go | 2 +- x/msg_authorization/internal/keeper/keeper.go | 34 ++++++++++++++++--- .../internal/keeper/keeper_test.go | 15 ++++---- x/msg_authorization/internal/types/msgs.go | 3 +- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index ff33d7fa57ae..21b9c66147b1 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -36,7 +36,7 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "grant", + Use: "grant [grantee_address] [capability] --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), diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index acb95937b9b9..950bbb9466dc 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -47,6 +47,8 @@ func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA grant.Capability = updated } +// DispatchActions attempts to execute the provided messages via capability +// grants from the message signer to the grantee. func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { var res sdk.Result for _, msg := range msgs { @@ -56,7 +58,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] } granter := signers[0] if !bytes.Equal(granter, grantee) { - capability, _ := k.GetCapability(ctx, grantee, granter, msg) + capability := k.GetCapability(ctx, grantee, granter, msg) if capability == nil { return sdk.ErrUnauthorized("authorization not found").Result() } @@ -79,6 +81,9 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] return sdk.Result{} } +// Grant method grants the provided capability to the grantee on the granter's account with the provided expiration +// time. If there is an existing capability grant for the same `sdk.Msg` type, this grant +// overwrites that. func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshalBinaryBare(types.CapabilityGrant{Capability: capability, Expiration: expiration}) @@ -86,19 +91,38 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd store.Set(actor, bz) } +// Revoke method revokes any capability for the provided message type granted to the grantee by the granter. func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { store := ctx.KVStore(k.storeKey) store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) } -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { +// GetCapability Returns any `Capability` (or `nil`) +// granted to the grantee by the granter for the provided msg type. +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability) { grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) if !found { - return nil, time.Time{} + return nil } if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { k.Revoke(ctx, grantee, granter, msgType) - return nil, time.Time{} + return nil } - return grant.Capability, grant.Expiration + return grant.Capability } + +// GetCapabilityExpiration Returns a Capability's the expiration time, +// granted to the grantee by the granter for the provided msg type. +func (k Keeper) GetCapabilityExpiration(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (expiration time.Time) { + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) + if !found { + return time.Time{} + } + + if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { + k.Revoke(ctx, grantee, granter, msgType) + return time.Time{} + } + + return grant.Expiration +} \ No newline at end of file diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 76fafcdb3e52..28afc1e401f2 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -34,7 +34,7 @@ func (s *TestSuite) TestKeeper() { s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) s.T().Log("Verify that no capability returns nil") - capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time @@ -43,33 +43,32 @@ func (s *TestSuite) TestKeeper() { newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) s.T().Log("Verify if expired capability is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) s.T().Log("Verify if capability is accepted") s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(capability) s.Require().Equal(capability.MsgType(), bank.MsgSend{}) s.T().Log("Verify fetching capability with wrong msg type fails") - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) s.Require().Nil(capability) s.T().Log("Verify fetching capability with wrong grantee fails") - capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + capability = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) s.Require().Nil(capability) s.T().Log("Verify revoke fails with wrong information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) s.T().Log("Verify revoke executes with correct information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(capability) - } func TestTestSuite(t *testing.T) { diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 87f2681b5fc7..dde8de4caf8c 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -6,7 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// MsgGrant grants the provided capability to the grantee on the granter's +// MsgGrantAuthorization grants the provided capability to the grantee on the granter's // account with the provided expiration time. type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` @@ -105,6 +105,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate Msgs: msgs, } } + func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } From 17ae7a608d40b372f4ef6dbc24e8ca62023928af Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Sun, 29 Dec 2019 16:46:35 +0530 Subject: [PATCH 032/162] Added query command for authorization --- x/msg_authorization/client/cli/query.go | 71 ++++++++++++++++++++++ x/msg_authorization/internal/types/keys.go | 9 +++ 2 files changed, 80 insertions(+) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index 7f1e458cd3ab..fc6c4a7ce004 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -1 +1,72 @@ package cli + +import ( + "fmt" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" + "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 distribution module", + Long: "", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + authorizationQueryCmd.AddCommand(client.GetCommands( + GetCmdQueryAuthorization(queryRoute, cdc), + )...) + + return authorizationQueryCmd +} + +// GetCmdQueryAuthorization implements the query authorizations command. +func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "", + Args: cobra.ExactArgs(3), + Short: "", + Long: "", + 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 + } + + var msgAuthorized sdk.Msg + err = cdc.UnmarshalJSON([]byte(args[2]), &msgAuthorized) + if err != nil { + return err + } + + res, _, err := cliCtx.QueryStore(types.GetActorCapabilityKey(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.CapabilityGrant + cdc.MustUnmarshalJSON(res, grant) + + return cliCtx.PrintOutput(grant) + }, + } +} diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go index 8d327e683e07..a3c1bf9a2aad 100644 --- a/x/msg_authorization/internal/types/keys.go +++ b/x/msg_authorization/internal/types/keys.go @@ -1,5 +1,10 @@ package types +import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" +) + const ( // ModuleName is the module name constant used in many places ModuleName = "msg_authorization" @@ -13,3 +18,7 @@ const ( // QuerierRoute is the querier route for msg_authorization QuerierRoute = ModuleName ) + +func GetActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +} From 78811fa946ef27672f24f235f6c6c71e997f59b6 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Sun, 29 Dec 2019 17:44:11 +0530 Subject: [PATCH 033/162] Revert GetCapability to match ADR --- x/msg_authorization/internal/keeper/keeper.go | 28 ++++--------------- .../internal/keeper/keeper_test.go | 15 +++++----- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 950bbb9466dc..372643be7a94 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -58,7 +58,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] } granter := signers[0] if !bytes.Equal(granter, grantee) { - capability := k.GetCapability(ctx, grantee, granter, msg) + capability, _ := k.GetCapability(ctx, grantee, granter, msg) if capability == nil { return sdk.ErrUnauthorized("authorization not found").Result() } @@ -97,32 +97,16 @@ func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) } -// GetCapability Returns any `Capability` (or `nil`) +// GetCapability Returns any `Capability` (or `nil`), with the expiration time, // granted to the grantee by the granter for the provided msg type. -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability) { +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) if !found { - return nil + return nil, time.Time{} } if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { k.Revoke(ctx, grantee, granter, msgType) - return nil + return nil, time.Time{} } - return grant.Capability + return grant.Capability, grant.Expiration } - -// GetCapabilityExpiration Returns a Capability's the expiration time, -// granted to the grantee by the granter for the provided msg type. -func (k Keeper) GetCapabilityExpiration(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (expiration time.Time) { - grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) - if !found { - return time.Time{} - } - - if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { - k.Revoke(ctx, grantee, granter, msgType) - return time.Time{} - } - - return grant.Expiration -} \ No newline at end of file diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 28afc1e401f2..76fafcdb3e52 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -34,7 +34,7 @@ func (s *TestSuite) TestKeeper() { s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) s.T().Log("Verify that no capability returns nil") - capability := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time @@ -43,32 +43,33 @@ func (s *TestSuite) TestKeeper() { newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) s.T().Log("Verify if expired capability is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) s.T().Log("Verify if capability is accepted") s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) - capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(capability) s.Require().Equal(capability.MsgType(), bank.MsgSend{}) s.T().Log("Verify fetching capability with wrong msg type fails") - capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) s.Require().Nil(capability) s.T().Log("Verify fetching capability with wrong grantee fails") - capability = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) s.Require().Nil(capability) s.T().Log("Verify revoke fails with wrong information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - capability = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) s.T().Log("Verify revoke executes with correct information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - capability = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(capability) + } func TestTestSuite(t *testing.T) { From c1ba63e68deca111418bed624bffdad272110321 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Sun, 29 Dec 2019 21:11:28 +0530 Subject: [PATCH 034/162] Refactor all the Authorization nomenclature --- x/msg_authorization/alias.go | 8 +-- x/msg_authorization/client/cli/query.go | 4 +- x/msg_authorization/client/cli/tx.go | 54 +++++++++++++++---- x/msg_authorization/client/rest/tx.go | 22 ++++---- x/msg_authorization/exported/keeper.go | 14 ++--- x/msg_authorization/handler.go | 4 +- x/msg_authorization/internal/keeper/keeper.go | 40 +++++++------- .../internal/keeper/keeper_test.go | 44 +++++++-------- .../internal/types/authorizations.go | 20 +++++++ .../internal/types/capabilities.go | 20 ------- x/msg_authorization/internal/types/codec.go | 8 +-- .../internal/types/generic_authorization.go | 21 ++++++++ .../internal/types/generic_capability.go | 21 -------- x/msg_authorization/internal/types/keys.go | 2 +- x/msg_authorization/internal/types/msgs.go | 38 ++++++------- ...nd_capability.go => send_authorization.go} | 12 ++--- x/msg_authorization/module.go | 4 +- 17 files changed, 186 insertions(+), 150 deletions(-) create mode 100644 x/msg_authorization/internal/types/authorizations.go delete mode 100644 x/msg_authorization/internal/types/capabilities.go create mode 100644 x/msg_authorization/internal/types/generic_authorization.go delete mode 100644 x/msg_authorization/internal/types/generic_capability.go rename x/msg_authorization/internal/types/{send_capability.go => send_authorization.go} (55%) diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index 701280c3c5f2..05fa33ba1f3d 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -37,10 +37,10 @@ var ( ) type ( - Capability = types.Capability - SendCapability = types.SendCapability - CapabilityGrant = types.CapabilityGrant - GenericCapability = types.GenericCapability + Authorization = types.Authorization + SendAuthorization = types.SendAuthorization + AuthorizationGrant = types.AuthorizationGrant + GenericAuthorization = types.GenericAuthorization CodeType = types.CodeType MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index fc6c4a7ce004..1acc77684b65 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -54,7 +54,7 @@ func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command return err } - res, _, err := cliCtx.QueryStore(types.GetActorCapabilityKey(granteeAddr, granterAddr, msgAuthorized), storeName) + res, _, err := cliCtx.QueryStore(types.GetActorAuthorizationKey(granteeAddr, granterAddr, msgAuthorized), storeName) if err != nil { return err } @@ -63,7 +63,7 @@ func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command return fmt.Errorf("no authorization found for given address pair ") } - var grant types.CapabilityGrant + var grant types.AuthorizationGrant cdc.MustUnmarshalJSON(res, grant) return cliCtx.PrintOutput(grant) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 21b9c66147b1..09293d32ce42 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -27,16 +27,17 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { } AuthorizationTxCmd.AddCommand(client.PostCommands( - GetCmdGrantCapability(cdc), - GetCmdRevokeCapability(cdc), + GetCmdGrantAuthorization(cdc), + GetCmdRevokeAuthorization(cdc), + //GetCmdSendAs(cdc), )...) return AuthorizationTxCmd } -func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { +func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "grant [grantee_address] [capability] --from [granter_address_or_key]", + 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), @@ -51,8 +52,8 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { return err } - var capability types.Capability - err = cdc.UnmarshalJSON([]byte(args[1]), &capability) + var authorization types.Authorization + err = cdc.UnmarshalJSON([]byte(args[1]), &authorization) if err != nil { return err } @@ -62,7 +63,7 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { return err } - msg := types.NewMsgGrantAuthorization(granter, grantee, capability, expiration) + msg := types.NewMsgGrantAuthorization(granter, grantee, authorization, expiration) if err := msg.ValidateBasic(); err != nil { return err } @@ -76,9 +77,9 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { return cmd } -func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { +func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "revoke", + Use: "revoke [grantee_address] [msg_type] --from [granter]", Short: "revoke authorization", Long: "revoke authorization from an address for a transaction", Args: cobra.ExactArgs(2), @@ -109,3 +110,38 @@ func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { } return cmd } + +// +//func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { +// cmd := &cobra.Command{ +// Use: "send-as [grantee] [generated_tx] --from [granter]", +// Short: "send-as authorization", +// Long: "send-as authorization", +// Args: cobra.ExactArgs(2), +// RunE: func(cmd *cobra.Command, args []string) error { +// inBuf := bufio.NewReader(cmd.InOrStdin()) +// txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) +// cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) +// +// granter := cliCtx.FromAddress +// grantee, err := sdk.AccAddressFromBech32(args[0]) +// if err != nil { +// return err +// } +// +// var msgAuthorized sdk.Msg +// err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) +// if err != nil { +// return err +// } +// +// msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) +// if err := msg.ValidateBasic(); err != nil { +// return err +// } +// +// return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) +// }, +// } +// return cmd +//} diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go index eee62e84a0c2..f2e08a6e72cf 100644 --- a/x/msg_authorization/client/rest/tx.go +++ b/x/msg_authorization/client/rest/tx.go @@ -17,18 +17,18 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { } type GrantRequest struct { - BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Capability types.Capability `json:"capability"` - Expiration time.Time `json:"expiration"` + 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"` - CapabilityMsgType sdk.Msg `json:"capability_msg_type"` + BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + AuthorizationMsgType sdk.Msg `json:"authorization_msg_type"` } func grantHandler(cliCtx context.CLIContext) http.HandlerFunc { @@ -44,7 +44,7 @@ func grantHandler(cliCtx context.CLIContext) http.HandlerFunc { return } - msg := types.NewMsgGrantAuthorization(req.Granter, req.Grantee, req.Capability, req.Expiration) + 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 @@ -67,7 +67,7 @@ func revokeHandler(cliCtx context.CLIContext) http.HandlerFunc { return } - msg := types.NewMsgRevokeAuthorization(req.Granter, req.Grantee, req.CapabilityMsgType) + msg := types.NewMsgRevokeAuthorization(req.Granter, req.Grantee, req.AuthorizationMsgType) if err := msg.ValidateBasic(); err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 69d581b5f182..b180c87424e0 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -8,17 +8,17 @@ import ( ) type Keeper interface { - //DispatchActions executes the provided messages via capability grants from the message signer to the grantee + //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 capability to the grantee on the granter's account with the provided expiration time - // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. - Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) + // 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 capability for the provided message type granted to the grantee by the granter. + //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 Capability (or nil), with the expiration time, + //Returns any Authorization (or nil), with the expiration time, // granted to the grantee by the granter for the provided msg type. - GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) + GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Authorization, expiration time.Time) } diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index c1a496fe679d..3024d9323623 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -25,7 +25,7 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { - k.Grant(ctx, msg.Grantee, msg.Granter, msg.Capability, msg.Expiration) + k.Grant(ctx, msg.Grantee, msg.Granter, msg.Authorization, msg.Expiration) ctx.EventManager().EmitEvent( sdk.NewEvent( @@ -39,7 +39,7 @@ func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k K } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { - k.Revoke(ctx, msg.Grantee, msg.Granter, msg.CapabilityMsgType) + k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) ctx.EventManager().EmitEvent( sdk.NewEvent( diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 372643be7a94..5f7939cce25a 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -25,11 +25,11 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } -func (k Keeper) getActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { +func (k Keeper) getActorAuthorizationKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) } -func (k Keeper) getCapabilityGrant(ctx sdk.Context, actor []byte) (grant types.CapabilityGrant, found bool) { +func (k Keeper) getAuthorizationGrant(ctx sdk.Context, actor []byte) (grant types.AuthorizationGrant, found bool) { store := ctx.KVStore(k.storeKey) bz := store.Get(actor) if bz == nil { @@ -39,15 +39,15 @@ func (k Keeper) getCapabilityGrant(ctx sdk.Context, actor []byte) (grant types.C return grant, true } -func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Capability) { - grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, updated.MsgType())) +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Authorization) { + grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, updated.MsgType())) if !found { return } - grant.Capability = updated + grant.Authorization = updated } -// DispatchActions attempts to execute the provided messages via capability +// DispatchActions attempts to execute the provided messages via authorization // grants from the message signer to the grantee. func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { var res sdk.Result @@ -58,11 +58,11 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] } granter := signers[0] if !bytes.Equal(granter, grantee) { - capability, _ := k.GetCapability(ctx, grantee, granter, msg) - if capability == nil { + authorization, _ := k.GetAuthorization(ctx, grantee, granter, msg) + if authorization == nil { return sdk.ErrUnauthorized("authorization not found").Result() } - allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) + allow, updated, del := authorization.Accept(msg, ctx.BlockHeader()) if !allow { return sdk.ErrUnauthorized(" ").Result() } @@ -81,26 +81,26 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] return sdk.Result{} } -// Grant method grants the provided capability to the grantee on the granter's account with the provided expiration -// time. If there is an existing capability grant for the same `sdk.Msg` type, this grant +// Grant method 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. -func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinaryBare(types.CapabilityGrant{Capability: capability, Expiration: expiration}) - actor := k.getActorCapabilityKey(grantee, granter, capability.MsgType()) + bz := k.cdc.MustMarshalBinaryBare(types.AuthorizationGrant{Authorization: authorization, Expiration: expiration}) + actor := k.getActorAuthorizationKey(grantee, granter, authorization.MsgType()) store.Set(actor, bz) } -// Revoke method revokes any capability for the provided message type granted to the grantee by the granter. +// Revoke method revokes any authorization for the provided message type granted to the grantee by the granter. func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { store := ctx.KVStore(k.storeKey) - store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) + store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType)) } -// GetCapability Returns any `Capability` (or `nil`), with the expiration time, +// GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time, // granted to the grantee by the granter for the provided msg type. -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { - grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) +func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Authorization, expiration time.Time) { + grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, msgType)) if !found { return nil, time.Time{} } @@ -108,5 +108,5 @@ func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter s k.Revoke(ctx, grantee, granter, msgType) return nil, time.Time{} } - return grant.Capability, grant.Expiration + return grant.Authorization, grant.Expiration } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 76fafcdb3e52..8730ac4147fc 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -33,42 +33,42 @@ func (s *TestSuite) TestKeeper() { s.Require().Nil(err) s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - s.T().Log("Verify that no capability returns nil") - capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - s.Require().Nil(capability) + s.T().Log("Verify that no authorization returns nil") + authorization, _ := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(authorization) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) - s.T().Log("Verify if expired capability is rejected") - s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - s.Require().Nil(capability) + s.T().Log("Verify if expired authorization is rejected") + s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendAuthorization{SpendLimit: newCoins}, now.Add(-1*time.Hour)) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(authorization) - s.T().Log("Verify if capability is accepted") - s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - s.Require().NotNil(capability) - s.Require().Equal(capability.MsgType(), bank.MsgSend{}) + s.T().Log("Verify if authorization is accepted") + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendAuthorization{SpendLimit: newCoins}, now.Add(time.Hour)) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(authorization) + s.Require().Equal(authorization.MsgType(), bank.MsgSend{}) - s.T().Log("Verify fetching capability with wrong msg type fails") - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) - s.Require().Nil(capability) + s.T().Log("Verify fetching authorization with wrong msg type fails") + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(authorization) - s.T().Log("Verify fetching capability with wrong grantee fails") - capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) - s.Require().Nil(capability) + s.T().Log("Verify fetching authorization with wrong grantee fails") + authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(authorization) s.T().Log("Verify revoke fails with wrong information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - s.Require().Nil(capability) + authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(authorization) s.T().Log("Verify revoke executes with correct information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - s.Require().NotNil(capability) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(authorization) } diff --git a/x/msg_authorization/internal/types/authorizations.go b/x/msg_authorization/internal/types/authorizations.go new file mode 100644 index 000000000000..d839451e746d --- /dev/null +++ b/x/msg_authorization/internal/types/authorizations.go @@ -0,0 +1,20 @@ +package types + +import ( + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + + abci "github.com/tendermint/tendermint/abci/types" +) + +type Authorization interface { + MsgType() sdk.Msg + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) +} + +type AuthorizationGrant struct { + Authorization Authorization + + Expiration time.Time +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go deleted file mode 100644 index 7029cb4228cc..000000000000 --- a/x/msg_authorization/internal/types/capabilities.go +++ /dev/null @@ -1,20 +0,0 @@ -package types - -import ( - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" - - abci "github.com/tendermint/tendermint/abci/types" -) - -type Capability interface { - MsgType() sdk.Msg - Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) -} - -type CapabilityGrant struct { - Capability Capability - - Expiration time.Time -} diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index cc2d381601b6..67199b4c9a76 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -10,9 +10,9 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) - cdc.RegisterConcrete(SendCapability{}, "cosmos-sdk/SendCapability", nil) - cdc.RegisterConcrete(CapabilityGrant{}, "cosmos-sdk/CapabilityGrant", nil) - cdc.RegisterConcrete(GenericCapability{}, "cosmos-sdk/GenericCapability", nil) + cdc.RegisterConcrete(SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) + cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) + cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) - cdc.RegisterInterface((*Capability)(nil), nil) + cdc.RegisterInterface((*Authorization)(nil), nil) } diff --git a/x/msg_authorization/internal/types/generic_authorization.go b/x/msg_authorization/internal/types/generic_authorization.go new file mode 100644 index 000000000000..a6529d42ba2d --- /dev/null +++ b/x/msg_authorization/internal/types/generic_authorization.go @@ -0,0 +1,21 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + +// GenericAuthorization grants the permission to execute any transaction of the provided +// sdk.Msg type without restrictions +type GenericAuthorization struct { + // MsgType is the type of Msg this capability grant allows + Msg sdk.Msg +} + +func (cap GenericAuthorization) MsgType() sdk.Msg { + return cap.MsgType() +} + +func (cap GenericAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) { + return true, cap, false +} diff --git a/x/msg_authorization/internal/types/generic_capability.go b/x/msg_authorization/internal/types/generic_capability.go deleted file mode 100644 index a269d726367b..000000000000 --- a/x/msg_authorization/internal/types/generic_capability.go +++ /dev/null @@ -1,21 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" -) - -// GenericCapability grants the permission to execute any transaction of the provided -// sdk.Msg type without restrictions -type GenericCapability struct { - // MsgType is the type of Msg this capability grant allows - Msg sdk.Msg -} - -func (cap GenericCapability) MsgType() sdk.Msg { - return cap.MsgType() -} - -func (cap GenericCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { - return true, cap, false -} diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go index a3c1bf9a2aad..1d9ec87d19a5 100644 --- a/x/msg_authorization/internal/types/keys.go +++ b/x/msg_authorization/internal/types/keys.go @@ -19,6 +19,6 @@ const ( QuerierRoute = ModuleName ) -func GetActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { +func GetActorAuthorizationKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) } diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index dde8de4caf8c..2a20e9626053 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -6,22 +6,22 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// MsgGrantAuthorization grants the provided capability to the grantee on the granter's +// MsgGrantAuthorization grants the provided authorization to the grantee on the granter's // account with the provided expiration time. type MsgGrantAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Capability Capability `json:"capability"` + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Authorization Authorization `json:"authorization"` // Expiration specifies the expiration time of the grant Expiration time.Time `json:"expiration"` } -func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorization Authorization, expiration time.Time) MsgGrantAuthorization { return MsgGrantAuthorization{ - Granter: granter, - Grantee: grantee, - Capability: capability, - Expiration: expiration, + Granter: granter, + Grantee: grantee, + Authorization: authorization, + Expiration: expiration, } } @@ -51,21 +51,21 @@ func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { return nil } -// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the +// MsgRevokeAuthorization revokes any authorization with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` - // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. - // i.e. this is what `Capability.MsgType()` returns - CapabilityMsgType sdk.Msg `json:"capability_msg_type"` + // AuthorizationMsgType is the type of sdk.Msg that the revoked Authorization refers to. + // i.e. this is what `Authorization.MsgType()` returns + AuthorizationMsgType sdk.Msg `json:"authorization_msg_type"` } -func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorizationMsgType sdk.Msg) MsgRevokeAuthorization { return MsgRevokeAuthorization{ - Granter: granter, - Grantee: grantee, - CapabilityMsgType: capabilityMsgType, + Granter: granter, + Grantee: grantee, + AuthorizationMsgType: authorizationMsgType, } } @@ -92,8 +92,8 @@ func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { } // MsgExecDelegated attempts to execute the provided messages using -// capabilities granted to the grantee. Each message should have only -// one signer corresponding to the granter of the capability. +// authorizations granted to the grantee. Each message should have only +// one signer corresponding to the granter of the authorization. type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` diff --git a/x/msg_authorization/internal/types/send_capability.go b/x/msg_authorization/internal/types/send_authorization.go similarity index 55% rename from x/msg_authorization/internal/types/send_capability.go rename to x/msg_authorization/internal/types/send_authorization.go index 230643102e4b..d95321e10357 100644 --- a/x/msg_authorization/internal/types/send_capability.go +++ b/x/msg_authorization/internal/types/send_authorization.go @@ -7,28 +7,28 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) -type SendCapability struct { +type SendAuthorization struct { // SpendLimit specifies the maximum amount of tokens that can be spent - // by this capability and will be updated as tokens are spent. If it is + // by this authorization and will be updated as tokens are spent. If it is // empty, there is no spend limit and any amount of coins can be spent. SpendLimit sdk.Coins } -func (capability SendCapability) MsgType() sdk.Msg { +func (authorization SendAuthorization) MsgType() sdk.Msg { return bank.MsgSend{} } -func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { +func (authorization SendAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) { switch msg := msg.(type) { case bank.MsgSend: - limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + limitLeft, isNegative := authorization.SpendLimit.SafeSub(msg.Amount) if isNegative { return false, nil, false } if limitLeft.IsZero() { return true, nil, true } - return true, SendCapability{SpendLimit: limitLeft}, false + return true, SendAuthorization{SpendLimit: limitLeft}, false } return false, nil, false } diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index 092cf9c0538b..923a05644493 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -60,8 +60,8 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { // GetTxCmd returns the transaction commands for this module func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { txCmd := &cobra.Command{ - Use: "msg_authorization", - Short: "msg_authorization transaction subcommands", + Use: "msg_auth", + Short: "msg_auth transaction subcommands", } txCmd.AddCommand(client.PostCommands()...) return txCmd From 4482b556d5bc1b294567b9d051bf0f42bfd222c2 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 30 Dec 2019 17:02:31 +0530 Subject: [PATCH 035/162] Add sendAs tx --- x/msg_authorization/client/cli/tx.go | 73 +++++++++++++++------------- x/msg_authorization/handler.go | 3 +- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 09293d32ce42..998902ee7b9f 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,8 @@ package cli import ( "bufio" + "fmt" + "github.com/cosmos/cosmos-sdk/client/input" "time" "github.com/cosmos/cosmos-sdk/client" @@ -111,37 +113,40 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { return cmd } -// -//func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { -// cmd := &cobra.Command{ -// Use: "send-as [grantee] [generated_tx] --from [granter]", -// Short: "send-as authorization", -// Long: "send-as authorization", -// Args: cobra.ExactArgs(2), -// RunE: func(cmd *cobra.Command, args []string) error { -// inBuf := bufio.NewReader(cmd.InOrStdin()) -// txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) -// cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) -// -// granter := cliCtx.FromAddress -// grantee, err := sdk.AccAddressFromBech32(args[0]) -// if err != nil { -// return err -// } -// -// var msgAuthorized sdk.Msg -// err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) -// if err != nil { -// return err -// } -// -// msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) -// if err := msg.ValidateBasic(); err != nil { -// return err -// } -// -// return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) -// }, -// } -// return cmd -//} +func GetCmdTxSendAs(cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "send-as [grantee] [generated_tx] --from [granter]", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + generatedTx, err := input.GetString("Enter generated tx json string:", inBuf) + + if err != nil { + return err + } + + var stdTx auth.StdTx + + err = cdc.UnmarshalJSON([]byte(generatedTx), &stdTx) + + msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + }, + } + return cmd +} diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 3024d9323623..97d43a545bca 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -53,6 +53,5 @@ func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k } func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result { - //TODO - return sdk.Result{Events: ctx.EventManager().Events()} + return k.DispatchActions(ctx, msg.Grantee, msg.Msgs) } From a4ae5025ab1e2491b489697816364cf3975154fd Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 30 Dec 2019 17:05:11 +0530 Subject: [PATCH 036/162] Fix signer --- x/msg_authorization/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 998902ee7b9f..3e770931e9e6 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -140,7 +140,7 @@ func GetCmdTxSendAs(cdc *codec.Codec) *cobra.Command { err = cdc.UnmarshalJSON([]byte(generatedTx), &stdTx) - msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs) + msg := types.NewMsgExecDelegated(granter, stdTx.Msgs) if err := msg.ValidateBasic(); err != nil { return err } From 7806826fa912aeafc7a74cecbed3ff3f1f78ee4a Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 30 Dec 2019 18:58:00 +0530 Subject: [PATCH 037/162] Fix tx input as a parameter to send-as --- x/msg_authorization/client/cli/tx.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 3e770931e9e6..93b8a0b36412 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,8 +2,6 @@ package cli import ( "bufio" - "fmt" - "github.com/cosmos/cosmos-sdk/client/input" "time" "github.com/cosmos/cosmos-sdk/client" @@ -115,7 +113,7 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { func GetCmdTxSendAs(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "send-as [grantee] [generated_tx] --from [granter]", + Use: "send-as [granter] [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), @@ -124,23 +122,27 @@ func GetCmdTxSendAs(cdc *codec.Codec) *cobra.Command { txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) - granter := cliCtx.FromAddress - grantee, err := sdk.AccAddressFromBech32(args[0]) - if err != nil { - return err - } + grantee := cliCtx.FromAddress - generatedTx, err := input.GetString("Enter generated tx json string:", inBuf) + // TODO cleanup this + //granter, err := sdk.AccAddressFromBech32(args[0]) + //if err != nil { + // return err + //} + // TODO interactive should look good, consider second arg as optional? + //generatedTx, err := input.GetString("Enter generated tx json string:", inBuf) + + var stdTx auth.StdTx + + err := cdc.UnmarshalJSON([]byte(args[1]), &stdTx) if err != nil { return err } - var stdTx auth.StdTx - - err = cdc.UnmarshalJSON([]byte(generatedTx), &stdTx) + msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs) + // TODO include the granter as delegated signer in the encoded JSON - msg := types.NewMsgExecDelegated(granter, stdTx.Msgs) if err := msg.ValidateBasic(); err != nil { return err } From b736780703d2dcc27eda18d85413fab2ffb4d57d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 31 Dec 2019 15:47:40 +0530 Subject: [PATCH 038/162] Merge branch 'master' of github.com:cosmos/cosmos-sdk into regen-network/skip-upgrade --- x/msg_authorization/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 93b8a0b36412..5343765ce872 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -147,7 +147,7 @@ func GetCmdTxSendAs(cdc *codec.Codec) *cobra.Command { return err } - return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) }, } return cmd From fc3218f4b060feaed6880ce124709d5a8f8cba6f Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 31 Dec 2019 19:32:55 +0530 Subject: [PATCH 039/162] Modified error handling in keeper --- x/msg_authorization/internal/keeper/keeper.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 5f7939cce25a..f7878b9ba583 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -7,6 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) @@ -49,22 +50,22 @@ func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA // DispatchActions attempts to execute the provided messages via authorization // grants from the message signer to the grantee. -func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { - var res sdk.Result +func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) (*sdk.Result, error) { + var res *sdk.Result for _, msg := range msgs { signers := msg.GetSigners() if len(signers) != 1 { - return sdk.ErrUnknownRequest("authorization can be given to msg with only one signer").Result() + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "authorization can be given to msg with only one signer") } granter := signers[0] if !bytes.Equal(granter, grantee) { authorization, _ := k.GetAuthorization(ctx, grantee, granter, msg) if authorization == nil { - return sdk.ErrUnauthorized("authorization not found").Result() + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } allow, updated, del := authorization.Accept(msg, ctx.BlockHeader()) if !allow { - return sdk.ErrUnauthorized(" ").Result() + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } if del { k.Revoke(ctx, grantee, granter, msg) @@ -72,8 +73,8 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] k.update(ctx, grantee, granter, updated) } } - res = k.router.Route(msg.Route())(ctx, msg) - if !res.IsOK() { + res, _ = k.router.Route(msg.Route())(ctx, msg) + if !res.Data { return res } } From 8c065c9b1caf4a6fc1d501d163dc3ea0073e7d69 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 12:48:47 +0530 Subject: [PATCH 040/162] Registered errors of msg_authorization --- x/msg_authorization/internal/keeper/keeper.go | 6 ++--- x/msg_authorization/internal/types/errors.go | 27 +++++-------------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index f7878b9ba583..974d4ea9890c 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -74,12 +74,12 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] } } res, _ = k.router.Route(msg.Route())(ctx, msg) - if !res.Data { - return res + if res != nil { + return res, nil } } - return sdk.Result{} + return nil, nil } // Grant method grants the provided authorization to the grantee on the granter's account with the provided expiration diff --git a/x/msg_authorization/internal/types/errors.go b/x/msg_authorization/internal/types/errors.go index 7e685bba677c..af7de1633c48 100644 --- a/x/msg_authorization/internal/types/errors.go +++ b/x/msg_authorization/internal/types/errors.go @@ -1,27 +1,12 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) -type CodeType = sdk.CodeType - -const ( - // Default slashing codespace - DefaultCodespace sdk.CodespaceType = ModuleName - - CodeInvalidGranter CodeType = 101 - CodeInvalidGrantee CodeType = 102 - CodeInvalidExpirationTime CodeType = 103 +// x/gov module sentinel errors +var ( + ErrInvalidGranter = sdkerrors.Register(ModuleName, 1, "invalid granter address") + ErrInvalidGrantee = sdkerrors.Register(ModuleName, 2, "invalid grantee address") + ErrInvalidExpirationTime = sdkerrors.Register(ModuleName, 3, "expiration time of authorization should be more than current time") ) - -func ErrInvalidGranter(codespace sdk.CodespaceType) sdk.Error { - return sdk.NewError(codespace, CodeInvalidGranter, "invalid granter address") -} - -func ErrInvalidGrantee(codespace sdk.CodespaceType) sdk.Error { - return sdk.NewError(codespace, CodeInvalidGrantee, "invalid grantee address") -} -func ErrInvalidExpirationTime(codespace sdk.CodespaceType) sdk.Error { - return sdk.NewError(codespace, CodeInvalidExpirationTime, "expiration time of authorization should be more than current time") -} From c7edbb7437dc480c9ff17e8b0f6ef71a3705eb16 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 14:28:36 +0530 Subject: [PATCH 041/162] Modified messages to new error format --- x/msg_authorization/internal/types/msgs.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 2a20e9626053..423fa480eae3 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -4,6 +4,7 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) // MsgGrantAuthorization grants the provided authorization to the grantee on the granter's @@ -37,15 +38,15 @@ func (msg MsgGrantAuthorization) GetSignBytes() []byte { return sdk.MustSortJSON(bz) } -func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { +func (msg MsgGrantAuthorization) ValidateBasic() error { if msg.Granter.Empty() { - return ErrInvalidGranter(DefaultCodespace) + return sdkerrors.Wrap(ErrInvalidGranter, "missing granter address") } if msg.Grantee.Empty() { - return ErrInvalidGrantee(DefaultCodespace) + return sdkerrors.Wrap(ErrInvalidGranter, "missing grantee address") } if msg.Expiration.Unix() < time.Now().Unix() { - return ErrInvalidExpirationTime(DefaultCodespace) + return sdkerrors.Wrap(ErrInvalidGranter, "Time can't be in the past") } return nil @@ -81,12 +82,12 @@ func (msg MsgRevokeAuthorization) GetSignBytes() []byte { return sdk.MustSortJSON(bz) } -func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { +func (msg MsgRevokeAuthorization) ValidateBasic() error { if msg.Granter.Empty() { - return sdk.ErrInvalidAddress(msg.Granter.String()) + return sdkerrors.Wrap(ErrInvalidGranter, "missing granter address") } if msg.Grantee.Empty() { - return sdk.ErrInvalidAddress(msg.Grantee.String()) + return sdkerrors.Wrap(ErrInvalidGranter, "missing grantee address") } return nil } @@ -118,9 +119,9 @@ func (msg MsgExecDelegated) GetSignBytes() []byte { return sdk.MustSortJSON(bz) } -func (msg MsgExecDelegated) ValidateBasic() sdk.Error { +func (msg MsgExecDelegated) ValidateBasic() error { if msg.Grantee.Empty() { - return sdk.ErrInvalidAddress(msg.Grantee.String()) + return sdkerrors.Wrap(ErrInvalidGranter, "missing grantee address") } return nil } From 7b62a0b03efffe816dc62752d8016a9183e7d844 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 14:49:03 +0530 Subject: [PATCH 042/162] Updated test keepers --- x/msg_authorization/internal/keeper/test_common.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index f7874ff21cfe..11b782a0263d 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -59,9 +59,9 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base blacklistedAddrs := make(map[string]bool) - paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace) + paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams) authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) - bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) + bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) bankKeeper.SetSendEnabled(ctx, true) router := baseapp.NewRouter() From 8402bbea1721d95d6a7c978927fbc62885f78c76 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 15:00:01 +0530 Subject: [PATCH 043/162] Modified cli to latest sdk format --- x/msg_authorization/alias.go | 13 ++++--------- x/msg_authorization/client/cli/query.go | 3 ++- x/msg_authorization/client/cli/tx.go | 7 ++++--- x/msg_authorization/handler.go | 18 ++++++++---------- x/msg_authorization/module.go | 6 +++--- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index 05fa33ba1f3d..bad605d02e0e 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -11,14 +11,10 @@ import ( ) const ( - DefaultCodespace = types.DefaultCodespace - CodeInvalidGranter = types.CodeInvalidGranter - CodeInvalidGrantee = types.CodeInvalidGrantee - CodeInvalidExpirationTime = types.CodeInvalidExpirationTime - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - QuerierRoute = types.QuerierRoute + ModuleName = types.ModuleName + StoreKey = types.StoreKey + RouterKey = types.RouterKey + QuerierRoute = types.QuerierRoute ) var ( @@ -41,7 +37,6 @@ type ( SendAuthorization = types.SendAuthorization AuthorizationGrant = types.AuthorizationGrant GenericAuthorization = types.GenericAuthorization - CodeType = types.CodeType MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization MsgExecDelegated = types.MsgExecDelegated diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index 1acc77684b65..5a193ffee882 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -4,6 +4,7 @@ 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" @@ -21,7 +22,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { RunE: client.ValidateCmd, } - authorizationQueryCmd.AddCommand(client.GetCommands( + authorizationQueryCmd.AddCommand(flags.GetCommands( GetCmdQueryAuthorization(queryRoute, cdc), )...) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 5343765ce872..2919ce6e0203 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -6,6 +6,7 @@ import ( "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" @@ -26,10 +27,10 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { RunE: client.ValidateCmd, } - AuthorizationTxCmd.AddCommand(client.PostCommands( + AuthorizationTxCmd.AddCommand(flags.PostCommands( GetCmdGrantAuthorization(cdc), GetCmdRevokeAuthorization(cdc), - //GetCmdSendAs(cdc), + GetCmdSendAs(cdc), )...) return AuthorizationTxCmd @@ -111,7 +112,7 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { return cmd } -func GetCmdTxSendAs(cdc *codec.Codec) *cobra.Command { +func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "send-as [granter] [msg_tx_json] --from [grantee]", Short: "execute tx on behalf of granter account", diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 97d43a545bca..ec63daba7eea 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1,14 +1,13 @@ package msg_authorization import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/distribution/types" ) func NewHandler(k Keeper) sdk.Handler { - return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { + return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { ctx = ctx.WithEventManager(sdk.NewEventManager()) switch msg := msg.(type) { case MsgGrantAuthorization: @@ -18,13 +17,12 @@ func NewHandler(k Keeper) sdk.Handler { case MsgExecDelegated: return handleMsgExecDelegated(ctx, msg, k) default: - errMsg := fmt.Sprintf("unrecognized authorization message type: %T", msg) - return sdk.ErrUnknownRequest(errMsg).Result() + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized authorization message type: %T", msg) } } } -func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { +func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) (*sdk.Result, error) { k.Grant(ctx, msg.Grantee, msg.Granter, msg.Authorization, msg.Expiration) ctx.EventManager().EmitEvent( @@ -35,10 +33,10 @@ func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k K ), ) - return sdk.Result{Events: ctx.EventManager().Events()} + return &sdk.Result{Events: ctx.EventManager().Events()}, nil } -func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { +func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) (*sdk.Result, error) { k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) ctx.EventManager().EmitEvent( @@ -49,9 +47,9 @@ func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k ), ) - return sdk.Result{Events: ctx.EventManager().Events()} + return &sdk.Result{Events: ctx.EventManager().Events()}, nil } -func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result { +func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) (*sdk.Result, error) { return k.DispatchActions(ctx, msg.Grantee, msg.Msgs) } diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index 923a05644493..75ee113d4aad 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -2,8 +2,8 @@ package msg_authorization import ( "encoding/json" - "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/types/module" @@ -49,7 +49,7 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { Use: "msg_authorization", Short: "Querying commands for the msg_authorization module", } - queryCmd.AddCommand(client.GetCommands( + queryCmd.AddCommand(flags.GetCommands( cli.GetPlanCmd(StoreKey, cdc), cli.GetAppliedHeightCmd(StoreKey, cdc), )...) @@ -63,7 +63,7 @@ func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { Use: "msg_auth", Short: "msg_auth transaction subcommands", } - txCmd.AddCommand(client.PostCommands()...) + txCmd.AddCommand(flags.PostCommands()...) return txCmd } From a59cee096ad55dad1a59fbd540c5e605db47e40c Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 16:46:18 +0530 Subject: [PATCH 044/162] Modified module to latest sdk format --- x/msg_authorization/module.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index 75ee113d4aad..e2cfb8302342 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -7,8 +7,8 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/client/cli" "github.com/cosmos/cosmos-sdk/x/msg_authorization/client/rest" - "github.com/cosmos/cosmos-sdk/x/upgrade/client/cli" "github.com/gorilla/mux" "github.com/spf13/cobra" abci "github.com/tendermint/tendermint/abci/types" @@ -50,8 +50,7 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { Short: "Querying commands for the msg_authorization module", } queryCmd.AddCommand(flags.GetCommands( - cli.GetPlanCmd(StoreKey, cdc), - cli.GetAppliedHeightCmd(StoreKey, cdc), + cli.GetCmdQueryAuthorization(StoreKey, cdc), )...) return queryCmd @@ -63,7 +62,11 @@ func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { Use: "msg_auth", Short: "msg_auth transaction subcommands", } - txCmd.AddCommand(flags.PostCommands()...) + txCmd.AddCommand(flags.PostCommands( + cli.GetCmdGrantAuthorization(cdc), + cli.GetCmdRevokeAuthorization(cdc), + cli.GetCmdSendAs(cdc), + )...) return txCmd } From 72ce01c6630b9df113ec8ecdebff746248ec6423 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 17:29:43 +0530 Subject: [PATCH 045/162] Modified module to latest sdk format --- x/msg_authorization/module.go | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index e2cfb8302342..84a5ec11aeac 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -2,16 +2,17 @@ package msg_authorization import ( "encoding/json" + + "github.com/gorilla/mux" + "github.com/spf13/cobra" + abci "github.com/tendermint/tendermint/abci/types" + "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/types/module" "github.com/cosmos/cosmos-sdk/x/msg_authorization/client/cli" "github.com/cosmos/cosmos-sdk/x/msg_authorization/client/rest" - "github.com/gorilla/mux" - "github.com/spf13/cobra" - abci "github.com/tendermint/tendermint/abci/types" ) // module codec @@ -45,29 +46,12 @@ func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, r *mux.Router) //GetQueryCmd returns the cli query commands for this module func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { - queryCmd := &cobra.Command{ - Use: "msg_authorization", - Short: "Querying commands for the msg_authorization module", - } - queryCmd.AddCommand(flags.GetCommands( - cli.GetCmdQueryAuthorization(StoreKey, cdc), - )...) - - return queryCmd + return cli.GetQueryCmd(StoreKey, cdc) } // GetTxCmd returns the transaction commands for this module func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { - txCmd := &cobra.Command{ - Use: "msg_auth", - Short: "msg_auth transaction subcommands", - } - txCmd.AddCommand(flags.PostCommands( - cli.GetCmdGrantAuthorization(cdc), - cli.GetCmdRevokeAuthorization(cdc), - cli.GetCmdSendAs(cdc), - )...) - return txCmd + return cli.GetTxCmd(cdc) } // AppModule implements the sdk.AppModule interface From 909369d4745a2b5758993a85e482df431f7724a3 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 17:54:20 +0530 Subject: [PATCH 046/162] Modified module to latest sdk format --- x/msg_authorization/client/cli/query.go | 7 ++++--- x/msg_authorization/client/cli/tx.go | 2 +- x/msg_authorization/module.go | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index 5a193ffee882..e6bd8896e7ea 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -31,11 +31,12 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { // GetCmdQueryAuthorization implements the query authorizations command. func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command { + //TODO update description return &cobra.Command{ - Use: "", + Use: "authorization", Args: cobra.ExactArgs(3), - Short: "", - Long: "", + Short: "query authorzation for a granter-grantee pair", + Long: "query authorzation for a granter-grantee pair", RunE: func(cmd *cobra.Command, args []string) error { cliCtx := context.NewCLIContext().WithCodec(cdc) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 2919ce6e0203..2fcff219fdb4 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -17,7 +17,7 @@ import ( ) // GetTxCmd returns the transaction commands for this module -func GetTxCmd(cdc *codec.Codec) *cobra.Command { +func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command { AuthorizationTxCmd := &cobra.Command{ Use: types.ModuleName, Short: "Authorization transactions subcommands", diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index 84a5ec11aeac..fe75f62d1b77 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -51,7 +51,7 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { // GetTxCmd returns the transaction commands for this module func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { - return cli.GetTxCmd(cdc) + return cli.GetTxCmd(StoreKey, cdc) } // AppModule implements the sdk.AppModule interface From 3fad8b859fc5f280794cdee4dad0a87770fead08 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 18:52:21 +0530 Subject: [PATCH 047/162] Modified cli for grant authorization --- x/msg_authorization/client/cli/tx.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 2fcff219fdb4..65ea67c7befe 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,7 @@ package cli import ( "bufio" + "io/ioutil" "time" "github.com/cosmos/cosmos-sdk/client" @@ -53,8 +54,13 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } + bz, err := ioutil.ReadFile(args[1]) + if err != nil { + return err + } + var authorization types.Authorization - err = cdc.UnmarshalJSON([]byte(args[1]), &authorization) + err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err } @@ -95,8 +101,13 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { return err } + bz, err := ioutil.ReadFile(args[1]) + if err != nil { + return err + } + var msgAuthorized sdk.Msg - err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) + err = cdc.UnmarshalJSON(bz, &msgAuthorized) if err != nil { return err } From 4ba96076b4d3e536716830f8fb40aa185a7b7c4b Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 20:05:54 +0530 Subject: [PATCH 048/162] Modified cli for grant authorization --- x/msg_authorization/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 65ea67c7befe..813741ae3f2b 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -59,7 +59,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - var authorization types.Authorization + var authorization types.SendAuthorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err From 733b90479f8f4f38c3e4a9284765ed6c2a22e7b6 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 20:17:25 +0530 Subject: [PATCH 049/162] Changed default value for expiration --- x/msg_authorization/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 813741ae3f2b..978a6b215b82 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -79,7 +79,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { }, } - cmd.Flags().String(FlagExpiration, "9999-12-31 23:59:59.52Z", "The time upto which the authorization is active for the user") + cmd.Flags().String(FlagExpiration, "9999-12-31T23:59:59.52Z", "The time upto which the authorization is active for the user") return cmd } From ec34c3d30aad48180d07fd5285880e62cd412d64 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 20:30:08 +0530 Subject: [PATCH 050/162] Reverted unmarshal interface type --- x/msg_authorization/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 978a6b215b82..74ff09e42301 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -59,7 +59,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - var authorization types.SendAuthorization + var authorization types.Authorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err From f2fc7112fd043e5e57b5420afca7dcb249fee5db Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 20:54:45 +0530 Subject: [PATCH 051/162] Added debugging --- x/msg_authorization/client/cli/tx.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 74ff09e42301..2629fafa5ccf 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,7 @@ package cli import ( "bufio" + "fmt" "io/ioutil" "time" @@ -64,6 +65,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { if err != nil { return err } + fmt.Printf("this is authorization %v", authorization) expirationString := viper.GetString(FlagExpiration) expiration, err := time.Parse(time.RFC3339, expirationString) if err != nil { From 878b9a08f8ac4468ee04e9522af8fa32487e817d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 2 Jan 2020 21:19:19 +0530 Subject: [PATCH 052/162] Fixed unmarshal method --- x/msg_authorization/client/cli/tx.go | 2 +- x/msg_authorization/internal/keeper/keeper_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 2629fafa5ccf..83c3a500fa15 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -61,7 +61,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { } var authorization types.Authorization - err = cdc.UnmarshalJSON(bz, &authorization) + err = cdc.UnmarshalBinaryBare(bz, &authorization) if err != nil { return err } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 8730ac4147fc..2dd53fe57c1c 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -47,7 +47,8 @@ func (s *TestSuite) TestKeeper() { s.Require().Nil(authorization) s.T().Log("Verify if authorization is accepted") - s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendAuthorization{SpendLimit: newCoins}, now.Add(time.Hour)) + x := types.SendAuthorization{SpendLimit: newCoins} + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, x, now.Add(time.Hour)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(authorization) s.Require().Equal(authorization.MsgType(), bank.MsgSend{}) From 163e8e441c7d2b6826ac08eba7689deded744733 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 15:43:00 +0530 Subject: [PATCH 053/162] Changed unmarshal method to Unmarshaljson --- x/msg_authorization/client/cli/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 83c3a500fa15..2629fafa5ccf 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -61,7 +61,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { } var authorization types.Authorization - err = cdc.UnmarshalBinaryBare(bz, &authorization) + err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err } From 123d92c13769cba54c9a67b911fd5a4f26b40359 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Fri, 3 Jan 2020 01:07:24 +0530 Subject: [PATCH 054/162] Add more keeper tests and debug logs -- WIP --- x/bank/handler.go | 4 + x/msg_authorization/internal/keeper/keeper.go | 1 + .../internal/keeper/keeper_test.go | 85 +++++++++++++++++-- .../internal/keeper/test_common.go | 2 + .../internal/types/send_authorization.go | 4 + 5 files changed, 88 insertions(+), 8 deletions(-) diff --git a/x/bank/handler.go b/x/bank/handler.go index 3c83ec4c2af4..03b952be203f 100644 --- a/x/bank/handler.go +++ b/x/bank/handler.go @@ -1,6 +1,7 @@ package bank import ( + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper" @@ -27,6 +28,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler { // Handle MsgSend. func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) (*sdk.Result, error) { + fmt.Println("inside msg send", msg.ToAddress.String(), msg.FromAddress.String(), msg.Amount) if !k.GetSendEnabled(ctx) { return nil, types.ErrSendDisabled } @@ -40,6 +42,8 @@ func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) (*sdk.Re return nil, err } + fmt.Println("no error") + ctx.EventManager().EmitEvent( sdk.NewEvent( sdk.EventTypeMessage, diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 974d4ea9890c..fb0ae56fd87e 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -64,6 +64,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } allow, updated, del := authorization.Accept(msg, ctx.BlockHeader()) + fmt.Println("inside accept: ", allow, updated, del) if !allow { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 2dd53fe57c1c..1b8abed12758 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1,6 +1,8 @@ package keeper import ( + "fmt" + "github.com/kr/pretty" "testing" "time" @@ -25,7 +27,6 @@ type TestSuite struct { func (s *TestSuite) SetupTest() { s.ctx, s.accountKeeper, s.paramsKeeper, s.bankKeeper, s.keeper, s.router = SetupTestInput() - } func (s *TestSuite) TestKeeper() { @@ -33,7 +34,7 @@ func (s *TestSuite) TestKeeper() { s.Require().Nil(err) s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - s.T().Log("Verify that no authorization returns nil") + s.T().Log("verify that no authorization returns nil") authorization, _ := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(authorization) //require.Nil(t, expiration) @@ -41,38 +42,106 @@ func (s *TestSuite) TestKeeper() { s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) - s.T().Log("Verify if expired authorization is rejected") + s.T().Log("verify if expired authorization is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendAuthorization{SpendLimit: newCoins}, now.Add(-1*time.Hour)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(authorization) - s.T().Log("Verify if authorization is accepted") + s.T().Log("verify if authorization is accepted") x := types.SendAuthorization{SpendLimit: newCoins} s.keeper.Grant(s.ctx, granteeAddr, granterAddr, x, now.Add(time.Hour)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(authorization) s.Require().Equal(authorization.MsgType(), bank.MsgSend{}) - s.T().Log("Verify fetching authorization with wrong msg type fails") + s.T().Log("verify fetching authorization with wrong msg type fails") authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) s.Require().Nil(authorization) - s.T().Log("Verify fetching authorization with wrong grantee fails") + s.T().Log("verify fetching authorization with wrong grantee fails") authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) s.Require().Nil(authorization) - s.T().Log("Verify revoke fails with wrong information") + s.T().Log("") + + s.T().Log("verify revoke fails with wrong information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(authorization) - s.T().Log("Verify revoke executes with correct information") + s.T().Log("verify revoke executes with correct information") s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(authorization) } +func (s *TestSuite) TestKeeperFees () { + err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + s.Require().Nil(err) + s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + + now := s.ctx.BlockHeader().Time + s.Require().NotNil(now) + + smallCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 20)) + someCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 123)) + //lotCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 4567)) + + msgs := types.MsgExecDelegated { + Grantee: granteeAddr, + Msgs: []sdk.Msg{ + bank.MsgSend { + Amount: sdk.NewCoins(sdk.NewInt64Coin("steak", 2)), + FromAddress: granterAddr, + ToAddress:recepientAddr, + }, + }, + } + + s.T().Log("verify dispatch fails with invalid authorization") + result, error := s.keeper.DispatchActions(s.ctx, granteeAddr, msgs.Msgs) + s.T().Log(error.Error()) + s.Require().Nil(result) + s.Require().NotNil(error) + + s.T().Log("verify dispatch executes with correct information") + // grant authorization + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendAuthorization{SpendLimit:smallCoin}, now) + authorization, _ := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(authorization) + s.Require().Equal(authorization.MsgType(), bank.MsgSend{}) + result, error = s.keeper.DispatchActions(s.ctx, granteeAddr, msgs.Msgs) + s.Require().NotNil(result) + s.Require().Nil(error) + fmt.Printf("%# v", pretty.Formatter(result)) + + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(authorization) + + s.T().Log("verify dispatch fails with overlimit") + // grant authorization + + msgs = types.MsgExecDelegated { + Grantee: granteeAddr, + Msgs: []sdk.Msg{ + bank.MsgSend { + Amount: someCoin, + FromAddress: granterAddr, + ToAddress:recepientAddr, + }, + }, + } + + result, error = s.keeper.DispatchActions(s.ctx, granteeAddr, msgs.Msgs) + s.Require().Nil(result) + s.Require().NotNil(error) + fmt.Printf("%# v", pretty.Formatter(error)) + + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(authorization) +} + func TestTestSuite(t *testing.T) { suite.Run(t, new(TestSuite)) } diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 11b782a0263d..507caf03f015 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -78,7 +78,9 @@ var ( granteePub = ed25519.GenPrivKey().PubKey() granterPub = ed25519.GenPrivKey().PubKey() recepientPub = ed25519.GenPrivKey().PubKey() + randomPub = ed25519.GenPrivKey().PubKey() granteeAddr = sdk.AccAddress(granteePub.Address()) granterAddr = sdk.AccAddress(granterPub.Address()) recepientAddr = sdk.AccAddress(recepientPub.Address()) + randomAddr = sdk.AccAddress(randomPub.Address()) ) diff --git a/x/msg_authorization/internal/types/send_authorization.go b/x/msg_authorization/internal/types/send_authorization.go index d95321e10357..2acea9279659 100644 --- a/x/msg_authorization/internal/types/send_authorization.go +++ b/x/msg_authorization/internal/types/send_authorization.go @@ -1,6 +1,7 @@ package types import ( + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" @@ -28,6 +29,9 @@ func (authorization SendAuthorization) Accept(msg sdk.Msg, block abci.Header) (a if limitLeft.IsZero() { return true, nil, true } + + fmt.Println(limitLeft, "limitLeft") + return true, SendAuthorization{SpendLimit: limitLeft}, false } return false, nil, false From 33f943595001f279f27f051286a414c629b50b9d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 16:01:49 +0530 Subject: [PATCH 055/162] Added debugging comments --- x/msg_authorization/client/cli/tx.go | 12 ++++++---- .../internal/keeper/keeper_test.go | 23 ++++++++++--------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 2629fafa5ccf..3209dfd7fab5 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -55,11 +55,15 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - bz, err := ioutil.ReadFile(args[1]) - if err != nil { - return err - } + //bz, err := ioutil.ReadFile(args[1]) + //if err != nil { + // return err + //} + bz := []byte(`{ + "type": "cosmos-sdk/SendAuthorization", + "value":{"spendlimit": "100stake"} +}`) var authorization types.Authorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 1b8abed12758..a61682de4bbf 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -43,7 +43,8 @@ func (s *TestSuite) TestKeeper() { newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) s.T().Log("verify if expired authorization is rejected") - s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendAuthorization{SpendLimit: newCoins}, now.Add(-1*time.Hour)) + x := types.SendAuthorization{SpendLimit: newCoins} + s.keeper.Grant(s.ctx, granterAddr, granteeAddr, x, now.Add(-1*time.Hour)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(authorization) @@ -76,7 +77,7 @@ func (s *TestSuite) TestKeeper() { } -func (s *TestSuite) TestKeeperFees () { +func (s *TestSuite) TestKeeperFees() { err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) s.Require().Nil(err) s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) @@ -88,13 +89,13 @@ func (s *TestSuite) TestKeeperFees () { someCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 123)) //lotCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 4567)) - msgs := types.MsgExecDelegated { + msgs := types.MsgExecDelegated{ Grantee: granteeAddr, Msgs: []sdk.Msg{ - bank.MsgSend { - Amount: sdk.NewCoins(sdk.NewInt64Coin("steak", 2)), + bank.MsgSend{ + Amount: sdk.NewCoins(sdk.NewInt64Coin("steak", 2)), FromAddress: granterAddr, - ToAddress:recepientAddr, + ToAddress: recepientAddr, }, }, } @@ -107,7 +108,7 @@ func (s *TestSuite) TestKeeperFees () { s.T().Log("verify dispatch executes with correct information") // grant authorization - s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendAuthorization{SpendLimit:smallCoin}, now) + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendAuthorization{SpendLimit: smallCoin}, now) authorization, _ := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(authorization) s.Require().Equal(authorization.MsgType(), bank.MsgSend{}) @@ -122,13 +123,13 @@ func (s *TestSuite) TestKeeperFees () { s.T().Log("verify dispatch fails with overlimit") // grant authorization - msgs = types.MsgExecDelegated { + msgs = types.MsgExecDelegated{ Grantee: granteeAddr, Msgs: []sdk.Msg{ - bank.MsgSend { - Amount: someCoin, + bank.MsgSend{ + Amount: someCoin, FromAddress: granterAddr, - ToAddress:recepientAddr, + ToAddress: recepientAddr, }, }, } From 3b496c0728567c0fadd01b803fc493e3e04d13d0 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 16:21:51 +0530 Subject: [PATCH 056/162] Moved authorization to exported --- x/msg_authorization/alias.go | 3 ++- x/msg_authorization/client/cli/tx.go | 18 +++++++++--------- x/msg_authorization/client/rest/tx.go | 11 ++++++----- x/msg_authorization/exported/keeper.go | 5 ++--- x/msg_authorization/exported/types.go | 12 ++++++++++++ x/msg_authorization/internal/keeper/keeper.go | 7 ++++--- .../internal/types/authorizations.go | 11 ++--------- x/msg_authorization/internal/types/codec.go | 3 ++- .../internal/types/generic_authorization.go | 4 +++- x/msg_authorization/internal/types/msgs.go | 9 +++++---- .../internal/types/send_authorization.go | 3 ++- 11 files changed, 49 insertions(+), 37 deletions(-) create mode 100644 x/msg_authorization/exported/types.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index bad605d02e0e..0afe3b8c428f 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -6,6 +6,7 @@ package msg_authorization import ( + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/keeper" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) @@ -33,7 +34,7 @@ var ( ) type ( - Authorization = types.Authorization + Authorization = exported.Authorization SendAuthorization = types.SendAuthorization AuthorizationGrant = types.AuthorizationGrant GenericAuthorization = types.GenericAuthorization diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 3209dfd7fab5..b896dc69ac87 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -55,16 +55,16 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - //bz, err := ioutil.ReadFile(args[1]) - //if err != nil { - // return err - //} + bz, err := ioutil.ReadFile(args[1]) + if err != nil { + return err + } - bz := []byte(`{ - "type": "cosmos-sdk/SendAuthorization", - "value":{"spendlimit": "100stake"} -}`) - var authorization types.Authorization + // bz := []byte(`{ + // "type": "cosmos-sdk/SendAuthorization", + // "value":{"spendlimit": "100stake"} + //}`) + var authorization types.SendAuthorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go index f2e08a6e72cf..bae1e7d1a714 100644 --- a/x/msg_authorization/client/rest/tx.go +++ b/x/msg_authorization/client/rest/tx.go @@ -5,6 +5,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/auth/client/utils" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/gorilla/mux" "net/http" @@ -17,11 +18,11 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { } 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"` + BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Authorization exported.Authorization `json:"authorization"` + Expiration time.Time `json:"expiration"` } type RevokeRequest struct { diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index b180c87424e0..73b8c6053d12 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -4,7 +4,6 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) type Keeper interface { @@ -13,12 +12,12 @@ type Keeper interface { // 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) + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization 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) + GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Authorization, expiration time.Time) } diff --git a/x/msg_authorization/exported/types.go b/x/msg_authorization/exported/types.go new file mode 100644 index 000000000000..64e29a4be253 --- /dev/null +++ b/x/msg_authorization/exported/types.go @@ -0,0 +1,12 @@ +package exported + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + abci "github.com/tendermint/tendermint/abci/types" +) + +type Authorization interface { + MsgType() sdk.Msg + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) +} diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index fb0ae56fd87e..1d0b163daaf4 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) @@ -40,7 +41,7 @@ func (k Keeper) getAuthorizationGrant(ctx sdk.Context, actor []byte) (grant type return grant, true } -func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Authorization) { +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated exported.Authorization) { grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, updated.MsgType())) if !found { return @@ -86,7 +87,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] // Grant method 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. -func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) { +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization exported.Authorization, expiration time.Time) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshalBinaryBare(types.AuthorizationGrant{Authorization: authorization, Expiration: expiration}) actor := k.getActorAuthorizationKey(grantee, granter, authorization.MsgType()) @@ -101,7 +102,7 @@ func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA // GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time, // granted to the grantee by the granter for the provided msg type. -func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Authorization, expiration time.Time) { +func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap exported.Authorization, expiration time.Time) { grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, msgType)) if !found { return nil, time.Time{} diff --git a/x/msg_authorization/internal/types/authorizations.go b/x/msg_authorization/internal/types/authorizations.go index d839451e746d..bce5f52181b5 100644 --- a/x/msg_authorization/internal/types/authorizations.go +++ b/x/msg_authorization/internal/types/authorizations.go @@ -3,18 +3,11 @@ package types import ( "time" - sdk "github.com/cosmos/cosmos-sdk/types" - - abci "github.com/tendermint/tendermint/abci/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" ) -type Authorization interface { - MsgType() sdk.Msg - Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) -} - type AuthorizationGrant struct { - Authorization Authorization + Authorization exported.Authorization Expiration time.Time } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index 67199b4c9a76..128bf8660638 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" ) var ModuleCdc = codec.New() @@ -14,5 +15,5 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) - cdc.RegisterInterface((*Authorization)(nil), nil) + cdc.RegisterInterface((*exported.Authorization)(nil), nil) } diff --git a/x/msg_authorization/internal/types/generic_authorization.go b/x/msg_authorization/internal/types/generic_authorization.go index a6529d42ba2d..bd30c66a69b6 100644 --- a/x/msg_authorization/internal/types/generic_authorization.go +++ b/x/msg_authorization/internal/types/generic_authorization.go @@ -2,6 +2,8 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -16,6 +18,6 @@ func (cap GenericAuthorization) MsgType() sdk.Msg { return cap.MsgType() } -func (cap GenericAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) { +func (cap GenericAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated exported.Authorization, delete bool) { return true, cap, false } diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 423fa480eae3..7dddd46ca94b 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -5,19 +5,20 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" ) // MsgGrantAuthorization grants the provided authorization to the grantee on the granter's // account with the provided expiration time. type MsgGrantAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Authorization Authorization `json:"authorization"` + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Authorization exported.Authorization `json:"authorization"` // Expiration specifies the expiration time of the grant Expiration time.Time `json:"expiration"` } -func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorization Authorization, expiration time.Time) MsgGrantAuthorization { +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorization exported.Authorization, expiration time.Time) MsgGrantAuthorization { return MsgGrantAuthorization{ Granter: granter, Grantee: grantee, diff --git a/x/msg_authorization/internal/types/send_authorization.go b/x/msg_authorization/internal/types/send_authorization.go index 2acea9279659..2753621f11c5 100644 --- a/x/msg_authorization/internal/types/send_authorization.go +++ b/x/msg_authorization/internal/types/send_authorization.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" abci "github.com/tendermint/tendermint/abci/types" ) @@ -19,7 +20,7 @@ func (authorization SendAuthorization) MsgType() sdk.Msg { return bank.MsgSend{} } -func (authorization SendAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) { +func (authorization SendAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated exported.Authorization, delete bool) { switch msg := msg.(type) { case bank.MsgSend: limitLeft, isNegative := authorization.SpendLimit.SafeSub(msg.Amount) From a0e59f6059dc9277d3fb9005aa835c85c6652d52 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 16:29:33 +0530 Subject: [PATCH 057/162] Modified coded --- x/msg_authorization/internal/types/codec.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index 128bf8660638..36f1854dd34e 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -8,12 +8,11 @@ import ( var ModuleCdc = codec.New() func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterInterface((*exported.Authorization)(nil), nil) cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) cdc.RegisterConcrete(SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) - - cdc.RegisterInterface((*exported.Authorization)(nil), nil) } From 8c557606b29a01e1b04fec31f3bd00a2750a1c64 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 16:58:40 +0530 Subject: [PATCH 058/162] Added init to codec --- x/msg_authorization/internal/types/codec.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index 36f1854dd34e..572943c95c68 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -16,3 +16,7 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) } + +func init() { + RegisterCodec(ModuleCdc) +} From 3922c15f07dd524c1efd79437a8752703a13d478 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Fri, 3 Jan 2020 16:38:55 +0530 Subject: [PATCH 059/162] Add more tests for sendAs authorization --- x/bank/handler.go | 4 ---- x/msg_authorization/internal/keeper/test_common.go | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/x/bank/handler.go b/x/bank/handler.go index 03b952be203f..3c83ec4c2af4 100644 --- a/x/bank/handler.go +++ b/x/bank/handler.go @@ -1,7 +1,6 @@ package bank import ( - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/x/bank/internal/keeper" @@ -28,7 +27,6 @@ func NewHandler(k keeper.Keeper) sdk.Handler { // Handle MsgSend. func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) (*sdk.Result, error) { - fmt.Println("inside msg send", msg.ToAddress.String(), msg.FromAddress.String(), msg.Amount) if !k.GetSendEnabled(ctx) { return nil, types.ErrSendDisabled } @@ -42,8 +40,6 @@ func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) (*sdk.Re return nil, err } - fmt.Println("no error") - ctx.EventManager().EmitEvent( sdk.NewEvent( sdk.EventTypeMessage, diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 507caf03f015..8449cd629a07 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -78,9 +78,9 @@ var ( granteePub = ed25519.GenPrivKey().PubKey() granterPub = ed25519.GenPrivKey().PubKey() recepientPub = ed25519.GenPrivKey().PubKey() - randomPub = ed25519.GenPrivKey().PubKey() + randomPub = ed25519.GenPrivKey().PubKey() granteeAddr = sdk.AccAddress(granteePub.Address()) granterAddr = sdk.AccAddress(granterPub.Address()) recepientAddr = sdk.AccAddress(recepientPub.Address()) - randomAddr = sdk.AccAddress(randomPub.Address()) + randomAddr = sdk.AccAddress(randomPub.Address()) ) From feaef7f99e94ff76de50e481873f456497a3aa67 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 17:50:46 +0530 Subject: [PATCH 060/162] Added debugging comments --- x/auth/client/tx.go | 2 ++ x/msg_authorization/internal/types/codec.go | 27 ++++++++++----------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/x/auth/client/tx.go b/x/auth/client/tx.go index 12d50a65ab03..1a862f4683f2 100644 --- a/x/auth/client/tx.go +++ b/x/auth/client/tx.go @@ -235,6 +235,8 @@ func ReadStdTxFromFile(cdc *codec.Codec, filename string) (stdTx authtypes.StdTx return } + fmt.Printf("this is the json from file %v", stdTx) + return } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index 572943c95c68..269959b4589a 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -2,21 +2,20 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" ) var ModuleCdc = codec.New() -func RegisterCodec(cdc *codec.Codec) { - cdc.RegisterInterface((*exported.Authorization)(nil), nil) - cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) - cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) - cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) - cdc.RegisterConcrete(SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) - cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) - cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) -} - -func init() { - RegisterCodec(ModuleCdc) -} +//func RegisterCodec(cdc *codec.Codec) { +// cdc.RegisterInterface((*exported.Authorization)(nil), nil) +// cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) +// cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) +// cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) +// cdc.RegisterConcrete(SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) +// cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) +// cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) +//} +// +//func init() { +// RegisterCodec(ModuleCdc) +//} From 743d0436243f3c69c41bcf2acee4ee04f88cd6be Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 17:56:51 +0530 Subject: [PATCH 061/162] Removed comments in codec --- x/msg_authorization/internal/types/codec.go | 28 +++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index 269959b4589a..b2b61ef2b2c3 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -2,20 +2,22 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" ) var ModuleCdc = codec.New() -//func RegisterCodec(cdc *codec.Codec) { -// cdc.RegisterInterface((*exported.Authorization)(nil), nil) -// cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) -// cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) -// cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) -// cdc.RegisterConcrete(SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) -// cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) -// cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) -//} -// -//func init() { -// RegisterCodec(ModuleCdc) -//} +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) + cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) + cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) + cdc.RegisterConcrete(SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) + cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) + cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) + + cdc.RegisterInterface((*exported.Authorization)(nil), nil) +} + +func init() { + RegisterCodec(ModuleCdc) +} From ec1e148ee938fac25ea81fb5978cf832146f069c Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 18:49:45 +0530 Subject: [PATCH 062/162] Added static value to client --- x/msg_authorization/client/cli/tx.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index b896dc69ac87..aa208d59586d 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -54,16 +54,14 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { if err != nil { return err } + // + //bz, err := ioutil.ReadFile(args[1]) + //if err != nil { + // return err + //} - bz, err := ioutil.ReadFile(args[1]) - if err != nil { - return err - } + bz := []byte(`{"type": "cosmos-sdk/SendAuthorization","value":{"spendlimit": [{"denom": "stake","amount": "50"}]}}`) - // bz := []byte(`{ - // "type": "cosmos-sdk/SendAuthorization", - // "value":{"spendlimit": "100stake"} - //}`) var authorization types.SendAuthorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { From 97e103842e1066874196a810b5f3bc7ab955f1c3 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 3 Jan 2020 19:17:19 +0530 Subject: [PATCH 063/162] Changed authorization type --- x/msg_authorization/client/cli/tx.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index aa208d59586d..d5a8ebc7996b 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -13,6 +13,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/client/utils" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -62,7 +63,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { bz := []byte(`{"type": "cosmos-sdk/SendAuthorization","value":{"spendlimit": [{"denom": "stake","amount": "50"}]}}`) - var authorization types.SendAuthorization + var authorization exported.Authorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err From 41b392cf83f4f233a7fe97e4f056b4eeb3c81303 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 29 Jan 2020 13:49:57 +0530 Subject: [PATCH 064/162] Merge branch 'master' of github.com:cosmos/cosmos-sdk into multistore-home --- types/proto.go | 2 +- x/msg_authorization/client/cli/tx.go | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/types/proto.go b/types/proto.go index e4cdbdc511c0..b6978c1eb9a5 100644 --- a/types/proto.go +++ b/types/proto.go @@ -1,7 +1,7 @@ package types import ( - _ "github.com/gogo/protobuf/gogoproto" // nolint + _ "github.com/gogo/protobuf/gogoproto" // nolint _ "github.com/regen-network/cosmos-proto" // nolint ) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index d5a8ebc7996b..2f33f3a61279 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -6,17 +6,18 @@ import ( "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" - "github.com/cosmos/cosmos-sdk/x/auth/client/utils" + authclient "github.com/cosmos/cosmos-sdk/x/auth/client" "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "github.com/spf13/cobra" - "github.com/spf13/viper" ) // GetTxCmd returns the transaction commands for this module @@ -47,7 +48,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) - txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) + txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc)) cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) granter := cliCtx.FromAddress @@ -80,7 +81,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + return authclient.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) }, } @@ -97,7 +98,7 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) - txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) + txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc)) cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) granter := cliCtx.FromAddress @@ -122,7 +123,7 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { return err } - return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + return authclient.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) }, } return cmd @@ -136,7 +137,7 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { inBuf := bufio.NewReader(cmd.InOrStdin()) - txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) + txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(authclient.GetTxEncoder(cdc)) cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) grantee := cliCtx.FromAddress @@ -164,7 +165,7 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { return err } - return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) + return authclient.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) }, } return cmd From d09cbe5d48a8d3e154e9817609aba533c9cf5bbd Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 29 Jan 2020 18:54:55 +0530 Subject: [PATCH 065/162] Removed unused imports --- x/msg_authorization/client/rest/tx.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go index bae1e7d1a714..fa9dc3df8e89 100644 --- a/x/msg_authorization/client/rest/tx.go +++ b/x/msg_authorization/client/rest/tx.go @@ -1,15 +1,17 @@ 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" - "github.com/cosmos/cosmos-sdk/x/auth/client/utils" + authclient "github.com/cosmos/cosmos-sdk/x/auth/client" "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "github.com/gorilla/mux" - "net/http" - "time" ) func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { @@ -51,7 +53,7 @@ func grantHandler(cliCtx context.CLIContext) http.HandlerFunc { return } - utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) + authclient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) } } @@ -74,6 +76,6 @@ func revokeHandler(cliCtx context.CLIContext) http.HandlerFunc { return } - utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) + authclient.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) } } From 1f0fa9adfd208cca063b09ed04712381f9557af8 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 29 Jan 2020 19:07:30 +0530 Subject: [PATCH 066/162] Modified router for dispatch actions --- x/msg_authorization/internal/keeper/keeper.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 1d0b163daaf4..f4c4b9243103 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -52,7 +52,8 @@ func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA // DispatchActions attempts to execute the provided messages via authorization // grants from the message signer to the grantee. func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) (*sdk.Result, error) { - var res *sdk.Result + var msgResult *sdk.Result + var err error for _, msg := range msgs { signers := msg.GetSigners() if len(signers) != 1 { @@ -75,13 +76,18 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] k.update(ctx, grantee, granter, updated) } } - res, _ = k.router.Route(msg.Route())(ctx, msg) - if res != nil { - return res, nil + handler := k.router.Route(ctx, msg.Route()) + if handler == nil { + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized message route: %s", msg.Route()) + } + + msgResult, err = handler(ctx, msg) + if err != nil { + return nil, sdkerrors.Wrapf(err, "failed to execute message; message %d", msg.Type()) } } - return nil, nil + return msgResult, nil } // Grant method grants the provided authorization to the grantee on the granter's account with the provided expiration From 6838943d405ff89be5d9a1091dcb4064708c8d12 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 31 Jan 2020 19:43:33 +0530 Subject: [PATCH 067/162] Modified keeper test --- go.mod | 1 + x/msg_authorization/internal/keeper/keeper.go | 2 +- x/msg_authorization/internal/keeper/keeper_test.go | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 6ebb9fdde282..5933bf15e5fb 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 github.com/gorilla/mux v1.7.3 github.com/hashicorp/golang-lru v0.5.4 + github.com/kr/pretty v0.1.0 github.com/mattn/go-isatty v0.0.12 github.com/pelletier/go-toml v1.6.0 github.com/pkg/errors v0.9.1 diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index f4c4b9243103..ae72a863a2a1 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -83,7 +83,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] msgResult, err = handler(ctx, msg) if err != nil { - return nil, sdkerrors.Wrapf(err, "failed to execute message; message %d", msg.Type()) + return nil, sdkerrors.Wrapf(err, "failed to execute message; message %s", msg.Type()) } } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index a61682de4bbf..d43858f52061 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -49,7 +49,7 @@ func (s *TestSuite) TestKeeper() { s.Require().Nil(authorization) s.T().Log("verify if authorization is accepted") - x := types.SendAuthorization{SpendLimit: newCoins} + x = types.SendAuthorization{SpendLimit: newCoins} s.keeper.Grant(s.ctx, granteeAddr, granterAddr, x, now.Add(time.Hour)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(authorization) From 5c94ca9f6e227d39a6eea7328f41ed8ad04a98a5 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 3 Feb 2020 15:24:26 +0530 Subject: [PATCH 068/162] Added router to msg_auth --- x/msg_authorization/internal/types/router.go | 51 ++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 x/msg_authorization/internal/types/router.go diff --git a/x/msg_authorization/internal/types/router.go b/x/msg_authorization/internal/types/router.go new file mode 100644 index 000000000000..8816fc382b57 --- /dev/null +++ b/x/msg_authorization/internal/types/router.go @@ -0,0 +1,51 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var _ Router = (*router)(nil) + +// Router implements a governance Handler router. +// +// TODO: Use generic router (ref #3976). +type Router interface { + AddRoute(r string, h sdk.Handler) (rtr Router) + Seal() +} + +type router struct { + routes map[string]sdk.Handler + sealed bool +} + +// NewRouter creates a new Router interface instance +func NewRouter() Router { + return &router{ + routes: make(map[string]sdk.Handler), + } +} + +// Seal seals the router which prohibits any subsequent route handlers to be +// added. Seal will panic if called more than once. +func (rtr *router) Seal() { + if rtr.sealed { + panic("router already sealed") + } + rtr.sealed = true +} + +// AddRoute adds a governance handler for a given path. It returns the Router +// so AddRoute calls can be linked. It will panic if the router is sealed. +func (rtr *router) AddRoute(path string, h sdk.Handler) Router { + if rtr.sealed { + panic("router sealed; cannot add route handler") + } + + if !sdk.IsAlphaNumeric(path) { + panic("route expressions can only contain alphanumeric characters") + } + + rtr.routes[path] = h + return rtr +} From f37d243af2b0d2e3fe0ba7d4f59f772dd715685f Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 3 Feb 2020 15:33:08 +0530 Subject: [PATCH 069/162] Added alias to router --- x/msg_authorization/alias.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index 0afe3b8c428f..771f6e241477 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -28,6 +28,7 @@ var ( NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization NewMsgExecDelegated = types.NewMsgExecDelegated NewKeeper = keeper.NewKeeper + NewRouter = types.NewRouter // variable aliases ModuleCdc = types.ModuleCdc From 25b6c823cfdeb58ef49f1afd85eb3c6e42d2fb2d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 3 Feb 2020 15:58:27 +0530 Subject: [PATCH 070/162] Modified router type in keeper --- x/msg_authorization/internal/keeper/keeper.go | 4 ++-- x/msg_authorization/internal/types/router.go | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index ae72a863a2a1..691504bf73a9 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -15,11 +15,11 @@ import ( type Keeper struct { storeKey sdk.StoreKey cdc *codec.Codec - router sdk.Router + router types.Router } // NewKeeper constructs a message authorisation Keeper -func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keeper { +func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router types.Router) Keeper { return Keeper{ storeKey: storeKey, cdc: cdc, diff --git a/x/msg_authorization/internal/types/router.go b/x/msg_authorization/internal/types/router.go index 8816fc382b57..2dd44ed3a5fa 100644 --- a/x/msg_authorization/internal/types/router.go +++ b/x/msg_authorization/internal/types/router.go @@ -12,6 +12,7 @@ var _ Router = (*router)(nil) type Router interface { AddRoute(r string, h sdk.Handler) (rtr Router) Seal() + Route(c sdk.Context, p string) (h sdk.Handler) } type router struct { @@ -49,3 +50,7 @@ func (rtr *router) AddRoute(path string, h sdk.Handler) Router { rtr.routes[path] = h return rtr } + +func (rtr *router) Route(_ sdk.Context, path string) sdk.Handler { + return rtr.routes[path] +} From b581b7503864d5f01575b26ba711640e50a449e6 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 3 Feb 2020 16:17:49 +0530 Subject: [PATCH 071/162] Modified router type in tests --- x/msg_authorization/internal/keeper/keeper_test.go | 2 +- x/msg_authorization/internal/keeper/test_common.go | 5 ++--- x/msg_authorization/internal/types/router.go | 3 --- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index d43858f52061..14cae235fade 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -22,7 +22,7 @@ type TestSuite struct { paramsKeeper params.Keeper bankKeeper bank.Keeper keeper Keeper - router sdk.Router + router types.Router } func (s *TestSuite) SetupTest() { diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 8449cd629a07..b084c8bd03a2 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -9,7 +9,6 @@ import ( "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" @@ -32,7 +31,7 @@ func makeTestCodec() *codec.Codec { return cdc } -func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { +func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, types.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -64,7 +63,7 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) bankKeeper.SetSendEnabled(ctx, true) - router := baseapp.NewRouter() + router := types.NewRouter() router.AddRoute("bank", bank.NewHandler(bankKeeper)) authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) diff --git a/x/msg_authorization/internal/types/router.go b/x/msg_authorization/internal/types/router.go index 2dd44ed3a5fa..9e2396d4c373 100644 --- a/x/msg_authorization/internal/types/router.go +++ b/x/msg_authorization/internal/types/router.go @@ -6,9 +6,6 @@ import ( var _ Router = (*router)(nil) -// Router implements a governance Handler router. -// -// TODO: Use generic router (ref #3976). type Router interface { AddRoute(r string, h sdk.Handler) (rtr Router) Seal() From 254a13cf85e55031cf999dc3c5beaad34fe21fe1 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 3 Feb 2020 18:55:09 +0530 Subject: [PATCH 072/162] Added handler to module --- x/msg_authorization/module.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index fe75f62d1b77..3ff763ab9286 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -75,7 +75,9 @@ func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} func (AppModule) Route() string { return RouterKey } // NewHandler is empty, as we do not handle Messages (just proposals) -func (am AppModule) NewHandler() sdk.Handler { return nil } +func (am AppModule) NewHandler() sdk.Handler { + return NewHandler(am.keeper) +} // QuerierRoute returns the route we respond to for abci queries func (AppModule) QuerierRoute() string { return QuerierRoute } From 3a9bcfb87cb2c6b85ec24f548137a5b6e29bf1b4 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 3 Feb 2020 20:03:07 +0530 Subject: [PATCH 073/162] Modified key name --- x/msg_authorization/internal/types/keys.go | 2 +- x/msg_authorization/module.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go index 1d9ec87d19a5..2e9ced103542 100644 --- a/x/msg_authorization/internal/types/keys.go +++ b/x/msg_authorization/internal/types/keys.go @@ -7,7 +7,7 @@ import ( const ( // ModuleName is the module name constant used in many places - ModuleName = "msg_authorization" + ModuleName = "msgauth" // StoreKey is the store key string for msg_authorization StoreKey = ModuleName diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go index 3ff763ab9286..56b2629c5616 100644 --- a/x/msg_authorization/module.go +++ b/x/msg_authorization/module.go @@ -74,7 +74,6 @@ func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route is empty, as we do not handle Messages (just proposals) func (AppModule) Route() string { return RouterKey } -// NewHandler is empty, as we do not handle Messages (just proposals) func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } From 914711380c476b9eae1849f565df47c13e736ee9 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 4 Feb 2020 16:35:35 +0530 Subject: [PATCH 074/162] Modified default input to cli --- x/msg_authorization/client/cli/tx.go | 10 ++++------ .../internal/types/send_authorization.go | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 2f33f3a61279..2afda30e2cb4 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -56,13 +56,11 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { if err != nil { return err } - // - //bz, err := ioutil.ReadFile(args[1]) - //if err != nil { - // return err - //} - bz := []byte(`{"type": "cosmos-sdk/SendAuthorization","value":{"spendlimit": [{"denom": "stake","amount": "50"}]}}`) + bz, err := ioutil.ReadFile(args[1]) + if err != nil { + return err + } var authorization exported.Authorization err = cdc.UnmarshalJSON(bz, &authorization) diff --git a/x/msg_authorization/internal/types/send_authorization.go b/x/msg_authorization/internal/types/send_authorization.go index 2753621f11c5..146cbc0e1c7d 100644 --- a/x/msg_authorization/internal/types/send_authorization.go +++ b/x/msg_authorization/internal/types/send_authorization.go @@ -13,7 +13,7 @@ type SendAuthorization struct { // SpendLimit specifies the maximum amount of tokens that can be spent // by this authorization and will be updated as tokens are spent. If it is // empty, there is no spend limit and any amount of coins can be spent. - SpendLimit sdk.Coins + SpendLimit sdk.Coins `json:"spend_limit"` } func (authorization SendAuthorization) MsgType() sdk.Msg { From 0ff3bfd6e7967da7950056cddc44c00a946af616 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 4 Feb 2020 16:51:24 +0530 Subject: [PATCH 075/162] Modified authorization type --- x/msg_authorization/client/cli/tx.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 2afda30e2cb4..d63f5a499545 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -16,7 +16,6 @@ import ( 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/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) @@ -62,7 +61,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - var authorization exported.Authorization + var authorization types.SendAuthorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err From f6ad6c1e46689c5c257e022d3310748581e2fc57 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 4 Feb 2020 19:09:57 +0530 Subject: [PATCH 076/162] Modified authorization type --- x/msg_authorization/client/cli/tx.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index d63f5a499545..4cbe7059e990 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -61,7 +61,8 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - var authorization types.SendAuthorization + var amount sdk.Coins + authorization := &types.SendAuthorization{SpendLimit: amount} err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err From 908807d04a3ebcfab550b27dd3f555d415e0bf50 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Sat, 8 Feb 2020 23:39:32 +0530 Subject: [PATCH 077/162] Modified tests in msg_auth --- x/msg_authorization/internal/keeper/test_common.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index b084c8bd03a2..1d81fb3de4a3 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -42,6 +42,7 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base keyAcc := sdk.NewKVStoreKey(auth.StoreKey) keyParams := sdk.NewKVStoreKey(params.StoreKey) + keyBank := sdk.NewKVStoreKey(bank.StoreKey) keyAuthorization := sdk.NewKVStoreKey(types.StoreKey) tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) @@ -60,7 +61,7 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams) authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) - bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) + bankKeeper := bank.NewBaseKeeper(cdc, keyBank, authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) bankKeeper.SetSendEnabled(ctx, true) router := types.NewRouter() From d3fcf2e1e7c420bbfb1407543331cd5b4caf8778 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Sun, 9 Feb 2020 01:04:41 +0530 Subject: [PATCH 078/162] Changed timestamp in keeper to int64 --- x/msg_authorization/alias.go | 3 +-- x/msg_authorization/client/rest/tx.go | 11 +++++------ x/msg_authorization/exported/keeper.go | 5 +++-- x/msg_authorization/exported/types.go | 12 ------------ x/msg_authorization/internal/keeper/keeper.go | 19 +++++++++---------- .../internal/keeper/keeper_test.go | 19 ++++++++++--------- .../internal/keeper/test_common.go | 1 + .../internal/types/authorizations.go | 15 ++++++++++----- x/msg_authorization/internal/types/codec.go | 3 +-- .../internal/types/generic_authorization.go | 11 +++++------ x/msg_authorization/internal/types/msgs.go | 9 ++++----- .../internal/types/send_authorization.go | 10 +++++----- 12 files changed, 54 insertions(+), 64 deletions(-) delete mode 100644 x/msg_authorization/exported/types.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index 771f6e241477..acfc1943c829 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -6,7 +6,6 @@ package msg_authorization import ( - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/keeper" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) @@ -35,7 +34,7 @@ var ( ) type ( - Authorization = exported.Authorization + Authorization = types.Authorization SendAuthorization = types.SendAuthorization AuthorizationGrant = types.AuthorizationGrant GenericAuthorization = types.GenericAuthorization diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go index fa9dc3df8e89..00105ea01c7f 100644 --- a/x/msg_authorization/client/rest/tx.go +++ b/x/msg_authorization/client/rest/tx.go @@ -10,7 +10,6 @@ import ( 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/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) @@ -20,11 +19,11 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router) { } type GrantRequest struct { - BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Authorization exported.Authorization `json:"authorization"` - Expiration time.Time `json:"expiration"` + 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 { diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 73b8c6053d12..4c8cd97fd376 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,6 +1,7 @@ package exported import ( + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -12,12 +13,12 @@ type Keeper interface { // 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 Authorization, expiration time.Time) + 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 Authorization, expiration time.Time) + GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Authorization, expiration time.Time) } diff --git a/x/msg_authorization/exported/types.go b/x/msg_authorization/exported/types.go deleted file mode 100644 index 64e29a4be253..000000000000 --- a/x/msg_authorization/exported/types.go +++ /dev/null @@ -1,12 +0,0 @@ -package exported - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - - abci "github.com/tendermint/tendermint/abci/types" -) - -type Authorization interface { - MsgType() sdk.Msg - Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) -} diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 691504bf73a9..a4a0dcdfee18 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) @@ -41,8 +40,8 @@ func (k Keeper) getAuthorizationGrant(ctx sdk.Context, actor []byte) (grant type return grant, true } -func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated exported.Authorization) { - grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, updated.MsgType())) +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Authorization) { + grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, updated.Msg())) if !found { return } @@ -93,10 +92,10 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] // Grant method 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. -func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization exported.Authorization, expiration time.Time) { +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) { store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinaryBare(types.AuthorizationGrant{Authorization: authorization, Expiration: expiration}) - actor := k.getActorAuthorizationKey(grantee, granter, authorization.MsgType()) + bz := k.cdc.MustMarshalBinaryBare(types.AuthorizationGrant{Authorization: authorization, Expiration: expiration.Unix()}) + actor := k.getActorAuthorizationKey(grantee, granter, authorization.Msg()) store.Set(actor, bz) } @@ -108,14 +107,14 @@ func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA // GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time, // granted to the grantee by the granter for the provided msg type. -func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap exported.Authorization, expiration time.Time) { +func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Authorization, expiration int64) { grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, msgType)) if !found { - return nil, time.Time{} + return nil, 0 } - if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { + if grant.Expiration != 0 && grant.Expiration < (ctx.BlockHeader().Time.Unix()) { k.Revoke(ctx, grantee, granter, msgType) - return nil, time.Time{} + return nil, 0 } return grant.Authorization, grant.Expiration } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 14cae235fade..47e118c809e6 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -30,14 +30,14 @@ func (s *TestSuite) SetupTest() { } func (s *TestSuite) TestKeeper() { - err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + err := s.bankKeeper.SetBalances(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) s.Require().Nil(err) - s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + s.Require().True(s.bankKeeper.GetBalance(s.ctx, granterAddr, "steak").IsEqual(sdk.NewCoin("steak", sdk.NewInt(10000)))) s.T().Log("verify that no authorization returns nil") - authorization, _ := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, expiration := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(authorization) - //require.Nil(t, expiration) + s.Require().Zero(expiration) now := s.ctx.BlockHeader().Time s.Require().NotNil(now) @@ -53,7 +53,7 @@ func (s *TestSuite) TestKeeper() { s.keeper.Grant(s.ctx, granteeAddr, granterAddr, x, now.Add(time.Hour)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(authorization) - s.Require().Equal(authorization.MsgType(), bank.MsgSend{}) + s.Require().Equal(authorization.MsgType(), bank.MsgSend{}.Type()) s.T().Log("verify fetching authorization with wrong msg type fails") authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) @@ -78,9 +78,9 @@ func (s *TestSuite) TestKeeper() { } func (s *TestSuite) TestKeeperFees() { - err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + err := s.bankKeeper.SetBalances(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) s.Require().Nil(err) - s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + s.Require().True(s.bankKeeper.GetBalance(s.ctx, granterAddr, "steak").IsEqual(sdk.NewCoin("steak", sdk.NewInt(10000)))) now := s.ctx.BlockHeader().Time s.Require().NotNil(now) @@ -109,9 +109,10 @@ func (s *TestSuite) TestKeeperFees() { s.T().Log("verify dispatch executes with correct information") // grant authorization s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendAuthorization{SpendLimit: smallCoin}, now) - authorization, _ := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, expiration := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().NotNil(authorization) - s.Require().Equal(authorization.MsgType(), bank.MsgSend{}) + s.Require().Zero(expiration) + s.Require().Equal(authorization.MsgType(), bank.MsgSend{}.Type()) result, error = s.keeper.DispatchActions(s.ctx, granteeAddr, msgs.Msgs) s.Require().NotNil(result) s.Require().Nil(error) diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 1d81fb3de4a3..ffba8c6c7c96 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -49,6 +49,7 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyBank, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyAuthorization, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) diff --git a/x/msg_authorization/internal/types/authorizations.go b/x/msg_authorization/internal/types/authorizations.go index bce5f52181b5..63da31d4fcc3 100644 --- a/x/msg_authorization/internal/types/authorizations.go +++ b/x/msg_authorization/internal/types/authorizations.go @@ -1,13 +1,18 @@ package types import ( - "time" - - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" ) +type Authorization interface { + Msg() sdk.Msg + MsgType() string + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) +} + type AuthorizationGrant struct { - Authorization exported.Authorization + Authorization Authorization `json:"authorization"` - Expiration time.Time + Expiration int64 `json:"expiration"` } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index b2b61ef2b2c3..2a34101254e0 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -2,7 +2,6 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" ) var ModuleCdc = codec.New() @@ -15,7 +14,7 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) - cdc.RegisterInterface((*exported.Authorization)(nil), nil) + cdc.RegisterInterface((*Authorization)(nil), nil) } func init() { diff --git a/x/msg_authorization/internal/types/generic_authorization.go b/x/msg_authorization/internal/types/generic_authorization.go index bd30c66a69b6..bebf3e31bf2c 100644 --- a/x/msg_authorization/internal/types/generic_authorization.go +++ b/x/msg_authorization/internal/types/generic_authorization.go @@ -2,8 +2,6 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" - abci "github.com/tendermint/tendermint/abci/types" ) @@ -11,13 +9,14 @@ import ( // sdk.Msg type without restrictions type GenericAuthorization struct { // MsgType is the type of Msg this capability grant allows - Msg sdk.Msg + Message sdk.Msg } -func (cap GenericAuthorization) MsgType() sdk.Msg { - return cap.MsgType() +func (cap GenericAuthorization) Msg() sdk.Msg { return cap.Message } +func (cap GenericAuthorization) MsgType() string { + return cap.Message.Type() } -func (cap GenericAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated exported.Authorization, delete bool) { +func (cap GenericAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) { return true, cap, false } diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 7dddd46ca94b..423fa480eae3 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -5,20 +5,19 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" ) // MsgGrantAuthorization grants the provided authorization to the grantee on the granter's // account with the provided expiration time. type MsgGrantAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Authorization exported.Authorization `json:"authorization"` + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Authorization Authorization `json:"authorization"` // Expiration specifies the expiration time of the grant Expiration time.Time `json:"expiration"` } -func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorization exported.Authorization, expiration time.Time) MsgGrantAuthorization { +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorization Authorization, expiration time.Time) MsgGrantAuthorization { return MsgGrantAuthorization{ Granter: granter, Grantee: grantee, diff --git a/x/msg_authorization/internal/types/send_authorization.go b/x/msg_authorization/internal/types/send_authorization.go index 146cbc0e1c7d..24e249f967c4 100644 --- a/x/msg_authorization/internal/types/send_authorization.go +++ b/x/msg_authorization/internal/types/send_authorization.go @@ -4,8 +4,6 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" - "github.com/cosmos/cosmos-sdk/x/msg_authorization/exported" - abci "github.com/tendermint/tendermint/abci/types" ) @@ -16,11 +14,13 @@ type SendAuthorization struct { SpendLimit sdk.Coins `json:"spend_limit"` } -func (authorization SendAuthorization) MsgType() sdk.Msg { - return bank.MsgSend{} +func (authorization SendAuthorization) Msg() sdk.Msg { return bank.MsgSend{} } + +func (authorization SendAuthorization) MsgType() string { + return bank.MsgSend{}.Type() } -func (authorization SendAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated exported.Authorization, delete bool) { +func (authorization SendAuthorization) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) { switch msg := msg.(type) { case bank.MsgSend: limitLeft, isNegative := authorization.SpendLimit.SafeSub(msg.Amount) From 7b74e35409842c8994eb30bfa3363e52b67320b0 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 10 Feb 2020 17:51:21 +0530 Subject: [PATCH 079/162] Replaced msg with msg type in grant and revoke --- x/msg_authorization/client/cli/query.go | 2 +- x/msg_authorization/client/cli/tx.go | 14 ++--------- x/msg_authorization/internal/keeper/keeper.go | 16 ++++++------- .../internal/keeper/keeper_test.go | 24 +++++++++---------- .../internal/types/authorizations.go | 1 - .../internal/types/generic_authorization.go | 1 - x/msg_authorization/internal/types/msgs.go | 4 ++-- .../internal/types/send_authorization.go | 2 -- 8 files changed, 25 insertions(+), 39 deletions(-) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index e6bd8896e7ea..59a21663de05 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -15,7 +15,7 @@ import ( func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { authorizationQueryCmd := &cobra.Command{ Use: types.ModuleName, - Short: "Querying commands for the distribution module", + Short: "Querying commands for the msg authorization module", Long: "", DisableFlagParsing: true, SuggestionsMinimumDistance: 2, diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 4cbe7059e990..267ccce55df3 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -61,8 +61,7 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { return err } - var amount sdk.Coins - authorization := &types.SendAuthorization{SpendLimit: amount} + var authorization types.Authorization err = cdc.UnmarshalJSON(bz, &authorization) if err != nil { return err @@ -105,16 +104,7 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { return err } - bz, err := ioutil.ReadFile(args[1]) - if err != nil { - return err - } - - var msgAuthorized sdk.Msg - err = cdc.UnmarshalJSON(bz, &msgAuthorized) - if err != nil { - return err - } + msgAuthorized := args[1] msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) if err := msg.ValidateBasic(); err != nil { diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index a4a0dcdfee18..e8b8e5042e81 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -26,8 +26,8 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router types.Router) Kee } } -func (k Keeper) getActorAuthorizationKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { - return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +func (k Keeper) getActorAuthorizationKey(grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s", grantee, granter, msgType)) } func (k Keeper) getAuthorizationGrant(ctx sdk.Context, actor []byte) (grant types.AuthorizationGrant, found bool) { @@ -41,7 +41,7 @@ func (k Keeper) getAuthorizationGrant(ctx sdk.Context, actor []byte) (grant type } func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Authorization) { - grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, updated.Msg())) + grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, updated.MsgType())) if !found { return } @@ -60,7 +60,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] } granter := signers[0] if !bytes.Equal(granter, grantee) { - authorization, _ := k.GetAuthorization(ctx, grantee, granter, msg) + authorization, _ := k.GetAuthorization(ctx, grantee, granter, msg.Type()) if authorization == nil { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } @@ -70,7 +70,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } if del { - k.Revoke(ctx, grantee, granter, msg) + k.Revoke(ctx, grantee, granter, msg.Type()) } else if updated != nil { k.update(ctx, grantee, granter, updated) } @@ -95,19 +95,19 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshalBinaryBare(types.AuthorizationGrant{Authorization: authorization, Expiration: expiration.Unix()}) - actor := k.getActorAuthorizationKey(grantee, granter, authorization.Msg()) + actor := k.getActorAuthorizationKey(grantee, granter, authorization.MsgType()) store.Set(actor, bz) } // Revoke method revokes any authorization for the provided message type granted to the grantee by the granter. -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) { store := ctx.KVStore(k.storeKey) store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType)) } // GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time, // granted to the grantee by the granter for the provided msg type. -func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Authorization, expiration int64) { +func (k Keeper) GetAuthorization(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) (cap types.Authorization, expiration int64) { grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, msgType)) if !found { return nil, 0 diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 47e118c809e6..e5fd66bfb10c 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -35,7 +35,7 @@ func (s *TestSuite) TestKeeper() { s.Require().True(s.bankKeeper.GetBalance(s.ctx, granterAddr, "steak").IsEqual(sdk.NewCoin("steak", sdk.NewInt(10000)))) s.T().Log("verify that no authorization returns nil") - authorization, expiration := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, expiration := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().Nil(authorization) s.Require().Zero(expiration) now := s.ctx.BlockHeader().Time @@ -45,34 +45,34 @@ func (s *TestSuite) TestKeeper() { s.T().Log("verify if expired authorization is rejected") x := types.SendAuthorization{SpendLimit: newCoins} s.keeper.Grant(s.ctx, granterAddr, granteeAddr, x, now.Add(-1*time.Hour)) - authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().Nil(authorization) s.T().Log("verify if authorization is accepted") x = types.SendAuthorization{SpendLimit: newCoins} s.keeper.Grant(s.ctx, granteeAddr, granterAddr, x, now.Add(time.Hour)) - authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) s.Require().Equal(authorization.MsgType(), bank.MsgSend{}.Type()) s.T().Log("verify fetching authorization with wrong msg type fails") - authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}.Type()) s.Require().Nil(authorization) s.T().Log("verify fetching authorization with wrong grantee fails") - authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}.Type()) s.Require().Nil(authorization) s.T().Log("") s.T().Log("verify revoke fails with wrong information") - s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}.Type()) + authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().Nil(authorization) s.T().Log("verify revoke executes with correct information") - s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) - authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}.Type()) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) } @@ -109,7 +109,7 @@ func (s *TestSuite) TestKeeperFees() { s.T().Log("verify dispatch executes with correct information") // grant authorization s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendAuthorization{SpendLimit: smallCoin}, now) - authorization, expiration := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, expiration := s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) s.Require().Zero(expiration) s.Require().Equal(authorization.MsgType(), bank.MsgSend{}.Type()) @@ -118,7 +118,7 @@ func (s *TestSuite) TestKeeperFees() { s.Require().Nil(error) fmt.Printf("%# v", pretty.Formatter(result)) - authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) s.T().Log("verify dispatch fails with overlimit") @@ -140,7 +140,7 @@ func (s *TestSuite) TestKeeperFees() { s.Require().NotNil(error) fmt.Printf("%# v", pretty.Formatter(error)) - authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) } diff --git a/x/msg_authorization/internal/types/authorizations.go b/x/msg_authorization/internal/types/authorizations.go index 63da31d4fcc3..990dff10e41a 100644 --- a/x/msg_authorization/internal/types/authorizations.go +++ b/x/msg_authorization/internal/types/authorizations.go @@ -6,7 +6,6 @@ import ( ) type Authorization interface { - Msg() sdk.Msg MsgType() string Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Authorization, delete bool) } diff --git a/x/msg_authorization/internal/types/generic_authorization.go b/x/msg_authorization/internal/types/generic_authorization.go index bebf3e31bf2c..97da3073e248 100644 --- a/x/msg_authorization/internal/types/generic_authorization.go +++ b/x/msg_authorization/internal/types/generic_authorization.go @@ -12,7 +12,6 @@ type GenericAuthorization struct { Message sdk.Msg } -func (cap GenericAuthorization) Msg() sdk.Msg { return cap.Message } func (cap GenericAuthorization) MsgType() string { return cap.Message.Type() } diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 423fa480eae3..cec8b6c50578 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -59,10 +59,10 @@ type MsgRevokeAuthorization struct { Grantee sdk.AccAddress `json:"grantee"` // AuthorizationMsgType is the type of sdk.Msg that the revoked Authorization refers to. // i.e. this is what `Authorization.MsgType()` returns - AuthorizationMsgType sdk.Msg `json:"authorization_msg_type"` + AuthorizationMsgType string `json:"authorization_msg_type"` } -func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorizationMsgType sdk.Msg) MsgRevokeAuthorization { +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, authorizationMsgType string) MsgRevokeAuthorization { return MsgRevokeAuthorization{ Granter: granter, Grantee: grantee, diff --git a/x/msg_authorization/internal/types/send_authorization.go b/x/msg_authorization/internal/types/send_authorization.go index 24e249f967c4..ec898c61087d 100644 --- a/x/msg_authorization/internal/types/send_authorization.go +++ b/x/msg_authorization/internal/types/send_authorization.go @@ -14,8 +14,6 @@ type SendAuthorization struct { SpendLimit sdk.Coins `json:"spend_limit"` } -func (authorization SendAuthorization) Msg() sdk.Msg { return bank.MsgSend{} } - func (authorization SendAuthorization) MsgType() string { return bank.MsgSend{}.Type() } From 58389acb75ddb6ee894367fc763ded0b5ed98975 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 10 Feb 2020 18:00:25 +0530 Subject: [PATCH 080/162] Modified msg type in rest --- x/msg_authorization/client/rest/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go index 00105ea01c7f..9ea3fc699eaa 100644 --- a/x/msg_authorization/client/rest/tx.go +++ b/x/msg_authorization/client/rest/tx.go @@ -30,7 +30,7 @@ type RevokeRequest struct { BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"` Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` - AuthorizationMsgType sdk.Msg `json:"authorization_msg_type"` + AuthorizationMsgType string `json:"authorization_msg_type"` } func grantHandler(cliCtx context.CLIContext) http.HandlerFunc { From 100254052e6d82da677a5f734dce96301a093792 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 10 Feb 2020 18:37:48 +0530 Subject: [PATCH 081/162] Modified msg type in keys --- x/msg_authorization/internal/types/keys.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go index 2e9ced103542..3d28db0b0bf8 100644 --- a/x/msg_authorization/internal/types/keys.go +++ b/x/msg_authorization/internal/types/keys.go @@ -19,6 +19,6 @@ const ( QuerierRoute = ModuleName ) -func GetActorAuthorizationKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { - return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +func GetActorAuthorizationKey(grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s", grantee, granter, msgType)) } From 904d6d54052eee62098206dc80180d83dc3a6bc9 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 10 Feb 2020 18:53:38 +0530 Subject: [PATCH 082/162] Modified args in query cli --- x/msg_authorization/client/cli/query.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index 59a21663de05..86002a845e61 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -50,11 +50,7 @@ func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command return err } - var msgAuthorized sdk.Msg - err = cdc.UnmarshalJSON([]byte(args[2]), &msgAuthorized) - if err != nil { - return err - } + msgAuthorized := args[2] res, _, err := cliCtx.QueryStore(types.GetActorAuthorizationKey(granteeAddr, granterAddr, msgAuthorized), storeName) if err != nil { From 85cba43e4ab139b4aef8696e1d192a9ed7a88493 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 12:10:37 +0530 Subject: [PATCH 083/162] Added more events to handler --- x/msg_authorization/handler.go | 34 ++++++++++++++++--- x/msg_authorization/internal/keeper/keeper.go | 6 ++-- x/msg_authorization/internal/types/events.go | 1 + 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index ec63daba7eea..61dc14582b39 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -3,7 +3,7 @@ package msg_authorization import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/x/distribution/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" ) func NewHandler(k Keeper) sdk.Handler { @@ -23,30 +23,54 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) (*sdk.Result, error) { - k.Grant(ctx, msg.Grantee, msg.Granter, msg.Authorization, msg.Expiration) + granted := k.Grant(ctx, msg.Grantee, msg.Granter, msg.Authorization, msg.Expiration) ctx.EventManager().EmitEvent( sdk.NewEvent( sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), + sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), ), ) + if granted { + ctx.EventManager().EmitEvent( + sdk.NewEvent( + types.EventGrantAuthorization, + sdk.NewAttribute(types.AttributeGranttype, msg.Authorization.MsgType()), + sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), + sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), + ), + ) + } + return &sdk.Result{Events: ctx.EventManager().Events()}, nil } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) (*sdk.Result, error) { - k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) + revoked := k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) ctx.EventManager().EmitEvent( sdk.NewEvent( sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), - sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), + sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), ), ) + if revoked { + ctx.EventManager().EmitEvent( + sdk.NewEvent( + types.EventGrantAuthorization, + sdk.NewAttribute(types.AttributeGranttype, msg.AuthorizationMsgType), + sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), + sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), + ), + ) + } + return &sdk.Result{Events: ctx.EventManager().Events()}, nil } diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index e8b8e5042e81..c6f8e2bea6a2 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -92,17 +92,19 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] // Grant method 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. -func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) { +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) bool { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshalBinaryBare(types.AuthorizationGrant{Authorization: authorization, Expiration: expiration.Unix()}) actor := k.getActorAuthorizationKey(grantee, granter, authorization.MsgType()) store.Set(actor, bz) + return true } // Revoke method revokes any authorization for the provided message type granted to the grantee by the granter. -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) bool { store := ctx.KVStore(k.storeKey) store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType)) + return true } // GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time, diff --git a/x/msg_authorization/internal/types/events.go b/x/msg_authorization/internal/types/events.go index 823ad3ce8cc5..f0c13d2debbb 100644 --- a/x/msg_authorization/internal/types/events.go +++ b/x/msg_authorization/internal/types/events.go @@ -6,6 +6,7 @@ const ( EventRevokeAuthorization = "revoke-authorization" EventExecuteAuthorization = "execute-authorization" + AttributeGranttype = "grant-type" AttributeKeyGranteeAddress = "grantee" AttributeKeyGranterAddress = "granter" From c553f4b8a3c1460f377c6c207b088fb70c3737f0 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 15:10:16 +0530 Subject: [PATCH 084/162] Fixed query authorization --- x/msg_authorization/client/cli/query.go | 2 +- x/msg_authorization/client/cli/tx.go | 15 +++------------ 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index 86002a845e61..e6275eb87402 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -62,7 +62,7 @@ func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command } var grant types.AuthorizationGrant - cdc.MustUnmarshalJSON(res, grant) + cdc.MustUnmarshalJSON(res, &grant) return cliCtx.PrintOutput(grant) }, diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 267ccce55df3..9dbdd9067bbd 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -119,7 +119,7 @@ func GetCmdRevokeAuthorization(cdc *codec.Codec) *cobra.Command { func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "send-as [granter] [msg_tx_json] --from [grantee]", + 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), @@ -130,24 +130,15 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { grantee := cliCtx.FromAddress - // TODO cleanup this - //granter, err := sdk.AccAddressFromBech32(args[0]) - //if err != nil { - // return err - //} - - // TODO interactive should look good, consider second arg as optional? - //generatedTx, err := input.GetString("Enter generated tx json string:", inBuf) - var stdTx auth.StdTx - err := cdc.UnmarshalJSON([]byte(args[1]), &stdTx) + bz := []byte(args[1]) + err := cdc.UnmarshalJSON(bz, &stdTx) if err != nil { return err } msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs) - // TODO include the granter as delegated signer in the encoded JSON if err := msg.ValidateBasic(); err != nil { return err From e9bb4267492b079347ef9b3d96ebe2d506188880 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 15:19:56 +0530 Subject: [PATCH 085/162] Fixed unmarshal in query authorization --- x/msg_authorization/client/cli/query.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index e6275eb87402..ba376ae90a5f 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -62,7 +62,10 @@ func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command } var grant types.AuthorizationGrant - cdc.MustUnmarshalJSON(res, &grant) + err = cdc.UnmarshalBinaryBare(res, &grant) + if err != nil { + return err + } return cliCtx.PrintOutput(grant) }, From a642f66cc28fd01c5a1b2d0abf541745a54e515a Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 15:52:00 +0530 Subject: [PATCH 086/162] Cleaned sendas tx --- x/msg_authorization/client/cli/tx.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 9dbdd9067bbd..596104bc493e 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -131,9 +131,12 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { grantee := cliCtx.FromAddress var stdTx auth.StdTx + bz, err := ioutil.ReadFile(args[1]) + if err != nil { + return err + } - bz := []byte(args[1]) - err := cdc.UnmarshalJSON(bz, &stdTx) + err = cdc.UnmarshalJSON(bz, &stdTx) if err != nil { return err } From b2c374394bf49c9980d06f66179528c20ac1fd6e Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 16:38:57 +0530 Subject: [PATCH 087/162] Added debugging comments --- x/msg_authorization/internal/keeper/keeper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index c6f8e2bea6a2..c8b7afe11c98 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -54,6 +54,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] var msgResult *sdk.Result var err error for _, msg := range msgs { + fmt.Printf("this is %v", msg) signers := msg.GetSigners() if len(signers) != 1 { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "authorization can be given to msg with only one signer") From ce5f7b2e10ff086cff9374459f65c162ec2f7553 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 17:12:34 +0530 Subject: [PATCH 088/162] Added debugging comments --- x/msg_authorization/client/cli/tx.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 596104bc493e..ebb06cda63c8 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -141,7 +141,10 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { return err } + fmt.Printf("this is stdTx: %v", stdTx) + msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs) + fmt.Printf("this is msgs: %v", stdTx.Msgs) if err := msg.ValidateBasic(); err != nil { return err From 7c3ab1ece8b612cc4d8ab9cdda40a23678f11ff5 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 18:20:36 +0530 Subject: [PATCH 089/162] Added debugging comments --- x/msg_authorization/internal/types/msgs.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index cec8b6c50578..eb1dff17395e 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -97,13 +97,13 @@ func (msg MsgRevokeAuthorization) ValidateBasic() error { // one signer corresponding to the granter of the authorization. type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` - Msgs []sdk.Msg `json:"msg"` + Msgs []sdk.Msg `json:"msgs"` } -func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { +func NewMsgExecDelegated(grantee sdk.AccAddress, msg []sdk.Msg) MsgExecDelegated { return MsgExecDelegated{ Grantee: grantee, - Msgs: msgs, + Msgs: msg, } } From 075fbb7fe014fa2f0df33cbaf61033fa68f66bc5 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 19:35:36 +0530 Subject: [PATCH 090/162] Did code cleanup in keeper --- x/msg_authorization/client/cli/tx.go | 3 -- x/msg_authorization/handler.go | 35 +++++-------------- x/msg_authorization/internal/keeper/keeper.go | 9 +++-- x/msg_authorization/internal/types/events.go | 2 +- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index ebb06cda63c8..596104bc493e 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -141,10 +141,7 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { return err } - fmt.Printf("this is stdTx: %v", stdTx) - msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs) - fmt.Printf("this is msgs: %v", stdTx.Msgs) if err := msg.ValidateBasic(); err != nil { return err diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 61dc14582b39..a91ee2de64a5 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -23,54 +23,37 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) (*sdk.Result, error) { - granted := k.Grant(ctx, msg.Grantee, msg.Granter, msg.Authorization, msg.Expiration) + k.Grant(ctx, msg.Grantee, msg.Granter, msg.Authorization, msg.Expiration) ctx.EventManager().EmitEvent( sdk.NewEvent( - sdk.EventTypeMessage, + types.EventGrantAuthorization, + sdk.NewAttribute(types.AttributeKeyGrantType, msg.Authorization.MsgType()), sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), ), ) - if granted { - ctx.EventManager().EmitEvent( - sdk.NewEvent( - types.EventGrantAuthorization, - sdk.NewAttribute(types.AttributeGranttype, msg.Authorization.MsgType()), - sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), - sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), - ), - ) - } - return &sdk.Result{Events: ctx.EventManager().Events()}, nil } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) (*sdk.Result, error) { - revoked := k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) + _, err := k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) + if err != nil { + return nil, err + } ctx.EventManager().EmitEvent( sdk.NewEvent( - sdk.EventTypeMessage, + types.EventRevokeAuthorization, sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(types.AttributeKeyGrantType, msg.AuthorizationMsgType), sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), ), ) - if revoked { - ctx.EventManager().EmitEvent( - sdk.NewEvent( - types.EventGrantAuthorization, - sdk.NewAttribute(types.AttributeGranttype, msg.AuthorizationMsgType), - sdk.NewAttribute(types.AttributeKeyGranterAddress, msg.Granter.String()), - sdk.NewAttribute(types.AttributeKeyGranteeAddress, msg.Grantee.String()), - ), - ) - } - return &sdk.Result{Events: ctx.EventManager().Events()}, nil } diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index c8b7afe11c98..66dbef303fa6 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -54,7 +54,6 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] var msgResult *sdk.Result var err error for _, msg := range msgs { - fmt.Printf("this is %v", msg) signers := msg.GetSigners() if len(signers) != 1 { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "authorization can be given to msg with only one signer") @@ -102,10 +101,14 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd } // Revoke method revokes any authorization for the provided message type granted to the grantee by the granter. -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) bool { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) (bool, error) { store := ctx.KVStore(k.storeKey) + authorization, _ := k.GetAuthorization(ctx, grantee, granter, msgType) + if authorization == nil { + return false, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") + } store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType)) - return true + return true, nil } // GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time, diff --git a/x/msg_authorization/internal/types/events.go b/x/msg_authorization/internal/types/events.go index f0c13d2debbb..4e89919ec067 100644 --- a/x/msg_authorization/internal/types/events.go +++ b/x/msg_authorization/internal/types/events.go @@ -6,7 +6,7 @@ const ( EventRevokeAuthorization = "revoke-authorization" EventExecuteAuthorization = "execute-authorization" - AttributeGranttype = "grant-type" + AttributeKeyGrantType = "grant-type" AttributeKeyGranteeAddress = "grantee" AttributeKeyGranterAddress = "granter" From 5ed3e84b7fa7c2aad858a075c9b18a364600d18e Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 19:37:47 +0530 Subject: [PATCH 091/162] Removed bool from grant --- x/msg_authorization/handler.go | 2 +- x/msg_authorization/internal/keeper/keeper.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index a91ee2de64a5..4fa893ee197f 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -39,7 +39,7 @@ func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k K } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) (*sdk.Result, error) { - _, err := k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) + err := k.Revoke(ctx, msg.Grantee, msg.Granter, msg.AuthorizationMsgType) if err != nil { return nil, err } diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 66dbef303fa6..5e1d25b1f269 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -92,23 +92,22 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] // Grant method 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. -func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) bool { +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, authorization types.Authorization, expiration time.Time) { store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshalBinaryBare(types.AuthorizationGrant{Authorization: authorization, Expiration: expiration.Unix()}) actor := k.getActorAuthorizationKey(grantee, granter, authorization.MsgType()) store.Set(actor, bz) - return true } // Revoke method revokes any authorization for the provided message type granted to the grantee by the granter. -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) (bool, error) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) error { store := ctx.KVStore(k.storeKey) authorization, _ := k.GetAuthorization(ctx, grantee, granter, msgType) if authorization == nil { - return false, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") + return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType)) - return true, nil + return nil } // GetAuthorization Returns any `Authorization` (or `nil`), with the expiration time, From 6456750ea5226ee82a9b65c2c69982930a30a906 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 11 Feb 2020 20:23:08 +0530 Subject: [PATCH 092/162] Did code cleanup --- x/msg_authorization/alias.go | 4 ++-- x/msg_authorization/client/cli/query.go | 4 ++-- x/msg_authorization/client/cli/tx.go | 4 +--- x/msg_authorization/internal/keeper/keeper.go | 4 ++-- .../internal/keeper/keeper_test.go | 4 ++-- x/msg_authorization/internal/types/codec.go | 2 +- x/msg_authorization/internal/types/msgs.go | 18 +++++++++--------- 7 files changed, 19 insertions(+), 21 deletions(-) diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index acfc1943c829..debb936678e8 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -25,7 +25,7 @@ var ( ErrInvalidExpirationTime = types.ErrInvalidExpirationTime NewMsgGrantAuthorization = types.NewMsgGrantAuthorization NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization - NewMsgExecDelegated = types.NewMsgExecDelegated + NewMsgExecDelegated = types.NewMsgExecAuthorized NewKeeper = keeper.NewKeeper NewRouter = types.NewRouter @@ -40,6 +40,6 @@ type ( GenericAuthorization = types.GenericAuthorization MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization - MsgExecDelegated = types.MsgExecDelegated + MsgExecDelegated = types.MsgExecAuthorized Keeper = keeper.Keeper ) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index ba376ae90a5f..9d7fc10f0389 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -35,8 +35,8 @@ func GetCmdQueryAuthorization(storeName string, cdc *codec.Codec) *cobra.Command return &cobra.Command{ Use: "authorization", Args: cobra.ExactArgs(3), - Short: "query authorzation for a granter-grantee pair", - Long: "query authorzation for a granter-grantee pair", + 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) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 596104bc493e..82f6e1a93ac1 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,7 +2,6 @@ package cli import ( "bufio" - "fmt" "io/ioutil" "time" @@ -66,7 +65,6 @@ func GetCmdGrantAuthorization(cdc *codec.Codec) *cobra.Command { if err != nil { return err } - fmt.Printf("this is authorization %v", authorization) expirationString := viper.GetString(FlagExpiration) expiration, err := time.Parse(time.RFC3339, expirationString) if err != nil { @@ -141,7 +139,7 @@ func GetCmdSendAs(cdc *codec.Codec) *cobra.Command { return err } - msg := types.NewMsgExecDelegated(grantee, stdTx.Msgs) + msg := types.NewMsgExecAuthorized(grantee, stdTx.Msgs) if err := msg.ValidateBasic(); err != nil { return err diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 5e1d25b1f269..6c5e8aff5347 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -102,8 +102,8 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd // Revoke method revokes any authorization for the provided message type granted to the grantee by the granter. func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) error { store := ctx.KVStore(k.storeKey) - authorization, _ := k.GetAuthorization(ctx, grantee, granter, msgType) - if authorization == nil { + _, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, msgType)) + if !found { return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType)) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index e5fd66bfb10c..8e4ceb6451d1 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -89,7 +89,7 @@ func (s *TestSuite) TestKeeperFees() { someCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 123)) //lotCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 4567)) - msgs := types.MsgExecDelegated{ + msgs := types.MsgExecAuthorized{ Grantee: granteeAddr, Msgs: []sdk.Msg{ bank.MsgSend{ @@ -124,7 +124,7 @@ func (s *TestSuite) TestKeeperFees() { s.T().Log("verify dispatch fails with overlimit") // grant authorization - msgs = types.MsgExecDelegated{ + msgs = types.MsgExecAuthorized{ Grantee: granteeAddr, Msgs: []sdk.Msg{ bank.MsgSend{ diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index 2a34101254e0..01af4e7add7f 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -9,7 +9,7 @@ var ModuleCdc = codec.New() func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) - cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) + cdc.RegisterConcrete(MsgExecAuthorized{}, "cosmos-sdk/ExecDelegated", nil) cdc.RegisterConcrete(SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) cdc.RegisterConcrete(AuthorizationGrant{}, "cosmos-sdk/AuthorizationGrant", nil) cdc.RegisterConcrete(GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index eb1dff17395e..60b8b1d2d058 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -92,34 +92,34 @@ func (msg MsgRevokeAuthorization) ValidateBasic() error { return nil } -// MsgExecDelegated attempts to execute the provided messages using +// MsgExecAuthorized attempts to execute the provided messages using // authorizations granted to the grantee. Each message should have only // one signer corresponding to the granter of the authorization. -type MsgExecDelegated struct { +type MsgExecAuthorized struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msgs"` } -func NewMsgExecDelegated(grantee sdk.AccAddress, msg []sdk.Msg) MsgExecDelegated { - return MsgExecDelegated{ +func NewMsgExecAuthorized(grantee sdk.AccAddress, msg []sdk.Msg) MsgExecAuthorized { + return MsgExecAuthorized{ Grantee: grantee, Msgs: msg, } } -func (msg MsgExecDelegated) Route() string { return RouterKey } -func (msg MsgExecDelegated) Type() string { return "exec_delegated" } +func (msg MsgExecAuthorized) Route() string { return RouterKey } +func (msg MsgExecAuthorized) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { +func (msg MsgExecAuthorized) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Grantee} } -func (msg MsgExecDelegated) GetSignBytes() []byte { +func (msg MsgExecAuthorized) GetSignBytes() []byte { bz := ModuleCdc.MustMarshalJSON(msg) return sdk.MustSortJSON(bz) } -func (msg MsgExecDelegated) ValidateBasic() error { +func (msg MsgExecAuthorized) ValidateBasic() error { if msg.Grantee.Empty() { return sdkerrors.Wrap(ErrInvalidGranter, "missing grantee address") } From 175f72e85d009b83c4e4fa57ba5f2516c24b54fa Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 093/162] Added code skeleton for msg authorization --- x/msg_authorization/client/cli/query.go | 2 ++ x/msg_authorization/client/cli/tx.go | 36 +++++++++++++++++++ x/msg_authorization/client/rest/query.go | 1 + x/msg_authorization/client/rest/tx.go | 1 + x/msg_authorization/exported/keeper.go | 23 ++++++++++++ x/msg_authorization/handler.go | 1 + x/msg_authorization/internal/keeper/keeper.go | 21 +++++++++++ .../internal/types/capabilities.go | 1 + x/msg_authorization/internal/types/keys.go | 15 ++++++++ x/msg_authorization/internal/types/msg.go | 34 ++++++++++++++++++ x/msg_authorization/module.go | 1 + 11 files changed, 136 insertions(+) create mode 100644 x/msg_authorization/client/cli/query.go create mode 100644 x/msg_authorization/client/cli/tx.go create mode 100644 x/msg_authorization/client/rest/query.go create mode 100644 x/msg_authorization/client/rest/tx.go create mode 100644 x/msg_authorization/exported/keeper.go create mode 100644 x/msg_authorization/handler.go create mode 100644 x/msg_authorization/internal/keeper/keeper.go create mode 100644 x/msg_authorization/internal/types/capabilities.go create mode 100644 x/msg_authorization/internal/types/keys.go create mode 100644 x/msg_authorization/internal/types/msg.go create mode 100644 x/msg_authorization/module.go diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go new file mode 100644 index 000000000000..c56a80569b50 --- /dev/null +++ b/x/msg_authorization/client/cli/query.go @@ -0,0 +1,2 @@ +package cli + diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go new file mode 100644 index 000000000000..b6e782d44f35 --- /dev/null +++ b/x/msg_authorization/client/cli/tx.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + AuthorizationTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Authorization transactions subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + AuthorizationTxCmd.AddCommand(client.PostCommands( + GetCmdGrantCapability(cdc), + GetCmdRevokeCapability(cdc), + )...) + + return AuthorizationTxCmd +} + +func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} + +func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} \ No newline at end of file diff --git a/x/msg_authorization/client/rest/query.go b/x/msg_authorization/client/rest/query.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/query.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/tx.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go new file mode 100644 index 000000000000..3ca0e9d31005 --- /dev/null +++ b/x/msg_authorization/exported/keeper.go @@ -0,0 +1,23 @@ +package exported + +import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" + +) + +type Keeper interface { + //DispatchActions executes the provided messages via capability grants from the message signer to the grantee + DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result + + // Grants the provided capability to the grantee on the granter's account with the provided expiration time + // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + + //Revokes any capability 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 Capability (or nil), with the expiration time, + // granted to the grantee by the granter for the provided msg type. + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) +} diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/handler.go @@ -0,0 +1 @@ +package msg_authorization diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go new file mode 100644 index 000000000000..8731cf48790c --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -0,0 +1,21 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + router sdk.Router +} + +// NewKeeper constructs a message authorisation Keeper +func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keeper { + return Keeper{ + storeKey: storeKey, + cdc: cdc, + router: router, + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go new file mode 100644 index 000000000000..ab1254f4c2be --- /dev/null +++ b/x/msg_authorization/internal/types/capabilities.go @@ -0,0 +1 @@ +package types diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go new file mode 100644 index 000000000000..8d327e683e07 --- /dev/null +++ b/x/msg_authorization/internal/types/keys.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the module name constant used in many places + ModuleName = "msg_authorization" + + // StoreKey is the store key string for msg_authorization + StoreKey = ModuleName + + // RouterKey is the message route for msg_authorization + RouterKey = ModuleName + + // QuerierRoute is the querier route for msg_authorization + QuerierRoute = ModuleName +) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/module.go @@ -0,0 +1 @@ +package msg_authorization From d113c3ecf68f61374a78c797327f364f85485470 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 094/162] Implemented sdk.msg methods --- x/msg_authorization/alias.go | 25 ++++++ x/msg_authorization/client/cli/query.go | 1 - x/msg_authorization/client/cli/tx.go | 4 +- x/msg_authorization/exported/keeper.go | 8 +- x/msg_authorization/handler.go | 14 ++++ .../internal/types/capabilities.go | 4 + x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++- 7 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 x/msg_authorization/alias.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go new file mode 100644 index 000000000000..bf996e6bff6d --- /dev/null +++ b/x/msg_authorization/alias.go @@ -0,0 +1,25 @@ +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 ( + NewKeeper = keeper.NewKeeper +) + +type ( + Keeper = keeper.Keeper + Capability = types.Capability + MsgGrantAuthorization = types.MsgGrantAuthorization + MsgRevokeAuthorization = types.MsgRevokeAuthorization + MsgExecDelegated = types.MsgExecDelegated +) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index c56a80569b50..7f1e458cd3ab 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -1,2 +1 @@ package cli - diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index b6e782d44f35..47244a825235 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -3,8 +3,8 @@ package cli import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/spf13/cobra" ) // GetTxCmd returns the transaction commands for this module @@ -33,4 +33,4 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { //TODO return nil -} \ No newline at end of file +} diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 3ca0e9d31005..ead70b2428ac 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,9 @@ package exported import ( - "time" sdk "github.com/cosmos/cosmos-sdk/types" - + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper interface { @@ -12,12 +12,12 @@ type Keeper interface { // Grants the provided capability to the grantee on the granter's account with the provided expiration time // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. - Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) //Revokes any capability 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 Capability (or nil), with the expiration time, // granted to the grantee by the granter for the provided msg type. - GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) } diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index d552162c4c47..e8fb5a08a29d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1 +1,15 @@ package msg_authorization + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + + switch msg := msg.(type) { + //TODO + } + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index ab1254f4c2be..17d0d6992f96 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1 +1,5 @@ package types + +type Capability interface { + //TODO +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From 6942f1c785b469246b8aff6e2694df86ce1a7c6a Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 095/162] Created handler for authorization --- x/msg_authorization/handler.go | 28 +++++++++++++++++-- x/msg_authorization/internal/keeper/keeper.go | 2 ++ x/msg_authorization/internal/types/codec.go | 9 ++++++ x/msg_authorization/internal/types/msg.go | 9 ++++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 x/msg_authorization/internal/types/codec.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index e8fb5a08a29d..9684b5c01187 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1,15 +1,39 @@ package msg_authorization import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) func NewHandler(k Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { ctx = ctx.WithEventManager(sdk.NewEventManager()) - switch msg := msg.(type) { - //TODO + case MsgGrantAuthorization: + return handleMsgGrantAuthorization(ctx, msg, k) + case MsgRevokeAuthorization: + return handleMsgRevokeAuthorization(ctx, msg, k) + case MsgExecDelegated: + return handleMsgExecDelegated(ctx, msg, k) + default: + errMsg := fmt.Sprintf("unrecognized authorization message type: %T", msg) + return sdk.ErrUnknownRequest(errMsg).Result() } } } + +func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 8731cf48790c..a7b70c5d1f03 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -19,3 +19,5 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe router: router, } } + +//TODO implement all keeper methods diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go new file mode 100644 index 000000000000..fe10042d1583 --- /dev/null +++ b/x/msg_authorization/internal/types/codec.go @@ -0,0 +1,9 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +var ModuleCdc = codec.New() + +//ToDO register concrete diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From 7abde97f837b4a2586ff3e16eeb7d1df1c785aff Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 096/162] Added validate basic to all messages --- .../internal/types/capabilities.go | 8 +++- x/msg_authorization/internal/types/errors.go | 27 ++++++++++++ .../internal/types/{msg.go => msgs.go} | 43 ++++++++++++------- 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/internal/types/errors.go rename x/msg_authorization/internal/types/{msg.go => msgs.go} (78%) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 17d0d6992f96..3806f3a66684 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,5 +1,11 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + type Capability interface { - //TODO + MsgType() sdk.Msg + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } diff --git a/x/msg_authorization/internal/types/errors.go b/x/msg_authorization/internal/types/errors.go new file mode 100644 index 000000000000..7e685bba677c --- /dev/null +++ b/x/msg_authorization/internal/types/errors.go @@ -0,0 +1,27 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type CodeType = sdk.CodeType + +const ( + // Default slashing codespace + DefaultCodespace sdk.CodespaceType = ModuleName + + CodeInvalidGranter CodeType = 101 + CodeInvalidGrantee CodeType = 102 + CodeInvalidExpirationTime CodeType = 103 +) + +func ErrInvalidGranter(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGranter, "invalid granter address") +} + +func ErrInvalidGrantee(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGrantee, "invalid grantee address") +} +func ErrInvalidExpirationTime(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidExpirationTime, "expiration time of authorization should be more than current time") +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msgs.go similarity index 78% rename from x/msg_authorization/internal/types/msg.go rename to x/msg_authorization/internal/types/msgs.go index ce6d370b3a9b..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -28,17 +28,25 @@ func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return ErrInvalidGranter(DefaultCodespace) + } + if msg.Grantee.Empty() { + return ErrInvalidGrantee(DefaultCodespace) + } + if msg.Expiration.Unix() < time.Now().Unix() { + return ErrInvalidExpirationTime(DefaultCodespace) + } + return nil } @@ -64,17 +72,21 @@ func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return sdk.ErrInvalidAddress(msg.Granter.String()) + } + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } @@ -96,16 +108,17 @@ func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Grantee} } func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } From 4c155f1a603881520ba79f68489c51dc87ae97aa Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:23:17 +0530 Subject: [PATCH 097/162] Added base for internal keeper --- x/msg_authorization/internal/keeper/keeper.go | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index a7b70c5d1f03..750f747684d3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -3,6 +3,8 @@ package keeper import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper struct { @@ -20,4 +22,20 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } -//TODO implement all keeper methods +func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { + //TODO + return sdk.Result{} +} + +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { + //TODO +} + +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { + //TODO +} + +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { + //TODO + return nil, time.Now() +} From e2828d2d231e963e21638dcc8b1592b43e19562c Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:33:25 +0530 Subject: [PATCH 098/162] Added message registration to codec --- x/msg_authorization/internal/keeper/keeper.go | 4 ++-- x/msg_authorization/internal/types/codec.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 750f747684d3..077b6c37c2f3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -31,11 +31,11 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd //TODO } -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { //TODO } -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { //TODO return nil, time.Now() } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index fe10042d1583..d9f8eb7ba7f6 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -6,4 +6,8 @@ import ( var ModuleCdc = codec.New() -//ToDO register concrete +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) + cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) + cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) +} From ddd8b699ee82983e24c365faf1f3ac9a4cdd8f8d Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 22 Nov 2019 12:43:18 +0530 Subject: [PATCH 099/162] Added Send Capability --- .../internal/types/capabilities.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 3806f3a66684..5ce76eb3c43d 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" abci "github.com/tendermint/tendermint/abci/types" ) @@ -9,3 +10,29 @@ type Capability interface { MsgType() sdk.Msg Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From 1253c2cdf1b8e9fa3b0c47afd43b2d2bc753c831 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 2 Dec 2019 19:53:06 +0530 Subject: [PATCH 100/162] Added keeper methods for authorization --- x/msg_authorization/internal/keeper/keeper.go | 75 +++++++++++++++++-- .../internal/keeper/keeper_test.go | 1 + .../internal/types/capabilities.go | 16 ++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/keeper_test.go diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 077b6c37c2f3..811451ef002b 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -1,10 +1,13 @@ package keeper import ( + "bytes" + "fmt" + "time" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper struct { @@ -22,20 +25,80 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } +func (k Keeper) getActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +} + +func (k Keeper) getCapabilityGrant(ctx sdk.Context, actor []byte) (grant types.CapabilityGrant, found bool) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(actor) + if bz == nil { + return grant, false + } + k.cdc.MustUnmarshalBinaryBare(bz, &grant) + return grant, true +} + +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Capability) { + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, updated.MsgType())) + if !found { + return + } + grant.Capability = updated +} + func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { - //TODO + var res sdk.Result + for _, msg := range msgs { + signers := msg.GetSigners() + if len(signers) != 1 { + return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + } + granter := signers[0] + if !bytes.Equal(granter, grantee) { + capability, _ := k.GetCapability(ctx, grantee, granter, msg) + if capability == nil { + return sdk.ErrUnauthorized("unauthorized").Result() + } + allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) + if !allow { + return sdk.ErrUnauthorized("unauthorized").Result() + } + if del { + k.Revoke(ctx, grantee, granter, msg) + } else if updated != nil { + k.update(ctx, grantee, granter, updated) + } + } + res = k.router.Route(msg.Route())(ctx, msg) + if !res.IsOK() { + return res + } + } + return sdk.Result{} } func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { - //TODO + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinaryBare(types.CapabilityGrant{Capability: capability, Expiration: expiration}) + actor := k.getActorCapabilityKey(grantee, granter, capability.MsgType()) + store.Set(actor, bz) } func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { - //TODO + store := ctx.KVStore(k.storeKey) + store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) } func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { - //TODO - return nil, time.Now() + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) + if !found { + return nil, time.Time{} + } + if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { + k.Revoke(ctx, grantee, granter, msgType) + return nil, time.Time{} + } + return grant.Capability, grant.Expiration } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go new file mode 100644 index 000000000000..b55569d4a442 --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -0,0 +1 @@ +package keeper diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 5ce76eb3c43d..acbb26e1593e 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -3,6 +3,9 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + + "time" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -36,3 +39,16 @@ func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow b } return false, nil, false } + +type CapabilityGrant struct { + Capability Capability + + Expiration time.Time +} + +// GenericCapability grants the permission to execute any transaction of the provided +// sdk.Msg type without restrictions +type GenericCapability struct { + // MsgType is the type of Msg this capability grant allows + MsgType sdk.Msg +} From debecf31344cc206ed9fa7386d23b5c01780113c Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 101/162] Added keeper implementations --- x/msg_authorization/alias.go | 36 +++++++++++++--- x/msg_authorization/exported/keeper.go | 3 +- x/msg_authorization/internal/keeper/keeper.go | 6 +-- .../internal/keeper/keeper_test.go | 2 +- .../internal/keeper/test_common.go | 43 +++++++++++++++++++ .../internal/types/capabilities.go | 31 +------------ x/msg_authorization/internal/types/msgs.go | 3 +- .../internal/types/send_capability.go | 34 +++++++++++++++ 8 files changed, 117 insertions(+), 41 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/test_common.go create mode 100644 x/msg_authorization/internal/types/send_capability.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index bf996e6bff6d..701280c3c5f2 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -1,3 +1,8 @@ +// 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 ( @@ -6,20 +11,39 @@ import ( ) const ( - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - QuerierRoute = types.QuerierRoute + DefaultCodespace = types.DefaultCodespace + CodeInvalidGranter = types.CodeInvalidGranter + CodeInvalidGrantee = types.CodeInvalidGrantee + CodeInvalidExpirationTime = types.CodeInvalidExpirationTime + ModuleName = types.ModuleName + StoreKey = types.StoreKey + RouterKey = types.RouterKey + QuerierRoute = types.QuerierRoute ) var ( - NewKeeper = keeper.NewKeeper + // functions aliases + RegisterCodec = types.RegisterCodec + ErrInvalidGranter = types.ErrInvalidGranter + ErrInvalidGrantee = types.ErrInvalidGrantee + ErrInvalidExpirationTime = types.ErrInvalidExpirationTime + NewMsgGrantAuthorization = types.NewMsgGrantAuthorization + NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization + NewMsgExecDelegated = types.NewMsgExecDelegated + NewKeeper = keeper.NewKeeper + + // variable aliases + ModuleCdc = types.ModuleCdc ) type ( - Keeper = keeper.Keeper Capability = types.Capability + SendCapability = types.SendCapability + CapabilityGrant = types.CapabilityGrant + GenericCapability = types.GenericCapability + CodeType = types.CodeType MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization MsgExecDelegated = types.MsgExecDelegated + Keeper = keeper.Keeper ) diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index ead70b2428ac..69d581b5f182 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,10 @@ package exported import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper interface { diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 811451ef002b..acb95937b9b9 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -52,17 +52,17 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] for _, msg := range msgs { signers := msg.GetSigners() if len(signers) != 1 { - return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + return sdk.ErrUnknownRequest("authorization can be given to msg with only one signer").Result() } granter := signers[0] if !bytes.Equal(granter, grantee) { capability, _ := k.GetCapability(ctx, grantee, granter, msg) if capability == nil { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized("authorization not found").Result() } allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) if !allow { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized(" ").Result() } if del { k.Revoke(ctx, grantee, granter, msg) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index b55569d4a442..9429264902a9 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1 @@ -package keeper +package keeper_test diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go new file mode 100644 index 000000000000..97a0e177613c --- /dev/null +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -0,0 +1,43 @@ +package keeper + +import ( + "github.com/tendermint/tendermint/crypto/ed25519" + + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type testInput struct { + cdc *codec.Codec + ctx sdk.Context + ak auth.AccountKeeper + pk params.Keeper + bk bank.Keeper + dk Keeper + router sdk.Router +} + +func setupTestInput() testInput { + db := dbm.NewMemDB() + + cdc := codec.New() + auth.RegisterCodec(cdc) + bank.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + + //TODO create test input + return testInput{} +} + +var ( + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) +) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index acbb26e1593e..8e47d7c461e4 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,11 +1,10 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - "time" + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -14,32 +13,6 @@ type Capability interface { Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } -type SendCapability struct { - // SpendLimit specifies the maximum amount of tokens that can be spent - // by this capability and will be updated as tokens are spent. If it is - // empty, there is no spend limit and any amount of coins can be spent. - SpendLimit sdk.Coins -} - -func (capability SendCapability) MsgType() sdk.Msg { - return bank.MsgSend{} -} - -func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { - switch msg := msg.(type) { - case bank.MsgSend: - limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) - if isNegative { - return false, nil, false - } - if limitLeft.IsZero() { - return true, nil, true - } - return true, SendCapability{SpendLimit: limitLeft}, false - } - return false, nil, false -} - type CapabilityGrant struct { Capability Capability diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's diff --git a/x/msg_authorization/internal/types/send_capability.go b/x/msg_authorization/internal/types/send_capability.go new file mode 100644 index 000000000000..230643102e4b --- /dev/null +++ b/x/msg_authorization/internal/types/send_capability.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" + + abci "github.com/tendermint/tendermint/abci/types" +) + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From dc1041ad9a983c64bb8247e9f67a1ce60be725b9 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 5 Dec 2019 20:52:08 +0530 Subject: [PATCH 102/162] Added test input for keeper --- .../internal/keeper/test_common.go | 75 +++++++++++++++---- x/msg_authorization/test_common.go | 3 + 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/test_common.go diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 97a0e177613c..988e873a6c51 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,28 +1,40 @@ package keeper import ( + "testing" + "time" + + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/supply" ) -type testInput struct { - cdc *codec.Codec - ctx sdk.Context - ak auth.AccountKeeper - pk params.Keeper - bk bank.Keeper - dk Keeper - router sdk.Router -} +func makeTestCodec() *codec.Codec { + var cdc = codec.New() + auth.RegisterCodec(cdc) + types.RegisterCodec(cdc) + supply.RegisterCodec(cdc) + staking.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) -func setupTestInput() testInput { + return cdc +} +func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -31,13 +43,44 @@ func setupTestInput() testInput { sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - //TODO create test input - return testInput{} + keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + keyParams := sdk.NewKVStoreKey(params.StoreKey) + keyAuthorization := sdk.NewKVStoreKey(types.StoreKey) + tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) + + ms := store.NewCommitMultiStore(db) + ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) + + err := ms.LoadLatestVersion() + require.Nil(t, err) + + ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) + cdc = makeTestCodec() + + blacklistedAddrs := make(map[string]bool) + + paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace) + authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) + bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) + bankKeeper.SetSendEnabled(ctx, true) + + router := baseapp.NewRouter() + router.AddRoute("bank", bank.NewHandler(bankKeeper)) + + authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) + + authKeeper.SetParams(ctx, auth.DefaultParams()) + + return ctx, authKeeper, paramsKeeper, bankKeeper, authorizationKeeper, router } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + authorizedPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) + authorizedAddr = sdk.AccAddress(authorizedPub.Address()) ) diff --git a/x/msg_authorization/test_common.go b/x/msg_authorization/test_common.go new file mode 100644 index 000000000000..d0cb9b8dfcc2 --- /dev/null +++ b/x/msg_authorization/test_common.go @@ -0,0 +1,3 @@ +package msg_authorization + +//TODO create a mock app to demonstrate msg_authorization functionality From 54ff2ed79162c347d6e126450eae58b37050e6e8 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 9 Dec 2019 20:06:35 +0530 Subject: [PATCH 103/162] Added tests for keeper --- x/msg_authorization/handler.go | 23 +++++++- .../internal/keeper/keeper_test.go | 55 ++++++++++++++++++- .../internal/keeper/test_common.go | 20 +++---- x/msg_authorization/internal/types/events.go | 13 +++++ 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 x/msg_authorization/internal/types/events.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 9684b5c01187..c1a496fe679d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) func NewHandler(k Keeper) sdk.Handler { @@ -24,12 +25,30 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { - //TODO + k.Grant(ctx, msg.Grantee, msg.Granter, msg.Capability, msg.Expiration) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { - //TODO + k.Revoke(ctx, msg.Grantee, msg.Granter, msg.CapabilityMsgType) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 9429264902a9..a846474bf037 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1,54 @@ -package keeper_test +package keeper + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type TestSuite struct { + suite.Suite + ctx sdk.Context + accountKeeper auth.AccountKeeper + paramsKeeper params.Keeper + bankKeeper bank.Keeper + keeper Keeper + router sdk.Router +} + +func (s *TestSuite) SetupTest() { + s.ctx, s.accountKeeper, s.paramsKeeper, s.bankKeeper, s.keeper, s.router = SetupTestInput() + +} + +func (s *TestSuite) TestKeeper(t *testing.T) { + err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + require.Nil(t, err) + require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + + t.Log("Verify that no capability returns nil") + capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + //require.Nil(t, expiration) + now := s.ctx.BlockHeader().Time + require.NotNil(t, now) + + newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) + capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + require.NotNil(t, capability) + require.NotNil(t, expiration) + //TODO add more cases +} + +func TestTestSuite(t *testing.T) { + suite.Run(t, new(TestSuite)) +} diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 988e873a6c51..f7874ff21cfe 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,10 +1,8 @@ package keeper import ( - "testing" "time" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" abci "github.com/tendermint/tendermint/abci/types" @@ -34,7 +32,7 @@ func makeTestCodec() *codec.Codec { return cdc } -func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { +func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -51,10 +49,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyAuthorization, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) - err := ms.LoadLatestVersion() - require.Nil(t, err) + ms.LoadLatestVersion() ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) cdc = makeTestCodec() @@ -77,10 +75,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - authorizedPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) - authorizedAddr = sdk.AccAddress(authorizedPub.Address()) + granteePub = ed25519.GenPrivKey().PubKey() + granterPub = ed25519.GenPrivKey().PubKey() + recepientPub = ed25519.GenPrivKey().PubKey() + granteeAddr = sdk.AccAddress(granteePub.Address()) + granterAddr = sdk.AccAddress(granterPub.Address()) + recepientAddr = sdk.AccAddress(recepientPub.Address()) ) diff --git a/x/msg_authorization/internal/types/events.go b/x/msg_authorization/internal/types/events.go new file mode 100644 index 000000000000..823ad3ce8cc5 --- /dev/null +++ b/x/msg_authorization/internal/types/events.go @@ -0,0 +1,13 @@ +package types + +// msg_authorization module events +const ( + EventGrantAuthorization = "grant-authorization" + EventRevokeAuthorization = "revoke-authorization" + EventExecuteAuthorization = "execute-authorization" + + AttributeKeyGranteeAddress = "grantee" + AttributeKeyGranterAddress = "granter" + + AttributeValueCategory = ModuleName +) From c08bef426ec75095c81b1464a11eb725251a3260 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 10 Dec 2019 13:16:55 +0530 Subject: [PATCH 104/162] Added more test cases for keeper --- .../internal/keeper/keeper_test.go | 43 ++++++++++++++----- x/msg_authorization/internal/types/codec.go | 4 ++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index a846474bf037..76fafcdb3e52 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,24 +28,48 @@ func (s *TestSuite) SetupTest() { } -func (s *TestSuite) TestKeeper(t *testing.T) { +func (s *TestSuite) TestKeeper() { err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) - require.Nil(t, err) - require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + s.Require().Nil(err) + s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - t.Log("Verify that no capability returns nil") + s.T().Log("Verify that no capability returns nil") capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time - require.NotNil(t, now) + s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.T().Log("Verify if expired capability is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - require.NotNil(t, capability) - require.NotNil(t, expiration) - //TODO add more cases + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify if capability is accepted") + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + s.Require().Equal(capability.MsgType(), bank.MsgSend{}) + + s.T().Log("Verify fetching capability with wrong msg type fails") + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify fetching capability with wrong grantee fails") + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke fails with wrong information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke executes with correct information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + } func TestTestSuite(t *testing.T) { diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index d9f8eb7ba7f6..b6003860776a 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -10,4 +10,8 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) + cdc.RegisterConcrete(SendCapability{}, "cosmos-sdk/SendCapability", nil) + cdc.RegisterConcrete(CapabilityGrant{}, "cosmos-sdk/CapabilityGrant", nil) + + cdc.RegisterInterface((*Capability)(nil), nil) } From cad1b07f4c2fec8d0ed3128b0c988437af228b6f Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Mon, 9 Dec 2019 21:11:00 +0530 Subject: [PATCH 105/162] Added grant,revoke to cli --- x/msg_authorization/client/cli/tx.go | 73 ++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 47244a825235..9242e3f61dff 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -1,8 +1,13 @@ package cli import ( + "bufio" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" ) @@ -12,6 +17,7 @@ func GetTxCmd(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, @@ -26,11 +32,70 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { } func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "grant", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var capability types.Capability + err = cdc.UnmarshalJSON([]byte(args[1]), &capability) + if err != nil { + return err + } + + msg := types.NewMsgGrantAuthorization(granter, grantee, capability, expiration) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + + }, + } + return cmd } func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "revoke", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var msgAuthorized sdk.Msg + err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) + if err != nil { + return err + } + + msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + }, + } + return cmd } From c07b445333c3e572f9db2ed296d95f8d6e8d0004 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 10 Dec 2019 19:53:49 +0530 Subject: [PATCH 106/162] Added expiration flag to grant tx --- x/msg_authorization/client/cli/flags.go | 3 +++ x/msg_authorization/client/cli/tx.go | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 x/msg_authorization/client/cli/flags.go diff --git a/x/msg_authorization/client/cli/flags.go b/x/msg_authorization/client/cli/flags.go new file mode 100644 index 000000000000..a77534c3551f --- /dev/null +++ b/x/msg_authorization/client/cli/flags.go @@ -0,0 +1,3 @@ +package cli + +const FlagExpiration = "expiration" diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 9242e3f61dff..ff33d7fa57ae 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,8 @@ package cli import ( "bufio" + "time" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" @@ -10,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // GetTxCmd returns the transaction commands for this module @@ -53,6 +56,11 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { 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, capability, expiration) if err := msg.ValidateBasic(); err != nil { @@ -63,6 +71,8 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { }, } + cmd.Flags().String(FlagExpiration, "9999-12-31 23:59:59.52Z", "The time upto which the authorization is active for the user") + return cmd } From ec6d38767619f9ac6cc52a5dfa2fb679e2ec1efc Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 107/162] Added code skeleton for msg authorization --- x/msg_authorization/client/cli/query.go | 2 ++ x/msg_authorization/client/cli/tx.go | 36 +++++++++++++++++++ x/msg_authorization/client/rest/query.go | 1 + x/msg_authorization/client/rest/tx.go | 1 + x/msg_authorization/exported/keeper.go | 23 ++++++++++++ x/msg_authorization/handler.go | 1 + x/msg_authorization/internal/keeper/keeper.go | 21 +++++++++++ .../internal/types/capabilities.go | 1 + x/msg_authorization/internal/types/keys.go | 15 ++++++++ x/msg_authorization/internal/types/msg.go | 34 ++++++++++++++++++ x/msg_authorization/module.go | 1 + 11 files changed, 136 insertions(+) create mode 100644 x/msg_authorization/client/cli/query.go create mode 100644 x/msg_authorization/client/cli/tx.go create mode 100644 x/msg_authorization/client/rest/query.go create mode 100644 x/msg_authorization/client/rest/tx.go create mode 100644 x/msg_authorization/exported/keeper.go create mode 100644 x/msg_authorization/handler.go create mode 100644 x/msg_authorization/internal/keeper/keeper.go create mode 100644 x/msg_authorization/internal/types/capabilities.go create mode 100644 x/msg_authorization/internal/types/keys.go create mode 100644 x/msg_authorization/internal/types/msg.go create mode 100644 x/msg_authorization/module.go diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go new file mode 100644 index 000000000000..c56a80569b50 --- /dev/null +++ b/x/msg_authorization/client/cli/query.go @@ -0,0 +1,2 @@ +package cli + diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go new file mode 100644 index 000000000000..b6e782d44f35 --- /dev/null +++ b/x/msg_authorization/client/cli/tx.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + AuthorizationTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Authorization transactions subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + AuthorizationTxCmd.AddCommand(client.PostCommands( + GetCmdGrantCapability(cdc), + GetCmdRevokeCapability(cdc), + )...) + + return AuthorizationTxCmd +} + +func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} + +func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} \ No newline at end of file diff --git a/x/msg_authorization/client/rest/query.go b/x/msg_authorization/client/rest/query.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/query.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/tx.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go new file mode 100644 index 000000000000..3ca0e9d31005 --- /dev/null +++ b/x/msg_authorization/exported/keeper.go @@ -0,0 +1,23 @@ +package exported + +import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" + +) + +type Keeper interface { + //DispatchActions executes the provided messages via capability grants from the message signer to the grantee + DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result + + // Grants the provided capability to the grantee on the granter's account with the provided expiration time + // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + + //Revokes any capability 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 Capability (or nil), with the expiration time, + // granted to the grantee by the granter for the provided msg type. + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) +} diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/handler.go @@ -0,0 +1 @@ +package msg_authorization diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go new file mode 100644 index 000000000000..8731cf48790c --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -0,0 +1,21 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + router sdk.Router +} + +// NewKeeper constructs a message authorisation Keeper +func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keeper { + return Keeper{ + storeKey: storeKey, + cdc: cdc, + router: router, + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go new file mode 100644 index 000000000000..ab1254f4c2be --- /dev/null +++ b/x/msg_authorization/internal/types/capabilities.go @@ -0,0 +1 @@ +package types diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go new file mode 100644 index 000000000000..8d327e683e07 --- /dev/null +++ b/x/msg_authorization/internal/types/keys.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the module name constant used in many places + ModuleName = "msg_authorization" + + // StoreKey is the store key string for msg_authorization + StoreKey = ModuleName + + // RouterKey is the message route for msg_authorization + RouterKey = ModuleName + + // QuerierRoute is the querier route for msg_authorization + QuerierRoute = ModuleName +) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/module.go @@ -0,0 +1 @@ +package msg_authorization From 5890a7e8b37356234c220cd913be1d4ee638aee5 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 108/162] Implemented sdk.msg methods --- x/msg_authorization/alias.go | 25 ++++++ x/msg_authorization/client/cli/query.go | 1 - x/msg_authorization/client/cli/tx.go | 4 +- x/msg_authorization/exported/keeper.go | 8 +- x/msg_authorization/handler.go | 14 ++++ .../internal/types/capabilities.go | 4 + x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++- 7 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 x/msg_authorization/alias.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go new file mode 100644 index 000000000000..bf996e6bff6d --- /dev/null +++ b/x/msg_authorization/alias.go @@ -0,0 +1,25 @@ +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 ( + NewKeeper = keeper.NewKeeper +) + +type ( + Keeper = keeper.Keeper + Capability = types.Capability + MsgGrantAuthorization = types.MsgGrantAuthorization + MsgRevokeAuthorization = types.MsgRevokeAuthorization + MsgExecDelegated = types.MsgExecDelegated +) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index c56a80569b50..7f1e458cd3ab 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -1,2 +1 @@ package cli - diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index b6e782d44f35..47244a825235 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -3,8 +3,8 @@ package cli import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/spf13/cobra" ) // GetTxCmd returns the transaction commands for this module @@ -33,4 +33,4 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { //TODO return nil -} \ No newline at end of file +} diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 3ca0e9d31005..ead70b2428ac 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,9 @@ package exported import ( - "time" sdk "github.com/cosmos/cosmos-sdk/types" - + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper interface { @@ -12,12 +12,12 @@ type Keeper interface { // Grants the provided capability to the grantee on the granter's account with the provided expiration time // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. - Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) //Revokes any capability 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 Capability (or nil), with the expiration time, // granted to the grantee by the granter for the provided msg type. - GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) } diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index d552162c4c47..e8fb5a08a29d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1 +1,15 @@ package msg_authorization + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + + switch msg := msg.(type) { + //TODO + } + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index ab1254f4c2be..17d0d6992f96 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1 +1,5 @@ package types + +type Capability interface { + //TODO +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From 0a8b0492dab4742136c39a8178a0954122bcf181 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 109/162] Created handler for authorization --- x/msg_authorization/handler.go | 28 +++++++++++++++++-- x/msg_authorization/internal/keeper/keeper.go | 2 ++ x/msg_authorization/internal/types/codec.go | 9 ++++++ x/msg_authorization/internal/types/msg.go | 9 ++++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 x/msg_authorization/internal/types/codec.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index e8fb5a08a29d..9684b5c01187 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1,15 +1,39 @@ package msg_authorization import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) func NewHandler(k Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { ctx = ctx.WithEventManager(sdk.NewEventManager()) - switch msg := msg.(type) { - //TODO + case MsgGrantAuthorization: + return handleMsgGrantAuthorization(ctx, msg, k) + case MsgRevokeAuthorization: + return handleMsgRevokeAuthorization(ctx, msg, k) + case MsgExecDelegated: + return handleMsgExecDelegated(ctx, msg, k) + default: + errMsg := fmt.Sprintf("unrecognized authorization message type: %T", msg) + return sdk.ErrUnknownRequest(errMsg).Result() } } } + +func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 8731cf48790c..a7b70c5d1f03 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -19,3 +19,5 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe router: router, } } + +//TODO implement all keeper methods diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go new file mode 100644 index 000000000000..fe10042d1583 --- /dev/null +++ b/x/msg_authorization/internal/types/codec.go @@ -0,0 +1,9 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +var ModuleCdc = codec.New() + +//ToDO register concrete diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From aab8be263db1232e863e2a72044b1074a84f811c Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 110/162] Added validate basic to all messages --- .../internal/types/capabilities.go | 8 +++- x/msg_authorization/internal/types/errors.go | 27 ++++++++++++ .../internal/types/{msg.go => msgs.go} | 43 ++++++++++++------- 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/internal/types/errors.go rename x/msg_authorization/internal/types/{msg.go => msgs.go} (78%) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 17d0d6992f96..3806f3a66684 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,5 +1,11 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + type Capability interface { - //TODO + MsgType() sdk.Msg + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } diff --git a/x/msg_authorization/internal/types/errors.go b/x/msg_authorization/internal/types/errors.go new file mode 100644 index 000000000000..7e685bba677c --- /dev/null +++ b/x/msg_authorization/internal/types/errors.go @@ -0,0 +1,27 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type CodeType = sdk.CodeType + +const ( + // Default slashing codespace + DefaultCodespace sdk.CodespaceType = ModuleName + + CodeInvalidGranter CodeType = 101 + CodeInvalidGrantee CodeType = 102 + CodeInvalidExpirationTime CodeType = 103 +) + +func ErrInvalidGranter(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGranter, "invalid granter address") +} + +func ErrInvalidGrantee(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGrantee, "invalid grantee address") +} +func ErrInvalidExpirationTime(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidExpirationTime, "expiration time of authorization should be more than current time") +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msgs.go similarity index 78% rename from x/msg_authorization/internal/types/msg.go rename to x/msg_authorization/internal/types/msgs.go index ce6d370b3a9b..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -28,17 +28,25 @@ func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return ErrInvalidGranter(DefaultCodespace) + } + if msg.Grantee.Empty() { + return ErrInvalidGrantee(DefaultCodespace) + } + if msg.Expiration.Unix() < time.Now().Unix() { + return ErrInvalidExpirationTime(DefaultCodespace) + } + return nil } @@ -64,17 +72,21 @@ func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return sdk.ErrInvalidAddress(msg.Granter.String()) + } + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } @@ -96,16 +108,17 @@ func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Grantee} } func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } From 902ced63cd981cf90a263eb202c581392fb92f31 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:23:17 +0530 Subject: [PATCH 111/162] Added base for internal keeper --- x/msg_authorization/internal/keeper/keeper.go | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index a7b70c5d1f03..750f747684d3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -3,6 +3,8 @@ package keeper import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper struct { @@ -20,4 +22,20 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } -//TODO implement all keeper methods +func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { + //TODO + return sdk.Result{} +} + +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { + //TODO +} + +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { + //TODO +} + +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { + //TODO + return nil, time.Now() +} From 9975358f83228194939556860e855b204dd48e72 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:33:25 +0530 Subject: [PATCH 112/162] Added message registration to codec --- x/msg_authorization/internal/keeper/keeper.go | 4 ++-- x/msg_authorization/internal/types/codec.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 750f747684d3..077b6c37c2f3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -31,11 +31,11 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd //TODO } -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { //TODO } -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { //TODO return nil, time.Now() } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index fe10042d1583..d9f8eb7ba7f6 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -6,4 +6,8 @@ import ( var ModuleCdc = codec.New() -//ToDO register concrete +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) + cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) + cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) +} From f7bd342d3268752270d23fd86762bf17c1013775 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 22 Nov 2019 12:43:18 +0530 Subject: [PATCH 113/162] Added Send Capability --- .../internal/types/capabilities.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 3806f3a66684..5ce76eb3c43d 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" abci "github.com/tendermint/tendermint/abci/types" ) @@ -9,3 +10,29 @@ type Capability interface { MsgType() sdk.Msg Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From 6cbf544b81e00c5c48d876058fdfeb1d75a87053 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 2 Dec 2019 19:53:06 +0530 Subject: [PATCH 114/162] Added keeper methods for authorization --- x/msg_authorization/internal/keeper/keeper.go | 75 +++++++++++++++++-- .../internal/keeper/keeper_test.go | 1 + .../internal/types/capabilities.go | 16 ++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/keeper_test.go diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 077b6c37c2f3..811451ef002b 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -1,10 +1,13 @@ package keeper import ( + "bytes" + "fmt" + "time" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper struct { @@ -22,20 +25,80 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } +func (k Keeper) getActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +} + +func (k Keeper) getCapabilityGrant(ctx sdk.Context, actor []byte) (grant types.CapabilityGrant, found bool) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(actor) + if bz == nil { + return grant, false + } + k.cdc.MustUnmarshalBinaryBare(bz, &grant) + return grant, true +} + +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Capability) { + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, updated.MsgType())) + if !found { + return + } + grant.Capability = updated +} + func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { - //TODO + var res sdk.Result + for _, msg := range msgs { + signers := msg.GetSigners() + if len(signers) != 1 { + return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + } + granter := signers[0] + if !bytes.Equal(granter, grantee) { + capability, _ := k.GetCapability(ctx, grantee, granter, msg) + if capability == nil { + return sdk.ErrUnauthorized("unauthorized").Result() + } + allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) + if !allow { + return sdk.ErrUnauthorized("unauthorized").Result() + } + if del { + k.Revoke(ctx, grantee, granter, msg) + } else if updated != nil { + k.update(ctx, grantee, granter, updated) + } + } + res = k.router.Route(msg.Route())(ctx, msg) + if !res.IsOK() { + return res + } + } + return sdk.Result{} } func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { - //TODO + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinaryBare(types.CapabilityGrant{Capability: capability, Expiration: expiration}) + actor := k.getActorCapabilityKey(grantee, granter, capability.MsgType()) + store.Set(actor, bz) } func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { - //TODO + store := ctx.KVStore(k.storeKey) + store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) } func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { - //TODO - return nil, time.Now() + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) + if !found { + return nil, time.Time{} + } + if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { + k.Revoke(ctx, grantee, granter, msgType) + return nil, time.Time{} + } + return grant.Capability, grant.Expiration } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go new file mode 100644 index 000000000000..b55569d4a442 --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -0,0 +1 @@ +package keeper diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 5ce76eb3c43d..acbb26e1593e 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -3,6 +3,9 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + + "time" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -36,3 +39,16 @@ func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow b } return false, nil, false } + +type CapabilityGrant struct { + Capability Capability + + Expiration time.Time +} + +// GenericCapability grants the permission to execute any transaction of the provided +// sdk.Msg type without restrictions +type GenericCapability struct { + // MsgType is the type of Msg this capability grant allows + MsgType sdk.Msg +} From 932c391f5251d7261d6b1a33e9d98cd9ef39ec30 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 115/162] Added keeper implementations --- x/msg_authorization/alias.go | 36 +++++++++++++--- x/msg_authorization/exported/keeper.go | 3 +- x/msg_authorization/internal/keeper/keeper.go | 6 +-- .../internal/keeper/keeper_test.go | 2 +- .../internal/keeper/test_common.go | 43 +++++++++++++++++++ .../internal/types/capabilities.go | 31 +------------ x/msg_authorization/internal/types/msgs.go | 3 +- .../internal/types/send_capability.go | 34 +++++++++++++++ 8 files changed, 117 insertions(+), 41 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/test_common.go create mode 100644 x/msg_authorization/internal/types/send_capability.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index bf996e6bff6d..701280c3c5f2 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -1,3 +1,8 @@ +// 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 ( @@ -6,20 +11,39 @@ import ( ) const ( - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - QuerierRoute = types.QuerierRoute + DefaultCodespace = types.DefaultCodespace + CodeInvalidGranter = types.CodeInvalidGranter + CodeInvalidGrantee = types.CodeInvalidGrantee + CodeInvalidExpirationTime = types.CodeInvalidExpirationTime + ModuleName = types.ModuleName + StoreKey = types.StoreKey + RouterKey = types.RouterKey + QuerierRoute = types.QuerierRoute ) var ( - NewKeeper = keeper.NewKeeper + // functions aliases + RegisterCodec = types.RegisterCodec + ErrInvalidGranter = types.ErrInvalidGranter + ErrInvalidGrantee = types.ErrInvalidGrantee + ErrInvalidExpirationTime = types.ErrInvalidExpirationTime + NewMsgGrantAuthorization = types.NewMsgGrantAuthorization + NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization + NewMsgExecDelegated = types.NewMsgExecDelegated + NewKeeper = keeper.NewKeeper + + // variable aliases + ModuleCdc = types.ModuleCdc ) type ( - Keeper = keeper.Keeper Capability = types.Capability + SendCapability = types.SendCapability + CapabilityGrant = types.CapabilityGrant + GenericCapability = types.GenericCapability + CodeType = types.CodeType MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization MsgExecDelegated = types.MsgExecDelegated + Keeper = keeper.Keeper ) diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index ead70b2428ac..69d581b5f182 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,10 @@ package exported import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper interface { diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 811451ef002b..acb95937b9b9 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -52,17 +52,17 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] for _, msg := range msgs { signers := msg.GetSigners() if len(signers) != 1 { - return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + return sdk.ErrUnknownRequest("authorization can be given to msg with only one signer").Result() } granter := signers[0] if !bytes.Equal(granter, grantee) { capability, _ := k.GetCapability(ctx, grantee, granter, msg) if capability == nil { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized("authorization not found").Result() } allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) if !allow { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized(" ").Result() } if del { k.Revoke(ctx, grantee, granter, msg) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index b55569d4a442..9429264902a9 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1 @@ -package keeper +package keeper_test diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go new file mode 100644 index 000000000000..97a0e177613c --- /dev/null +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -0,0 +1,43 @@ +package keeper + +import ( + "github.com/tendermint/tendermint/crypto/ed25519" + + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type testInput struct { + cdc *codec.Codec + ctx sdk.Context + ak auth.AccountKeeper + pk params.Keeper + bk bank.Keeper + dk Keeper + router sdk.Router +} + +func setupTestInput() testInput { + db := dbm.NewMemDB() + + cdc := codec.New() + auth.RegisterCodec(cdc) + bank.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + + //TODO create test input + return testInput{} +} + +var ( + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) +) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index acbb26e1593e..8e47d7c461e4 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,11 +1,10 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - "time" + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -14,32 +13,6 @@ type Capability interface { Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } -type SendCapability struct { - // SpendLimit specifies the maximum amount of tokens that can be spent - // by this capability and will be updated as tokens are spent. If it is - // empty, there is no spend limit and any amount of coins can be spent. - SpendLimit sdk.Coins -} - -func (capability SendCapability) MsgType() sdk.Msg { - return bank.MsgSend{} -} - -func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { - switch msg := msg.(type) { - case bank.MsgSend: - limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) - if isNegative { - return false, nil, false - } - if limitLeft.IsZero() { - return true, nil, true - } - return true, SendCapability{SpendLimit: limitLeft}, false - } - return false, nil, false -} - type CapabilityGrant struct { Capability Capability diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's diff --git a/x/msg_authorization/internal/types/send_capability.go b/x/msg_authorization/internal/types/send_capability.go new file mode 100644 index 000000000000..230643102e4b --- /dev/null +++ b/x/msg_authorization/internal/types/send_capability.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" + + abci "github.com/tendermint/tendermint/abci/types" +) + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From 7713d1c24647eb61fda645441b36a05ceb90fd07 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 5 Dec 2019 20:52:08 +0530 Subject: [PATCH 116/162] Added test input for keeper --- .../internal/keeper/test_common.go | 75 +++++++++++++++---- x/msg_authorization/test_common.go | 3 + 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/test_common.go diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 97a0e177613c..988e873a6c51 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,28 +1,40 @@ package keeper import ( + "testing" + "time" + + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/supply" ) -type testInput struct { - cdc *codec.Codec - ctx sdk.Context - ak auth.AccountKeeper - pk params.Keeper - bk bank.Keeper - dk Keeper - router sdk.Router -} +func makeTestCodec() *codec.Codec { + var cdc = codec.New() + auth.RegisterCodec(cdc) + types.RegisterCodec(cdc) + supply.RegisterCodec(cdc) + staking.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) -func setupTestInput() testInput { + return cdc +} +func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -31,13 +43,44 @@ func setupTestInput() testInput { sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - //TODO create test input - return testInput{} + keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + keyParams := sdk.NewKVStoreKey(params.StoreKey) + keyAuthorization := sdk.NewKVStoreKey(types.StoreKey) + tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) + + ms := store.NewCommitMultiStore(db) + ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) + + err := ms.LoadLatestVersion() + require.Nil(t, err) + + ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) + cdc = makeTestCodec() + + blacklistedAddrs := make(map[string]bool) + + paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace) + authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) + bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) + bankKeeper.SetSendEnabled(ctx, true) + + router := baseapp.NewRouter() + router.AddRoute("bank", bank.NewHandler(bankKeeper)) + + authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) + + authKeeper.SetParams(ctx, auth.DefaultParams()) + + return ctx, authKeeper, paramsKeeper, bankKeeper, authorizationKeeper, router } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + authorizedPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) + authorizedAddr = sdk.AccAddress(authorizedPub.Address()) ) diff --git a/x/msg_authorization/test_common.go b/x/msg_authorization/test_common.go new file mode 100644 index 000000000000..d0cb9b8dfcc2 --- /dev/null +++ b/x/msg_authorization/test_common.go @@ -0,0 +1,3 @@ +package msg_authorization + +//TODO create a mock app to demonstrate msg_authorization functionality From 9ad28db7368aff9453fd8db1bf008c9cdeeb379d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 9 Dec 2019 20:06:35 +0530 Subject: [PATCH 117/162] Added tests for keeper --- x/msg_authorization/handler.go | 23 +++++++- .../internal/keeper/keeper_test.go | 55 ++++++++++++++++++- .../internal/keeper/test_common.go | 20 +++---- x/msg_authorization/internal/types/events.go | 13 +++++ 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 x/msg_authorization/internal/types/events.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 9684b5c01187..c1a496fe679d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) func NewHandler(k Keeper) sdk.Handler { @@ -24,12 +25,30 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { - //TODO + k.Grant(ctx, msg.Grantee, msg.Granter, msg.Capability, msg.Expiration) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { - //TODO + k.Revoke(ctx, msg.Grantee, msg.Granter, msg.CapabilityMsgType) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 9429264902a9..a846474bf037 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1,54 @@ -package keeper_test +package keeper + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type TestSuite struct { + suite.Suite + ctx sdk.Context + accountKeeper auth.AccountKeeper + paramsKeeper params.Keeper + bankKeeper bank.Keeper + keeper Keeper + router sdk.Router +} + +func (s *TestSuite) SetupTest() { + s.ctx, s.accountKeeper, s.paramsKeeper, s.bankKeeper, s.keeper, s.router = SetupTestInput() + +} + +func (s *TestSuite) TestKeeper(t *testing.T) { + err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + require.Nil(t, err) + require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + + t.Log("Verify that no capability returns nil") + capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + //require.Nil(t, expiration) + now := s.ctx.BlockHeader().Time + require.NotNil(t, now) + + newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) + capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + require.NotNil(t, capability) + require.NotNil(t, expiration) + //TODO add more cases +} + +func TestTestSuite(t *testing.T) { + suite.Run(t, new(TestSuite)) +} diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 988e873a6c51..f7874ff21cfe 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,10 +1,8 @@ package keeper import ( - "testing" "time" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" abci "github.com/tendermint/tendermint/abci/types" @@ -34,7 +32,7 @@ func makeTestCodec() *codec.Codec { return cdc } -func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { +func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -51,10 +49,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyAuthorization, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) - err := ms.LoadLatestVersion() - require.Nil(t, err) + ms.LoadLatestVersion() ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) cdc = makeTestCodec() @@ -77,10 +75,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - authorizedPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) - authorizedAddr = sdk.AccAddress(authorizedPub.Address()) + granteePub = ed25519.GenPrivKey().PubKey() + granterPub = ed25519.GenPrivKey().PubKey() + recepientPub = ed25519.GenPrivKey().PubKey() + granteeAddr = sdk.AccAddress(granteePub.Address()) + granterAddr = sdk.AccAddress(granterPub.Address()) + recepientAddr = sdk.AccAddress(recepientPub.Address()) ) diff --git a/x/msg_authorization/internal/types/events.go b/x/msg_authorization/internal/types/events.go new file mode 100644 index 000000000000..823ad3ce8cc5 --- /dev/null +++ b/x/msg_authorization/internal/types/events.go @@ -0,0 +1,13 @@ +package types + +// msg_authorization module events +const ( + EventGrantAuthorization = "grant-authorization" + EventRevokeAuthorization = "revoke-authorization" + EventExecuteAuthorization = "execute-authorization" + + AttributeKeyGranteeAddress = "grantee" + AttributeKeyGranterAddress = "granter" + + AttributeValueCategory = ModuleName +) From 21531f0c8b5d62efeb360d8b7741d3a048ce3938 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Mon, 9 Dec 2019 21:11:00 +0530 Subject: [PATCH 118/162] Added grant,revoke to cli --- x/msg_authorization/client/cli/tx.go | 73 ++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 47244a825235..9242e3f61dff 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -1,8 +1,13 @@ package cli import ( + "bufio" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" ) @@ -12,6 +17,7 @@ func GetTxCmd(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, @@ -26,11 +32,70 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { } func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "grant", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var capability types.Capability + err = cdc.UnmarshalJSON([]byte(args[1]), &capability) + if err != nil { + return err + } + + msg := types.NewMsgGrantAuthorization(granter, grantee, capability, expiration) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + + }, + } + return cmd } func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "revoke", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var msgAuthorized sdk.Msg + err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) + if err != nil { + return err + } + + msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + }, + } + return cmd } From 8098f5976f0859c29f9b0fd1ac3824199720f07a Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 10 Dec 2019 13:16:55 +0530 Subject: [PATCH 119/162] Added more test cases for keeper --- .../internal/keeper/keeper_test.go | 43 ++++++++++++++----- x/msg_authorization/internal/types/codec.go | 4 ++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index a846474bf037..76fafcdb3e52 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,24 +28,48 @@ func (s *TestSuite) SetupTest() { } -func (s *TestSuite) TestKeeper(t *testing.T) { +func (s *TestSuite) TestKeeper() { err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) - require.Nil(t, err) - require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + s.Require().Nil(err) + s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - t.Log("Verify that no capability returns nil") + s.T().Log("Verify that no capability returns nil") capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time - require.NotNil(t, now) + s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.T().Log("Verify if expired capability is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - require.NotNil(t, capability) - require.NotNil(t, expiration) - //TODO add more cases + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify if capability is accepted") + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + s.Require().Equal(capability.MsgType(), bank.MsgSend{}) + + s.T().Log("Verify fetching capability with wrong msg type fails") + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify fetching capability with wrong grantee fails") + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke fails with wrong information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke executes with correct information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + } func TestTestSuite(t *testing.T) { diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index d9f8eb7ba7f6..b6003860776a 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -10,4 +10,8 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) + cdc.RegisterConcrete(SendCapability{}, "cosmos-sdk/SendCapability", nil) + cdc.RegisterConcrete(CapabilityGrant{}, "cosmos-sdk/CapabilityGrant", nil) + + cdc.RegisterInterface((*Capability)(nil), nil) } From a839a2831522ac378f04047becb07510772f54ff Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 10 Dec 2019 19:53:49 +0530 Subject: [PATCH 120/162] Added expiration flag to grant tx --- x/msg_authorization/client/cli/flags.go | 3 +++ x/msg_authorization/client/cli/tx.go | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 x/msg_authorization/client/cli/flags.go diff --git a/x/msg_authorization/client/cli/flags.go b/x/msg_authorization/client/cli/flags.go new file mode 100644 index 000000000000..a77534c3551f --- /dev/null +++ b/x/msg_authorization/client/cli/flags.go @@ -0,0 +1,3 @@ +package cli + +const FlagExpiration = "expiration" diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 9242e3f61dff..ff33d7fa57ae 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,8 @@ package cli import ( "bufio" + "time" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" @@ -10,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // GetTxCmd returns the transaction commands for this module @@ -53,6 +56,11 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { 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, capability, expiration) if err := msg.ValidateBasic(); err != nil { @@ -63,6 +71,8 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { }, } + cmd.Flags().String(FlagExpiration, "9999-12-31 23:59:59.52Z", "The time upto which the authorization is active for the user") + return cmd } From d3c18828b16815908111fb29f3a900137ddf1c5a Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 121/162] Added code skeleton for msg authorization --- x/msg_authorization/internal/types/msg.go | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} From fd6127897525b595b7acb3326323cff6115f1721 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 122/162] Implemented sdk.msg methods --- x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++++++- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From 893c2a0eab53b9fadce0beab4e7bc48d87cd9cdc Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 123/162] Created handler for authorization --- x/msg_authorization/internal/types/msg.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From 6beadfbc67648f3c7e4fc8e5a46ab60f57f530f1 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 124/162] Added validate basic to all messages --- x/msg_authorization/internal/types/msg.go | 111 --------------------- x/msg_authorization/internal/types/msgs.go | 3 +- 2 files changed, 1 insertion(+), 113 deletions(-) delete mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go deleted file mode 100644 index ce6d370b3a9b..000000000000 --- a/x/msg_authorization/internal/types/msg.go +++ /dev/null @@ -1,111 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "time" -) - -// MsgGrant grants the provided capability to the grantee on the granter's -// account with the provided expiration time. -type MsgGrantAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Capability Capability `json:"capability"` - // Expiration specifies the expiration time of the grant - Expiration time.Time `json:"expiration"` -} - -func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { - return MsgGrantAuthorization{ - Granter: granter, - Grantee: grantee, - Capability: capability, - Expiration: expiration, - } -} - -func (msg MsgGrantAuthorization) Route() string { return RouterKey } -func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } - -func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the -// granter's account with that has been granted to the grantee. -type MsgRevokeAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. - // i.e. this is what `Capability.MsgType()` returns - CapabilityMsgType sdk.Msg `json:"capability_msg_type"` -} - -func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { - return MsgRevokeAuthorization{ - Granter: granter, - Grantee: grantee, - CapabilityMsgType: capabilityMsgType, - } -} - -func (msg MsgRevokeAuthorization) Route() string { return RouterKey } -func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } - -func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgExecDelegated attempts to execute the provided messages using -// capabilities granted to the grantee. Each message should have only -// one signer corresponding to the granter of the capability. -type MsgExecDelegated struct { - Grantee sdk.AccAddress `json:"grantee"` - Msgs []sdk.Msg `json:"msg"` -} - -func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { - return MsgExecDelegated{ - Grantee: grantee, - Msgs: msgs, - } -} -func (msg MsgExecDelegated) Route() string { return RouterKey } -func (msg MsgExecDelegated) Type() string { return "exec_delegated" } - -func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO - return nil -} diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 87f2681b5fc7..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,9 +1,8 @@ package types import ( - "time" - sdk "github.com/cosmos/cosmos-sdk/types" + "time" ) // MsgGrant grants the provided capability to the grantee on the granter's From c784dc08996a7e286c127cd68862f099edd67409 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 125/162] Added keeper implementations --- x/msg_authorization/internal/types/msgs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's From 5aa13ddafc895223ebc059ab8b042a6b969575f8 Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 126/162] Added code skeleton for msg authorization --- x/msg_authorization/client/cli/query.go | 2 ++ x/msg_authorization/client/cli/tx.go | 36 +++++++++++++++++++ x/msg_authorization/client/rest/query.go | 1 + x/msg_authorization/client/rest/tx.go | 1 + x/msg_authorization/exported/keeper.go | 23 ++++++++++++ x/msg_authorization/handler.go | 1 + x/msg_authorization/internal/keeper/keeper.go | 21 +++++++++++ .../internal/types/capabilities.go | 1 + x/msg_authorization/internal/types/keys.go | 15 ++++++++ x/msg_authorization/internal/types/msg.go | 34 ++++++++++++++++++ x/msg_authorization/module.go | 1 + 11 files changed, 136 insertions(+) create mode 100644 x/msg_authorization/client/cli/query.go create mode 100644 x/msg_authorization/client/cli/tx.go create mode 100644 x/msg_authorization/client/rest/query.go create mode 100644 x/msg_authorization/client/rest/tx.go create mode 100644 x/msg_authorization/exported/keeper.go create mode 100644 x/msg_authorization/handler.go create mode 100644 x/msg_authorization/internal/keeper/keeper.go create mode 100644 x/msg_authorization/internal/types/capabilities.go create mode 100644 x/msg_authorization/internal/types/keys.go create mode 100644 x/msg_authorization/internal/types/msg.go create mode 100644 x/msg_authorization/module.go diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go new file mode 100644 index 000000000000..c56a80569b50 --- /dev/null +++ b/x/msg_authorization/client/cli/query.go @@ -0,0 +1,2 @@ +package cli + diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go new file mode 100644 index 000000000000..b6e782d44f35 --- /dev/null +++ b/x/msg_authorization/client/cli/tx.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(cdc *codec.Codec) *cobra.Command { + AuthorizationTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Authorization transactions subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + AuthorizationTxCmd.AddCommand(client.PostCommands( + GetCmdGrantCapability(cdc), + GetCmdRevokeCapability(cdc), + )...) + + return AuthorizationTxCmd +} + +func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} + +func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { + //TODO + return nil +} \ No newline at end of file diff --git a/x/msg_authorization/client/rest/query.go b/x/msg_authorization/client/rest/query.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/query.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/client/rest/tx.go b/x/msg_authorization/client/rest/tx.go new file mode 100644 index 000000000000..0062e0ca87d9 --- /dev/null +++ b/x/msg_authorization/client/rest/tx.go @@ -0,0 +1 @@ +package rest diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go new file mode 100644 index 000000000000..3ca0e9d31005 --- /dev/null +++ b/x/msg_authorization/exported/keeper.go @@ -0,0 +1,23 @@ +package exported + +import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" + +) + +type Keeper interface { + //DispatchActions executes the provided messages via capability grants from the message signer to the grantee + DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result + + // Grants the provided capability to the grantee on the granter's account with the provided expiration time + // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + + //Revokes any capability 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 Capability (or nil), with the expiration time, + // granted to the grantee by the granter for the provided msg type. + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) +} diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/handler.go @@ -0,0 +1 @@ +package msg_authorization diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go new file mode 100644 index 000000000000..8731cf48790c --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -0,0 +1,21 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type Keeper struct { + storeKey sdk.StoreKey + cdc *codec.Codec + router sdk.Router +} + +// NewKeeper constructs a message authorisation Keeper +func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keeper { + return Keeper{ + storeKey: storeKey, + cdc: cdc, + router: router, + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go new file mode 100644 index 000000000000..ab1254f4c2be --- /dev/null +++ b/x/msg_authorization/internal/types/capabilities.go @@ -0,0 +1 @@ +package types diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go new file mode 100644 index 000000000000..8d327e683e07 --- /dev/null +++ b/x/msg_authorization/internal/types/keys.go @@ -0,0 +1,15 @@ +package types + +const ( + // ModuleName is the module name constant used in many places + ModuleName = "msg_authorization" + + // StoreKey is the store key string for msg_authorization + StoreKey = ModuleName + + // RouterKey is the message route for msg_authorization + RouterKey = ModuleName + + // QuerierRoute is the querier route for msg_authorization + QuerierRoute = ModuleName +) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} diff --git a/x/msg_authorization/module.go b/x/msg_authorization/module.go new file mode 100644 index 000000000000..d552162c4c47 --- /dev/null +++ b/x/msg_authorization/module.go @@ -0,0 +1 @@ +package msg_authorization From 2dd9fd66bb6e9478ee8dcb555848a9c1499aaafc Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 127/162] Implemented sdk.msg methods --- x/msg_authorization/alias.go | 25 ++++++ x/msg_authorization/client/cli/query.go | 1 - x/msg_authorization/client/cli/tx.go | 4 +- x/msg_authorization/exported/keeper.go | 8 +- x/msg_authorization/handler.go | 14 ++++ .../internal/types/capabilities.go | 4 + x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++- 7 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 x/msg_authorization/alias.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go new file mode 100644 index 000000000000..bf996e6bff6d --- /dev/null +++ b/x/msg_authorization/alias.go @@ -0,0 +1,25 @@ +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 ( + NewKeeper = keeper.NewKeeper +) + +type ( + Keeper = keeper.Keeper + Capability = types.Capability + MsgGrantAuthorization = types.MsgGrantAuthorization + MsgRevokeAuthorization = types.MsgRevokeAuthorization + MsgExecDelegated = types.MsgExecDelegated +) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index c56a80569b50..7f1e458cd3ab 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -1,2 +1 @@ package cli - diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index b6e782d44f35..47244a825235 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -3,8 +3,8 @@ package cli import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/spf13/cobra" ) // GetTxCmd returns the transaction commands for this module @@ -33,4 +33,4 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { //TODO return nil -} \ No newline at end of file +} diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 3ca0e9d31005..ead70b2428ac 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,9 @@ package exported import ( - "time" sdk "github.com/cosmos/cosmos-sdk/types" - + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper interface { @@ -12,12 +12,12 @@ type Keeper interface { // Grants the provided capability to the grantee on the granter's account with the provided expiration time // If there is an existing capability grant for the same sdk.Msg type, this grant overwrites that. - Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability Capability, expiration time.Time) + Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) //Revokes any capability 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 Capability (or nil), with the expiration time, // granted to the grantee by the granter for the provided msg type. - GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap Capability, expiration time.Time) + GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) } diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index d552162c4c47..e8fb5a08a29d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1 +1,15 @@ package msg_authorization + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func NewHandler(k Keeper) sdk.Handler { + return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { + ctx = ctx.WithEventManager(sdk.NewEventManager()) + + switch msg := msg.(type) { + //TODO + } + } +} diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index ab1254f4c2be..17d0d6992f96 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1 +1,5 @@ package types + +type Capability interface { + //TODO +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From eb1ee1222bcc2c17acc26405c9f6a0634c6ed031 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 128/162] Created handler for authorization --- x/msg_authorization/handler.go | 28 +++++++++++++++++-- x/msg_authorization/internal/keeper/keeper.go | 2 ++ x/msg_authorization/internal/types/codec.go | 9 ++++++ x/msg_authorization/internal/types/msg.go | 9 ++++-- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 x/msg_authorization/internal/types/codec.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index e8fb5a08a29d..9684b5c01187 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -1,15 +1,39 @@ package msg_authorization import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) func NewHandler(k Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { ctx = ctx.WithEventManager(sdk.NewEventManager()) - switch msg := msg.(type) { - //TODO + case MsgGrantAuthorization: + return handleMsgGrantAuthorization(ctx, msg, k) + case MsgRevokeAuthorization: + return handleMsgRevokeAuthorization(ctx, msg, k) + case MsgExecDelegated: + return handleMsgExecDelegated(ctx, msg, k) + default: + errMsg := fmt.Sprintf("unrecognized authorization message type: %T", msg) + return sdk.ErrUnknownRequest(errMsg).Result() } } } + +func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} + +func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result { + //TODO + return sdk.Result{Events: ctx.EventManager().Events()} +} diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 8731cf48790c..a7b70c5d1f03 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -19,3 +19,5 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe router: router, } } + +//TODO implement all keeper methods diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go new file mode 100644 index 000000000000..fe10042d1583 --- /dev/null +++ b/x/msg_authorization/internal/types/codec.go @@ -0,0 +1,9 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" +) + +var ModuleCdc = codec.New() + +//ToDO register concrete diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From 57e3dc3defd4e325c9eb545c91b6410148fa758d Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 129/162] Added validate basic to all messages --- .../internal/types/capabilities.go | 8 +++- x/msg_authorization/internal/types/errors.go | 27 ++++++++++++ .../internal/types/{msg.go => msgs.go} | 43 ++++++++++++------- 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/internal/types/errors.go rename x/msg_authorization/internal/types/{msg.go => msgs.go} (78%) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 17d0d6992f96..3806f3a66684 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,5 +1,11 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" +) + type Capability interface { - //TODO + MsgType() sdk.Msg + Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } diff --git a/x/msg_authorization/internal/types/errors.go b/x/msg_authorization/internal/types/errors.go new file mode 100644 index 000000000000..7e685bba677c --- /dev/null +++ b/x/msg_authorization/internal/types/errors.go @@ -0,0 +1,27 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type CodeType = sdk.CodeType + +const ( + // Default slashing codespace + DefaultCodespace sdk.CodespaceType = ModuleName + + CodeInvalidGranter CodeType = 101 + CodeInvalidGrantee CodeType = 102 + CodeInvalidExpirationTime CodeType = 103 +) + +func ErrInvalidGranter(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGranter, "invalid granter address") +} + +func ErrInvalidGrantee(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidGrantee, "invalid grantee address") +} +func ErrInvalidExpirationTime(codespace sdk.CodespaceType) sdk.Error { + return sdk.NewError(codespace, CodeInvalidExpirationTime, "expiration time of authorization should be more than current time") +} diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msgs.go similarity index 78% rename from x/msg_authorization/internal/types/msg.go rename to x/msg_authorization/internal/types/msgs.go index ce6d370b3a9b..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -28,17 +28,25 @@ func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return ErrInvalidGranter(DefaultCodespace) + } + if msg.Grantee.Empty() { + return ErrInvalidGrantee(DefaultCodespace) + } + if msg.Expiration.Unix() < time.Now().Unix() { + return ErrInvalidExpirationTime(DefaultCodespace) + } + return nil } @@ -64,17 +72,21 @@ func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Granter} } func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO + if msg.Granter.Empty() { + return sdk.ErrInvalidAddress(msg.Granter.String()) + } + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } @@ -96,16 +108,17 @@ func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil + return []sdk.AccAddress{msg.Grantee} } func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) } func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO + if msg.Grantee.Empty() { + return sdk.ErrInvalidAddress(msg.Grantee.String()) + } return nil } From dc3bea4517654325fa6666e8fa64a15fa49f371e Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:23:17 +0530 Subject: [PATCH 130/162] Added base for internal keeper --- x/msg_authorization/internal/keeper/keeper.go | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index a7b70c5d1f03..750f747684d3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -3,6 +3,8 @@ package keeper import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "time" ) type Keeper struct { @@ -20,4 +22,20 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } -//TODO implement all keeper methods +func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { + //TODO + return sdk.Result{} +} + +func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { + //TODO +} + +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { + //TODO +} + +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { + //TODO + return nil, time.Now() +} From 37226fd334b16dd34599f3ed6429b54230a1ee79 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 02:33:25 +0530 Subject: [PATCH 131/162] Added message registration to codec --- x/msg_authorization/internal/keeper/keeper.go | 4 ++-- x/msg_authorization/internal/types/codec.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 750f747684d3..077b6c37c2f3 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -31,11 +31,11 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd //TODO } -func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { +func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { //TODO } -func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { +func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { //TODO return nil, time.Now() } diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index fe10042d1583..d9f8eb7ba7f6 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -6,4 +6,8 @@ import ( var ModuleCdc = codec.New() -//ToDO register concrete +func RegisterCodec(cdc *codec.Codec) { + cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) + cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) + cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) +} From 780a4df387135d52d0bff3a2b6aa6104fcca4425 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 22 Nov 2019 12:43:18 +0530 Subject: [PATCH 132/162] Added Send Capability --- .../internal/types/capabilities.go | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 3806f3a66684..5ce76eb3c43d 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -2,6 +2,7 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" abci "github.com/tendermint/tendermint/abci/types" ) @@ -9,3 +10,29 @@ type Capability interface { MsgType() sdk.Msg Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From 0eead6289bc35fa1ab49290ebeb3a3862c7adb77 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 2 Dec 2019 19:53:06 +0530 Subject: [PATCH 133/162] Added keeper methods for authorization --- x/msg_authorization/internal/keeper/keeper.go | 75 +++++++++++++++++-- .../internal/keeper/keeper_test.go | 1 + .../internal/types/capabilities.go | 16 ++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/keeper_test.go diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 077b6c37c2f3..811451ef002b 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -1,10 +1,13 @@ package keeper import ( + "bytes" + "fmt" + "time" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper struct { @@ -22,20 +25,80 @@ func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router sdk.Router) Keepe } } +func (k Keeper) getActorCapabilityKey(grantee sdk.AccAddress, granter sdk.AccAddress, msg sdk.Msg) []byte { + return []byte(fmt.Sprintf("c/%x/%x/%s/%s", grantee, granter, msg.Route(), msg.Type())) +} + +func (k Keeper) getCapabilityGrant(ctx sdk.Context, actor []byte) (grant types.CapabilityGrant, found bool) { + store := ctx.KVStore(k.storeKey) + bz := store.Get(actor) + if bz == nil { + return grant, false + } + k.cdc.MustUnmarshalBinaryBare(bz, &grant) + return grant, true +} + +func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Capability) { + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, updated.MsgType())) + if !found { + return + } + grant.Capability = updated +} + func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs []sdk.Msg) sdk.Result { - //TODO + var res sdk.Result + for _, msg := range msgs { + signers := msg.GetSigners() + if len(signers) != 1 { + return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + } + granter := signers[0] + if !bytes.Equal(granter, grantee) { + capability, _ := k.GetCapability(ctx, grantee, granter, msg) + if capability == nil { + return sdk.ErrUnauthorized("unauthorized").Result() + } + allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) + if !allow { + return sdk.ErrUnauthorized("unauthorized").Result() + } + if del { + k.Revoke(ctx, grantee, granter, msg) + } else if updated != nil { + k.update(ctx, grantee, granter, updated) + } + } + res = k.router.Route(msg.Route())(ctx, msg) + if !res.IsOK() { + return res + } + } + return sdk.Result{} } func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, capability types.Capability, expiration time.Time) { - //TODO + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinaryBare(types.CapabilityGrant{Capability: capability, Expiration: expiration}) + actor := k.getActorCapabilityKey(grantee, granter, capability.MsgType()) + store.Set(actor, bz) } func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) { - //TODO + store := ctx.KVStore(k.storeKey) + store.Delete(k.getActorCapabilityKey(grantee, granter, msgType)) } func (k Keeper) GetCapability(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType sdk.Msg) (cap types.Capability, expiration time.Time) { - //TODO - return nil, time.Now() + grant, found := k.getCapabilityGrant(ctx, k.getActorCapabilityKey(grantee, granter, msgType)) + if !found { + return nil, time.Time{} + } + if !grant.Expiration.IsZero() && grant.Expiration.Before(ctx.BlockHeader().Time) { + k.Revoke(ctx, grantee, granter, msgType) + return nil, time.Time{} + } + return grant.Capability, grant.Expiration } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go new file mode 100644 index 000000000000..b55569d4a442 --- /dev/null +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -0,0 +1 @@ +package keeper diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index 5ce76eb3c43d..acbb26e1593e 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -3,6 +3,9 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + + "time" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -36,3 +39,16 @@ func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow b } return false, nil, false } + +type CapabilityGrant struct { + Capability Capability + + Expiration time.Time +} + +// GenericCapability grants the permission to execute any transaction of the provided +// sdk.Msg type without restrictions +type GenericCapability struct { + // MsgType is the type of Msg this capability grant allows + MsgType sdk.Msg +} From 9cc5bd9e3cac7a6480ed67f3d26438c00210df56 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 134/162] Added keeper implementations --- x/msg_authorization/alias.go | 36 +++++++++++++--- x/msg_authorization/exported/keeper.go | 3 +- x/msg_authorization/internal/keeper/keeper.go | 6 +-- .../internal/keeper/keeper_test.go | 2 +- .../internal/keeper/test_common.go | 43 +++++++++++++++++++ .../internal/types/capabilities.go | 31 +------------ x/msg_authorization/internal/types/msgs.go | 3 +- .../internal/types/send_capability.go | 34 +++++++++++++++ 8 files changed, 117 insertions(+), 41 deletions(-) create mode 100644 x/msg_authorization/internal/keeper/test_common.go create mode 100644 x/msg_authorization/internal/types/send_capability.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index bf996e6bff6d..701280c3c5f2 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -1,3 +1,8 @@ +// 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 ( @@ -6,20 +11,39 @@ import ( ) const ( - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - QuerierRoute = types.QuerierRoute + DefaultCodespace = types.DefaultCodespace + CodeInvalidGranter = types.CodeInvalidGranter + CodeInvalidGrantee = types.CodeInvalidGrantee + CodeInvalidExpirationTime = types.CodeInvalidExpirationTime + ModuleName = types.ModuleName + StoreKey = types.StoreKey + RouterKey = types.RouterKey + QuerierRoute = types.QuerierRoute ) var ( - NewKeeper = keeper.NewKeeper + // functions aliases + RegisterCodec = types.RegisterCodec + ErrInvalidGranter = types.ErrInvalidGranter + ErrInvalidGrantee = types.ErrInvalidGrantee + ErrInvalidExpirationTime = types.ErrInvalidExpirationTime + NewMsgGrantAuthorization = types.NewMsgGrantAuthorization + NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization + NewMsgExecDelegated = types.NewMsgExecDelegated + NewKeeper = keeper.NewKeeper + + // variable aliases + ModuleCdc = types.ModuleCdc ) type ( - Keeper = keeper.Keeper Capability = types.Capability + SendCapability = types.SendCapability + CapabilityGrant = types.CapabilityGrant + GenericCapability = types.GenericCapability + CodeType = types.CodeType MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization MsgExecDelegated = types.MsgExecDelegated + Keeper = keeper.Keeper ) diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index ead70b2428ac..69d581b5f182 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,10 @@ package exported import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" - "time" ) type Keeper interface { diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 811451ef002b..acb95937b9b9 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -52,17 +52,17 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] for _, msg := range msgs { signers := msg.GetSigners() if len(signers) != 1 { - return sdk.ErrUnknownRequest("can only dispatch a delegated msg with 1 signer").Result() + return sdk.ErrUnknownRequest("authorization can be given to msg with only one signer").Result() } granter := signers[0] if !bytes.Equal(granter, grantee) { capability, _ := k.GetCapability(ctx, grantee, granter, msg) if capability == nil { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized("authorization not found").Result() } allow, updated, del := capability.Accept(msg, ctx.BlockHeader()) if !allow { - return sdk.ErrUnauthorized("unauthorized").Result() + return sdk.ErrUnauthorized(" ").Result() } if del { k.Revoke(ctx, grantee, granter, msg) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index b55569d4a442..9429264902a9 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1 @@ -package keeper +package keeper_test diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go new file mode 100644 index 000000000000..97a0e177613c --- /dev/null +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -0,0 +1,43 @@ +package keeper + +import ( + "github.com/tendermint/tendermint/crypto/ed25519" + + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type testInput struct { + cdc *codec.Codec + ctx sdk.Context + ak auth.AccountKeeper + pk params.Keeper + bk bank.Keeper + dk Keeper + router sdk.Router +} + +func setupTestInput() testInput { + db := dbm.NewMemDB() + + cdc := codec.New() + auth.RegisterCodec(cdc) + bank.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) + + //TODO create test input + return testInput{} +} + +var ( + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) +) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go index acbb26e1593e..8e47d7c461e4 100644 --- a/x/msg_authorization/internal/types/capabilities.go +++ b/x/msg_authorization/internal/types/capabilities.go @@ -1,11 +1,10 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - "time" + sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/tendermint/abci/types" ) @@ -14,32 +13,6 @@ type Capability interface { Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) } -type SendCapability struct { - // SpendLimit specifies the maximum amount of tokens that can be spent - // by this capability and will be updated as tokens are spent. If it is - // empty, there is no spend limit and any amount of coins can be spent. - SpendLimit sdk.Coins -} - -func (capability SendCapability) MsgType() sdk.Msg { - return bank.MsgSend{} -} - -func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { - switch msg := msg.(type) { - case bank.MsgSend: - limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) - if isNegative { - return false, nil, false - } - if limitLeft.IsZero() { - return true, nil, true - } - return true, SendCapability{SpendLimit: limitLeft}, false - } - return false, nil, false -} - type CapabilityGrant struct { Capability Capability diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's diff --git a/x/msg_authorization/internal/types/send_capability.go b/x/msg_authorization/internal/types/send_capability.go new file mode 100644 index 000000000000..230643102e4b --- /dev/null +++ b/x/msg_authorization/internal/types/send_capability.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" + + abci "github.com/tendermint/tendermint/abci/types" +) + +type SendCapability struct { + // SpendLimit specifies the maximum amount of tokens that can be spent + // by this capability and will be updated as tokens are spent. If it is + // empty, there is no spend limit and any amount of coins can be spent. + SpendLimit sdk.Coins +} + +func (capability SendCapability) MsgType() sdk.Msg { + return bank.MsgSend{} +} + +func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { + switch msg := msg.(type) { + case bank.MsgSend: + limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) + if isNegative { + return false, nil, false + } + if limitLeft.IsZero() { + return true, nil, true + } + return true, SendCapability{SpendLimit: limitLeft}, false + } + return false, nil, false +} From 86490d17335c364bd52d5df276999e3c0192084b Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 5 Dec 2019 20:52:08 +0530 Subject: [PATCH 135/162] Added test input for keeper --- .../internal/keeper/test_common.go | 75 +++++++++++++++---- x/msg_authorization/test_common.go | 3 + 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 x/msg_authorization/test_common.go diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 97a0e177613c..988e873a6c51 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,28 +1,40 @@ package keeper import ( + "testing" + "time" + + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/cosmos/cosmos-sdk/x/params" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/supply" ) -type testInput struct { - cdc *codec.Codec - ctx sdk.Context - ak auth.AccountKeeper - pk params.Keeper - bk bank.Keeper - dk Keeper - router sdk.Router -} +func makeTestCodec() *codec.Codec { + var cdc = codec.New() + auth.RegisterCodec(cdc) + types.RegisterCodec(cdc) + supply.RegisterCodec(cdc) + staking.RegisterCodec(cdc) + sdk.RegisterCodec(cdc) + codec.RegisterCrypto(cdc) -func setupTestInput() testInput { + return cdc +} +func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -31,13 +43,44 @@ func setupTestInput() testInput { sdk.RegisterCodec(cdc) codec.RegisterCrypto(cdc) - //TODO create test input - return testInput{} + keyAcc := sdk.NewKVStoreKey(auth.StoreKey) + keyParams := sdk.NewKVStoreKey(params.StoreKey) + keyAuthorization := sdk.NewKVStoreKey(types.StoreKey) + tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) + + ms := store.NewCommitMultiStore(db) + ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) + + err := ms.LoadLatestVersion() + require.Nil(t, err) + + ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) + cdc = makeTestCodec() + + blacklistedAddrs := make(map[string]bool) + + paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace) + authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) + bankKeeper := bank.NewBaseKeeper(authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs) + bankKeeper.SetSendEnabled(ctx, true) + + router := baseapp.NewRouter() + router.AddRoute("bank", bank.NewHandler(bankKeeper)) + + authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) + + authKeeper.SetParams(ctx, auth.DefaultParams()) + + return ctx, authKeeper, paramsKeeper, bankKeeper, authorizationKeeper, router } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) + senderPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() + authorizedPub = ed25519.GenPrivKey().PubKey() + senderAddr = sdk.AccAddress(senderPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) + authorizedAddr = sdk.AccAddress(authorizedPub.Address()) ) diff --git a/x/msg_authorization/test_common.go b/x/msg_authorization/test_common.go new file mode 100644 index 000000000000..d0cb9b8dfcc2 --- /dev/null +++ b/x/msg_authorization/test_common.go @@ -0,0 +1,3 @@ +package msg_authorization + +//TODO create a mock app to demonstrate msg_authorization functionality From fdcf6fa97ce486e2a5663672556c8646af87e53d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 9 Dec 2019 20:06:35 +0530 Subject: [PATCH 136/162] Added tests for keeper --- x/msg_authorization/handler.go | 23 +++++++- .../internal/keeper/keeper_test.go | 55 ++++++++++++++++++- .../internal/keeper/test_common.go | 20 +++---- x/msg_authorization/internal/types/events.go | 13 +++++ 4 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 x/msg_authorization/internal/types/events.go diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 9684b5c01187..c1a496fe679d 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -4,6 +4,7 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution/types" ) func NewHandler(k Keeper) sdk.Handler { @@ -24,12 +25,30 @@ func NewHandler(k Keeper) sdk.Handler { } func handleMsgGrantAuthorization(ctx sdk.Context, msg MsgGrantAuthorization, k Keeper) sdk.Result { - //TODO + k.Grant(ctx, msg.Grantee, msg.Granter, msg.Capability, msg.Expiration) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k Keeper) sdk.Result { - //TODO + k.Revoke(ctx, msg.Grantee, msg.Granter, msg.CapabilityMsgType) + + ctx.EventManager().EmitEvent( + sdk.NewEvent( + sdk.EventTypeMessage, + sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), + sdk.NewAttribute(sdk.AttributeKeySender, msg.Granter.String()), + ), + ) + return sdk.Result{Events: ctx.EventManager().Events()} } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 9429264902a9..a846474bf037 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1 +1,54 @@ -package keeper_test +package keeper + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + "github.com/cosmos/cosmos-sdk/x/params" +) + +type TestSuite struct { + suite.Suite + ctx sdk.Context + accountKeeper auth.AccountKeeper + paramsKeeper params.Keeper + bankKeeper bank.Keeper + keeper Keeper + router sdk.Router +} + +func (s *TestSuite) SetupTest() { + s.ctx, s.accountKeeper, s.paramsKeeper, s.bankKeeper, s.keeper, s.router = SetupTestInput() + +} + +func (s *TestSuite) TestKeeper(t *testing.T) { + err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) + require.Nil(t, err) + require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + + t.Log("Verify that no capability returns nil") + capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + //require.Nil(t, expiration) + now := s.ctx.BlockHeader().Time + require.NotNil(t, now) + + newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) + capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + require.NotNil(t, capability) + require.NotNil(t, expiration) + //TODO add more cases +} + +func TestTestSuite(t *testing.T) { + suite.Run(t, new(TestSuite)) +} diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 988e873a6c51..f7874ff21cfe 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -1,10 +1,8 @@ package keeper import ( - "testing" "time" - "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/ed25519" abci "github.com/tendermint/tendermint/abci/types" @@ -34,7 +32,7 @@ func makeTestCodec() *codec.Codec { return cdc } -func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { +func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, sdk.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -51,10 +49,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db) + ms.MountStoreWithDB(keyAuthorization, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db) - err := ms.LoadLatestVersion() - require.Nil(t, err) + ms.LoadLatestVersion() ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger()) cdc = makeTestCodec() @@ -77,10 +75,10 @@ func setupTestInput(t *testing.T) (sdk.Context, auth.AccountKeeper, params.Keepe } var ( - senderPub = ed25519.GenPrivKey().PubKey() - recipientPub = ed25519.GenPrivKey().PubKey() - authorizedPub = ed25519.GenPrivKey().PubKey() - senderAddr = sdk.AccAddress(senderPub.Address()) - recipientAddr = sdk.AccAddress(recipientPub.Address()) - authorizedAddr = sdk.AccAddress(authorizedPub.Address()) + granteePub = ed25519.GenPrivKey().PubKey() + granterPub = ed25519.GenPrivKey().PubKey() + recepientPub = ed25519.GenPrivKey().PubKey() + granteeAddr = sdk.AccAddress(granteePub.Address()) + granterAddr = sdk.AccAddress(granterPub.Address()) + recepientAddr = sdk.AccAddress(recepientPub.Address()) ) diff --git a/x/msg_authorization/internal/types/events.go b/x/msg_authorization/internal/types/events.go new file mode 100644 index 000000000000..823ad3ce8cc5 --- /dev/null +++ b/x/msg_authorization/internal/types/events.go @@ -0,0 +1,13 @@ +package types + +// msg_authorization module events +const ( + EventGrantAuthorization = "grant-authorization" + EventRevokeAuthorization = "revoke-authorization" + EventExecuteAuthorization = "execute-authorization" + + AttributeKeyGranteeAddress = "grantee" + AttributeKeyGranterAddress = "granter" + + AttributeValueCategory = ModuleName +) From 6a4814cc97ad2f09be0729206ac1213e25d61b64 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Mon, 9 Dec 2019 21:11:00 +0530 Subject: [PATCH 137/162] Added grant,revoke to cli --- x/msg_authorization/client/cli/tx.go | 73 ++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 47244a825235..9242e3f61dff 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -1,8 +1,13 @@ package cli import ( + "bufio" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" ) @@ -12,6 +17,7 @@ func GetTxCmd(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, @@ -26,11 +32,70 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { } func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "grant", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var capability types.Capability + err = cdc.UnmarshalJSON([]byte(args[1]), &capability) + if err != nil { + return err + } + + msg := types.NewMsgGrantAuthorization(granter, grantee, capability, expiration) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + + }, + } + return cmd } func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command { - //TODO - return nil + cmd := &cobra.Command{ + Use: "revoke", + 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(utils.GetTxEncoder(cdc)) + cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc) + + granter := cliCtx.FromAddress + grantee, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + return err + } + + var msgAuthorized sdk.Msg + err = cdc.UnmarshalJSON([]byte(args[1]), &msgAuthorized) + if err != nil { + return err + } + + msg := types.NewMsgRevokeAuthorization(granter, grantee, msgAuthorized) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, []sdk.Msg{msg}) + }, + } + return cmd } From 5d091800bb4a68335eea4f8489183033ece16f99 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 10 Dec 2019 13:16:55 +0530 Subject: [PATCH 138/162] Added more test cases for keeper --- .../internal/keeper/keeper_test.go | 43 ++++++++++++++----- x/msg_authorization/internal/types/codec.go | 4 ++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index a846474bf037..76fafcdb3e52 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -4,7 +4,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,24 +28,48 @@ func (s *TestSuite) SetupTest() { } -func (s *TestSuite) TestKeeper(t *testing.T) { +func (s *TestSuite) TestKeeper() { err := s.bankKeeper.SetCoins(s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000))) - require.Nil(t, err) - require.True(t, s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) + s.Require().Nil(err) + s.Require().True(s.bankKeeper.GetCoins(s.ctx, granterAddr).IsEqual(sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - t.Log("Verify that no capability returns nil") + s.T().Log("Verify that no capability returns nil") capability, _ := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) s.Require().Nil(capability) //require.Nil(t, expiration) now := s.ctx.BlockHeader().Time - require.NotNil(t, now) + s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) + s.T().Log("Verify if expired capability is rejected") s.keeper.Grant(s.ctx, granterAddr, granteeAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(-1*time.Hour)) - capability, expiration := s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) - require.NotNil(t, capability) - require.NotNil(t, expiration) - //TODO add more cases + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify if capability is accepted") + s.keeper.Grant(s.ctx, granteeAddr, granterAddr, types.SendCapability{SpendLimit: newCoins}, now.Add(time.Hour)) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + s.Require().Equal(capability.MsgType(), bank.MsgSend{}) + + s.T().Log("Verify fetching capability with wrong msg type fails") + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify fetching capability with wrong grantee fails") + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke fails with wrong information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + s.Require().Nil(capability) + + s.T().Log("Verify revoke executes with correct information") + s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}) + capability, _ = s.keeper.GetCapability(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}) + s.Require().NotNil(capability) + } func TestTestSuite(t *testing.T) { diff --git a/x/msg_authorization/internal/types/codec.go b/x/msg_authorization/internal/types/codec.go index d9f8eb7ba7f6..b6003860776a 100644 --- a/x/msg_authorization/internal/types/codec.go +++ b/x/msg_authorization/internal/types/codec.go @@ -10,4 +10,8 @@ func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(MsgGrantAuthorization{}, "cosmos-sdk/GrantAuthorization", nil) cdc.RegisterConcrete(MsgRevokeAuthorization{}, "cosmos-sdk/RevokeAuthorization", nil) cdc.RegisterConcrete(MsgExecDelegated{}, "cosmos-sdk/ExecDelegated", nil) + cdc.RegisterConcrete(SendCapability{}, "cosmos-sdk/SendCapability", nil) + cdc.RegisterConcrete(CapabilityGrant{}, "cosmos-sdk/CapabilityGrant", nil) + + cdc.RegisterInterface((*Capability)(nil), nil) } From 0ace115880e312bd85df2a1b1615e22969d77217 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 10 Dec 2019 19:53:49 +0530 Subject: [PATCH 139/162] Added expiration flag to grant tx --- x/msg_authorization/client/cli/flags.go | 3 +++ x/msg_authorization/client/cli/tx.go | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 x/msg_authorization/client/cli/flags.go diff --git a/x/msg_authorization/client/cli/flags.go b/x/msg_authorization/client/cli/flags.go new file mode 100644 index 000000000000..a77534c3551f --- /dev/null +++ b/x/msg_authorization/client/cli/flags.go @@ -0,0 +1,3 @@ +package cli + +const FlagExpiration = "expiration" diff --git a/x/msg_authorization/client/cli/tx.go b/x/msg_authorization/client/cli/tx.go index 9242e3f61dff..ff33d7fa57ae 100644 --- a/x/msg_authorization/client/cli/tx.go +++ b/x/msg_authorization/client/cli/tx.go @@ -2,6 +2,8 @@ package cli import ( "bufio" + "time" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" @@ -10,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // GetTxCmd returns the transaction commands for this module @@ -53,6 +56,11 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { 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, capability, expiration) if err := msg.ValidateBasic(); err != nil { @@ -63,6 +71,8 @@ func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command { }, } + cmd.Flags().String(FlagExpiration, "9999-12-31 23:59:59.52Z", "The time upto which the authorization is active for the user") + return cmd } From 5bbec41a42f04ac3dd7e8aadcf9a04913ae95544 Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 140/162] Added code skeleton for msg authorization --- x/msg_authorization/internal/types/msg.go | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} From af18f8af146e4f7f47990616754014ce67b24127 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 141/162] Implemented sdk.msg methods --- x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++++++- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From d1d8e0acd39a77e3248a960cc9451ba84cf692e2 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 142/162] Created handler for authorization --- x/msg_authorization/internal/types/msg.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From 3c6dc866a6de8d90a2f44a738e89c03ce09cac22 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 143/162] Added validate basic to all messages --- x/msg_authorization/internal/types/msg.go | 111 --------------------- x/msg_authorization/internal/types/msgs.go | 3 +- 2 files changed, 1 insertion(+), 113 deletions(-) delete mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go deleted file mode 100644 index ce6d370b3a9b..000000000000 --- a/x/msg_authorization/internal/types/msg.go +++ /dev/null @@ -1,111 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "time" -) - -// MsgGrant grants the provided capability to the grantee on the granter's -// account with the provided expiration time. -type MsgGrantAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Capability Capability `json:"capability"` - // Expiration specifies the expiration time of the grant - Expiration time.Time `json:"expiration"` -} - -func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { - return MsgGrantAuthorization{ - Granter: granter, - Grantee: grantee, - Capability: capability, - Expiration: expiration, - } -} - -func (msg MsgGrantAuthorization) Route() string { return RouterKey } -func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } - -func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the -// granter's account with that has been granted to the grantee. -type MsgRevokeAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. - // i.e. this is what `Capability.MsgType()` returns - CapabilityMsgType sdk.Msg `json:"capability_msg_type"` -} - -func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { - return MsgRevokeAuthorization{ - Granter: granter, - Grantee: grantee, - CapabilityMsgType: capabilityMsgType, - } -} - -func (msg MsgRevokeAuthorization) Route() string { return RouterKey } -func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } - -func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgExecDelegated attempts to execute the provided messages using -// capabilities granted to the grantee. Each message should have only -// one signer corresponding to the granter of the capability. -type MsgExecDelegated struct { - Grantee sdk.AccAddress `json:"grantee"` - Msgs []sdk.Msg `json:"msg"` -} - -func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { - return MsgExecDelegated{ - Grantee: grantee, - Msgs: msgs, - } -} -func (msg MsgExecDelegated) Route() string { return RouterKey } -func (msg MsgExecDelegated) Type() string { return "exec_delegated" } - -func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO - return nil -} diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 87f2681b5fc7..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,9 +1,8 @@ package types import ( - "time" - sdk "github.com/cosmos/cosmos-sdk/types" + "time" ) // MsgGrant grants the provided capability to the grantee on the granter's From d5f3276ea93af96de5e833056e2918ca305a5c58 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 144/162] Added keeper implementations --- x/msg_authorization/internal/types/msgs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's From d441bd3f41f3274c89f0e7b18556d3b36fb2da24 Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 145/162] Added code skeleton for msg authorization --- x/msg_authorization/internal/types/msg.go | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} From 0b9abeee373aa264838ee9367623b8b988e3894a Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 146/162] Implemented sdk.msg methods --- x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++++++- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From b8c56fab9f546b2af0868a6c9c73fb1702246cae Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 147/162] Created handler for authorization --- x/msg_authorization/internal/types/msg.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From a8794342667368f62b7be5e4976a433d2880adf5 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 148/162] Added validate basic to all messages --- x/msg_authorization/internal/types/msg.go | 111 --------------------- x/msg_authorization/internal/types/msgs.go | 3 +- 2 files changed, 1 insertion(+), 113 deletions(-) delete mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go deleted file mode 100644 index ce6d370b3a9b..000000000000 --- a/x/msg_authorization/internal/types/msg.go +++ /dev/null @@ -1,111 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "time" -) - -// MsgGrant grants the provided capability to the grantee on the granter's -// account with the provided expiration time. -type MsgGrantAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Capability Capability `json:"capability"` - // Expiration specifies the expiration time of the grant - Expiration time.Time `json:"expiration"` -} - -func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { - return MsgGrantAuthorization{ - Granter: granter, - Grantee: grantee, - Capability: capability, - Expiration: expiration, - } -} - -func (msg MsgGrantAuthorization) Route() string { return RouterKey } -func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } - -func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the -// granter's account with that has been granted to the grantee. -type MsgRevokeAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. - // i.e. this is what `Capability.MsgType()` returns - CapabilityMsgType sdk.Msg `json:"capability_msg_type"` -} - -func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { - return MsgRevokeAuthorization{ - Granter: granter, - Grantee: grantee, - CapabilityMsgType: capabilityMsgType, - } -} - -func (msg MsgRevokeAuthorization) Route() string { return RouterKey } -func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } - -func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgExecDelegated attempts to execute the provided messages using -// capabilities granted to the grantee. Each message should have only -// one signer corresponding to the granter of the capability. -type MsgExecDelegated struct { - Grantee sdk.AccAddress `json:"grantee"` - Msgs []sdk.Msg `json:"msg"` -} - -func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { - return MsgExecDelegated{ - Grantee: grantee, - Msgs: msgs, - } -} -func (msg MsgExecDelegated) Route() string { return RouterKey } -func (msg MsgExecDelegated) Type() string { return "exec_delegated" } - -func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO - return nil -} diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 87f2681b5fc7..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,9 +1,8 @@ package types import ( - "time" - sdk "github.com/cosmos/cosmos-sdk/types" + "time" ) // MsgGrant grants the provided capability to the grantee on the granter's From 6fa240a7f3fae22b1060e6dd8a841644d4a7035c Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 149/162] Added keeper implementations --- x/msg_authorization/internal/types/msgs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's From 08bbe52eefa722b338257edf4f656e1c11b03b84 Mon Sep 17 00:00:00 2001 From: saren Date: Tue, 19 Nov 2019 03:52:58 +0530 Subject: [PATCH 150/162] Added code skeleton for msg authorization --- x/msg_authorization/internal/types/msg.go | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go new file mode 100644 index 000000000000..2969b556bd47 --- /dev/null +++ b/x/msg_authorization/internal/types/msg.go @@ -0,0 +1,34 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "time" +) + +// MsgGrant grants the provided capability to the grantee on the granter's +// account with the provided expiration time. +type MsgGrant struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + Capability Capability `json:"capability"` + // Expiration specifies the expiration time of the grant + Expiration time.Time `json:"expiration"` +} + +// MsgRevoke revokes any capability with the provided sdk.Msg type on the +// granter's account with that has been granted to the grantee. +type MsgRevoke struct { + Granter sdk.AccAddress `json:"granter"` + Grantee sdk.AccAddress `json:"grantee"` + // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. + // i.e. this is what `Capability.MsgType()` returns + CapabilityMsgType sdk.Msg `json:"capability_msg_type"` +} + +// MsgExecDelegated attempts to execute the provided messages using +// capabilities granted to the grantee. Each message should have only +// one signer corresponding to the granter of the capability. +type MsgExecDelegated struct { + Grantee sdk.AccAddress `json:"grantee"` + Msgs []sdk.Msg `json:"msg"` +} From 7c3859c3f8f984cefb6ca8c47fd01f7fcaf1ac97 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Tue, 19 Nov 2019 05:28:52 +0530 Subject: [PATCH 151/162] Implemented sdk.msg methods --- x/msg_authorization/internal/types/msg.go | 80 ++++++++++++++++++++++- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index 2969b556bd47..ae4fce36e35b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -7,7 +7,7 @@ import ( // MsgGrant grants the provided capability to the grantee on the granter's // account with the provided expiration time. -type MsgGrant struct { +type MsgGrantAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` Capability Capability `json:"capability"` @@ -15,9 +15,33 @@ type MsgGrant struct { Expiration time.Time `json:"expiration"` } -// MsgRevoke revokes any capability with the provided sdk.Msg type on the +func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { + return MsgGrantAuthorization{ + Granter: granter, + Grantee: grantee, + Capability: capability, + Expiration: expiration, + } +} + +func (msg MsgGrantAuthorization) Route() string { return RouterKey } +func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } + +func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { + return nil +} + +func (msg MsgGrantAuthorization) GetSignBytes() []byte { + return nil +} + +func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + return nil +} + +// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the // granter's account with that has been granted to the grantee. -type MsgRevoke struct { +type MsgRevokeAuthorization struct { Granter sdk.AccAddress `json:"granter"` Grantee sdk.AccAddress `json:"grantee"` // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. @@ -25,6 +49,32 @@ type MsgRevoke struct { CapabilityMsgType sdk.Msg `json:"capability_msg_type"` } +func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { + return MsgRevokeAuthorization{ + Granter: granter, + Grantee: grantee, + CapabilityMsgType: capabilityMsgType, + } +} + +func (msg MsgRevokeAuthorization) Route() string { return RouterKey } +func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } + +func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { + //TODO + return nil +} + // MsgExecDelegated attempts to execute the provided messages using // capabilities granted to the grantee. Each message should have only // one signer corresponding to the granter of the capability. @@ -32,3 +82,27 @@ type MsgExecDelegated struct { Grantee sdk.AccAddress `json:"grantee"` Msgs []sdk.Msg `json:"msg"` } + +func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { + return MsgExecDelegated{ + Grantee: grantee, + Msgs: msgs, + } +} +func (msg MsgExecDelegated) Route() string { return RouterKey } +func (msg MsgExecDelegated) Type() string { return "exec_delegated" } + +func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { + //TODO + return nil +} + +func (msg MsgExecDelegated) GetSignBytes() []byte { + //TODO + return nil +} + +func (msg MsgExecDelegated) ValidateBasic() sdk.Error { + //TODO + return nil +} From 533e16c82a6a113346e10b22543e9a3d547d7f3d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Tue, 19 Nov 2019 18:11:59 +0530 Subject: [PATCH 152/162] Created handler for authorization --- x/msg_authorization/internal/types/msg.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go index ae4fce36e35b..ce6d370b3a9b 100644 --- a/x/msg_authorization/internal/types/msg.go +++ b/x/msg_authorization/internal/types/msg.go @@ -27,15 +27,18 @@ func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, ca func (msg MsgGrantAuthorization) Route() string { return RouterKey } func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } -func (msg MsgGrantAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { + //TODO return nil } func (msg MsgGrantAuthorization) GetSignBytes() []byte { + //TODO return nil } func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { + //TODO return nil } @@ -60,7 +63,7 @@ func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, c func (msg MsgRevokeAuthorization) Route() string { return RouterKey } func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } -func (msg MsgRevokeAuthorization) GetSigners() sdk.AccAddress { +func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { //TODO return nil } @@ -92,7 +95,7 @@ func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegate func (msg MsgExecDelegated) Route() string { return RouterKey } func (msg MsgExecDelegated) Type() string { return "exec_delegated" } -func (msg MsgExecDelegated) GetSigners() sdk.AccAddress { +func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { //TODO return nil } From 0a472820c41ca63327aa6d1a973b532e0e8f47d0 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Thu, 21 Nov 2019 00:59:26 +0530 Subject: [PATCH 153/162] Added validate basic to all messages --- x/msg_authorization/internal/types/msg.go | 111 --------------------- x/msg_authorization/internal/types/msgs.go | 3 +- 2 files changed, 1 insertion(+), 113 deletions(-) delete mode 100644 x/msg_authorization/internal/types/msg.go diff --git a/x/msg_authorization/internal/types/msg.go b/x/msg_authorization/internal/types/msg.go deleted file mode 100644 index ce6d370b3a9b..000000000000 --- a/x/msg_authorization/internal/types/msg.go +++ /dev/null @@ -1,111 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "time" -) - -// MsgGrant grants the provided capability to the grantee on the granter's -// account with the provided expiration time. -type MsgGrantAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - Capability Capability `json:"capability"` - // Expiration specifies the expiration time of the grant - Expiration time.Time `json:"expiration"` -} - -func NewMsgGrantAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capability Capability, expiration time.Time) MsgGrantAuthorization { - return MsgGrantAuthorization{ - Granter: granter, - Grantee: grantee, - Capability: capability, - Expiration: expiration, - } -} - -func (msg MsgGrantAuthorization) Route() string { return RouterKey } -func (msg MsgGrantAuthorization) Type() string { return "grant_authorization" } - -func (msg MsgGrantAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgGrantAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgRevokeAuthorization revokes any capability with the provided sdk.Msg type on the -// granter's account with that has been granted to the grantee. -type MsgRevokeAuthorization struct { - Granter sdk.AccAddress `json:"granter"` - Grantee sdk.AccAddress `json:"grantee"` - // CapabilityMsgType is the type of sdk.Msg that the revoked Capability refers to. - // i.e. this is what `Capability.MsgType()` returns - CapabilityMsgType sdk.Msg `json:"capability_msg_type"` -} - -func NewMsgRevokeAuthorization(granter sdk.AccAddress, grantee sdk.AccAddress, capabilityMsgType sdk.Msg) MsgRevokeAuthorization { - return MsgRevokeAuthorization{ - Granter: granter, - Grantee: grantee, - CapabilityMsgType: capabilityMsgType, - } -} - -func (msg MsgRevokeAuthorization) Route() string { return RouterKey } -func (msg MsgRevokeAuthorization) Type() string { return "revoke_authorization" } - -func (msg MsgRevokeAuthorization) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgRevokeAuthorization) ValidateBasic() sdk.Error { - //TODO - return nil -} - -// MsgExecDelegated attempts to execute the provided messages using -// capabilities granted to the grantee. Each message should have only -// one signer corresponding to the granter of the capability. -type MsgExecDelegated struct { - Grantee sdk.AccAddress `json:"grantee"` - Msgs []sdk.Msg `json:"msg"` -} - -func NewMsgExecDelegated(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExecDelegated { - return MsgExecDelegated{ - Grantee: grantee, - Msgs: msgs, - } -} -func (msg MsgExecDelegated) Route() string { return RouterKey } -func (msg MsgExecDelegated) Type() string { return "exec_delegated" } - -func (msg MsgExecDelegated) GetSigners() []sdk.AccAddress { - //TODO - return nil -} - -func (msg MsgExecDelegated) GetSignBytes() []byte { - //TODO - return nil -} - -func (msg MsgExecDelegated) ValidateBasic() sdk.Error { - //TODO - return nil -} diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 87f2681b5fc7..8f6c65eb6261 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,9 +1,8 @@ package types import ( - "time" - sdk "github.com/cosmos/cosmos-sdk/types" + "time" ) // MsgGrant grants the provided capability to the grantee on the granter's From 3a5842d9b13a07f667037fe016a24dae6afcf30f Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Wed, 4 Dec 2019 21:40:08 +0530 Subject: [PATCH 154/162] Added keeper implementations --- x/msg_authorization/internal/types/msgs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/types/msgs.go b/x/msg_authorization/internal/types/msgs.go index 8f6c65eb6261..87f2681b5fc7 100644 --- a/x/msg_authorization/internal/types/msgs.go +++ b/x/msg_authorization/internal/types/msgs.go @@ -1,8 +1,9 @@ package types import ( - sdk "github.com/cosmos/cosmos-sdk/types" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" ) // MsgGrant grants the provided capability to the grantee on the granter's From 2564b165094dcb4456f6e7c65d9e8c7cad35a6f9 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 17 Feb 2020 11:28:48 +0530 Subject: [PATCH 155/162] Fix module codec for params keeper --- x/msg_authorization/internal/keeper/test_common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index ffba8c6c7c96..9a25b4f40953 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -60,7 +60,7 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base blacklistedAddrs := make(map[string]bool) - paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams) + paramsKeeper := params.NewKeeper(params.ModuleCdc, keyParams, tkeyParams) authKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount) bankKeeper := bank.NewBaseKeeper(cdc, keyBank, authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) bankKeeper.SetSendEnabled(ctx, true) From 1a905d7324938889db5dc321e4e103c2442c02c3 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 17 Feb 2020 13:23:40 +0530 Subject: [PATCH 156/162] Fix update grant --- x/msg_authorization/internal/keeper/keeper.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 6c5e8aff5347..2992fbb00ebb 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -41,11 +41,15 @@ func (k Keeper) getAuthorizationGrant(ctx sdk.Context, actor []byte) (grant type } func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, updated types.Authorization) { - grant, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, updated.MsgType())) + actor := k.getActorAuthorizationKey(grantee, granter, updated.MsgType()) + grant, found := k.getAuthorizationGrant(ctx, actor) if !found { return } grant.Authorization = updated + + store := ctx.KVStore(k.storeKey) + store.Set(actor, k.cdc.MustMarshalBinaryBare(grant)) } // DispatchActions attempts to execute the provided messages via authorization From 4ff3b727b071a0fa8deacbd2c7f4377e8584e711 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 17 Feb 2020 16:10:09 +0530 Subject: [PATCH 157/162] Avoid duplicate calls --- x/msg_authorization/internal/keeper/keeper.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 2992fbb00ebb..5c281e60717e 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -47,7 +47,6 @@ func (k Keeper) update(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccA return } grant.Authorization = updated - store := ctx.KVStore(k.storeKey) store.Set(actor, k.cdc.MustMarshalBinaryBare(grant)) } @@ -106,11 +105,13 @@ func (k Keeper) Grant(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAd // Revoke method revokes any authorization for the provided message type granted to the grantee by the granter. func (k Keeper) Revoke(ctx sdk.Context, grantee sdk.AccAddress, granter sdk.AccAddress, msgType string) error { store := ctx.KVStore(k.storeKey) - _, found := k.getAuthorizationGrant(ctx, k.getActorAuthorizationKey(grantee, granter, msgType)) + actor := k.getActorAuthorizationKey(grantee, granter, msgType) + _, found := k.getAuthorizationGrant(ctx, actor) if !found { return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } - store.Delete(k.getActorAuthorizationKey(grantee, granter, msgType)) + store.Delete(actor) + return nil } From 2328e44c977f1159e8f9e34e2a7e0fe240da0d2c Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 17 Feb 2020 16:44:37 +0530 Subject: [PATCH 158/162] Remove debug comments --- x/msg_authorization/internal/keeper/keeper.go | 1 - .../internal/keeper/keeper_test.go | 5 --- .../internal/types/capabilities.go | 27 --------------- .../internal/types/send_authorization.go | 3 -- .../internal/types/send_capability.go | 34 ------------------- 5 files changed, 70 deletions(-) delete mode 100644 x/msg_authorization/internal/types/capabilities.go delete mode 100644 x/msg_authorization/internal/types/send_capability.go diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index 6c5e8aff5347..bf1899ea015d 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -65,7 +65,6 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } allow, updated, del := authorization.Accept(msg, ctx.BlockHeader()) - fmt.Println("inside accept: ", allow, updated, del) if !allow { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "authorization not found") } diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 8e4ceb6451d1..0744c94c7f74 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -1,8 +1,6 @@ package keeper import ( - "fmt" - "github.com/kr/pretty" "testing" "time" @@ -102,7 +100,6 @@ func (s *TestSuite) TestKeeperFees() { s.T().Log("verify dispatch fails with invalid authorization") result, error := s.keeper.DispatchActions(s.ctx, granteeAddr, msgs.Msgs) - s.T().Log(error.Error()) s.Require().Nil(result) s.Require().NotNil(error) @@ -116,7 +113,6 @@ func (s *TestSuite) TestKeeperFees() { result, error = s.keeper.DispatchActions(s.ctx, granteeAddr, msgs.Msgs) s.Require().NotNil(result) s.Require().Nil(error) - fmt.Printf("%# v", pretty.Formatter(result)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) @@ -138,7 +134,6 @@ func (s *TestSuite) TestKeeperFees() { result, error = s.keeper.DispatchActions(s.ctx, granteeAddr, msgs.Msgs) s.Require().Nil(result) s.Require().NotNil(error) - fmt.Printf("%# v", pretty.Formatter(error)) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) diff --git a/x/msg_authorization/internal/types/capabilities.go b/x/msg_authorization/internal/types/capabilities.go deleted file mode 100644 index 8e47d7c461e4..000000000000 --- a/x/msg_authorization/internal/types/capabilities.go +++ /dev/null @@ -1,27 +0,0 @@ -package types - -import ( - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" - - abci "github.com/tendermint/tendermint/abci/types" -) - -type Capability interface { - MsgType() sdk.Msg - Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) -} - -type CapabilityGrant struct { - Capability Capability - - Expiration time.Time -} - -// GenericCapability grants the permission to execute any transaction of the provided -// sdk.Msg type without restrictions -type GenericCapability struct { - // MsgType is the type of Msg this capability grant allows - MsgType sdk.Msg -} diff --git a/x/msg_authorization/internal/types/send_authorization.go b/x/msg_authorization/internal/types/send_authorization.go index ec898c61087d..c1385f2841cc 100644 --- a/x/msg_authorization/internal/types/send_authorization.go +++ b/x/msg_authorization/internal/types/send_authorization.go @@ -1,7 +1,6 @@ package types import ( - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" abci "github.com/tendermint/tendermint/abci/types" @@ -29,8 +28,6 @@ func (authorization SendAuthorization) Accept(msg sdk.Msg, block abci.Header) (a return true, nil, true } - fmt.Println(limitLeft, "limitLeft") - return true, SendAuthorization{SpendLimit: limitLeft}, false } return false, nil, false diff --git a/x/msg_authorization/internal/types/send_capability.go b/x/msg_authorization/internal/types/send_capability.go deleted file mode 100644 index 230643102e4b..000000000000 --- a/x/msg_authorization/internal/types/send_capability.go +++ /dev/null @@ -1,34 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/bank" - - abci "github.com/tendermint/tendermint/abci/types" -) - -type SendCapability struct { - // SpendLimit specifies the maximum amount of tokens that can be spent - // by this capability and will be updated as tokens are spent. If it is - // empty, there is no spend limit and any amount of coins can be spent. - SpendLimit sdk.Coins -} - -func (capability SendCapability) MsgType() sdk.Msg { - return bank.MsgSend{} -} - -func (capability SendCapability) Accept(msg sdk.Msg, block abci.Header) (allow bool, updated Capability, delete bool) { - switch msg := msg.(type) { - case bank.MsgSend: - limitLeft, isNegative := capability.SpendLimit.SafeSub(msg.Amount) - if isNegative { - return false, nil, false - } - if limitLeft.IsZero() { - return true, nil, true - } - return true, SendCapability{SpendLimit: limitLeft}, false - } - return false, nil, false -} From 2103faf9c72f98877f05732d0c62f9d6992c0d4d Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Mon, 17 Feb 2020 18:19:03 +0530 Subject: [PATCH 159/162] Renamed execdelegate to execauthorized --- x/msg_authorization/alias.go | 4 ++-- x/msg_authorization/handler.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index debb936678e8..bb1d58f5988d 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -25,7 +25,7 @@ var ( ErrInvalidExpirationTime = types.ErrInvalidExpirationTime NewMsgGrantAuthorization = types.NewMsgGrantAuthorization NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization - NewMsgExecDelegated = types.NewMsgExecAuthorized + NewMsgExecAuthorized = types.NewMsgExecAuthorized NewKeeper = keeper.NewKeeper NewRouter = types.NewRouter @@ -40,6 +40,6 @@ type ( GenericAuthorization = types.GenericAuthorization MsgGrantAuthorization = types.MsgGrantAuthorization MsgRevokeAuthorization = types.MsgRevokeAuthorization - MsgExecDelegated = types.MsgExecAuthorized + MsgExecAuthorized = types.MsgExecAuthorized Keeper = keeper.Keeper ) diff --git a/x/msg_authorization/handler.go b/x/msg_authorization/handler.go index 4fa893ee197f..62f111af5b6f 100644 --- a/x/msg_authorization/handler.go +++ b/x/msg_authorization/handler.go @@ -14,8 +14,8 @@ func NewHandler(k Keeper) sdk.Handler { return handleMsgGrantAuthorization(ctx, msg, k) case MsgRevokeAuthorization: return handleMsgRevokeAuthorization(ctx, msg, k) - case MsgExecDelegated: - return handleMsgExecDelegated(ctx, msg, k) + case MsgExecAuthorized: + return handleMsgExecAuthorized(ctx, msg, k) default: return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized authorization message type: %T", msg) } @@ -57,6 +57,6 @@ func handleMsgRevokeAuthorization(ctx sdk.Context, msg MsgRevokeAuthorization, k return &sdk.Result{Events: ctx.EventManager().Events()}, nil } -func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) (*sdk.Result, error) { +func handleMsgExecAuthorized(ctx sdk.Context, msg MsgExecAuthorized, k Keeper) (*sdk.Result, error) { return k.DispatchActions(ctx, msg.Grantee, msg.Msgs) } From 03d3e6df6114be9520e584cf4cf9505b08fb611c Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 17 Feb 2020 19:19:17 +0530 Subject: [PATCH 160/162] Fix gofmt --- x/msg_authorization/exported/keeper.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/msg_authorization/exported/keeper.go b/x/msg_authorization/exported/keeper.go index 4c8cd97fd376..d23bfd32453a 100644 --- a/x/msg_authorization/exported/keeper.go +++ b/x/msg_authorization/exported/keeper.go @@ -1,9 +1,10 @@ package exported import ( - "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" "time" + "github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/types" + sdk "github.com/cosmos/cosmos-sdk/types" ) From 3aedcc35d2cf2decf3bd1adec3b23e15e959c851 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Mon, 17 Feb 2020 19:36:49 +0530 Subject: [PATCH 161/162] Fix golint issues --- x/msg_authorization/client/cli/query.go | 1 + x/msg_authorization/internal/keeper/keeper_test.go | 12 ++++++------ x/msg_authorization/internal/keeper/test_common.go | 6 ++---- x/msg_authorization/internal/types/keys.go | 1 + 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/x/msg_authorization/client/cli/query.go b/x/msg_authorization/client/cli/query.go index 9d7fc10f0389..a21b75d26235 100644 --- a/x/msg_authorization/client/cli/query.go +++ b/x/msg_authorization/client/cli/query.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/flags" diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index 0744c94c7f74..ce7aa3bc1f55 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -58,18 +58,18 @@ func (s *TestSuite) TestKeeper() { s.Require().Nil(authorization) s.T().Log("verify fetching authorization with wrong grantee fails") - authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgMultiSend{}.Type()) + authorization, _ = s.keeper.GetAuthorization(s.ctx, recipientAddr, granterAddr, bank.MsgMultiSend{}.Type()) s.Require().Nil(authorization) s.T().Log("") s.T().Log("verify revoke fails with wrong information") - s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}.Type()) - authorization, _ = s.keeper.GetAuthorization(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}.Type()) + s.keeper.Revoke(s.ctx, recipientAddr, granterAddr, bank.MsgSend{}.Type()) + authorization, _ = s.keeper.GetAuthorization(s.ctx, recipientAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().Nil(authorization) s.T().Log("verify revoke executes with correct information") - s.keeper.Revoke(s.ctx, recepientAddr, granterAddr, bank.MsgSend{}.Type()) + s.keeper.Revoke(s.ctx, recipientAddr, granterAddr, bank.MsgSend{}.Type()) authorization, _ = s.keeper.GetAuthorization(s.ctx, granteeAddr, granterAddr, bank.MsgSend{}.Type()) s.Require().NotNil(authorization) @@ -93,7 +93,7 @@ func (s *TestSuite) TestKeeperFees() { bank.MsgSend{ Amount: sdk.NewCoins(sdk.NewInt64Coin("steak", 2)), FromAddress: granterAddr, - ToAddress: recepientAddr, + ToAddress: recipientAddr, }, }, } @@ -126,7 +126,7 @@ func (s *TestSuite) TestKeeperFees() { bank.MsgSend{ Amount: someCoin, FromAddress: granterAddr, - ToAddress: recepientAddr, + ToAddress: recipientAddr, }, }, } diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 9a25b4f40953..64852b10beac 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -69,7 +69,6 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base router.AddRoute("bank", bank.NewHandler(bankKeeper)) authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) - authKeeper.SetParams(ctx, auth.DefaultParams()) return ctx, authKeeper, paramsKeeper, bankKeeper, authorizationKeeper, router @@ -78,10 +77,9 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base var ( granteePub = ed25519.GenPrivKey().PubKey() granterPub = ed25519.GenPrivKey().PubKey() - recepientPub = ed25519.GenPrivKey().PubKey() + recipientPub = ed25519.GenPrivKey().PubKey() randomPub = ed25519.GenPrivKey().PubKey() granteeAddr = sdk.AccAddress(granteePub.Address()) granterAddr = sdk.AccAddress(granterPub.Address()) - recepientAddr = sdk.AccAddress(recepientPub.Address()) - randomAddr = sdk.AccAddress(randomPub.Address()) + recipientAddr = sdk.AccAddress(recipientPub.Address()) ) diff --git a/x/msg_authorization/internal/types/keys.go b/x/msg_authorization/internal/types/keys.go index 3d28db0b0bf8..93eff1457a22 100644 --- a/x/msg_authorization/internal/types/keys.go +++ b/x/msg_authorization/internal/types/keys.go @@ -2,6 +2,7 @@ package types import ( "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) From c9654e8f4ffd00ca23e622818aeae93b8a50584a Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Thu, 27 Feb 2020 16:39:11 +0530 Subject: [PATCH 162/162] Did review changes --- x/msg_authorization/alias.go | 1 - x/msg_authorization/internal/keeper/keeper.go | 5 +- .../internal/keeper/keeper_test.go | 3 +- .../internal/keeper/test_common.go | 6 +-- x/msg_authorization/internal/types/router.go | 53 ------------------- 5 files changed, 8 insertions(+), 60 deletions(-) delete mode 100644 x/msg_authorization/internal/types/router.go diff --git a/x/msg_authorization/alias.go b/x/msg_authorization/alias.go index bb1d58f5988d..6c4fd11756b5 100644 --- a/x/msg_authorization/alias.go +++ b/x/msg_authorization/alias.go @@ -27,7 +27,6 @@ var ( NewMsgRevokeAuthorization = types.NewMsgRevokeAuthorization NewMsgExecAuthorized = types.NewMsgExecAuthorized NewKeeper = keeper.NewKeeper - NewRouter = types.NewRouter // variable aliases ModuleCdc = types.ModuleCdc diff --git a/x/msg_authorization/internal/keeper/keeper.go b/x/msg_authorization/internal/keeper/keeper.go index e3524ec71321..6e04db4e64ed 100644 --- a/x/msg_authorization/internal/keeper/keeper.go +++ b/x/msg_authorization/internal/keeper/keeper.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -14,11 +15,11 @@ import ( type Keeper struct { storeKey sdk.StoreKey cdc *codec.Codec - router types.Router + router baseapp.Router } // NewKeeper constructs a message authorisation Keeper -func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router types.Router) Keeper { +func NewKeeper(storeKey sdk.StoreKey, cdc *codec.Codec, router baseapp.Router) Keeper { return Keeper{ storeKey: storeKey, cdc: cdc, diff --git a/x/msg_authorization/internal/keeper/keeper_test.go b/x/msg_authorization/internal/keeper/keeper_test.go index ce7aa3bc1f55..c6eed352958f 100644 --- a/x/msg_authorization/internal/keeper/keeper_test.go +++ b/x/msg_authorization/internal/keeper/keeper_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/suite" + "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" @@ -20,7 +21,7 @@ type TestSuite struct { paramsKeeper params.Keeper bankKeeper bank.Keeper keeper Keeper - router types.Router + router baseapp.Router } func (s *TestSuite) SetupTest() { diff --git a/x/msg_authorization/internal/keeper/test_common.go b/x/msg_authorization/internal/keeper/test_common.go index 64852b10beac..65672379609b 100644 --- a/x/msg_authorization/internal/keeper/test_common.go +++ b/x/msg_authorization/internal/keeper/test_common.go @@ -9,6 +9,7 @@ import ( "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" @@ -31,7 +32,7 @@ func makeTestCodec() *codec.Codec { return cdc } -func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, types.Router) { +func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.BaseKeeper, Keeper, baseapp.Router) { db := dbm.NewMemDB() cdc := codec.New() @@ -65,7 +66,7 @@ func SetupTestInput() (sdk.Context, auth.AccountKeeper, params.Keeper, bank.Base bankKeeper := bank.NewBaseKeeper(cdc, keyBank, authKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), blacklistedAddrs) bankKeeper.SetSendEnabled(ctx, true) - router := types.NewRouter() + router := *baseapp.NewRouter() router.AddRoute("bank", bank.NewHandler(bankKeeper)) authorizationKeeper := NewKeeper(keyAuthorization, cdc, router) @@ -78,7 +79,6 @@ var ( granteePub = ed25519.GenPrivKey().PubKey() granterPub = ed25519.GenPrivKey().PubKey() recipientPub = ed25519.GenPrivKey().PubKey() - randomPub = ed25519.GenPrivKey().PubKey() granteeAddr = sdk.AccAddress(granteePub.Address()) granterAddr = sdk.AccAddress(granterPub.Address()) recipientAddr = sdk.AccAddress(recipientPub.Address()) diff --git a/x/msg_authorization/internal/types/router.go b/x/msg_authorization/internal/types/router.go deleted file mode 100644 index 9e2396d4c373..000000000000 --- a/x/msg_authorization/internal/types/router.go +++ /dev/null @@ -1,53 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -var _ Router = (*router)(nil) - -type Router interface { - AddRoute(r string, h sdk.Handler) (rtr Router) - Seal() - Route(c sdk.Context, p string) (h sdk.Handler) -} - -type router struct { - routes map[string]sdk.Handler - sealed bool -} - -// NewRouter creates a new Router interface instance -func NewRouter() Router { - return &router{ - routes: make(map[string]sdk.Handler), - } -} - -// Seal seals the router which prohibits any subsequent route handlers to be -// added. Seal will panic if called more than once. -func (rtr *router) Seal() { - if rtr.sealed { - panic("router already sealed") - } - rtr.sealed = true -} - -// AddRoute adds a governance handler for a given path. It returns the Router -// so AddRoute calls can be linked. It will panic if the router is sealed. -func (rtr *router) AddRoute(path string, h sdk.Handler) Router { - if rtr.sealed { - panic("router sealed; cannot add route handler") - } - - if !sdk.IsAlphaNumeric(path) { - panic("route expressions can only contain alphanumeric characters") - } - - rtr.routes[path] = h - return rtr -} - -func (rtr *router) Route(_ sdk.Context, path string) sdk.Handler { - return rtr.routes[path] -}