github.com/core-coin/go-core/v2@v2.1.9/signer/core/stdioui.go (about)

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