github.com/felberj/go-ethereum@v1.8.23/signer/core/auditlog.go (about)

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