Skip to content

Commit 1ae9618

Browse files
committed
cmd/ethkit: add --entropy option
1 parent b557114 commit 1ae9618

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cmd/ethkit/wallet.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func init() {
3131
cmd.Flags().Bool("print-mnemonic", false, "print wallet secret mnemonic from keyfile (danger!)")
3232
cmd.Flags().Bool("print-private-key", false, "print wallet private key from keyfile (danger!)")
3333
cmd.Flags().Bool("import-mnemonic", false, "import a secret mnemonic to a new keyfile")
34+
cmd.Flags().Int("entropy", 256, "set entropy bit size for new wallet, options: 128, 256 (default: 256)")
3435
cmd.Flags().String("path", "", fmt.Sprintf("set derivation path, default: %s", ethwallet.DefaultWalletOptions.DerivationPath))
3536

3637
rootCmd.AddCommand(cmd)
@@ -44,6 +45,7 @@ type wallet struct {
4445
fPrintMnemonic bool
4546
fPrintPrivateKey bool
4647
fImportMnemonic bool
48+
fEntropy int
4749
fPath string
4850

4951
// wallet key file
@@ -58,6 +60,7 @@ func (c *wallet) Run(cmd *cobra.Command, args []string) {
5860
c.fPrintMnemonic, _ = cmd.Flags().GetBool("print-mnemonic")
5961
c.fPrintPrivateKey, _ = cmd.Flags().GetBool("print-private-key")
6062
c.fImportMnemonic, _ = cmd.Flags().GetBool("import-mnemonic")
63+
c.fEntropy, _ = cmd.Flags().GetInt("entropy")
6164
c.fPath, _ = cmd.Flags().GetString("path")
6265

6366
if c.fKeyFile == "" {
@@ -185,7 +188,7 @@ func (c *wallet) createNew() error {
185188
derivationPath = ethwallet.DefaultWalletOptions.DerivationPath
186189
}
187190

188-
c.wallet, err = getWallet(importMnemonic, derivationPath)
191+
c.wallet, err = getWallet(importMnemonic, derivationPath, c.fEntropy)
189192
if err != nil {
190193
return err
191194
}
@@ -275,7 +278,7 @@ func readPlainInput(prompt string) ([]byte, error) {
275278
return []byte(text), nil
276279
}
277280

278-
func getWallet(mnemonic, derivationPath string) (*ethwallet.Wallet, error) {
281+
func getWallet(mnemonic, derivationPath string, entropy int) (*ethwallet.Wallet, error) {
279282
var err error
280283
var wallet *ethwallet.Wallet
281284

@@ -286,9 +289,15 @@ func getWallet(mnemonic, derivationPath string) (*ethwallet.Wallet, error) {
286289
if mnemonic != "" {
287290
wallet, err = ethwallet.NewWalletFromMnemonic(mnemonic, derivationPath)
288291
} else {
292+
if entropy == 0 {
293+
entropy = 256 // default
294+
}
295+
if entropy != 128 && entropy != 256 {
296+
return nil, fmt.Errorf("invalid entropy bit size, options are 128 or 256")
297+
}
289298
wallet, err = ethwallet.NewWalletFromRandomEntropy(ethwallet.WalletOptions{
290299
DerivationPath: derivationPath,
291-
RandomWalletEntropyBitSize: ethwallet.EntropyBitSize24WordMnemonic,
300+
RandomWalletEntropyBitSize: entropy,
292301
})
293302
}
294303
if err != nil {

0 commit comments

Comments
 (0)