4.4 KiB
x/auth
The x/auth
module is responsible for specifying the base transaction and
account types for an application, as well as AnteHandler and authentication logic.
Usage
-
Import the module.
import ( "github.com/cosmos/cosmos-sdk/x/auth" )
-
Add
AppModuleBasic
to yourModuleBasics
.var ( ModuleBasics = module.NewBasicManager( // ... auth.AppModuleBasic{}, } )
-
Create the module's parameter subspace in your application constructor.
func NewApp(...) *App { // ... app.subspaces[auth.ModuleName] = app.ParamsKeeper.Subspace(auth.DefaultParamspace) }
-
Create the keeper.
func NewApp(...) *App { // ... app.AccountKeeper = auth.NewAccountKeeper( app.cdc, keys[auth.StoreKey], app.subspaces[auth.ModuleName], auth.ProtoBaseAccount, ) }
-
Add the
x/auth
module to the app'sModuleManager
.func NewApp(...) *App { // ... app.mm = module.NewManager( // ... auth.NewAppModule(app.AccountKeeper), // ... ) }
-
Set the
x/auth
module genesis order.func NewApp(...) *App { // ... app.mm.SetOrderInitGenesis(..., auth.ModuleName, ...) }
-
Add the
x/auth
module to the simulation manager (if you have one set).func NewApp(...) *App { // ... app.sm = module.NewSimulationManager( // ... auth.NewAppModule(app.AccountKeeper), // ... ) }
-
Set the
AnteHandler
if you're using the default provided byx/auth
. Note, the defaultAnteHandler
provided by thex/auth
module depends on thex/supply
module.func NewApp(...) *App { app.SetAnteHandler(ante.NewAnteHandler( app.AccountKeeper, app.SupplyKeeper, auth.DefaultSigVerificationGasConsumer, )) }
Vesting Accounts
The x/auth
modules also defines a few standard vesting account types under the
vesting
sub-package. In order to get your application to automatically support
these in terms of encoding and decoding, you must register the types with your
application Amino codec.
Where ever you define the application Codec
, be sure to register types via:
import (
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
)
func MakeCodec() *codec.Codec {
var cdc = codec.New()
// ...
vesting.RegisterCodec(cdc)
// ...
return cdc
}
Genesis
The x/auth
module defines its genesis state as follows:
type GenesisState struct {
Params Params `json:"params" yaml:"params"`
Accounts exported.GenesisAccounts `json:"accounts" yaml:"accounts"`
}
Which relies on the following types:
type Account interface {
GetAddress() sdk.AccAddress
SetAddress(sdk.AccAddress) error
GetPubKey() crypto.PubKey
SetPubKey(crypto.PubKey) error
GetAccountNumber() uint64
SetAccountNumber(uint64) error
GetSequence() uint64
SetSequence(uint64) error
GetCoins() sdk.Coins
SetCoins(sdk.Coins) error
SpendableCoins(blockTime time.Time) sdk.Coins
String() string
}
type Params struct {
MaxMemoCharacters uint64 `json:"max_memo_characters" yaml:"max_memo_characters"`
TxSigLimit uint64 `json:"tx_sig_limit" yaml:"tx_sig_limit"`
TxSizeCostPerByte uint64 `json:"tx_size_cost_per_byte" yaml:"tx_size_cost_per_byte"`
SigVerifyCostED25519 uint64 `json:"sig_verify_cost_ed25519" yaml:"sig_verify_cost_ed25519"`
SigVerifyCostSecp256k1 uint64 `json:"sig_verify_cost_secp256k1" yaml:"sig_verify_cost_secp256k1"`
}
Client
CLI
The x/auth
module provides various auxiliary CLI commands and a few that are
part of the module itself via the ModuleManager
. The commands that are part of
the module itself are defined below:
-
Query an account.
app q auth account [address] [...flags]
-
Sign an unsigned transaction using a single signature.
app tx auth sign [file]
-
Sign an unsigned transaction using a multisig.
app tx auth multisign [file] [name] [[signature]...]
REST
The x/auth
module provides various auxiliary REST handlers and a few that are
part of the module itself via the ModuleManager
. The endpoints that are part of
the module itself are defined below:
- Query an account.
Method Path GET
/auth/accounts/{address}