github.com/bcskill/bcschain/v3@v3.4.9-beta2/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/bcskill/bcschain/v3/common" 24 "github.com/bcskill/bcschain/v3/common/hexutil" 25 "github.com/bcskill/bcschain/v3/internal/ethapi" 26 "github.com/bcskill/bcschain/v3/log" 27 ) 28 29 type AuditLogger struct { 30 log log.Logger 31 api ExternalAPI 32 } 33 34 func (l *AuditLogger) List(ctx context.Context) ([]common.Address, error) { 35 l.log.Info("List", "type", "request", "metadata", MetadataFromContext(ctx).String()) 36 res, e := l.api.List(ctx) 37 l.log.Info("List", "type", "response", "data", res) 38 39 return res, e 40 } 41 42 func (l *AuditLogger) New(ctx context.Context) (common.Address, error) { 43 return l.api.New(ctx) 44 } 45 46 func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) { 47 sel := "<nil>" 48 if methodSelector != nil { 49 sel = *methodSelector 50 } 51 l.log.Info("SignTransaction", "type", "request", "metadata", MetadataFromContext(ctx).String(), 52 "tx", args.String(), 53 "methodSelector", sel) 54 55 res, e := l.api.SignTransaction(ctx, args, methodSelector) 56 if res != nil { 57 l.log.Info("SignTransaction", "type", "response", "data", common.Bytes2Hex(res.Raw), "error", e) 58 } else { 59 l.log.Info("SignTransaction", "type", "response", "data", res, "error", e) 60 } 61 return res, e 62 } 63 64 func (l *AuditLogger) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) { 65 marshalledData, _ := json.Marshal(data) // can ignore error, marshalling what we just unmarshalled 66 l.log.Info("SignData", "type", "request", "metadata", MetadataFromContext(ctx).String(), 67 "addr", addr.String(), "data", marshalledData, "content-type", contentType) 68 b, e := l.api.SignData(ctx, contentType, addr, data) 69 l.log.Info("SignData", "type", "response", "data", common.Bytes2Hex(b), "error", e) 70 return b, e 71 } 72 73 func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) { 74 l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(), 75 "addr", addr.String(), "data", data) 76 b, e := l.api.SignTypedData(ctx, addr, data) 77 l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e) 78 return b, e 79 } 80 81 func (l *AuditLogger) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) { 82 l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(), 83 "data", common.Bytes2Hex(data), "sig", common.Bytes2Hex(sig)) 84 b, e := l.api.EcRecover(ctx, data, sig) 85 l.log.Info("EcRecover", "type", "response", "address", b.String(), "error", e) 86 return b, e 87 } 88 89 func (l *AuditLogger) Version(ctx context.Context) (string, error) { 90 l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String()) 91 data, err := l.api.Version(ctx) 92 l.log.Info("Version", "type", "response", "data", data, "error", err) 93 return data, err 94 95 } 96 97 func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) { 98 l := log.New("api", "signer") 99 handler, err := log.FileHandler(path, log.LogfmtFormat()) 100 if err != nil { 101 return nil, err 102 } 103 l.SetHandler(handler) 104 l.Info("Configured", "audit log", path) 105 return &AuditLogger{l, api}, nil 106 }