github.com/klaytn/klaytn@v1.12.1/cmd/homi/extra/cmd.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // Copyright 2017 AMIS Technologies
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package extra
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  
    24  	"github.com/klaytn/klaytn/common"
    25  	"github.com/naoina/toml"
    26  	"github.com/urfave/cli/v2"
    27  )
    28  
    29  var ExtraCommand = &cli.Command{
    30  	Name:  "extra",
    31  	Usage: "Istanbul extraData manipulation",
    32  	Subcommands: []*cli.Command{
    33  		{
    34  			Action:    decode,
    35  			Name:      "decode",
    36  			Usage:     "To decode an Istanbul extraData",
    37  			ArgsUsage: "<extra data>",
    38  			Flags: []cli.Flag{
    39  				extraDataFlag,
    40  			},
    41  			Description: `
    42  		This command decodes extraData to vanity and validators.
    43  		`,
    44  		},
    45  		{
    46  			Action:    encode,
    47  			Name:      "encode",
    48  			Usage:     "To encode an Istanbul extraData",
    49  			ArgsUsage: "<config file> or \"0xValidator1,0xValidator2...\"",
    50  			Flags: []cli.Flag{
    51  				configFlag,
    52  				validatorsFlag,
    53  				vanityFlag,
    54  			},
    55  			Description: `
    56  		This command encodes vanity and validators to extraData. Please refer to example/config.toml.
    57  		`,
    58  		},
    59  	},
    60  }
    61  
    62  func encode(ctx *cli.Context) error {
    63  	path := ctx.String(configFlag.Name)
    64  	validators := ctx.String(validatorsFlag.Name)
    65  	if len(path) == 0 && len(validators) == 0 {
    66  		return cli.NewExitError("Must supply config file or enter validators", 0)
    67  	}
    68  
    69  	if len(path) != 0 {
    70  		extraData, err := fromConfig(path)
    71  		if err != nil {
    72  			return cli.NewExitError("Failed to encode from config data", 0)
    73  		}
    74  		fmt.Println("Encoded Istanbul extra-data:", extraData)
    75  	}
    76  
    77  	if len(validators) != 0 {
    78  		extraData, err := fromRawData(ctx.String(vanityFlag.Name), validators)
    79  		if err != nil {
    80  			return cli.NewExitError("Failed to encode from flags", 0)
    81  		}
    82  		fmt.Println("Encoded Istanbul extra-data:", extraData)
    83  	}
    84  	return nil
    85  }
    86  
    87  func fromRawData(vanity string, validators string) (string, error) {
    88  	vs := splitAndTrim(validators)
    89  
    90  	addrs := make([]common.Address, len(vs))
    91  	for i, v := range vs {
    92  		addrs[i] = common.HexToAddress(v)
    93  	}
    94  	return Encode(vanity, addrs)
    95  }
    96  
    97  func fromConfig(path string) (string, error) {
    98  	file, err := os.Open(path)
    99  	if err != nil {
   100  		return "", cli.NewExitError(fmt.Sprintf("Failed to read config file: %v", err), 1)
   101  	}
   102  	defer file.Close()
   103  
   104  	var config struct {
   105  		Vanity     string
   106  		Validators []common.Address
   107  	}
   108  
   109  	if err := toml.NewDecoder(file).Decode(&config); err != nil {
   110  		return "", cli.NewExitError(fmt.Sprintf("Failed to parse config file: %v", err), 2)
   111  	}
   112  
   113  	return Encode(config.Vanity, config.Validators)
   114  }
   115  
   116  func decode(ctx *cli.Context) error {
   117  	if !ctx.IsSet(extraDataFlag.Name) {
   118  		return cli.NewExitError("Must supply extra data", 10)
   119  	}
   120  
   121  	extraString := ctx.String(extraDataFlag.Name)
   122  	vanity, istanbulExtra, err := Decode(extraString)
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	fmt.Println("vanity: ", "0x"+common.Bytes2Hex(vanity))
   128  
   129  	for _, v := range istanbulExtra.Validators {
   130  		fmt.Println("validator: ", v.Hex())
   131  	}
   132  
   133  	if len(istanbulExtra.Seal) != 0 {
   134  		fmt.Println("seal:", "0x"+common.Bytes2Hex(istanbulExtra.Seal))
   135  	}
   136  
   137  	for _, seal := range istanbulExtra.CommittedSeal {
   138  		fmt.Println("committed seal: ", "0x"+common.Bytes2Hex(seal))
   139  	}
   140  
   141  	return nil
   142  }