github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/cmd/nodekeycheck/main.go (about) 1 // Copyright 2015 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 // bootnode runs a bootstrap node for the Ethereum Discovery Protocol. 18 package main 19 20 import ( 21 "crypto/ecdsa" 22 "flag" 23 "fmt" 24 "github.com/ethereum/go-ethereum/cmd/utils" 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/p2p/discover" 29 "net" 30 "os" 31 "strings" 32 ) 33 // setNodeKey creates a node key from set command line flags, either loading it 34 // from a file or as a specified hex value. If neither flags were provided, this 35 // method returns nil and an emphemeral key is to be generated. 36 /* func setNodeKey(ctx *cli.Context, cfg *p2p.Config) { 37 var ( 38 hex = ctx.GlobalString(NodeKeyHexFlag.Name) 39 file = ctx.GlobalString(NodeKeyFileFlag.Name) 40 key *ecdsa.PrivateKey 41 err error 42 ) 43 switch { 44 case file != "" && hex != "": 45 Fatalf("Options %q and %q are mutually exclusive", NodeKeyFileFlag.Name, NodeKeyHexFlag.Name) 46 case file != "": 47 if key, err = crypto.LoadECDSA(file); err != nil { //산출되는 key 가 20바이트 Address 인가? 48 Fatalf("Option %q: %v", NodeKeyFileFlag.Name, err) // 49 } 50 cfg.PrivateKey = key 51 case hex != "": 52 if key, err = crypto.HexToECDSA(hex); err != nil { 53 Fatalf("Option %q: %v", NodeKeyHexFlag.Name, err) 54 } 55 cfg.PrivateKey = key 56 } 57 } */ 58 59 /** 60 * SimpleBackend 61 * Private key: bb047e5940b6d83354d9432db7c449ac8fca2248008aaa7271369880f9f11cc1 62 * Public key: 04a2bfb0f7da9e1b9c0c64e14f87e8fb82eb0144e97c25fe3a977a921041a50976984d18257d2495e7bfd3d4b280220217f429287d252b0d7c0f7aae9aa624 63 c0001a6a20 64 65 5d686a07e38d2862322a2b7e829ee90c9931f119391c63328cab0d56506783588e46cb16dc2a0e920cf1a6a68806e6129b986b6b143cdb7d0752dec45a7f12c 66 * Address: 0x26ea01d982c8aeafab7f440084f90fe078cba92 67 68 69 // nodekey : 0d53d73629f75adcecd6fc1eb1c1ecb1e6a20e82a2227c0905b5bc0440be6036 70 71 */ 72 func getAddress() common.Address { 73 return common.HexToAddress("0x70524d664ffe731100208a0154e556f9bb679ae6") 74 } 75 76 func getInvalidAddress() common.Address { 77 return common.HexToAddress("0x9535b2e7faaba5288511d89341d94a38063a349b") 78 } 79 80 func generatePrivateKey() (*ecdsa.PrivateKey, error) { 81 key := "bb047e5940b6d83354d9432db7c449ac8fca2248008aaa7271369880f9f11cc1" 82 return crypto.HexToECDSA(key) 83 } 84 /* 85 func newTestValidatorSet(n int) (istanbul.ValidatorSet, []*ecdsa.PrivateKey) { 86 // generate validators 87 keys := make(Keys, n) 88 addrs := make([]common.Address, n) 89 for i := 0; i < n; i++ { 90 privateKey, _ := crypto.GenerateKey() 91 keys[i] = privateKey 92 addrs[i] = crypto.PubkeyToAddress(privateKey.PublicKey) 93 } 94 // vset := validator.NewSet(addrs, istanbul.RoundRobin) 95 sort.Sort(keys) //Keys need to be sorted by its public key address 96 return vset, keys 97 } 98 */ 99 100 type Keys []*ecdsa.PrivateKey 101 102 func (slice Keys) Len() int { 103 return len(slice) 104 } 105 106 func (slice Keys) Less(i, j int) bool { 107 return strings.Compare(crypto.PubkeyToAddress(slice[i].PublicKey).String(), crypto.PubkeyToAddress(slice[j].PublicKey).String()) < 0 108 } 109 110 func (slice Keys) Swap(i, j int) { 111 slice[i], slice[j] = slice[j], slice[i] 112 } 113 /* 114 func newSimpleBackend() (backend *simpleBackend, validatorKeys Keys, validatorSet istanbul.ValidatorSet) { 115 key, _ := generatePrivateKey() 116 validatorSet, validatorKeys = newTestValidatorSet(5) 117 backend = &simpleBackend{ 118 privateKey: key, 119 logger: log.New("backend", "simple"), 120 commitCh: make(chan *types.Block, 1), 121 } 122 return 123 } 124 */ 125 // FromECDSA exports a private key into a binary dump. 126 /* func FromECDSA(priv *ecdsa.PrivateKey) []byte { 127 if priv == nil { 128 return nil 129 } 130 return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8) 131 } 132 // Keccak256Hash calculates and returns the Keccak256 hash of the input data, 133 // converting it to an internal Hash data structure. 134 func Keccak256Hash(data ...[]byte) (h common.Hash) { 135 d := sha3.NewKeccak256() 136 for _, b := range data { 137 d.Write(b) 138 } 139 d.Sum(h[:0]) 140 return h 141 } */ 142 func PubkeyToAddress(p ecdsa.PublicKey) common.Address { 143 pubBytes := crypto.FromECDSAPub(&p) 144 return common.BytesToAddress(crypto.Keccak256(pubBytes[1:])[12:]) 145 } 146 /* 147 type conn struct { 148 fd net.Conn 149 p2p.transport 150 flags connFlag 151 cont chan error // The run loop uses cont to signal errors to setupConn. 152 id discover.NodeID // valid after the encryption handshake 153 caps []Cap // valid after the protocol handshake 154 name string // valid after the protocol handshake 155 } */ 156 157 // Peer represents a connected remote node. 158 /* type Peer struct { 159 rw *p2p.conn 160 running map[string]*p2p.protoRW 161 log log.Logger 162 //created mclock.AbsTime 163 164 wg sync.WaitGroup 165 protoErr chan error 166 closed chan struct{} 167 // disc chan DiscReason 168 } */ 169 /* 170 func sendEvent2(event istanbul.ConsensusDataEvent) { 171 // FIXME: it's inefficient because it retrieves all peers every time 172 //p := pm.findPeer(event.Target) 173 var p *p2p.Peer=&{ 174 rw : 1 175 } 176 177 if p == nil { 178 log.Warn("Failed to find peer by address", "addr", event.Target) 179 return 180 } 181 p2p.Send(p.rw, 0, event.Data) 182 } */ 183 func GetLocalIP() string { 184 addrs, err := net.InterfaceAddrs() 185 if err != nil { 186 return "" 187 } 188 for _, address := range addrs { 189 // check the address type and if it is not a loopback the display it 190 if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { 191 if ipnet.IP.To4() != nil { 192 return ipnet.IP.String() 193 } 194 } 195 } 196 return "" 197 } 198 func main() { 199 var ( 200 listenAddr = flag.String("addr", ":30391", "listen address") 201 port = flag.String("port", ":30391", "port") 202 genKey = flag.String("genkey", "", "generate a node key") 203 writeAddr = flag.Bool("writeAddress", false, "write out the node's pubkey hash and quit") 204 writeAccount = flag.Bool("writeAccount", false, "write out the node's account hash and quit") 205 nodeKeyFile = flag.String("nodekey", "", "private key filename") 206 nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)") 207 //natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") 208 //netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)") 209 //runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode") 210 verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)") 211 vmodule = flag.String("vmodule", "", "log verbosity pattern") 212 qman = flag.String( "qman", "", "boot as bootnode and qmanager") 213 214 nodeKey *ecdsa.PrivateKey 215 //key *ecdsa.PrivateKey 216 err error 217 ) 218 flag.Parse() 219 220 glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) 221 //glogger.Qmanager() 222 glogger.Verbosity(log.Lvl(*verbosity)) 223 glogger.Vmodule(*vmodule) 224 log.Root().SetHandler(glogger) 225 226 //natm, err := nat.Parse(*natdesc) 227 if err != nil { 228 utils.Fatalf("-nat: %v", err) 229 } 230 switch { 231 case *genKey != "": 232 nodeKey, err = crypto.GenerateKey() //private key와 public key를 쌍으로 생성하고, private key 구조체는 public key를 담고 있다. 233 234 fmt.Printf("nodeKey(Private key)= %v\n, public Key= %v\n", nodeKey, nodeKey.Public() ) 235 if err != nil { 236 utils.Fatalf("could not generate key: %v", err) 237 } 238 if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil { 239 utils.Fatalf("%v", err) 240 } 241 return 242 case *nodeKeyFile == "" && *nodeKeyHex == "": 243 utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key") 244 case *nodeKeyFile != "" && *nodeKeyHex != "": 245 utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive") 246 case *nodeKeyFile != "": 247 if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil { 248 fmt.Printf("%v\n", nodeKey ) 249 } 250 251 // fmt.Printf("read nodekey= %x\n, read nodekey(Public)= %x\n", nodeKey, nodeKey.Public() ) 252 if(err != nil) { 253 fmt.Printf("%v\n", nodeKey ) 254 255 utils.Fatalf("-nodekey: %v", err) 256 } 257 258 case *nodeKeyHex != "": 259 if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil { 260 utils.Fatalf("-nodekeyhex: %v", err) 261 } 262 fmt.Printf("return private key from nodekey= %x\n", nodeKey ) 263 264 } 265 266 if *writeAddr { 267 fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey)) 268 os.Exit(0) 269 } 270 if *writeAccount { 271 var account common.Address 272 account = PubkeyToAddress(nodeKey.PublicKey) 273 fmt.Printf("Address(20byte account) : %v\n, %x\n", account ,account ) 274 // sendEvent sends a p2p message with given data to a peer 275 276 277 //var accountfile string 278 //if err = crypto.SaveECDSA(accountfile, account); err != nil { 279 // utils.Fatalf("%v", err) 280 //} 281 //os.Exit(0) 282 } 283 //var restrictList *netutil.Netlist 284 //if *netrestrict != "" { 285 // restrictList, err = netutil.ParseNetlist(*netrestrict) //// ParseCIDR parses s as a CIDR notation IP address and prefix length, 286 // // like "192.0.2.0/24" 287 // //amazon:inet 172.31.6.169 netmask 255.255.240.0 broadcast 172.31.15.255 => CIDR TYPE: 172.31.6.169/20 288 // //제한 ip 범위: 172.31.0.0 ~ 172.31.15.255 289 // // https://www.ipaddressguide.com/cidr 290 // if err != nil { 291 // utils.Fatalf("-netrestrict: %v", err) 292 // } 293 //} 294 if( *qman !="" ){ 295 fmt.Printf("%v\n", qman ) 296 //p2p.Send(p.rw, 0, event.Data) 297 298 } 299 if( *listenAddr ==""){ 300 *listenAddr = GetLocalIP() 301 302 log.Info("I've get Local IP on this machine!", "listenAddr", listenAddr) 303 } 304 if( *port ==""){ 305 *port = "30391" 306 307 log.Info("I've get Local IP on this machine!") 308 } 309 310 // 311 //if *runv5 { 312 // if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil { 313 // utils.Fatalf("%v", err) 314 // } 315 //} else { //main Listen server init and run discovery 316 // 317 // var account common.Address 318 // account = PubkeyToAddress(nodeKey.PublicKey) 319 // fmt.Printf("Address(20byte account) : %v\n, %x\n", PubkeyToAddress(nodeKey.PublicKey),account ) 320 // 321 // config.Config.GetConfig("REAPCHAIN_ENV", "SETUP_INFO") 322 // global.IsBootNode = true 323 // if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil { //main 324 // utils.Fatalf("%v", err) 325 // } 326 //} 327 // 328 //select {} 329 }