github.com/theQRL/go-zond@v0.2.1/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/theQRL/go-zond/common"
    24  	"github.com/theQRL/go-zond/common/hexutil"
    25  	"github.com/theQRL/go-zond/internal/zondapi"
    26  	"github.com/theQRL/go-zond/log"
    27  	"github.com/theQRL/go-zond/signer/core/apitypes"
    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) (common.Address, error) {
    44  	return l.api.New(ctx)
    45  }
    46  
    47  func (l *AuditLogger) SignTransaction(ctx context.Context, args apitypes.SendTxArgs, methodSelector *string) (*zondapi.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) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) {
    66  	marshalledData, _ := json.Marshal(data) // can ignore error, marshalling what we just unmarshalled
    67  	l.log.Info("SignData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    68  		"addr", addr.String(), "data", marshalledData, "content-type", contentType)
    69  	b, e := l.api.SignData(ctx, contentType, addr, data)
    70  	l.log.Info("SignData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    71  	return b, e
    72  }
    73  
    74  func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data apitypes.TypedData) (hexutil.Bytes, error) {
    75  	l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    76  		"addr", addr.String(), "data", data)
    77  	b, e := l.api.SignTypedData(ctx, addr, data)
    78  	l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    79  	return b, e
    80  }
    81  
    82  func (l *AuditLogger) Version(ctx context.Context) (string, error) {
    83  	l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String())
    84  	data, err := l.api.Version(ctx)
    85  	l.log.Info("Version", "type", "response", "data", data, "error", err)
    86  	return data, err
    87  }
    88  
    89  func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
    90  	l := log.New("api", "signer")
    91  	handler, err := log.FileHandler(path, log.LogfmtFormat())
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	l.SetHandler(handler)
    96  	l.Info("Configured", "audit log", path)
    97  	return &AuditLogger{l, api}, nil
    98  }