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

cmd/devp2p: add more nodekey commands #26129

Merged
merged 2 commits into from
Nov 8, 2022
Merged
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
73 changes: 65 additions & 8 deletions cmd/devp2p/keycmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/urfave/cli/v2"
)

Expand All @@ -31,7 +32,9 @@ var (
Usage: "Operations on node keys",
Subcommands: []*cli.Command{
keyGenerateCommand,
keyToIDCommand,
keyToNodeCommand,
keyToRecordCommand,
},
}
keyGenerateCommand = &cli.Command{
Expand All @@ -40,13 +43,27 @@ var (
ArgsUsage: "keyfile",
Action: genkey,
}
keyToIDCommand = &cli.Command{
Name: "to-id",
Usage: "Creates a node ID from a node key file",
ArgsUsage: "keyfile",
Action: keyToID,
Flags: []cli.Flag{},
}
keyToNodeCommand = &cli.Command{
Name: "to-enode",
Usage: "Creates an enode URL from a node key file",
ArgsUsage: "keyfile",
Action: keyToURL,
Flags: []cli.Flag{hostFlag, tcpPortFlag, udpPortFlag},
}
keyToRecordCommand = &cli.Command{
Name: "to-enr",
Usage: "Creates an ENR from a node key file",
ArgsUsage: "keyfile",
Action: keyToRecord,
Flags: []cli.Flag{hostFlag, tcpPortFlag, udpPortFlag},
}
)

var (
Expand Down Expand Up @@ -80,9 +97,36 @@ func genkey(ctx *cli.Context) error {
return crypto.SaveECDSA(file, key)
}

func keyToID(ctx *cli.Context) error {
n, err := makeRecord(ctx)
if err != nil {
return err
}
fmt.Println(n.ID())
return nil
}

func keyToURL(ctx *cli.Context) error {
n, err := makeRecord(ctx)
if err != nil {
return err
}
fmt.Println(n.URLv4())
return nil
}

func keyToRecord(ctx *cli.Context) error {
n, err := makeRecord(ctx)
if err != nil {
return err
}
fmt.Println(n.String())
return nil
}

func makeRecord(ctx *cli.Context) (*enode.Node, error) {
if ctx.NArg() != 1 {
return fmt.Errorf("need key file as argument")
return nil, fmt.Errorf("need key file as argument")
}

var (
Expand All @@ -93,13 +137,26 @@ func keyToURL(ctx *cli.Context) error {
)
key, err := crypto.LoadECDSA(file)
if err != nil {
return err
return nil, err
}

var r enr.Record
if host != "" {
ip := net.ParseIP(host)
if ip == nil {
return nil, fmt.Errorf("invalid IP address %q", host)
}
r.Set(enr.IP(ip))
}
ip := net.ParseIP(host)
if ip == nil {
return fmt.Errorf("invalid IP address %q", host)
if udp != 0 {
r.Set(enr.UDP(udp))
}
node := enode.NewV4(&key.PublicKey, ip, tcp, udp)
fmt.Println(node.URLv4())
return nil
if tcp != 0 {
r.Set(enr.TCP(tcp))
}

if err := enode.SignV4(&r, key); err != nil {
return nil, err
}
return enode.New(enode.ValidSchemes, &r)
}