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