proxy_api.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2015 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 ethapi
  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/core/types"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/rpc"
  24. )
  25. type ProxyAPISupport interface {
  26. ProxyEnabled() bool
  27. ProxyClient() *rpc.Client
  28. }
  29. // PublicTransactionPoolAPI exposes methods for the RPC interface
  30. type PublicTransactionPoolProxyAPI struct {
  31. PublicTransactionPoolAPI
  32. proxyClient *rpc.Client
  33. }
  34. // NewPublicTransactionPoolAPI creates a new RPC service with methods specific for the transaction pool.
  35. func NewPublicTransactionPoolProxyAPI(b Backend, nonceLock *AddrLocker) interface{} {
  36. apiSupport, ok := b.(ProxyAPISupport)
  37. if ok && apiSupport.ProxyEnabled() {
  38. signer := types.LatestSigner(b.ChainConfig())
  39. return &PublicTransactionPoolProxyAPI{
  40. PublicTransactionPoolAPI{b, nonceLock, signer},
  41. apiSupport.ProxyClient(),
  42. }
  43. }
  44. return NewPublicTransactionPoolAPI(b, nonceLock)
  45. }
  46. func (s *PublicTransactionPoolProxyAPI) SendTransaction(ctx context.Context, args SendTxArgs) (common.Hash, error) {
  47. log.Info("QLight - proxy enabled")
  48. var result common.Hash
  49. err := s.proxyClient.CallContext(ctx, &result, "eth_sendTransaction", args)
  50. return result, err
  51. }
  52. func (s *PublicTransactionPoolProxyAPI) SendRawTransaction(ctx context.Context, encodedTx hexutil.Bytes) (common.Hash, error) {
  53. log.Info("QLight - proxy enabled")
  54. var result common.Hash
  55. err := s.proxyClient.CallContext(ctx, &result, "eth_sendRawTransaction", encodedTx)
  56. return result, err
  57. }
  58. func (s *PublicTransactionPoolProxyAPI) SendRawPrivateTransaction(ctx context.Context, encodedTx hexutil.Bytes, args SendRawTxArgs) (common.Hash, error) {
  59. log.Info("QLight - proxy enabled")
  60. var result common.Hash
  61. err := s.proxyClient.CallContext(ctx, &result, "eth_sendRawPrivateTransaction", encodedTx, args)
  62. return result, err
  63. }
  64. func (s *PublicTransactionPoolProxyAPI) FillTransaction(ctx context.Context, args SendTxArgs) (*SignTransactionResult, error) {
  65. log.Info("QLight - proxy enabled")
  66. var result SignTransactionResult
  67. err := s.proxyClient.CallContext(ctx, &result, "eth_fillTransaction", args)
  68. return &result, err
  69. }
  70. func (s *PublicTransactionPoolProxyAPI) DistributePrivateTransaction(ctx context.Context, encodedTx hexutil.Bytes, args SendRawTxArgs) (string, error) {
  71. log.Info("QLight - proxy enabled")
  72. var result string
  73. err := s.proxyClient.CallContext(ctx, &result, "eth_distributePrivateTransaction", encodedTx, args)
  74. return result, err
  75. }
  76. func (s *PublicTransactionPoolProxyAPI) Resend(ctx context.Context, sendArgs SendTxArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error) {
  77. log.Info("QLight - proxy enabled")
  78. var result common.Hash
  79. err := s.proxyClient.CallContext(ctx, &result, "eth_resend", sendArgs, gasPrice, gasLimit)
  80. return result, err
  81. }
  82. func (s *PublicTransactionPoolProxyAPI) SendTransactionAsync(ctx context.Context, args AsyncSendTxArgs) (common.Hash, error) {
  83. log.Info("QLight - proxy enabled")
  84. var result common.Hash
  85. err := s.proxyClient.CallContext(ctx, &result, "eth_sendTransactionAsync", args)
  86. return result, err
  87. }
  88. func (s *PublicTransactionPoolProxyAPI) Sign(addr common.Address, data hexutil.Bytes) (hexutil.Bytes, error) {
  89. log.Info("QLight - proxy enabled")
  90. var result hexutil.Bytes
  91. err := s.proxyClient.Call(&result, "eth_sign", addr, data)
  92. return result, err
  93. }
  94. func (s *PublicTransactionPoolProxyAPI) SignTransaction(ctx context.Context, args SendTxArgs) (*SignTransactionResult, error) {
  95. log.Info("QLight - proxy enabled")
  96. var result SignTransactionResult
  97. err := s.proxyClient.CallContext(ctx, &result, "eth_signTransaction", args)
  98. return &result, err
  99. }
  100. type PrivateAccountProxyAPI struct {
  101. PrivateAccountAPI
  102. proxyClient *rpc.Client
  103. }
  104. func NewPrivateAccountProxyAPI(b Backend, nonceLock *AddrLocker) interface{} {
  105. apiSupport, ok := b.(ProxyAPISupport)
  106. if ok && apiSupport.ProxyEnabled() {
  107. return &PrivateAccountProxyAPI{
  108. PrivateAccountAPI{
  109. am: b.AccountManager(),
  110. nonceLock: nonceLock,
  111. b: b,
  112. },
  113. apiSupport.ProxyClient(),
  114. }
  115. }
  116. return NewPrivateAccountAPI(b, nonceLock)
  117. }
  118. func (s *PrivateAccountProxyAPI) SendTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) {
  119. log.Info("QLight - proxy enabled")
  120. var result common.Hash
  121. err := s.proxyClient.CallContext(ctx, &result, "personal_sendTransaction", args, passwd)
  122. return result, err
  123. }
  124. func (s *PrivateAccountProxyAPI) SignTransaction(ctx context.Context, args SendTxArgs, passwd string) (*SignTransactionResult, error) {
  125. log.Info("QLight - proxy enabled")
  126. var result SignTransactionResult
  127. err := s.proxyClient.CallContext(ctx, &result, "personal_signTransaction", args, passwd)
  128. return &result, err
  129. }
  130. func (s *PrivateAccountProxyAPI) Sign(ctx context.Context, data hexutil.Bytes, addr common.Address, passwd string) (hexutil.Bytes, error) {
  131. log.Info("QLight - proxy enabled")
  132. var result hexutil.Bytes
  133. err := s.proxyClient.CallContext(ctx, &result, "personal_sign", data, addr, passwd)
  134. return result, err
  135. }
  136. func (s *PrivateAccountProxyAPI) SignAndSendTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) {
  137. return s.SendTransaction(ctx, args, passwd)
  138. }