inspect.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "encoding/hex"
  19. "fmt"
  20. "os"
  21. "github.com/ethereum/go-ethereum/accounts/keystore"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/urfave/cli/v2"
  25. )
  26. type outputInspect struct {
  27. Address string
  28. PublicKey string
  29. PrivateKey string
  30. }
  31. var (
  32. privateFlag = &cli.BoolFlag{
  33. Name: "private",
  34. Usage: "include the private key in the output",
  35. }
  36. )
  37. var commandInspect = &cli.Command{
  38. Name: "inspect",
  39. Usage: "inspect a keyfile",
  40. ArgsUsage: "<keyfile>",
  41. Description: `
  42. Print various information about the keyfile.
  43. Private key information can be printed by using the --private flag;
  44. make sure to use this feature with great caution!`,
  45. Flags: []cli.Flag{
  46. passphraseFlag,
  47. jsonFlag,
  48. privateFlag,
  49. },
  50. Action: func(ctx *cli.Context) error {
  51. keyfilepath := ctx.Args().First()
  52. // Read key from file.
  53. keyjson, err := os.ReadFile(keyfilepath)
  54. if err != nil {
  55. utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
  56. }
  57. // Decrypt key with passphrase.
  58. passphrase := getPassphrase(ctx, false)
  59. key, err := keystore.DecryptKey(keyjson, passphrase)
  60. if err != nil {
  61. utils.Fatalf("Error decrypting key: %v", err)
  62. }
  63. // Output all relevant information we can retrieve.
  64. showPrivate := ctx.Bool(privateFlag.Name)
  65. out := outputInspect{
  66. Address: key.Address.Hex(),
  67. PublicKey: hex.EncodeToString(
  68. crypto.FromECDSAPub(&key.PrivateKey.PublicKey)),
  69. }
  70. if showPrivate {
  71. out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey))
  72. }
  73. if ctx.Bool(jsonFlag.Name) {
  74. mustPrintJSON(out)
  75. } else {
  76. fmt.Println("Address: ", out.Address)
  77. fmt.Println("Public key: ", out.PublicKey)
  78. if showPrivate {
  79. fmt.Println("Private key: ", out.PrivateKey)
  80. }
  81. }
  82. return nil
  83. },
  84. }