stdioui.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "context"
  19. "github.com/ethereum/go-ethereum/internal/ethapi"
  20. "github.com/ethereum/go-ethereum/log"
  21. "github.com/ethereum/go-ethereum/rpc"
  22. )
  23. type StdIOUI struct {
  24. client rpc.Client
  25. }
  26. func NewStdIOUI() *StdIOUI {
  27. client, err := rpc.DialContext(context.Background(), "stdio://")
  28. if err != nil {
  29. log.Crit("Could not create stdio client", "err", err)
  30. }
  31. ui := &StdIOUI{client: *client}
  32. return ui
  33. }
  34. func (ui *StdIOUI) RegisterUIServer(api *UIServerAPI) {
  35. ui.client.RegisterName("clef", api)
  36. }
  37. // dispatch sends a request over the stdio
  38. func (ui *StdIOUI) dispatch(serviceMethod string, args interface{}, reply interface{}) error {
  39. err := ui.client.Call(&reply, serviceMethod, args)
  40. if err != nil {
  41. log.Info("Error", "exc", err.Error())
  42. }
  43. return err
  44. }
  45. // notify sends a request over the stdio, and does not listen for a response
  46. func (ui *StdIOUI) notify(serviceMethod string, args interface{}) error {
  47. ctx := context.Background()
  48. err := ui.client.Notify(ctx, serviceMethod, args)
  49. if err != nil {
  50. log.Info("Error", "exc", err.Error())
  51. }
  52. return err
  53. }
  54. func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
  55. var result SignTxResponse
  56. err := ui.dispatch("ui_approveTx", request, &result)
  57. return result, err
  58. }
  59. func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
  60. var result SignDataResponse
  61. err := ui.dispatch("ui_approveSignData", request, &result)
  62. return result, err
  63. }
  64. func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) {
  65. var result ListResponse
  66. err := ui.dispatch("ui_approveListing", request, &result)
  67. return result, err
  68. }
  69. func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
  70. var result NewAccountResponse
  71. err := ui.dispatch("ui_approveNewAccount", request, &result)
  72. return result, err
  73. }
  74. func (ui *StdIOUI) ShowError(message string) {
  75. err := ui.notify("ui_showError", &Message{message})
  76. if err != nil {
  77. log.Info("Error calling 'ui_showError'", "exc", err.Error(), "msg", message)
  78. }
  79. }
  80. func (ui *StdIOUI) ShowInfo(message string) {
  81. err := ui.notify("ui_showInfo", Message{message})
  82. if err != nil {
  83. log.Info("Error calling 'ui_showInfo'", "exc", err.Error(), "msg", message)
  84. }
  85. }
  86. func (ui *StdIOUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
  87. err := ui.notify("ui_onApprovedTx", tx)
  88. if err != nil {
  89. log.Info("Error calling 'ui_onApprovedTx'", "exc", err.Error(), "tx", tx)
  90. }
  91. }
  92. func (ui *StdIOUI) OnSignerStartup(info StartupInfo) {
  93. err := ui.notify("ui_onSignerStartup", info)
  94. if err != nil {
  95. log.Info("Error calling 'ui_onSignerStartup'", "exc", err.Error(), "info", info)
  96. }
  97. }
  98. func (ui *StdIOUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) {
  99. var result UserInputResponse
  100. err := ui.dispatch("ui_onInputRequired", info, &result)
  101. if err != nil {
  102. log.Info("Error calling 'ui_onInputRequired'", "exc", err.Error(), "info", info)
  103. }
  104. return result, err
  105. }