github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/README.md (about) 1 --- 2 sidebar_position: 1 3 --- 4 5 # `x/authz` 6 7 ## Abstract 8 9 `x/authz` is an implementation of a Cosmos SDK module, per [ADR 30](https://github.com/cosmos/cosmos-sdk/blob/main/docs/architecture/adr-030-authz-module.md), that allows 10 granting arbitrary privileges from one account (the granter) to another account (the grantee). Authorizations must be granted for a particular Msg service method one by one using an implementation of the `Authorization` interface. 11 12 ## Contents 13 14 * [Concepts](#concepts) 15 * [Authorization and Grant](#authorization-and-grant) 16 * [Built-in Authorizations](#built-in-authorizations) 17 * [Gas](#gas) 18 * [State](#state) 19 * [Grant](#grant) 20 * [GrantQueue](#grantqueue) 21 * [Messages](#messages) 22 * [MsgGrant](#msggrant) 23 * [MsgRevoke](#msgrevoke) 24 * [MsgExec](#msgexec) 25 * [Events](#events) 26 * [Client](#client) 27 * [CLI](#cli) 28 * [gRPC](#grpc) 29 * [REST](#rest) 30 31 ## Concepts 32 33 ### Authorization and Grant 34 35 The `x/authz` module defines interfaces and messages grant authorizations to perform actions 36 on behalf of one account to other accounts. The design is defined in the [ADR 030](https://github.com/cosmos/cosmos-sdk/blob/main/docs/architecture/adr-030-authz-module.md). 37 38 A *grant* is an allowance to execute a Msg by the grantee on behalf of the granter. 39 Authorization is an interface that must be implemented by a concrete authorization logic to validate and execute grants. Authorizations are extensible and can be defined for any Msg service method even outside of the module where the Msg method is defined. See the `SendAuthorization` example in the next section for more details. 40 41 **Note:** The authz module is different from the [auth (authentication)](../modules/auth/) module that is responsible for specifying the base transaction and account types. 42 43 ```go reference 44 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/x/authz/authorizations.go#L11-L25 45 ``` 46 47 ### Built-in Authorizations 48 49 The Cosmos SDK `x/authz` module comes with following authorization types: 50 51 #### GenericAuthorization 52 53 `GenericAuthorization` implements the `Authorization` interface that gives unrestricted permission to execute the provided Msg on behalf of granter's account. 54 55 ```protobuf reference 56 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/authz/v1beta1/authz.proto#L14-L22 57 ``` 58 59 ```go reference 60 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/x/authz/generic_authorization.go#L16-L29 61 ``` 62 63 * `msg` stores Msg type URL. 64 65 #### SendAuthorization 66 67 `SendAuthorization` implements the `Authorization` interface for the `cosmos.bank.v1beta1.MsgSend` Msg. 68 69 * It takes a (positive) `SpendLimit` that specifies the maximum amount of tokens the grantee can spend. The `SpendLimit` is updated as the tokens are spent. 70 * It takes an (optional) `AllowList` that specifies to which addresses a grantee can send token. 71 72 ```protobuf reference 73 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/bank/v1beta1/authz.proto#L11-L30 74 ``` 75 76 ```go reference 77 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/x/bank/types/send_authorization.go#L29-L62 78 ``` 79 80 * `spend_limit` keeps track of how many coins are left in the authorization. 81 * `allow_list` specifies an optional list of addresses to whom the grantee can send tokens on behalf of the granter. 82 83 #### StakeAuthorization 84 85 `StakeAuthorization` implements the `Authorization` interface for messages in the [staking module](https://docs.cosmos.network/v0.50/build/modules/staking). It takes an `AuthorizationType` to specify whether you want to authorise delegating, undelegating or redelegating (i.e. these have to be authorised separately). It also takes an optional `MaxTokens` that keeps track of a limit to the amount of tokens that can be delegated/undelegated/redelegated. If left empty, the amount is unlimited. Additionally, this Msg takes an `AllowList` or a `DenyList`, which allows you to select which validators you allow or deny grantees to stake with. 86 87 ```protobuf reference 88 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/staking/v1beta1/authz.proto#L11-L35 89 ``` 90 91 ```go reference 92 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/x/staking/types/authz.go#L15-L35 93 ``` 94 95 ### Gas 96 97 In order to prevent DoS attacks, granting `StakeAuthorization`s with `x/authz` incurs gas. `StakeAuthorization` allows you to authorize another account to delegate, undelegate, or redelegate to validators. The authorizer can define a list of validators they allow or deny delegations to. The Cosmos SDK iterates over these lists and charge 10 gas for each validator in both of the lists. 98 99 Since the state maintaining a list for granter, grantee pair with same expiration, we are iterating over the list to remove the grant (incase of any revoke of paritcular `msgType`) from the list and we are charging 20 gas per iteration. 100 101 ## State 102 103 ### Grant 104 105 Grants are identified by combining granter address (the address bytes of the granter), grantee address (the address bytes of the grantee) and Authorization type (its type URL). Hence we only allow one grant for the (granter, grantee, Authorization) triple. 106 107 * Grant: `0x01 | granter_address_len (1 byte) | granter_address_bytes | grantee_address_len (1 byte) | grantee_address_bytes | msgType_bytes -> ProtocolBuffer(AuthorizationGrant)` 108 109 The grant object encapsulates an `Authorization` type and an expiration timestamp: 110 111 ```protobuf reference 112 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/authz/v1beta1/authz.proto#L24-L32 113 ``` 114 115 ### GrantQueue 116 117 We are maintaining a queue for authz pruning. Whenever a grant is created, an item will be added to `GrantQueue` with a key of expiration, granter, grantee. 118 119 In `EndBlock` (which runs for every block) we continuously check and prune the expired grants by forming a prefix key with current blocktime that passed the stored expiration in `GrantQueue`, we iterate through all the matched records from `GrantQueue` and delete them from the `GrantQueue` & `Grant`s store. 120 121 ```go reference 122 https://github.com/cosmos/cosmos-sdk/blob/5f4ddc6f80f9707320eec42182184207fff3833a/x/authz/keeper/keeper.go#L378-L403 123 ``` 124 125 * GrantQueue: `0x02 | expiration_bytes | granter_address_len (1 byte) | granter_address_bytes | grantee_address_len (1 byte) | grantee_address_bytes -> ProtocalBuffer(GrantQueueItem)` 126 127 The `expiration_bytes` are the expiration date in UTC with the format `"2006-01-02T15:04:05.000000000"`. 128 129 ```go reference 130 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/x/authz/keeper/keys.go#L77-L93 131 ``` 132 133 The `GrantQueueItem` object contains the list of type urls between granter and grantee that expire at the time indicated in the key. 134 135 ## Messages 136 137 In this section we describe the processing of messages for the authz module. 138 139 ### MsgGrant 140 141 An authorization grant is created using the `MsgGrant` message. 142 If there is already a grant for the `(granter, grantee, Authorization)` triple, then the new grant overwrites the previous one. To update or extend an existing grant, a new grant with the same `(granter, grantee, Authorization)` triple should be created. 143 144 ```protobuf reference 145 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/authz/v1beta1/tx.proto#L35-L45 146 ``` 147 148 The message handling should fail if: 149 150 * both granter and grantee have the same address. 151 * provided `Expiration` time is less than current unix timestamp (but a grant will be created if no `expiration` time is provided since `expiration` is optional). 152 * provided `Grant.Authorization` is not implemented. 153 * `Authorization.MsgTypeURL()` is not defined in the router (there is no defined handler in the app router to handle that Msg types). 154 155 ### MsgRevoke 156 157 A grant can be removed with the `MsgRevoke` message. 158 159 ```protobuf reference 160 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/authz/v1beta1/tx.proto#L69-L78 161 ``` 162 163 The message handling should fail if: 164 165 * both granter and grantee have the same address. 166 * provided `MsgTypeUrl` is empty. 167 168 NOTE: The `MsgExec` message removes a grant if the grant has expired. 169 170 ### MsgExec 171 172 When a grantee wants to execute a transaction on behalf of a granter, they must send `MsgExec`. 173 174 ```protobuf reference 175 https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/authz/v1beta1/tx.proto#L52-L63 176 ``` 177 178 The message handling should fail if: 179 180 * provided `Authorization` is not implemented. 181 * grantee doesn't have permission to run the transaction. 182 * if granted authorization is expired. 183 184 ## Events 185 186 The authz module emits proto events defined in [the Protobuf reference](https://buf.build/cosmos/cosmos-sdk/docs/main/cosmos.authz.v1beta1#cosmos.authz.v1beta1.EventGrant). 187 188 ## Client 189 190 ### CLI 191 192 A user can query and interact with the `authz` module using the CLI. 193 194 #### Query 195 196 The `query` commands allow users to query `authz` state. 197 198 ```bash 199 simd query authz --help 200 ``` 201 202 ##### grants 203 204 The `grants` command allows users to query grants for a granter-grantee pair. If the message type URL is set, it selects grants only for that message type. 205 206 ```bash 207 simd query authz grants [granter-addr] [grantee-addr] [msg-type-url]? [flags] 208 ``` 209 210 Example: 211 212 ```bash 213 simd query authz grants cosmos1.. cosmos1.. /cosmos.bank.v1beta1.MsgSend 214 ``` 215 216 Example Output: 217 218 ```bash 219 grants: 220 - authorization: 221 '@type': /cosmos.bank.v1beta1.SendAuthorization 222 spend_limit: 223 - amount: "100" 224 denom: stake 225 expiration: "2022-01-01T00:00:00Z" 226 pagination: null 227 ``` 228 229 #### Transactions 230 231 The `tx` commands allow users to interact with the `authz` module. 232 233 ```bash 234 simd tx authz --help 235 ``` 236 237 ##### exec 238 239 The `exec` command allows a grantee to execute a transaction on behalf of granter. 240 241 ```bash 242 simd tx authz exec [tx-json-file] --from [grantee] [flags] 243 ``` 244 245 Example: 246 247 ```bash 248 simd tx authz exec tx.json --from=cosmos1.. 249 ``` 250 251 ##### grant 252 253 The `grant` command allows a granter to grant an authorization to a grantee. 254 255 ```bash 256 simd tx authz grant <grantee> <authorization_type="send"|"generic"|"delegate"|"unbond"|"redelegate"> --from <granter> [flags] 257 ``` 258 - The `send` authorization_type refers to the built-in `SendAuthorization` type. The custom flags available are `spend-limit` (required) and `allow-list` (optional) , documented [here](#SendAuthorization) 259 260 Example: 261 262 ```bash 263 simd tx authz grant cosmos1.. send --spend-limit=100stake --allow-list=cosmos1...,cosmos2... --from=cosmos1.. 264 ``` 265 - The `generic` authorization_type refers to the built-in `GenericAuthorization` type. The custom flag available is `msg-type` ( required) documented [here](#GenericAuthorization). 266 267 > Note: `msg-type` is any valid Cosmos SDK `Msg` type url. 268 269 Example: 270 ```bash 271 simd tx authz grant cosmos1.. generic --msg-type=/cosmos.bank.v1beta1.MsgSend --from=cosmos1.. 272 ``` 273 - The `delegate`,`unbond`,`redelegate` authorization_types refer to the built-in `StakeAuthorization` type. The custom flags available are `spend-limit` (optional), `allowed-validators` (optional) and `deny-validators` (optional) documented [here](#StakeAuthorization). 274 > Note: `allowed-validators` and `deny-validators` cannot both be empty. `spend-limit` represents the `MaxTokens` 275 276 Example: 277 278 ```bash 279 simd tx authz grant cosmos1.. delegate --spend-limit=100stake --allowed-validators=cosmos...,cosmos... --deny-validators=cosmos... --from=cosmos1.. 280 ``` 281 282 ##### revoke 283 284 The `revoke` command allows a granter to revoke an authorization from a grantee. 285 286 ```bash 287 simd tx authz revoke [grantee] [msg-type-url] --from=[granter] [flags] 288 ``` 289 290 Example: 291 292 ```bash 293 simd tx authz revoke cosmos1.. /cosmos.bank.v1beta1.MsgSend --from=cosmos1.. 294 ``` 295 296 ### gRPC 297 298 A user can query the `authz` module using gRPC endpoints. 299 300 #### Grants 301 302 The `Grants` endpoint allows users to query grants for a granter-grantee pair. If the message type URL is set, it selects grants only for that message type. 303 304 ```bash 305 cosmos.authz.v1beta1.Query/Grants 306 ``` 307 308 Example: 309 310 ```bash 311 grpcurl -plaintext \ 312 -d '{"granter":"cosmos1..","grantee":"cosmos1..","msg_type_url":"/cosmos.bank.v1beta1.MsgSend"}' \ 313 localhost:9090 \ 314 cosmos.authz.v1beta1.Query/Grants 315 ``` 316 317 Example Output: 318 319 ```bash 320 { 321 "grants": [ 322 { 323 "authorization": { 324 "@type": "/cosmos.bank.v1beta1.SendAuthorization", 325 "spendLimit": [ 326 { 327 "denom":"stake", 328 "amount":"100" 329 } 330 ] 331 }, 332 "expiration": "2022-01-01T00:00:00Z" 333 } 334 ] 335 } 336 ``` 337 338 ### REST 339 340 A user can query the `authz` module using REST endpoints. 341 342 ```bash 343 /cosmos/authz/v1beta1/grants 344 ``` 345 346 Example: 347 348 ```bash 349 curl "localhost:1317/cosmos/authz/v1beta1/grants?granter=cosmos1..&grantee=cosmos1..&msg_type_url=/cosmos.bank.v1beta1.MsgSend" 350 ``` 351 352 Example Output: 353 354 ```bash 355 { 356 "grants": [ 357 { 358 "authorization": { 359 "@type": "/cosmos.bank.v1beta1.SendAuthorization", 360 "spend_limit": [ 361 { 362 "denom": "stake", 363 "amount": "100" 364 } 365 ] 366 }, 367 "expiration": "2022-01-01T00:00:00Z" 368 } 369 ], 370 "pagination": null 371 } 372 ```