Skip to content

Commit

Permalink
feat: create new grantee, if it doesn't exist
Browse files Browse the repository at this point in the history
copied from cosmos#10703
  • Loading branch information
albertchon committed Mar 4, 2023
1 parent ec9318d commit 4b1ecc8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion simapp/app.go
Expand Up @@ -275,7 +275,7 @@ func NewSimApp(
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
)

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

// register the proposal types
govRouter := govtypes.NewRouter()
Expand Down
17 changes: 10 additions & 7 deletions x/authz/keeper/keeper.go
Expand Up @@ -14,21 +14,24 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/authz"
)

type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
router *baseapp.MsgServiceRouter
storeKey sdk.StoreKey
cdc codec.BinaryCodec
router *baseapp.MsgServiceRouter
authKeeper authkeeper.AccountKeeper
}

// NewKeeper constructs a message authorization Keeper
func NewKeeper(storeKey sdk.StoreKey, cdc codec.BinaryCodec, router *baseapp.MsgServiceRouter) Keeper {
func NewKeeper(storeKey sdk.StoreKey, cdc codec.BinaryCodec, router *baseapp.MsgServiceRouter, ak authkeeper.AccountKeeper) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
router: router,
storeKey: storeKey,
cdc: cdc,
router: router,
authKeeper: ak,
}
}

Expand Down
7 changes: 7 additions & 0 deletions x/authz/keeper/msg_server.go
Expand Up @@ -23,6 +23,13 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
return nil, err
}

// create the account if it is not in account state
granteeAcc := k.authKeeper.GetAccount(ctx, grantee)
if granteeAcc == nil {
granteeAcc = k.authKeeper.NewAccountWithAddress(ctx, grantee)
k.authKeeper.SetAccount(ctx, granteeAcc)
}

authorization := msg.GetAuthorization()
if authorization == nil {
return nil, sdkerrors.ErrUnpackAny.Wrap("Authorization is not present in the msg")
Expand Down

0 comments on commit 4b1ecc8

Please sign in to comment.