github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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  	"gopkg.in/urfave/cli.v1"
    24  
    25  	"github.com/scroll-tech/go-ethereum/crypto"
    26  	"github.com/scroll-tech/go-ethereum/p2p/enode"
    27  )
    28  
    29  var (
    30  	keyCommand = cli.Command{
    31  		Name:  "key",
    32  		Usage: "Operations on node keys",
    33  		Subcommands: []cli.Command{
    34  			keyGenerateCommand,
    35  			keyToNodeCommand,
    36  		},
    37  	}
    38  	keyGenerateCommand = cli.Command{
    39  		Name:      "generate",
    40  		Usage:     "Generates node key files",
    41  		ArgsUsage: "keyfile",
    42  		Action:    genkey,
    43  	}
    44  	keyToNodeCommand = cli.Command{
    45  		Name:      "to-enode",
    46  		Usage:     "Creates an enode URL from a node key file",
    47  		ArgsUsage: "keyfile",
    48  		Action:    keyToURL,
    49  		Flags:     []cli.Flag{hostFlag, tcpPortFlag, udpPortFlag},
    50  	}
    51  )
    52  
    53  var (
    54  	hostFlag = cli.StringFlag{
    55  		Name:  "ip",
    56  		Usage: "IP address of the node",
    57  		Value: "127.0.0.1",
    58  	}
    59  	tcpPortFlag = cli.IntFlag{
    60  		Name:  "tcp",
    61  		Usage: "TCP port of the node",
    62  		Value: 30303,
    63  	}
    64  	udpPortFlag = cli.IntFlag{
    65  		Name:  "udp",
    66  		Usage: "UDP port of the node",
    67  		Value: 30303,
    68  	}
    69  )
    70  
    71  func genkey(ctx *cli.Context) error {
    72  	if ctx.NArg() != 1 {
    73  		return fmt.Errorf("need key file as argument")
    74  	}
    75  	file := ctx.Args().Get(0)
    76  
    77  	key, err := crypto.GenerateKey()
    78  	if err != nil {
    79  		return fmt.Errorf("could not generate key: %v", err)
    80  	}
    81  	return crypto.SaveECDSA(file, key)
    82  }
    83  
    84  func keyToURL(ctx *cli.Context) error {
    85  	if ctx.NArg() != 1 {
    86  		return fmt.Errorf("need key file as argument")
    87  	}
    88  
    89  	var (
    90  		file = ctx.Args().Get(0)
    91  		host = ctx.String(hostFlag.Name)
    92  		tcp  = ctx.Int(tcpPortFlag.Name)
    93  		udp  = ctx.Int(udpPortFlag.Name)
    94  	)
    95  	key, err := crypto.LoadECDSA(file)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	ip := net.ParseIP(host)
   100  	if ip == nil {
   101  		return fmt.Errorf("invalid IP address %q", host)
   102  	}
   103  	node := enode.NewV4(&key.PublicKey, ip, tcp, udp)
   104  	fmt.Println(node.URLv4())
   105  	return nil
   106  }