inspect.go 1.8 KB

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