github.com/gregpr07/bsc@v1.1.2/signer/core/auditlog.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  
    23  	"github.com/gregpr07/bsc/common"
    24  	"github.com/gregpr07/bsc/common/hexutil"
    25  	"github.com/gregpr07/bsc/internal/ethapi"
    26  	"github.com/gregpr07/bsc/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  	marshalledData, _ := json.Marshal(data) // can ignore error, marshalling what we just unmarshalled
    66  	l.log.Info("SignData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    67  		"addr", addr.String(), "data", marshalledData, "content-type", contentType)
    68  	b, e := l.api.SignData(ctx, contentType, addr, data)
    69  	l.log.Info("SignData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    70  	return b, e
    71  }
    72  
    73  func (l *AuditLogger) SignGnosisSafeTx(ctx context.Context, addr common.MixedcaseAddress, gnosisTx GnosisSafeTx, methodSelector *string) (*GnosisSafeTx, error) {
    74  	sel := "<nil>"
    75  	if methodSelector != nil {
    76  		sel = *methodSelector
    77  	}
    78  	data, _ := json.Marshal(gnosisTx) // can ignore error, marshalling what we just unmarshalled
    79  	l.log.Info("SignGnosisSafeTx", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    80  		"addr", addr.String(), "data", string(data), "selector", sel)
    81  	res, e := l.api.SignGnosisSafeTx(ctx, addr, gnosisTx, methodSelector)
    82  	if res != nil {
    83  		data, _ := json.Marshal(res) // can ignore error, marshalling what we just unmarshalled
    84  		l.log.Info("SignGnosisSafeTx", "type", "response", "data", string(data), "error", e)
    85  	} else {
    86  		l.log.Info("SignGnosisSafeTx", "type", "response", "data", res, "error", e)
    87  	}
    88  	return res, e
    89  }
    90  
    91  func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) {
    92  	l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    93  		"addr", addr.String(), "data", data)
    94  	b, e := l.api.SignTypedData(ctx, addr, data)
    95  	l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    96  	return b, e
    97  }
    98  
    99  func (l *AuditLogger) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) {
   100  	l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(),
   101  		"data", common.Bytes2Hex(data), "sig", common.Bytes2Hex(sig))
   102  	b, e := l.api.EcRecover(ctx, data, sig)
   103  	l.log.Info("EcRecover", "type", "response", "address", b.String(), "error", e)
   104  	return b, e
   105  }
   106  
   107  func (l *AuditLogger) Version(ctx context.Context) (string, error) {
   108  	l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String())
   109  	data, err := l.api.Version(ctx)
   110  	l.log.Info("Version", "type", "response", "data", data, "error", err)
   111  	return data, err
   112  
   113  }
   114  
   115  func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
   116  	l := log.New("api", "signer")
   117  	handler, err := log.FileHandler(path, log.LogfmtFormat())
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	l.SetHandler(handler)
   122  	l.Info("Configured", "audit log", path)
   123  	return &AuditLogger{l, api}, nil
   124  }