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
+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
}