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