github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/signer/core/auditlog.go (about)

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