github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/signer/core/cliui.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package core 19 20 import ( 21 "bufio" 22 "encoding/json" 23 "fmt" 24 "os" 25 "strings" 26 "sync" 27 28 "github.com/AigarNetwork/aigar/common/hexutil" 29 "github.com/AigarNetwork/aigar/internal/ethapi" 30 "github.com/AigarNetwork/aigar/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 func (ui *CommandlineUI) RegisterUIServer(api *UIServerAPI) { 44 // noop 45 } 46 47 // readString reads a single line from stdin, trimming if from spaces, enforcing 48 // non-emptyness. 49 func (ui *CommandlineUI) readString() string { 50 for { 51 fmt.Printf("> ") 52 text, err := ui.in.ReadString('\n') 53 if err != nil { 54 log.Crit("Failed to read user input", "err", err) 55 } 56 if text = strings.TrimSpace(text); text != "" { 57 return text 58 } 59 } 60 } 61 62 // readPassword reads a single line from stdin, trimming it from the trailing new 63 // line and returns it. The input will not be echoed. 64 func (ui *CommandlineUI) readPassword() string { 65 fmt.Printf("Enter password to approve:\n") 66 fmt.Printf("> ") 67 68 text, err := terminal.ReadPassword(int(os.Stdin.Fd())) 69 if err != nil { 70 log.Crit("Failed to read password", "err", err) 71 } 72 fmt.Println() 73 fmt.Println("-----------------------") 74 return string(text) 75 } 76 77 // readPassword reads a single line from stdin, trimming it from the trailing new 78 // line and returns it. The input will not be echoed. 79 func (ui *CommandlineUI) readPasswordText(inputstring string) string { 80 fmt.Printf("Enter %s:\n", inputstring) 81 fmt.Printf("> ") 82 text, err := terminal.ReadPassword(int(os.Stdin.Fd())) 83 if err != nil { 84 log.Crit("Failed to read password", "err", err) 85 } 86 fmt.Println("-----------------------") 87 return string(text) 88 } 89 90 func (ui *CommandlineUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) { 91 92 fmt.Printf("## %s\n\n%s\n", info.Title, info.Prompt) 93 if info.IsPassword { 94 fmt.Printf("> ") 95 text, err := terminal.ReadPassword(int(os.Stdin.Fd())) 96 if err != nil { 97 log.Error("Failed to read password", "err", err) 98 } 99 fmt.Println("-----------------------") 100 return UserInputResponse{string(text)}, err 101 } 102 text := ui.readString() 103 fmt.Println("-----------------------") 104 return UserInputResponse{text}, nil 105 } 106 107 // confirm returns true if user enters 'Yes', otherwise false 108 func (ui *CommandlineUI) confirm() bool { 109 fmt.Printf("Approve? [y/N]:\n") 110 if ui.readString() == "y" { 111 return true 112 } 113 fmt.Println("-----------------------") 114 return false 115 } 116 117 func showMetadata(metadata Metadata) { 118 fmt.Printf("Request context:\n\t%v -> %v -> %v\n", metadata.Remote, metadata.Scheme, metadata.Local) 119 fmt.Printf("\nAdditional HTTP header data, provided by the external caller:\n") 120 fmt.Printf("\tUser-Agent: %v\n\tOrigin: %v\n", metadata.UserAgent, metadata.Origin) 121 } 122 123 // ApproveTx prompt the user for confirmation to request to sign Transaction 124 func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) { 125 ui.mu.Lock() 126 defer ui.mu.Unlock() 127 weival := request.Transaction.Value.ToInt() 128 fmt.Printf("--------- Transaction request-------------\n") 129 if to := request.Transaction.To; to != nil { 130 fmt.Printf("to: %v\n", to.Original()) 131 if !to.ValidChecksum() { 132 fmt.Printf("\nWARNING: Invalid checksum on to-address!\n\n") 133 } 134 } else { 135 fmt.Printf("to: <contact creation>\n") 136 } 137 fmt.Printf("from: %v\n", request.Transaction.From.String()) 138 fmt.Printf("value: %v wei\n", weival) 139 fmt.Printf("gas: %v (%v)\n", request.Transaction.Gas, uint64(request.Transaction.Gas)) 140 fmt.Printf("gasprice: %v wei\n", request.Transaction.GasPrice.ToInt()) 141 fmt.Printf("nonce: %v (%v)\n", request.Transaction.Nonce, uint64(request.Transaction.Nonce)) 142 if request.Transaction.Data != nil { 143 d := *request.Transaction.Data 144 if len(d) > 0 { 145 146 fmt.Printf("data: %v\n", hexutil.Encode(d)) 147 } 148 } 149 if request.Callinfo != nil { 150 fmt.Printf("\nTransaction validation:\n") 151 for _, m := range request.Callinfo { 152 fmt.Printf(" * %s : %s\n", m.Typ, m.Message) 153 } 154 fmt.Println() 155 156 } 157 fmt.Printf("\n") 158 showMetadata(request.Meta) 159 fmt.Printf("-------------------------------------------\n") 160 if !ui.confirm() { 161 return SignTxResponse{request.Transaction, false}, nil 162 } 163 return SignTxResponse{request.Transaction, true}, nil 164 } 165 166 // ApproveSignData prompt the user for confirmation to request to sign data 167 func (ui *CommandlineUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) { 168 ui.mu.Lock() 169 defer ui.mu.Unlock() 170 171 fmt.Printf("-------- Sign data request--------------\n") 172 fmt.Printf("Account: %s\n", request.Address.String()) 173 fmt.Printf("messages:\n") 174 for _, nvt := range request.Messages { 175 fmt.Printf("\u00a0\u00a0%v\n", strings.TrimSpace(nvt.Pprint(1))) 176 } 177 fmt.Printf("raw data: \n%q\n", request.Rawdata) 178 fmt.Printf("data hash: %v\n", request.Hash) 179 fmt.Printf("-------------------------------------------\n") 180 showMetadata(request.Meta) 181 if !ui.confirm() { 182 return SignDataResponse{false}, nil 183 } 184 return SignDataResponse{true}, nil 185 } 186 187 // ApproveListing prompt the user for confirmation to list accounts 188 // the list of accounts to list can be modified by the UI 189 func (ui *CommandlineUI) ApproveListing(request *ListRequest) (ListResponse, error) { 190 ui.mu.Lock() 191 defer ui.mu.Unlock() 192 193 fmt.Printf("-------- List Account request--------------\n") 194 fmt.Printf("A request has been made to list all accounts. \n") 195 fmt.Printf("You can select which accounts the caller can see\n") 196 for _, account := range request.Accounts { 197 fmt.Printf(" [x] %v\n", account.Address.Hex()) 198 fmt.Printf(" URL: %v\n", account.URL) 199 } 200 fmt.Printf("-------------------------------------------\n") 201 showMetadata(request.Meta) 202 if !ui.confirm() { 203 return ListResponse{nil}, nil 204 } 205 return ListResponse{request.Accounts}, nil 206 } 207 208 // ApproveNewAccount prompt the user for confirmation to create new Account, and reveal to caller 209 func (ui *CommandlineUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) { 210 211 ui.mu.Lock() 212 defer ui.mu.Unlock() 213 214 fmt.Printf("-------- New Account request--------------\n\n") 215 fmt.Printf("A request has been made to create a new account. \n") 216 fmt.Printf("Approving this operation means that a new account is created,\n") 217 fmt.Printf("and the address is returned to the external caller\n\n") 218 showMetadata(request.Meta) 219 if !ui.confirm() { 220 return NewAccountResponse{false}, nil 221 } 222 return NewAccountResponse{true}, nil 223 } 224 225 // ShowError displays error message to user 226 func (ui *CommandlineUI) ShowError(message string) { 227 fmt.Printf("## Error \n%s\n", message) 228 fmt.Printf("-------------------------------------------\n") 229 } 230 231 // ShowInfo displays info message to user 232 func (ui *CommandlineUI) ShowInfo(message string) { 233 fmt.Printf("## Info \n%s\n", message) 234 } 235 236 func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) { 237 fmt.Printf("Transaction signed:\n ") 238 if jsn, err := json.MarshalIndent(tx.Tx, " ", " "); err != nil { 239 fmt.Printf("WARN: marshalling error %v\n", err) 240 } else { 241 fmt.Println(string(jsn)) 242 } 243 } 244 245 func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) { 246 247 fmt.Printf("------- Signer info -------\n") 248 for k, v := range info.Info { 249 fmt.Printf("* %v : %v\n", k, v) 250 } 251 }