github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/signer/core/cliui.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package core
    26  
    27  import (
    28  	"bufio"
    29  	"fmt"
    30  	"os"
    31  	"strings"
    32  
    33  	"sync"
    34  
    35  	"github.com/davecgh/go-spew/spew"
    36  	"github.com/ethereum/go-ethereum/common"
    37  	"github.com/ethereum/go-ethereum/internal/ethapi"
    38  	"github.com/ethereum/go-ethereum/log"
    39  	"golang.org/x/crypto/ssh/terminal"
    40  )
    41  
    42  type CommandlineUI struct {
    43  	in *bufio.Reader
    44  	mu sync.Mutex
    45  }
    46  
    47  func NewCommandlineUI() *CommandlineUI {
    48  	return &CommandlineUI{in: bufio.NewReader(os.Stdin)}
    49  }
    50  
    51  //readstring从stdin读取一行,从空格中剪裁if,强制
    52  //非空性。
    53  func (ui *CommandlineUI) readString() string {
    54  	for {
    55  		fmt.Printf("> ")
    56  		text, err := ui.in.ReadString('\n')
    57  		if err != nil {
    58  			log.Crit("Failed to read user input", "err", err)
    59  		}
    60  		if text = strings.TrimSpace(text); text != "" {
    61  			return text
    62  		}
    63  	}
    64  }
    65  
    66  //readpassword从stdin读取一行,从尾随的new
    67  //行并返回它。输入将不会被回送。
    68  func (ui *CommandlineUI) readPassword() string {
    69  	fmt.Printf("Enter password to approve:\n")
    70  	fmt.Printf("> ")
    71  
    72  	text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
    73  	if err != nil {
    74  		log.Crit("Failed to read password", "err", err)
    75  	}
    76  	fmt.Println()
    77  	fmt.Println("-----------------------")
    78  	return string(text)
    79  }
    80  
    81  //readpassword从stdin读取一行,从尾随的new
    82  //行并返回它。输入将不会被回送。
    83  func (ui *CommandlineUI) readPasswordText(inputstring string) string {
    84  	fmt.Printf("Enter %s:\n", inputstring)
    85  	fmt.Printf("> ")
    86  	text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
    87  	if err != nil {
    88  		log.Crit("Failed to read password", "err", err)
    89  	}
    90  	fmt.Println("-----------------------")
    91  	return string(text)
    92  }
    93  
    94  //如果用户输入“是”,则confirm返回true,否则返回false
    95  func (ui *CommandlineUI) confirm() bool {
    96  	fmt.Printf("Approve? [y/N]:\n")
    97  	if ui.readString() == "y" {
    98  		return true
    99  	}
   100  	fmt.Println("-----------------------")
   101  	return false
   102  }
   103  
   104  func showMetadata(metadata Metadata) {
   105  	fmt.Printf("Request context:\n\t%v -> %v -> %v\n", metadata.Remote, metadata.Scheme, metadata.Local)
   106  }
   107  
   108  //approvetx提示用户确认请求签署交易
   109  func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
   110  	ui.mu.Lock()
   111  	defer ui.mu.Unlock()
   112  	weival := request.Transaction.Value.ToInt()
   113  	fmt.Printf("--------- Transaction request-------------\n")
   114  	if to := request.Transaction.To; to != nil {
   115  		fmt.Printf("to:    %v\n", to.Original())
   116  		if !to.ValidChecksum() {
   117  			fmt.Printf("\nWARNING: Invalid checksum on to-address!\n\n")
   118  		}
   119  	} else {
   120  		fmt.Printf("to:    <contact creation>\n")
   121  	}
   122  	fmt.Printf("from:  %v\n", request.Transaction.From.String())
   123  	fmt.Printf("value: %v wei\n", weival)
   124  	if request.Transaction.Data != nil {
   125  		d := *request.Transaction.Data
   126  		if len(d) > 0 {
   127  			fmt.Printf("data:  %v\n", common.Bytes2Hex(d))
   128  		}
   129  	}
   130  	if request.Callinfo != nil {
   131  		fmt.Printf("\nTransaction validation:\n")
   132  		for _, m := range request.Callinfo {
   133  			fmt.Printf("  * %s : %s", m.Typ, m.Message)
   134  		}
   135  		fmt.Println()
   136  
   137  	}
   138  	fmt.Printf("\n")
   139  	showMetadata(request.Meta)
   140  	fmt.Printf("-------------------------------------------\n")
   141  	if !ui.confirm() {
   142  		return SignTxResponse{request.Transaction, false, ""}, nil
   143  	}
   144  	return SignTxResponse{request.Transaction, true, ui.readPassword()}, nil
   145  }
   146  
   147  //ApproveSignData提示用户确认请求签署数据
   148  func (ui *CommandlineUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
   149  	ui.mu.Lock()
   150  	defer ui.mu.Unlock()
   151  
   152  	fmt.Printf("-------- Sign data request--------------\n")
   153  	fmt.Printf("Account:  %s\n", request.Address.String())
   154  	fmt.Printf("message:  \n%q\n", request.Message)
   155  	fmt.Printf("raw data: \n%v\n", request.Rawdata)
   156  	fmt.Printf("message hash:  %v\n", request.Hash)
   157  	fmt.Printf("-------------------------------------------\n")
   158  	showMetadata(request.Meta)
   159  	if !ui.confirm() {
   160  		return SignDataResponse{false, ""}, nil
   161  	}
   162  	return SignDataResponse{true, ui.readPassword()}, nil
   163  }
   164  
   165  //approveexport提示用户确认导出加密帐户json
   166  func (ui *CommandlineUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
   167  	ui.mu.Lock()
   168  	defer ui.mu.Unlock()
   169  
   170  	fmt.Printf("-------- Export Account request--------------\n")
   171  	fmt.Printf("A request has been made to export the (encrypted) keyfile\n")
   172  	fmt.Printf("Approving this operation means that the caller obtains the (encrypted) contents\n")
   173  	fmt.Printf("\n")
   174  	fmt.Printf("Account:  %x\n", request.Address)
   175  //fmt.printf(“keyfile:\n%v\n”,request.file)
   176  	fmt.Printf("-------------------------------------------\n")
   177  	showMetadata(request.Meta)
   178  	return ExportResponse{ui.confirm()}, nil
   179  }
   180  
   181  //approveImport提示用户确认导入账号json
   182  func (ui *CommandlineUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
   183  	ui.mu.Lock()
   184  	defer ui.mu.Unlock()
   185  
   186  	fmt.Printf("-------- Import Account request--------------\n")
   187  	fmt.Printf("A request has been made to import an encrypted keyfile\n")
   188  	fmt.Printf("-------------------------------------------\n")
   189  	showMetadata(request.Meta)
   190  	if !ui.confirm() {
   191  		return ImportResponse{false, "", ""}, nil
   192  	}
   193  	return ImportResponse{true, ui.readPasswordText("Old password"), ui.readPasswordText("New password")}, nil
   194  }
   195  
   196  //批准提示用户确认列出帐户
   197  //用户界面可以修改要列出的科目列表
   198  func (ui *CommandlineUI) ApproveListing(request *ListRequest) (ListResponse, error) {
   199  
   200  	ui.mu.Lock()
   201  	defer ui.mu.Unlock()
   202  
   203  	fmt.Printf("-------- List Account request--------------\n")
   204  	fmt.Printf("A request has been made to list all accounts. \n")
   205  	fmt.Printf("You can select which accounts the caller can see\n")
   206  	for _, account := range request.Accounts {
   207  		fmt.Printf("\t[x] %v\n", account.Address.Hex())
   208  	}
   209  	fmt.Printf("-------------------------------------------\n")
   210  	showMetadata(request.Meta)
   211  	if !ui.confirm() {
   212  		return ListResponse{nil}, nil
   213  	}
   214  	return ListResponse{request.Accounts}, nil
   215  }
   216  
   217  //ApproveWaccount提示用户确认创建新帐户,并显示给调用方
   218  func (ui *CommandlineUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
   219  
   220  	ui.mu.Lock()
   221  	defer ui.mu.Unlock()
   222  
   223  	fmt.Printf("-------- New Account request--------------\n")
   224  	fmt.Printf("A request has been made to create a new. \n")
   225  	fmt.Printf("Approving this operation means that a new Account is created,\n")
   226  	fmt.Printf("and the address show to the caller\n")
   227  	showMetadata(request.Meta)
   228  	if !ui.confirm() {
   229  		return NewAccountResponse{false, ""}, nil
   230  	}
   231  	return NewAccountResponse{true, ui.readPassword()}, nil
   232  }
   233  
   234  //ShowError向用户显示错误消息
   235  func (ui *CommandlineUI) ShowError(message string) {
   236  
   237  	fmt.Printf("ERROR: %v\n", message)
   238  }
   239  
   240  //ShowInfo向用户显示信息消息
   241  func (ui *CommandlineUI) ShowInfo(message string) {
   242  	fmt.Printf("Info: %v\n", message)
   243  }
   244  
   245  func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
   246  	fmt.Printf("Transaction signed:\n ")
   247  	spew.Dump(tx.Tx)
   248  }
   249  
   250  func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) {
   251  
   252  	fmt.Printf("------- Signer info -------\n")
   253  	for k, v := range info.Info {
   254  		fmt.Printf("* %v : %v\n", k, v)
   255  	}
   256  }