utils.go 2.7 KB

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