stdioui.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "sync"
  20. "github.com/ethereum/go-ethereum/internal/ethapi"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/rpc"
  23. )
  24. type StdIOUI struct {
  25. client rpc.Client
  26. mu sync.Mutex
  27. }
  28. func NewStdIOUI() *StdIOUI {
  29. client, err := rpc.DialContext(context.Background(), "stdio://")
  30. if err != nil {
  31. log.Crit("Could not create stdio client", "err", err)
  32. }
  33. ui := &StdIOUI{client: *client}
  34. return ui
  35. }
  36. func (ui *StdIOUI) RegisterUIServer(api *UIServerAPI) {
  37. ui.client.RegisterName("clef", api)
  38. }
  39. // dispatch sends a request over the stdio
  40. func (ui *StdIOUI) dispatch(serviceMethod string, args interface{}, reply interface{}) error {
  41. err := ui.client.Call(&reply, serviceMethod, args)
  42. if err != nil {
  43. log.Info("Error", "exc", err.Error())
  44. }
  45. return err
  46. }
  47. // notify sends a request over the stdio, and does not listen for a response
  48. func (ui *StdIOUI) notify(serviceMethod string, args interface{}) error {
  49. ctx := context.Background()
  50. err := ui.client.Notify(ctx, serviceMethod, args)
  51. if err != nil {
  52. log.Info("Error", "exc", err.Error())
  53. }
  54. return err
  55. }
  56. func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
  57. var result SignTxResponse
  58. err := ui.dispatch("ui_approveTx", request, &result)
  59. return result, err
  60. }
  61. func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
  62. var result SignDataResponse
  63. err := ui.dispatch("ui_approveSignData", request, &result)
  64. return result, err
  65. }
  66. func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) {
  67. var result ListResponse
  68. err := ui.dispatch("ui_approveListing", request, &result)
  69. return result, err
  70. }
  71. func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
  72. var result NewAccountResponse
  73. err := ui.dispatch("ui_approveNewAccount", request, &result)
  74. return result, err
  75. }
  76. func (ui *StdIOUI) ShowError(message string) {
  77. err := ui.notify("ui_showError", &Message{message})
  78. if err != nil {
  79. log.Info("Error calling 'ui_showError'", "exc", err.Error(), "msg", message)
  80. }
  81. }
  82. func (ui *StdIOUI) ShowInfo(message string) {
  83. err := ui.notify("ui_showInfo", Message{message})
  84. if err != nil {
  85. log.Info("Error calling 'ui_showInfo'", "exc", err.Error(), "msg", message)
  86. }
  87. }
  88. func (ui *StdIOUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
  89. err := ui.notify("ui_onApprovedTx", tx)
  90. if err != nil {
  91. log.Info("Error calling 'ui_onApprovedTx'", "exc", err.Error(), "tx", tx)
  92. }
  93. }
  94. func (ui *StdIOUI) OnSignerStartup(info StartupInfo) {
  95. err := ui.notify("ui_onSignerStartup", info)
  96. if err != nil {
  97. log.Info("Error calling 'ui_onSignerStartup'", "exc", err.Error(), "info", info)
  98. }
  99. }
  100. func (ui *StdIOUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) {
  101. var result UserInputResponse
  102. err := ui.dispatch("ui_onInputRequired", info, &result)
  103. if err != nil {
  104. log.Info("Error calling 'ui_onInputRequired'", "exc", err.Error(), "info", info)
  105. }
  106. return result, err
  107. }