package auth import ( "context" "encoding/json" "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" "cosmossdk.io/core/appmodule" appmodulev2 "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/legacy" "cosmossdk.io/core/registry" "cosmossdk.io/core/transaction" "cosmossdk.io/x/auth/ante" "cosmossdk.io/x/auth/keeper" "cosmossdk.io/x/auth/simulation" "cosmossdk.io/x/auth/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" ) // ConsensusVersion defines the current x/auth module consensus version. const ( ConsensusVersion = 5 GovModuleName = "gov" ) var ( _ module.AppModuleSimulation = AppModule{} _ appmodulev2.HasGenesis = AppModule{} _ appmodulev2.AppModule = AppModule{} _ appmodule.HasServices = AppModule{} _ appmodulev2.HasMigrations = AppModule{} ) // AppModule implements an application module for the auth module. type AppModule struct { accountKeeper keeper.AccountKeeper randGenAccountsFn types.RandomGenesisAccountsFn accountsModKeeper types.AccountsModKeeper cdc codec.Codec } // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} // NewAppModule creates a new AppModule object func NewAppModule( cdc codec.Codec, accountKeeper keeper.AccountKeeper, ak types.AccountsModKeeper, randGenAccountsFn types.RandomGenesisAccountsFn, ) AppModule { return AppModule{ accountKeeper: accountKeeper, randGenAccountsFn: randGenAccountsFn, accountsModKeeper: ak, cdc: cdc, } } // Name returns the module's name. // Deprecated: kept for legacy reasons. func (AppModule) Name() string { return types.ModuleName } // RegisterLegacyAminoCodec registers the auth module's types for the given codec. func (AppModule) RegisterLegacyAminoCodec(cdc legacy.Amino) { types.RegisterLegacyAminoCodec(cdc) } // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the auth module. func (AppModule) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) { if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { panic(err) } } // RegisterInterfaces registers interfaces and implementations of the auth module. func (AppModule) RegisterInterfaces(registrar registry.InterfaceRegistrar) { types.RegisterInterfaces(registrar) } // RegisterServices registers module services. func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.accountKeeper)) types.RegisterQueryServer(registrar, keeper.NewQueryServer(am.accountKeeper)) return nil } // RegisterMigrations registers module migrations func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { m := keeper.NewMigrator(am.accountKeeper) if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) } if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %w", types.ModuleName, err) } if err := mr.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %w", types.ModuleName, err) } if err := mr.Register(types.ModuleName, 4, m.Migrate4To5); err != nil { return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %w", types.ModuleName, err) } return nil } // DefaultGenesis returns default genesis state as raw bytes for the auth module. func (am AppModule) DefaultGenesis() json.RawMessage { return am.cdc.MustMarshalJSON(types.DefaultGenesisState()) } // ValidateGenesis performs genesis state validation for the auth module. func (am AppModule) ValidateGenesis(bz json.RawMessage) error { var data types.GenesisState if err := am.cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } return types.ValidateGenesis(data) } // InitGenesis performs genesis initialization for the auth module. func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error { var genesisState types.GenesisState if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil { return err } return am.accountKeeper.InitGenesis(ctx, genesisState) } // ExportGenesis returns the exported genesis state as raw bytes for the auth // module. func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) { gs, err := am.accountKeeper.ExportGenesis(ctx) if err != nil { return nil, err } return am.cdc.MarshalJSON(gs) } // TxValidator implements appmodulev2.HasTxValidator. // It replaces auth ante handlers for server/v2 func (am AppModule) TxValidator(ctx context.Context, tx transaction.Tx) error { validators := []appmodulev2.TxValidator[sdk.Tx]{ ante.NewValidateBasicDecorator(am.accountKeeper.GetEnvironment()), ante.NewTxTimeoutHeightDecorator(am.accountKeeper.GetEnvironment()), ante.NewValidateMemoDecorator(am.accountKeeper), ante.NewConsumeGasForTxSizeDecorator(am.accountKeeper), ante.NewValidateSigCountDecorator(am.accountKeeper), } sdkTx, ok := tx.(sdk.Tx) if !ok { return fmt.Errorf("invalid tx type %T, expected sdk.Tx", tx) } for _, validator := range validators { if err := validator.ValidateTx(ctx, sdkTx); err != nil { return err } } return nil } // ConsensusVersion implements appmodule.HasConsensusVersion func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } // AppModuleSimulation functions // GenerateGenesisState creates a randomized GenState of the auth module func (am AppModule) GenerateGenesisState(simState *module.SimulationState) { simulation.RandomizedGenState(simState, am.randGenAccountsFn) } // ProposalMsgs returns msgs used for governance proposals for simulations. func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { return simulation.ProposalMsgs() } // RegisterStoreDecoder registers a decoder for auth module's types func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) { sdr[types.StoreKey] = simtypes.NewStoreDecoderFuncFromCollectionsSchema(am.accountKeeper.Schema) } // WeightedOperations doesn't return any auth module operation. func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation { return nil }