github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/cmd/qman-new/main.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/ethereum/go-ethereum/cmd/utils"
    10  	"github.com/ethereum/go-ethereum/config"
    11  	"github.com/ethereum/go-ethereum/ethdb"
    12  	"github.com/ethereum/go-ethereum/internal/debug"
    13  	"github.com/ethereum/go-ethereum/log"
    14  	"github.com/ethereum/go-ethereum/params"
    15  	"gopkg.in/urfave/cli.v1"
    16  )
    17  
    18  var (
    19  	app = cli.NewApp()
    20  
    21  	// Flags
    22  	// keystoreFlag = cli.StringFlag{
    23  	// 	Name:  "keystore",
    24  	// 	Value: filepath.Join(node.DefaultDataDir(), "keystore"),
    25  	// 	Usage: "Directory for the keystore",
    26  	// }
    27  	// configdirFlag = cli.StringFlag{
    28  	// 	Name:  "configdir",
    29  	// 	Value: DefaultConfigDir(),
    30  	// 	Usage: "Directory for Clef configuration",
    31  	// }
    32  	chainIdFlag = cli.Int64Flag{
    33  		Name:  "chainid",
    34  		Value: params.MainnetChainConfig.ChainId.Int64(),
    35  		Usage: "Chain id to use for signing (1=mainnet, 3=Ropsten, 4=Rinkeby, 5=Goerli)",
    36  	}
    37  	listenAddrFlag = cli.StringFlag{
    38  		Name:  "addr",
    39  		Usage: "HTTP server listening interface",
    40  		Value: "localhost:9980",
    41  	}
    42  	// listenPortFlag = cli.IntFlag{
    43  	// 	Name:  "port",
    44  	// 	Usage: "HTTP server listening port",
    45  	// 	Value: 9980,
    46  	// }
    47  	verbosityFlag = cli.IntFlag{
    48  		Name:  "verbosity",
    49  		Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
    50  		Value: 3,
    51  	}
    52  
    53  	// Commands
    54  	initCommand = cli.Command{
    55  		Action:    utils.MigrateFlags(initializeSecrets),
    56  		Name:      "init",
    57  		Usage:     "Initialize the signer, generate secret storage",
    58  		ArgsUsage: "",
    59  		Flags: []cli.Flag{
    60  			utils.DataDirFlag,
    61  			//configdirFlag,
    62  		},
    63  		Description: `
    64  The init command generates a master seed which Clef can use to store credentials and data needed for
    65  the rule-engine to work.`,
    66  	}
    67  )
    68  
    69  func init() {
    70  	app.Name = "Qmanager"
    71  	app.Usage = "Manage Reapchain Qrand operations"
    72  	app.Flags = []cli.Flag{
    73  		//keystoreFlag,
    74  		//configdirFlag,
    75  		chainIdFlag,
    76  		listenAddrFlag,
    77  		// listenPortFlag,
    78  		verbosityFlag,
    79  		utils.DataDirFlag,
    80  		utils.NoUSBFlag,
    81  	}
    82  	app.Commands = []cli.Command{
    83  		initCommand,
    84  	}
    85  	app.Before = func(ctx *cli.Context) error {
    86  		return debug.Setup(ctx)
    87  	}
    88  	app.After = func(ctx *cli.Context) error {
    89  		debug.Exit()
    90  		//prompt.Stdin.Close() // Resets terminal mode.
    91  		return nil
    92  	}
    93  	app.Action = qman
    94  }
    95  
    96  func main() {
    97  	if err := app.Run(os.Args); err != nil {
    98  		fmt.Fprintln(os.Stderr, err)
    99  		os.Exit(1)
   100  	}
   101  }
   102  
   103  func qman(ctx *cli.Context) error {
   104  	if args := ctx.Args(); len(args) > 0 {
   105  		return fmt.Errorf("invalid command: %q", args[0])
   106  	}
   107  	config := getConfig(ctx)
   108  	path := "./qman"
   109  	if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
   110  		path = ctx.GlobalString(utils.DataDirFlag.Name)
   111  	}
   112  	db, err := ethdb.NewLDBDatabase(filepath.Join(path, "qmandata"), 0, 0)
   113  	if err != nil {
   114  		return errors.New("Cannot open database")
   115  	}
   116  	addr := "127.0.0.1:9980"
   117  	if ctx.GlobalIsSet(listenAddrFlag.Name) {
   118  		addr = ctx.GlobalString(listenAddrFlag.Name)
   119  	}
   120  	qman := NewQmanager(db, config, addr)
   121  	qman.Start()
   122  
   123  	return nil
   124  }
   125  
   126  func getConfig(ctx *cli.Context) *config.EnvConfig {
   127  	var config config.EnvConfig
   128  	config.GetConfig("REAPCHAIN_ENV", "SETUP_INFO")
   129  	log.Info("Load config.json", "config", config)
   130  	return &config
   131  }
   132  
   133  func initializeSecrets(c *cli.Context) error {
   134  	return nil
   135  }