github.com/status-im/status-go@v1.1.0/services/status/service.go (about)

     1  package status
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  
     7  	"github.com/ethereum/go-ethereum/node"
     8  	"github.com/ethereum/go-ethereum/p2p"
     9  	"github.com/ethereum/go-ethereum/rpc"
    10  
    11  	"github.com/status-im/status-go/eth-node/types"
    12  	"github.com/status-im/status-go/protocol"
    13  	"github.com/status-im/status-go/protocol/common/shard"
    14  )
    15  
    16  // Make sure that Service implements node.Lifecycle interface.
    17  var _ node.Lifecycle = (*Service)(nil)
    18  var ErrNotInitialized = errors.New("status public api not initialized")
    19  
    20  // Service represents out own implementation of personal sign operations.
    21  type Service struct {
    22  	messenger *protocol.Messenger
    23  }
    24  
    25  // New returns a new Service.
    26  func New() *Service {
    27  	return &Service{}
    28  }
    29  
    30  func (s *Service) Init(messenger *protocol.Messenger) {
    31  	s.messenger = messenger
    32  }
    33  
    34  // Protocols returns a new protocols list. In this case, there are none.
    35  func (s *Service) Protocols() []p2p.Protocol {
    36  	return []p2p.Protocol{}
    37  }
    38  
    39  // APIs returns a list of new APIs.
    40  func (s *Service) APIs() []rpc.API {
    41  	return []rpc.API{
    42  		{
    43  			Namespace: "status",
    44  			Version:   "1.0",
    45  			Service:   NewPublicAPI(s),
    46  			Public:    true,
    47  		},
    48  	}
    49  }
    50  
    51  // NewPublicAPI returns a reference to the PublicAPI object
    52  func NewPublicAPI(s *Service) *PublicAPI {
    53  	api := &PublicAPI{
    54  		service: s,
    55  	}
    56  	return api
    57  }
    58  
    59  // Start is run when a service is started.
    60  func (s *Service) Start() error {
    61  	return nil
    62  }
    63  
    64  // Stop is run when a service is stopped.
    65  func (s *Service) Stop() error {
    66  	return nil
    67  }
    68  
    69  type PublicAPI struct {
    70  	service *Service
    71  }
    72  
    73  func (p *PublicAPI) CommunityInfo(communityID types.HexBytes, shard *shard.Shard) (json.RawMessage, error) {
    74  	if p.service.messenger == nil {
    75  		return nil, ErrNotInitialized
    76  	}
    77  
    78  	community, err := p.service.messenger.FetchCommunity(&protocol.FetchCommunityRequest{
    79  		CommunityKey:    communityID.String(),
    80  		Shard:           shard,
    81  		TryDatabase:     true,
    82  		WaitForResponse: true,
    83  	})
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return community.MarshalPublicAPIJSON()
    89  }