2017-07-18 11:12:51 -07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2017-07-18 11:55:12 -07:00
|
|
|
"strings"
|
|
|
|
|
2017-07-18 11:12:51 -07:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
2017-08-04 10:21:22 -07:00
|
|
|
"github.com/spf13/viper"
|
2017-07-18 11:12:51 -07:00
|
|
|
|
2017-07-19 01:51:36 -07:00
|
|
|
lc "github.com/tendermint/light-client"
|
|
|
|
|
2017-08-21 14:13:58 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/commands"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/commands/query"
|
|
|
|
"github.com/cosmos/cosmos-sdk/modules/nonce"
|
|
|
|
"github.com/cosmos/cosmos-sdk/stack"
|
2017-07-18 11:12:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// NonceQueryCmd - command to query an nonce account
|
|
|
|
var NonceQueryCmd = &cobra.Command{
|
|
|
|
Use: "nonce [address]",
|
|
|
|
Short: "Get details of a nonce sequence number, with proof",
|
2017-07-19 05:25:17 -07:00
|
|
|
RunE: commands.RequireInit(nonceQueryCmd),
|
2017-07-18 11:12:51 -07:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:13:39 -07:00
|
|
|
func nonceQueryCmd(cmd *cobra.Command, args []string) error {
|
2017-07-18 11:55:12 -07:00
|
|
|
if len(args) == 0 {
|
|
|
|
return errors.New("Missing required argument [address]")
|
|
|
|
}
|
|
|
|
addr := strings.Join(args, ",")
|
2017-07-18 21:13:39 -07:00
|
|
|
|
2017-07-19 05:25:17 -07:00
|
|
|
signers, err := commands.ParseActors(addr)
|
2017-07-18 11:12:51 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-04 10:21:22 -07:00
|
|
|
seq, height, err := doNonceQuery(signers)
|
2017-07-18 21:13:39 -07:00
|
|
|
if err != nil {
|
2017-07-18 11:12:51 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:50:36 -07:00
|
|
|
return query.OutputProof(seq, height)
|
2017-07-18 11:12:51 -07:00
|
|
|
}
|
2017-07-18 21:13:39 -07:00
|
|
|
|
2017-08-21 14:13:58 -07:00
|
|
|
func doNonceQuery(signers []sdk.Actor) (sequence uint32, height uint64, err error) {
|
2017-07-18 21:13:39 -07:00
|
|
|
key := stack.PrefixedKey(nonce.NameNonce, nonce.GetSeqKey(signers))
|
2017-08-04 10:21:22 -07:00
|
|
|
prove := !viper.GetBool(commands.FlagTrustNode)
|
2017-08-04 11:50:36 -07:00
|
|
|
height, err = query.GetParsed(key, &sequence, prove)
|
2017-07-18 21:13:39 -07:00
|
|
|
if lc.IsNoDataErr(err) {
|
2017-07-19 04:32:55 -07:00
|
|
|
// no data, return sequence 0
|
2017-08-04 10:21:22 -07:00
|
|
|
return 0, 0, nil
|
2017-07-18 21:13:39 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|