github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/signer/core/stdioui.go (about)

     1  //
     2  
     3  package core
     4  
     5  import (
     6  	"context"
     7  	"sync"
     8  
     9  	"github.com/quickchainproject/quickchain/internal/qctapi"
    10  	"github.com/quickchainproject/quickchain/log"
    11  	"github.com/quickchainproject/quickchain/rpc"
    12  )
    13  
    14  type StdIOUI struct {
    15  	client rpc.Client
    16  	mu     sync.Mutex
    17  }
    18  
    19  func NewStdIOUI() *StdIOUI {
    20  	log.Info("NewStdIOUI")
    21  	client, err := rpc.DialContext(context.Background(), "stdio://")
    22  	if err != nil {
    23  		log.Crit("Could not create stdio client", "err", err)
    24  	}
    25  	return &StdIOUI{client: *client}
    26  }
    27  
    28  // dispatch sends a request over the stdio
    29  func (ui *StdIOUI) dispatch(serviceMethod string, args interface{}, reply interface{}) error {
    30  	err := ui.client.Call(&reply, serviceMethod, args)
    31  	if err != nil {
    32  		log.Info("Error", "exc", err.Error())
    33  	}
    34  	return err
    35  }
    36  
    37  func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
    38  	var result SignTxResponse
    39  	err := ui.dispatch("ApproveTx", request, &result)
    40  	return result, err
    41  }
    42  
    43  func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
    44  	var result SignDataResponse
    45  	err := ui.dispatch("ApproveSignData", request, &result)
    46  	return result, err
    47  }
    48  
    49  func (ui *StdIOUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
    50  	var result ExportResponse
    51  	err := ui.dispatch("ApproveExport", request, &result)
    52  	return result, err
    53  }
    54  
    55  func (ui *StdIOUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
    56  	var result ImportResponse
    57  	err := ui.dispatch("ApproveImport", request, &result)
    58  	return result, err
    59  }
    60  
    61  func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) {
    62  	var result ListResponse
    63  	err := ui.dispatch("ApproveListing", request, &result)
    64  	return result, err
    65  }
    66  
    67  func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
    68  	var result NewAccountResponse
    69  	err := ui.dispatch("ApproveNewAccount", request, &result)
    70  	return result, err
    71  }
    72  
    73  func (ui *StdIOUI) ShowError(message string) {
    74  	err := ui.dispatch("ShowError", &Message{message}, nil)
    75  	if err != nil {
    76  		log.Info("Error calling 'ShowError'", "exc", err.Error(), "msg", message)
    77  	}
    78  }
    79  
    80  func (ui *StdIOUI) ShowInfo(message string) {
    81  	err := ui.dispatch("ShowInfo", Message{message}, nil)
    82  	if err != nil {
    83  		log.Info("Error calling 'ShowInfo'", "exc", err.Error(), "msg", message)
    84  	}
    85  }
    86  func (ui *StdIOUI) OnApprovedTx(tx qctapi.SignTransactionResult) {
    87  	err := ui.dispatch("OnApprovedTx", tx, nil)
    88  	if err != nil {
    89  		log.Info("Error calling 'OnApprovedTx'", "exc", err.Error(), "tx", tx)
    90  	}
    91  }
    92  
    93  func (ui *StdIOUI) OnSignerStartup(info StartupInfo) {
    94  	err := ui.dispatch("OnSignerStartup", info, nil)
    95  	if err != nil {
    96  		log.Info("Error calling 'OnSignerStartup'", "exc", err.Error(), "info", info)
    97  	}
    98  }