Add roles wrapper/tx/query command to basecoin

This commit is contained in:
Ethan Frey
2017-07-19 14:26:40 +02:00
parent 737e3740dd
commit 911dd1423e
12 changed files with 219 additions and 65 deletions
+3 -22
View File
@@ -37,31 +37,12 @@ func sendTxCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
tx, err = txcmd.Middleware.Wrap(tx)
if err != nil {
return err
}
err = txcmd.SignTx(tx)
if err != nil {
return err
}
// otherwise, post it and display response
bres, err := txcmd.PrepareOrPostTx(tx)
if err != nil {
return err
}
if bres == nil {
return nil // successful prep, nothing left to do
}
return txcmd.OutputTx(bres) // print response of the post
return txcmd.DoTx(tx)
}
func readSendTxFlags() (tx basecoin.Tx, err error) {
// parse to address
toAddr, err := commands.ParseAddress(viper.GetString(FlagTo))
toAddr, err := commands.ParseActor(viper.GetString(FlagTo))
if err != nil {
return tx, err
}
@@ -94,5 +75,5 @@ func readFromAddr() (basecoin.Actor, error) {
if from == "" {
return txcmd.GetSignerAct(), nil
}
return commands.ParseAddress(from)
return commands.ParseActor(from)
}
+1 -1
View File
@@ -55,5 +55,5 @@ func readPayer() (basecoin.Actor, error) {
if payer == "" {
return txcmd.GetSignerAct(), nil
}
return commands.ParseAddress(payer)
return commands.ParseActor(payer)
}
+3 -3
View File
@@ -9,7 +9,7 @@ import (
lc "github.com/tendermint/light-client"
"github.com/tendermint/basecoin"
lcmd "github.com/tendermint/basecoin/client/commands"
"github.com/tendermint/basecoin/client/commands"
proofcmd "github.com/tendermint/basecoin/client/commands/proofs"
"github.com/tendermint/basecoin/modules/nonce"
"github.com/tendermint/basecoin/stack"
@@ -19,7 +19,7 @@ import (
var NonceQueryCmd = &cobra.Command{
Use: "nonce [address]",
Short: "Get details of a nonce sequence number, with proof",
RunE: lcmd.RequireInit(nonceQueryCmd),
RunE: commands.RequireInit(nonceQueryCmd),
}
func nonceQueryCmd(cmd *cobra.Command, args []string) error {
@@ -28,7 +28,7 @@ func nonceQueryCmd(cmd *cobra.Command, args []string) error {
}
addr := strings.Join(args, ",")
signers, err := parseActors(addr)
signers, err := commands.ParseActors(addr)
if err != nil {
return err
}
+1 -14
View File
@@ -2,7 +2,6 @@ package commands
import (
"fmt"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
@@ -54,19 +53,7 @@ func readNonceKey() ([]basecoin.Actor, error) {
if nonce == "" {
return []basecoin.Actor{txcmd.GetSignerAct()}, nil
}
return parseActors(nonce)
}
func parseActors(key string) (signers []basecoin.Actor, err error) {
var act basecoin.Actor
for _, k := range strings.Split(key, ",") {
act, err = commands.ParseAddress(k)
if err != nil {
return
}
signers = append(signers, act)
}
return
return commands.ParseActors(nonce)
}
// read the sequence from the flag or query for it if flag is -1
+40
View File
@@ -0,0 +1,40 @@
package commands
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
lcmd "github.com/tendermint/basecoin/client/commands"
proofcmd "github.com/tendermint/basecoin/client/commands/proofs"
"github.com/tendermint/basecoin/modules/roles"
"github.com/tendermint/basecoin/stack"
)
// RoleQueryCmd - command to query a role
var RoleQueryCmd = &cobra.Command{
Use: "role [name]",
Short: "Get details of a role, with proof",
RunE: lcmd.RequireInit(roleQueryCmd),
}
func roleQueryCmd(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument [name]")
} else if len(args) > 1 {
return errors.New("Command only supports one name")
}
role, err := parseRole(args[0])
if err != nil {
return err
}
var res roles.Role
key := stack.PrefixedKey(roles.NameRole, role)
proof, err := proofcmd.GetAndParseAppProof(key, &res)
if err != nil {
return err
}
return proofcmd.OutputProof(res, proof.BlockHeight())
}
+65
View File
@@ -0,0 +1,65 @@
package commands
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/client/commands"
txcmd "github.com/tendermint/basecoin/client/commands/txs"
"github.com/tendermint/basecoin/modules/roles"
)
// CreateRoleTxCmd is CLI command to send tokens between basecoin accounts
var CreateRoleTxCmd = &cobra.Command{
Use: "send",
Short: "send tokens from one account to another",
RunE: commands.RequireInit(createRoleTxCmd),
}
//nolint
const (
FlagRole = "role"
FlagMembers = "members"
FlagMinSigs = "min-sigs"
)
func init() {
flags := CreateRoleTxCmd.Flags()
flags.String(FlagRole, "", "Name of the role to create")
flags.String(FlagMembers, "", "Set of comma-separated addresses for this role")
flags.Int(FlagMinSigs, 0, "Minimum number of signatures needed to assume this role")
}
// createRoleTxCmd is an example of how to make a tx
func createRoleTxCmd(cmd *cobra.Command, args []string) error {
tx, err := readCreateRoleTxFlags()
if err != nil {
return err
}
return txcmd.DoTx(tx)
}
func readCreateRoleTxFlags() (tx basecoin.Tx, err error) {
role, err := parseRole(viper.GetString(FlagRole))
if err != nil {
return tx, err
}
sigs := viper.GetInt(FlagMinSigs)
if sigs < 1 {
return tx, errors.Errorf("--%s must be at least 1", FlagMinSigs)
}
signers, err := commands.ParseActors(viper.GetString(FlagMembers))
if err != nil {
return tx, err
}
if len(signers) == 0 {
return tx, errors.New("must specify at least one member")
}
tx = roles.NewCreateRoleTx(role, uint32(sigs), signers)
return tx, nil
}
+48
View File
@@ -0,0 +1,48 @@
package commands
import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tendermint/basecoin"
txcmd "github.com/tendermint/basecoin/client/commands/txs"
"github.com/tendermint/basecoin/modules/roles"
)
// nolint
const (
FlagAssumeRole = "assume-role"
)
// RoleWrapper wraps a tx with 0, 1, or more roles
type RoleWrapper struct{}
var _ txcmd.Wrapper = RoleWrapper{}
// Wrap grabs the sequence number from the flag and wraps
// the tx with this nonce. Grabs the permission from the signer,
// as we still only support single sig on the cli
func (RoleWrapper) Wrap(tx basecoin.Tx) (basecoin.Tx, error) {
assume := viper.GetStringSlice(FlagAssumeRole)
// we wrap from inside-out, so we must wrap them in the reverse order,
// so they are applied in the order the user intended
for i := len(assume) - 1; i >= 0; i-- {
r, err := parseRole(assume[i])
if err != nil {
return tx, err
}
tx = roles.NewAssumeRoleTx(r, tx)
}
return tx, nil
}
// Register adds the sequence flags to the cli
func (RoleWrapper) Register(fs *pflag.FlagSet) {
fs.StringSlice(FlagAssumeRole, nil, "Roles to assume (can use multiple times)")
}
// parse role turns the string->byte... todo: support hex?
func parseRole(role string) ([]byte, error) {
return []byte(role), nil
}