Skip to content

Commit

Permalink
Merge pull request #43 from QuorumControl/full-local
Browse files Browse the repository at this point in the history
Support random key generation in local runs of rpc
  • Loading branch information
brandonwestcott authored Oct 10, 2018
2 parents 45924b3 + b5efc45 commit dea1764
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 34 deletions.
43 changes: 33 additions & 10 deletions cmd/generateNodeKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,50 @@ import (
"github.com/spf13/cobra"
)

func generateKeySet(numberOfKeys int) (privateKeys []*PrivateKeySet, publicKeys []*PublicKeySet, err error) {
for i := 1; i <= numberOfKeys; i++ {
blsKey, err := bls.NewSignKey()
if err != nil {
return nil, nil, err
}
ecdsa, err := crypto.GenerateKey()
if err != nil {
return nil, nil, err
}

privateKeys = append(privateKeys, &PrivateKeySet{
BlsHexPrivateKey: hexutil.Encode(blsKey.Bytes()),
EcdsaHexPrivateKey: hexutil.Encode(crypto.FromECDSA(ecdsa)),
})

publicKeys = append(publicKeys, &PublicKeySet{
BlsHexPublicKey: hexutil.Encode(consensus.BlsKeyToPublicKey(blsKey.MustVerKey()).PublicKey),
EcdsaHexPublicKey: hexutil.Encode(consensus.EcdsaToPublicKey(&ecdsa.PublicKey).PublicKey),
})
}

return privateKeys, publicKeys, err
}

// 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) {
blsKey, err := bls.NewSignKey()
if err != nil {
panic("error generating bls")
}
ecdsa, err := crypto.GenerateKey()
privateKeys, publicKeys, err := generateKeySet(1)

if err != nil {
panic("error generating ecdsa")
panic(err)
}

fmt.Printf(
"bls: '%v'\nbls public: '%v'\necdsa: '%v'\necdsa public: '%v'\n",
hexutil.Encode(blsKey.Bytes()),
hexutil.Encode(consensus.BlsKeyToPublicKey(blsKey.MustVerKey()).PublicKey),
hexutil.Encode(crypto.FromECDSA(ecdsa)),
hexutil.Encode(consensus.EcdsaToPublicKey(&ecdsa.PublicKey).PublicKey))
privateKeys[0].BlsHexPrivateKey,
publicKeys[0].BlsHexPublicKey,
privateKeys[0].EcdsaHexPrivateKey,
publicKeys[0].EcdsaHexPublicKey,
)
},
}

Expand Down
22 changes: 22 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ import (
)

var cfgFile string
var bootstrapPublicKeysFile string
var bootstrapPublicKeys []*PublicKeySet

type PublicKeySet struct {
BlsHexPublicKey string `json:"blsHexPublicKey,omitempty"`
EcdsaHexPublicKey string `json:"ecdsaHexPublicKey,omitempty"`
}

type PrivateKeySet struct {
BlsHexPrivateKey string `json:"blsHexPrivateKey,omitempty"`
EcdsaHexPrivateKey string `json:"ecdsaHexPrivateKey,omitempty"`
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -35,6 +47,16 @@ 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.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if bootstrapPublicKeysFile != "" {
var err error
bootstrapPublicKeys, err = loadPublicKeyFile(bootstrapPublicKeysFile)
if err != nil {
panic("Error loading public keys file")
}
}
},

// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand Down
17 changes: 16 additions & 1 deletion cmd/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
tls bool
certFile string
keyFile string
localNetworkNodeCount int
)

func loadPrivateKeyFile(path string) ([]*PrivateKeySet, error) {
Expand Down Expand Up @@ -54,7 +55,20 @@ var rpcServerCmd = &cobra.Command{
Use: "rpc-server",
Short: "Launches a Tupelo RPC Server",
Run: func(cmd *cobra.Command, args []string) {
privateKeys, _ := loadPrivateKeyFile(bootstrapPrivateKeysFile)
var privateKeys []*PrivateKeySet

if localNetworkNodeCount > 0 {
var err error
var publicKeys []*PublicKeySet
privateKeys, publicKeys, err = generateKeySet(localNetworkNodeCount)
if err != nil {
panic("Can't generate node keys")
}
bootstrapPublicKeys = publicKeys
} else {
privateKeys, _ = loadPrivateKeyFile(bootstrapPrivateKeysFile)
}

signers := make([]*signer.GossipedSigner, len(privateKeys))
for i, keys := range privateKeys {
signers[i] = setupGossipNode(keys.EcdsaHexPrivateKey, keys.BlsHexPrivateKey)
Expand All @@ -74,6 +88,7 @@ func init() {
rootCmd.AddCommand(rpcServerCmd)
rpcServerCmd.Flags().StringVarP(&bootstrapPublicKeysFile, "bootstrap-keys", "k", "", "which public keys to bootstrap the notary groups with")
rpcServerCmd.Flags().StringVarP(&bootstrapPrivateKeysFile, "bootstrap-private-keys", "s", "", "which private keys to bootstrap the notary groups with")
rpcServerCmd.Flags().IntVarP(&localNetworkNodeCount, "local-network", "l", 0, "Run local network with randomly generated keys, specifying number of nodes as argument. Mutually exlusive with bootstrap-*")
rpcServerCmd.Flags().BoolVarP(&tls, "tls", "t", false, "Encrypt connections with TLS/SSL")
rpcServerCmd.Flags().StringVarP(&certFile, "tls-cert", "C", "", "TLS certificate file")
rpcServerCmd.Flags().StringVarP(&keyFile, "tls-key", "K", "", "TLS private key file")
Expand Down
27 changes: 5 additions & 22 deletions cmd/testnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,10 @@ import (
)

var (
BlsSignKeys []*bls.SignKey
EcdsaKeys []*ecdsa.PrivateKey
bootstrapPublicKeysFile string
BlsSignKeys []*bls.SignKey
EcdsaKeys []*ecdsa.PrivateKey
)

type PublicKeySet struct {
BlsHexPublicKey string `json:"blsHexPublicKey,omitempty"`
EcdsaHexPublicKey string `json:"ecdsaHexPublicKey,omitempty"`
}

type PrivateKeySet struct {
BlsHexPrivateKey string `json:"blsHexPrivateKey,omitempty"`
EcdsaHexPrivateKey string `json:"ecdsaHexPrivateKey,omitempty"`
}

func expandHomePath(path string) (string, error) {
currentUser, err := user.Current()
if err != nil {
Expand Down Expand Up @@ -100,14 +89,8 @@ func loadPublicKeyFile(path string) ([]*PublicKeySet, error) {
return jsonLoadedKeys, nil
}

func bootstrapMembers(path string) (members []*consensus.RemoteNode) {
jsonLoadedKeys, err := loadPublicKeyFile(path)
if err != nil {
fmt.Printf("Error loading key file: %v", err)
return nil
}

for _, keySet := range jsonLoadedKeys {
func bootstrapMembers(keys []*PublicKeySet) (members []*consensus.RemoteNode) {
for _, keySet := range keys {
blsPubKey := consensus.PublicKey{
PublicKey: hexutil.MustDecode(keySet.BlsHexPublicKey),
Type: consensus.KeyTypeBLSGroupSig,
Expand Down Expand Up @@ -143,7 +126,7 @@ func setupNotaryGroup(storageAdapter storage.Storage) *consensus.NotaryGroup {
nodeStore := nodestore.NewStorageBasedStore(storageAdapter)
group := consensus.NewNotaryGroup("hardcodedprivatekeysareunsafe", nodeStore)
if group.IsGenesis() {
testNetMembers := bootstrapMembers(bootstrapPublicKeysFile)
testNetMembers := bootstrapMembers(bootstrapPublicKeys)
log.Debug("Creating gensis state", "nodes", len(testNetMembers))
group.CreateGenesisState(group.RoundAt(time.Now()), testNetMembers...)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/workflowtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func smokeTestNetwork() (bool, string) {
nodeStore := nodestore.NewStorageBasedStore(storage.NewMemStorage())
group := consensus.NewNotaryGroup("hardcodedprivatekeysareunsafe", nodeStore)
if group.IsGenesis() {
testNetMembers := bootstrapMembers(bootstrapPublicKeysFile)
testNetMembers := bootstrapMembers(bootstrapPublicKeys)
group.CreateGenesisState(group.RoundAt(time.Now()), testNetMembers...)
}

Expand Down

0 comments on commit dea1764

Please sign in to comment.