auditlog.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "context"
  19. "encoding/json"
  20. "github.com/ethereum/go-ethereum/accounts"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/hexutil"
  23. "github.com/ethereum/go-ethereum/internal/ethapi"
  24. "github.com/ethereum/go-ethereum/log"
  25. )
  26. type AuditLogger struct {
  27. log log.Logger
  28. api ExternalAPI
  29. }
  30. func (l *AuditLogger) List(ctx context.Context) ([]common.Address, error) {
  31. l.log.Info("List", "type", "request", "metadata", MetadataFromContext(ctx).String())
  32. res, e := l.api.List(ctx)
  33. l.log.Info("List", "type", "response", "data", res)
  34. return res, e
  35. }
  36. func (l *AuditLogger) New(ctx context.Context) (accounts.Account, error) {
  37. return l.api.New(ctx)
  38. }
  39. func (l *AuditLogger) SignTransaction(ctx context.Context, args SendTxArgs, methodSelector *string) (*ethapi.SignTransactionResult, error) {
  40. sel := "<nil>"
  41. if methodSelector != nil {
  42. sel = *methodSelector
  43. }
  44. l.log.Info("SignTransaction", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  45. "tx", args.String(),
  46. "methodSelector", sel)
  47. res, e := l.api.SignTransaction(ctx, args, methodSelector)
  48. if res != nil {
  49. l.log.Info("SignTransaction", "type", "response", "data", common.Bytes2Hex(res.Raw), "error", e)
  50. } else {
  51. l.log.Info("SignTransaction", "type", "response", "data", res, "error", e)
  52. }
  53. return res, e
  54. }
  55. func (l *AuditLogger) Sign(ctx context.Context, addr common.MixedcaseAddress, data hexutil.Bytes) (hexutil.Bytes, error) {
  56. l.log.Info("Sign", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  57. "addr", addr.String(), "data", common.Bytes2Hex(data))
  58. b, e := l.api.Sign(ctx, addr, data)
  59. l.log.Info("Sign", "type", "response", "data", common.Bytes2Hex(b), "error", e)
  60. return b, e
  61. }
  62. func (l *AuditLogger) Export(ctx context.Context, addr common.Address) (json.RawMessage, error) {
  63. l.log.Info("Export", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  64. "addr", addr.Hex())
  65. j, e := l.api.Export(ctx, addr)
  66. // In this case, we don't actually log the json-response, which may be extra sensitive
  67. l.log.Info("Export", "type", "response", "json response size", len(j), "error", e)
  68. return j, e
  69. }
  70. func (l *AuditLogger) Version(ctx context.Context) (string, error) {
  71. l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String())
  72. data, err := l.api.Version(ctx)
  73. l.log.Info("Version", "type", "response", "data", data, "error", err)
  74. return data, err
  75. }
  76. func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
  77. l := log.New("api", "signer")
  78. handler, err := log.FileHandler(path, log.LogfmtFormat())
  79. if err != nil {
  80. return nil, err
  81. }
  82. l.SetHandler(handler)
  83. l.Info("Configured", "audit log", path)
  84. return &AuditLogger{l, api}, nil
  85. }