github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/cmd/ginb/misccmd.go (about)

     1  // Copyright 2016 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  	"encoding/hex"
    22  	"fmt"
    23  	"github.com/insight-chain/inb-go/accounts/keystore"
    24  	"github.com/insight-chain/inb-go/cmd/utils"
    25  	"github.com/insight-chain/inb-go/consensus/ethash"
    26  	"github.com/insight-chain/inb-go/crypto"
    27  	"github.com/insight-chain/inb-go/eth"
    28  	"github.com/insight-chain/inb-go/params"
    29  	"gopkg.in/urfave/cli.v1"
    30  	"io/ioutil"
    31  	"os"
    32  	"runtime"
    33  	"strconv"
    34  	"strings"
    35  )
    36  
    37  var (
    38  	makecacheCommand = cli.Command{
    39  		Action:    utils.MigrateFlags(makecache),
    40  		Name:      "makecache",
    41  		Usage:     "Generate ethash verification cache (for testing)",
    42  		ArgsUsage: "<blockNum> <outputDir>",
    43  		Category:  "MISCELLANEOUS COMMANDS",
    44  		Description: `
    45  The makecache command generates an ethash cache in <outputDir>.
    46  
    47  This command exists to support the system testing project.
    48  Regular users do not need to execute it.
    49  `,
    50  	}
    51  	makedagCommand = cli.Command{
    52  		Action:    utils.MigrateFlags(makedag),
    53  		Name:      "makedag",
    54  		Usage:     "Generate ethash mining DAG (for testing)",
    55  		ArgsUsage: "<blockNum> <outputDir>",
    56  		Category:  "MISCELLANEOUS COMMANDS",
    57  		Description: `
    58  The makedag command generates an ethash DAG in <outputDir>.
    59  
    60  This command exists to support the system testing project.
    61  Regular users do not need to execute it.
    62  `,
    63  	}
    64  	versionCommand = cli.Command{
    65  		Action:    utils.MigrateFlags(version),
    66  		Name:      "version",
    67  		Usage:     "Print version numbers",
    68  		ArgsUsage: " ",
    69  		Category:  "MISCELLANEOUS COMMANDS",
    70  		Description: `
    71  The output of this command is supposed to be machine-readable.
    72  `,
    73  	}
    74  	licenseCommand = cli.Command{
    75  		Action:    utils.MigrateFlags(license),
    76  		Name:      "license",
    77  		Usage:     "Display license information",
    78  		ArgsUsage: " ",
    79  		Category:  "MISCELLANEOUS COMMANDS",
    80  	}
    81  	//inb by ghy begin
    82  	nodekeyCommand = cli.Command{
    83  		Action:    utils.MigrateFlags(nodekey),
    84  		Name:      "nodekey",
    85  		Usage:     "create nodekey before init",
    86  		ArgsUsage: " data-source path",
    87  		Category:  "create nodekey before init",
    88  	}
    89  	//inb by ghy end
    90  
    91  	ImportPrivateKeyCommand = cli.Command{
    92  		Action:    utils.MigrateFlags(keystoreToPrivateKey),
    93  		Name:      "privatekey",
    94  		Usage:     "print private key",
    95  		ArgsUsage: " data-source path",
    96  		Category:  "print private key",
    97  	}
    98  )
    99  
   100  // makecache generates an ethash verification cache into the provided folder.
   101  func makecache(ctx *cli.Context) error {
   102  	args := ctx.Args()
   103  	if len(args) != 2 {
   104  		utils.Fatalf(`Usage: ginb makecache <block number> <outputdir>`)
   105  	}
   106  	block, err := strconv.ParseUint(args[0], 0, 64)
   107  	if err != nil {
   108  		utils.Fatalf("Invalid block number: %v", err)
   109  	}
   110  	ethash.MakeCache(block, args[1])
   111  
   112  	return nil
   113  }
   114  
   115  // makedag generates an ethash mining DAG into the provided folder.
   116  func makedag(ctx *cli.Context) error {
   117  	args := ctx.Args()
   118  	if len(args) != 2 {
   119  		utils.Fatalf(`Usage: ginb makedag <block number> <outputdir>`)
   120  	}
   121  	block, err := strconv.ParseUint(args[0], 0, 64)
   122  	if err != nil {
   123  		utils.Fatalf("Invalid block number: %v", err)
   124  	}
   125  	ethash.MakeDataset(block, args[1])
   126  
   127  	return nil
   128  }
   129  
   130  func version(ctx *cli.Context) error {
   131  	fmt.Println(strings.Title(clientIdentifier))
   132  	fmt.Println("Version:", params.VersionWithMeta)
   133  	if gitCommit != "" {
   134  		fmt.Println("Git Commit:", gitCommit)
   135  	}
   136  	fmt.Println("Architecture:", runtime.GOARCH)
   137  	fmt.Println("Protocol Versions:", eth.ProtocolVersions)
   138  	fmt.Println("Network Id:", eth.DefaultConfig.NetworkId)
   139  	fmt.Println("Go Version:", runtime.Version())
   140  	fmt.Println("Operating System:", runtime.GOOS)
   141  	fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
   142  	fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
   143  	return nil
   144  }
   145  
   146  func license(_ *cli.Context) error {
   147  	fmt.Println(`Ginb is free software: you can redistribute it and/or modify
   148  it under the terms of the GNU General Public License as published by
   149  the Free Software Foundation, either version 3 of the License, or
   150  (at your option) any later version.
   151  
   152  Geth is distributed in the hope that it will be useful,
   153  but WITHOUT ANY WARRANTY; without even the implied warranty of
   154  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   155  GNU General Public License for more details.
   156  
   157  You should have received a copy of the GNU General Public License
   158  along with ginb. If not, see <http://www.gnu.org/licenses/>.`)
   159  	return nil
   160  }
   161  
   162  //inb by ghy begin
   163  //Create file nodekey for generate nodeid
   164  func nodekey(ctx *cli.Context) error {
   165  	args := ctx.Args()
   166  	if len(args) != 1 {
   167  		utils.Fatalf(`Usage: ginb nodekey <block number> <outputdir>`)
   168  	}
   169  	var nodeKey *ecdsa.PrivateKey
   170  	_, err := os.Stat(args[0] + "/ginb/nodekey")
   171  	if os.IsExist(err) || err == nil {
   172  		fmt.Println("nodekey is already exist")
   173  		nodeKey, _ = crypto.LoadECDSA(args[0] + "/ginb/nodekey")
   174  
   175  	} else {
   176  		nodeKey, _ = crypto.GenerateKey()
   177  
   178  		err = os.MkdirAll(args[0]+"/ginb/", os.ModePerm)
   179  		if err = crypto.SaveECDSA(args[0]+"/ginb/nodekey", nodeKey); err != nil {
   180  			utils.Fatalf(error.Error(err))
   181  		}
   182  	}
   183  	nodeid := fmt.Sprintf("%x", crypto.FromECDSAPub(&nodeKey.PublicKey)[1:])
   184  	fmt.Println(nodeid)
   185  	return nil
   186  }
   187  
   188  //inb by ghy end
   189  
   190  func keystoreToPrivateKey(ctx *cli.Context) error {
   191  	args := ctx.Args()
   192  	if len(args) != 2 {
   193  		utils.Fatalf(`filename and password are necessary`)
   194  	}
   195  	filename := args[0]
   196  	auth := args[1]
   197  	// Load the key from the keystore and decrypt its contents
   198  	keyjson, err := ioutil.ReadFile(filename)
   199  	if err != nil {
   200  		return err
   201  	}
   202  	key, err := keystore.DecryptKey(keyjson, auth)
   203  	if err != nil {
   204  		return err
   205  	}
   206  	fmt.Println("privateKey:", hex.EncodeToString(key.PrivateKey.D.Bytes()))
   207  	fmt.Println("address:", crypto.PubkeyToAddress(key.PrivateKey.PublicKey).String())
   208  
   209  	return nil
   210  }