auditlog.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. package core
  17. import (
  18. "context"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/common/hexutil"
  21. "github.com/ethereum/go-ethereum/internal/ethapi"
  22. "github.com/ethereum/go-ethereum/log"
  23. )
  24. type AuditLogger struct {
  25. log log.Logger
  26. api ExternalAPI
  27. }
  28. func (l *AuditLogger) List(ctx context.Context) ([]common.Address, error) {
  29. l.log.Info("List", "type", "request", "metadata", MetadataFromContext(ctx).String())
  30. res, e := l.api.List(ctx)
  31. l.log.Info("List", "type", "response", "data", res)
  32. return res, e
  33. }
  34. func (l *AuditLogger) New(ctx context.Context) (common.Address, error) {
  35. return l.api.New(ctx)
  36. }
  37. func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
  38. sel := "<nil>"
  39. if methodSelector != nil {
  40. sel = *methodSelector
  41. }
  42. l.log.Info("SignTransaction", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  43. "tx", args.String(),
  44. "methodSelector", sel)
  45. res, e := l.api.SignTransaction(ctx, args, methodSelector)
  46. if res != nil {
  47. l.log.Info("SignTransaction", "type", "response", "data", common.Bytes2Hex(res.Raw), "error", e)
  48. } else {
  49. l.log.Info("SignTransaction", "type", "response", "data", res, "error", e)
  50. }
  51. return res, e
  52. }
  53. func (l *AuditLogger) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) {
  54. l.log.Info("SignData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  55. "addr", addr.String(), "data", data, "content-type", contentType)
  56. b, e := l.api.SignData(ctx, contentType, addr, data)
  57. l.log.Info("SignData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
  58. return b, e
  59. }
  60. func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data TypedData) (hexutil.Bytes, error) {
  61. l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  62. "addr", addr.String(), "data", data)
  63. b, e := l.api.SignTypedData(ctx, addr, data)
  64. l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
  65. return b, e
  66. }
  67. func (l *AuditLogger) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) {
  68. l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  69. "data", common.Bytes2Hex(data), "sig", common.Bytes2Hex(sig))
  70. b, e := l.api.EcRecover(ctx, data, sig)
  71. l.log.Info("EcRecover", "type", "response", "address", b.String(), "error", e)
  72. return b, e
  73. }
  74. func (l *AuditLogger) Version(ctx context.Context) (string, error) {
  75. l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String())
  76. data, err := l.api.Version(ctx)
  77. l.log.Info("Version", "type", "response", "data", data, "error", err)
  78. return data, err
  79. }
  80. func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
  81. l := log.New("api", "signer")
  82. handler, err := log.FileHandler(path, log.LogfmtFormat())
  83. if err != nil {
  84. return nil, err
  85. }
  86. l.SetHandler(handler)
  87. l.Info("Configured", "audit log", path)
  88. return &AuditLogger{l, api}, nil
  89. }