github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/cmd/qman/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  package main
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"flag"
    22  	"fmt"
    23  	"github.com/ethereum/go-ethereum/cmd/utils"
    24  	"github.com/ethereum/go-ethereum/config"
    25  	"github.com/ethereum/go-ethereum/crypto"
    26  	"github.com/ethereum/go-ethereum/log"
    27  	"github.com/ethereum/go-ethereum/p2p/discover"
    28  	"github.com/ethereum/go-ethereum/qmanager"
    29  	"os"
    30  )
    31  func main() {
    32  	var (
    33  		listenAddr  = flag.String("addr", ":30301", "listen address")
    34  		genKey      = flag.String("genkey", "", "generate a qman key")
    35  		qmanKeyFile = flag.String("qmankey", "", "private key filename")
    36  		qmanKeyHex  = flag.String("qmankeyhex", "", "private key as hex (for testing)")
    37  		verbosity   = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
    38  		vmodule     = flag.String("vmodule", "", "log verbosity pattern")
    39  
    40  		qmanKey *ecdsa.PrivateKey
    41  		err     error
    42  	)
    43  	flag.Parse()
    44  	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
    45  	//glogger.Qmanager()
    46  	glogger.Verbosity(log.Lvl(*verbosity))
    47  	glogger.Vmodule(*vmodule)
    48  	log.Root().SetHandler(glogger)
    49  
    50  
    51  	switch {
    52  	case *genKey != "":
    53  		qmanKey, err = crypto.GenerateKey()  //private key와 public key를 쌍으로 생성하고, private key 구조체는 public key를 담고 있다.
    54  
    55  		fmt.Printf("qmanKey(Private key)= %v\n, public Key= %v\n", qmanKey, qmanKey.Public() )
    56  		if err != nil {
    57  			utils.Fatalf("could not generate key: %v", err)
    58  		}
    59  		if err = crypto.SaveECDSA(*genKey, qmanKey); err != nil {
    60  			utils.Fatalf("%v", err)
    61  		}
    62  		return
    63  	case *qmanKeyFile == "" && *qmanKeyHex == "":
    64  		utils.Fatalf("Use -qmankey or -qmankeyhex to specify a private key")
    65  	case *qmanKeyFile != "" && *qmanKeyHex != "":
    66  		utils.Fatalf("Options -qmankey and -qmankeyhex are mutually exclusive")
    67  	case *qmanKeyFile != "":
    68  		if qmanKey, err = crypto.LoadECDSA(*qmanKeyFile); err != nil {
    69  			fmt.Printf("%v\n", qmanKey )
    70  		}
    71  
    72  		if(err != nil) {
    73  			fmt.Printf("%v\n", qmanKey )
    74  
    75  			utils.Fatalf("-qmankey: %v", err)
    76  		}
    77  
    78  	case *qmanKeyHex != "":
    79  		if qmanKey, err = crypto.HexToECDSA(*qmanKeyHex); err != nil {
    80  			utils.Fatalf("-qmankeyhex: %v", err)
    81  		}
    82  		fmt.Printf("return private key from qmankey= %x\n", listenAddr )
    83  
    84  	}
    85  
    86  	//pwd, err := os.Getwd()
    87  	//fmt.Printf("current working directory: pwd= %v \n",  pwd)
    88  	//if err != nil {
    89  	//	fmt.Printf("failed to get current working directory: pwd= %v , err=%v",  pwd, err)
    90  	//}
    91  
    92  	//log.Info("QManager Standalone Started")
    93  
    94  	config.Config.GetConfig("REAPCHAIN_ENV", "SETUP_INFO")
    95  		//var account common.Address
    96  		//account = PubkeyToAddress(nodeKey.PublicKey)
    97  		//fmt.Printf("Address(20byte account) : %v\n, %x\n", PubkeyToAddress(nodeKey.PublicKey),account )
    98  	NodeID := discover.PubkeyID(&qmanKey.PublicKey).String()
    99  	log.Info("QManager ID: ", "self",  NodeID + "@" + *listenAddr)
   100  		qmanager.InitializeQManager()
   101  		qmanager.Start(listenAddr, qmanKey)
   102  
   103  	select {}
   104  }