Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating the ablity to generate and append new peer key using the ge… #3

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/emucon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ USAGE:
OPTIONS:
--count value number of participant in quorum (default: 4)
--config value output quorum file (default: "./quorum.json")
--append value append key to quorum file (default: 0)
--help, -h show help (default: false)

```
Expand Down
41 changes: 40 additions & 1 deletion cmd/emucon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"os"
"time"

"github.com/urfave/cli/v2"
"github.com/yonggewang/bdls"
"github.com/yonggewang/bdls/agent-tcp"
"github.com/yonggewang/bdls/crypto/blake2b"
"github.com/urfave/cli/v2"
)

// A quorum set for consenus
Expand Down Expand Up @@ -46,6 +46,11 @@ func main() {
Value: "./quorum.json",
Usage: "output quorum file",
},
&cli.IntFlag{
Name: "append",
Value: 0,
Usage: "append key to quorum file",
},
},
Action: func(c *cli.Context) error {
count := c.Int("count")
Expand All @@ -60,6 +65,40 @@ func main() {
quorum.Keys = append(quorum.Keys, privateKey.D)
}

newKey := c.Int("append")
if newKey != 0 {
quorum := &Quorum{}
fileBytes, _ := os.ReadFile(c.String("config"))
err := json.Unmarshal(fileBytes, quorum)

if err != nil {
fmt.Println("JSON decode error!")
return err
}
// generate private keys
for i := 0; i < newKey; i++ {
privateKey, err := ecdsa.GenerateKey(bdls.S256Curve, rand.Reader)
if err != nil {
return err
}

quorum.Keys = append(quorum.Keys, privateKey.D)
}
existFile, err := os.Create(c.String("config"))
if err != nil {
return err
}
enc := json.NewEncoder(existFile)
enc.SetIndent("", "\t")
err = enc.Encode(quorum)
if err != nil {
return err
}
existFile.Close()
log.Println("generate", c.Int("append"), "keys")
return nil
}

file, err := os.Create(c.String("config"))
if err != nil {
return err
Expand Down