github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/README.md (about) 1 --- 2 sidebar_position: 1 3 --- 4 5 # `x/auth` 6 7 ## Abstract 8 9 This document specifies the auth module of the Cosmos SDK. 10 11 The auth module is responsible for specifying the base transaction and account types 12 for an application, since the SDK itself is agnostic to these particulars. It contains 13 the middlewares, where all basic transaction validity checks (signatures, nonces, auxiliary fields) 14 are performed, and exposes the account keeper, which allows other modules to read, write, and modify accounts. 15 16 This module is used in the Cosmos Hub. 17 18 ## Contents 19 20 * [Concepts](#concepts) 21 * [Gas & Fees](#gas--fees) 22 * [State](#state) 23 * [Accounts](#accounts) 24 * [AnteHandlers](#antehandlers) 25 * [Keepers](#keepers) 26 * [Account Keeper](#account-keeper) 27 * [Parameters](#parameters) 28 * [Client](#client) 29 * [CLI](#cli) 30 * [gRPC](#grpc) 31 * [REST](#rest) 32 33 ## Concepts 34 35 **Note:** The auth module is different from the [authz module](../modules/authz/). 36 37 The differences are: 38 39 * `auth` - authentication of accounts and transactions for Cosmos SDK applications and is responsible for specifying the base transaction and account types. 40 * `authz` - authorization for accounts to perform actions on behalf of other accounts and enables a granter to grant authorizations to a grantee that allows the grantee to execute messages on behalf of the granter. 41 42 ### Gas & Fees 43 44 Fees serve two purposes for an operator of the network. 45 46 Fees limit the growth of the state stored by every full node and allow for 47 general purpose censorship of transactions of little economic value. Fees 48 are best suited as an anti-spam mechanism where validators are disinterested in 49 the use of the network and identities of users. 50 51 Fees are determined by the gas limits and gas prices transactions provide, where 52 `fees = ceil(gasLimit * gasPrices)`. Txs incur gas costs for all state reads/writes, 53 signature verification, as well as costs proportional to the tx size. Operators 54 should set minimum gas prices when starting their nodes. They must set the unit 55 costs of gas in each token denomination they wish to support: 56 57 `simd start ... --minimum-gas-prices=0.00001stake;0.05photinos` 58 59 When adding transactions to mempool or gossipping transactions, validators check 60 if the transaction's gas prices, which are determined by the provided fees, meet 61 any of the validator's minimum gas prices. In other words, a transaction must 62 provide a fee of at least one denomination that matches a validator's minimum 63 gas price. 64 65 CometBFT does not currently provide fee based mempool prioritization, and fee 66 based mempool filtering is local to node and not part of consensus. But with 67 minimum gas prices set, such a mechanism could be implemented by node operators. 68 69 Because the market value for tokens will fluctuate, validators are expected to 70 dynamically adjust their minimum gas prices to a level that would encourage the 71 use of the network. 72 73 ## State 74 75 ### Accounts 76 77 Accounts contain authentication information for a uniquely identified external user of an SDK blockchain, 78 including public key, address, and account number / sequence number for replay protection. For efficiency, 79 since account balances must also be fetched to pay fees, account structs also store the balance of a user 80 as `sdk.Coins`. 81 82 Accounts are exposed externally as an interface, and stored internally as 83 either a base account or vesting account. Module clients wishing to add more 84 account types may do so. 85 86 * `0x01 | Address -> ProtocolBuffer(account)` 87 88 #### Account Interface 89 90 The account interface exposes methods to read and write standard account information. 91 Note that all of these methods operate on an account struct conforming to the 92 interface - in order to write the account to the store, the account keeper will 93 need to be used. 94 95 ```go 96 // AccountI is an interface used to store coins at a given address within state. 97 // It presumes a notion of sequence numbers for replay protection, 98 // a notion of account numbers for replay protection for previously pruned accounts, 99 // and a pubkey for authentication purposes. 100 // 101 // Many complex conditions can be used in the concrete struct which implements AccountI. 102 type AccountI interface { 103 proto.Message 104 105 GetAddress() sdk.AccAddress 106 SetAddress(sdk.AccAddress) error // errors if already set. 107 108 GetPubKey() crypto.PubKey // can return nil. 109 SetPubKey(crypto.PubKey) error 110 111 GetAccountNumber() uint64 112 SetAccountNumber(uint64) error 113 114 GetSequence() uint64 115 SetSequence(uint64) error 116 117 // Ensure that account implements stringer 118 String() string 119 } 120 ``` 121 122 ##### Base Account 123 124 A base account is the simplest and most common account type, which just stores all requisite 125 fields directly in a struct. 126 127 ```protobuf 128 // BaseAccount defines a base account type. It contains all the necessary fields 129 // for basic account functionality. Any custom account type should extend this 130 // type for additional functionality (e.g. vesting). 131 message BaseAccount { 132 string address = 1; 133 google.protobuf.Any pub_key = 2; 134 uint64 account_number = 3; 135 uint64 sequence = 4; 136 } 137 ``` 138 139 ### Vesting Account 140 141 See [Vesting](https://docs.cosmos.network/main/modules/auth/vesting/). 142 143 ## AnteHandlers 144 145 The `x/auth` module presently has no transaction handlers of its own, but does expose the special `AnteHandler`, used for performing basic validity checks on a transaction, such that it could be thrown out of the mempool. 146 The `AnteHandler` can be seen as a set of decorators that check transactions within the current context, per [ADR 010](https://github.com/cosmos/cosmos-sdk/blob/main/docs/architecture/adr-010-modular-antehandler.md). 147 148 Note that the `AnteHandler` is called on both `CheckTx` and `DeliverTx`, as CometBFT proposers presently have the ability to include in their proposed block transactions which fail `CheckTx`. 149 150 ### Decorators 151 152 The auth module provides `AnteDecorator`s that are recursively chained together into a single `AnteHandler` in the following order: 153 154 * `SetUpContextDecorator`: Sets the `GasMeter` in the `Context` and wraps the next `AnteHandler` with a defer clause to recover from any downstream `OutOfGas` panics in the `AnteHandler` chain to return an error with information on gas provided and gas used. 155 156 * `RejectExtensionOptionsDecorator`: Rejects all extension options which can optionally be included in protobuf transactions. 157 158 * `MempoolFeeDecorator`: Checks if the `tx` fee is above local mempool `minFee` parameter during `CheckTx`. 159 160 * `ValidateBasicDecorator`: Calls `tx.ValidateBasic` and returns any non-nil error. 161 162 * `TxTimeoutHeightDecorator`: Check for a `tx` height timeout. 163 164 * `ValidateMemoDecorator`: Validates `tx` memo with application parameters and returns any non-nil error. 165 166 * `ConsumeGasTxSizeDecorator`: Consumes gas proportional to the `tx` size based on application parameters. 167 168 * `DeductFeeDecorator`: Deducts the `FeeAmount` from first signer of the `tx`. If the `x/feegrant` module is enabled and a fee granter is set, it deducts fees from the fee granter account. 169 170 * `SetPubKeyDecorator`: Sets the pubkey from a `tx`'s signers that does not already have its corresponding pubkey saved in the state machine and in the current context. 171 172 * `ValidateSigCountDecorator`: Validates the number of signatures in `tx` based on app-parameters. 173 174 * `SigGasConsumeDecorator`: Consumes parameter-defined amount of gas for each signature. This requires pubkeys to be set in context for all signers as part of `SetPubKeyDecorator`. 175 176 * `SigVerificationDecorator`: Verifies all signatures are valid. This requires pubkeys to be set in context for all signers as part of `SetPubKeyDecorator`. 177 178 * `IncrementSequenceDecorator`: Increments the account sequence for each signer to prevent replay attacks. 179 180 ## Keepers 181 182 The auth module only exposes one keeper, the account keeper, which can be used to read and write accounts. 183 184 ### Account Keeper 185 186 Presently only one fully-permissioned account keeper is exposed, which has the ability to both read and write 187 all fields of all accounts, and to iterate over all stored accounts. 188 189 ```go 190 // AccountKeeperI is the interface contract that x/auth's keeper implements. 191 type AccountKeeperI interface { 192 // Return a new account with the next account number and the specified address. Does not save the new account to the store. 193 NewAccountWithAddress(sdk.Context, sdk.AccAddress) types.AccountI 194 195 // Return a new account with the next account number. Does not save the new account to the store. 196 NewAccount(sdk.Context, types.AccountI) types.AccountI 197 198 // Check if an account exists in the store. 199 HasAccount(sdk.Context, sdk.AccAddress) bool 200 201 // Retrieve an account from the store. 202 GetAccount(sdk.Context, sdk.AccAddress) types.AccountI 203 204 // Set an account in the store. 205 SetAccount(sdk.Context, types.AccountI) 206 207 // Remove an account from the store. 208 RemoveAccount(sdk.Context, types.AccountI) 209 210 // Iterate over all accounts, calling the provided function. Stop iteration when it returns true. 211 IterateAccounts(sdk.Context, func(types.AccountI) bool) 212 213 // Fetch the public key of an account at a specified address 214 GetPubKey(sdk.Context, sdk.AccAddress) (crypto.PubKey, error) 215 216 // Fetch the sequence of an account at a specified address. 217 GetSequence(sdk.Context, sdk.AccAddress) (uint64, error) 218 219 // Fetch the next account number, and increment the internal counter. 220 NextAccountNumber(sdk.Context) uint64 221 } 222 ``` 223 224 ## Parameters 225 226 The auth module contains the following parameters: 227 228 | Key | Type | Example | 229 | ---------------------- | --------------- | ------- | 230 | MaxMemoCharacters | uint64 | 256 | 231 | TxSigLimit | uint64 | 7 | 232 | TxSizeCostPerByte | uint64 | 10 | 233 | SigVerifyCostED25519 | uint64 | 590 | 234 | SigVerifyCostSecp256k1 | uint64 | 1000 | 235 236 ## Client 237 238 ### CLI 239 240 A user can query and interact with the `auth` module using the CLI. 241 242 ### Query 243 244 The `query` commands allow users to query `auth` state. 245 246 ```bash 247 simd query auth --help 248 ``` 249 250 #### account 251 252 The `account` command allow users to query for an account by it's address. 253 254 ```bash 255 simd query auth account [address] [flags] 256 ``` 257 258 Example: 259 260 ```bash 261 simd query auth account cosmos1... 262 ``` 263 264 Example Output: 265 266 ```bash 267 '@type': /cosmos.auth.v1beta1.BaseAccount 268 account_number: "0" 269 address: cosmos1zwg6tpl8aw4rawv8sgag9086lpw5hv33u5ctr2 270 pub_key: 271 '@type': /cosmos.crypto.secp256k1.PubKey 272 key: ApDrE38zZdd7wLmFS9YmqO684y5DG6fjZ4rVeihF/AQD 273 sequence: "1" 274 ``` 275 276 #### accounts 277 278 The `accounts` command allow users to query all the available accounts. 279 280 ```bash 281 simd query auth accounts [flags] 282 ``` 283 284 Example: 285 286 ```bash 287 simd query auth accounts 288 ``` 289 290 Example Output: 291 292 ```bash 293 accounts: 294 - '@type': /cosmos.auth.v1beta1.BaseAccount 295 account_number: "0" 296 address: cosmos1zwg6tpl8aw4rawv8sgag9086lpw5hv33u5ctr2 297 pub_key: 298 '@type': /cosmos.crypto.secp256k1.PubKey 299 key: ApDrE38zZdd7wLmFS9YmqO684y5DG6fjZ4rVeihF/AQD 300 sequence: "1" 301 - '@type': /cosmos.auth.v1beta1.ModuleAccount 302 base_account: 303 account_number: "8" 304 address: cosmos1yl6hdjhmkf37639730gffanpzndzdpmhwlkfhr 305 pub_key: null 306 sequence: "0" 307 name: transfer 308 permissions: 309 - minter 310 - burner 311 - '@type': /cosmos.auth.v1beta1.ModuleAccount 312 base_account: 313 account_number: "4" 314 address: cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh 315 pub_key: null 316 sequence: "0" 317 name: bonded_tokens_pool 318 permissions: 319 - burner 320 - staking 321 - '@type': /cosmos.auth.v1beta1.ModuleAccount 322 base_account: 323 account_number: "5" 324 address: cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r 325 pub_key: null 326 sequence: "0" 327 name: not_bonded_tokens_pool 328 permissions: 329 - burner 330 - staking 331 - '@type': /cosmos.auth.v1beta1.ModuleAccount 332 base_account: 333 account_number: "6" 334 address: cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn 335 pub_key: null 336 sequence: "0" 337 name: gov 338 permissions: 339 - burner 340 - '@type': /cosmos.auth.v1beta1.ModuleAccount 341 base_account: 342 account_number: "3" 343 address: cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl 344 pub_key: null 345 sequence: "0" 346 name: distribution 347 permissions: [] 348 - '@type': /cosmos.auth.v1beta1.BaseAccount 349 account_number: "1" 350 address: cosmos147k3r7v2tvwqhcmaxcfql7j8rmkrlsemxshd3j 351 pub_key: null 352 sequence: "0" 353 - '@type': /cosmos.auth.v1beta1.ModuleAccount 354 base_account: 355 account_number: "7" 356 address: cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q 357 pub_key: null 358 sequence: "0" 359 name: mint 360 permissions: 361 - minter 362 - '@type': /cosmos.auth.v1beta1.ModuleAccount 363 base_account: 364 account_number: "2" 365 address: cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta 366 pub_key: null 367 sequence: "0" 368 name: fee_collector 369 permissions: [] 370 pagination: 371 next_key: null 372 total: "0" 373 ``` 374 375 #### params 376 377 The `params` command allow users to query the current auth parameters. 378 379 ```bash 380 simd query auth params [flags] 381 ``` 382 383 Example: 384 385 ```bash 386 simd query auth params 387 ``` 388 389 Example Output: 390 391 ```bash 392 max_memo_characters: "256" 393 sig_verify_cost_ed25519: "590" 394 sig_verify_cost_secp256k1: "1000" 395 tx_sig_limit: "7" 396 tx_size_cost_per_byte: "10" 397 ``` 398 399 ### Transactions 400 401 The `auth` module supports transactions commands to help you with signing and more. Compared to other modules you can access directly the `auth` module transactions commands using the only `tx` command. 402 403 Use directly the `--help` flag to get more information about the `tx` command. 404 405 ```bash 406 simd tx --help 407 ``` 408 409 #### `sign` 410 411 The `sign` command allows users to sign transactions that was generated offline. 412 413 ```bash 414 simd tx sign tx.json --from $ALICE > tx.signed.json 415 ``` 416 417 The result is a signed transaction that can be broadcasted to the network thanks to the broadcast command. 418 419 More information about the `sign` command can be found running `simd tx sign --help`. 420 421 #### `sign-batch` 422 423 The `sign-batch` command allows users to sign multiples offline generated transactions. 424 The transactions can be in one file, with one tx per line, or in multiple files. 425 426 ```bash 427 simd tx sign txs.json --from $ALICE > tx.signed.json 428 ``` 429 430 or 431 432 ```bash 433 simd tx sign tx1.json tx2.json tx3.json --from $ALICE > tx.signed.json 434 ``` 435 436 The result is multiples signed transactions. For combining the signed transactions into one transactions, use the `--append` flag. 437 438 More information about the `sign-batch` command can be found running `simd tx sign-batch --help`. 439 440 #### `multi-sign` 441 442 The `multi-sign` command allows users to sign transactions that was generated offline by a multisig account. 443 444 ```bash 445 simd tx multisign transaction.json k1k2k3 k1sig.json k2sig.json k3sig.json 446 ``` 447 448 Where `k1k2k3` is the multisig account address, `k1sig.json` is the signature of the first signer, `k2sig.json` is the signature of the second signer, and `k3sig.json` is the signature of the third signer. 449 450 ##### Nested multisig transactions 451 452 To allow transactions to be signed by nested multisigs, meaning that a participant of a multisig account can be another multisig account, the `--skip-signature-verification` flag must be used. 453 454 ```bash 455 # First aggregate signatures of the multisig participant 456 simd tx multi-sign transaction.json ms1 ms1p1sig.json ms1p2sig.json --signature-only --skip-signature-verification > ms1sig.json 457 458 # Then use the aggregated signatures and the other signatures to sign the final transaction 459 simd tx multi-sign transaction.json k1ms1 k1sig.json ms1sig.json --skip-signature-verification 460 ``` 461 462 Where `ms1` is the nested multisig account address, `ms1p1sig.json` is the signature of the first participant of the nested multisig account, `ms1p2sig.json` is the signature of the second participant of the nested multisig account, and `ms1sig.json` is the aggregated signature of the nested multisig account. 463 464 `k1ms1` is a multisig account comprised of an individual signer and another nested multisig account (`ms1`). `k1sig.json` is the signature of the first signer of the individual member. 465 466 More information about the `multi-sign` command can be found running `simd tx multi-sign --help`. 467 468 #### `multisign-batch` 469 470 The `multisign-batch` works the same way as `sign-batch`, but for multisig accounts. 471 With the difference that the `multisign-batch` command requires all transactions to be in one file, and the `--append` flag does not exist. 472 473 More information about the `multisign-batch` command can be found running `simd tx multisign-batch --help`. 474 475 #### `validate-signatures` 476 477 The `validate-signatures` command allows users to validate the signatures of a signed transaction. 478 479 ```bash 480 $ simd tx validate-signatures tx.signed.json 481 Signers: 482 0: cosmos1l6vsqhh7rnwsyr2kyz3jjg3qduaz8gwgyl8275 483 484 Signatures: 485 0: cosmos1l6vsqhh7rnwsyr2kyz3jjg3qduaz8gwgyl8275 [OK] 486 ``` 487 488 More information about the `validate-signatures` command can be found running `simd tx validate-signatures --help`. 489 490 #### `broadcast` 491 492 The `broadcast` command allows users to broadcast a signed transaction to the network. 493 494 ```bash 495 simd tx broadcast tx.signed.json 496 ``` 497 498 More information about the `broadcast` command can be found running `simd tx broadcast --help`. 499 500 501 ### gRPC 502 503 A user can query the `auth` module using gRPC endpoints. 504 505 #### Account 506 507 The `account` endpoint allow users to query for an account by it's address. 508 509 ```bash 510 cosmos.auth.v1beta1.Query/Account 511 ``` 512 513 Example: 514 515 ```bash 516 grpcurl -plaintext \ 517 -d '{"address":"cosmos1.."}' \ 518 localhost:9090 \ 519 cosmos.auth.v1beta1.Query/Account 520 ``` 521 522 Example Output: 523 524 ```bash 525 { 526 "account":{ 527 "@type":"/cosmos.auth.v1beta1.BaseAccount", 528 "address":"cosmos1zwg6tpl8aw4rawv8sgag9086lpw5hv33u5ctr2", 529 "pubKey":{ 530 "@type":"/cosmos.crypto.secp256k1.PubKey", 531 "key":"ApDrE38zZdd7wLmFS9YmqO684y5DG6fjZ4rVeihF/AQD" 532 }, 533 "sequence":"1" 534 } 535 } 536 ``` 537 538 #### Accounts 539 540 The `accounts` endpoint allow users to query all the available accounts. 541 542 ```bash 543 cosmos.auth.v1beta1.Query/Accounts 544 ``` 545 546 Example: 547 548 ```bash 549 grpcurl -plaintext \ 550 localhost:9090 \ 551 cosmos.auth.v1beta1.Query/Accounts 552 ``` 553 554 Example Output: 555 556 ```bash 557 { 558 "accounts":[ 559 { 560 "@type":"/cosmos.auth.v1beta1.BaseAccount", 561 "address":"cosmos1zwg6tpl8aw4rawv8sgag9086lpw5hv33u5ctr2", 562 "pubKey":{ 563 "@type":"/cosmos.crypto.secp256k1.PubKey", 564 "key":"ApDrE38zZdd7wLmFS9YmqO684y5DG6fjZ4rVeihF/AQD" 565 }, 566 "sequence":"1" 567 }, 568 { 569 "@type":"/cosmos.auth.v1beta1.ModuleAccount", 570 "baseAccount":{ 571 "address":"cosmos1yl6hdjhmkf37639730gffanpzndzdpmhwlkfhr", 572 "accountNumber":"8" 573 }, 574 "name":"transfer", 575 "permissions":[ 576 "minter", 577 "burner" 578 ] 579 }, 580 { 581 "@type":"/cosmos.auth.v1beta1.ModuleAccount", 582 "baseAccount":{ 583 "address":"cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh", 584 "accountNumber":"4" 585 }, 586 "name":"bonded_tokens_pool", 587 "permissions":[ 588 "burner", 589 "staking" 590 ] 591 }, 592 { 593 "@type":"/cosmos.auth.v1beta1.ModuleAccount", 594 "baseAccount":{ 595 "address":"cosmos1tygms3xhhs3yv487phx3dw4a95jn7t7lpm470r", 596 "accountNumber":"5" 597 }, 598 "name":"not_bonded_tokens_pool", 599 "permissions":[ 600 "burner", 601 "staking" 602 ] 603 }, 604 { 605 "@type":"/cosmos.auth.v1beta1.ModuleAccount", 606 "baseAccount":{ 607 "address":"cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn", 608 "accountNumber":"6" 609 }, 610 "name":"gov", 611 "permissions":[ 612 "burner" 613 ] 614 }, 615 { 616 "@type":"/cosmos.auth.v1beta1.ModuleAccount", 617 "baseAccount":{ 618 "address":"cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl", 619 "accountNumber":"3" 620 }, 621 "name":"distribution" 622 }, 623 { 624 "@type":"/cosmos.auth.v1beta1.BaseAccount", 625 "accountNumber":"1", 626 "address":"cosmos147k3r7v2tvwqhcmaxcfql7j8rmkrlsemxshd3j" 627 }, 628 { 629 "@type":"/cosmos.auth.v1beta1.ModuleAccount", 630 "baseAccount":{ 631 "address":"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q", 632 "accountNumber":"7" 633 }, 634 "name":"mint", 635 "permissions":[ 636 "minter" 637 ] 638 }, 639 { 640 "@type":"/cosmos.auth.v1beta1.ModuleAccount", 641 "baseAccount":{ 642 "address":"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta", 643 "accountNumber":"2" 644 }, 645 "name":"fee_collector" 646 } 647 ], 648 "pagination":{ 649 "total":"9" 650 } 651 } 652 ``` 653 654 #### Params 655 656 The `params` endpoint allow users to query the current auth parameters. 657 658 ```bash 659 cosmos.auth.v1beta1.Query/Params 660 ``` 661 662 Example: 663 664 ```bash 665 grpcurl -plaintext \ 666 localhost:9090 \ 667 cosmos.auth.v1beta1.Query/Params 668 ``` 669 670 Example Output: 671 672 ```bash 673 { 674 "params": { 675 "maxMemoCharacters": "256", 676 "txSigLimit": "7", 677 "txSizeCostPerByte": "10", 678 "sigVerifyCostEd25519": "590", 679 "sigVerifyCostSecp256k1": "1000" 680 } 681 } 682 ``` 683 684 ### REST 685 686 A user can query the `auth` module using REST endpoints. 687 688 #### Account 689 690 The `account` endpoint allow users to query for an account by it's address. 691 692 ```bash 693 /cosmos/auth/v1beta1/account?address={address} 694 ``` 695 696 #### Accounts 697 698 The `accounts` endpoint allow users to query all the available accounts. 699 700 ```bash 701 /cosmos/auth/v1beta1/accounts 702 ``` 703 704 #### Params 705 706 The `params` endpoint allow users to query the current auth parameters. 707 708 ```bash 709 /cosmos/auth/v1beta1/params 710 ```