Modified CLI context

This commit is contained in:
Tony Stark 2019-03-01 01:10:55 +05:30
parent b42410ea80
commit cf18b5a11a
No known key found for this signature in database
GPG Key ID: CB0E78E8A3D60001
1 changed files with 21 additions and 20 deletions

View File

@ -66,15 +66,13 @@ func (ctx CLIContext) GetAccount(address []byte) (auth.Account, error) {
return nil, errors.New("account decoder required but not provided")
}
res, err := ctx.QueryStore(auth.AddressStoreKey(address), ctx.AccountStore)
res, err := ctx.queryAccount(address)
if err != nil {
return nil, err
} else if len(res) == 0 {
return nil, ErrInvalidAccount(address)
}
account, err := ctx.AccDecoder(res)
if err != nil {
var account auth.Account
if err := ctx.Codec.UnmarshalJSON(res, &account); err != nil {
return nil, err
}
@ -117,32 +115,35 @@ func (ctx CLIContext) GetAccountSequence(address []byte) (uint64, error) {
// error is returned if it does not.
func (ctx CLIContext) EnsureAccountExists() error {
addr := ctx.GetFromAddress()
accountBytes, err := ctx.QueryStore(auth.AddressStoreKey(addr), ctx.AccountStore)
if err != nil {
return err
}
if len(accountBytes) == 0 {
return ErrInvalidAccount(addr)
}
return nil
return ctx.EnsureAccountExistsFromAddr(addr)
}
// EnsureAccountExistsFromAddr ensures that an account exists for a given
// address. Instead of using the context's from name, a direct address is
// given. An error is returned if it does not.
func (ctx CLIContext) EnsureAccountExistsFromAddr(addr sdk.AccAddress) error {
accountBytes, err := ctx.QueryStore(auth.AddressStoreKey(addr), ctx.AccountStore)
if err != nil {
_, err := ctx.queryAccount(addr)
return err
}
// queryAccount queries an account using custom query endpoint of auth module
// returns an error if result is `null` otherwise account data
func (ctx CLIContext) queryAccount(addr sdk.AccAddress) ([]byte, error) {
bz, err := ctx.Codec.MarshalJSON(auth.NewQueryAccountParams(addr))
if err != nil {
return nil, err
}
if len(accountBytes) == 0 {
return ErrInvalidAccount(addr)
route := fmt.Sprintf("custom/%s/%s", ctx.AccountStore, auth.QueryAccount)
res, err := ctx.QueryWithData(route, bz)
if err != nil {
return nil, err
} else if len(res) == 4 && string(res) == "null" {
return nil, ErrInvalidAccount(addr)
}
return nil
return res, nil
}
// query performs a query from a Tendermint node with the provided store name