From 545665a79fde85387ea6b1c8a70f2695b093fdf9 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Mon, 8 May 2023 09:21:20 +0100 Subject: [PATCH] Add generate-keystore to account derive. --- CHANGELOG.md | 1 + cmd/account/derive/input.go | 6 +++- cmd/account/derive/output.go | 55 ++++++++++++++++++++++++++++++++--- cmd/account/derive/process.go | 4 ++- cmd/accountderive.go | 4 +++ docs/usage.md | 1 + 6 files changed, 65 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e39926..cc7f893 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ dev: - initial support for deneb + - add "--generate-keystore" option for "account derive" 1.30.0: - add "chain spec" command diff --git a/cmd/account/derive/input.go b/cmd/account/derive/input.go index bb6026c..d8ba0fa 100644 --- a/cmd/account/derive/input.go +++ b/cmd/account/derive/input.go @@ -1,4 +1,4 @@ -// Copyright © 2020 Weald Technology Trading +// Copyright © 2020, 2023 Weald Technology Trading // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -28,6 +28,7 @@ type dataIn struct { // Output options. showPrivateKey bool showWithdrawalCredentials bool + generateKeystore bool } func input(_ context.Context) (*dataIn, error) { @@ -54,5 +55,8 @@ func input(_ context.Context) (*dataIn, error) { // Show withdrawal credentials. data.showWithdrawalCredentials = viper.GetBool("show-withdrawal-credentials") + // Generate keystore. + data.generateKeystore = viper.GetBool("generate-keystore") + return data, nil } diff --git a/cmd/account/derive/output.go b/cmd/account/derive/output.go index d4ef2d5..19d603e 100644 --- a/cmd/account/derive/output.go +++ b/cmd/account/derive/output.go @@ -1,4 +1,4 @@ -// Copyright © 2020 Weald Technology Trading +// Copyright © 2020, 2023 Weald Technology Trading // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -15,21 +15,29 @@ package accountderive import ( "context" + "encoding/json" "fmt" + "os" "strings" + "time" + "github.com/google/uuid" "github.com/pkg/errors" + "github.com/wealdtech/ethdo/util" e2types "github.com/wealdtech/go-eth2-types/v2" - util "github.com/wealdtech/go-eth2-util" + ethutil "github.com/wealdtech/go-eth2-util" + keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4" ) type dataOut struct { showPrivateKey bool showWithdrawalCredentials bool + generateKeystore bool key *e2types.BLSPrivateKey + path string } -func output(_ context.Context, data *dataOut) (string, error) { +func output(ctx context.Context, data *dataOut) (string, error) { if data == nil { return "", errors.New("no data") } @@ -37,13 +45,17 @@ func output(_ context.Context, data *dataOut) (string, error) { return "", errors.New("no key") } + if data.generateKeystore { + return outputKeystore(ctx, data) + } + builder := strings.Builder{} if data.showPrivateKey { builder.WriteString(fmt.Sprintf("Private key: %#x\n", data.key.Marshal())) } if data.showWithdrawalCredentials { - withdrawalCredentials := util.SHA256(data.key.PublicKey().Marshal()) + withdrawalCredentials := ethutil.SHA256(data.key.PublicKey().Marshal()) withdrawalCredentials[0] = byte(0) // BLS_WITHDRAWAL_PREFIX builder.WriteString(fmt.Sprintf("Withdrawal credentials: %#x\n", withdrawalCredentials)) } @@ -53,3 +65,38 @@ func output(_ context.Context, data *dataOut) (string, error) { return builder.String(), nil } + +func outputKeystore(_ context.Context, data *dataOut) (string, error) { + passphrase, err := util.GetPassphrase() + if err != nil { + return "", errors.New("no passphrase supplied") + } + + encryptor := keystorev4.New() + crypto, err := encryptor.Encrypt(data.key.Marshal(), passphrase) + if err != nil { + return "", errors.New("failed to encrypt private key") + } + + uuid, err := uuid.NewRandom() + if err != nil { + return "", errors.New("failed to generate UUID") + } + ks := make(map[string]interface{}) + ks["uuid"] = uuid.String() + ks["pubkey"] = fmt.Sprintf("%x", data.key.PublicKey().Marshal()) + ks["version"] = 4 + ks["path"] = data.path + ks["crypto"] = crypto + out, err := json.Marshal(ks) + if err != nil { + return "", errors.Wrap(err, "failed to marshal keystore JSON") + } + + keystoreFilename := fmt.Sprintf("keystore-%s-%d.json", strings.ReplaceAll(data.path, "/", "_"), time.Now().Unix()) + + if err := os.WriteFile(keystoreFilename, out, 0o600); err != nil { + return "", errors.Wrap(err, fmt.Sprintf("failed to write %s", keystoreFilename)) + } + return "", errors.New("not implemented") +} diff --git a/cmd/account/derive/process.go b/cmd/account/derive/process.go index 4135fa6..7a1a6be 100644 --- a/cmd/account/derive/process.go +++ b/cmd/account/derive/process.go @@ -1,4 +1,4 @@ -// Copyright © 2020 Weald Technology Trading +// Copyright © 2020, 2023 Weald Technology Trading // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -40,7 +40,9 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) { results := &dataOut{ showPrivateKey: data.showPrivateKey, showWithdrawalCredentials: data.showWithdrawalCredentials, + generateKeystore: data.generateKeystore, key: key.(*e2types.BLSPrivateKey), + path: data.path, } return results, nil diff --git a/cmd/accountderive.go b/cmd/accountderive.go index 2d9910c..86511bb 100644 --- a/cmd/accountderive.go +++ b/cmd/accountderive.go @@ -49,6 +49,7 @@ func init() { accountFlags(accountDeriveCmd) accountDeriveCmd.Flags().Bool("show-private-key", false, "show private key for derived account") accountDeriveCmd.Flags().Bool("show-withdrawal-credentials", false, "show withdrawal credentials for derived account") + accountDeriveCmd.Flags().Bool("generate-keystore", false, "generate a keystore for the derived account") } func accountDeriveBindings(cmd *cobra.Command) { @@ -58,4 +59,7 @@ func accountDeriveBindings(cmd *cobra.Command) { if err := viper.BindPFlag("show-withdrawal-credentials", cmd.Flags().Lookup("show-withdrawal-credentials")); err != nil { panic(err) } + if err := viper.BindPFlag("generate-keystore", cmd.Flags().Lookup("generate-keystore")); err != nil { + panic(err) + } } diff --git a/docs/usage.md b/docs/usage.md index df2b400..da04cbb 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -169,6 +169,7 @@ $ ethdo account create --account="Personal wallet/Operations" --wallet-passphras - `path`: the HD path used to derive the account - `show-private-key`: show the private of the derived account. **Warning** displaying private keys, especially those derived from seeds held on hardware wallets, can expose your Ether to risk of being stolen. Only use this option if you are sure you understand the risks involved - `show-withdrawal-credentials`: show the withdrawal credentials of the derived account +- `generate-keystore`: generate a keystore for the account ```sh $ ethdo account derive --mnemonic="abandon ... abandon art" --path="m/12381/3600/0/0"