Skip to content

Commit

Permalink
Merge pull request #99 from quorumcontrol/feature/dev-ux
Browse files Browse the repository at this point in the history
Feature/dev ux
  • Loading branch information
zonotope authored Jan 4, 2019
2 parents d4a2435 + c8fc1d9 commit c030d12
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 204 deletions.
Empty file removed .storage/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cmd/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ var benchmark = &cobra.Command{

func init() {
rootCmd.AddCommand(benchmark)
benchmark.Flags().StringVarP(&bootstrapPublicKeysFile, "bootstrap-keys", "k", "", "which keys to bootstrap the notary groups with")
benchmark.Flags().IntVarP(&benchmarkConcurrency, "concurrency", "c", 1, "how many transactions to execute at once")
benchmark.Flags().IntVarP(&benchmarkIterations, "iterations", "i", 10, "how many transactions to execute total")
benchmark.Flags().IntVarP(&benchmarkTimeout, "timeout", "t", 0, "seconds to wait before timing out")
Expand Down
91 changes: 49 additions & 42 deletions cmd/generateNodeKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -20,7 +18,7 @@ var (
generateNodeKeysPath string
)

func generateKeySet(numberOfKeys int) (privateKeys []*PrivateKeySet, publicKeys []*PublicKeySet, err error) {
func generateKeySets(numberOfKeys int) (privateKeys []*PrivateKeySet, publicKeys []*PublicKeySet, err error) {
for i := 1; i <= numberOfKeys; i++ {
blsKey, err := bls.NewSignKey()
if err != nil {
Expand Down Expand Up @@ -50,48 +48,67 @@ func generateKeySet(numberOfKeys int) (privateKeys []*PrivateKeySet, publicKeys
return privateKeys, publicKeys, err
}

func printTextKeys(privateKeys []*PrivateKeySet, publicKeys []*PublicKeySet) {
for i := 1; i <= len(privateKeys); i++ {
fmt.Printf("================ Key %v ================\n", i)
fmt.Printf(
"bls: '%v'\nbls public: '%v'\necdsa: '%v'\necdsa public: '%v'\npeer id: '%v'",
privateKeys[0].BlsHexPrivateKey,
publicKeys[0].BlsHexPublicKey,
privateKeys[0].EcdsaHexPrivateKey,
publicKeys[0].EcdsaHexPublicKey,
publicKeys[0].PeerIDBase58Key,
)
}
}

const (
publicKeyFile = "public-keys.json"
privateKeyFile = "private-keys.json"
)

func writeJSONKeys(privateKeys []*PrivateKeySet, publicKeys []*PublicKeySet, path string) error {
publicKeyJson, err := json.Marshal(publicKeys)
if err != nil {
return fmt.Errorf("Error marshaling public keys: %v", err)
}

err = writeFile(path, publicKeyFile, publicKeyJson)
if err != nil {
return fmt.Errorf("error writing public keys: %v", err)
}

privateKeyJson, err := json.Marshal(privateKeys)
if err != nil {
return fmt.Errorf("Error marshaling private keys: %v", err)
}

err = writeFile(path, privateKeyFile, privateKeyJson)
if err != nil {
return fmt.Errorf("error writing private keys: %v", err)
}

return nil
}

// generateNodeKeysCmd represents the generateNodeKeys command
var generateNodeKeysCmd = &cobra.Command{
Use: "generate-node-keys",
Short: "Generate a new set of node keys",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
privateKeys, publicKeys, err := generateKeySet(generateNodeKeysCount)

privateKeys, publicKeys, err := generateKeySets(generateNodeKeysCount)
if err != nil {
panic(err)
panic(fmt.Sprintf("error generating key sets: %v", err))
}

switch generateNodeKeysOutput {
case "text":
for i := 1; i <= len(privateKeys); i++ {
fmt.Printf("================ Key %v ================\n", i)
fmt.Printf(
"bls: '%v'\nbls public: '%v'\necdsa: '%v'\necdsa public: '%v'\npeer id: '%v'",
privateKeys[0].BlsHexPrivateKey,
publicKeys[0].BlsHexPublicKey,
privateKeys[0].EcdsaHexPrivateKey,
publicKeys[0].EcdsaHexPublicKey,
publicKeys[0].PeerIDBase58Key,
)
}
printTextKeys(privateKeys, publicKeys)
case "json-file":
publicKeyJson, err := json.Marshal(publicKeys)
err := writeJSONKeys(privateKeys, publicKeys, generateNodeKeysPath)
if err != nil {
panic(fmt.Sprintf("error writing json %v", err))
}
err = ioutil.WriteFile(filepath.Join(generateNodeKeysPath, "public-keys.json"), publicKeyJson, 0644)
if err != nil {
panic(fmt.Sprintf("error writing file %v", err))
}

privateKeyJson, err := json.Marshal(privateKeys)
if err != nil {
panic(fmt.Sprintf("error writing json %v", err))
}
err = ioutil.WriteFile(filepath.Join(generateNodeKeysPath, "private-keys.json"), privateKeyJson, 0644)
if err != nil {
panic(fmt.Sprintf("error writing file %v", err))
panic(fmt.Sprintf("error writing json file: %v", err))
}
default:
panic(fmt.Sprintf("output=%v type is not supported", generateNodeKeysOutput))
Expand All @@ -104,14 +121,4 @@ func init() {
generateNodeKeysCmd.Flags().IntVarP(&generateNodeKeysCount, "count", "c", 1, "how many keys to generate")
generateNodeKeysCmd.Flags().StringVarP(&generateNodeKeysOutput, "output", "o", "text", "format for keys output (default text): text, json-file")
generateNodeKeysCmd.Flags().StringVarP(&generateNodeKeysPath, "path", "p", ".", "directory to store files if using json")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// generateNodeKeysCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// generateNodeKeysCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
134 changes: 106 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,36 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/ethereum/go-ethereum/log"
ipfslogging "github.com/ipsn/go-ipfs/gxlibs/github.com/ipfs/go-log"
homedir "github.com/mitchellh/go-homedir"
"github.com/shibukawa/configdir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
bootstrapKeyFile = "bootstrap-keys.json"
)

var (
bootstrapPublicKeys []*PublicKeySet
cfgFile string
logLvlName string
newKeysFile string
overrideKeysFile string

localConfig = configDir("local-network")
remoteConfig = configDir("remote-network")
)

var logLevels = map[string]log.Lvl{
"critical": log.LvlCrit,
"error": log.LvlError,
Expand All @@ -44,10 +63,64 @@ func getLogLevel(lvlName string) (log.Lvl, error) {
return lvl, nil
}

var logLvlName string
var cfgFile string
var bootstrapPublicKeysFile string
var bootstrapPublicKeys []*PublicKeySet
func configDir(namespace string) string {
conf := configdir.New("tupelo", namespace)
folders := conf.QueryFolders(configdir.Global)

return folders[0].Path
}

func readConfig(path string, filename string) ([]byte, error) {
_, err := os.Stat(filepath.Join(path, filename))
if os.IsNotExist(err) {
return nil, nil
}

return ioutil.ReadFile(filepath.Join(path, filename))
}

func writeFile(parentDir string, filename string, data []byte) error {
err := os.MkdirAll(parentDir, 0755)
if err != nil {
return fmt.Errorf("error creating directory: %v", err)
}

return ioutil.WriteFile(filepath.Join(parentDir, filename), data, 0644)
}

func loadBootstrapKeyFile(path string) ([]*PublicKeySet, error) {
var keySet []*PublicKeySet

file, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading path %v: %v", path, err)
}

err = unmarshalKeys(keySet, file)

return keySet, err
}

func saveBootstrapKeys(keys []*PublicKeySet) error {
bootstrapKeyJson, err := json.Marshal(keys)
if err != nil {
return fmt.Errorf("Error marshaling bootstrap keys: %v", err)
}

err = writeFile(remoteConfig, bootstrapKeyFile, bootstrapKeyJson)
if err != nil {
return fmt.Errorf("error writing bootstrap keys: %v", err)
}

return nil
}

func readBootstrapKeys() ([]*PublicKeySet, error) {
var keySet []*PublicKeySet
err := loadKeyFile(keySet, remoteConfig, bootstrapKeyFile)

return keySet, err
}

type PublicKeySet struct {
BlsHexPublicKey string `json:"blsHexPublicKey,omitempty"`
Expand All @@ -63,19 +136,22 @@ type PrivateKeySet struct {
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "tupelo",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Tupelo interface",
Long: `Tupelo is a distributed ledger optimized for ownership`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if bootstrapPublicKeysFile != "" {
var err error
bootstrapPublicKeys, err = loadPublicKeyFile(bootstrapPublicKeysFile)
if overrideKeysFile != "" {
publicKeys, err := loadPublicKeyFile(overrideKeysFile)
if err != nil {
panic("Error loading public keys file")
panic(fmt.Sprintf("Error loading public keys: %v", err))
}

bootstrapPublicKeys = publicKeys
} else {
publicKeys, err := readBootstrapKeys()
if err != nil {
fmt.Printf("error loading stored bootstrap keys: %v", err)
} else {
bootstrapPublicKeys = publicKeys
}
}

Expand All @@ -86,14 +162,23 @@ to quickly create a Cobra application.`,
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(logLevel), log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
err = ipfslogging.SetLogLevel("*", strings.ToUpper(logLvlName))
if err != nil {
fmt.Println("unkown ipfs log level")
fmt.Println("unknown ipfs log level")
}

},
Run: func(cmd *cobra.Command, args []string) {
if newKeysFile != "" {
keys, err := loadBootstrapKeyFile(newKeysFile)
if err != nil {
panic(fmt.Sprintf("Error loading bootstrap keys: %v", err))
}

// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
err = saveBootstrapKeys(keys)
if err != nil {
panic(fmt.Sprintf("Error saving bootstrap keys: %v", err))
}
}
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -108,16 +193,9 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.tupelo.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

rootCmd.PersistentFlags().StringVarP(&logLvlName, "log-level", "L", "error", "Log level")
rootCmd.PersistentFlags().StringVarP(&overrideKeysFile, "override-keys", "k", "", "path to notary group bootstrap keys file")
rootCmd.Flags().StringVarP(&newKeysFile, "import-boot-keys", "i", "", "Path of a notary group key file to import")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
Loading

0 comments on commit c030d12

Please sign in to comment.