Skip to content

Commit f3ae2ba

Browse files
committed
Allow derived keystore to be displayed on stdout.
Fixes #143
1 parent 8aaf16a commit f3ae2ba

File tree

7 files changed

+22
-4
lines changed

7 files changed

+22
-4
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
1.35.5:
2+
- allow keystore to be output to the console
3+
14
1.35.4:
25
- provide consensus and execution client info in block info output
36

cmd/account/derive/input.go

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
type dataIn struct {
2424
quiet bool
25+
json bool
2526
// Derivation information.
2627
mnemonic string
2728
path string
@@ -37,6 +38,9 @@ func input(_ context.Context) (*dataIn, error) {
3738
// Quiet.
3839
data.quiet = viper.GetBool("quiet")
3940

41+
// JSON.
42+
data.json = viper.GetBool("json")
43+
4044
// Mnemonic.
4145
if viper.GetString("mnemonic") == "" {
4246
return nil, errors.New("mnemonic is required")

cmd/account/derive/output.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
)
3232

3333
type dataOut struct {
34+
json bool
3435
showPrivateKey bool
3536
showWithdrawalCredentials bool
3637
generateKeystore bool
@@ -94,10 +95,14 @@ func outputKeystore(_ context.Context, data *dataOut) (string, error) {
9495
return "", errors.Wrap(err, "failed to marshal keystore JSON")
9596
}
9697

97-
keystoreFilename := fmt.Sprintf("keystore-%s-%d.json", strings.ReplaceAll(data.path, "/", "_"), time.Now().Unix())
98+
if data.json {
99+
fmt.Fprintf(os.Stdout, "%s\n", string(out))
100+
} else {
101+
keystoreFilename := fmt.Sprintf("keystore-%s-%d.json", strings.ReplaceAll(data.path, "/", "_"), time.Now().Unix())
98102

99-
if err := os.WriteFile(keystoreFilename, out, 0o600); err != nil {
100-
return "", errors.Wrap(err, fmt.Sprintf("failed to write %s", keystoreFilename))
103+
if err := os.WriteFile(keystoreFilename, out, 0o600); err != nil {
104+
return "", errors.Wrap(err, fmt.Sprintf("failed to write %s", keystoreFilename))
105+
}
101106
}
102107
return "", nil
103108
}

cmd/account/derive/process.go

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func process(ctx context.Context, data *dataIn) (*dataOut, error) {
3838
}
3939

4040
results := &dataOut{
41+
json: data.json,
4142
showPrivateKey: data.showPrivateKey,
4243
showWithdrawalCredentials: data.showWithdrawalCredentials,
4344
generateKeystore: data.generateKeystore,

cmd/accountderive.go

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func init() {
5050
accountDeriveCmd.Flags().Bool("show-private-key", false, "show private key for derived account")
5151
accountDeriveCmd.Flags().Bool("show-withdrawal-credentials", false, "show withdrawal credentials for derived account")
5252
accountDeriveCmd.Flags().Bool("generate-keystore", false, "generate a keystore for the derived account")
53+
accountDeriveCmd.Flags().Bool("json", false, "display the JSON keystore for the derived account on stdout")
5354
}
5455

5556
func accountDeriveBindings(cmd *cobra.Command) {
@@ -62,4 +63,7 @@ func accountDeriveBindings(cmd *cobra.Command) {
6263
if err := viper.BindPFlag("generate-keystore", cmd.Flags().Lookup("generate-keystore")); err != nil {
6364
panic(err)
6465
}
66+
if err := viper.BindPFlag("json", cmd.Flags().Lookup("json")); err != nil {
67+
panic(err)
68+
}
6569
}

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

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

2929
// versionCmd represents the version command.
3030
var versionCmd = &cobra.Command{

docs/usage.md

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ $ ethdo account create --account="Personal wallet/Operations" --wallet-passphras
183183
- `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
184184
- `show-withdrawal-credentials`: show the withdrawal credentials of the derived account
185185
- `generate-keystore`: generate a keystore for the account
186+
- `json`: output the generated keystore to stdout
186187

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

0 commit comments

Comments
 (0)