github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/signer/core/stdioui.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  //
    25  
    26  package core
    27  
    28  import (
    29  	"context"
    30  	"sync"
    31  
    32  	"github.com/ethereum/go-ethereum/internal/ethapi"
    33  	"github.com/ethereum/go-ethereum/log"
    34  	"github.com/ethereum/go-ethereum/rpc"
    35  )
    36  
    37  type StdIOUI struct {
    38  	client rpc.Client
    39  	mu     sync.Mutex
    40  }
    41  
    42  func NewStdIOUI() *StdIOUI {
    43  	log.Info("NewStdIOUI")
    44  client, err := rpc.DialContext(context.Background(), "stdio://“”
    45  	if err != nil {
    46  		log.Crit("Could not create stdio client", "err", err)
    47  	}
    48  	return &StdIOUI{client: *client}
    49  }
    50  
    51  //调度通过stdio发送请求
    52  func (ui *StdIOUI) dispatch(serviceMethod string, args interface{}, reply interface{}) error {
    53  	err := ui.client.Call(&reply, serviceMethod, args)
    54  	if err != nil {
    55  		log.Info("Error", "exc", err.Error())
    56  	}
    57  	return err
    58  }
    59  
    60  func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
    61  	var result SignTxResponse
    62  	err := ui.dispatch("ApproveTx", request, &result)
    63  	return result, err
    64  }
    65  
    66  func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
    67  	var result SignDataResponse
    68  	err := ui.dispatch("ApproveSignData", request, &result)
    69  	return result, err
    70  }
    71  
    72  func (ui *StdIOUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
    73  	var result ExportResponse
    74  	err := ui.dispatch("ApproveExport", request, &result)
    75  	return result, err
    76  }
    77  
    78  func (ui *StdIOUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
    79  	var result ImportResponse
    80  	err := ui.dispatch("ApproveImport", request, &result)
    81  	return result, err
    82  }
    83  
    84  func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) {
    85  	var result ListResponse
    86  	err := ui.dispatch("ApproveListing", request, &result)
    87  	return result, err
    88  }
    89  
    90  func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
    91  	var result NewAccountResponse
    92  	err := ui.dispatch("ApproveNewAccount", request, &result)
    93  	return result, err
    94  }
    95  
    96  func (ui *StdIOUI) ShowError(message string) {
    97  	err := ui.dispatch("ShowError", &Message{message}, nil)
    98  	if err != nil {
    99  		log.Info("Error calling 'ShowError'", "exc", err.Error(), "msg", message)
   100  	}
   101  }
   102  
   103  func (ui *StdIOUI) ShowInfo(message string) {
   104  	err := ui.dispatch("ShowInfo", Message{message}, nil)
   105  	if err != nil {
   106  		log.Info("Error calling 'ShowInfo'", "exc", err.Error(), "msg", message)
   107  	}
   108  }
   109  func (ui *StdIOUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
   110  	err := ui.dispatch("OnApprovedTx", tx, nil)
   111  	if err != nil {
   112  		log.Info("Error calling 'OnApprovedTx'", "exc", err.Error(), "tx", tx)
   113  	}
   114  }
   115  
   116  func (ui *StdIOUI) OnSignerStartup(info StartupInfo) {
   117  	err := ui.dispatch("OnSignerStartup", info, nil)
   118  	if err != nil {
   119  		log.Info("Error calling 'OnSignerStartup'", "exc", err.Error(), "info", info)
   120  	}
   121  }