github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/signer/core/cliui.go (about)

     1  // Copyright 2018 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 core
    18  
    19  import (
    20  	"bufio"
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  
    25  	"sync"
    26  
    27  	"github.com/davecgh/go-spew/spew"
    28  	"github.com/vntchain/go-vnt/common"
    29  	"github.com/vntchain/go-vnt/internal/vntapi"
    30  	"github.com/vntchain/go-vnt/log"
    31  	"golang.org/x/crypto/ssh/terminal"
    32  )
    33  
    34  type CommandlineUI struct {
    35  	in *bufio.Reader
    36  	mu sync.Mutex
    37  }
    38  
    39  func NewCommandlineUI() *CommandlineUI {
    40  	return &CommandlineUI{in: bufio.NewReader(os.Stdin)}
    41  }
    42  
    43  // readString reads a single line from stdin, trimming if from spaces, enforcing
    44  // non-emptyness.
    45  func (ui *CommandlineUI) readString() string {
    46  	for {
    47  		fmt.Printf("> ")
    48  		text, err := ui.in.ReadString('\n')
    49  		if err != nil {
    50  			log.Crit("Failed to read user input", "err", err)
    51  		}
    52  		if text = strings.TrimSpace(text); text != "" {
    53  			return text
    54  		}
    55  	}
    56  }
    57  
    58  // readPassword reads a single line from stdin, trimming it from the trailing new
    59  // line and returns it. The input will not be echoed.
    60  func (ui *CommandlineUI) readPassword() string {
    61  	fmt.Printf("Enter password to approve:\n")
    62  	fmt.Printf("> ")
    63  
    64  	text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
    65  	if err != nil {
    66  		log.Crit("Failed to read password", "err", err)
    67  	}
    68  	fmt.Println()
    69  	fmt.Println("-----------------------")
    70  	return string(text)
    71  }
    72  
    73  // readPassword reads a single line from stdin, trimming it from the trailing new
    74  // line and returns it. The input will not be echoed.
    75  func (ui *CommandlineUI) readPasswordText(inputstring string) string {
    76  	fmt.Printf("Enter %s:\n", inputstring)
    77  	fmt.Printf("> ")
    78  	text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
    79  	if err != nil {
    80  		log.Crit("Failed to read password", "err", err)
    81  	}
    82  	fmt.Println("-----------------------")
    83  	return string(text)
    84  }
    85  
    86  // confirm returns true if user enters 'Yes', otherwise false
    87  func (ui *CommandlineUI) confirm() bool {
    88  	fmt.Printf("Approve? [y/N]:\n")
    89  	if ui.readString() == "y" {
    90  		return true
    91  	}
    92  	fmt.Println("-----------------------")
    93  	return false
    94  }
    95  
    96  func showMetadata(metadata Metadata) {
    97  	fmt.Printf("Request context:\n\t%v -> %v -> %v\n", metadata.Remote, metadata.Scheme, metadata.Local)
    98  }
    99  
   100  // ApproveTx prompt the user for confirmation to request to sign Transaction
   101  func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
   102  	ui.mu.Lock()
   103  	defer ui.mu.Unlock()
   104  	weival := request.Transaction.Value.ToInt()
   105  	fmt.Printf("--------- Transaction request-------------\n")
   106  	if to := request.Transaction.To; to != nil {
   107  		fmt.Printf("to:    %v\n", to.Original())
   108  		if !to.ValidChecksum() {
   109  			fmt.Printf("\nWARNING: Invalid checksum on to-address!\n\n")
   110  		}
   111  	} else {
   112  		fmt.Printf("to:    <contact creation>\n")
   113  	}
   114  	fmt.Printf("from:  %v\n", request.Transaction.From.String())
   115  	fmt.Printf("value: %v wei\n", weival)
   116  	if request.Transaction.Data != nil {
   117  		d := *request.Transaction.Data
   118  		if len(d) > 0 {
   119  			fmt.Printf("data:  %v\n", common.Bytes2Hex(d))
   120  		}
   121  	}
   122  	if request.Callinfo != nil {
   123  		fmt.Printf("\nTransaction validation:\n")
   124  		for _, m := range request.Callinfo {
   125  			fmt.Printf("  * %s : %s", m.Typ, m.Message)
   126  		}
   127  		fmt.Println()
   128  
   129  	}
   130  	fmt.Printf("\n")
   131  	showMetadata(request.Meta)
   132  	fmt.Printf("-------------------------------------------\n")
   133  	if !ui.confirm() {
   134  		return SignTxResponse{request.Transaction, false, ""}, nil
   135  	}
   136  	return SignTxResponse{request.Transaction, true, ui.readPassword()}, nil
   137  }
   138  
   139  // ApproveSignData prompt the user for confirmation to request to sign data
   140  func (ui *CommandlineUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
   141  	ui.mu.Lock()
   142  	defer ui.mu.Unlock()
   143  
   144  	fmt.Printf("-------- Sign data request--------------\n")
   145  	fmt.Printf("Account:  %s\n", request.Address.String())
   146  	fmt.Printf("message:  \n%q\n", request.Message)
   147  	fmt.Printf("raw data: \n%v\n", request.Rawdata)
   148  	fmt.Printf("message hash:  %v\n", request.Hash)
   149  	fmt.Printf("-------------------------------------------\n")
   150  	showMetadata(request.Meta)
   151  	if !ui.confirm() {
   152  		return SignDataResponse{false, ""}, nil
   153  	}
   154  	return SignDataResponse{true, ui.readPassword()}, nil
   155  }
   156  
   157  // ApproveExport prompt the user for confirmation to export encrypted Account json
   158  func (ui *CommandlineUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
   159  	ui.mu.Lock()
   160  	defer ui.mu.Unlock()
   161  
   162  	fmt.Printf("-------- Export Account request--------------\n")
   163  	fmt.Printf("A request has been made to export the (encrypted) keyfile\n")
   164  	fmt.Printf("Approving this operation means that the caller obtains the (encrypted) contents\n")
   165  	fmt.Printf("\n")
   166  	fmt.Printf("Account:  %x\n", request.Address)
   167  	//fmt.Printf("keyfile:  \n%v\n", request.file)
   168  	fmt.Printf("-------------------------------------------\n")
   169  	showMetadata(request.Meta)
   170  	return ExportResponse{ui.confirm()}, nil
   171  }
   172  
   173  // ApproveImport prompt the user for confirmation to import Account json
   174  func (ui *CommandlineUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
   175  	ui.mu.Lock()
   176  	defer ui.mu.Unlock()
   177  
   178  	fmt.Printf("-------- Import Account request--------------\n")
   179  	fmt.Printf("A request has been made to import an encrypted keyfile\n")
   180  	fmt.Printf("-------------------------------------------\n")
   181  	showMetadata(request.Meta)
   182  	if !ui.confirm() {
   183  		return ImportResponse{false, "", ""}, nil
   184  	}
   185  	return ImportResponse{true, ui.readPasswordText("Old password"), ui.readPasswordText("New password")}, nil
   186  }
   187  
   188  // ApproveListing prompt the user for confirmation to list accounts
   189  // the list of accounts to list can be modified by the UI
   190  func (ui *CommandlineUI) ApproveListing(request *ListRequest) (ListResponse, error) {
   191  
   192  	ui.mu.Lock()
   193  	defer ui.mu.Unlock()
   194  
   195  	fmt.Printf("-------- List Account request--------------\n")
   196  	fmt.Printf("A request has been made to list all accounts. \n")
   197  	fmt.Printf("You can select which accounts the caller can see\n")
   198  	for _, account := range request.Accounts {
   199  		fmt.Printf("\t[x] %v\n", account.Address.Hex())
   200  	}
   201  	fmt.Printf("-------------------------------------------\n")
   202  	showMetadata(request.Meta)
   203  	if !ui.confirm() {
   204  		return ListResponse{nil}, nil
   205  	}
   206  	return ListResponse{request.Accounts}, nil
   207  }
   208  
   209  // ApproveNewAccount prompt the user for confirmation to create new Account, and reveal to caller
   210  func (ui *CommandlineUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
   211  
   212  	ui.mu.Lock()
   213  	defer ui.mu.Unlock()
   214  
   215  	fmt.Printf("-------- New Account request--------------\n")
   216  	fmt.Printf("A request has been made to create a new. \n")
   217  	fmt.Printf("Approving this operation means that a new Account is created,\n")
   218  	fmt.Printf("and the address show to the caller\n")
   219  	showMetadata(request.Meta)
   220  	if !ui.confirm() {
   221  		return NewAccountResponse{false, ""}, nil
   222  	}
   223  	return NewAccountResponse{true, ui.readPassword()}, nil
   224  }
   225  
   226  // ShowError displays error message to user
   227  func (ui *CommandlineUI) ShowError(message string) {
   228  
   229  	fmt.Printf("ERROR: %v\n", message)
   230  }
   231  
   232  // ShowInfo displays info message to user
   233  func (ui *CommandlineUI) ShowInfo(message string) {
   234  	fmt.Printf("Info: %v\n", message)
   235  }
   236  
   237  func (ui *CommandlineUI) OnApprovedTx(tx vntapi.SignTransactionResult) {
   238  	fmt.Printf("Transaction signed:\n ")
   239  	spew.Dump(tx.Tx)
   240  }
   241  
   242  func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) {
   243  
   244  	fmt.Printf("------- Signer info -------\n")
   245  	for k, v := range info.Info {
   246  		fmt.Printf("* %v : %v\n", k, v)
   247  	}
   248  }