github.com/felberj/go-ethereum@v1.8.23/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/ethereum/go-ethereum/common/hexutil" 29 "github.com/ethereum/go-ethereum/internal/ethapi" 30 "github.com/ethereum/go-ethereum/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 func (ui *CommandlineUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) { 87 fmt.Println(info.Title) 88 fmt.Println(info.Prompt) 89 if info.IsPassword { 90 text, err := terminal.ReadPassword(int(os.Stdin.Fd())) 91 if err != nil { 92 log.Error("Failed to read password", "err", err) 93 } 94 fmt.Println("-----------------------") 95 return UserInputResponse{string(text)}, err 96 } 97 text := ui.readString() 98 fmt.Println("-----------------------") 99 return UserInputResponse{text}, nil 100 } 101 102 // confirm returns true if user enters 'Yes', otherwise false 103 func (ui *CommandlineUI) confirm() bool { 104 fmt.Printf("Approve? [y/N]:\n") 105 if ui.readString() == "y" { 106 return true 107 } 108 fmt.Println("-----------------------") 109 return false 110 } 111 112 func showMetadata(metadata Metadata) { 113 fmt.Printf("Request context:\n\t%v -> %v -> %v\n", metadata.Remote, metadata.Scheme, metadata.Local) 114 fmt.Printf("\nAdditional HTTP header data, provided by the external caller:\n") 115 fmt.Printf("\tUser-Agent: %v\n\tOrigin: %v\n", metadata.UserAgent, metadata.Origin) 116 } 117 118 // ApproveTx prompt the user for confirmation to request to sign Transaction 119 func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) { 120 ui.mu.Lock() 121 defer ui.mu.Unlock() 122 weival := request.Transaction.Value.ToInt() 123 fmt.Printf("--------- Transaction request-------------\n") 124 if to := request.Transaction.To; to != nil { 125 fmt.Printf("to: %v\n", to.Original()) 126 if !to.ValidChecksum() { 127 fmt.Printf("\nWARNING: Invalid checksum on to-address!\n\n") 128 } 129 } else { 130 fmt.Printf("to: <contact creation>\n") 131 } 132 fmt.Printf("from: %v\n", request.Transaction.From.String()) 133 fmt.Printf("value: %v wei\n", weival) 134 fmt.Printf("gas: %v (%v)\n", request.Transaction.Gas, uint64(request.Transaction.Gas)) 135 fmt.Printf("gasprice: %v wei\n", request.Transaction.GasPrice.ToInt()) 136 fmt.Printf("nonce: %v (%v)\n", request.Transaction.Nonce, uint64(request.Transaction.Nonce)) 137 if request.Transaction.Data != nil { 138 d := *request.Transaction.Data 139 if len(d) > 0 { 140 141 fmt.Printf("data: %v\n", hexutil.Encode(d)) 142 } 143 } 144 if request.Callinfo != nil { 145 fmt.Printf("\nTransaction validation:\n") 146 for _, m := range request.Callinfo { 147 fmt.Printf(" * %s : %s\n", m.Typ, m.Message) 148 } 149 fmt.Println() 150 151 } 152 fmt.Printf("\n") 153 showMetadata(request.Meta) 154 fmt.Printf("-------------------------------------------\n") 155 if !ui.confirm() { 156 return SignTxResponse{request.Transaction, false, ""}, nil 157 } 158 return SignTxResponse{request.Transaction, true, ui.readPassword()}, nil 159 } 160 161 // ApproveSignData prompt the user for confirmation to request to sign data 162 func (ui *CommandlineUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) { 163 ui.mu.Lock() 164 defer ui.mu.Unlock() 165 166 fmt.Printf("-------- Sign data request--------------\n") 167 fmt.Printf("Account: %s\n", request.Address.String()) 168 fmt.Printf("message: \n%q\n", request.Message) 169 fmt.Printf("raw data: \n%v\n", request.Rawdata) 170 fmt.Printf("message hash: %v\n", request.Hash) 171 fmt.Printf("-------------------------------------------\n") 172 showMetadata(request.Meta) 173 if !ui.confirm() { 174 return SignDataResponse{false, ""}, nil 175 } 176 return SignDataResponse{true, ui.readPassword()}, nil 177 } 178 179 // ApproveExport prompt the user for confirmation to export encrypted Account json 180 func (ui *CommandlineUI) ApproveExport(request *ExportRequest) (ExportResponse, error) { 181 ui.mu.Lock() 182 defer ui.mu.Unlock() 183 184 fmt.Printf("-------- Export Account request--------------\n") 185 fmt.Printf("A request has been made to export the (encrypted) keyfile\n") 186 fmt.Printf("Approving this operation means that the caller obtains the (encrypted) contents\n") 187 fmt.Printf("\n") 188 fmt.Printf("Account: %x\n", request.Address) 189 //fmt.Printf("keyfile: \n%v\n", request.file) 190 fmt.Printf("-------------------------------------------\n") 191 showMetadata(request.Meta) 192 return ExportResponse{ui.confirm()}, nil 193 } 194 195 // ApproveImport prompt the user for confirmation to import Account json 196 func (ui *CommandlineUI) ApproveImport(request *ImportRequest) (ImportResponse, error) { 197 ui.mu.Lock() 198 defer ui.mu.Unlock() 199 200 fmt.Printf("-------- Import Account request--------------\n") 201 fmt.Printf("A request has been made to import an encrypted keyfile\n") 202 fmt.Printf("-------------------------------------------\n") 203 showMetadata(request.Meta) 204 if !ui.confirm() { 205 return ImportResponse{false, "", ""}, nil 206 } 207 return ImportResponse{true, ui.readPasswordText("Old password"), ui.readPasswordText("New password")}, nil 208 } 209 210 // ApproveListing prompt the user for confirmation to list accounts 211 // the list of accounts to list can be modified by the UI 212 func (ui *CommandlineUI) ApproveListing(request *ListRequest) (ListResponse, error) { 213 214 ui.mu.Lock() 215 defer ui.mu.Unlock() 216 217 fmt.Printf("-------- List Account request--------------\n") 218 fmt.Printf("A request has been made to list all accounts. \n") 219 fmt.Printf("You can select which accounts the caller can see\n") 220 for _, account := range request.Accounts { 221 fmt.Printf(" [x] %v\n", account.Address.Hex()) 222 fmt.Printf(" URL: %v\n", account.URL) 223 fmt.Printf(" Type: %v\n", account.Typ) 224 } 225 fmt.Printf("-------------------------------------------\n") 226 showMetadata(request.Meta) 227 if !ui.confirm() { 228 return ListResponse{nil}, nil 229 } 230 return ListResponse{request.Accounts}, nil 231 } 232 233 // ApproveNewAccount prompt the user for confirmation to create new Account, and reveal to caller 234 func (ui *CommandlineUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) { 235 236 ui.mu.Lock() 237 defer ui.mu.Unlock() 238 239 fmt.Printf("-------- New Account request--------------\n\n") 240 fmt.Printf("A request has been made to create a new account. \n") 241 fmt.Printf("Approving this operation means that a new account is created,\n") 242 fmt.Printf("and the address is returned to the external caller\n\n") 243 showMetadata(request.Meta) 244 if !ui.confirm() { 245 return NewAccountResponse{false, ""}, nil 246 } 247 return NewAccountResponse{true, ui.readPassword()}, nil 248 } 249 250 // ShowError displays error message to user 251 func (ui *CommandlineUI) ShowError(message string) { 252 fmt.Printf("-------- Error message from Clef-----------\n") 253 fmt.Println(message) 254 fmt.Printf("-------------------------------------------\n") 255 } 256 257 // ShowInfo displays info message to user 258 func (ui *CommandlineUI) ShowInfo(message string) { 259 fmt.Printf("Info: %v\n", message) 260 } 261 262 func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) { 263 fmt.Printf("Transaction signed:\n ") 264 spew.Dump(tx.Tx) 265 } 266 267 func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) { 268 269 fmt.Printf("------- Signer info -------\n") 270 for k, v := range info.Info { 271 fmt.Printf("* %v : %v\n", k, v) 272 } 273 }