-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
sahith-narahari
wants to merge
181
commits into
cosmos:master
from
regen-network:regen-network/msg_authorization
Closed
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 b2517ca
Implemented sdk.msg methods
sahith-narahari cbe122e
Created handler for authorization
59db8ca
Added validate basic to all messages
sahith-narahari 1b23770
Added base for internal keeper
sahith-narahari bc63ef7
Added message registration to codec
sahith-narahari 7a428a5
Merge pull request #1 from regen-friends/msg_internal
sahith-narahari b04b90d
Added Send Capability
sahith-narahari 5ddb995
Added keeper methods for authorization
2253d4f
Added keeper implementations
425b1bd
Merge pull request #2 from regen-friends/msg_internal
anilcse ec6ef9b
Added test input for keeper
b50902a
Added tests for keeper
6c43fd5
Added grant,revoke to cli
sahith-narahari 47b2974
Added more test cases for keeper
a4505b7
Merge pull request #3 from regen-friends/msg_internal_client
anilcse a92593f
Added expiration flag to grant tx
f9d03fb
Merge pull request #4 from regen-friends/msg_internal
anilcse 2535465
Merge branch 'msg_authorisation' of github.com:regen-friends/cosmos-s…
sahith-narahari cb0152f
Added code skeleton for msg authorization
sahith-narahari b3ed7a8
Implemented sdk.msg methods
sahith-narahari 0f4567b
Created handler for authorization
56f84ae
Added validate basic to all messages
sahith-narahari 1fa819b
Added base for internal keeper
sahith-narahari ee7aef9
Added message registration to codec
sahith-narahari 396afae
Added Send Capability
sahith-narahari dabf01c
Added keeper methods for authorization
c7acac3
Added keeper implementations
9fc6ce8
Added test input for keeper
7a0f87e
Added tests for keeper
d4b4579
Added more test cases for keeper
a543ff9
Added grant,revoke to cli
sahith-narahari 1aec3d0
Added expiration flag to grant tx
01f6483
Added txs to rest
sahith-narahari 8066921
Added module.go
sahith-narahari 5421223
Update review changes
anilcse 17ae7a6
Added query command for authorization
sahith-narahari 78811fa
Revert GetCapability to match ADR
anilcse c1ba63e
Refactor all the Authorization nomenclature
anilcse 4482b55
Add sendAs tx
anilcse a4ae502
Fix signer
anilcse 7806826
Fix tx input as a parameter to send-as
anilcse b736780
Merge branch 'master' of github.com:cosmos/cosmos-sdk into regen-netw…
fc3218f
Modified error handling in keeper
8c065c9
Registered errors of msg_authorization
c7edbb7
Modified messages to new error format
7b62a0b
Updated test keepers
8402bbe
Modified cli to latest sdk format
a59cee0
Modified module to latest sdk format
72ce01c
Modified module to latest sdk format
909369d
Modified module to latest sdk format
3fad8b8
Modified cli for grant authorization
4ba9607
Modified cli for grant authorization
733b904
Changed default value for expiration
ec34c3d
Reverted unmarshal interface type
f2fc711
Added debugging
878b9a0
Fixed unmarshal method
163e8e4
Changed unmarshal method to Unmarshaljson
123d92c
Add more keeper tests and debug logs -- WIP
anilcse 33f9435
Added debugging comments
3b496c0
Moved authorization to exported
a0e59f6
Modified coded
8c55760
Added init to codec
3922c15
Add more tests for sendAs authorization
anilcse feaef7f
Added debugging comments
743d043
Removed comments in codec
ec1e148
Added static value to client
97e1038
Changed authorization type
41b392c
Merge branch 'master' of github.com:cosmos/cosmos-sdk into multistore…
d09cbe5
Removed unused imports
1f0fa9a
Modified router for dispatch actions
6838943
Modified keeper test
5c94ca9
Added router to msg_auth
f37d243
Added alias to router
25b6c82
Modified router type in keeper
b581b75
Modified router type in tests
254a13c
Added handler to module
3a9bcfb
Modified key name
9147113
Modified default input to cli
0ff3bfd
Modified authorization type
f6ad6c1
Modified authorization type
74bf1f1
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth-m…
sahith-narahari 908807d
Modified tests in msg_auth
sahith-narahari d3fcf2e
Changed timestamp in keeper to int64
sahith-narahari 7b74e35
Replaced msg with msg type in grant and revoke
58389ac
Modified msg type in rest
1002540
Modified msg type in keys
904d6d5
Modified args in query cli
85cba43
Added more events to handler
c553f4b
Fixed query authorization
e9bb426
Fixed unmarshal in query authorization
a642f66
Cleaned sendas tx
b2c3743
Added debugging comments
ce5f7b2
Added debugging comments
7c3ab1e
Added debugging comments
075fbb7
Did code cleanup in keeper
5ed3e84
Removed bool from grant
0c78c72
Merge pull request #22 from regen-network/msg_auth-test
anilcse 6456750
Did code cleanup
175f72e
Added code skeleton for msg authorization
sahith-narahari d113c3e
Implemented sdk.msg methods
sahith-narahari 6942f1c
Created handler for authorization
7abde97
Added validate basic to all messages
sahith-narahari 4c155f1
Added base for internal keeper
sahith-narahari e2828d2
Added message registration to codec
sahith-narahari ddd8b69
Added Send Capability
sahith-narahari 1253c2c
Added keeper methods for authorization
debecf3
Added keeper implementations
dc1041a
Added test input for keeper
54ff2ed
Added tests for keeper
c08bef4
Added more test cases for keeper
cad1b07
Added grant,revoke to cli
sahith-narahari c07b445
Added expiration flag to grant tx
e99d483
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
6ff5be6
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
ec6d387
Added code skeleton for msg authorization
sahith-narahari 5890a7e
Implemented sdk.msg methods
sahith-narahari 0a8b049
Created handler for authorization
aab8be2
Added validate basic to all messages
sahith-narahari 902ced6
Added base for internal keeper
sahith-narahari 9975358
Added message registration to codec
sahith-narahari f7bd342
Added Send Capability
sahith-narahari 6cbf544
Added keeper methods for authorization
932c391
Added keeper implementations
7713d1c
Added test input for keeper
9ad28db
Added tests for keeper
21531f0
Added grant,revoke to cli
sahith-narahari 8098f59
Added more test cases for keeper
a839a28
Added expiration flag to grant tx
d3c1882
Added code skeleton for msg authorization
sahith-narahari fd61278
Implemented sdk.msg methods
sahith-narahari 893c2a0
Created handler for authorization
6beadfb
Added validate basic to all messages
sahith-narahari c784dc0
Added keeper implementations
f3b9333
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
sahith-narahari 01d19a0
Merge branch 'master' of github.com:cosmos/cosmos-sdk into msg_auth-m…
sahith-narahari 5aa13dd
Added code skeleton for msg authorization
sahith-narahari 2dd9fd6
Implemented sdk.msg methods
sahith-narahari eb1ee12
Created handler for authorization
57e3dc3
Added validate basic to all messages
sahith-narahari dc3bea4
Added base for internal keeper
sahith-narahari 37226fd
Added message registration to codec
sahith-narahari 780a4df
Added Send Capability
sahith-narahari 0eead62
Added keeper methods for authorization
9cc5bd9
Added keeper implementations
86490d1
Added test input for keeper
fdcf6fa
Added tests for keeper
6a4814c
Added grant,revoke to cli
sahith-narahari 5d09180
Added more test cases for keeper
0ace115
Added expiration flag to grant tx
5bbec41
Added code skeleton for msg authorization
sahith-narahari af18f8a
Implemented sdk.msg methods
sahith-narahari d1d8e0a
Created handler for authorization
3c6dc86
Added validate basic to all messages
sahith-narahari d5f3276
Added keeper implementations
d441bd3
Added code skeleton for msg authorization
sahith-narahari 0b9abee
Implemented sdk.msg methods
sahith-narahari b8c56fa
Created handler for authorization
a879434
Added validate basic to all messages
sahith-narahari 6fa240a
Added keeper implementations
08bbe52
Added code skeleton for msg authorization
sahith-narahari 7c3859c
Implemented sdk.msg methods
sahith-narahari 533e16c
Created handler for authorization
0a47282
Added validate basic to all messages
sahith-narahari 3a5842d
Added keeper implementations
ac8c780
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
sahith-narahari fb31b7d
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
sahith-narahari 61878ee
Merge branch 'master' into regen-network/msg_authorization
anilcse 8b54fb3
Merge pull request #23 from regen-network/msg_auth-master
anilcse 2564b16
Fix module codec for params keeper
anilcse 1a905d7
Fix update grant
anilcse 4ff3b72
Avoid duplicate calls
anilcse 2328e44
Remove debug comments
anilcse d389cd9
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
anilcse 2103faf
Renamed execdelegate to execauthorized
c2a9fd1
Merge pull request #27 from regen-network/anil/msg_authorization
anilcse bf53202
Merge branch 'master' into regen-network/msg_authorization
anilcse 03d3e6d
Fix gofmt
anilcse eeaa6fb
Merge branch 'regen-network/msg_authorization' of github.com:regen-ne…
anilcse 3aedcc3
Fix golint issues
anilcse c9654e8
Did review changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package cli | ||
|
||
const FlagExpiration = "expiration" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package rest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package rest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
del
result fromAccept
should still cause the authorization to be revoked even withallow
is also false.