cliui.go 9.0 KB

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