github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/accountcreate.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 account 7 8 import ( 9 "encoding/json" 10 "fmt" 11 "log" 12 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/go-pkgs/crypto" 16 "github.com/iotexproject/iotex-core/ioctl/config" 17 "github.com/iotexproject/iotex-core/ioctl/output" 18 ) 19 20 var numAccounts uint 21 22 // Multi-language support 23 var ( 24 _createCmdShorts = map[config.Language]string{ 25 config.English: "Create N new accounts and print them", 26 config.Chinese: "创建 N 个新账户,并打印", 27 } 28 _flagNumUsages = map[config.Language]string{ 29 config.English: "number of accounts to create", 30 config.Chinese: "指定创建账户的数量", 31 } 32 ) 33 34 // _accountCreateCmd represents the account create command 35 var _accountCreateCmd = &cobra.Command{ 36 Use: "create", 37 Short: config.TranslateInLang(_createCmdShorts, config.UILanguage), 38 Args: cobra.ExactArgs(0), 39 RunE: func(cmd *cobra.Command, args []string) error { 40 cmd.SilenceUsage = true 41 err := accountCreate() 42 return output.PrintError(err) 43 }, 44 } 45 46 type createMessage struct { 47 Accounts []generatedAccount `json:"accounts"` 48 } 49 50 type generatedAccount struct { 51 Address string `json:"address"` 52 EthAddress string `json:"ethAddress"` 53 PrivateKey string `json:"privateKey"` 54 PublicKey string `json:"publicKey"` 55 } 56 57 func init() { 58 _accountCreateCmd.Flags().UintVarP(&numAccounts, "num", "n", 1, 59 config.TranslateInLang(_flagNumUsages, config.UILanguage)) 60 } 61 62 func accountCreate() error { 63 var err error 64 var private crypto.PrivateKey 65 newAccounts := make([]generatedAccount, 0) 66 for i := 0; i < int(numAccounts); i++ { 67 if !CryptoSm2 { 68 private, err = crypto.GenerateKey() 69 if err != nil { 70 return output.NewError(output.CryptoError, "failed to generate new private key", err) 71 } 72 73 } else { 74 private, err = crypto.GenerateKeySm2() 75 if err != nil { 76 return output.NewError(output.CryptoError, "failed to generate new sm2 private key", err) 77 } 78 } 79 addr := private.PublicKey().Address() 80 if addr == nil { 81 return output.NewError(output.ConvertError, "failed to convert public key into address", nil) 82 } 83 newAccount := generatedAccount{ 84 Address: addr.String(), 85 EthAddress: addr.Hex(), 86 PrivateKey: fmt.Sprintf("%x", private.Bytes()), 87 PublicKey: fmt.Sprintf("%x", private.PublicKey().Bytes()), 88 } 89 newAccounts = append(newAccounts, newAccount) 90 } 91 92 message := createMessage{Accounts: newAccounts} 93 fmt.Println(message.String()) 94 return nil 95 } 96 97 func (m *createMessage) String() string { 98 if output.Format == "" { 99 byteAsJSON, err := json.MarshalIndent(m, "", " ") 100 if err != nil { 101 log.Panic(err) 102 } 103 return fmt.Sprint(string(byteAsJSON)) 104 } 105 return output.FormatString(output.Result, m) 106 }