api.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 utils
  17. import (
  18. "fmt"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/crypto"
  21. "github.com/ethereum/go-ethereum/node"
  22. "github.com/ethereum/go-ethereum/p2p"
  23. rpc "github.com/ethereum/go-ethereum/rpc/v2"
  24. )
  25. // PublicWeb3API offers helper utils
  26. type PublicWeb3API struct {
  27. stack *node.Node
  28. }
  29. // NewPublicWeb3API creates a new Web3Service instance
  30. func NewPublicWeb3API(stack *node.Node) *PublicWeb3API {
  31. return &PublicWeb3API{stack}
  32. }
  33. // ClientVersion returns the node name
  34. func (s *PublicWeb3API) ClientVersion() string {
  35. return s.stack.Server().Name
  36. }
  37. // Sha3 applies the ethereum sha3 implementation on the input.
  38. // It assumes the input is hex encoded.
  39. func (s *PublicWeb3API) Sha3(input string) string {
  40. return common.ToHex(crypto.Sha3(common.FromHex(input)))
  41. }
  42. // PublicNetAPI offers network related RPC methods
  43. type PublicNetAPI struct {
  44. net *p2p.Server
  45. networkVersion int
  46. }
  47. // NewPublicNetAPI creates a new net api instance.
  48. func NewPublicNetAPI(net *p2p.Server, networkVersion int) *PublicNetAPI {
  49. return &PublicNetAPI{net, networkVersion}
  50. }
  51. // Listening returns an indication if the node is listening for network connections.
  52. func (s *PublicNetAPI) Listening() bool {
  53. return true // always listening
  54. }
  55. // Peercount returns the number of connected peers
  56. func (s *PublicNetAPI) PeerCount() *rpc.HexNumber {
  57. return rpc.NewHexNumber(s.net.PeerCount())
  58. }
  59. // ProtocolVersion returns the current ethereum protocol version.
  60. func (s *PublicNetAPI) Version() string {
  61. return fmt.Sprintf("%d", s.networkVersion)
  62. }