cli: key command
This commit is contained in:
@@ -20,6 +20,7 @@ func main() {
|
||||
app.Commands = []cli.Command{
|
||||
commands.StartCmd,
|
||||
commands.TxCmd,
|
||||
commands.KeyCmd,
|
||||
commands.QueryCmd,
|
||||
commands.VerifyCmd, // TODO: move to merkleeyes?
|
||||
commands.BlockCmd,
|
||||
|
||||
@@ -16,6 +16,7 @@ func main() {
|
||||
commands.StartCmd,
|
||||
commands.TxCmd,
|
||||
commands.QueryCmd,
|
||||
commands.KeyCmd,
|
||||
commands.VerifyCmd, // TODO: move to merkleeyes?
|
||||
commands.BlockCmd, // TODO: move to adam?
|
||||
commands.AccountCmd,
|
||||
|
||||
@@ -56,7 +56,7 @@ var (
|
||||
|
||||
FromFlag = cli.StringFlag{
|
||||
Name: "from",
|
||||
Value: "priv_validator.json",
|
||||
Value: "key.json",
|
||||
Usage: "Path to a private key to sign the transaction",
|
||||
}
|
||||
|
||||
|
||||
+1
-3
@@ -20,9 +20,7 @@ import (
|
||||
// Register the IBC plugin at start and for transactions
|
||||
func RegisterIBC() {
|
||||
RegisterTxSubcommand(IbcCmd)
|
||||
RegisterStartPlugin("ibc", func() types.Plugin {
|
||||
return ibc.New()
|
||||
})
|
||||
RegisterStartPlugin("ibc", func() types.Plugin { return ibc.New() })
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
|
||||
cmn "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-crypto"
|
||||
"github.com/tendermint/go-wire"
|
||||
)
|
||||
|
||||
var (
|
||||
KeyCmd = cli.Command{
|
||||
Name: "key",
|
||||
Usage: "Manage keys",
|
||||
ArgsUsage: "",
|
||||
Subcommands: []cli.Command{NewKeyCmd},
|
||||
}
|
||||
|
||||
NewKeyCmd = cli.Command{
|
||||
Name: "new",
|
||||
Usage: "Create a new private key",
|
||||
ArgsUsage: "",
|
||||
Action: func(c *cli.Context) error {
|
||||
return cmdNewKey(c)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func cmdNewKey(c *cli.Context) error {
|
||||
key := genKey()
|
||||
keyJSON := wire.JSONBytesPretty(key)
|
||||
fmt.Println(string(keyJSON))
|
||||
return nil
|
||||
}
|
||||
|
||||
//---------------------------------------------
|
||||
// simple implementation of a key
|
||||
|
||||
type Key struct {
|
||||
Address []byte `json:"address"`
|
||||
PubKey crypto.PubKey `json:"pub_key"`
|
||||
PrivKey crypto.PrivKey `json:"priv_key"`
|
||||
}
|
||||
|
||||
// Implements Signer
|
||||
func (k *Key) Sign(msg []byte) crypto.Signature {
|
||||
return k.PrivKey.Sign(msg)
|
||||
}
|
||||
|
||||
// Generates a new validator with private key.
|
||||
func genKey() *Key {
|
||||
privKey := crypto.GenPrivKeyEd25519()
|
||||
return &Key{
|
||||
Address: privKey.PubKey().Address(),
|
||||
PubKey: privKey.PubKey(),
|
||||
PrivKey: privKey,
|
||||
}
|
||||
}
|
||||
|
||||
func LoadKey(filePath string) *Key {
|
||||
keyJSONBytes, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
cmn.Exit(err.Error())
|
||||
}
|
||||
key := wire.ReadJSON(&Key{}, keyJSONBytes, &err).(*Key)
|
||||
if err != nil {
|
||||
cmn.Exit(cmn.Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
|
||||
}
|
||||
return key
|
||||
}
|
||||
@@ -43,15 +43,15 @@ var StartCmd = cli.Command{
|
||||
}
|
||||
|
||||
type plugin struct {
|
||||
name string
|
||||
init func() types.Plugin
|
||||
name string
|
||||
newPlugin func() types.Plugin
|
||||
}
|
||||
|
||||
var plugins = []plugin{}
|
||||
|
||||
// RegisterStartPlugin is used to enable a plugin
|
||||
func RegisterStartPlugin(name string, initFunc func() types.Plugin) {
|
||||
plugins = append(plugins, plugin{name: name, init: initFunc})
|
||||
func RegisterStartPlugin(name string, newPlugin func() types.Plugin) {
|
||||
plugins = append(plugins, plugin{name: name, newPlugin: newPlugin})
|
||||
}
|
||||
|
||||
func cmdStart(c *cli.Context) error {
|
||||
@@ -73,7 +73,7 @@ func cmdStart(c *cli.Context) error {
|
||||
|
||||
// register all plugins
|
||||
for _, p := range plugins {
|
||||
basecoinApp.RegisterPlugin(p.init())
|
||||
basecoinApp.RegisterPlugin(p.newPlugin())
|
||||
}
|
||||
|
||||
// If genesis file exists, set key-value options
|
||||
|
||||
+9
-10
@@ -82,18 +82,17 @@ func cmdSendTx(c *cli.Context) error {
|
||||
return errors.New("To address is invalid hex: " + err.Error())
|
||||
}
|
||||
|
||||
// load the priv validator
|
||||
// XXX: this is overkill for now, we need a keys solution
|
||||
privVal := tmtypes.LoadPrivValidator(fromFile)
|
||||
// load the priv key
|
||||
privKey := LoadKey(fromFile)
|
||||
|
||||
// get the sequence number for the tx
|
||||
sequence, err := getSeq(c, privVal.Address)
|
||||
sequence, err := getSeq(c, privKey.Address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// craft the tx
|
||||
input := types.NewTxInput(privVal.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
|
||||
input := types.NewTxInput(privKey.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
|
||||
output := newOutput(to, coin, amount)
|
||||
tx := &types.SendTx{
|
||||
Gas: int64(gas),
|
||||
@@ -104,7 +103,7 @@ func cmdSendTx(c *cli.Context) error {
|
||||
|
||||
// sign that puppy
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
tx.Inputs[0].Signature = privVal.Sign(signBytes)
|
||||
tx.Inputs[0].Signature = privKey.Sign(signBytes)
|
||||
|
||||
fmt.Println("Signed SendTx:")
|
||||
fmt.Println(string(wire.JSONBytes(tx)))
|
||||
@@ -134,14 +133,14 @@ func AppTx(c *cli.Context, name string, data []byte) error {
|
||||
gas, fee := c.Int("gas"), int64(c.Int("fee"))
|
||||
chainID := c.String("chain_id")
|
||||
|
||||
privVal := tmtypes.LoadPrivValidator(fromFile)
|
||||
privKey := tmtypes.LoadPrivValidator(fromFile)
|
||||
|
||||
sequence, err := getSeq(c, privVal.Address)
|
||||
sequence, err := getSeq(c, privKey.Address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input := types.NewTxInput(privVal.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
|
||||
input := types.NewTxInput(privKey.PubKey, types.Coins{types.Coin{coin, amount}}, sequence)
|
||||
tx := &types.AppTx{
|
||||
Gas: int64(gas),
|
||||
Fee: types.Coin{coin, fee},
|
||||
@@ -150,7 +149,7 @@ func AppTx(c *cli.Context, name string, data []byte) error {
|
||||
Data: data,
|
||||
}
|
||||
|
||||
tx.Input.Signature = privVal.Sign(tx.SignBytes(chainID))
|
||||
tx.Input.Signature = privKey.Sign(tx.SignBytes(chainID))
|
||||
|
||||
fmt.Println("Signed AppTx:")
|
||||
fmt.Println(string(wire.JSONBytes(tx)))
|
||||
|
||||
+1
-3
@@ -13,9 +13,7 @@ import (
|
||||
|
||||
func init() {
|
||||
commands.RegisterTxSubcommand(CounterTxCmd)
|
||||
commands.RegisterStartPlugin("counter", func() types.Plugin {
|
||||
return counter.New("counter")
|
||||
})
|
||||
commands.RegisterStartPlugin("counter", func() types.Plugin { return counter.New() })
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -15,6 +15,7 @@ func main() {
|
||||
app.Commands = []cli.Command{
|
||||
commands.StartCmd,
|
||||
commands.TxCmd,
|
||||
commands.KeyCmd,
|
||||
commands.QueryCmd,
|
||||
commands.AccountCmd,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user