github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/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  	log.Info("NewStdIOUI")
    36  	client, err := rpc.DialContext(context.Background(), "stdio://")
    37  	if err != nil {
    38  		log.Crit("Could not create stdio client", "err", err)
    39  	}
    40  	return &StdIOUI{client: *client}
    41  }
    42  
    43  // dispatch sends a request over the stdio
    44  func (ui *StdIOUI) dispatch(serviceMethod string, args interface{}, reply interface{}) error {
    45  	err := ui.client.Call(&reply, serviceMethod, args)
    46  	if err != nil {
    47  		log.Info("Error", "exc", err.Error())
    48  	}
    49  	return err
    50  }
    51  
    52  func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
    53  	var result SignTxResponse
    54  	err := ui.dispatch("ApproveTx", request, &result)
    55  	return result, err
    56  }
    57  
    58  func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
    59  	var result SignDataResponse
    60  	err := ui.dispatch("ApproveSignData", request, &result)
    61  	return result, err
    62  }
    63  
    64  func (ui *StdIOUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
    65  	var result ExportResponse
    66  	err := ui.dispatch("ApproveExport", request, &result)
    67  	return result, err
    68  }
    69  
    70  func (ui *StdIOUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
    71  	var result ImportResponse
    72  	err := ui.dispatch("ApproveImport", 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("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("ApproveNewAccount", request, &result)
    85  	return result, err
    86  }
    87  
    88  func (ui *StdIOUI) ShowError(message string) {
    89  	err := ui.dispatch("ShowError", &Message{message}, nil)
    90  	if err != nil {
    91  		log.Info("Error calling 'ShowError'", "exc", err.Error(), "msg", message)
    92  	}
    93  }
    94  
    95  func (ui *StdIOUI) ShowInfo(message string) {
    96  	err := ui.dispatch("ShowInfo", Message{message}, nil)
    97  	if err != nil {
    98  		log.Info("Error calling 'ShowInfo'", "exc", err.Error(), "msg", message)
    99  	}
   100  }
   101  func (ui *StdIOUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
   102  	err := ui.dispatch("OnApprovedTx", tx, nil)
   103  	if err != nil {
   104  		log.Info("Error calling 'OnApprovedTx'", "exc", err.Error(), "tx", tx)
   105  	}
   106  }
   107  
   108  func (ui *StdIOUI) OnSignerStartup(info StartupInfo) {
   109  	err := ui.dispatch("OnSignerStartup", info, nil)
   110  	if err != nil {
   111  		log.Info("Error calling 'OnSignerStartup'", "exc", err.Error(), "info", info)
   112  	}
   113  }