cliui.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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"
  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. // confirm returns true if user enters 'Yes', otherwise false
  76. func (ui *CommandlineUI) confirm() bool {
  77. fmt.Printf("Approve? [y/N]:\n")
  78. if ui.readString() == "y" {
  79. return true
  80. }
  81. fmt.Println("-----------------------")
  82. return false
  83. }
  84. func showMetadata(metadata Metadata) {
  85. fmt.Printf("Request context:\n\t%v -> %v -> %v\n", metadata.Remote, metadata.Scheme, metadata.Local)
  86. }
  87. // ApproveTx prompt the user for confirmation to request to sign Transaction
  88. func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
  89. ui.mu.Lock()
  90. defer ui.mu.Unlock()
  91. weival := request.Transaction.Value.ToInt()
  92. fmt.Printf("--------- Transaction request-------------\n")
  93. if to := request.Transaction.To; to != nil {
  94. fmt.Printf("to: %v\n", to.Original())
  95. if !to.ValidChecksum() {
  96. fmt.Printf("\nWARNING: Invalid checksum on to-address!\n\n")
  97. }
  98. } else {
  99. fmt.Printf("to: <contact creation>\n")
  100. }
  101. fmt.Printf("from: %v\n", request.Transaction.From.String())
  102. fmt.Printf("value: %v wei\n", weival)
  103. if request.Transaction.Data != nil {
  104. d := *request.Transaction.Data
  105. if len(d) > 0 {
  106. fmt.Printf("data: %v\n", common.Bytes2Hex(d))
  107. }
  108. }
  109. if request.Callinfo != nil {
  110. fmt.Printf("\nTransaction validation:\n")
  111. for _, m := range request.Callinfo {
  112. fmt.Printf(" * %s : %s", m.Typ, m.Message)
  113. }
  114. fmt.Println()
  115. }
  116. fmt.Printf("\n")
  117. showMetadata(request.Meta)
  118. fmt.Printf("-------------------------------------------\n")
  119. if !ui.confirm() {
  120. return SignTxResponse{request.Transaction, false, ""}, nil
  121. }
  122. return SignTxResponse{request.Transaction, true, ui.readPassword()}, nil
  123. }
  124. // ApproveSignData prompt the user for confirmation to request to sign data
  125. func (ui *CommandlineUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
  126. ui.mu.Lock()
  127. defer ui.mu.Unlock()
  128. fmt.Printf("-------- Sign data request--------------\n")
  129. fmt.Printf("Account: %s\n", request.Address.String())
  130. fmt.Printf("message: \n%q\n", request.Message)
  131. fmt.Printf("raw data: \n%v\n", request.Rawdata)
  132. fmt.Printf("message hash: %v\n", request.Hash)
  133. fmt.Printf("-------------------------------------------\n")
  134. showMetadata(request.Meta)
  135. if !ui.confirm() {
  136. return SignDataResponse{false, ""}, nil
  137. }
  138. return SignDataResponse{true, ui.readPassword()}, nil
  139. }
  140. // ApproveExport prompt the user for confirmation to export encrypted Account json
  141. func (ui *CommandlineUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
  142. ui.mu.Lock()
  143. defer ui.mu.Unlock()
  144. fmt.Printf("-------- Export Account request--------------\n")
  145. fmt.Printf("A request has been made to export the (encrypted) keyfile\n")
  146. fmt.Printf("Approving this operation means that the caller obtains the (encrypted) contents\n")
  147. fmt.Printf("\n")
  148. fmt.Printf("Account: %x\n", request.Address)
  149. //fmt.Printf("keyfile: \n%v\n", request.file)
  150. fmt.Printf("-------------------------------------------\n")
  151. showMetadata(request.Meta)
  152. return ExportResponse{ui.confirm()}, nil
  153. }
  154. // ApproveImport prompt the user for confirmation to import Account json
  155. func (ui *CommandlineUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
  156. ui.mu.Lock()
  157. defer ui.mu.Unlock()
  158. fmt.Printf("-------- Import Account request--------------\n")
  159. fmt.Printf("A request has been made to import an encrypted keyfile\n")
  160. fmt.Printf("-------------------------------------------\n")
  161. showMetadata(request.Meta)
  162. if !ui.confirm() {
  163. return ImportResponse{false, "", ""}, nil
  164. }
  165. return ImportResponse{true, ui.readPasswordText("Old password"), ui.readPasswordText("New password")}, nil
  166. }
  167. // ApproveListing prompt the user for confirmation to list accounts
  168. // the list of accounts to list can be modified by the UI
  169. func (ui *CommandlineUI) ApproveListing(request *ListRequest) (ListResponse, error) {
  170. ui.mu.Lock()
  171. defer ui.mu.Unlock()
  172. fmt.Printf("-------- List Account request--------------\n")
  173. fmt.Printf("A request has been made to list all accounts. \n")
  174. fmt.Printf("You can select which accounts the caller can see\n")
  175. for _, account := range request.Accounts {
  176. fmt.Printf("\t[x] %v\n", account.Address.Hex())
  177. }
  178. fmt.Printf("-------------------------------------------\n")
  179. showMetadata(request.Meta)
  180. if !ui.confirm() {
  181. return ListResponse{nil}, nil
  182. }
  183. return ListResponse{request.Accounts}, nil
  184. }
  185. // ApproveNewAccount prompt the user for confirmation to create new Account, and reveal to caller
  186. func (ui *CommandlineUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
  187. ui.mu.Lock()
  188. defer ui.mu.Unlock()
  189. fmt.Printf("-------- New Account request--------------\n")
  190. fmt.Printf("A request has been made to create a new. \n")
  191. fmt.Printf("Approving this operation means that a new Account is created,\n")
  192. fmt.Printf("and the address show to the caller\n")
  193. showMetadata(request.Meta)
  194. if !ui.confirm() {
  195. return NewAccountResponse{false, ""}, nil
  196. }
  197. return NewAccountResponse{true, ui.readPassword()}, nil
  198. }
  199. // ShowError displays error message to user
  200. func (ui *CommandlineUI) ShowError(message string) {
  201. fmt.Printf("ERROR: %v\n", message)
  202. }
  203. // ShowInfo displays info message to user
  204. func (ui *CommandlineUI) ShowInfo(message string) {
  205. fmt.Printf("Info: %v\n", message)
  206. }
  207. func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
  208. fmt.Printf("Transaction signed:\n ")
  209. spew.Dump(tx.Tx)
  210. }
  211. func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) {
  212. fmt.Printf("------- Signer info -------\n")
  213. for k, v := range info.Info {
  214. fmt.Printf("* %v : %v\n", k, v)
  215. }
  216. }