-
Notifications
You must be signed in to change notification settings - Fork 179
/
config.go
134 lines (114 loc) · 3.82 KB
/
config.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
package rpc
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/okex/exchain/libs/cosmos-sdk/client/flags"
"github.com/okex/exchain/libs/cosmos-sdk/client/input"
"github.com/okex/exchain/libs/cosmos-sdk/client/lcd"
"github.com/okex/exchain/libs/cosmos-sdk/crypto/keys"
cmserver "github.com/okex/exchain/libs/cosmos-sdk/server"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/okex/exchain/app/crypto/ethsecp256k1"
"github.com/okex/exchain/app/crypto/hd"
"github.com/okex/exchain/app/rpc/pendingtx"
"github.com/okex/exchain/app/rpc/websockets"
"github.com/spf13/viper"
)
const (
flagUnlockKey = "unlock-key"
flagWebsocket = "wsport"
FlagPersonalAPI = "personal-api"
FlagRateLimitAPI = "rpc.rate-limit-api"
FlagRateLimitCount = "rpc.rate-limit-count"
FlagRateLimitBurst = "rpc.rate-limit-burst"
FlagEnableMonitor = "rpc.enable-monitor"
FlagDisableAPI = "rpc.disable-api"
FlagKafkaAddr = "pendingtx.kafka-addr"
FlagKafkaTopic = "pendingtx.kafka-topic"
MetricsNamespace = "x"
// MetricsSubsystem is a subsystem shared by all metrics exposed by this package.
MetricsSubsystem = "rpc"
MetricsFieldName = "Metrics"
)
// RegisterRoutes creates a new server and registers the `/rpc` endpoint.
// Rpc calls are enabled based on their associated module (eg. "eth").
func RegisterRoutes(rs *lcd.RestServer) {
server := rpc.NewServer()
accountName := viper.GetString(cmserver.FlagUlockKey)
accountNames := strings.Split(accountName, ",")
var privkeys []ethsecp256k1.PrivKey
if len(accountName) > 0 {
var err error
inBuf := bufio.NewReader(os.Stdin)
keyringBackend := viper.GetString(flags.FlagKeyringBackend)
passphrase := ""
switch keyringBackend {
case keys.BackendOS:
break
case keys.BackendFile:
passphrase, err = input.GetPassword(
"Enter password to unlock key for RPC API: ",
inBuf)
if err != nil {
panic(err)
}
}
privkeys, err = unlockKeyFromNameAndPassphrase(accountNames, passphrase)
if err != nil {
panic(err)
}
}
apis := GetAPIs(rs.CliCtx, rs.Logger(), privkeys...)
// Register all the APIs exposed by the namespace services
// TODO: handle allowlist and private APIs
for _, api := range apis {
if err := server.RegisterName(api.Namespace, api.Service); err != nil {
panic(err)
}
}
// Web3 RPC API route
rs.Mux.HandleFunc("/", server.ServeHTTP).Methods("POST", "OPTIONS")
// start websockets server
websocketAddr := viper.GetString(flagWebsocket)
ws := websockets.NewServer(rs.CliCtx, rs.Logger(), websocketAddr)
ws.Start()
// pending tx watcher
kafkaAddrs := viper.GetString(FlagKafkaAddr)
kafkaTopic := viper.GetString(FlagKafkaTopic)
if kafkaAddrs != "" && kafkaTopic != "" {
kafkaClient := pendingtx.NewKafkaClient(strings.Split(kafkaAddrs, ","), kafkaTopic)
ptw := pendingtx.NewWatcher(rs.CliCtx, rs.Logger(), kafkaClient)
ptw.Start()
}
}
func unlockKeyFromNameAndPassphrase(accountNames []string, passphrase string) ([]ethsecp256k1.PrivKey, error) {
keybase, err := keys.NewKeyring(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(cmserver.FlagUlockKeyHome),
os.Stdin,
hd.EthSecp256k1Options()...,
)
if err != nil {
return []ethsecp256k1.PrivKey{}, err
}
// try the for loop with array []string accountNames
// run through the bottom code inside the for loop
keys := make([]ethsecp256k1.PrivKey, len(accountNames))
for i, acc := range accountNames {
// With keyring keybase, password is not required as it is pulled from the OS prompt
privKey, err := keybase.ExportPrivateKeyObject(acc, passphrase)
if err != nil {
return []ethsecp256k1.PrivKey{}, err
}
var ok bool
keys[i], ok = privKey.(ethsecp256k1.PrivKey)
if !ok {
panic(fmt.Sprintf("invalid private key type %T at index %d", privKey, i))
}
}
return keys, nil
}