cosmos-sdk/x/auth/commands/account.go

88 lines
1.9 KiB
Go
Raw Normal View History

2018-02-28 17:57:38 -08:00
package commands
import (
"encoding/hex"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/context"
2018-02-28 17:57:38 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-02 01:24:07 -08:00
"github.com/cosmos/cosmos-sdk/wire"
2018-02-28 17:57:38 -08:00
)
// GetAccountCmd for the auth.BaseAccount type
func GetAccountCmdDefault(storeName string, cdc *wire.Codec) *cobra.Command {
2018-03-20 18:22:15 -07:00
return GetAccountCmd(storeName, cdc, GetAccountDecoder(cdc))
2018-02-28 17:57:38 -08:00
}
2018-04-18 21:49:24 -07:00
// Get account decoder for auth.DefaultAccount
2018-03-20 18:22:15 -07:00
func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder {
2018-04-07 10:56:49 -07:00
return func(accBytes []byte) (acct sdk.Account, err error) {
// acct := new(auth.BaseAccount)
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
2018-03-03 13:03:34 -08:00
if err != nil {
panic(err)
}
2018-02-28 17:57:38 -08:00
return acct, err
}
}
// GetAccountCmd returns a query account that will display the
// state of the account at a given address
2018-03-20 18:27:50 -07:00
func GetAccountCmd(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) *cobra.Command {
2018-02-28 17:57:38 -08:00
cmdr := commander{
storeName,
cdc,
2018-03-20 18:27:50 -07:00
decoder,
2018-02-28 17:57:38 -08:00
}
return &cobra.Command{
Use: "account <address>",
Short: "Query account balance",
RunE: cmdr.getAccountCmd,
}
}
type commander struct {
storeName string
cdc *wire.Codec
2018-03-20 18:27:50 -07:00
decoder sdk.AccountDecoder
2018-02-28 17:57:38 -08:00
}
func (c commander) getAccountCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 || len(args[0]) == 0 {
return errors.New("You must provide an account name")
}
// find the key to look up the account
addr := args[0]
bz, err := hex.DecodeString(addr)
if err != nil {
return err
}
2018-03-01 23:49:07 -08:00
key := sdk.Address(bz)
2018-02-28 17:57:38 -08:00
ctx := context.NewCoreContextFromViper()
2018-03-30 05:57:53 -07:00
res, err := ctx.Query(key, c.storeName)
if err != nil {
return err
}
2018-02-28 17:57:38 -08:00
2018-03-20 18:27:50 -07:00
// decode the value
account, err := c.decoder(res)
2018-02-28 17:57:38 -08:00
if err != nil {
return err
}
// print out whole account
2018-04-09 13:18:56 -07:00
output, err := wire.MarshalJSONIndent(c.cdc, account)
2018-02-28 17:57:38 -08:00
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}