2020-07-08 04:12:38 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-25 04:17:18 -07:00
|
|
|
"errors"
|
|
|
|
"strings"
|
2020-07-08 04:12:38 -07:00
|
|
|
|
2021-02-22 06:21:46 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
|
|
|
|
2020-07-08 04:12:38 -07:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ types.QueryServer = AccountKeeper{}
|
|
|
|
|
2021-02-22 06:21:46 -08:00
|
|
|
func (ak AccountKeeper) Accounts(c context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
store := ctx.KVStore(ak.key)
|
|
|
|
accountsStore := prefix.NewStore(store, types.AddressStoreKeyPrefix)
|
|
|
|
|
|
|
|
var accounts []*codectypes.Any
|
|
|
|
pageRes, err := query.Paginate(accountsStore, req.Pagination, func(key, value []byte) error {
|
|
|
|
account := ak.decodeAccount(value)
|
|
|
|
any, err := codectypes.NewAnyWithValue(account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts = append(accounts, any)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "paginate: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryAccountsResponse{Accounts: accounts, Pagination: pageRes}, err
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:12:38 -07:00
|
|
|
// Account returns account details based on address
|
2020-07-28 05:53:35 -07:00
|
|
|
func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
|
2020-07-08 04:12:38 -07:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
if req.Address == "" {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "Address cannot be empty")
|
2020-07-08 04:12:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2020-09-25 03:25:37 -07:00
|
|
|
addr, err := sdk.AccAddressFromBech32(req.Address)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
account := ak.GetAccount(ctx, addr)
|
2020-07-08 04:12:38 -07:00
|
|
|
if account == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:47:31 -07:00
|
|
|
any, err := codectypes.NewAnyWithValue(account)
|
2020-07-08 04:12:38 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:47:31 -07:00
|
|
|
return &types.QueryAccountResponse{Account: any}, nil
|
2020-07-08 04:12:38 -07:00
|
|
|
}
|
|
|
|
|
2020-07-21 07:04:51 -07:00
|
|
|
// Params returns parameters of auth module
|
2020-07-28 05:53:35 -07:00
|
|
|
func (ak AccountKeeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
|
2020-07-08 04:12:38 -07:00
|
|
|
if req == nil {
|
2020-07-28 05:53:35 -07:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
2020-07-08 04:12:38 -07:00
|
|
|
}
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2020-07-28 05:53:35 -07:00
|
|
|
params := ak.GetParams(ctx)
|
2020-07-08 04:12:38 -07:00
|
|
|
|
2020-07-21 07:04:51 -07:00
|
|
|
return &types.QueryParamsResponse{Params: params}, nil
|
2020-07-08 04:12:38 -07:00
|
|
|
}
|
feat: Add cli for listing all module accounts (#9812)
## Description
This PR adds a CLI for querying all module accounts, `module-accounts`, in the auth module.
Using this command would display all the account information, including human readable name of the module, module account address, account number, permissions, etc.
This command would be especially useful for developers using cosmos-sdk as there are lots of instances where a developer would have to look into the module account, but only to do so by getting the module account address after getting the module name, hashing it and bech32fying it in the status quo. By using this command, users would be able to get a quick look on the list of all module accounts' information.
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [x] reviewed tests and test coverage
- [x] manually tested (if applicable)
2021-08-02 11:47:43 -07:00
|
|
|
|
|
|
|
// ModuleAccounts returns all the existing Module Accounts
|
|
|
|
func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModuleAccountsRequest) (*types.QueryModuleAccountsResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
|
|
|
modAccounts := make([]*codectypes.Any, 0, len(ak.permAddrs))
|
|
|
|
|
|
|
|
for moduleName := range ak.permAddrs {
|
|
|
|
account := ak.GetModuleAccount(ctx, moduleName)
|
|
|
|
if account == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName)
|
|
|
|
}
|
|
|
|
any, err := codectypes.NewAnyWithValue(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
modAccounts = append(modAccounts, any)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryModuleAccountsResponse{Accounts: modAccounts}, nil
|
|
|
|
}
|
2021-08-25 04:17:18 -07:00
|
|
|
|
|
|
|
func (ak AccountKeeper) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
|
|
|
|
bech32Prefix, err := ak.getBech32Prefix()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Bech32PrefixResponse{Bech32Prefix: bech32Prefix}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.AddressBytesToStringRequest) (*types.AddressBytesToStringResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.AddressBytes) == 0 {
|
|
|
|
return nil, errors.New("empty address bytes is not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
text, err := ak.addressCdc.BytesToString(req.AddressBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.AddressBytesToStringResponse{AddressString: text}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.AddressStringToBytesRequest) (*types.AddressStringToBytesResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(req.AddressString)) == 0 {
|
|
|
|
return nil, errors.New("empty address string is not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := ak.addressCdc.StringToBytes(req.AddressString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.AddressStringToBytesResponse{AddressBytes: bz}, nil
|
|
|
|
}
|