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