Skip to content

Commit

Permalink
Allow derived keystore to be displayed on stdout.
Browse files Browse the repository at this point in the history
Fixes #143
  • Loading branch information
mcdee committed Aug 2, 2024
1 parent 8aaf16a commit f3ae2ba
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.35.5:
- allow keystore to be output to the console

1.35.4:
- provide consensus and execution client info in block info output

Expand Down
4 changes: 4 additions & 0 deletions cmd/account/derive/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

type dataIn struct {
quiet bool
json bool
// Derivation information.
mnemonic string
path string
Expand All @@ -37,6 +38,9 @@ func input(_ context.Context) (*dataIn, error) {
// Quiet.
data.quiet = viper.GetBool("quiet")

// JSON.
data.json = viper.GetBool("json")

// Mnemonic.
if viper.GetString("mnemonic") == "" {
return nil, errors.New("mnemonic is required")
Expand Down
11 changes: 8 additions & 3 deletions cmd/account/derive/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
)

type dataOut struct {
json bool
showPrivateKey bool
showWithdrawalCredentials bool
generateKeystore bool
Expand Down Expand Up @@ -94,10 +95,14 @@ func outputKeystore(_ context.Context, data *dataOut) (string, error) {
return "", errors.Wrap(err, "failed to marshal keystore JSON")
}

keystoreFilename := fmt.Sprintf("keystore-%s-%d.json", strings.ReplaceAll(data.path, "/", "_"), time.Now().Unix())
if data.json {
fmt.Fprintf(os.Stdout, "%s\n", string(out))
} else {
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))
if err := os.WriteFile(keystoreFilename, out, 0o600); err != nil {
return "", errors.Wrap(err, fmt.Sprintf("failed to write %s", keystoreFilename))
}
}
return "", nil
}
1 change: 1 addition & 0 deletions cmd/account/derive/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
}

results := &dataOut{
json: data.json,
showPrivateKey: data.showPrivateKey,
showWithdrawalCredentials: data.showWithdrawalCredentials,
generateKeystore: data.generateKeystore,
Expand Down
4 changes: 4 additions & 0 deletions cmd/accountderive.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func init() {
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")
accountDeriveCmd.Flags().Bool("json", false, "display the JSON keystore for the derived account on stdout")
}

func accountDeriveBindings(cmd *cobra.Command) {
Expand All @@ -62,4 +63,7 @@ func accountDeriveBindings(cmd *cobra.Command) {
if err := viper.BindPFlag("generate-keystore", cmd.Flags().Lookup("generate-keystore")); err != nil {
panic(err)
}
if err := viper.BindPFlag("json", cmd.Flags().Lookup("json")); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// ReleaseVersion is the release version of the codebase.
// Usually overridden by tag names when building binaries.
var ReleaseVersion = "local build (latest release 1.35.4)"
var ReleaseVersion = "local build (latest release 1.35.5)"

// versionCmd represents the version command.
var versionCmd = &cobra.Command{
Expand Down
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ $ ethdo account create --account="Personal wallet/Operations" --wallet-passphras
- `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
- `json`: output the generated keystore to stdout

```sh
$ ethdo account derive --mnemonic="abandon ... abandon art" --path="m/12381/3600/0/0"
Expand Down

0 comments on commit f3ae2ba

Please sign in to comment.