github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/signer/core/auditlog.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package core
    19  
    20  import (
    21  	"context"
    22  
    23  	"github.com/AigarNetwork/aigar/common"
    24  	"github.com/AigarNetwork/aigar/common/hexutil"
    25  	"github.com/AigarNetwork/aigar/internal/ethapi"
    26  	"github.com/AigarNetwork/aigar/log"
    27  )
    28  
    29  type AuditLogger struct {
    30  	log log.Logger
    31  	api ExternalAPI
    32  }
    33  
    34  func (l *AuditLogger) List(ctx context.Context) ([]common.Address, error) {
    35  	l.log.Info("List", "type", "request", "metadata", MetadataFromContext(ctx).String())
    36  	res, e := l.api.List(ctx)
    37  	l.log.Info("List", "type", "response", "data", res)
    38  
    39  	return res, e
    40  }
    41  
    42  func (l *AuditLogger) New(ctx context.Context) (common.Address, error) {
    43  	return l.api.New(ctx)
    44  }
    45  
    46  func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
    47  	sel := "<nil>"
    48  	if methodSelector != nil {
    49  		sel = *methodSelector
    50  	}
    51  	l.log.Info("SignTransaction", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    52  		"tx", args.String(),
    53  		"methodSelector", sel)
    54  
    55  	res, e := l.api.SignTransaction(ctx, args, methodSelector)
    56  	if res != nil {
    57  		l.log.Info("SignTransaction", "type", "response", "data", common.Bytes2Hex(res.Raw), "error", e)
    58  	} else {
    59  		l.log.Info("SignTransaction", "type", "response", "data", res, "error", e)
    60  	}
    61  	return res, e
    62  }
    63  
    64  func (l *AuditLogger) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) {
    65  	l.log.Info("SignData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    66  		"addr", addr.String(), "data", data, "content-type", contentType)
    67  	b, e := l.api.SignData(ctx, contentType, addr, data)
    68  	l.log.Info("SignData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    69  	return b, e
    70  }
    71  
    72  func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) {
    73  	l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    74  		"addr", addr.String(), "data", data)
    75  	b, e := l.api.SignTypedData(ctx, addr, data)
    76  	l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    77  	return b, e
    78  }
    79  
    80  func (l *AuditLogger) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) {
    81  	l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    82  		"data", common.Bytes2Hex(data), "sig", common.Bytes2Hex(sig))
    83  	b, e := l.api.EcRecover(ctx, data, sig)
    84  	l.log.Info("EcRecover", "type", "response", "address", b.String(), "error", e)
    85  	return b, e
    86  }
    87  
    88  func (l *AuditLogger) Version(ctx context.Context) (string, error) {
    89  	l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String())
    90  	data, err := l.api.Version(ctx)
    91  	l.log.Info("Version", "type", "response", "data", data, "error", err)
    92  	return data, err
    93  
    94  }
    95  
    96  func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
    97  	l := log.New("api", "signer")
    98  	handler, err := log.FileHandler(path, log.LogfmtFormat())
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	l.SetHandler(handler)
   103  	l.Info("Configured", "audit log", path)
   104  	return &AuditLogger{l, api}, nil
   105  }