forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walletextension_container.go
143 lines (123 loc) · 3.93 KB
/
walletextension_container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package walletextension
import (
"net/http"
"os"
"time"
"github.com/ten-protocol/go-ten/go/common/subscription"
"github.com/ten-protocol/go-ten/tools/walletextension/api"
"github.com/ten-protocol/go-ten/tools/walletextension/httpapi"
"github.com/ten-protocol/go-ten/tools/walletextension/rpcapi"
"github.com/ten-protocol/go-ten/lib/gethfork/node"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/stopcontrol"
gethrpc "github.com/ten-protocol/go-ten/lib/gethfork/rpc"
wecommon "github.com/ten-protocol/go-ten/tools/walletextension/common"
"github.com/ten-protocol/go-ten/tools/walletextension/storage"
)
type Container struct {
stopControl *stopcontrol.StopControl
logger gethlog.Logger
rpcServer node.Server
services *rpcapi.Services
newHeadsService *subscription.NewHeadsService
}
func NewContainerFromConfig(config wecommon.Config, logger gethlog.Logger) *Container {
// create the account manager with a single unauthenticated connection
hostRPCBindAddrWS := wecommon.WSProtocol + config.NodeRPCWebsocketAddress
hostRPCBindAddrHTTP := wecommon.HTTPProtocol + config.NodeRPCHTTPAddress
// start the database
databaseStorage, err := storage.New(config.DBType, config.DBConnectionURL, config.DBPathOverride)
if err != nil {
logger.Crit("unable to create database to store viewing keys ", log.ErrKey, err)
os.Exit(1)
}
// captures version in the env vars
version := os.Getenv("OBSCURO_GATEWAY_VERSION")
if version == "" {
version = "dev"
}
stopControl := stopcontrol.New()
walletExt := rpcapi.NewServices(hostRPCBindAddrHTTP, hostRPCBindAddrWS, databaseStorage, stopControl, version, logger, &config)
cfg := &node.RPCConfig{
EnableHTTP: true,
HTTPPort: config.WalletExtensionPortHTTP,
EnableWs: true,
WsPort: config.WalletExtensionPortWS,
WsPath: wecommon.APIVersion1 + "/",
HTTPPath: wecommon.APIVersion1 + "/",
Host: config.WalletExtensionHost,
}
rpcServer := node.NewServer(cfg, logger)
rpcServer.RegisterRoutes(httpapi.NewHTTPRoutes(walletExt))
// register all RPC endpoints exposed by a typical Geth node
rpcServer.RegisterAPIs([]gethrpc.API{
{
Namespace: "eth",
Service: rpcapi.NewEthereumAPI(walletExt),
}, {
Namespace: "eth",
Service: rpcapi.NewBlockChainAPI(walletExt),
}, {
Namespace: "eth",
Service: rpcapi.NewTransactionAPI(walletExt),
}, {
Namespace: "txpool",
Service: rpcapi.NewTxPoolAPI(walletExt),
}, {
Namespace: "debug",
Service: rpcapi.NewDebugAPI(walletExt),
}, {
Namespace: "eth",
Service: rpcapi.NewFilterAPI(walletExt),
}, {
Namespace: "net",
Service: rpcapi.NewNetAPI(walletExt),
}, {
Namespace: "web3",
Service: rpcapi.NewWeb3API(walletExt),
},
})
// register the static files
// todo - remove this when the frontend is no longer served from the enclave
staticHandler := api.StaticFilesHandler(wecommon.PathStatic)
rpcServer.RegisterRoutes([]node.Route{{
Name: wecommon.PathStatic,
Func: func(resp http.ResponseWriter, req *http.Request) {
staticHandler.ServeHTTP(resp, req)
},
}})
return &Container{
stopControl: stopControl,
rpcServer: rpcServer,
newHeadsService: walletExt.NewHeadsService,
services: walletExt,
logger: logger,
}
}
// Start starts the wallet extension container
func (w *Container) Start() error {
err := w.newHeadsService.Start()
if err != nil {
return err
}
err = w.rpcServer.Start()
if err != nil {
return err
}
return nil
}
func (w *Container) Stop() error {
w.stopControl.Stop()
_ = w.newHeadsService.Stop()
if w.rpcServer != nil {
// rpc server cannot be stopped synchronously as it will kill current request
go func() {
// make sure it's not killing the connection before returning the response
time.Sleep(time.Second) // todo review this sleep
w.rpcServer.Stop()
}()
}
w.services.Stop()
return nil
}