cliui.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. "encoding/json"
  20. "fmt"
  21. "os"
  22. "strings"
  23. "sync"
  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. func (ui *CommandlineUI) RegisterUIServer(api *UIServerAPI) {
  37. // noop
  38. }
  39. // readString reads a single line from stdin, trimming if from spaces, enforcing
  40. // non-emptyness.
  41. func (ui *CommandlineUI) readString() string {
  42. for {
  43. fmt.Printf("> ")
  44. text, err := ui.in.ReadString('\n')
  45. if err != nil {
  46. log.Crit("Failed to read user input", "err", err)
  47. }
  48. if text = strings.TrimSpace(text); text != "" {
  49. return text
  50. }
  51. }
  52. }
  53. // readPassword reads a single line from stdin, trimming it from the trailing new
  54. // line and returns it. The input will not be echoed.
  55. func (ui *CommandlineUI) readPassword() string {
  56. fmt.Printf("Enter password to approve:\n")
  57. fmt.Printf("> ")
  58. text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
  59. if err != nil {
  60. log.Crit("Failed to read password", "err", err)
  61. }
  62. fmt.Println()
  63. fmt.Println("-----------------------")
  64. return string(text)
  65. }
  66. // readPassword reads a single line from stdin, trimming it from the trailing new
  67. // line and returns it. The input will not be echoed.
  68. func (ui *CommandlineUI) readPasswordText(inputstring string) string {
  69. fmt.Printf("Enter %s:\n", inputstring)
  70. fmt.Printf("> ")
  71. text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
  72. if err != nil {
  73. log.Crit("Failed to read password", "err", err)
  74. }
  75. fmt.Println("-----------------------")
  76. return string(text)
  77. }
  78. func (ui *CommandlineUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) {
  79. fmt.Printf("## %s\n\n%s\n", info.Title, info.Prompt)
  80. if info.IsPassword {
  81. fmt.Printf("> ")
  82. text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
  83. if err != nil {
  84. log.Error("Failed to read password", "err", err)
  85. }
  86. fmt.Println("-----------------------")
  87. return UserInputResponse{string(text)}, err
  88. }
  89. text := ui.readString()
  90. fmt.Println("-----------------------")
  91. return UserInputResponse{text}, nil
  92. }
  93. // confirm returns true if user enters 'Yes', otherwise false
  94. func (ui *CommandlineUI) confirm() bool {
  95. fmt.Printf("Approve? [y/N]:\n")
  96. if ui.readString() == "y" {
  97. return true
  98. }
  99. fmt.Println("-----------------------")
  100. return false
  101. }
  102. func showMetadata(metadata Metadata) {
  103. fmt.Printf("Request context:\n\t%v -> %v -> %v\n", metadata.Remote, metadata.Scheme, metadata.Local)
  104. fmt.Printf("\nAdditional HTTP header data, provided by the external caller:\n")
  105. fmt.Printf("\tUser-Agent: %v\n\tOrigin: %v\n", metadata.UserAgent, metadata.Origin)
  106. }
  107. // ApproveTx prompt the user for confirmation to request to sign Transaction
  108. func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
  109. ui.mu.Lock()
  110. defer ui.mu.Unlock()
  111. weival := request.Transaction.Value.ToInt()
  112. fmt.Printf("--------- Transaction request-------------\n")
  113. if to := request.Transaction.To; to != nil {
  114. fmt.Printf("to: %v\n", to.Original())
  115. if !to.ValidChecksum() {
  116. fmt.Printf("\nWARNING: Invalid checksum on to-address!\n\n")
  117. }
  118. } else {
  119. fmt.Printf("to: <contact creation>\n")
  120. }
  121. fmt.Printf("from: %v\n", request.Transaction.From.String())
  122. fmt.Printf("value: %v wei\n", weival)
  123. fmt.Printf("gas: %v (%v)\n", request.Transaction.Gas, uint64(request.Transaction.Gas))
  124. fmt.Printf("gasprice: %v wei\n", request.Transaction.GasPrice.ToInt())
  125. fmt.Printf("nonce: %v (%v)\n", request.Transaction.Nonce, uint64(request.Transaction.Nonce))
  126. if request.Transaction.Data != nil {
  127. d := *request.Transaction.Data
  128. if len(d) > 0 {
  129. fmt.Printf("data: %v\n", hexutil.Encode(d))
  130. }
  131. }
  132. if request.Callinfo != nil {
  133. fmt.Printf("\nTransaction validation:\n")
  134. for _, m := range request.Callinfo {
  135. fmt.Printf(" * %s : %s\n", m.Typ, m.Message)
  136. }
  137. fmt.Println()
  138. }
  139. fmt.Printf("\n")
  140. showMetadata(request.Meta)
  141. fmt.Printf("-------------------------------------------\n")
  142. if !ui.confirm() {
  143. return SignTxResponse{request.Transaction, false}, nil
  144. }
  145. return SignTxResponse{request.Transaction, true}, nil
  146. }
  147. // ApproveSignData prompt the user for confirmation to request to sign data
  148. func (ui *CommandlineUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
  149. ui.mu.Lock()
  150. defer ui.mu.Unlock()
  151. fmt.Printf("-------- Sign data request--------------\n")
  152. fmt.Printf("Account: %s\n", request.Address.String())
  153. fmt.Printf("messages:\n")
  154. for _, nvt := range request.Messages {
  155. fmt.Printf("%v\n", nvt.Pprint(1))
  156. }
  157. fmt.Printf("raw data: \n%q\n", request.Rawdata)
  158. fmt.Printf("data hash: %v\n", request.Hash)
  159. fmt.Printf("-------------------------------------------\n")
  160. showMetadata(request.Meta)
  161. if !ui.confirm() {
  162. return SignDataResponse{false}, nil
  163. }
  164. return SignDataResponse{true}, nil
  165. }
  166. // ApproveListing prompt the user for confirmation to list accounts
  167. // the list of accounts to list can be modified by the UI
  168. func (ui *CommandlineUI) ApproveListing(request *ListRequest) (ListResponse, error) {
  169. ui.mu.Lock()
  170. defer ui.mu.Unlock()
  171. fmt.Printf("-------- List Account request--------------\n")
  172. fmt.Printf("A request has been made to list all accounts. \n")
  173. fmt.Printf("You can select which accounts the caller can see\n")
  174. for _, account := range request.Accounts {
  175. fmt.Printf(" [x] %v\n", account.Address.Hex())
  176. fmt.Printf(" URL: %v\n", account.URL)
  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\n")
  190. fmt.Printf("A request has been made to create a new account. \n")
  191. fmt.Printf("Approving this operation means that a new account is created,\n")
  192. fmt.Printf("and the address is returned to the external caller\n\n")
  193. showMetadata(request.Meta)
  194. if !ui.confirm() {
  195. return NewAccountResponse{false}, nil
  196. }
  197. return NewAccountResponse{true}, nil
  198. }
  199. // ShowError displays error message to user
  200. func (ui *CommandlineUI) ShowError(message string) {
  201. fmt.Printf("## Error \n%s\n", message)
  202. fmt.Printf("-------------------------------------------\n")
  203. }
  204. // ShowInfo displays info message to user
  205. func (ui *CommandlineUI) ShowInfo(message string) {
  206. fmt.Printf("## Info \n%s\n", message)
  207. }
  208. func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
  209. fmt.Printf("Transaction signed:\n ")
  210. if jsn, err := json.MarshalIndent(tx.Tx, " ", " "); err != nil {
  211. fmt.Printf("WARN: marshalling error %v\n", err)
  212. } else {
  213. fmt.Println(string(jsn))
  214. }
  215. }
  216. func (ui *CommandlineUI) OnSignerStartup(info StartupInfo) {
  217. fmt.Printf("------- Signer info -------\n")
  218. for k, v := range info.Info {
  219. fmt.Printf("* %v : %v\n", k, v)
  220. }
  221. }