github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/cmd/devp2p/keycmd.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  
    23  	"github.com/ethxdao/go-ethereum/crypto"
    24  	"github.com/ethxdao/go-ethereum/p2p/enode"
    25  )
    26  
    27  var (
    28  	keyCommand = &cli.Command{
    29  		Name:  "key",
    30  		Usage: "Operations on node keys",
    31  		Subcommands: []*cli.Command{
    32  			keyGenerateCommand,
    33  			keyToNodeCommand,
    34  		},
    35  	}
    36  	keyGenerateCommand = &cli.Command{
    37  		Name:      "generate",
    38  		Usage:     "Generates node key files",
    39  		ArgsUsage: "keyfile",
    40  		Action:    genkey,
    41  	}
    42  	keyToNodeCommand = &cli.Command{
    43  		Name:      "to-enode",
    44  		Usage:     "Creates an enode URL from a node key file",
    45  		ArgsUsage: "keyfile",
    46  		Action:    keyToURL,
    47  		Flags:     []cli.Flag{hostFlag, tcpPortFlag, udpPortFlag},
    48  	}
    49  )
    50  
    51  var (
    52  	hostFlag = &cli.StringFlag{
    53  		Name:  "ip",
    54  		Usage: "IP address of the node",
    55  		Value: "127.0.0.1",
    56  	}
    57  	tcpPortFlag = &cli.IntFlag{
    58  		Name:  "tcp",
    59  		Usage: "TCP port of the node",
    60  		Value: 30303,
    61  	}
    62  	udpPortFlag = &cli.IntFlag{
    63  		Name:  "udp",
    64  		Usage: "UDP port of the node",
    65  		Value: 30303,
    66  	}
    67  )
    68  
    69  func genkey(ctx *cli.Context) error {
    70  	if ctx.NArg() != 1 {
    71  		return fmt.Errorf("need key file as argument")
    72  	}
    73  	file := ctx.Args().Get(0)
    74  
    75  	key, err := crypto.GenerateKey()
    76  	if err != nil {
    77  		return fmt.Errorf("could not generate key: %v", err)
    78  	}
    79  	return crypto.SaveECDSA(file, key)
    80  }
    81  
    82  func keyToURL(ctx *cli.Context) error {
    83  	if ctx.NArg() != 1 {
    84  		return fmt.Errorf("need key file as argument")
    85  	}
    86  
    87  	var (
    88  		file = ctx.Args().Get(0)
    89  		host = ctx.String(hostFlag.Name)
    90  		tcp  = ctx.Int(tcpPortFlag.Name)
    91  		udp  = ctx.Int(udpPortFlag.Name)
    92  	)
    93  	key, err := crypto.LoadECDSA(file)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	ip := net.ParseIP(host)
    98  	if ip == nil {
    99  		return fmt.Errorf("invalid IP address %q", host)
   100  	}
   101  	node := enode.NewV4(&key.PublicKey, ip, tcp, udp)
   102  	fmt.Println(node.URLv4())
   103  	return nil
   104  }