github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/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  	"encoding/json"
    22  
    23  	"github.com/ethereum/go-ethereum/accounts"
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/hexutil"
    26  	"github.com/ethereum/go-ethereum/internal/ethapi"
    27  	"github.com/ethereum/go-ethereum/log"
    28  )
    29  
    30  type AuditLogger struct {
    31  	log log.Logger
    32  	api ExternalAPI
    33  }
    34  
    35  func (l *AuditLogger) List(ctx context.Context) ([]common.Address, error) {
    36  	l.log.Info("List", "type", "request", "metadata", MetadataFromContext(ctx).String())
    37  	res, e := l.api.List(ctx)
    38  	l.log.Info("List", "type", "response", "data", res)
    39  
    40  	return res, e
    41  }
    42  
    43  func (l *AuditLogger) New(ctx context.Context) (accounts.Account, error) {
    44  	return l.api.New(ctx)
    45  }
    46  
    47  func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
    48  	sel := "<nil>"
    49  	if methodSelector != nil {
    50  		sel = *methodSelector
    51  	}
    52  	l.log.Info("SignTransaction", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    53  		"tx", args.String(),
    54  		"methodSelector", sel)
    55  
    56  	res, e := l.api.SignTransaction(ctx, args, methodSelector)
    57  	if res != nil {
    58  		l.log.Info("SignTransaction", "type", "response", "data", common.Bytes2Hex(res.Raw), "error", e)
    59  	} else {
    60  		l.log.Info("SignTransaction", "type", "response", "data", res, "error", e)
    61  	}
    62  	return res, e
    63  }
    64  
    65  func (l *AuditLogger) Sign(ctx context.Context, addr common.MixedcaseAddress, data hexutil.Bytes) (hexutil.Bytes, error) {
    66  	l.log.Info("Sign", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    67  		"addr", addr.String(), "data", common.Bytes2Hex(data))
    68  	b, e := l.api.Sign(ctx, addr, data)
    69  	l.log.Info("Sign", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    70  	return b, e
    71  }
    72  
    73  func (l *AuditLogger) Export(ctx context.Context, addr common.Address) (json.RawMessage, error) {
    74  	l.log.Info("Export", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    75  		"addr", addr.Hex())
    76  	j, e := l.api.Export(ctx, addr)
    77  	// In this case, we don't actually log the json-response, which may be extra sensitive
    78  	l.log.Info("Export", "type", "response", "json response size", len(j), "error", e)
    79  	return j, e
    80  }
    81  
    82  //func (l *AuditLogger) Import(ctx context.Context, keyJSON json.RawMessage) (Account, error) {
    83  //	// Don't actually log the json contents
    84  //	l.log.Info("Import", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    85  //		"keyJSON size", len(keyJSON))
    86  //	a, e := l.api.Import(ctx, keyJSON)
    87  //	l.log.Info("Import", "type", "response", "addr", a.String(), "error", e)
    88  //	return a, e
    89  //}
    90  
    91  func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
    92  	l := log.New("api", "signer")
    93  	handler, err := log.FileHandler(path, log.LogfmtFormat())
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	l.SetHandler(handler)
    98  	l.Info("Configured", "audit log", path)
    99  	return &AuditLogger{l, api}, nil
   100  }