github.com/clem109/go-ethereum@v1.8.3-0.20180316121352-fe6cf00f480a/cmd/ethkey/utils.go (about)

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