github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/cmd/ethkey/utils.go (about)

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