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

x/authz: audit updates #9042

Merged
merged 50 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
7833410
x/authz: audit updates
robert-zaremba Apr 1, 2021
45a9ee3
audit with Aaron
robert-zaremba Apr 14, 2021
83780d3
authz: Update Authorization.Accept method
robert-zaremba Apr 19, 2021
d258480
authz: add event proto definitions
robert-zaremba Apr 19, 2021
8de54d0
update query service
robert-zaremba Apr 19, 2021
a721a97
authz: use typed events
robert-zaremba Apr 19, 2021
0f6158f
refactore and rename query authorizations
robert-zaremba Apr 19, 2021
5e029a8
remve Authorization infix from proto services
robert-zaremba Apr 19, 2021
424af8f
renames wip
robert-zaremba Apr 20, 2021
3876db2
refactoring
robert-zaremba Apr 20, 2021
c85574b
update tests
robert-zaremba Apr 22, 2021
55ce5b8
fix compilation
robert-zaremba Apr 22, 2021
385205a
fixing gRPC query tests
robert-zaremba Apr 22, 2021
94776a9
Merge branch 'master' into robert/authz-audit
robert-zaremba Apr 23, 2021
90da7b6
fix simulation tests
robert-zaremba Apr 23, 2021
bf42888
few renames
robert-zaremba Apr 23, 2021
afb9e8f
more refactore
robert-zaremba Apr 23, 2021
46d547d
add missing file
robert-zaremba Apr 23, 2021
7e9e325
moving export genesis to keeper
robert-zaremba Apr 23, 2021
2d5b037
Update docs
robert-zaremba Apr 23, 2021
5639620
update tests
robert-zaremba Apr 23, 2021
fc048d0
rename event Msg attribute to MsgTypeURL
robert-zaremba Apr 23, 2021
155913e
Upate Authorization interface
robert-zaremba Apr 23, 2021
c1b077c
rollback Makefile changes
robert-zaremba Apr 23, 2021
3ca0a98
fix tests
robert-zaremba Apr 23, 2021
a14a0c8
Apply suggestions from code review
robert-zaremba Apr 23, 2021
eda3fc4
Merge branch 'master' into robert/authz-audit
robert-zaremba Apr 27, 2021
46635db
renames
robert-zaremba Apr 27, 2021
f73ab9e
refactore authz/exported
robert-zaremba Apr 27, 2021
90329fc
lint fix
robert-zaremba Apr 28, 2021
2465221
authz/types refactore
robert-zaremba Apr 28, 2021
51b5e12
Merge remote-tracking branch 'origin/master' into robert/authz-audit
robert-zaremba Apr 28, 2021
79a69d3
comment update
robert-zaremba Apr 28, 2021
d6e9708
Merge branch 'master' into robert/authz-audit
robert-zaremba May 1, 2021
8320e4e
conflict updates
robert-zaremba May 1, 2021
b589571
Apply suggestions from code review
robert-zaremba May 4, 2021
a813d40
authz: move storage keys to keeper
robert-zaremba May 4, 2021
b5209b6
review updates
robert-zaremba May 4, 2021
ead0ee8
docs update
robert-zaremba May 4, 2021
a4bd3d9
Update x/authz/client/cli/query.go
robert-zaremba May 5, 2021
6050e1e
move codec to the root package
robert-zaremba May 5, 2021
2708696
Merge remote-tracking branch 'origin/master' into robert/authz-audit
robert-zaremba May 5, 2021
b7403e8
authz CMD info update
robert-zaremba May 5, 2021
52bc0b1
comment update
robert-zaremba May 5, 2021
059982c
update imports and build flags
robert-zaremba May 5, 2021
4218389
fix functional tests
robert-zaremba May 5, 2021
2e8caf5
update proto comment
robert-zaremba May 6, 2021
94b6c9f
fix tests
robert-zaremba May 6, 2021
e64b659
Merge branch 'master' into robert/authz-audit
robert-zaremba May 6, 2021
47324d4
fix test
robert-zaremba May 6, 2021
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
178 changes: 99 additions & 79 deletions docs/core/proto-docs.md

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions proto/cosmos/authz/v1beta1/authz.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import "google/protobuf/timestamp.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/authz/types";
option go_package = "github.com/cosmos/cosmos-sdk/x/authz";
option (gogoproto.goproto_getters_all) = false;

// GenericAuthorization gives the grantee unrestricted permissions to execute
// the provided method on behalf of the granter's account.
message GenericAuthorization {
option (cosmos_proto.implements_interface) = "Authorization";

// method name to grant unrestricted permissions to execute
// Note: MethodName() is already a method on `GenericAuthorization` type,
// we need some custom naming here so using `MessageName`
string method_name = 1 [(gogoproto.customname) = "MessageName"];
// Msg, identified by it's type URL, to grant unrestricted permissions to execute
string msg = 1;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
string msg = 1;
string msg_type_url = 1;

Copy link
Member

@aaronc aaronc Apr 23, 2021

Choose a reason for hiding this comment

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

Or... we could alternatively consider this approach if we wanted to go with this https://github.com/cosmos/cosmos-sdk/pull/9042/files#r619348288

Suggested change
string msg = 1;
google.protobuf.Any msg = 1;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The first suggestion won't work - see #9042 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

If we use google.protobuf.Any msg = 1;, what should its value hold? asaict, only the type_url is needed, so in order to store minimum stuff (and also not to confuse users), I prefer to keep string.

Copy link
Member

Choose a reason for hiding this comment

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

Let's just use string msg_type_url

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm using msg because it allows to use MsgTypeURL as method name in the Authorization interface (which, imho is more in Go style and more "straight"). Amaury suggested to use GetMsgTypeURL as an alternative. If you have a preference then let me know.

}

// AuthorizationGrant gives permissions to execute
// Grant gives permissions to execute
// the provide method with expiration time.
message AuthorizationGrant {
message Grant {
google.protobuf.Any authorization = 1 [(cosmos_proto.accepts_interface) = "Authorization"];
google.protobuf.Timestamp expiration = 2 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
}
aaronc marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions proto/cosmos/authz/v1beta1/event.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
syntax = "proto3";
package cosmos.authz.v1beta1;

option go_package = "github.com/cosmos/cosmos-sdk/x/authz";

// EventGrant is emitted on Msg/Grant
message EventGrant {
// Msg type URL for which an autorization is granted
string msg_type_url = 2;
// Granter account address
string granter = 3;
// Grantee account address
string grantee = 4;
}

// EventRevoke is emitted on Msg/Revoke
message EventRevoke {
// Msg type URL for which an autorization is revoked
string msg_type_url = 2;
// Granter account address
string granter = 3;
// Grantee account address
string grantee = 4;
}
2 changes: 1 addition & 1 deletion proto/cosmos/authz/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "google/protobuf/any.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/authz/types";
option go_package = "github.com/cosmos/cosmos-sdk/x/authz";

// GenesisState defines the authz module's genesis state.
message GenesisState {
Expand Down
39 changes: 11 additions & 28 deletions proto/cosmos/authz/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,30 @@ import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "cosmos/authz/v1beta1/authz.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/authz/types";
option go_package = "github.com/cosmos/cosmos-sdk/x/authz";

// Query defines the gRPC querier service.
service Query {
// Returns any `Authorization` (or `nil`), with the expiration time, granted to the grantee by the granter for the
// provided msg type.
rpc Authorization(QueryAuthorizationRequest) returns (QueryAuthorizationResponse) {
option (google.api.http).get = "/cosmos/authz/v1beta1/granters/{granter}/grantees/{grantee}/grant";
}

// Returns list of `Authorization`, granted to the grantee by the granter.
rpc Authorizations(QueryAuthorizationsRequest) returns (QueryAuthorizationsResponse) {
option (google.api.http).get = "/cosmos/authz/v1beta1/granters/{granter}/grantees/{grantee}/grants";
rpc Grants(QueryGrantsRequest) returns (QueryGrantsResponse) {
option (google.api.http).get = "/cosmos/authz/v1beta1/grants";
}
}

// QueryAuthorizationRequest is the request type for the Query/Authorization RPC method.
message QueryAuthorizationRequest {
string granter = 1;
string grantee = 2;
string method_name = 3;
}

// QueryAuthorizationResponse is the response type for the Query/Authorization RPC method.
message QueryAuthorizationResponse {
// authorization is a authorization granted for grantee by granter.
cosmos.authz.v1beta1.AuthorizationGrant authorization = 1;
}

// QueryAuthorizationsRequest is the request type for the Query/Authorizations RPC method.
message QueryAuthorizationsRequest {
// QueryGrantsRequest is the request type for the Query/Grants RPC method.
message QueryGrantsRequest {
string granter = 1;
string grantee = 2;
// Optional, msg_type_url, when set, will query only grants matching given msg type.
string msg_type_url = 3;
// pagination defines an pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 3;
cosmos.base.query.v1beta1.PageRequest pagination = 4;
}

// QueryAuthorizationsResponse is the response type for the Query/Authorizations RPC method.
message QueryAuthorizationsResponse {
// QueryGrantsResponse is the response type for the Query/Authorizations RPC method.
message QueryGrantsResponse {
// authorizations is a list of grants granted for grantee by granter.
repeated cosmos.authz.v1beta1.AuthorizationGrant authorizations = 1;
repeated cosmos.authz.v1beta1.Grant grants = 1;
// pagination defines an pagination for the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
46 changes: 25 additions & 21 deletions proto/cosmos/authz/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,61 @@ import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/any.proto";
import "cosmos/base/abci/v1beta1/abci.proto";
option go_package = "github.com/cosmos/cosmos-sdk/x/authz/types";

option go_package = "github.com/cosmos/cosmos-sdk/x/authz";
option (gogoproto.goproto_getters_all) = false;

// Msg defines the authz Msg service.
service Msg {
// GrantAuthorization grants the provided authorization to the grantee on the granter's
// Grant grants the provided authorization to the grantee on the granter's
// account with the provided expiration time.
rpc GrantAuthorization(MsgGrantAuthorizationRequest) returns (MsgGrantAuthorizationResponse);
rpc Grant(MsgGrant) returns (MsgGrantResponse);

// ExecAuthorized attempts to execute the provided messages using
// Exec 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.
rpc ExecAuthorized(MsgExecAuthorizedRequest) returns (MsgExecAuthorizedResponse);
rpc Exec(MsgExec) returns (MsgExecResponse);

// RevokeAuthorization revokes any authorization corresponding to the provided method name on the
// Revoke revokes any authorization corresponding to the provided method name on the
// granter's account that has been granted to the grantee.
rpc RevokeAuthorization(MsgRevokeAuthorizationRequest) returns (MsgRevokeAuthorizationResponse);
rpc Revoke(MsgRevoke) returns (MsgRevokeResponse);
}

// MsgGrantAuthorizationRequest grants the provided authorization to the grantee on the granter's
// MsgGrant grants the provided authorization to the grantee on the granter's
// account with the provided expiration time.
message MsgGrantAuthorizationRequest {
message MsgGrant {
string granter = 1;
string grantee = 2;

google.protobuf.Any authorization = 3 [(cosmos_proto.accepts_interface) = "Authorization"];
google.protobuf.Timestamp expiration = 4 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}

// MsgExecAuthorizedResponse defines the Msg/MsgExecAuthorizedResponse response type.
message MsgExecAuthorizedResponse {
// MsgExecResponse defines the Msg/MsgExecResponse response type.
message MsgExecResponse {
cosmos.base.abci.v1beta1.Result result = 1;
}

// MsgExecAuthorizedRequest attempts to execute the provided messages using
// MsgExec 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.
message MsgExecAuthorizedRequest {
message MsgExec {
string grantee = 1;
repeated google.protobuf.Any msgs = 2;
// MsgService requests to execute. The x/authz will try to find a grant matching
// (msg.signers[0], grantee, MsgTypeURL(msg)) triple and validate it.
repeated google.protobuf.Any msgs = 2 [(cosmos_proto.accepts_interface) = "sdk.Msg"];;
}

// MsgGrantAuthorizationResponse defines the Msg/MsgGrantAuthorization response type.
message MsgGrantAuthorizationResponse {}
// MsgGrantResponse defines the Msg/MsgGrant response type.
message MsgGrantResponse {}

// MsgRevokeAuthorizationRequest revokes any authorization with the provided sdk.Msg type on the
// MsgRevoke revokes any authorization with the provided sdk.Msg type on the
// granter's account with that has been granted to the grantee.
message MsgRevokeAuthorizationRequest {
message MsgRevoke {
string granter = 1;
string grantee = 2;
string method_name = 3;
string msg_type_url = 3;
}

// MsgRevokeAuthorizationResponse defines the Msg/MsgRevokeAuthorizationResponse response type.
message MsgRevokeAuthorizationResponse {}
// MsgRevokeResponse defines the Msg/MsgRevokeResponse response type.
message MsgRevokeResponse {}
19 changes: 9 additions & 10 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import (
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authz_m "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/crisis"
crisiskeeper "github.com/cosmos/cosmos-sdk/x/crisis/keeper"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
Expand Down Expand Up @@ -82,10 +85,6 @@ import (
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

authz "github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authztypes "github.com/cosmos/cosmos-sdk/x/authz/types"

// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
)
Expand Down Expand Up @@ -116,7 +115,7 @@ var (
feegrant.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
authz.AppModuleBasic{},
authz_m.AppModuleBasic{},
vesting.AppModuleBasic{},
)

Expand Down Expand Up @@ -208,7 +207,7 @@ func NewSimApp(
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, upgradetypes.StoreKey, feegranttypes.StoreKey,
evidencetypes.StoreKey, capabilitytypes.StoreKey,
authztypes.StoreKey,
authzkeeper.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -265,7 +264,7 @@ func NewSimApp(
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
)

app.AuthzKeeper = authzkeeper.NewKeeper(keys[authztypes.StoreKey], appCodec, app.BaseApp.MsgServiceRouter())
app.AuthzKeeper = authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], appCodec, app.BaseApp.MsgServiceRouter())

// register the proposal types
govRouter := govtypes.NewRouter()
Expand Down Expand Up @@ -318,7 +317,7 @@ func NewSimApp(
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
params.NewAppModule(app.ParamsKeeper),
authz.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
authz_m.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand All @@ -339,7 +338,7 @@ func NewSimApp(
app.mm.SetOrderInitGenesis(
capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, stakingtypes.ModuleName,
slashingtypes.ModuleName, govtypes.ModuleName, minttypes.ModuleName, crisistypes.ModuleName,
genutiltypes.ModuleName, evidencetypes.ModuleName, authztypes.ModuleName,
genutiltypes.ModuleName, evidencetypes.ModuleName, authz.ModuleName,
feegranttypes.ModuleName,
)

Expand Down Expand Up @@ -367,7 +366,7 @@ func NewSimApp(
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
params.NewAppModule(app.ParamsKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
authz.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
authz_m.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
)

app.sm.RegisterStoreDecoders()
Expand Down
6 changes: 3 additions & 3 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/authz"
authz_m "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/capability"
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestRunMigrations(t *testing.T) {
module.VersionMap{
"bank": 1,
"auth": auth.AppModule{}.ConsensusVersion(),
"authz": authz.AppModule{}.ConsensusVersion(),
"authz": authz_m.AppModule{}.ConsensusVersion(),
"staking": staking.AppModule{}.ConsensusVersion(),
"mint": mint.AppModule{}.ConsensusVersion(),
"distribution": distribution.AppModule{}.ConsensusVersion(),
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestInitGenesisOnMigration(t *testing.T) {
module.VersionMap{
"bank": bank.AppModule{}.ConsensusVersion(),
"auth": auth.AppModule{}.ConsensusVersion(),
"authz": authz.AppModule{}.ConsensusVersion(),
"authz": authz_m.AppModule{}.ConsensusVersion(),
"staking": staking.AppModule{}.ConsensusVersion(),
"mint": mint.AppModule{}.ConsensusVersion(),
"distribution": distribution.AppModule{}.ConsensusVersion(),
Expand Down
2 changes: 1 addition & 1 deletion simapp/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
)

// The genesis state of the blockchain is represented here as a map of raw json
// GenesisState of the blockchain is represented here as a map of raw json
// messages key'd by a identifier string.
// The identifier is used to determine which module genesis information belongs
// to so it may be appropriately routed during init chain.
Expand Down
4 changes: 2 additions & 2 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz/types"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestAppImportExport(t *testing.T) {
{app.keys[govtypes.StoreKey], newApp.keys[govtypes.StoreKey], [][]byte{}},
{app.keys[evidencetypes.StoreKey], newApp.keys[evidencetypes.StoreKey], [][]byte{}},
{app.keys[capabilitytypes.StoreKey], newApp.keys[capabilitytypes.StoreKey], [][]byte{}},
{app.keys[authztypes.StoreKey], newApp.keys[authztypes.StoreKey], [][]byte{}},
{app.keys[authz.StoreKey], newApp.keys[authz.StoreKey], [][]byte{}},
}

for _, skp := range storeKeysPrefixes {
Expand Down
2 changes: 1 addition & 1 deletion store/gaskv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (gi *gasIterator) Error() error {
return gi.parent.Error()
}

// consumeSeekGas consumes a flat gas cost for seeking and a variable gas cost
// consumeSeekGas consumes on each iteration step a flat gas cost and a variable gas cost
// based on the current value's length.
func (gi *gasIterator) consumeSeekGas() {
value := gi.Value()
Expand Down
49 changes: 49 additions & 0 deletions x/authz/authorization_grant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package authz

import (
"time"

proto "github.com/gogo/protobuf/proto"

cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// NewGrant returns new AuthrizationGrant
func NewGrant(authorization Authorization, expiration time.Time) (Grant, error) {
auth := Grant{
Expiration: expiration,
}
msg, ok := authorization.(proto.Message)
if !ok {
return Grant{}, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", authorization)
}

any, err := cdctypes.NewAnyWithValue(msg)
if err != nil {
return Grant{}, err
}

auth.Authorization = any

return auth, nil
}

var (
_ cdctypes.UnpackInterfacesMessage = &Grant{}
)

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (auth Grant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
var authorization Authorization
return unpacker.UnpackAny(auth.Authorization, &authorization)
}

// GetAuthorization returns the cached value from the Grant.Authorization if present.
func (auth Grant) GetAuthorization() Authorization {
authorization, ok := auth.Authorization.GetCachedValue().(Authorization)
if !ok {
return nil
}
return authorization
}
Loading