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