auditlog.go 4.8 KB

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