Include Prysm Tool to Generate Unencrypted Keys (#3324)

* next compatible, tests pass

* terence feedback

* skip comment

* fixes

* misc fix

* on block

* parse from unencrypted keys json

* mod val client

* launching unencrypted workssss

* fix broken build

* fix up build

* rem prints

* unencrypted keys file generator

* generate json

* unencrypted keys gen files

* tool done

* function abstractions

* removed docker img stuff

* lint
This commit is contained in:
Raul Jordan 2019-08-28 11:07:31 -05:00 committed by GitHub
parent cbb66dab50
commit 14f77449ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 156 additions and 1 deletions

View File

@ -0,0 +1,23 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "github.com/prysmaticlabs/prysm/tools/unencrypted-keys-gen",
visibility = ["//visibility:private"],
deps = [
"//shared/bls:go_default_library",
],
)
go_binary(
name = "unencrypted-keys-gen",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["main_test.go"],
embed = [":go_default_library"],
)

View File

@ -0,0 +1,19 @@
# Unencrypted Keys Generator
This tool is used to generate JSON file of unencrypted, base64 encoded, validator
signing and withdrawal keys. These keys can be fed into the Prysm validator
client for fast development startup times instead of using the Prysm keystore.
Usage:
```
bazel run //tools/unencrypted-keys-gen -- --num-keys 64 --output-file /path/to/output.json
```
Which will create 64 BLS private keys each for validator signing and withdrawals.
These will then be output to an `output.json` file. Both arguments are required.
The file can then be used to start the Prysm validator with the command:
```
bazel run //validator -- --unencrypted-keys /path/to/output.json
```

View File

@ -0,0 +1,88 @@
package main
import (
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/prysmaticlabs/prysm/shared/bls"
)
var (
numKeys = flag.Int("num-keys", 0, "Number of validator private/withdrawal keys to generate")
outputJSON = flag.String("output-json", "", "JSON file to write output to")
)
type unencryptedKeysContainer struct {
Keys []*unencryptedKeys `json:"keys"`
}
type unencryptedKeys struct {
ValidatorKey []byte `json:"validator_key"`
WithdrawalKey []byte `json:"withdrawal_key"`
}
func main() {
flag.Parse()
if *numKeys == 0 {
log.Fatal("Please specify --num-keys to generate")
}
if *outputJSON == "" {
log.Fatal("Please specify an --output-json file to write the unencrypted keys to")
}
file, err := os.Create(*outputJSON)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := file.Close(); err != nil {
log.Fatal(err)
}
}()
ctnr := generateUnencryptedKeys(rand.Reader)
if err := saveUnencryptedKeysToFile(file, ctnr); err != nil {
log.Fatal(err)
}
}
func generateUnencryptedKeys(r io.Reader) *unencryptedKeysContainer {
ctnr := &unencryptedKeysContainer{
Keys: make([]*unencryptedKeys, *numKeys),
}
for i := 0; i < *numKeys; i++ {
signingKey, err := bls.RandKey(r)
if err != nil {
log.Fatal(err)
}
withdrawalKey, err := bls.RandKey(r)
if err != nil {
log.Fatal(err)
}
ctnr.Keys[i] = &unencryptedKeys{
ValidatorKey: signingKey.Marshal(),
WithdrawalKey: withdrawalKey.Marshal(),
}
}
return ctnr
}
func saveUnencryptedKeysToFile(w io.Writer, ctnr *unencryptedKeysContainer) error {
enc, err := json.Marshal(ctnr)
if err != nil {
log.Fatal(err)
}
n, err := w.Write(enc)
if err != nil {
return err
}
if n != len(enc) {
return fmt.Errorf("failed to write %d bytes to file, wrote %d", len(enc), n)
}
return nil
}

View File

@ -0,0 +1,25 @@
package main
import (
"bytes"
"crypto/rand"
"encoding/json"
"reflect"
"testing"
)
func TestSavesUnencryptedKeys(t *testing.T) {
ctnr := generateUnencryptedKeys(rand.Reader)
buf := new(bytes.Buffer)
if err := saveUnencryptedKeysToFile(buf, ctnr); err != nil {
t.Fatal(err)
}
enc := buf.Bytes()
dec := &unencryptedKeysContainer{}
if err := json.Unmarshal(enc, dec); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(ctnr, dec) {
t.Errorf("Wanted %v, received %v", ctnr, dec)
}
}

View File

@ -17,7 +17,7 @@ import (
var log = logrus.WithField("prefix", "accounts")
// DecryptKeysFromKeystore extracts a set of validator private keys from
// and encrypted keystore directory and a password string.
// an encrypted keystore directory and a password string.
func DecryptKeysFromKeystore(directory string, password string) (map[string]*keystore.Key, error) {
validatorPrefix := params.BeaconConfig().ValidatorPrivkeyFileName
ks := keystore.NewKeystore(directory)