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

87 lines
1.8 KiB
Go
Raw Normal View History

2018-02-28 17:57:38 -08:00
package commands
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
2018-03-03 11:07:50 -08:00
"github.com/cosmos/cosmos-sdk/client/builder"
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
"github.com/cosmos/cosmos-sdk/x/auth"
)
// 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-03-20 18:22:15 -07:00
func GetAccountDecoder(cdc *wire.Codec) sdk.AccountDecoder {
2018-02-28 17:57:38 -08:00
return func(accBytes []byte) (sdk.Account, error) {
acct := new(auth.BaseAccount)
2018-03-04 16:45:31 -08:00
err := cdc.UnmarshalBinary(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
2018-03-03 11:07:50 -08:00
res, err := builder.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
output, err := json.MarshalIndent(account, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}