auditlog.go 4.9 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. "github.com/ethereum/go-ethereum/signer/core/apitypes"
  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) (common.Address, error) {
  37. return l.api.New(ctx)
  38. }
  39. func (l *AuditLogger) SignTransaction(ctx context.Context, args apitypes.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) SignData(ctx context.Context, contentType string, addr common.MixedcaseAddress, data interface{}) (hexutil.Bytes, error) {
  56. marshalledData, _ := json.Marshal(data) // can ignore error, marshalling what we just unmarshalled
  57. l.log.Info("SignData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  58. "addr", addr.String(), "data", marshalledData, "content-type", contentType)
  59. b, e := l.api.SignData(ctx, contentType, addr, data)
  60. l.log.Info("SignData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
  61. return b, e
  62. }
  63. func (l *AuditLogger) SignGnosisSafeTx(ctx context.Context, addr common.MixedcaseAddress, gnosisTx GnosisSafeTx, methodSelector *string) (*GnosisSafeTx, error) {
  64. sel := "<nil>"
  65. if methodSelector != nil {
  66. sel = *methodSelector
  67. }
  68. data, _ := json.Marshal(gnosisTx) // can ignore error, marshalling what we just unmarshalled
  69. l.log.Info("SignGnosisSafeTx", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  70. "addr", addr.String(), "data", string(data), "selector", sel)
  71. res, e := l.api.SignGnosisSafeTx(ctx, addr, gnosisTx, methodSelector)
  72. if res != nil {
  73. data, _ := json.Marshal(res) // can ignore error, marshalling what we just unmarshalled
  74. l.log.Info("SignGnosisSafeTx", "type", "response", "data", string(data), "error", e)
  75. } else {
  76. l.log.Info("SignGnosisSafeTx", "type", "response", "data", res, "error", e)
  77. }
  78. return res, e
  79. }
  80. func (l *AuditLogger) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, data apitypes.TypedData) (hexutil.Bytes, error) {
  81. l.log.Info("SignTypedData", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  82. "addr", addr.String(), "data", data)
  83. b, e := l.api.SignTypedData(ctx, addr, data)
  84. l.log.Info("SignTypedData", "type", "response", "data", common.Bytes2Hex(b), "error", e)
  85. return b, e
  86. }
  87. func (l *AuditLogger) EcRecover(ctx context.Context, data hexutil.Bytes, sig hexutil.Bytes) (common.Address, error) {
  88. l.log.Info("EcRecover", "type", "request", "metadata", MetadataFromContext(ctx).String(),
  89. "data", common.Bytes2Hex(data), "sig", common.Bytes2Hex(sig))
  90. b, e := l.api.EcRecover(ctx, data, sig)
  91. l.log.Info("EcRecover", "type", "response", "address", b.String(), "error", e)
  92. return b, e
  93. }
  94. func (l *AuditLogger) Version(ctx context.Context) (string, error) {
  95. l.log.Info("Version", "type", "request", "metadata", MetadataFromContext(ctx).String())
  96. data, err := l.api.Version(ctx)
  97. l.log.Info("Version", "type", "response", "data", data, "error", err)
  98. return data, err
  99. }
  100. func NewAuditLogger(path string, api ExternalAPI) (*AuditLogger, error) {
  101. l := log.New("api", "signer")
  102. handler, err := log.FileHandler(path, log.LogfmtFormat())
  103. if err != nil {
  104. return nil, err
  105. }
  106. l.SetHandler(handler)
  107. l.Info("Configured", "audit log", path)
  108. return &AuditLogger{l, api}, nil
  109. }