-
Notifications
You must be signed in to change notification settings - Fork 90
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
cmd: add gen-p2pkey command #316
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,20 +46,14 @@ func newEnrCmd(runFunc func(io.Writer, p2p.Config, string) error) *cobra.Command | |
return cmd | ||
} | ||
|
||
// Function for printing status of ENR for this instance. | ||
// runNewENR loads the p2pkey from disk and prints the ENR for the provided config. | ||
func runNewENR(w io.Writer, config p2p.Config, dataDir string) error { | ||
identityKey, loaded, err := p2p.LoadOrCreatePrivKey(dataDir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #893 was fixed here |
||
key, err := p2p.LoadPrivKey(dataDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if loaded { | ||
_, _ = fmt.Fprintf(w, "Loaded p2p key from folder %s", dataDir) | ||
} else { | ||
_, _ = fmt.Fprintf(w, "Generated new p2p key to folder %s", dataDir) | ||
} | ||
|
||
localEnode, db, err := p2p.NewLocalEnode(config, identityKey) | ||
localEnode, db, err := p2p.NewLocalEnode(config, key) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to open peer DB") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright © 2021 Obol Technologies Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/obolnetwork/charon/app/errors" | ||
"github.com/obolnetwork/charon/p2p" | ||
) | ||
|
||
func newGenP2PKeyCmd(runFunc func(io.Writer, p2p.Config, string) error) *cobra.Command { | ||
var ( | ||
config p2p.Config | ||
dataDir string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "gen-p2pkey", | ||
Short: "Generates a new p2p key", | ||
Long: `Generates a new p2p authentication key (ecdsa-k1) and saves it to the data directory`, | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runFunc(cmd.OutOrStdout(), config, dataDir) | ||
}, | ||
} | ||
|
||
bindGeneralFlags(cmd.Flags(), &dataDir) | ||
bindP2PFlags(cmd.Flags(), &config) | ||
|
||
return cmd | ||
} | ||
|
||
// runGenP2PKey stores a new p2pkey to disk and prints the ENR for the provided config. | ||
func runGenP2PKey(w io.Writer, config p2p.Config, dataDir string) error { | ||
key, err := p2p.NewSavedPrivKey(dataDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
localEnode, db, err := p2p.NewLocalEnode(config, key) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to open peer DB") | ||
} | ||
defer db.Close() | ||
|
||
_, _ = fmt.Fprintf(w, "Created key: %s/p2pkey\n", dataDir) | ||
_, _ = fmt.Fprintln(w, localEnode.Node().String()) | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright © 2021 Obol Technologies Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/obolnetwork/charon/p2p" | ||
) | ||
|
||
func TestRunGenP2P(t *testing.T) { | ||
temp, err := os.MkdirTemp("", "") | ||
require.NoError(t, err) | ||
|
||
err = runGenP2PKey(io.Discard, p2p.Config{}, temp) | ||
require.NoError(t, err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,11 +27,8 @@ import ( | |
//go:generate go test . -run=TestGenSimnet -update | ||
|
||
func TestGenSimnet(t *testing.T) { | ||
dir := "testdata/simnet" | ||
require.NoError(t, os.RemoveAll(dir)) | ||
err := os.MkdirAll(dir, 0o755) | ||
dir, err := os.MkdirTemp("", "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixing this test that was flapping |
||
require.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
|
||
var buf bytes.Buffer | ||
conf := simnetConfig{ | ||
|
@@ -45,7 +42,9 @@ func TestGenSimnet(t *testing.T) { | |
err = runGenSimnet(&buf, conf) | ||
require.NoError(t, err) | ||
|
||
testutil.RequireGoldenBytes(t, buf.Bytes()) | ||
out := buf.Bytes() | ||
out = bytes.Replace(out, []byte(dir), []byte("charon-simnet"), 1) | ||
testutil.RequireGoldenBytes(t, out) | ||
|
||
// TODO(corver): Assert generated files. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,32 +18,29 @@ import ( | |
"crypto/ecdsa" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/ethereum/go-ethereum/crypto" | ||
|
||
"github.com/obolnetwork/charon/app/errors" | ||
) | ||
|
||
// LoadOrCreatePrivKey returns a k1 (secp256k1) private key and true from the provided folder. | ||
// If it doesn't exist, a new key is generated and stored and returned with false. | ||
func LoadOrCreatePrivKey(dataDir string) (*ecdsa.PrivateKey, bool, error) { | ||
keyPath := path.Join(dataDir, "p2pkey") | ||
|
||
key, err := crypto.LoadECDSA(keyPath) | ||
if errors.Is(err, os.ErrNotExist) { | ||
key, err = newSavedPrivKey(keyPath) | ||
return key, false, err | ||
} else if err != nil { | ||
return nil, false, errors.Wrap(err, "load key") | ||
func p2pKeyPath(datadir string) string { | ||
return path.Join(datadir, "p2pkey") | ||
} | ||
|
||
// LoadPrivKey returns the ecdsa k1 key saved in the directory. | ||
func LoadPrivKey(dataDir string) (*ecdsa.PrivateKey, error) { | ||
key, err := crypto.LoadECDSA(p2pKeyPath(dataDir)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "load key") | ||
} | ||
|
||
return key, true, nil | ||
return key, nil | ||
} | ||
|
||
// newSavedPrivKey generates a new key and saves the new node identity. | ||
func newSavedPrivKey(keyPath string) (*ecdsa.PrivateKey, error) { | ||
if err := os.MkdirAll(filepath.Dir(keyPath), 0o755); err != nil { | ||
// NewSavedPrivKey generates a new ecdsa k1 key and saves it to the directory. | ||
func NewSavedPrivKey(datadir string) (*ecdsa.PrivateKey, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe name as NewP2PKey or NewPrivKey ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if err := os.MkdirAll(datadir, 0o755); err != nil { | ||
return nil, errors.Wrap(err, "mkdir") | ||
} | ||
|
||
|
@@ -52,7 +49,7 @@ func newSavedPrivKey(keyPath string) (*ecdsa.PrivateKey, error) { | |
return nil, errors.Wrap(err, "gen key") | ||
} | ||
|
||
err = crypto.SaveECDSA(keyPath, key) | ||
err = crypto.SaveECDSA(p2pKeyPath(datadir), key) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "save key") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not related, but I saw that cli-reference created for the release is incorrect.