auditlog.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) (Accounts, 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.String())
  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) EcRecover(ctx context.Context, data, sig hexutil.Bytes) (common.Address, error) {
  63. l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  64. "data", common.Bytes2Hex(data))
  65. a, e := l.api.EcRecover(ctx, data, sig)
  66. l.log.Info("EcRecover", "type", "response", "addr", a.String(), "error", e)
  67. return a, e
  68. }
  69. func (l *AuditLogger) Export(ctx context.Context, addr common.Address) (json.RawMessage, error) {
  70. l.log.Info("Export", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  71. "addr", addr.Hex())
  72. j, e := l.api.Export(ctx, addr)
  73. // In this case, we don't actually log the json-response, which may be extra sensitive
  74. l.log.Info("Export", "type", "response", "json response size", len(j), "error", e)
  75. return j, e
  76. }
  77. func (l *AuditLogger) Import(ctx context.Context, keyJSON json.RawMessage) (Account, error) {
  78. // Don't actually log the json contents
  79. l.log.Info("Import", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  80. "keyJSON size", len(keyJSON))
  81. a, e := l.api.Import(ctx, keyJSON)
  82. l.log.Info("Import", "type", "response", "addr", a.String(), "error", e)
  83. return a, e
  84. }
  85. func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
  86. l := log.New("api", "signer")
  87. handler, err := log.FileHandler(path, log.LogfmtFormat())
  88. if err != nil {
  89. return nil, err
  90. }
  91. l.SetHandler(handler)
  92. l.Info("Configured", "audit log", path)
  93. return &AuditLogger{l, api}, nil
  94. }