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