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

     1  package accounts
     2  
     3  import (
     4  	"github.com/ethereum/go-ethereum/event"
     5  	"github.com/ethereum/go-ethereum/p2p"
     6  	"github.com/ethereum/go-ethereum/rpc"
     7  	"github.com/status-im/status-go/multiaccounts/settings"
     8  	"github.com/status-im/status-go/server"
     9  
    10  	"github.com/status-im/status-go/account"
    11  	"github.com/status-im/status-go/multiaccounts"
    12  	"github.com/status-im/status-go/multiaccounts/accounts"
    13  	"github.com/status-im/status-go/params"
    14  	"github.com/status-im/status-go/protocol"
    15  )
    16  
    17  // NewService initializes service instance.
    18  func NewService(db *accounts.Database, mdb *multiaccounts.Database, manager *account.GethManager, config *params.NodeConfig, feed *event.Feed, mediaServer *server.MediaServer) *Service {
    19  	return &Service{db, mdb, manager, config, feed, nil, mediaServer}
    20  }
    21  
    22  // Service is a browsers service.
    23  type Service struct {
    24  	db          *accounts.Database
    25  	mdb         *multiaccounts.Database
    26  	manager     *account.GethManager
    27  	config      *params.NodeConfig
    28  	feed        *event.Feed
    29  	messenger   *protocol.Messenger
    30  	mediaServer *server.MediaServer
    31  }
    32  
    33  func (s *Service) Init(messenger *protocol.Messenger) {
    34  	s.messenger = messenger
    35  }
    36  
    37  // Start a service.
    38  func (s *Service) Start() error {
    39  	return s.manager.InitKeystore(s.config.KeyStoreDir)
    40  }
    41  
    42  // Stop a service.
    43  func (s *Service) Stop() error {
    44  	return nil
    45  }
    46  
    47  // APIs returns list of available RPC APIs.
    48  func (s *Service) APIs() []rpc.API {
    49  	return []rpc.API{
    50  		{
    51  			Namespace: "settings",
    52  			Version:   "0.1.0",
    53  			Service:   NewSettingsAPI(&s.messenger, s.db, s.config),
    54  		},
    55  		{
    56  			Namespace: "accounts",
    57  			Version:   "0.1.0",
    58  			Service:   s.AccountsAPI(),
    59  		},
    60  		{
    61  			Namespace: "multiaccounts",
    62  			Version:   "0.1.0",
    63  			Service:   NewMultiAccountsAPI(s.mdb, s.mediaServer),
    64  		},
    65  	}
    66  }
    67  
    68  func (s *Service) AccountsAPI() *API {
    69  	return NewAccountsAPI(s.manager, s.config, s.db, s.feed, &s.messenger)
    70  }
    71  
    72  // Protocols returns list of p2p protocols.
    73  func (s *Service) Protocols() []p2p.Protocol {
    74  	return nil
    75  }
    76  
    77  func (s *Service) GetKeypairByKeyUID(keyUID string) (*accounts.Keypair, error) {
    78  
    79  	return s.db.GetKeypairByKeyUID(keyUID)
    80  }
    81  
    82  func (s *Service) GetSettings() (settings.Settings, error) {
    83  	return s.db.GetSettings()
    84  }
    85  
    86  func (s *Service) GetMessenger() *protocol.Messenger {
    87  	return s.messenger
    88  }
    89  
    90  func (s *Service) VerifyPassword(password string) bool {
    91  	address, err := s.db.GetChatAddress()
    92  	if err != nil {
    93  		return false
    94  	}
    95  	_, err = s.manager.VerifyAccountPassword(s.config.KeyStoreDir, address.Hex(), password)
    96  	return err == nil
    97  }