github.com/ethereum/go-ethereum@v1.16.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 "log/slog" 23 "os" 24 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 "github.com/ethereum/go-ethereum/signer/core/apitypes" 30 ) 31 32 type AuditLogger struct { 33 log log.Logger 34 api ExternalAPI 35 } 36 37 func (l *AuditLogger) List(ctx context.Context) ([]common.Address, error) { 38 l.log.Info("List", "type", "request", "metadata", MetadataFromContext(ctx).String()) 39 res, e := l.api.List(ctx) 40 l.log.Info("List", "type", "response", "data", res) 41 42 return res, e 43 } 44 45 func (l *AuditLogger) New(ctx context.Context) (common.Address, error) { 46 return l.api.New(ctx) 47 } 48 49 func (l *AuditLogger) SignTransaction(ctx context.Context, args apitypes.SendTxArgs, methodSelector *string) (*ethapi.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) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) { 68 marshalledData, _ := json.Marshal(data) // can ignore error, marshalling what we just unmarshalled 69 l.log.Info("SignData", "type", "request", "metadata", MetadataFromContext(ctx).String(), 70 "addr", addr.String(), "data", marshalledData, "content-type", contentType) 71 b, e := l.api.SignData(ctx, contentType, addr, data) 72 l.log.Info("SignData", "type", "response", "data", common.Bytes2Hex(b), "error", e) 73 return b, e 74 } 75 76 func (l *AuditLogger) SignGnosisSafeTx(ctx context.Context, addr common.MixedcaseAddress, gnosisTx GnosisSafeTx, methodSelector *string) (*GnosisSafeTx, error) { 77 sel := "<nil>" 78 if methodSelector != nil { 79 sel = *methodSelector 80 } 81 data, _ := json.Marshal(gnosisTx) // can ignore error, marshalling what we just unmarshalled 82 l.log.Info("SignGnosisSafeTx", "type", "request", "metadata", MetadataFromContext(ctx).String(), 83 "addr", addr.String(), "data", string(data), "selector", sel) 84 res, e := l.api.SignGnosisSafeTx(ctx, addr, gnosisTx, methodSelector) 85 if res != nil { 86 data, _ := json.Marshal(res) // can ignore error, marshalling what we just unmarshalled 87 l.log.Info("SignGnosisSafeTx", "type", "response", "data", string(data), "error", e) 88 } else { 89 l.log.Info("SignGnosisSafeTx", "type", "response", "data", res, "error", e) 90 } 91 return res, e 92 } 93 94 func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data apitypes.TypedData) (hexutil.Bytes, error) { 95 l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(), 96 "addr", addr.String(), "data", data) 97 b, e := l.api.SignTypedData(ctx, addr, data) 98 l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e) 99 return b, e 100 } 101 102 func (l *AuditLogger) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) { 103 l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(), 104 "data", common.Bytes2Hex(data), "sig", common.Bytes2Hex(sig)) 105 b, e := l.api.EcRecover(ctx, data, sig) 106 l.log.Info("EcRecover", "type", "response", "address", b.String(), "error", e) 107 return b, e 108 } 109 110 func (l *AuditLogger) Version(ctx context.Context) (string, error) { 111 l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String()) 112 data, err := l.api.Version(ctx) 113 l.log.Info("Version", "type", "response", "data", data, "error", err) 114 return data, err 115 } 116 117 func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) { 118 f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) 119 if err != nil { 120 return nil, err 121 } 122 123 handler := slog.NewTextHandler(f, nil) 124 l := log.NewLogger(handler).With("api", "signer") 125 l.Info("Configured", "audit log", path) 126 return &AuditLogger{l, api}, nil 127 }