-
Notifications
You must be signed in to change notification settings - Fork 182
/
config.go
110 lines (93 loc) · 2.87 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
package rpc
import (
"bufio"
"fmt"
"os"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/crypto/keys"
cmserver "github.com/cosmos/cosmos-sdk/server"
"github.com/ethereum/go-ethereum/rpc"
"github.com/okex/okexchain/app/crypto/ethsecp256k1"
"github.com/okex/okexchain/app/crypto/hd"
"github.com/okex/okexchain/app/rpc/websockets"
)
const (
flagUnlockKey = "unlock-key"
flagWebsocket = "wsport"
)
// 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, 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, websocketAddr)
ws.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
}