github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/signer/core/auditlog.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  package core
    26  
    27  import (
    28  	"context"
    29  
    30  	"encoding/json"
    31  
    32  	"github.com/ethereum/go-ethereum/accounts"
    33  	"github.com/ethereum/go-ethereum/common"
    34  	"github.com/ethereum/go-ethereum/common/hexutil"
    35  	"github.com/ethereum/go-ethereum/internal/ethapi"
    36  	"github.com/ethereum/go-ethereum/log"
    37  )
    38  
    39  type AuditLogger struct {
    40  	log log.Logger
    41  	api ExternalAPI
    42  }
    43  
    44  func (l *AuditLogger) List(ctx context.Context) (Accounts, error) {
    45  	l.log.Info("List", "type", "request", "metadata", MetadataFromContext(ctx).String())
    46  	res, e := l.api.List(ctx)
    47  
    48  	l.log.Info("List", "type", "response", "data", res.String())
    49  
    50  	return res, e
    51  }
    52  
    53  func (l *AuditLogger) New(ctx context.Context) (accounts.Account, error) {
    54  	return l.api.New(ctx)
    55  }
    56  
    57  func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
    58  	sel := "<nil>"
    59  	if methodSelector != nil {
    60  		sel = *methodSelector
    61  	}
    62  	l.log.Info("SignTransaction", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    63  		"tx", args.String(),
    64  		"methodSelector", sel)
    65  
    66  	res, e := l.api.SignTransaction(ctx, args, methodSelector)
    67  	if res != nil {
    68  		l.log.Info("SignTransaction", "type", "response", "data", common.Bytes2Hex(res.Raw), "error", e)
    69  	} else {
    70  		l.log.Info("SignTransaction", "type", "response", "data", res, "error", e)
    71  	}
    72  	return res, e
    73  }
    74  
    75  func (l *AuditLogger) Sign(ctx context.Context, addr common.MixedcaseAddress, data hexutil.Bytes) (hexutil.Bytes, error) {
    76  	l.log.Info("Sign", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    77  		"addr", addr.String(), "data", common.Bytes2Hex(data))
    78  	b, e := l.api.Sign(ctx, addr, data)
    79  	l.log.Info("Sign", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    80  	return b, e
    81  }
    82  
    83  func (l *AuditLogger) EcRecover(ctx context.Context, data, sig hexutil.Bytes) (common.Address, error) {
    84  	l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    85  		"data", common.Bytes2Hex(data))
    86  	a, e := l.api.EcRecover(ctx, data, sig)
    87  	l.log.Info("EcRecover", "type", "response", "addr", a.String(), "error", e)
    88  	return a, e
    89  }
    90  
    91  func (l *AuditLogger) Export(ctx context.Context, addr common.Address) (json.RawMessage, error) {
    92  	l.log.Info("Export", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    93  		"addr", addr.Hex())
    94  	j, e := l.api.Export(ctx, addr)
    95  //在这种情况下,我们实际上不记录JSON响应,这可能是非常敏感的。
    96  	l.log.Info("Export", "type", "response", "json response size", len(j), "error", e)
    97  	return j, e
    98  }
    99  
   100  func (l *AuditLogger) Import(ctx context.Context, keyJSON json.RawMessage) (Account, error) {
   101  //不要记录JSON内容
   102  	l.log.Info("Import", "type", "request", "metadata", MetadataFromContext(ctx).String(),
   103  		"keyJSON size", len(keyJSON))
   104  	a, e := l.api.Import(ctx, keyJSON)
   105  	l.log.Info("Import", "type", "response", "addr", a.String(), "error", e)
   106  	return a, e
   107  }
   108  
   109  func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
   110  	l := log.New("api", "signer")
   111  	handler, err := log.FileHandler(path, log.LogfmtFormat())
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  	l.SetHandler(handler)
   116  	l.Info("Configured", "audit log", path)
   117  	return &AuditLogger{l, api}, nil
   118  }