inspect.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "io/ioutil"
  21. "github.com/ethereum/go-ethereum/accounts/keystore"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "gopkg.in/urfave/cli.v1"
  25. )
  26. type outputInspect struct {
  27. Address string
  28. PublicKey string
  29. PrivateKey string
  30. }
  31. var commandInspect = cli.Command{
  32. Name: "inspect",
  33. Usage: "inspect a keyfile",
  34. ArgsUsage: "<keyfile>",
  35. Description: `
  36. Print various information about the keyfile.
  37. Private key information can be printed by using the --private flag;
  38. make sure to use this feature with great caution!`,
  39. Flags: []cli.Flag{
  40. passphraseFlag,
  41. jsonFlag,
  42. cli.BoolFlag{
  43. Name: "private",
  44. Usage: "include the private key in the output",
  45. },
  46. },
  47. Action: func(ctx *cli.Context) error {
  48. keyfilepath := ctx.Args().First()
  49. // Read key from file.
  50. keyjson, err := ioutil.ReadFile(keyfilepath)
  51. if err != nil {
  52. utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
  53. }
  54. // Decrypt key with passphrase.
  55. passphrase := getPassPhrase(ctx, false)
  56. key, err := keystore.DecryptKey(keyjson, passphrase)
  57. if err != nil {
  58. utils.Fatalf("Error decrypting key: %v", err)
  59. }
  60. // Output all relevant information we can retrieve.
  61. showPrivate := ctx.Bool("private")
  62. out := outputInspect{
  63. Address: key.Address.Hex(),
  64. PublicKey: hex.EncodeToString(
  65. crypto.FromECDSAPub(&key.PrivateKey.PublicKey)),
  66. }
  67. if showPrivate {
  68. out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey))
  69. }
  70. if ctx.Bool(jsonFlag.Name) {
  71. mustPrintJSON(out)
  72. } else {
  73. fmt.Println("Address: ", out.Address)
  74. fmt.Println("Public key: ", out.PublicKey)
  75. if showPrivate {
  76. fmt.Println("Private key: ", out.PrivateKey)
  77. }
  78. }
  79. return nil
  80. },
  81. }