github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/addrgen/internal/cmd/createconfig.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package cmd 7 8 import ( 9 "fmt" 10 "os" 11 12 "github.com/iotexproject/go-pkgs/crypto" 13 "github.com/spf13/cobra" 14 "go.uber.org/zap" 15 16 "github.com/iotexproject/iotex-core/pkg/log" 17 ) 18 19 // createConfigCmd represents the create-config command 20 var createConfigCmd = &cobra.Command{ 21 Use: "create-config [# output-file]", 22 Short: "Creates a yaml config using generated pub/pri key pair.", 23 Long: `Creates a yaml config using generated pub/pri key pair.`, 24 Run: func(cmd *cobra.Command, args []string) { 25 private, err := crypto.GenerateKey() 26 if err != nil { 27 log.L().Fatal("failed to create key pair", zap.Error(err)) 28 } 29 priKeyBytes := private.Bytes() 30 pubKeyBytes := private.PublicKey().Bytes() 31 cfgStr := fmt.Sprintf( 32 `chain: 33 producerPrivKey: "%x" 34 producerPubKey: "%x" 35 `, 36 priKeyBytes, 37 pubKeyBytes, 38 ) 39 if err := os.WriteFile(_outputFile, []byte(cfgStr), 0600); err != nil { 40 log.L().Fatal("failed to write file", zap.Error(err)) 41 } 42 }, 43 } 44 45 var _outputFile string 46 47 func init() { 48 createConfigCmd.Flags().StringVarP(&_outputFile, "output-file", "o", "", "config output file") 49 if err := createConfigCmd.MarkFlagRequired("output-file"); err != nil { 50 log.L().Fatal(err.Error()) 51 } 52 rootCmd.AddCommand(createConfigCmd) 53 }