utils.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/json"
  19. "fmt"
  20. "io/ioutil"
  21. "strings"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/console"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "gopkg.in/urfave/cli.v1"
  26. )
  27. // promptPassphrase prompts the user for a passphrase. Set confirmation to true
  28. // to require the user to confirm the passphrase.
  29. func promptPassphrase(confirmation bool) string {
  30. passphrase, err := console.Stdin.PromptPassword("Password: ")
  31. if err != nil {
  32. utils.Fatalf("Failed to read password: %v", err)
  33. }
  34. if confirmation {
  35. confirm, err := console.Stdin.PromptPassword("Repeat password: ")
  36. if err != nil {
  37. utils.Fatalf("Failed to read password confirmation: %v", err)
  38. }
  39. if passphrase != confirm {
  40. utils.Fatalf("Passwords do not match")
  41. }
  42. }
  43. return passphrase
  44. }
  45. // getPassphrase obtains a passphrase given by the user. It first checks the
  46. // --passfile command line flag and ultimately prompts the user for a
  47. // passphrase.
  48. func getPassphrase(ctx *cli.Context) string {
  49. // Look for the --passwordfile flag.
  50. passphraseFile := ctx.String(passphraseFlag.Name)
  51. if passphraseFile != "" {
  52. content, err := ioutil.ReadFile(passphraseFile)
  53. if err != nil {
  54. utils.Fatalf("Failed to read password file '%s': %v",
  55. passphraseFile, err)
  56. }
  57. return strings.TrimRight(string(content), "\r\n")
  58. }
  59. // Otherwise prompt the user for the passphrase.
  60. return promptPassphrase(false)
  61. }
  62. // signHash is a helper function that calculates a hash for the given message
  63. // that can be safely used to calculate a signature from.
  64. //
  65. // The hash is calulcated as
  66. // keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
  67. //
  68. // This gives context to the signed message and prevents signing of transactions.
  69. func signHash(data []byte) []byte {
  70. msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
  71. return crypto.Keccak256([]byte(msg))
  72. }
  73. // mustPrintJSON prints the JSON encoding of the given object and
  74. // exits the program with an error message when the marshaling fails.
  75. func mustPrintJSON(jsonObject interface{}) {
  76. str, err := json.MarshalIndent(jsonObject, "", " ")
  77. if err != nil {
  78. utils.Fatalf("Failed to marshal JSON object: %v", err)
  79. }
  80. fmt.Println(string(str))
  81. }