stdioui.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. //
  17. package core
  18. import (
  19. "context"
  20. "sync"
  21. "github.com/ethereum/go-ethereum/internal/ethapi"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/rpc"
  24. )
  25. type StdIOUI struct {
  26. client rpc.Client
  27. mu sync.Mutex
  28. }
  29. func NewStdIOUI() *StdIOUI {
  30. log.Info("NewStdIOUI")
  31. client, err := rpc.DialContext(context.Background(), "stdio://")
  32. if err != nil {
  33. log.Crit("Could not create stdio client", "err", err)
  34. }
  35. return &StdIOUI{client: *client}
  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. func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
  46. var result SignTxResponse
  47. err := ui.dispatch("ApproveTx", request, &result)
  48. return result, err
  49. }
  50. func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
  51. var result SignDataResponse
  52. err := ui.dispatch("ApproveSignData", request, &result)
  53. return result, err
  54. }
  55. func (ui *StdIOUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
  56. var result ExportResponse
  57. err := ui.dispatch("ApproveExport", request, &result)
  58. return result, err
  59. }
  60. func (ui *StdIOUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
  61. var result ImportResponse
  62. err := ui.dispatch("ApproveImport", request, &result)
  63. return result, err
  64. }
  65. func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) {
  66. var result ListResponse
  67. err := ui.dispatch("ApproveListing", request, &result)
  68. return result, err
  69. }
  70. func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
  71. var result NewAccountResponse
  72. err := ui.dispatch("ApproveNewAccount", request, &result)
  73. return result, err
  74. }
  75. func (ui *StdIOUI) ShowError(message string) {
  76. err := ui.dispatch("ShowError", &Message{message}, nil)
  77. if err != nil {
  78. log.Info("Error calling 'ShowError'", "exc", err.Error(), "msg", message)
  79. }
  80. }
  81. func (ui *StdIOUI) ShowInfo(message string) {
  82. err := ui.dispatch("ShowInfo", Message{message}, nil)
  83. if err != nil {
  84. log.Info("Error calling 'ShowInfo'", "exc", err.Error(), "msg", message)
  85. }
  86. }
  87. func (ui *StdIOUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
  88. err := ui.dispatch("OnApprovedTx", tx, nil)
  89. if err != nil {
  90. log.Info("Error calling 'OnApprovedTx'", "exc", err.Error(), "tx", tx)
  91. }
  92. }
  93. func (ui *StdIOUI) OnSignerStartup(info StartupInfo) {
  94. err := ui.dispatch("OnSignerStartup", info, nil)
  95. if err != nil {
  96. log.Info("Error calling 'OnSignerStartup'", "exc", err.Error(), "info", info)
  97. }
  98. }