cmd: utils.go
This commit is contained in:
parent
241c163876
commit
4ff02fd681
|
@ -7,18 +7,14 @@ import (
|
|||
|
||||
"github.com/urfave/cli"
|
||||
|
||||
"github.com/tendermint/basecoin/types"
|
||||
cmn "github.com/tendermint/go-common"
|
||||
client "github.com/tendermint/go-rpc/client"
|
||||
"github.com/tendermint/go-wire"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
)
|
||||
|
||||
func cmdAccount(c *cli.Context) error {
|
||||
if len(c.Args()) != 1 {
|
||||
return errors.New("account command requires an argument ([address])")
|
||||
}
|
||||
addrHex := c.Args()[0]
|
||||
addrHex := stripHex(c.Args()[0])
|
||||
|
||||
// convert destination address to bytes
|
||||
addr, err := hex.DecodeString(addrHex)
|
||||
|
@ -26,44 +22,10 @@ func cmdAccount(c *cli.Context) error {
|
|||
return errors.New("Account address is invalid hex: " + err.Error())
|
||||
}
|
||||
|
||||
acc, err := getAcc(c, addr)
|
||||
acc, err := getAcc(c.String("tendermint"), addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(wire.JSONBytes(acc)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// fetch the account by querying the app
|
||||
func getAcc(c *cli.Context, address []byte) (*types.Account, error) {
|
||||
tmAddr := c.String("tendermint")
|
||||
clientURI := client.NewClientURI(tmAddr)
|
||||
tmResult := new(ctypes.TMResult)
|
||||
|
||||
params := map[string]interface{}{
|
||||
"path": "/key",
|
||||
"data": append([]byte("base/a/"), address...),
|
||||
"prove": false,
|
||||
}
|
||||
_, err := clientURI.Call("abci_query", params, tmResult)
|
||||
if err != nil {
|
||||
return nil, errors.New(cmn.Fmt("Error calling /abci_query: %v", err))
|
||||
}
|
||||
res := (*tmResult).(*ctypes.ResultABCIQuery)
|
||||
if !res.Response.Code.IsOK() {
|
||||
return nil, errors.New(cmn.Fmt("Query got non-zero exit code: %v. %s", res.Response.Code, res.Response.Log))
|
||||
}
|
||||
accountBytes := res.Response.Value
|
||||
|
||||
if len(accountBytes) == 0 {
|
||||
return nil, errors.New(cmn.Fmt("Account bytes are empty from query for address %X", address))
|
||||
}
|
||||
var acc *types.Account
|
||||
err = wire.ReadBinaryBytes(accountBytes, &acc)
|
||||
if err != nil {
|
||||
return nil, errors.New(cmn.Fmt("Error reading account %X error: %v",
|
||||
accountBytes, err.Error()))
|
||||
}
|
||||
|
||||
return acc, nil
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ func cmdSendTx(c *cli.Context) error {
|
|||
chainID := c.String("chain_id")
|
||||
|
||||
// convert destination address to bytes
|
||||
to, err := hex.DecodeString(toHex)
|
||||
to, err := hex.DecodeString(stripHex(toHex))
|
||||
if err != nil {
|
||||
return errors.New("To address is invalid hex: " + err.Error())
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ func cmdAppTx(c *cli.Context) error {
|
|||
|
||||
// convert data to bytes
|
||||
data := []byte(dataString)
|
||||
if cmn.IsHex(dataString) {
|
||||
if isHex(dataString) {
|
||||
data, _ = hex.DecodeString(dataString)
|
||||
}
|
||||
|
||||
|
@ -135,34 +135,10 @@ func getSeq(c *cli.Context, address []byte) (int, error) {
|
|||
return c.Int("sequence"), nil
|
||||
}
|
||||
tmAddr := c.String("tendermint")
|
||||
clientURI := client.NewClientURI(tmAddr)
|
||||
tmResult := new(ctypes.TMResult)
|
||||
|
||||
params := map[string]interface{}{
|
||||
"path": "/key",
|
||||
"data": append([]byte("base/a/"), address...),
|
||||
"prove": false,
|
||||
}
|
||||
_, err := clientURI.Call("abci_query", params, tmResult)
|
||||
acc, err := getAcc(tmAddr, address)
|
||||
if err != nil {
|
||||
return 0, errors.New(cmn.Fmt("Error calling /abci_query: %v", err))
|
||||
return 0, err
|
||||
}
|
||||
res := (*tmResult).(*ctypes.ResultABCIQuery)
|
||||
if !res.Response.Code.IsOK() {
|
||||
return 0, errors.New(cmn.Fmt("Query got non-zero exit code: %v. %s", res.Response.Code, res.Response.Log))
|
||||
}
|
||||
accountBytes := res.Response.Value
|
||||
|
||||
if len(accountBytes) == 0 {
|
||||
return 0, errors.New(cmn.Fmt("Account bytes are empty from query for address %X", address))
|
||||
}
|
||||
var acc *types.Account
|
||||
err = wire.ReadBinaryBytes(accountBytes, &acc)
|
||||
if err != nil {
|
||||
return 0, errors.New(cmn.Fmt("Error reading account %X error: %v",
|
||||
accountBytes, err.Error()))
|
||||
}
|
||||
|
||||
return acc.Sequence + 1, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
||||
"github.com/tendermint/basecoin/types"
|
||||
|
||||
cmn "github.com/tendermint/go-common"
|
||||
client "github.com/tendermint/go-rpc/client"
|
||||
"github.com/tendermint/go-wire"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
)
|
||||
|
||||
// Returns true for non-empty hex-string prefixed with "0x"
|
||||
func isHex(s string) bool {
|
||||
if len(s) > 2 && s[:2] == "0x" {
|
||||
_, err := hex.DecodeString(s[2:])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func stripHex(s string) string {
|
||||
if isHex(s) {
|
||||
return s[2:]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// fetch the account by querying the app
|
||||
func getAcc(tmAddr string, address []byte) (*types.Account, error) {
|
||||
clientURI := client.NewClientURI(tmAddr)
|
||||
tmResult := new(ctypes.TMResult)
|
||||
|
||||
params := map[string]interface{}{
|
||||
"path": "/key",
|
||||
"data": append([]byte("base/a/"), address...),
|
||||
"prove": false,
|
||||
}
|
||||
_, err := clientURI.Call("abci_query", params, tmResult)
|
||||
if err != nil {
|
||||
return nil, errors.New(cmn.Fmt("Error calling /abci_query: %v", err))
|
||||
}
|
||||
res := (*tmResult).(*ctypes.ResultABCIQuery)
|
||||
if !res.Response.Code.IsOK() {
|
||||
return nil, errors.New(cmn.Fmt("Query got non-zero exit code: %v. %s", res.Response.Code, res.Response.Log))
|
||||
}
|
||||
accountBytes := res.Response.Value
|
||||
|
||||
if len(accountBytes) == 0 {
|
||||
return nil, errors.New(cmn.Fmt("Account bytes are empty from query for address %X", address))
|
||||
}
|
||||
var acc *types.Account
|
||||
err = wire.ReadBinaryBytes(accountBytes, &acc)
|
||||
if err != nil {
|
||||
return nil, errors.New(cmn.Fmt("Error reading account %X error: %v",
|
||||
accountBytes, err.Error()))
|
||||
}
|
||||
|
||||
return acc, nil
|
||||
}
|
Loading…
Reference in New Issue