2019-06-14 10:16:30 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewService initializes service instance.
|
2019-07-25 05:35:09 +00:00
|
|
|
func NewService(db *Database) *Service {
|
2019-06-14 10:16:30 +00:00
|
|
|
feed := &event.Feed{}
|
|
|
|
return &Service{
|
2019-07-25 05:35:09 +00:00
|
|
|
db: db,
|
2019-06-14 10:16:30 +00:00
|
|
|
feed: feed,
|
|
|
|
signals: &SignalsTransmitter{publisher: feed},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service is a wallet service.
|
|
|
|
type Service struct {
|
|
|
|
feed *event.Feed
|
|
|
|
db *Database
|
|
|
|
reactor *Reactor
|
|
|
|
signals *SignalsTransmitter
|
2019-07-02 07:28:57 +00:00
|
|
|
client *ethclient.Client
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start signals transmitter.
|
|
|
|
func (s *Service) Start(*p2p.Server) error {
|
|
|
|
return s.signals.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartReactor separately because it requires known ethereum address, which will become available only after login.
|
2019-07-25 05:35:09 +00:00
|
|
|
func (s *Service) StartReactor(client *ethclient.Client, accounts []common.Address, chain *big.Int) error {
|
|
|
|
reactor := NewReactor(s.db, s.feed, client, accounts, chain)
|
|
|
|
err := reactor.Start()
|
2019-06-14 10:16:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.reactor = reactor
|
2019-07-02 07:28:57 +00:00
|
|
|
s.client = client
|
2019-06-14 10:16:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopReactor stops reactor and closes database.
|
|
|
|
func (s *Service) StopReactor() error {
|
|
|
|
if s.reactor == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.reactor.Stop()
|
2019-07-25 05:35:09 +00:00
|
|
|
return nil
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop reactor, signals transmitter and close db.
|
|
|
|
func (s *Service) Stop() error {
|
|
|
|
log.Info("wallet will be stopped")
|
|
|
|
err := s.StopReactor()
|
|
|
|
s.signals.Stop()
|
|
|
|
log.Info("wallet stopped")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIs returns list of available RPC APIs.
|
|
|
|
func (s *Service) APIs() []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: "wallet",
|
|
|
|
Version: "0.1.0",
|
|
|
|
Service: NewAPI(s),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocols returns list of p2p protocols.
|
|
|
|
func (s *Service) Protocols() []p2p.Protocol {
|
|
|
|
return nil
|
|
|
|
}
|