Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

WIP: Add msg_authorization module #5412

Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
181 commits
Select commit Hold shift + click to select a range
429e77d
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
b2517ca
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
cbe122e
Created handler for authorization
Nov 19, 2019
59db8ca
Added validate basic to all messages
sahith-narahari Nov 20, 2019
1b23770
Added base for internal keeper
sahith-narahari Nov 20, 2019
bc63ef7
Added message registration to codec
sahith-narahari Nov 20, 2019
7a428a5
Merge pull request #1 from regen-friends/msg_internal
sahith-narahari Nov 20, 2019
b04b90d
Added Send Capability
sahith-narahari Nov 22, 2019
5ddb995
Added keeper methods for authorization
Dec 2, 2019
2253d4f
Added keeper implementations
Dec 4, 2019
425b1bd
Merge pull request #2 from regen-friends/msg_internal
anilcse Dec 4, 2019
ec6ef9b
Added test input for keeper
Dec 5, 2019
b50902a
Added tests for keeper
Dec 9, 2019
6c43fd5
Added grant,revoke to cli
sahith-narahari Dec 9, 2019
47b2974
Added more test cases for keeper
Dec 10, 2019
a4505b7
Merge pull request #3 from regen-friends/msg_internal_client
anilcse Dec 10, 2019
a92593f
Added expiration flag to grant tx
Dec 10, 2019
f9d03fb
Merge pull request #4 from regen-friends/msg_internal
anilcse Dec 16, 2019
2535465
Merge branch 'msg_authorisation' of github.com:regen-friends/cosmos-s…
sahith-narahari Dec 16, 2019
cb0152f
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
b3ed7a8
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
0f4567b
Created handler for authorization
Nov 19, 2019
56f84ae
Added validate basic to all messages
sahith-narahari Nov 20, 2019
1fa819b
Added base for internal keeper
sahith-narahari Nov 20, 2019
ee7aef9
Added message registration to codec
sahith-narahari Nov 20, 2019
396afae
Added Send Capability
sahith-narahari Nov 22, 2019
dabf01c
Added keeper methods for authorization
Dec 2, 2019
c7acac3
Added keeper implementations
Dec 4, 2019
9fc6ce8
Added test input for keeper
Dec 5, 2019
7a0f87e
Added tests for keeper
Dec 9, 2019
d4b4579
Added more test cases for keeper
Dec 10, 2019
a543ff9
Added grant,revoke to cli
sahith-narahari Dec 9, 2019
1aec3d0
Added expiration flag to grant tx
Dec 10, 2019
01f6483
Added txs to rest
sahith-narahari Dec 18, 2019
8066921
Added module.go
sahith-narahari Dec 18, 2019
5421223
Update review changes
anilcse Dec 29, 2019
17ae7a6
Added query command for authorization
sahith-narahari Dec 29, 2019
78811fa
Revert GetCapability to match ADR
anilcse Dec 29, 2019
c1ba63e
Refactor all the Authorization nomenclature
anilcse Dec 29, 2019
4482b55
Add sendAs tx
anilcse Dec 30, 2019
a4ae502
Fix signer
anilcse Dec 30, 2019
7806826
Fix tx input as a parameter to send-as
anilcse Dec 30, 2019
b736780
Merge branch 'master' of github.com:cosmos/cosmos-sdk into regen-netw…
Dec 31, 2019
fc3218f
Modified error handling in keeper
Dec 31, 2019
8c065c9
Registered errors of msg_authorization
Jan 2, 2020
c7edbb7
Modified messages to new error format
Jan 2, 2020
7b62a0b
Updated test keepers
Jan 2, 2020
8402bbe
Modified cli to latest sdk format
Jan 2, 2020
a59cee0
Modified module to latest sdk format
Jan 2, 2020
72ce01c
Modified module to latest sdk format
Jan 2, 2020
909369d
Modified module to latest sdk format
Jan 2, 2020
3fad8b8
Modified cli for grant authorization
Jan 2, 2020
4ba9607
Modified cli for grant authorization
Jan 2, 2020
733b904
Changed default value for expiration
Jan 2, 2020
ec34c3d
Reverted unmarshal interface type
Jan 2, 2020
f2fc711
Added debugging
Jan 2, 2020
878b9a0
Fixed unmarshal method
Jan 2, 2020
163e8e4
Changed unmarshal method to Unmarshaljson
Jan 3, 2020
123d92c
Add more keeper tests and debug logs -- WIP
anilcse Jan 2, 2020
33f9435
Added debugging comments
Jan 3, 2020
3b496c0
Moved authorization to exported
Jan 3, 2020
a0e59f6
Modified coded
Jan 3, 2020
8c55760
Added init to codec
Jan 3, 2020
3922c15
Add more tests for sendAs authorization
anilcse Jan 3, 2020
feaef7f
Added debugging comments
Jan 3, 2020
743d043
Removed comments in codec
Jan 3, 2020
ec1e148
Added static value to client
Jan 3, 2020
97e1038
Changed authorization type
Jan 3, 2020
41b392c
Merge branch 'master' of github.com:cosmos/cosmos-sdk into multistore…
Jan 29, 2020
d09cbe5
Removed unused imports
Jan 29, 2020
1f0fa9a
Modified router for dispatch actions
Jan 29, 2020
6838943
Modified keeper test
Jan 31, 2020
5c94ca9
Added router to msg_auth
Feb 3, 2020
f37d243
Added alias to router
Feb 3, 2020
25b6c82
Modified router type in keeper
Feb 3, 2020
b581b75
Modified router type in tests
Feb 3, 2020
254a13c
Added handler to module
Feb 3, 2020
3a9bcfb
Modified key name
Feb 3, 2020
9147113
Modified default input to cli
Feb 4, 2020
0ff3bfd
Modified authorization type
Feb 4, 2020
f6ad6c1
Modified authorization type
Feb 4, 2020
74bf1f1
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth-m…
sahith-narahari Feb 8, 2020
908807d
Modified tests in msg_auth
sahith-narahari Feb 8, 2020
d3fcf2e
Changed timestamp in keeper to int64
sahith-narahari Feb 8, 2020
7b74e35
Replaced msg with msg type in grant and revoke
Feb 10, 2020
58389ac
Modified msg type in rest
Feb 10, 2020
1002540
Modified msg type in keys
Feb 10, 2020
904d6d5
Modified args in query cli
Feb 10, 2020
85cba43
Added more events to handler
Feb 11, 2020
c553f4b
Fixed query authorization
Feb 11, 2020
e9bb426
Fixed unmarshal in query authorization
Feb 11, 2020
a642f66
Cleaned sendas tx
Feb 11, 2020
b2c3743
Added debugging comments
Feb 11, 2020
ce5f7b2
Added debugging comments
Feb 11, 2020
7c3ab1e
Added debugging comments
Feb 11, 2020
075fbb7
Did code cleanup in keeper
Feb 11, 2020
5ed3e84
Removed bool from grant
Feb 11, 2020
0c78c72
Merge pull request #22 from regen-network/msg_auth-test
anilcse Feb 11, 2020
6456750
Did code cleanup
Feb 11, 2020
175f72e
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
d113c3e
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
6942f1c
Created handler for authorization
Nov 19, 2019
7abde97
Added validate basic to all messages
sahith-narahari Nov 20, 2019
4c155f1
Added base for internal keeper
sahith-narahari Nov 20, 2019
e2828d2
Added message registration to codec
sahith-narahari Nov 20, 2019
ddd8b69
Added Send Capability
sahith-narahari Nov 22, 2019
1253c2c
Added keeper methods for authorization
Dec 2, 2019
debecf3
Added keeper implementations
Dec 4, 2019
dc1041a
Added test input for keeper
Dec 5, 2019
54ff2ed
Added tests for keeper
Dec 9, 2019
c08bef4
Added more test cases for keeper
Dec 10, 2019
cad1b07
Added grant,revoke to cli
sahith-narahari Dec 9, 2019
c07b445
Added expiration flag to grant tx
Dec 10, 2019
e99d483
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
Feb 11, 2020
6ff5be6
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
Feb 11, 2020
ec6d387
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
5890a7e
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
0a8b049
Created handler for authorization
Nov 19, 2019
aab8be2
Added validate basic to all messages
sahith-narahari Nov 20, 2019
902ced6
Added base for internal keeper
sahith-narahari Nov 20, 2019
9975358
Added message registration to codec
sahith-narahari Nov 20, 2019
f7bd342
Added Send Capability
sahith-narahari Nov 22, 2019
6cbf544
Added keeper methods for authorization
Dec 2, 2019
932c391
Added keeper implementations
Dec 4, 2019
7713d1c
Added test input for keeper
Dec 5, 2019
9ad28db
Added tests for keeper
Dec 9, 2019
21531f0
Added grant,revoke to cli
sahith-narahari Dec 9, 2019
8098f59
Added more test cases for keeper
Dec 10, 2019
a839a28
Added expiration flag to grant tx
Dec 10, 2019
d3c1882
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
fd61278
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
893c2a0
Created handler for authorization
Nov 19, 2019
6beadfb
Added validate basic to all messages
sahith-narahari Nov 20, 2019
c784dc0
Added keeper implementations
Dec 4, 2019
f3b9333
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
sahith-narahari Feb 14, 2020
01d19a0
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth-m…
sahith-narahari Feb 14, 2020
5aa13dd
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
2dd9fd6
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
eb1ee12
Created handler for authorization
Nov 19, 2019
57e3dc3
Added validate basic to all messages
sahith-narahari Nov 20, 2019
dc3bea4
Added base for internal keeper
sahith-narahari Nov 20, 2019
37226fd
Added message registration to codec
sahith-narahari Nov 20, 2019
780a4df
Added Send Capability
sahith-narahari Nov 22, 2019
0eead62
Added keeper methods for authorization
Dec 2, 2019
9cc5bd9
Added keeper implementations
Dec 4, 2019
86490d1
Added test input for keeper
Dec 5, 2019
fdcf6fa
Added tests for keeper
Dec 9, 2019
6a4814c
Added grant,revoke to cli
sahith-narahari Dec 9, 2019
5d09180
Added more test cases for keeper
Dec 10, 2019
0ace115
Added expiration flag to grant tx
Dec 10, 2019
5bbec41
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
af18f8a
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
d1d8e0a
Created handler for authorization
Nov 19, 2019
3c6dc86
Added validate basic to all messages
sahith-narahari Nov 20, 2019
d5f3276
Added keeper implementations
Dec 4, 2019
d441bd3
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
0b9abee
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
b8c56fa
Created handler for authorization
Nov 19, 2019
a879434
Added validate basic to all messages
sahith-narahari Nov 20, 2019
6fa240a
Added keeper implementations
Dec 4, 2019
08bbe52
Added code skeleton for msg authorization
sahith-narahari Nov 18, 2019
7c3859c
Implemented sdk.msg methods
sahith-narahari Nov 18, 2019
533e16c
Created handler for authorization
Nov 19, 2019
0a47282
Added validate basic to all messages
sahith-narahari Nov 20, 2019
3a5842d
Added keeper implementations
Dec 4, 2019
ac8c780
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
sahith-narahari Feb 14, 2020
fb31b7d
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
sahith-narahari Feb 14, 2020
61878ee
Merge branch 'master' into regen-network/msg_authorization
anilcse Feb 16, 2020
8b54fb3
Merge pull request #23 from regen-network/msg_auth-master
anilcse Feb 17, 2020
2564b16
Fix module codec for params keeper
anilcse Feb 17, 2020
1a905d7
Fix update grant
anilcse Feb 17, 2020
4ff3b72
Avoid duplicate calls
anilcse Feb 17, 2020
2328e44
Remove debug comments
anilcse Feb 17, 2020
d389cd9
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
anilcse Feb 17, 2020
2103faf
Renamed execdelegate to execauthorized
Feb 17, 2020
c2a9fd1
Merge pull request #27 from regen-network/anil/msg_authorization
anilcse Feb 17, 2020
bf53202
Merge branch 'master' into regen-network/msg_authorization
anilcse Feb 17, 2020
03d3e6d
Fix gofmt
anilcse Feb 17, 2020
eeaa6fb
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
anilcse Feb 17, 2020
3aedcc3
Fix golint issues
anilcse Feb 17, 2020
c9654e8
Did review changes
Feb 27, 2020
File filter

Filter by extension

Filter by extension

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

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

const (
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 (
// 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 (
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
)
3 changes: 3 additions & 0 deletions x/msg_authorization/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cli

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

import (
"bufio"
"time"

"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"
"github.com/spf13/viper"
)

// 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",
Long: "Authorize and revoke access to execute transactions on behalf of your address",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

AuthorizationTxCmd.AddCommand(client.PostCommands(
GetCmdGrantCapability(cdc),
GetCmdRevokeCapability(cdc),
)...)

return AuthorizationTxCmd
}

func GetCmdGrantCapability(cdc *codec.Codec) *cobra.Command {
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
}
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 {
return err
}

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

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

return cmd
}

func GetCmdRevokeCapability(cdc *codec.Codec) *cobra.Command {
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
}
1 change: 1 addition & 0 deletions x/msg_authorization/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rest
1 change: 1 addition & 0 deletions x/msg_authorization/client/rest/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rest
24 changes: 24 additions & 0 deletions x/msg_authorization/exported/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package exported

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/msg_authorization/internal/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 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 types.Capability, expiration time.Time)
}
58 changes: 58 additions & 0 deletions x/msg_authorization/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package msg_authorization

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/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) {
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 {
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 {
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()}
}

func handleMsgExecDelegated(ctx sdk.Context, msg MsgExecDelegated, k Keeper) sdk.Result {
//TODO
return sdk.Result{Events: ctx.EventManager().Events()}
}
104 changes: 104 additions & 0 deletions x/msg_authorization/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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"
)

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,
}
}

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 {
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()
}
granter := signers[0]
if !bytes.Equal(granter, grantee) {
capability, _ := k.GetCapability(ctx, grantee, granter, msg)
if capability == nil {
return sdk.ErrUnauthorized("authorization not found").Result()
}
allow, updated, del := capability.Accept(msg, ctx.BlockHeader())
if !allow {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A del result from Accept should still cause the authorization to be revoked even with allow is also false.

return sdk.ErrUnauthorized(" ").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) {
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) {
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) {
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
}
Loading