github.com/core-coin/go-core/v2@v2.1.9/signer/core/auditlog.go (about)

     1  // Copyright 2018 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core 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/core-coin/go-core/v2/internal/xcbapi"
    24  
    25  	"github.com/core-coin/go-core/v2/common"
    26  	"github.com/core-coin/go-core/v2/common/hexutil"
    27  	"github.com/core-coin/go-core/v2/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) (common.Address, error) {
    44  	return l.api.New(ctx)
    45  }
    46  
    47  func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*xcbapi.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.Address, 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) SignGnosisSafeTx(ctx context.Context, addr common.Address, gnosisTx GnosisSafeTx, methodSelector *string) (*GnosisSafeTx, error) {
    75  	sel := "<nil>"
    76  	if methodSelector != nil {
    77  		sel = *methodSelector
    78  	}
    79  	data, _ := json.Marshal(gnosisTx) // can ignore error, marshalling what we just unmarshalled
    80  	l.log.Info("SignGnosisSafeTx", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    81  		"addr", addr.String(), "data", string(data), "selector", sel)
    82  	res, e := l.api.SignGnosisSafeTx(ctx, addr, gnosisTx, methodSelector)
    83  	if res != nil {
    84  		data, _ := json.Marshal(res) // can ignore error, marshalling what we just unmarshalled
    85  		l.log.Info("SignGnosisSafeTx", "type", "response", "data", string(data), "error", e)
    86  	} else {
    87  		l.log.Info("SignGnosisSafeTx", "type", "response", "data", res, "error", e)
    88  	}
    89  	return res, e
    90  }
    91  
    92  func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.Address, data TypedData) (hexutil.Bytes, error) {
    93  	l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
    94  		"addr", addr.String(), "data", data)
    95  	b, e := l.api.SignTypedData(ctx, addr, data)
    96  	l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
    97  	return b, e
    98  }
    99  
   100  func (l *AuditLogger) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) {
   101  	l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(),
   102  		"data", common.Bytes2Hex(data), "sig", common.Bytes2Hex(sig))
   103  	b, e := l.api.EcRecover(ctx, data, sig)
   104  	l.log.Info("EcRecover", "type", "response", "address", b.String(), "error", e)
   105  	return b, e
   106  }
   107  
   108  func (l *AuditLogger) Version(ctx context.Context) (string, error) {
   109  	l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String())
   110  	data, err := l.api.Version(ctx)
   111  	l.log.Info("Version", "type", "response", "data", data, "error", err)
   112  	return data, err
   113  
   114  }
   115  
   116  func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
   117  	l := log.New("api", "signer")
   118  	handler, err := log.FileHandler(path, log.LogfmtFormat())
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	l.SetHandler(handler)
   123  	l.Info("Configured", "audit log", path)
   124  	return &AuditLogger{l, api}, nil
   125  }