github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/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/ethereum/go-ethereum/internal/ethapi" 25 "github.com/ethereum/go-ethereum/log" 26 "github.com/ethereum/go-ethereum/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("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("ApproveSignData", request, &result) 75 return result, err 76 } 77 78 func (ui *StdIOUI) ApproveExport(request *ExportRequest) (ExportResponse, error) { 79 var result ExportResponse 80 err := ui.dispatch("ApproveExport", request, &result) 81 return result, err 82 } 83 84 func (ui *StdIOUI) ApproveImport(request *ImportRequest) (ImportResponse, error) { 85 var result ImportResponse 86 err := ui.dispatch("ApproveImport", request, &result) 87 return result, err 88 } 89 90 func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) { 91 var result ListResponse 92 err := ui.dispatch("ApproveListing", request, &result) 93 return result, err 94 } 95 96 func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) { 97 var result NewAccountResponse 98 err := ui.dispatch("ApproveNewAccount", request, &result) 99 return result, err 100 } 101 102 func (ui *StdIOUI) ShowError(message string) { 103 err := ui.notify("ShowError", &Message{message}) 104 if err != nil { 105 log.Info("Error calling 'ShowError'", "exc", err.Error(), "msg", message) 106 } 107 } 108 109 func (ui *StdIOUI) ShowInfo(message string) { 110 err := ui.notify("ShowInfo", Message{message}) 111 if err != nil { 112 log.Info("Error calling 'ShowInfo'", "exc", err.Error(), "msg", message) 113 } 114 } 115 func (ui *StdIOUI) OnApprovedTx(tx ethapi.SignTransactionResult) { 116 err := ui.notify("OnApprovedTx", tx) 117 if err != nil { 118 log.Info("Error calling 'OnApprovedTx'", "exc", err.Error(), "tx", tx) 119 } 120 } 121 122 func (ui *StdIOUI) OnSignerStartup(info StartupInfo) { 123 err := ui.notify("OnSignerStartup", info) 124 if err != nil { 125 log.Info("Error calling 'OnSignerStartup'", "exc", err.Error(), "info", info) 126 } 127 } 128 func (ui *StdIOUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) { 129 var result UserInputResponse 130 err := ui.dispatch("OnInputRequired", info, &result) 131 if err != nil { 132 log.Info("Error calling 'OnInputRequired'", "exc", err.Error(), "info", info) 133 } 134 return result, err 135 }